DxfReader: without $DWGCODEPAGE, pre-AC1021 text is decoded as UTF-8 and non-ASCII characters are lost (no option to set the encoding)

Open
#1,413 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 3 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
52/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
csharp
Domain
backend

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

  1. 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.

  2. When no option is given, fall back to Windows-1252 instead of UTF-8, so the decoding agrees with the Header.CodePage default.

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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from DomCR/ACadSharp

All issues in DomCR/ACadSharp

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.