HPGL export produces wrong output for multi-segments SVG path
Steps to reproduce:
- open Inkscape
- open the following SVG source.svg:
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" width="328" height="397" viewBox="0 0 328 397"
style="position: fixed; background: white;">
<path style="stroke-width: 2px; fill: none; stroke: black"
d=" M239.47,85.04L161.27,290.58 M161.27,290.58L98.45,105.81 M98.45,105.81L239.47,85.04" />
</svg>
- save it to HPGL with the following settings:
- Open the resulting HPGL file and see that the shape is corrupted : corrupted.hpgl
IN;PU;SP1;PU2534,3302;PD1707,1126;SP0;PU0,0;IN;
What happened?
The original SVG (Produced by https://mitxela.com/plotterfun/) defines a path with successive line segments, each new one starting by a move command. The resulting HPGL miss some lines because of the repeated coordinates.
What should have happened?
The same triangle present in the original SVG should be present when opening the exported HPGL file.
After some investigation, the following patch in hpgl_encoder.py fixes the issue for this use case:
--- ./usr/share/inkscape/extensions/hpgl_encoder.py.org 2023-02-21 23:10:25.349549839 +0100
+++ ./usr/share/inkscape/extensions/hpgl_encoder.py 2023-02-21 23:10:31.673507639 +0100
@@ -311,9 +311,9 @@
round(posY)
) != int(round(oldPosY)):
self.processOffset(cmd, Vector2d(posX, posY), pen, speed, force)
- cmd = "PD"
oldPosX = posX
oldPosY = posY
+ cmd = "PD"
# perform overcut
if self.overcut > 0.0 and not self.dryRun:
# check if last and first points are the same, otherwise the path
From what I understand, the repeated coordinates in the files resets the current command to "PU" even if it should stay as "PD". By moving cmd="PD" out of the test, this ensure that on next loop the command will be "PD" as it should.
With this patch applied, the same SVG is exported as the following "fixed" HPFL file: fixed.hpgl
IN;PU;SP1;PU2534,3302;PD1707,1126,1042,3082,2534,3302;SP0;PU0,0;IN;
I did not provide a merge request with this file because I am not sure that this do not create problems with other use case or kind of SVG path. If this patch is deemed sufficient by experts, I can provide a merge request.
Inkscape Version and Operating System:
- Inkscape 1.2.2 (b0a8486, 2022-12-01)
- GLib version: 2.64.6
- GTK version: 3.24.20
- glibmm version: 2.64.2
- gtkmm version: 3.24.2
- libxml2 version: 2.9.10
- libxslt version: 1.1.34
- Cairo version: 1.17.6
- Pango version: 1.44.7
- HarfBuzz version: 2.6.4
- OS version: Ubuntu 20.04.5 LTS

