getFont method
Future<Font>
getFont({
- PdfBaseCache? pdfCache,
- bool protect = false,
- Map<
String, String> ? headers, - String assetPrefix = 'google_fonts/',
- AssetBundle? bundle,
- 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();
}
}