getFont method

Future<Font> getFont({
  1. PdfBaseCache? pdfCache,
  2. bool protect = false,
  3. Map<String, String>? headers,
  4. String assetPrefix = 'google_fonts/',
  5. AssetBundle? bundle,
  6. bool cache = true,
})
inherited

Get the font to use in a Pdf document

Implementation

Future<Font> getFont({
  PdfBaseCache? pdfCache,
  bool protect = false,
  Map<String, String>? headers,
  String assetPrefix = 'google_fonts/',
  AssetBundle? bundle,
  bool cache = true,
}) async {
  final asset = '$assetPrefix$name.ttf';
  if (await manifest.AssetManifest.contains(asset)) {
    bundle ??= rootBundle;
    final data = await bundle.load(asset);
    return TtfFont(
      data,
      protect: protect,
    );
  }

  pdfCache ??= PdfBaseCache.defaultCache;

  try {
    final bytes = await pdfCache.resolve(
      name: name,
      uri: Uri.parse(url),
      headers: headers,
      cache: cache,
    );

    return TtfFont(
      bytes.buffer.asByteData(bytes.offsetInBytes, bytes.lengthInBytes),
      protect: protect,
    );
  } catch (e) {
    assert(() {
      // ignore: avoid_print
      print('$e\nError loading $name, fallback to Helvetica.');
      return true;
    }());

    return Font.helvetica();
  }
}