Documentation
¶
Overview ¶
Package pdfsvg is a port of the Rust crate krilla-svg, from the base commit e3459d06 of krilla (D62). It draws a tree of the package svg onto a surface of the package pdf. docs/STATUS.md names the last commit of krilla that it follows.
It passes nearly the whole test suite of resvg. A program parses an SVG file with svg, and draws the tree onto a surface:
tree, err := svg.FromData(data)
if err != nil {
return err
}
d := pdf.NewDocument(pdf.WithSVGGlyphs(pdfsvg.RenderGlyph))
ps, _ := pdf.PageSettingsFromWH(tree.Size().Width(), tree.Size().Height())
page := d.StartPage(ps)
pdfsvg.Draw(page.Surface(), tree, tree.Size())
page.Finish()
return d.Finish(w)
RenderGlyph draws the SVG glyphs of a font, and pdf.WithSVGGlyphs takes it. pdfsvg imports pdf and svg, so a program that writes PDF without SVG does not link svg (D62).
The state of the port ¶
Phase 17 of "The PDF writer" in docs/PLAN.md ports the 9 files of krilla-svg 0.8.1. Each file of src is a file of the same name, and lib.rs is lib.go. Draw is SurfaceExt::draw_svg, the options WithEmbedText and WithFilterScale are the fields of SvgSettings, and RenderGlyph is render_svg_glyph. Each function named render in a module of upstream takes the name of its module as a suffix, such as renderGroup and renderClipPath (D10).
The differences from krilla-svg ¶
krilla-svg 0.8.1 requires usvg 0.47. The tree of svg follows a later usvg, after 0.49 (D62). Each change that the newer tree needs has a comment that starts with "okage:":
- The layer bounding box of a group is optional. A group without one has no content, and the raster fallback of filter.go draws nothing for it.
- usvg has the blend mode PlusLighter, which PDF does not have. A group blends with the groups before it, up to the closest group that svg isolates, or up to the root of a tree or of a subtree. When a group in that range has PlusLighter, the whole range is drawn with svg into an image, with the code of filter.go. The image holds the opacity, the clip path and the mask of the range. renderRoot draws a root, where upstream calls render of group.rs.
- usvg does not give the crate fontdb to its users. The font database of the tree is the FontDatabase of svg, and the tags of the axes of a variable font come from its table fvar, where upstream reads them with skrifa.
The port also differs in these ways:
- resvg draws a filter in upstream. svg draws it in the port, so the pixels of the image are the pixels of svg (D62, item 10). svg follows the commit d950e16b of resvg, which draws the filters with filtra and gives usvg the filter tree of filtra_types. pdfsvg reads that tree with the same code, and the pixels of a filter differ from the pixels of resvg 0.47, which krilla-svg uses (D108).
- Draw and RenderGlyph return false where upstream panics: for a tree whose size gives no rectangle, and for a glyph whose units per em are not positive.
- pdf and svg take the Transform, the Path and the Rect of the package path, so the conversions of util.rs between the types of krilla and of usvg have no port, except PathExt::to_krilla, which builds the path again from its segments, as upstream does.
- convertPaint takes the surface, and makes a StreamBuilder only for a pattern. Upstream makes one for each paint, and a StreamBuilder that is not finished writes nothing.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Draw ¶
Draw is SurfaceExt::draw_svg in krilla-svg src/lib.rs. It returns false where upstream returns None. Upstream always returns Some, and so does Draw, except for a tree whose size gives no rectangle, where upstream panics.
Draw a tree of svg onto a surface with the given size and settings.
func RenderGlyph ¶
func RenderGlyph(data []byte, contextColor pdf.ColorRGB, glyph pdf.GlyphID, width, height float32, surface *pdf.Surface) bool
RenderGlyph is render_svg_glyph in krilla-svg src/lib.rs. It has the type pdf.SVGGlyphFunc, and a program gives it to pdf.WithSVGGlyphs. It returns false where upstream returns None. Upstream panics where width or height is not positive, and RenderGlyph returns false.
Render an SVG glyph from an OpenType font into a surface. You can plug this function into pdf.WithSVGGlyphs.
Types ¶
type Option ¶
type Option func(*svgSettings)
Option is an option of Draw. Each option sets a field of SvgSettings of krilla-svg (D20).
func WithEmbedText ¶
WithEmbedText is the field embed_text of SvgSettings in krilla-svg src/lib.rs. The default is true.
Whether text should be embedded as properly selectable text. Otherwise, it will be drawn as outlined paths instead.
func WithFilterScale ¶
WithFilterScale is the field filter_scale of SvgSettings in krilla-svg src/lib.rs. The default is 4. The raster fallback of a group that PDF cannot express takes the same scale (D62).
How much filters, which will be converted to bitmaps, should be scaled. Higher values mean better quality, but also bigger file sizes.