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 viewer crash with empty layers #339

Merged
merged 5 commits into from
Nov 20, 2021
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,5 @@ cython_debug/
/bug_reports
/W3C_SVG_11_TestSuite
/my_work
/pip-wheel-metadata
/pip-wheel-metadata
/test_report_img_sim/
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ New features and improvements:

Bug fixes:
* Fixed issue with HPGL export where page size auto-detection would fail when using the default device from the config file (instead of specifying the device with `--device`) (#328)
* Fixed issue where the viewer would crash with empty layers (#339)


#### 1.7 (2021-06-10)
Expand Down
79 changes: 41 additions & 38 deletions tests/test_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,38 @@

from .utils import TEST_FILE_DIRECTORY

RENDER_KWARGS = [
pytest.param({"view_mode": ViewMode.OUTLINE}, id="outline"),
pytest.param({"view_mode": ViewMode.OUTLINE_COLORFUL}, id="outline_colorful"),
pytest.param({"view_mode": ViewMode.PREVIEW}, id="preview"),
pytest.param({"view_mode": ViewMode.OUTLINE, "show_points": True}, id="points"),
pytest.param(
{"view_mode": ViewMode.OUTLINE_COLORFUL, "show_points": True}, id="colorful_points"
),
pytest.param({"view_mode": ViewMode.OUTLINE, "show_pen_up": True}, id="outline_pen_up"),
pytest.param({"view_mode": ViewMode.PREVIEW, "show_pen_up": True}, id="preview_pen_up"),
pytest.param(
{"view_mode": ViewMode.PREVIEW, "pen_opacity": 0.3}, id="preview_transparent"
),
pytest.param({"view_mode": ViewMode.PREVIEW, "pen_width": 4.0}, id="preview_thick"),
pytest.param(
{"view_mode": ViewMode.OUTLINE, "show_ruler": True, "unit_type": UnitType.PIXELS},
id="outline_pixels",
),
pytest.param(
{"view_mode": ViewMode.OUTLINE, "show_ruler": True, "unit_type": UnitType.METRIC},
id="outline_metric",
),
pytest.param(
{
"view_mode": ViewMode.OUTLINE,
"show_ruler": True,
"unit_type": UnitType.IMPERIAL,
},
id="outline_imperial",
),
]


# assert_image_similarity fixture added for automated exclusion on unsupported runner
def test_viewer_engine_properties(assert_image_similarity):
Expand Down Expand Up @@ -67,51 +99,22 @@ def test_viewer_engine_properties(assert_image_similarity):
["misc/empty.svg", "misc/multilayer.svg", "issue_124/plotter.svg"],
ids=lambda s: os.path.splitext(s)[0],
)
@pytest.mark.parametrize(
"render_kwargs",
[
pytest.param({"view_mode": ViewMode.OUTLINE}, id="outline"),
pytest.param({"view_mode": ViewMode.OUTLINE_COLORFUL}, id="outline_colorful"),
pytest.param({"view_mode": ViewMode.PREVIEW}, id="preview"),
pytest.param({"view_mode": ViewMode.OUTLINE, "show_points": True}, id="points"),
pytest.param(
{"view_mode": ViewMode.OUTLINE_COLORFUL, "show_points": True}, id="colorful_points"
),
pytest.param(
{"view_mode": ViewMode.OUTLINE, "show_pen_up": True}, id="outline_pen_up"
),
pytest.param(
{"view_mode": ViewMode.PREVIEW, "show_pen_up": True}, id="preview_pen_up"
),
pytest.param(
{"view_mode": ViewMode.PREVIEW, "pen_opacity": 0.3}, id="preview_transparent"
),
pytest.param({"view_mode": ViewMode.PREVIEW, "pen_width": 4.0}, id="preview_thick"),
pytest.param(
{"view_mode": ViewMode.OUTLINE, "show_ruler": True, "unit_type": UnitType.PIXELS},
id="outline_pixels",
),
pytest.param(
{"view_mode": ViewMode.OUTLINE, "show_ruler": True, "unit_type": UnitType.METRIC},
id="outline_metric",
),
pytest.param(
{
"view_mode": ViewMode.OUTLINE,
"show_ruler": True,
"unit_type": UnitType.IMPERIAL,
},
id="outline_imperial",
),
],
)
@pytest.mark.parametrize("render_kwargs", RENDER_KWARGS)
def test_viewer(assert_image_similarity, file, render_kwargs):
doc = vp.read_multilayer_svg(str(TEST_FILE_DIRECTORY / file), 0.4)

# noinspection PyArgumentList
assert_image_similarity(render_image(doc, (1024, 1024), **render_kwargs))


@pytest.mark.parametrize("render_kwargs", RENDER_KWARGS)
def test_viewer_empty_layer(assert_image_similarity, render_kwargs):
# Note: assert_image_similarity added to avoid running CI tests on Linux
doc = vp.Document()
doc.add(vp.LineCollection(), 1)
render_image(doc, (1024, 1024), **render_kwargs)


def test_viewer_zoom_scale(assert_image_similarity):
doc = vp.read_multilayer_svg(str(TEST_FILE_DIRECTORY / "issue_124/plotter.svg"), 0.4)
renderer = ImageRenderer((1024, 1024))
Expand Down
6 changes: 4 additions & 2 deletions vpype_viewer/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,11 @@ def _rebuild(self):
color_index = 0
for layer_id in sorted(self._document.layers):
layer_color: ColorType = _COLORS[color_index % len(_COLORS)]
color_index += 1

lc = self._document.layers[layer_id]
if lc.is_empty():
continue

if self.view_mode == ViewMode.OUTLINE:
self._layer_painters[layer_id].append(
Expand Down Expand Up @@ -463,8 +467,6 @@ def _rebuild(self):
LineCollectionPointsPainter(self._ctx, lc=lc, color=layer_color)
)

color_index += 1

page_size = self._document.page_size
if page_size is not None:
self._paper_bounds_painter = PaperBoundsPainter(
Expand Down