Merge pull request #228 from guzba/master

context -> contexts, svg display property, pixieDebugSvg
This commit is contained in:
treeform 2021-06-12 09:59:08 -07:00 committed by GitHub
commit 1c1ea24ea0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 57 additions and 17 deletions

View file

@ -8,7 +8,7 @@ srcDir = "src"
requires "nim >= 1.2.6"
requires "vmath >= 1.0.8"
requires "chroma >= 0.2.5"
requires "zippy >= 0.3.5"
requires "zippy >= 0.5.12"
requires "flatty >= 0.1.3"
requires "nimsimd >= 1.0.0"
requires "bumpy >= 1.0.3"

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

@ -113,7 +113,7 @@ proc decodeGif*(data: string): Image =
if bs.pos + bitSize.int > bs.data.len * 8: failInvalid()
var
# Read variable bits out of the table.
codeId = bs.readBits(bitSize.int).int
codeId = bs.readBits(bitSize).int
# Some time we need to carry over table information.
carryOver: seq[int]

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