libgfx 2.0.1
libgfx: ^2.0.1 copied to clipboard
A comprehensive 2D vector graphics engine for Dart with software rasterization, supporting paths, strokes, gradients, text, and advanced rendering features.
1.0.0 #
- Initial release of libgfx
- Comprehensive 2D vector graphics engine with software rasterization
- Full TrueType font support with text rendering
- Linear, radial, and conic gradients
- Advanced blend modes and image filters
- Path operations and boolean operations
- Multiple image codec support (PNG, BMP, PPM)
- Fluent API for chainable operations
- 30+ example programs demonstrating features
- Font files distributed separately via download scripts to reduce package size
1.0.1 #
- Minor upgrades
1.0.2 #
- Linting upgrade
1.0.3 #
- Update dependencies
1.0.4 #
- Maintenance and housekeeping
- Update Dart SDK constraint to ^3.8.1
- Update dev dependencies: lints ^6.0.0, test ^1.24.0
- Documentation and examples
- Improve README and examples;
- Code quality
- Apply lint fixes, minor refactors, and formatting
- Pulled SVG parsing out into the library
- Performance improvements for common operations
- Added I/O abstraction to allow the library to run in the browser
- Update documentation as appropriate
2.0.0 #
Rendering correctness, hardened parsing of untrusted fonts and images, a complete pure-Dart PNG decoder, and documentation that matches the API.
Breaking changes #
Each change is followed by how to migrate.
-
Loading returns
Resultinstead of throwing.TTFFont.fromBytesreturnsResult<TTFFont, ErrorDetails>;TTFFont.load,FontFallbackChain.load,GraphicsEngine.loadFontreturnFuture<Result<...>>;GraphicsEngine.readFontandGraphicsEngine.decodereturnResult(names as renamed below).ErrorDetails.kindis one oftruncated,malformed,unsupported,limit,ioorsingular. A failed font load keeps the current font.- Migration: replace
try/catch(or unchecked use) with a switch:switch (TTFFont.fromBytes(bytes)) { case Ok(:final value): use(value); case Err(:final error): report(error); }.
- Migration: replace
-
Matrix2D.inverse()returnsResult<Matrix2D, ErrorDetails>,Err(singular)for a non-invertible matrix;invert(), which silently reset the matrix to identity, is removed.- Migration:
if (matrix.inverse() case Ok(:final value)) { ... }; useisInvertibleto test first.
- Migration:
-
Matrix2D.decompose()returns a record({Point translation, double rotation, Point scale})instead of filling out-parameters.- Migration:
final (:translation, :rotation, :scale) = matrix.decompose();.
- Migration:
-
Pointis immutable:xandyare final and the constructor isconst.- Migration: build a new point (
Point(p.x + 1, p.y)) instead of assigningp.x.
- Migration: build a new point (
-
Paths are immutable after construction.
Path.commandsandPathCommand.pointsare unmodifiable; aPathCommandcopies the points it is given.- Migration: add commands with
add/addPathor aPathBuilderinstead ofpath.commands.add(...).
- Migration: add commands with
-
Gradients are immutable.
addColorStopreturns a new gradient of the same type instead of changing the gradient;setColorStopsandclearColorStopsare removed. Constructor stops are copied, clamped to 0..1 and sorted (stops at the same offset keep their order).- Migration:
gradient = gradient.addColorStop(0.5, color);, or pass all stops to the constructor. The engine'screateLinearGradient/createRadialGradient/createConicGradientstill return empty gradients to extend this way.
- Migration:
-
PatternPaintcopies its bitmap and transform; itspatternandtransformgetters return copies.- Migration: changing the source bitmap or matrix after construction no longer changes the paint; create a new
PatternPaintinstead.
- Migration: changing the source bitmap or matrix after construction no longer changes the paint; create a new
-
fillandclipuse the non-zero fill rule by default, as W3C canvas does (fills previously behaved as even-odd, andclipused even-odd):fill(Path path, [FillRule rule])andclip(Path path, [FillRule rule]).clipWithFillRuleis removed.- Migration: pass
FillRule.evenOddwhere shapes with holes relied on even-odd:engine.fill(path, FillRule.evenOdd),engine.clip(path, FillRule.evenOdd).
- Migration: pass
-
Text settings are saved state. Font, size, alignment and baseline are covered by
save()/restore().- Migration: settings changed inside a
save()/restore()pair no longer persist afterrestore(); set them outside the pair if they should.
- Migration: settings changed inside a
-
Resource limits.
BitmapthrowsArgumentErrorfor negative dimensions or more than 2^28 pixels; fonts over 64 MiB, composite glyphs nested deeper than 16 or larger than 65536 points, and images over the dimension and pixel limits are rejected withErr(limit); blur radii over 256 and dash patterns producing more than 1e6 pieces per path throwArgumentError.- Migration: pass a custom
Limits(GraphicsEngine(w, h, limits: ...),TTFFont.fromBytes(bytes, limits: ...),GraphicsEngine.decode(bytes, limits: ...),ImageFilters.gaussian(image, sigma, limits: ...)) to raise them.
- Migration: pass a custom
-
Names follow the W3C canvas API and single words.
GraphicsEngineuses theCanvasRenderingContext2Dnames where one exists: canvas state is set through properties,PathBuilderhasbezierCurveTo,closePath,rectandroundRect, andarctakes an end angle. Elsewhere multi-word and abbreviated names become single words. Old names are removed, not deprecated.ColorimplementsPaint, soSolidPaintis removed.PathBuilder.arctakes an end angle (it took a sweep), drawn in increasing angle unlesscounterclockwise, with at most one full turn:arc(x, y, radius, start, start + sweep)draws whatarc(x, y, radius, start, sweep)drew.ellipsefollows the same rules.createConicGradient(startAngle, x, y)takes its arguments in W3C order (it tookcx, cy, angle).- Shapes moved to
PathBuilder: thefill/strokepairs for round rectangles, circles, ellipses, polygons and stars, and the staticcreate*Pathmethods, are removed. Build the shape and fill it:engine.fill(PathBuilder().roundRect(x, y, width, height, radius).build()).fillRect,strokeRect,clearRectandclipRectremain. drawImage(image, dx, dy, {width, height, source, filter})replacesdrawImageScaledanddrawImageRect, and takes theImageFilterused to sample the image (nearest by default).ImageOptionsis removed.- Migration: rename by the table below; most changes are mechanical.
Type Old New GraphicsEngine setFillColor,setFillPaint,setFillStylefillStyle = ...setStrokeColor,setStrokePaint,setStrokeStylestrokeStyle = ...setLineWidth,setLineCap,setLineJoin,setMiterLimitlineWidth,lineCap,lineJoin,miterLimitsetGlobalAlpha,setGlobalCompositeOperationglobalAlpha,globalCompositeOperationsetFontSize,setTextAlign,setTextBaseline,currentFontfontSize,textAlign,textBaseline,fontsetFontFromFile,setFontFromBytesloadFont,readFontclipWithFillRule(path, fillRule: rule)clip(path, rule)saveToFile,getPixel,setPixel,toBitmapwrite,pixel,plot,snapshotloadImageFromBytes,saveImageToBytesdecode,encodefillAndStrokeTextfillTextthenstrokeTextPathBuilder curveTo,cubicCurveTo,closebezierCurveTo,closePathPath addCommand,exclusiveOradd,xorPaint getColorAtsampleGradientPaint withColorStop,withColorStopsaddColorStop, constructorstops:getColorAtOffset,applySpreadlookup,wrapLinearGradient startPoint,endPoint,getTAt,getTDeltaXstart,end,offset,stepColor, ColorUtils ColorUtils.parseHexColorColor.parse(returnsResult)unpremultiplyRGBA,toGrayscale,adjustBrightness,withAlphaunpremultiply,grayscale,brighten,opacitylerpRgb,bilerpColor,hslToRgb(hsl)lerp,bilerp,hsl.colorMatrix2D setIdentity,setTransform,preMultiply,transformVector,rotateZreset,assign,premultiply,direction,rotatePoint distanceTo,distanceToSquareddistance,distance2Bitmap getPixel,setPixel,blendPixelpixel,plot,blendflipHorizontal,flipVertical,withOpacity,rotateAnglemirror,flip,fade,rotatedrawHorizontalSpan,drawAntiAliasedSpan,fillSolidspan,shade,fillFont, TTFFont familyName,styleName,fullNamefamily,style,namegetGlyphIndex,getGlyphMetrics,getGlyphPath,getKerning,hasGlyphglyph,dimensions,outline,kerning,coversmeasureText,getTextPath,TTFFont.loadFromFilemeasure,path,TTFFont.loadFontFallbackChain primaryFont,addFont,addFontFromFile,removeFontprimary,add,load,removegetFontForCharacter,getFontForString,hasGlyph,splitIntoFontRunslookup,find,covers,runsmeasureText,getTextPath,SystemFontManager.loadSystemFontsmeasure,path,SystemFontManager.loadTextEngine addFont,clearCache,cacheStats,measureText,shapeTextadd,purge,stats,measure,shapegenerateTextPath,generateMultilineTextPaths,layoutMultilineText,breakLinespath,paths,wrap,splitTextShaper shapeText;ShapedGlyph.glyphId;ShapedText.totalAdvanceshape;glyph;advanceTextRenderer addFont,primaryFont,getTextPath,measureTextadd,primary,path,measurerenderUnicodeText,measureUnicodeText,getTextPathWithSpacing,getWrappedTextPaths,getCategorylayout,extent,spaced,wrap,categoryTransformUtils fitRect,positionInRect,createTRS,createSRTfit,position,compose,reversetransformBounds,transformPointsBounds,getTranslation,getRotation,getScalebounds,extent,translation,rotation,scaleImageFilters gaussianBlur,boxBlur,stackBlur,motionBlur,edgeDetect,customFiltergaussian,box,stack,motion,edges,convolveClipRegion spansByY,fillRule,clipSpans,containsPoint,calculatePathBoundsrows,rule,clip,includes,extentGraphicsFileSystem readFile,writeFileread,writeLogging setLogger(l),enableConsoleLogging(),ConsoleLogger(enableDebug:)logger = l,logger = ConsoleLogger(),ConsoleLogger(verbose:)SVG parsePaths,SVGPathDataSVGPath.parse,SVGPathTextEngine.wraploses itslineSpacingargument, which had no effect sincewrapreturns the lines as strings;TextEngine.pathskeeps its own.getImageData(x, y, width, height)andputImageData(image, dx, dy, ...)take and return anImageData(width,height, RGBA bytes), as W3C canvas does;getImageData()returned the canvas's own pixel words as bytes andputImageDatatook aUint8List.TextAligngainsstartandend, as in W3C canvas, andstartis the default alignment ofGraphicsEngine,TextEngineandTextRenderer(it wasleft): the same for left-to-right text, and right-anchored for right-to-left text.GraphicsEngine.directionandTextEngine.direction(defaultltr) set the paragraph direction;directionis saved with the state.fillandcliptake the fill rule as a named argument (fill(path, rule: FillRule.evenOdd)), so thatfillcan also taketext; W3C passes it positionally, which Dart does not allow beside a named argument. -
New runtime dependency on
metafor the@experimentalannotation.- Migration: none;
metahas no dependencies of its own.
- Migration: none;
Behaviour changes #
- Coverage and opacity: coverage masks the composited result (
lerp(dest, composite(src * alpha, dest), coverage)), soclearandsrcaffect only the covered part of an edge pixel;globalAlphascales the source instead of the coverage; clip coverage multiplies shape coverage, so anti-aliased clips have soft edges for fills and images, and images composite through a partial clip by the same rule as fills. - Anti-aliasing measures the covered area of each pixel with the fill rule applied before coverage is added up. Each pixel row is split into bands at the edge ends inside it and, where no edges cross, each band's inside area is measured exactly; rows where edges cross, or with many edge ends (lines of text), are sampled at 16 sub-rows, exact horizontally and within 1/16 of a pixel vertically. Shapes thinner than a pixel row no longer vanish, and the fill rule holds inside a pixel (shapes wound in opposite directions no longer cancel, and shapes or strokes that overlap themselves are no longer counted twice at their edges). Rasterizing costs up to about 2x more for typical shapes and about 3x for shapes with very dense edges.
- Fill rules are computed from signed edge winding, for fills and clips; non-zero now fills nested same-direction contours.
- Example scenes in
doc/examples.mdare written for Y-up graphics space. - Paints and images are sampled in graphics space (gradients and patterns are no longer mirrored vertically) and at pixel centres (a gradient across ten pixels is symmetric).
- Images paint only the pixels whose centre maps inside the source rectangle, and honour
globalAlpha. Transparent image pixels are composited too, so modes such assrc,srcInandclearempty the destination under them. - Singular transforms paint nothing.
- Dashes with round caps: overlapping caps of adjacent dashes stay filled instead of cancelling.
- Filters: soft-light follows the W3C compositing formula; stack blur preserves constant images; Sobel edge detection is independent of edge polarity.
- Fonts: glyph ids at or beyond the font's glyph count are treated as missing; glyph metrics come from the glyph
header, so measuring no longer decodes outlines. Every glyph record is read within its own
locarange and checked completely at load (contour end points, instructions, flags and coordinates, composite components), so a font that loads can draw every glyph; a malformed or truncated glyph rejects the font withErr. - Text:
TextRendererwraps on any run of whitespace, likeTextEngine; both use the same line breaker. Thetop,middle,bottom,hangingandideographicbaselines position text correctly in Y-up graphics space (text hangs below atopbaseline); they previously applied Y-down offsets. - Path operations:
union,intersection,difference,xorandoffsetare marked@experimental, and their documentation now states what they compute (edge-based approximations, not true area operations).
Added #
GraphicsEngine.textRendering(W3C canvastextRendering): unless it isgeometricPrecision,fillTextandstrokeTextmap partial coveragectoc^0.6, so unhinted glyph stems that fall between pixels read less blurred;fill(path, text: true)does the same for glyph outlines fromTextRendererorFont.path. Saved with the state.Result<T, E>(Ok/Err),ErrorDetailsandErrorKind.Limits, accepted byGraphicsEngine(and applied to the fonts it loads),TTFFont.fromBytes,TTFFont.load,FontFallbackChain.load, image loading and the blur filters.- Full PNG decoding in pure Dart: stored, fixed and dynamic Huffman compression, all five row filters, every colour type
and bit depth (1 to 16),
tRNStransparency and Adam7 interlacing, with CRC and Adler-32 checks. - BMP: top-down images (negative height) and validation of headers and payload sizes. PPM: comments and any whitespace in headers, samples scaled by maxval, 16-bit P6.
TextEngine.split(text, maxWidth, width), the shared line breaker.TTFFont.decodes, the number of glyph outlines decoded (outlines are cached).doc/rendering-model.md.- Exports that examples reached through
package:libgfx/src/...:ColorUtilsandHSLColor, the SVG path parser (SVGPath) and logging (LibgfxLogger,ConsoleLogger,SilentLogger,logger). Color.parse,PathBuilder.rect,roundRect,polygonandstar, and thefilterargument ofdrawImage.
Fixed #
- Malformed, truncated or hostile fonts and images return an
Errinstead of throwingRangeError, overflowing the stack or exhausting memory (decompression bombs, composite glyph cycles and fan-out, cmap format 12 groups covering every code point, oversized table directories). Path.boundsof an empty path no longer throws.PathBuilder.arcTocentres its arc on the bisector of the corner, as W3C canvas does, so the arc touches both lines (it was off the corner at every angle); with collinear points or no radius it draws a line to the corner.TextRenderer.layoutaligns text by its width at the size passed to it (it used the renderer's size), andwrapandspacedalign once (centred and right-aligned lines moved twice, andspacedre-aligned each character).TextEngine.pathsandTextRenderer.wrapput each wrapped line below the one before (in Y-up graphics space they went up the page).Rectangle<int>.fromCenter,centerXandcenterYno longer throw (they halve integer sizes by flooring), nor doesRectangle.zero<double>().- Image data round trips:
getImageDatareturned a live view of the canvas in the machine's byte order (blue, green, red, alpha on little-endian machines), so clearing the canvas cleared the data andputImageData, which read red, green, blue, alpha, swapped red and blue.putImageDataalso read the data with the canvas width as its row length and wrote a dirty rectangle at (dx, dy) instead of its offset from it. FontFallbackChain.findlooks up the first character of text that starts outside the Basic Multilingual Plane (emoji, for example); it looked up half of a surrogate pair and found no font.- Composite glyphs apply their components' 2x2 transforms (they were skipped) and place components by matched points (they were drawn at the origin); a matched point number a glyph does not have is a malformed font. Offsets marked as scaled are transformed with the component.
SVGPath.parsereads quadratic curves (Q,T) and elliptical arcs (A), absolute and relative, including arc flags written without separators; it skipped them and their coordinates.SystemFontManager.loadlooks for fonts through the file system it is given (it checked withdart:iofirst, so a custom file system never saw the paths) and loads each name once (a name found in two directories loaded twice).- PNG decoding accepts a compressed block of literals only that declares no distance codes, as RFC 1951 allows; it was rejected as an incomplete Huffman code.
- Right-to-left text is drawn right to left: the shaper and
TextRendererorder glyphs by a simplified Unicode bidirectional algorithm (UAX #9 without explicit embeddings, isolates or bracket pairs), so Hebrew and Arabic read correctly and mixed lines keep each run in its own direction, with brackets mirrored in right-to-left runs. It drew every character left to right in the order stored. - Arabic letters join: each takes the isolated, final, initial or medial form its neighbours call for (marks do not break joining), and lam followed by alef becomes its ligature, drawn with the font's Arabic presentation forms when it maps them. Every letter was drawn in its isolated form.
- Characters beyond the Basic Multilingual Plane (emoji, for example) map in fonts that list a BMP-only cmap subtable before their full-repertoire one, as most fonts do: the first Unicode subtable was used, so they never mapped.
- Text uses the font's ligatures: the GSUB ligature lookups of the
ccmpandligafeatures (default script and language), so flags and other emoji sequences draw as one glyph and Latin text takes ligatures such asfiandffwhere the font has them.Font.ligateexposes them; a GSUB that is malformed or larger than the newLimits.ligaturesis ignored and the font loads without ligatures. - Path simplification no longer overflows the stack on long paths.
- Text wrapping is linear in the line length instead of quadratic.
- Loading 8000 one-byte PNG IDAT chunks is linear instead of quadratic.
- Box blur takes the same time at any radius (it grew with the radius squared); merging overlapping spans is linear
in their number (it was quadratic); gradient colour lookup is a binary search over the stops; an opaque source
drawn with
srcOveris written directly, and bilinear image sampling reads pixels without bounds checks. Output is unchanged. - Internal
save()/restore()pairs are exception safe. - The README and the guides in
doc/match the API; their code blocks are compiled by the test suite.
2.0.1 #
- pub.dev formatting cleanup.