Blog · DXF · 1 Oct 2026 · 2 min read

How to measure the real width of a text in a DXF with ezdxf

ezdxf's bounding box underestimates text width by up to 44%, and a checker built on the same box reported zero overflows on 147 cells with the note sticking out. The font-based measurement that works, and the rule of one measuring function for placing, framing and testing.

Short answer: do not use ezdxf.bbox to measure TEXT or MTEXT. Load the font with ezdxf.fonts.fonts.make_font(font_file, height) and call text_width(string), line by line for MTEXT. Add a 2% margin. Use the same function to place the text, draw the frame around it and test the result.

How wrong the box is

Method Same string Error
ezdxf.bbox on the TEXT 11.46 very wrong
characters × height × 0.6 27.3 12% short
make_font(...).text_width(...) 31.09 reference

On MTEXT, the box underestimated by 44% (225 against about 493 at height 50).

What it cost

Notes sticking out of their cell frames on 93 of 136 cells on one job and 147 of 170 on another, all by the same 309.8 mm. And the checker used the same bounding box, so it reported "0 overflows" on 147 cells with the note outside.

A test that measures with the tool that placed the text agrees with it, by construction.

The code

from ezdxf.fonts import fonts

style = doc.styles.get(text.dxf.style)
fm = fonts.make_font(style.dxf.font, height)        # e.g. 'tahoma.ttf'
width = fm.text_width("Chequered plate 5+2 S235JR") # 31.09

For MTEXT, measure each line. Lines are separated by \P, and alignment codes like \A1; must be stripped:

import re
f = fonts.make_font("ariali.ttf", h)
boxes = []
for i, line in enumerate(note.text.split("\\P")):
    line = re.sub(r"^\\A\d;", "", line)
    top = y1 - i * h * LINE_H
    boxes.append((x0, top - h * LINE_H, x0 + f.text_width(line) * 1.02, top))

The 1.02 covers rendering differences between machines.

One measuring function, three uses

Place the text, size the frame, test the overflow: all three must import the same function. Three copies drift apart, and a test that disagrees with the placement is either wrong or right by accident.

If the font file is missing and you fall back to the character estimate, say so in the report. A fallback that stays silent becomes the measurement.

Do and don't

Do

Don't

Questions people ask

Why does the bounding box get it wrong? It does not lay out the glyphs of the font, so it cannot know their real advance widths. The font metrics do.

Does this matter for an AI agent? More than for a person. A person sees the note sticking out. An agent trusts the number unless it renders the drawing: render a DXF to PNG.