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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
13 changes: 11 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.customFonts,
});

final SvgParser parser;

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

final PdfRect boundingBox;

final SvgCustomFonts? customFonts;

void paint() {
final brush = parser.colorFilter == null
? SvgBrush.defaultContext
Expand All @@ -59,6 +63,11 @@ class SvgPainter {
}

Font getFont(String fontFamily, String fontStyle, String fontWeight) {
final customFont = customFonts?.getFont(fontFamily, fontStyle, fontWeight);
if (customFont != null) {
return customFont;
}

switch (fontFamily) {
case 'serif':
switch (fontStyle) {
Expand Down
10 changes: 10 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,
SvgCustomFonts? customFonts,
}) {
final xml = XmlDocument.parse(svg);
final parser = SvgParser(
Expand All @@ -46,6 +47,7 @@ class SvgImage extends Widget {
clip,
width,
height,
customFonts,
);
}

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

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

final double? height;

final SvgCustomFonts? customFonts;

late FittedSizes sizes;

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

abstract class SvgCustomFonts {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just a function?

Font? getFont(String fontFamily, String fontStyle, String fontWeight);
}
19 changes: 19 additions & 0 deletions pdf/test/widget_svg_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ void main() {
);
});

test('SVG Widgets Text custom fonts', () {
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="dodgerblue" font-family="myfont" >Hello, PDF</text><rect x="1" y="1" width="998" height="298" fill="none" stroke="purple" stroke-width="2" /></svg>',
customFonts: _CustomFonts(),
),
),
);
});

test('SVG Widgets Barcode', () {
pdf.addPage(
Page(
Expand Down Expand Up @@ -200,3 +212,10 @@ void main() {
await file.writeAsBytes(await pdf.save());
});
}

class _CustomFonts implements SvgCustomFonts {
@override
Font? getFont(String fontFamily, String fontStyle, String fontWeight) {
return null;
}
}
Loading