diff --git a/pdf/lib/src/svg/painter.dart b/pdf/lib/src/svg/painter.dart index 3f4cd0c4..61d00049 100644 --- a/pdf/lib/src/svg/painter.dart +++ b/pdf/lib/src/svg/painter.dart @@ -16,6 +16,7 @@ import '../../pdf.dart'; import '../widgets/font.dart'; +import '../widgets/svg.dart'; import 'brush.dart'; import 'color.dart'; import 'group.dart'; @@ -26,8 +27,9 @@ class SvgPainter { this.parser, this._canvas, this.document, - this.boundingBox, - ); + this.boundingBox, { + this.customFontLookup, + }); final SvgParser parser; @@ -37,6 +39,8 @@ class SvgPainter { final PdfRect boundingBox; + final SvgCustomFontLookup? customFontLookup; + void paint() { final brush = parser.colorFilter == null ? SvgBrush.defaultContext @@ -59,6 +63,12 @@ class SvgPainter { } Font getFont(String fontFamily, String fontStyle, String fontWeight) { + final customFont = + customFontLookup?.call(fontFamily, fontStyle, fontWeight); + if (customFont != null) { + return customFont; + } + switch (fontFamily) { case 'serif': switch (fontStyle) { diff --git a/pdf/lib/src/widgets/svg.dart b/pdf/lib/src/widgets/svg.dart index d6968c81..6ccc4e58 100644 --- a/pdf/lib/src/widgets/svg.dart +++ b/pdf/lib/src/widgets/svg.dart @@ -32,6 +32,7 @@ class SvgImage extends Widget { double? width, double? height, PdfColor? colorFilter, + SvgCustomFontLookup? customFontLookup, }) { final xml = XmlDocument.parse(svg); final parser = SvgParser( @@ -46,6 +47,7 @@ class SvgImage extends Widget { clip, width, height, + customFontLookup, ); } @@ -56,6 +58,7 @@ class SvgImage extends Widget { this.clip, this.width, this.height, + this.customFontLookup, ); final SvgParser _svgParser; @@ -70,6 +73,8 @@ class SvgImage extends Widget { final double? height; + final SvgCustomFontLookup? customFontLookup; + late FittedSizes sizes; @override @@ -126,6 +131,7 @@ class SvgImage extends Widget { context.page.pageFormat.width, context.page.pageFormat.height, ), + customFontLookup: customFontLookup, ); painter.paint(); context.canvas.restoreContext(); @@ -154,3 +160,6 @@ class DecorationSvgImage extends DecorationGraphic { ); } } + +typedef SvgCustomFontLookup = Font? Function( + String fontFamily, String fontStyle, String fontWeight); diff --git a/pdf/test/widget_svg_test.dart b/pdf/test/widget_svg_test.dart index 83d53230..c1fbc526 100644 --- a/pdf/test/widget_svg_test.dart +++ b/pdf/test/widget_svg_test.dart @@ -20,6 +20,8 @@ import 'package:pdf/pdf.dart'; import 'package:pdf/widgets.dart'; import 'package:test/test.dart'; +import 'utils.dart'; + late Document pdf; void main() { @@ -100,6 +102,31 @@ void main() { ); }); + test('SVG Widgets Text custom fonts', () { + const customFamilyName1 = 'Test-Font-Family1'; + const customFamilyName2 = 'Test-Font-Family2'; + final customFont1 = loadFont('open-sans-bold.ttf'); + final customFont2 = loadFont('genyomintw.ttf'); + pdf.addPage( + Page( + build: (context) => SvgImage( + svg: + 'Custom fonts', + customFontLookup: + (String fontFamily, String fontStyle, String fontWeight) { + switch (fontFamily) { + case customFamilyName1: + return customFont1; + case customFamilyName2: + return customFont2; + default: + return null; + } + }), + ), + ); + }); + test('SVG Widgets Barcode', () { pdf.addPage( Page(