From 0dda91f6ec4267e24e53218b6574709b8efb4faf Mon Sep 17 00:00:00 2001 From: gryaznulka Date: Sun, 6 Dec 2026 16:01:29 +0100 Subject: [PATCH 1/3] fix: strip MTEXT paragraph properties without adding line breaks --- src/DwgIO/Model/DwgEntity.cs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/DwgIO/Model/DwgEntity.cs b/src/DwgIO/Model/DwgEntity.cs index 23fa0d5..08fff16 100644 --- a/src/DwgIO/Model/DwgEntity.cs +++ b/src/DwgIO/Model/DwgEntity.cs @@ -322,10 +322,27 @@ private static string StripMTextFormatting(string raw) switch (next) { case 'P': - case 'p': sb.Append('\n'); i++; break; + case 'p': + { + // Paragraph properties (indentation, alignment, tabs) end at ';'. + // A missing terminator must not consume the next formatting command or group. + var arguments = raw.AsSpan(i + 2); + int end = arguments.IndexOfAny(";\\{}\r\n".AsSpan()); + if (end >= 0 && arguments[end] == ';') + { + i += end + 2; + } + else + { + sb.Append("\\p"); + i++; + } + + break; + } case '~': sb.Append(' '); i++; From 87f8678344f00c8f36d42eaa9776af0e6532c959 Mon Sep 17 00:00:00 2001 From: gryaznulka Date: Sun, 6 Dec 2026 16:02:15 +0100 Subject: [PATCH 2/3] test: cover MTEXT paragraph properties in scene labels --- .../DwgIO.Tests/HatchDimensionAttribTests.cs | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/DwgIO.Tests/HatchDimensionAttribTests.cs b/tests/DwgIO.Tests/HatchDimensionAttribTests.cs index 5b789cc..71f03a5 100644 --- a/tests/DwgIO.Tests/HatchDimensionAttribTests.cs +++ b/tests/DwgIO.Tests/HatchDimensionAttribTests.cs @@ -50,6 +50,31 @@ public void PlainText_DecodesUnicodeAndCadSpecialEscapes() Assert.Equal("Ось 45° ±0.5 ⌀100 10%", attrib.PlainText); } + [Theory] + [InlineData(@"{\pxi-2,l2;Ось\PРазмер}", "Ось\nРазмер")] + [InlineData(@"\pxqc,t1,r5,c8;A\P\pi*,l*,r*,q*,t;B", "A\nB")] + [InlineData(@"\U+041E\U+0441\U+044C\PРазмер", "Ось\nРазмер")] + [InlineData(@"\\pxi-2,l2;Ось", @"\pxi-2,l2;Ось")] + [InlineData(@"before\pafter\Pline; end", "before\\pafter\nline; end")] + public void PlainText_DistinguishesParagraphPropertiesFromParagraphBreaks(string raw, string expected) + { + var mtext = new DwgEntity + { + Handle = 0x10, + TypeCode = 0x2C, + TypeName = "MTEXT", + Kind = DwgEntityKind.MText, + Text = raw, + Geometry = [0, 0, 0, 1, 0, 5, 1], + }; + Assert.Equal(raw, mtext.Text); + Assert.Equal(expected, mtext.PlainText); + + var doc = new DwgDocument("paragraph.dwg", Header2018, [mtext]); + var scene = DwgSceneReader.Read(doc, new DwgSceneOptions { ModelKey = "paragraph" }); + Assert.Equal(expected, Assert.Single(scene.Annotations).Text); + } + [Fact] public void Hatch_Solid_EmitsOutlineAndFill_AndStaysFlatInAutoMode() { From f2265749aafa241b3c1092d00d9ba74d2f708bc7 Mon Sep 17 00:00:00 2001 From: mahach666 <79989386+mahach666@users.noreply.github.com> Date: Sun, 6 Dec 2026 18:47:48 +0300 Subject: [PATCH 3/3] Preserve MTEXT after malformed paragraph properties --- docs/public-api.md | 2 ++ src/DwgIO/Model/DwgEntity.cs | 16 +++++++++++++++- tests/DwgIO.Tests/HatchDimensionAttribTests.cs | 4 ++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/docs/public-api.md b/docs/public-api.md index 511341d..22133e9 100644 --- a/docs/public-api.md +++ b/docs/public-api.md @@ -45,6 +45,8 @@ change in any release and is visible only to the test project through `Internals | `DwgHeaderVariables`, `DwgInsUnits`, `DwgPoint2D`, `DwgPoint3D` | Drawing system variables from `AcDb:Header`: `$INSUNITS` (`TryGetSceneUnit`), `$MEASUREMENT`, `$EXTMIN`/`$EXTMAX`, `$LIMMIN`/`$LIMMAX`, `$LUNITS`/`$LUPREC`, `$AUNITS`/`$AUPREC`, `$PDMODE`/`$PDSIZE`, `$LTSCALE`/`$CELTSCALE`/`$DIMSCALE`, `$CELWEIGHT`, `$TDCREATE`/`$TDUPDATE`, `$HANDSEED`, `$FINGERPRINTGUID` and `$VERSIONGUID`. | | `DwgTemplate` | The template description of `AcDb:Template`: description and unit system (English/metric). | +For MTEXT, `PlainText` turns `\P` into a line break, strips recognized paragraph properties and preserves text following malformed paragraph commands. + ## JSON export (`DwgIO.Json`) | Type | Use | diff --git a/src/DwgIO/Model/DwgEntity.cs b/src/DwgIO/Model/DwgEntity.cs index 08fff16..2b5d06e 100644 --- a/src/DwgIO/Model/DwgEntity.cs +++ b/src/DwgIO/Model/DwgEntity.cs @@ -1,4 +1,5 @@ using System.Globalization; +using System.Text.RegularExpressions; namespace DwgIO.Model; @@ -278,6 +279,19 @@ private static bool TryReadPercentEscape(char code, out char? symbol) } } + private const string ParagraphNumber = @"[+-]?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?"; + private const string ParagraphTabStop = "[crD]?" + ParagraphNumber; + private const string ParagraphProperty = + @"(?:[abilr|](?:\*|" + ParagraphNumber + ")" + + "|q[lrcdj*]" + + @"|s(?:\*|[eam]" + ParagraphNumber + ")" + + "|t(?:[z*]|" + ParagraphTabStop + "(?:," + ParagraphTabStop + ")*)?)"; + + // Some writers place the scaling flag between properties or omit the comma between adjacent properties. + private static readonly Regex MTextParagraphProperties = new( + @"\Ax*" + ParagraphProperty + "(?:,?x*" + ParagraphProperty + @")*x*\z", + RegexOptions.NonBacktracking); + private static string StripMTextFormatting(string raw) { if (raw.IndexOf('\\') < 0 && raw.IndexOf('{') < 0 && raw.IndexOf('}') < 0 && raw.IndexOf("%%", StringComparison.Ordinal) < 0) @@ -331,7 +345,7 @@ private static string StripMTextFormatting(string raw) // A missing terminator must not consume the next formatting command or group. var arguments = raw.AsSpan(i + 2); int end = arguments.IndexOfAny(";\\{}\r\n".AsSpan()); - if (end >= 0 && arguments[end] == ';') + if (end > 0 && arguments[end] == ';' && MTextParagraphProperties.IsMatch(arguments[..end])) { i += end + 2; } diff --git a/tests/DwgIO.Tests/HatchDimensionAttribTests.cs b/tests/DwgIO.Tests/HatchDimensionAttribTests.cs index 71f03a5..71b3b22 100644 --- a/tests/DwgIO.Tests/HatchDimensionAttribTests.cs +++ b/tests/DwgIO.Tests/HatchDimensionAttribTests.cs @@ -56,6 +56,10 @@ public void PlainText_DecodesUnicodeAndCadSpecialEscapes() [InlineData(@"\U+041E\U+0441\U+044C\PРазмер", "Ось\nРазмер")] [InlineData(@"\\pxi-2,l2;Ось", @"\pxi-2,l2;Ось")] [InlineData(@"before\pafter\Pline; end", "before\\pafter\nline; end")] + [InlineData(@"Диаметр\p45 мм; допуск 0.5", @"Диаметр\p45 мм; допуск 0.5")] + [InlineData(@"До\pxi-2,l2 подпись; после", @"До\pxi-2,l2 подпись; после")] + [InlineData(@"\pxi-3,l4t4;1. Первый\P2. Второй", "1. Первый\n2. Второй")] + [InlineData(@"\pxb0.5,a1,se2,tD3,4,qr;A\P\ptz;B", "A\nB")] public void PlainText_DistinguishesParagraphPropertiesFromParagraphBreaks(string raw, string expected) { var mtext = new DwgEntity