Skip to content
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
2 changes: 2 additions & 0 deletions docs/public-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
33 changes: 32 additions & 1 deletion src/DwgIO/Model/DwgEntity.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Globalization;
using System.Text.RegularExpressions;

namespace DwgIO.Model;

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -322,10 +336,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] == ';' && MTextParagraphProperties.IsMatch(arguments[..end]))
{
i += end + 2;
}
else
{
sb.Append("\\p");
i++;
}

break;
}
case '~':
sb.Append(' ');
i++;
Expand Down
29 changes: 29 additions & 0 deletions tests/DwgIO.Tests/HatchDimensionAttribTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,35 @@ 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")]
[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
{
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()
{
Expand Down
Loading