DxfReader: without $DWGCODEPAGE, pre-AC1021 text is decoded as UTF-8 and non-ASCII characters are lost (no option to set the encoding)
Maintainers usually reply within 3 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 52/100
Research direction
Start by tracing how DxfReader selects an encoding through CadReaderBase._encoding and how the header's CodePage is set when $DWGCODEPAGE is absent. Reproduce the issue with samples/sample_AC1009_ascii.dxf and verify that Ø45,6 survives reading and Header.CodePage matches the decoding. Consider how reader configuration and pre-AC1021 DWG behavior fit the suggested fallback and override options.
Written by the indexing model from the issue text.
Description
Describe the bug
When a pre-AC1021 DXF has no $DWGCODEPAGE in its header (R12 files, or files written without a complete HEADER section), DxfReader decodes strings with Encoding.Default (CadReaderBase._encoding), which is UTF-8 on .NET Core and .NET 5+. Single-byte characters above 0x7F (Ø, ±, °, or national letters such as Turkish ş, ğ, ı) become U+FFFD, so the original text cannot be recovered after reading.
At the same time Header.CodePage reports ANSI_1252 (its default value), although that encoding was not used to decode the file.
There is no option on CadReaderConfiguration or DxfReaderConfiguration to supply the encoding, so callers have no way to read these files correctly.
The R12 sample in this repository shows it: samples/sample_AC1009_ascii.dxf has no $DWGCODEPAGE and contains the byte 0xD8 (Ø45,6 in Windows-1252); it is read as �45,6.
To Reproduce
using System.Text;
using ACadSharp;
using ACadSharp.Entities;
using ACadSharp.IO;
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
// AC1015 ASCII DXF, no $DWGCODEPAGE in the header, TEXT stored as Windows-1252 bytes.
string text = "Ø45 ±0.5 25°C";
string dxf = string.Join("\n",
"0", "SECTION", "2", "HEADER",
"9", "$ACADVER", "1", "AC1015",
"0", "ENDSEC",
"0", "SECTION", "2", "ENTITIES",
"0", "TEXT", "5", "A1", "100", "AcDbEntity", "8", "0", "100", "AcDbText",
"10", "0", "20", "0", "30", "0", "40", "1",
"1", text,
"0", "ENDSEC",
"0", "EOF");
using var stream = new MemoryStream(Encoding.GetEncoding(1252).GetBytes(dxf));
using var reader = new DxfReader(stream);
CadDocument doc = reader.Read();
var entity = doc.Entities.OfType<TextEntity>().Single();
Console.WriteLine($"Expected : {text}");
Console.WriteLine($"Read : {entity.Value}");
Console.WriteLine($"Contains U+FFFD: {entity.Value.Contains('�')}");
Console.WriteLine($"Header.CodePage: {doc.Header.CodePage}");
Output:
Expected : Ø45 ±0.5 25°C
Read : �45 �0.5 25�C
Contains U+FFFD: True
Header.CodePage: ANSI_1252
Expected behavior
Characters are not silently lost, and Header.CodePage matches the encoding that was actually used.
Suggested fix
-
Add an encoding option to the reader configuration, for example:
- a fallback encoding, used when the file does not declare
$DWGCODEPAGE; - optionally an override, used even when the file declares one (some files declare a wrong code page).
The same option could also be useful for pre-AC1021 DWG files.
- a fallback encoding, used when the file does not declare
-
When no option is given, fall back to Windows-1252 instead of UTF-8, so the decoding agrees with the
Header.CodePagedefault.
Related: #1280 and #1314 (culture-sensitive lookup of $DWGCODEPAGE).
Version
ACadSharp 3.8.35 (NuGet), .NET 10, Windows. The same code is on master (0acbe974).
- Dominant language
- C#
- Stars
- 873
- Forks
- 223
- Avg merge
- 1d 15h
- Merged PRs (30d)
- 28
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from DomCR/ACadSharp
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 3 days
-
DWG writer: text styles get the text-style table as their xref block handlePossibly taken @menes97 claimed this 4 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 81/100
Maintainers usually reply within 3 days
-
Difficulty 1/5 1-3 hours Newbie friendliness 84/100
Maintainers usually reply within 3 days
-
DwgWriter writes a null shape-file handle for SHAPE entities (WriteShapes = true)Possibly taken @menes97 claimed this 4 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 3 days
-
DwgReader reads a color-book entity's Color as ByBlock instead of the book's colorPossibly taken @menes97 claimed this 4 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 3 days
Similar issues
-
hand-to-AI
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
CactuseSecurity/firewall-orchestrator#5803 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
Maintainers usually reply within 1 day
-
bug python
Difficulty 1/5 Under an hour Newbie friendliness 88/100
ilia-sokolov/OfficeAgent.NET#89 ·
Maintainers usually reply within 13 days