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

fix TypeError when converting pathops.Path to svg #305

Merged
merged 1 commit into from
Aug 4, 2023
Merged
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
38 changes: 11 additions & 27 deletions src/picosvg/svg_pathops.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,34 +45,18 @@
}


def _simple_skia_to_svg(svg_cmd, points) -> SVGCommandGen:
def _skia_pts_to_svg(svg_cmd, points) -> SVGCommandGen:
# pathops.Path gives us sequences of points, flatten 'em
yield (svg_cmd, tuple(c for pt in points for c in pt))


def _qcurveto_to_svg(points) -> SVGCommandGen:
for control_pt, end_pt in pathops.decompose_quadratic_segment(points):
yield ("Q", control_pt + end_pt)


def _end_path(points) -> SVGCommandGen:
if points:
raise ValueError("endPath should have no points")
return # pytype: disable=bad-return-type
yield


_SKIA_CMD_TO_SVG_CMD = {
# simple conversions
"moveTo": functools.partial(_simple_skia_to_svg, "M"),
"lineTo": functools.partial(_simple_skia_to_svg, "L"),
"quadTo": functools.partial(_simple_skia_to_svg, "Q"),
"curveTo": functools.partial(_simple_skia_to_svg, "C"),
"closePath": functools.partial(_simple_skia_to_svg, "Z"),
# more interesting conversions
"qCurveTo": _qcurveto_to_svg,
# nop
"endPath": _end_path,
pathops.PathVerb.MOVE: functools.partial(_skia_pts_to_svg, "M"),
pathops.PathVerb.LINE: functools.partial(_skia_pts_to_svg, "L"),
pathops.PathVerb.QUAD: functools.partial(_skia_pts_to_svg, "Q"),
pathops.PathVerb.CUBIC: functools.partial(_skia_pts_to_svg, "C"),
pathops.PathVerb.CLOSE: functools.partial(_skia_pts_to_svg, "Z"),
# pathops.PathVerb.CONIC... convertConicsToQuads should have taken care of these
}

