Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.
C# audio-converteravaloniabatch-conversionconvertercsharpdocument-converterdocx-to-pdfdotnetdrag-and-dropfile-conversionfile-converterheic-converterhwphwpximage-converterlinuxmacospdf-convertervideo-converterwindows
Read HP-GL/1 plots that start with any command (#432 review)
cre8ive-marketingb3a8ce76f0ad
diff --git a/src/Filee.Engines/Vector/HpglPlot.cs b/src/Filee.Engines/Vector/HpglPlot.csindex 7808aab4..3d38d571 100644--- a/src/Filee.Engines/Vector/HpglPlot.cs+++ b/src/Filee.Engines/Vector/HpglPlot.cs@@ -4,9 +4,9 @@//// Read: IN, SP / PC / NP / PW (pens and their colours and widths), PU / PD / PA / PR, CI, AA / AR (arcs as short// lines), PE (HP-GL/2 encoded polylines, the bulk of a printer driver's plot), PM / EP / FP (polygons, filled with-// FP). A PJL / PCL wrapper around HP-GL/2 is skipped up to "enter HP-GL/2 mode" (ESC % … B). Not drawn: text (LB),-// fill patterns, raster data; user scaling (SC) is ignored, so a scaled plot keeps its shape but not its size.-// The PE decoding follows ezdxf's hpgl2 add-on (MIT).+// FP). A PJL / PCL wrapper around HP-GL/2 is skipped up to "enter HP-GL/2 mode" (ESC % … B), and the device-control+// sequences of HP-GL/1 plotters (ESC . …) are skipped. Not drawn: text (LB), fill patterns, raster data; user scaling+// (SC) is ignored, so a scaled plot keeps its shape but not its size.using System.Globalization;using System.Text;@@ -65,19 +65,21 @@ internal static class HpglPlot// ───────────────────────── Commands ─────────────────────────/// <summary>- /// Splits the plot into commands: two letters and their arguments up to ';' or the next command. PE's arguments- /// are its encoded bytes; LB's run to the label terminator (ETX).+ /// Splits the plot into commands: two letters and their arguments up to ';' or the next command (quoted text such+ /// as a CO comment included). PE's arguments are its encoded bytes; LB's run to the label terminator (ETX). Any+ /// command may come first./// </summary>internal static IEnumerable<(string Name, string Args)> Commands(byte[] s){- var i = StartsWithHpgl(s) ? 0 : EntryPoint(s, 0);+ var i = 0;while (i < s.Length){var c = s[i];if (c == 27){- // An escape sequence leaves HP-GL/2 (PCL, PJL): go on where the plot enters it again.- i = EntryPoint(s, i + 1);+ // ESC . is an HP-GL/1 device-control sequence; any other escape (PCL, PJL) leaves HP-GL/2, which goes on+ // where the plot enters it again (ESC % … B).+ i = i + 1 < s.Length && s[i + 1] == '.' ? DeviceControlEnd(s, i) : EntryPoint(s, i);continue;}if (c <= 32 || c == ';' || !char.IsAsciiLetterUpper((char)c) && !char.IsAsciiLetterLower((char)c))@@ -95,21 +97,29 @@ internal static class HpglPlotelse if (name == "LB")while (end < s.Length && s[end] != 3) end++;else- while (end < s.Length && s[end] != ';' && s[end] != 27 && !char.IsAsciiLetter((char)s[end])) end++;+ while (end < s.Length && s[end] != ';' && s[end] != 27 && !char.IsAsciiLetter((char)s[end]))+ end = s[end] == '"' ? Math.Min(s.Length, Array.IndexOf(s, (byte)'"', end + 1) is var close and >= 0 ? close + 1 : s.Length) : end + 1;yield return (name, Encoding.Latin1.GetString(s, start, end - start));i = end < s.Length && s[end] is (byte)';' or 3 ? end + 1 : end;}}- private static bool StartsWithHpgl(byte[] s)+ /// <summary>+ /// Index after a device-control sequence at <paramref name="at"/>: ESC . and one character (ESC . ( turns the+ /// plotter on), with its numeric parameters when they end in ':' (ESC . I 81;;17:, ESC . N ;19:).+ /// </summary>+ private static int DeviceControlEnd(byte[] s, int at){- var head = Encoding.ASCII.GetString(s, 0, Math.Min(s.Length, 16)).TrimStart();- return head.StartsWith("IN", StringComparison.OrdinalIgnoreCase) || head.StartsWith("BP", StringComparison.Ordinal)- || head.StartsWith("SP", StringComparison.Ordinal) || head.StartsWith("PU", StringComparison.Ordinal)- || head.StartsWith("PA", StringComparison.Ordinal) || head.StartsWith("DF", StringComparison.Ordinal);+ var next = at + 3;+ if (next > s.Length)+ return s.Length;+ var end = next;+ while (end < s.Length && (char.IsAsciiDigit((char)s[end]) || s[end] is (byte)';' or (byte)'-' or (byte)'.'))+ end++;+ return end < s.Length && s[end] == ':' ? end + 1 : next;}- /// <summary>Index after the next "enter HP-GL/2 mode" (ESC % 0-3 B, also ESC % -1 B), or the end.</summary>+ /// <summary>Index after the next "enter HP-GL/2 mode" (ESC % 0-3 B, also ESC % -1 B) from <paramref name="from"/>, or the end.</summary>private static int EntryPoint(byte[] s, int from){for (var i = from; i + 3 < s.Length; i++)diff --git a/tests/Filee.Engines.Tests/HpglPlotTests.cs b/tests/Filee.Engines.Tests/HpglPlotTests.csindex 887b13e5..1db8e766 100644--- a/tests/Filee.Engines.Tests/HpglPlotTests.cs+++ b/tests/Filee.Engines.Tests/HpglPlotTests.cs@@ -1,5 +1,5 @@// HP-GL plot files (.plt, issue #410): pen moves read as lines, drawn as SVG and rendered through the vector engine.-// The packed HP-GL/2 sample (PE) was encoded with ezdxf's hpgl2 add-on, not with Filee's own decoder.+// The packed HP-GL/2 sample (PE) was encoded independently of Filee's decoder, so the test is not circular.using System.Text;using Filee.Core.Conversion;@@ -33,6 +33,19 @@ public class HpglPlotTests(EngineFixture fx) : IClassFixture<EngineFixture>Assert.Equal("#FF0000", strokes[1].Color);}+ [Theory]+ [InlineData("\u001B.(;\u001B.I81;;17:\u001B.N;19:IN;SP1;PU0,0;PD400,0,400,400;")] // device control first+ [InlineData("IP0,0,4000,4000;SC;SP1;PU0,0;PD400,0,400,400;")] // scaling points first+ [InlineData("\u0003IN;SP1;PU0,0;PD400,0,400,400;")] // a leading ETX+ [InlineData("CO\"made by x; PD 9,9\";IN;SP1;PU0,0;PD400,0,400,400;")] // a comment first, with a command in it+ [InlineData("in;sp1;pu0,0;pd400,0,400,400;")] // lower case+ public void Hpgl1_plots_starting_with_any_command_are_read(string plot)+ {+ var stroke = Assert.Single(HpglPlot.Read(Encoding.Latin1.GetBytes(plot)));++ Assert.Equal([(0, 0), (400, 0), (400, 400)], stroke.Points);+ }+[Fact]public void Encoded_polylines_of_a_printer_driver_are_read(){