Skip to content

Write and read DXF text heights as the height of the capital letters (#506) - #507

Open
dsn27 wants to merge 2 commits into
masterfrom
claude/intelligent-archimedes-hcfnbc
Open

dsn27 wants to merge 2 commits into
masterfrom
claude/intelligent-archimedes-hcfnbc

Conversation

@dsn27

@dsn27 dsn27 commented Oct 26, 2026

Copy link
Copy Markdown
Collaborator

Fixes #506.

Problem

Text.TextSize and DimensionStyle.TextSize are the em height of the font: CADability draws the glyphs with it, and Text.GetBoundingCube() is computed from it. In DXF, the height of a TEXT or MTEXT (group 40) and the dimension text height DIMTXT are the height of the capital letters, and AutoCAD and other viewers scale the font to that height.

Since the move to ACadSharp (v1.1.0), the exporter wrote TextSize unchanged as the DXF height, and the importer read it back unchanged. So every other program showed exported texts larger than CADability draws them and larger than their bounding cube, by em height / cap height: 2048/1467 ≈ 1.40 for Arial and ≈ 1.6 for Consolas. The screenshot in the issue shows exactly these factors. Round trips through CADability were unaffected, which hid the bug. The netDxf exporter of 1.0.x had scaled the height by the font metrics (TextSize * 1000 / font.Height).

Changes

  • FontCache.GetCapHeight / Text.CapHeightFactor / Text.GetCapHeightFactor (all internal) give the cap height of a font as a fraction of its em height. It is measured at the outline of "H", so it works the same with the managed and the GDI font provider. If there is no font or no "H", the factor is 1.0.
  • TEXT / MTEXT:
    • The export writes TextSize × cap height. The import divides by the same factor.
    • The MTEXT line spacing is converted with the same factor.
    • The export now also writes the width factor (group 41) from the length of the line direction. The import already read it, but the export dropped it.
  • Dimension styles:
    • DIMTXT is written as TextSize × cap height of the style's font, and read back the other way.
    • TextSizeTol follows TextSize.
    • TextDist is computed from the converted size, so the distance of the text from the dimension line stays at DIMGAP × DIMSCALE.

Texts in dimension blocks, dimensions drawn by ACadSharp, table cells and TOLERANCE entities all go through the TEXT import and are covered by the first point.

Behaviour change (for the release notes)

Imported DXF/DWG texts and dimension texts are now drawn larger in CADability than before, as large as AutoCAD shows them, about 1.4× for Arial. After an import, TextSize no longer equals the DXF height. Exporting and importing again keeps TextSize, the line spacing and the extent unchanged. No public API changes and no binary break, so a patch release.

Tests

  • New DxfTextHeightTest:
    • The written group 40 equals the height of the "H" CADability draws.
    • An imported TEXT of height 10 draws an "H" exactly 10 high.
    • A TEXT keeps its size, width factor and extent through export and re-import.
    • An MTEXT keeps its size and line spacing through export and re-import.
    • Without the fix, all four tests fail (e.g. 12 instead of 8.26).
  • DxfDimensionTest: four assertions that expected TextSize == DIMTXT now check that the capital letters are as high as DIMTXT. The existing import/export/import test still checks that TextSize is kept.
  • Run on Linux:
    • CADability.ImportTests: 96 passed, 1 skipped
    • CADability.Tests: 708 passed, 16 skipped
    • CADability.Forms builds.

Generated by Claude Code

…rs (#506)

The height of a DXF TEXT or MTEXT (group 40) is the height of the capital
letters, which AutoCAD and other viewers scale the font to, while
Text.TextSize is the em height of the font. Since the move to ACadSharp the
exporter wrote TextSize unchanged, so every other program showed exported
texts 1.4 (Arial) to 1.6 (Consolas) times larger than CADability drew them
and than their bounding cube. The netDxf exporter of 1.0.x had scaled the
height by the font metrics.

The export now multiplies TextSize by the cap height of the font, measured at
the outline of "H" (FontCache.GetCapHeight, Text.CapHeightFactor), and the
import divides by the same factor, so a round trip keeps TextSize and the
extent unchanged. Imported DXF and DWG texts are therefore drawn larger than
before, as large as AutoCAD shows them. The MTEXT line spacing is converted
with the same factor. The export also writes the width factor (group 41) from
the length of the line direction, which it dropped before, while the import
already read it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F53kFpUYJXo4cE8wSe2jHi
DIMTXT, the text height of a DXF dimension style, is the height of the
capital letters like the height of a TEXT, while CADability draws the
dimension text with DimensionStyle.TextSize as the em height of the font.
The export now multiplies TextSize by the cap height of the style's font and
the import divides by it, so imported dimensions show their text as large as
AutoCAD does and a round trip keeps TextSize. TextSizeTol follows TextSize,
and TextDist is computed from the converted size, which keeps the distance of
the text from the dimension line at DIMGAP.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F53kFpUYJXo4cE8wSe2jHi

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Text Width and Bounding Box Width Mismatch (~1.6x Scaling Difference)

2 participants