_SVG_FILL_RULE_TO_SKIA_FILL_TYPE = {
Expand All @@ -95,10 +79,10 @@ def skia_path(svg_cmds: SVGCommandSeq, fill_rule: str) -> pathops.Path:


def svg_commands(skia_path: pathops.Path) -> SVGCommandGen:
for cmd, points in skia_path.segments:
if cmd not in _SKIA_CMD_TO_SVG_CMD:
raise ValueError(f'No mapping to svg for "{cmd} {points}"')
for svg_cmd, svg_args in _SKIA_CMD_TO_SVG_CMD[cmd](points):
for verb, points in skia_path:
if verb not in _SKIA_CMD_TO_SVG_CMD:
raise ValueError(f'No mapping to svg for "{verb} {points}"')
for svg_cmd, svg_args in _SKIA_CMD_TO_SVG_CMD[verb](points):
yield (svg_cmd, svg_args)


Expand Down
2 changes: 1 addition & 1 deletion tests/arcs-nano.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/stroke-polyline-nano.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
64 changes: 48 additions & 16 deletions tests/svg_pathops_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import pytest
from picosvg import svg_pathops
from picosvg.svg_types import SVGCircle, SVGPath, SVGRect
from pathops import PathVerb


def _round(pt, digits):
Expand All @@ -27,43 +28,74 @@ def _round(pt, digits):
# path
(
SVGPath(d="M1,1 2,2 z"),
(("moveTo", ((1.0, 1.0),)), ("lineTo", ((2.0, 2.0),)), ("closePath", ())),
(
(PathVerb.MOVE, ((1.0, 1.0),)),
(PathVerb.LINE, ((2.0, 2.0),)),
(PathVerb.CLOSE, ()),
),
"M1,1 L2,2 Z",
),
# rect
(
SVGRect(x=4, y=4, width=6, height=16),
(
("moveTo", ((4.0, 4.0),)),
("lineTo", ((10.0, 4.0),)),
("lineTo", ((10.0, 20.0),)),
("lineTo", ((4.0, 20.0),)),
("lineTo", ((4.0, 4.0),)),
("closePath", ()),
(PathVerb.MOVE, ((4.0, 4.0),)),
(PathVerb.LINE, ((10.0, 4.0),)),
(PathVerb.LINE, ((10.0, 20.0),)),
(PathVerb.LINE, ((4.0, 20.0),)),
(PathVerb.LINE, ((4.0, 4.0),)),
(PathVerb.CLOSE, ()),
),
"M4,4 L10,4 L10,20 L4,20 L4,4 Z",
),
(
SVGCircle(cx=5, cy=5, r=4),
(
("moveTo", ((9.0, 5.0),)),
("curveTo", ((9.0, 7.2091), (7.2091, 9.0), (5.0, 9.0))),
("curveTo", ((2.7909, 9.0), (1.0, 7.2091), (1.0, 5.0))),
("curveTo", ((1.0, 2.7909), (2.7909, 1.0), (5.0, 1.0))),
("curveTo", ((7.2091, 1.0), (9.0, 2.7909), (9.0, 5.0))),
("closePath", ()),
(PathVerb.MOVE, ((9.0, 5.0),)),
(PathVerb.CUBIC, ((9.0, 7.2091), (7.2091, 9.0), (5.0, 9.0))),
(PathVerb.CUBIC, ((2.7909, 9.0), (1.0, 7.2091), (1.0, 5.0))),
(PathVerb.CUBIC, ((1.0, 2.7909), (2.7909, 1.0), (5.0, 1.0))),
(PathVerb.CUBIC, ((7.2091, 1.0), (9.0, 2.7909), (9.0, 5.0))),
(PathVerb.CLOSE, ()),
),
"M9,5 C9,7.2091 7.2091,9 5,9 C2.7909,9 1,7.2091 1,5 C1,2.7909 2.7909,1 5,1 C7.2091,1 9,2.7909 9,5 Z",
),
# All-quad closed contour; when using Path.segments iterator (matching FontTools
# SegmentPen protocol), the on-curve point would become implied (None), leading
# to uncaught TypeError; but we don't have to use that for converting to SVG
# commands; in fact we now simply iterate over the Path verbs as is, that
# already yields individual curve segments that are SVG compatible.
# https://github.com/googlefonts/picosvg/issues/304
(
SVGPath(
d=(
"M0.117,0.055 Q0.117,0.029 0.107,0.029 Q0.097,0.029 0.097,0.055 "
"Q0.097,0.081 0.107,0.081 Q0.117,0.081 0.117,0.055 Z"
)
),
(
(PathVerb.MOVE, ((0.117, 0.055),)),
(PathVerb.QUAD, ((0.117, 0.029), (0.107, 0.029))),
(PathVerb.QUAD, ((0.097, 0.029), (0.097, 0.055))),
(PathVerb.QUAD, ((0.097, 0.081), (0.107, 0.081))),
(PathVerb.QUAD, ((0.117, 0.081), (0.117, 0.055))),
(PathVerb.CLOSE, ()),
),
"M0.117,0.055 Q0.117,0.029 0.107,0.029 Q0.097,0.029 0.097,0.055 "
"Q0.097,0.081 0.107,0.081 Q0.117,0.081 0.117,0.055 Z",
)
# TODO: round-trip SVGPath with fill_rule="evenodd"
],
)
def test_skia_path_roundtrip(shape, expected_segments, expected_path):
# We round to 4 decimal places to confirm custom value works
skia_path = svg_pathops.skia_path(shape.as_cmd_seq(), shape.fill_rule)
rounded_segments = list(skia_path.segments)
for idx, (cmd, points) in enumerate(rounded_segments):
rounded_segments[idx] = (cmd, tuple(_round(pt, 4) for pt in points))
rounded_segments = list(skia_path)
for idx, (verb, points) in enumerate(rounded_segments):
rounded_segments[idx] = (
verb,
tuple(_round(pt, 4) if pt is not None else None for pt in points),
)
assert tuple(rounded_segments) == expected_segments
assert (
SVGPath.from_commands(svg_pathops.svg_commands(skia_path))
Expand Down
Loading