context.nim -> contexts.nim, pixieDebugSvg

This commit is contained in:
Ryan Oldenburg 2021-06-11 22:03:37 -05:00
parent c93726dd86
commit 7dfea4dc39
6 changed files with 55 additions and 15 deletions

View file

@ -1,9 +1,9 @@
import bumpy, chroma, flatty/binny, os, pixie/blends, pixie/common,
pixie/context, pixie/fileformats/bmp, pixie/fileformats/gif,
pixie/contexts, pixie/fileformats/bmp, pixie/fileformats/gif,
pixie/fileformats/jpg, pixie/fileformats/png, pixie/fileformats/svg,
pixie/fonts, pixie/images, pixie/masks, pixie/paints, pixie/paths, strutils, vmath
export blends, bumpy, chroma, common, context, fonts, images, masks, paints,
export blends, bumpy, chroma, common, contexts, fonts, images, masks, paints,
paths, vmath
type

View file

@ -8,6 +8,7 @@ const
svgSignature* = "<svg"
type Ctx = object
display: bool
fillRule: WindingRule
fill, stroke: ColorRGBX
strokeWidth: float32
@ -27,6 +28,7 @@ proc attrOrDefault(node: XmlNode, name, default: string): string =
result = default
proc initCtx(): Ctx =
result.display = true
result.fill = parseHtmlColor("black").rgbx
result.stroke = parseHtmlColor("black").rgbx
result.strokeWidth = 1
@ -54,6 +56,23 @@ proc decodeCtx(inherited: Ctx, node: XmlNode): Ctx =
strokeDashArray = node.attr("stroke-dasharray")
transform = node.attr("transform")
style = node.attr("style")
display = node.attr("display")
when defined(pixieDebugSvg):
proc maybeLogPair(k, v: string) =
if k notin [ # Handled, never need to log
"fill-rule", "fill", "stroke", "stroke-width", "stroke-linecap",
"stroke-linejoin", "stroke-miterlimit", "stroke-dasharray",
"transform", "style", "version", "viewBox", "width", "height",
"xmlns", "x", "y", "x1", "x2", "y1", "y2", "id", "d", "cx", "cy",
"r", "points", "rx", "ry", "enable-background", "xml:space",
"xmlns:xlink", "data-name", "role", "class"
]:
echo k, ": ", v
if node.attrs() != nil:
for k, v in node.attrs():
maybeLogPair(k, v)
let pairs = style.split(';')
for pair in pairs:
@ -61,6 +80,9 @@ proc decodeCtx(inherited: Ctx, node: XmlNode): Ctx =
if parts.len == 2:
# Do not override element properties
case parts[0].strip():
of "fill-rule":
if fillRule.len == 0:
fillRule = parts[1].strip()
of "fill":
if fill.len == 0:
fill = parts[1].strip()
@ -82,6 +104,18 @@ proc decodeCtx(inherited: Ctx, node: XmlNode): Ctx =
of "stroke-dasharray":
if strokeDashArray.len == 0:
strokeDashArray = parts[1].strip()
of "display":
if display.len == 0:
display = parts[1].strip()
else:
when defined(pixieDebugSvg):
maybeLogPair(parts[0], parts[1])
elif pair.len > 0:
when defined(pixieDebugSvg):
echo "Invalid style pair: ", pair
if display.len > 0:
result.display = display.strip() != "none"
if fillRule == "":
discard # Inherit
@ -232,19 +266,21 @@ proc decodeCtx(inherited: Ctx, node: XmlNode): Ctx =
failInvalidTransform(transform)
proc fill(img: Image, ctx: Ctx, path: Path) {.inline.} =
img.fillPath(path, ctx.fill, ctx.transform, ctx.fillRule)
if ctx.display:
img.fillPath(path, ctx.fill, ctx.transform, ctx.fillRule)
proc stroke(img: Image, ctx: Ctx, path: Path) {.inline.} =
img.strokePath(
path,
ctx.stroke,
ctx.transform,
ctx.strokeWidth,
ctx.strokeLineCap,
ctx.strokeLineJoin,
miterLimit = ctx.strokeMiterLimit,
dashes = ctx.strokeDashArray
)
if ctx.display:
img.strokePath(
path,
ctx.stroke,
ctx.transform,
ctx.strokeWidth,
ctx.strokeLineCap,
ctx.strokeLineJoin,
miterLimit = ctx.strokeMiterLimit,
dashes = ctx.strokeDashArray
)
proc draw(img: Image, node: XmlNode, ctxStack: var seq[Ctx]) =
if node.kind != xnElement:
@ -252,9 +288,13 @@ proc draw(img: Image, node: XmlNode, ctxStack: var seq[Ctx]) =
return
case node.tag:
of "title", "desc", "defs":
of "title", "desc":
discard
of "defs":
when defined(pixieDebugSvg):
echo node
of "g":
let ctx = decodeCtx(ctxStack[^1], node)
ctxStack.add(ctx)

View file

@ -1,6 +1,6 @@
import
test_bmp,
test_context,
test_contexts,
test_fonts,
test_gif,
test_images,

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.4 MiB

After

Width:  |  Height:  |  Size: 2.4 MiB