font_config_manager.dart 1.43 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import '../base/file_system.dart';
8
import '../globals.dart' as globals;
9 10 11

/// Manages a Font configuration that can be shared across multiple tests.
class FontConfigManager {
12
  Directory? _fontsDirectory;
13 14 15

  /// Returns a Font configuration that limits font fallback to the artifact
  /// cache directory.
16
  late final File fontConfigFile = (){
17 18 19 20 21 22 23 24
    final StringBuffer sb = StringBuffer();
    sb.writeln('<fontconfig>');
    sb.writeln('  <dir>${globals.cache.getCacheArtifacts().path}</dir>');
    sb.writeln('  <cachedir>/var/cache/fontconfig</cachedir>');
    sb.writeln('</fontconfig>');

    if (_fontsDirectory == null) {
      _fontsDirectory = globals.fs.systemTempDirectory.createTempSync('flutter_test_fonts.');
25
      globals.printTrace('Using this directory for fonts configuration: ${_fontsDirectory!.path}');
26 27
    }

28 29 30 31 32
    final File cachedFontConfig = globals.fs.file('${_fontsDirectory!.path}/fonts.conf');
    cachedFontConfig.createSync();
    cachedFontConfig.writeAsStringSync(sb.toString());
    return cachedFontConfig;
  }();
33 34 35

  Future<void> dispose() async {
    if (_fontsDirectory != null) {
36 37
      globals.printTrace('Deleting ${_fontsDirectory!.path}...');
      await _fontsDirectory!.delete(recursive: true);
38 39 40 41
      _fontsDirectory = null;
    }
  }
}