Text Width / Height Measurement
Text Width / Height Measurement
Automatic drawing often needs the laid-out size of text before placing table rows, columns, or blocks. WebCAD provides local WASM measurement and server-side ODA measurement.
Online Examples
| Example | Description | Link |
|---|---|---|
| Text size | measureTextLocal / measureTexts local vs server | Online Demo{target="_blank"} |
| Measure text (template) | Same batch measured locally and on the server | Online Demo{target="_blank"} |
API Overview
| API | Description |
|---|---|
measureTextLocal(item) | Sync local measure of one item; returns null if WASM/fonts are not ready |
measureTextsLocal(items) | Sync batch local measure |
measureTextsOnServer(mapid, version, items) | Batch server measure against a template |
measureTexts(items, options?) | Unified entry; default mode: 'auto' routes by source |
const {
measureTextLocal,
measureTexts,
measureTextsOnServer
} = vjcad;Local Measurement
Does not add entities to the drawing or wait for a render frame:
const size = measureTextLocal({
type: 'TEXT',
text: 'WebCAD',
height: 8,
styleName: 'Standard'
});
if (!size) {
// WASM not initialized, or no fonts loaded via MainView.fonts
return;
}
console.log(size.width, size.height, size.bounds, size.isFallbackFont);For MTEXT, pass column width width (same meaning as entity width):
const m = measureTextLocal({
type: 'MTEXT',
text: 'Multiline text wraps by column width',
height: 3,
width: 120
});Lay out by measured widths
const tags = ['Layer', 'Linetype', 'Color'];
const sizes = await measureTexts(
tags.map(text => ({ type: 'TEXT', text, height: 8 })),
{ mode: 'local' }
);
let x = 0;
tags.forEach((text, i) => {
const w = sizes[i]?.width ?? 0;
x += w + 4;
});Unified Entry: measureTexts
const results = await measureTexts([
// With clone source → server in auto mode (real template fonts)
{
text: 'Silty clay',
height: 2.5,
width: 30,
cloneSource: 'clone_96BA_template_sect_v1'
},
// No clone source → local WASM
{ text: 'Plain text', height: 3, type: 'TEXT', styleName: 'Standard' }
], {
mode: 'auto' // 'auto' | 'local' | 'server'
});Options ITextMeasureOptions
| Field | Description |
|---|---|
mode | 'auto' (default) / 'local' / 'server' |
mapid | Default template map id when mode: 'server' |
version | Default template version, 'v1' |
Result fields
| Field | Description |
|---|---|
width / height | Laid-out size; MTEXT height is logical height summed per line |
geomWidth / geomHeight | Content bounding-box size (use to hug the glyphs) |
bounds | [minX, minY, maxX, maxY] relative to insertion point |
source | 'local' or 'server' |
isFallbackFont | Local measure fell back to another font — treat as approximate |
Failed items return null without aborting the batch.
Server Measurement
Template DWG fonts (FangSong, SimSun, …) are usually not in MainView.fonts. Local resolveFontForName silently falls back; CJK widths may be close, but Latin/punctuation differences change wrapping and thus height. The server lays out with ODA on the real DWG (OdDbMText::actualWidth() / actualHeight()), matching exported DWG.
const server = await measureTextsOnServer('template_sect', 'v1', [
{ handle: '96BA', text: 'Silty clay', height: 2.5, width: 30 }
]);Requires the backend measureText command. Server batches in measureTexts are grouped by template (few round-trips).
Notes
- Local measure needs WASM initialized and at least one font from
MainView.fonts - Do not use
isFallbackFont === trueresults for precise published layouts - See Template Drawing for clone-based server measure