Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom fonts in SVGs. #1742

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions pdf/lib/src/svg/painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import '../../pdf.dart';
import '../widgets/font.dart';
import '../widgets/svg.dart';
import 'brush.dart';
import 'color.dart';
import 'group.dart';
Expand All @@ -26,8 +27,9 @@ class SvgPainter {
this.parser,
this._canvas,
this.document,
this.boundingBox,
);
this.boundingBox, {
this.customFontLookup,
});

final SvgParser parser;

Expand All @@ -37,6 +39,8 @@ class SvgPainter {

final PdfRect boundingBox;

final SvgCustomFontLookup? customFontLookup;

void paint() {
final brush = parser.colorFilter == null
? SvgBrush.defaultContext
Expand All @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions pdf/lib/src/widgets/svg.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class SvgImage extends Widget {
double? width,
double? height,
PdfColor? colorFilter,
SvgCustomFontLookup? customFontLookup,
}) {
final xml = XmlDocument.parse(svg);
final parser = SvgParser(
Expand All @@ -46,6 +47,7 @@ class SvgImage extends Widget {
clip,
width,
height,
customFontLookup,
);
}

Expand All @@ -56,6 +58,7 @@ class SvgImage extends Widget {
this.clip,
this.width,
this.height,
this.customFontLookup,
);

final SvgParser _svgParser;
Expand All @@ -70,6 +73,8 @@ class SvgImage extends Widget {

final double? height;

final SvgCustomFontLookup? customFontLookup;

late FittedSizes sizes;

@override
Expand Down Expand Up @@ -126,6 +131,7 @@ class SvgImage extends Widget {
context.page.pageFormat.width,
context.page.pageFormat.height,
),
customFontLookup: customFontLookup,
);
painter.paint();
context.canvas.restoreContext();
Expand Down Expand Up @@ -154,3 +160,6 @@ class DecorationSvgImage extends DecorationGraphic {
);
}
}

typedef SvgCustomFontLookup = Font? Function(
String fontFamily, String fontStyle, String fontWeight);
27 changes: 27 additions & 0 deletions pdf/test/widget_svg_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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:
'<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg viewBox="0 0 1000 300" xmlns="http://www.w3.org/2000/svg" version="1.1"><text x="367.055" y="168.954" font-size="55" fill="darkgreen" font-family="$customFamilyName1" >Custom <tspan font-family="$customFamilyName2">fonts</tspan></text><rect x="1" y="1" width="998" height="298" fill="none" stroke="purple" stroke-width="2" /></svg>',
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(
Expand Down
Loading