diff --git a/CHANGELOG.md b/CHANGELOG.md index cab0507..9e87ee0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,25 @@ # Changelog +## Unreleased + +- An edit changes an xref layer's linetype in its host, as `e.layer("SURVEY|BOUNDARY", { linetype: + "SURVEY|PHANTOM2" })`, to a linetype the host holds. Both writers mark it as the host's override, + so a reload of the xref keeps it. +- An edit's additions may use a text style of the file whose font tsdraft does not hold, such as a + firm's Baskerville Old Face, for a text justified left and a paragraph of one column, which the + leading CAD app lays out itself. What tsdraft would lay out from the font's widths is refused in + such a style: a justified text or attribute, a paragraph in columns, a multileader, a dimension + and a table. A file's style is now matched to the font it names in any spelling, such as + `ROMANS.SHX`, or `cour.ttf` with no family. +- `e.change(handle, { boxHeight: 0 })` fits a multileader's note box to its text. A file can + store a box taller than its note, and the leading CAD app sizes the note's mask by that height, + so the mask can hide what lies below the note. 0 is what the app writes for a new note that does + not wrap. A multileader with no note, an annotative one, a note in columns and an entity of + another kind are refused. The picture draws the mask as tall as the note, so the change shows in + no picture. +- A read multileader's note gives `boxHeight`, the height of its box as the file stores it, when + above 0. + ## 0.6.0 tsdraft edits a firm's own drawings, with no CAD program: an edit file erases, moves, stretches, diff --git a/docs/editing.md b/docs/editing.md index 31faa76..4218776 100644 --- a/docs/editing.md +++ b/docs/editing.md @@ -195,6 +195,13 @@ and every format its text does not replace. `%%` codes or a TrueType text justified Middle, a DXF keeps the start it holds, which a CAD program works out again when it reads the file, and a DWG, which stores the start a CAD program draws from, refuses the change. +- `boxHeight: 0` fits a multileader's note box to its text. A file can store a box taller than + its note, and the leading CAD app sizes the note's mask by that height, so the mask can hide what + lies below. 0 is what the app writes for a new note that does not wrap, and the only height set. + The read gives the stored height as the note's `boxHeight`. The picture draws the mask as tall + as the note before and after, so the change shows in no picture and the changed sheets do not + list it. A multileader with no note, an annotative one, which holds a box for each scale, and a + note in columns are refused. - `attributes` changes an insert's attributes by tag, each as `text` is. - `layer`, `color` (1 to 255, 0 for ByBlock, 256 for ByLayer), `trueColor` (0xRRGGBB, with the nearest index beside it), `linetype` (one the file holds, `ByLayer` or `ByBlock`), `lineweight` @@ -206,8 +213,8 @@ and every format its text does not replace. `color`, `trueColor`, `linetype` and `lineweight`. A host changes an xref's layer, `XREF|LAYER`, in its own record, marked as the host's override so a reload of the xref keeps it, and sets `$VISRETAIN` to 1, without which a host keeps no change of an xref's layer. A DWG host whose -`VISRETAIN` is 0 holds no record of an xref's layers, and such a change is refused there. A -change of an xref layer's linetype is not made yet. +`VISRETAIN` is 0 holds no record of an xref's layers, and such a change is refused there. An +xref layer's linetype is one the host holds, its own or the xref's, such as `SURVEY|PHANTOM2`. `e.viewport(handle, changes)` re-aims a viewport: `center` is the point of model space at its middle, `ratio` its scale in paper units to drawing units, `twist` its view turned in degrees. @@ -252,6 +259,11 @@ are named `Standard`. Under a standard, a text role's and a leader style's `styl firm's style: in an edit of a file that holds it, the addition draws in it, and a new drawing that draws to a `styleName` of `Standard` is refused. Every addition names its layer. +A style of the file whose font tsdraft does not hold, such as a firm's Baskerville Old Face, takes a +text justified left and a paragraph of one column, which the leading CAD app lays out itself; a +justified text or attribute, a paragraph in columns, a multileader, a dimension and a table in it +are refused, since tsdraft would work out where they go from another font's widths. + An entity takes its own `linetypeScale`, group 48, and a dimension its own `scale`, `DIMSCALE`, which the writers write as the dimension's override of its style's, the extended data `ACAD`, `DSTYLE`. A dimension style's `lineExtension`, `DIMDLE`, is how far a tick's dimension line runs diff --git a/docs/reading.md b/docs/reading.md index a5a0c1d..8eacf8b 100644 --- a/docs/reading.md +++ b/docs/reading.md @@ -167,7 +167,9 @@ leader: does not, in `textStyle`. `attachments` says where a landing meets each side of the note, `vertical` that its landings meet the note's top or bottom, `frame` that a frame goes around the note, and `mask` that a mask is behind it: the drawing's background, or the color index `color`, - over the note's box grown by `offsetFactor` times its height less one. A block as its content + over the note's box grown by `offsetFactor` times its height less one. `boxHeight` is the height + of the note's box as the file stores it, when above 0: the leading CAD app sizes the mask by it, + and a box stored taller than the note hides what lies below. A block as its content has `text` `""`, `textAt` at the block, and `block`: its `name`, `at`, `scale`, `rotation` in degrees, and `attributes`, the value each of its labels gives, by the tag of the attribute definition it fills. A label whose definition the read does not hold is left out, and at most diff --git a/src/edit/change-shape.ts b/src/edit/change-shape.ts index de7241b..b6163b2 100644 --- a/src/edit/change-shape.ts +++ b/src/edit/change-shape.ts @@ -53,6 +53,7 @@ const ENTITY_KEYS: ReadonlySet = new Set([ "text", "height", "width", + "boxHeight", "attributes", "layer", "color", @@ -89,6 +90,9 @@ export function entityChangesProblem(value: EntityChanges): string | undefined { if (value.width !== undefined && !(finite(value.width) && value.width >= 0 && value.width < 1e15)) return "width is not 0 or more"; + if (value.boxHeight !== undefined && value.boxHeight !== 0) + return "boxHeight is not 0, the one height set: 0 fits a multileader's note box to its text"; + if (value.attributes !== undefined) { if (!isRecord(value.attributes)) return "attributes is not a record of tags"; let n = 0; diff --git a/src/edit/changed.ts b/src/edit/changed.ts index 18174d2..745aab6 100644 --- a/src/edit/changed.ts +++ b/src/edit/changed.ts @@ -175,6 +175,16 @@ export function changedEntity( out = { ...out, width: c.width }; } + if (c.boxHeight !== undefined) { + // A multileader holds a note, a block or neither, and the read gives a note's text. + if (out.kind !== "multileader" || out.block !== undefined || out.text === "") + return { + problem: `a note box is fitted only of a multileader with a note, and this is ${out.kind === "multileader" ? "one with none" : `a ${e.kind}`}`, + }; + const { boxHeight: _, ...rest } = out; + out = rest; + } + if (c.attributes !== undefined) { if (out.kind !== "insert") return { problem: `attributes are changed only of an insert, and this is a ${e.kind}` }; diff --git a/src/edit/dxf-change.ts b/src/edit/dxf-change.ts index 8745aa2..65ff4e2 100644 --- a/src/edit/dxf-change.ts +++ b/src/edit/dxf-change.ts @@ -300,8 +300,58 @@ export function textSplices( return w.out; } +/** + * The splice that fits a multileader's note box to its text: its context's own group 44 set to 0, + * as the leading CAD app writes a new note that does not wrap, or why it cannot. The context's own groups are those + * from `CONTEXT_DATA{` to its `}` outside each `LEADER{`; its first 290 says it holds a note, and + * group 293 after it that the multileader is annotative, with a context for each scale. A note in + * columns, the context's group 173, is refused: no case shows how its columns take the box. + */ +export function boxSplices(groups: readonly HeldGroup[]): Splice[] | string { + let where: "before" | "context" | "after" = "before"; + let depth = 0; + let note: HeldGroup | undefined; + let box: HeldGroup | undefined; + let columns: HeldGroup | undefined; + let annotative = false; + + for (const { g } of own(groups)) { + const mark = g.value.trim(); + + if (where === "before") { + if (g.code === 300 && mark === "CONTEXT_DATA{") where = "context"; + } else if (where === "after") annotative ||= g.code === 293 && Number(g.value) === 1; + else if (g.code === 302 && mark.endsWith("{")) depth++; + else if (depth > 0 && g.code === 303) depth--; + else if (depth === 0 && g.code === 301) where = "after"; + else if (depth === 0) { + if (g.code === 290) note ??= g; + else if (g.code === 44) box ??= g; + else if (g.code === 173) columns ??= g; + } + } + + if (annotative) return "it is annotative, with a note box for each scale"; + + if (note === undefined || Number(note.value) !== 1 || box === undefined) + return "the multileader holds no note box"; + + if (columns !== undefined && Number(columns.value) !== 0) + return "its note is in columns, whose box a fit to the text is not made for"; + + return Number(box.value) === 0 ? [] : [numberAs(box, 0)]; +} + /** The bits a host's AcXRefLayerPropertyOverride sets for each property of an xref's layer. */ -const OVERRIDES = { on: 1, frozen: 2, locked: 4, plot: 8, lineweight: 64, color: 0x110 } as const; +const OVERRIDES = { + on: 1, + frozen: 2, + locked: 4, + plot: 8, + linetype: 32, + lineweight: 64, + color: 0x110, +} as const; /** The splices of a layer's change, or a reason it cannot be made. */ export function layerSplices( @@ -372,7 +422,7 @@ export function layerSplices( // A host marks each property of an xref's layer it overrides, so a reload of the xref keeps it. let bits = 0; - for (const key of ["on", "frozen", "locked", "plot", "lineweight"] as const) + for (const key of ["on", "frozen", "locked", "plot", "linetype", "lineweight"] as const) if (c[key] !== undefined) bits |= OVERRIDES[key]; if (c.color !== undefined || c.trueColor !== undefined) bits |= OVERRIDES.color; diff --git a/src/edit/dxf-changes.ts b/src/edit/dxf-changes.ts index af14a63..65d98bf 100644 --- a/src/edit/dxf-changes.ts +++ b/src/edit/dxf-changes.ts @@ -6,6 +6,7 @@ import type { DxfDocument } from "../read/document.ts"; import type { DrawingRead, ReadAttribute, ReadEntity } from "../read/model.ts"; import { changedEntity, changedViewport, layerNamed, linetypeNamed } from "./changed.ts"; import { + boxSplices, headerSplice, layerSplices, penSplices, @@ -48,17 +49,21 @@ export interface ChangeEdits { readonly erased: ReadonlyMap; } -/** The dictionary of a text's fields, which its extension dictionary names `ACAD_FIELD`, or undefined. */ -function fieldsOf(r: ChangeRecords, i: number): number | undefined { +/** The record its extension dictionary names `name`, or undefined. */ +function extensionOf(r: ChangeRecords, i: number, name: string): number | undefined { const groups = r.groups(i); const at = groups.findIndex((g) => g.code === 102 && g.value.trim() === "{ACAD_XDICTIONARY"); const h = at >= 0 ? groups[at + 1]?.value.trim() : undefined; const x = h === undefined ? undefined : r.byHandle.get(canonicalHandle(h)); - const f = x === undefined ? undefined : r.dictionaryEntry(x, "ACAD_FIELD"); + const f = x === undefined ? undefined : r.dictionaryEntry(x, name); return f === undefined ? undefined : r.byHandle.get(canonicalHandle(f)); } +/** The dictionary of a text's fields, which its extension dictionary names `ACAD_FIELD`, or undefined. */ +const fieldsOf = (r: ChangeRecords, i: number): number | undefined => + extensionOf(r, i, "ACAD_FIELD"); + /** The changes of a text a change writes, from the entity before and after it. */ function textValues( before: ReadEntity | ReadAttribute, @@ -202,6 +207,15 @@ export function changeSplices(r: ChangeRecords, edit: Edit, read: DrawingRead): add(i, textSplices(r.groups(i), place, textValues(before, after, c.text, c)), statement, h); } + if (c.boxHeight !== undefined) { + if (kind !== "MULTILEADER") + throw new EditError(`${h} is a ${kind}, which holds no note box`, statement); + + if (extensionOf(r, i, "AcDbContextDataManager") !== undefined) + throw new EditError(`${h} is annotative, with a note box for each scale`, statement); + add(i, boxSplices(r.groups(i)), statement, h); + } + if (c.attributes !== undefined && before.kind === "insert" && after.kind === "insert") { const list = before.attributes ?? []; diff --git a/src/edit/file-tables.ts b/src/edit/file-tables.ts index 125f8f4..fe23607 100644 --- a/src/edit/file-tables.ts +++ b/src/edit/file-tables.ts @@ -13,7 +13,7 @@ import type { TableStyle, TextStyle, } from "../drawing.ts"; -import { STANDARD_TEXT_STYLE } from "../fonts.ts"; +import { nearestFont, STANDARD_TEXT_STYLE } from "../fonts.ts"; import { isString } from "../guards.ts"; import { layerKey } from "../layers.ts"; import type { @@ -62,11 +62,17 @@ function linetypeOf(l: ReadLinetype): Linetype { return { name: l.name, description: l.description ?? "", pattern: [...l.pattern] }; } -/** A text style by the font it names: a TrueType family where it names one, else its file. */ +/** + * A text style by the font of `FONTS` it names, by its TrueType family or else its file, in any + * spelling `nearestFont` takes. A font not in `FONTS`, such as a firm's Baskerville Old Face, + * stands in as the nearest one: an edit writes no style of the file's, so the stand-in only + * measures what the additions draw, and `lintEdit` refuses an addition whose layout the writers + * would work out from the stand-in's widths. + */ function textStyleOf(s: ReadTextStyle): TextStyle { return { name: s.name, - font: s.family ?? s.font ?? "txt.shx", + font: nearestFont(s.family ?? s.font ?? "txt.shx").font, ...(s.widthFactor !== undefined && s.widthFactor !== 1 && { widthFactor: s.widthFactor }), ...(s.oblique !== undefined && s.oblique !== 0 && { obliqueAngle: s.oblique }), }; diff --git a/src/edit/lint-change.ts b/src/edit/lint-change.ts index f639c7f..405cbeb 100644 --- a/src/edit/lint-change.ts +++ b/src/edit/lint-change.ts @@ -176,15 +176,6 @@ export function layerChangeProblems( ), ); - if (layer.xref && c.linetype !== undefined) - // The bit a host's AcXRefLayerPropertyOverride sets for a linetype is not known yet. - out.push( - error( - "edit-layer", - `${layer.name} is an xref's layer, whose linetype a host's change is not made of yet`, - ), - ); - const current = read.header.clayer ?? "0"; if (c.frozen === true && layerKey(layer.name) === layerKey(current)) diff --git a/src/edit/lint.ts b/src/edit/lint.ts index 81a1d01..a8dd268 100644 --- a/src/edit/lint.ts +++ b/src/edit/lint.ts @@ -2,9 +2,10 @@ // and stays bounded on any value, as `lint` does: the edit's shape is checked first, each handle is // paid for before it is worked, and what goes past the limit is one diagnostic. import type { CallSite } from "../callsite.ts"; +import { nearestFont } from "../fonts.ts"; import { layerKey } from "../layers.ts"; import { OUTSIDE_THE_READ, partFilter, type PartFilter } from "../read/part.ts"; -import type { DrawingRead, ReadEntity } from "../read/model.ts"; +import type { DrawingRead, ReadEntity, ReadTable } from "../read/model.ts"; import { addedOf, readAdditionsFile, type Added } from "./added.ts"; import { canonicalHandle, checkEdit, MADE_KINDS, statementCall, type Edit } from "./model.ts"; import { replacement, writesAdditions } from "./replaced.ts"; @@ -29,6 +30,16 @@ const absoluteHeight = (value: string): boolean => { return false; }; +/** What a refusal in a block calls an entity of each kind tsdraft lays out itself. */ +const KIND_NAMES: Partial> = { + text: "a justified text", + insert: "an insert with a justified attribute", + mtext: "a paragraph in columns", + multileader: "a multileader", + dimension: "a dimension", + table: "a table", +}; + /** True when an entity holds paragraph text with a height of its own, which a scale leaves. */ function ownHeight(e: ReadEntity): boolean { switch (e.kind) { @@ -649,28 +660,117 @@ function lintAdditions(edit: Edit, read: DrawingRead, report: (d: EditDiagnostic read.dimensionStyles.filter((d) => (d.textLevelInside ?? 1) === 1).map((d) => layerKey(d.name)), ); - // The file's own Standard, whose font the builder does not take: it measures Standard in its - // own font, so a justified text there would start where the file's font does not put it. - const standard = read.textStyles.find((t) => layerKey(t.name) === "standard"); + // The file's styles whose font the builder does not take, so a justified text in one would + // start where the file's font does not put it: its own Standard, which the builder measures in + // txt.shx, and a style in a font not in FONTS, which it measures in a stand-in. + const unmeasured = new Map( + read.textStyles + .filter((t) => + layerKey(t.name) === "standard" + ? (t.font ?? "").toLowerCase().replace(/\.shx$/, "") !== "txt" + : !nearestFont(t.family ?? t.font ?? "txt.shx").exact, + ) + .map((t) => [layerKey(t.name), t.name]), + ); + + // Of those, the styles in a stand-in font, where every width the writers work out from the + // font would be the stand-in's: what the leading CAD app lays out itself is all they take. + const standIn = (name: string | undefined): string | undefined => + name === undefined || layerKey(name) === "standard" + ? undefined + : unmeasured.get(layerKey(name)); + + // The file's styles and those the edit adds alike. + const dimensionStyles = [...read.dimensionStyles, ...added.dimensionStyles]; + const tableStyles = [...read.tableStyles, ...added.tableStyles]; + + const dimensionText = (style: string): string | undefined => + standIn(dimensionStyles.find((s) => layerKey(s.name) === layerKey(style))?.textStyle); + + // Of a table, the cell styles of the rows that hold text: the title's, the header's, the data's. + const tableText = (t: ReadTable): string | undefined => { + const s = tableStyles.find( + (x) => t.style !== undefined && layerKey(x.name) === layerKey(t.style), + ); + + if (s === undefined) return undefined; + const first = (t.title ? 1 : 0) + (t.header ? 1 : 0); + + const holds = (from: number, to: number): boolean => + t.cells.some((c) => c.row >= from && c.row < to && (c.text ?? "") !== ""); + + return [ + t.title && holds(0, 1) ? s.title : undefined, + t.header && holds(first - 1, first) ? s.header : undefined, + holds(first, Infinity) ? s.data : undefined, + ] + .map((c) => standIn(c?.textStyle)) + .find((n) => n); + }; + + // The stand-in style a kind tsdraft lays out itself is drawn in, if any. + const laidOutIn = (e: ReadEntity): string | undefined => { + switch (e.kind) { + case "insert": + return e.attributes + ?.filter((a) => a.alignAt !== undefined && a.paragraph === undefined) + .map((a) => standIn(a.style)) + .find((n) => n); + case "mtext": + return e.columns === undefined ? undefined : standIn(e.style); + case "multileader": + return standIn(e.textStyle); + case "dimension": + return dimensionText(e.style); + case "table": + return tableText(e); + default: + return undefined; + } + }; - const otherStandard = - standard !== undefined && (standard.font ?? "").toLowerCase().replace(/\.shx$/, "") !== "txt"; + // A block the edit defines is drawn where each insert puts it, as tsdraft laid it out. + for (const b of added.blocks) + for (const e of b.entities) { + const style = + (e.kind === "text" && e.alignAt !== undefined + ? unmeasured.get(layerKey(e.style)) + : undefined) ?? laidOutIn(e); + + if (style !== undefined) + report({ + rule: "edit-addition", + severity: "error", + ...(e.handle !== undefined && { handle: e.handle }), + message: `The block ${b.name} the edit adds holds ${KIND_NAMES[e.kind] ?? e.kind} laid out by tsdraft in the file's text style ${style}, whose font tsdraft does not measure it in: only a text justified left and a paragraph of one column may be drawn in it`, + }); + } for (const space of added.spaces) for (const e of space.entities) { - if ( - otherStandard && - e.kind === "text" && - e.alignAt !== undefined && - layerKey(e.style) === "standard" - ) + const style = e.kind === "text" ? unmeasured.get(layerKey(e.style)) : undefined; + const id = e.mark?.id ?? e.handle ?? ""; + + const refuse = (message: string): void => report({ rule: "edit-addition", severity: "error", ...(e.handle !== undefined && { handle: e.handle }), - message: `The addition ${e.mark?.id ?? e.handle ?? ""} is a justified text in the file's Standard, whose font tsdraft does not measure it in: justify it left, or name another of the file's styles`, + message, }); + if (e.kind === "text" && e.alignAt !== undefined && style !== undefined) + refuse( + `The addition ${id} is a justified text in the file's ${style}, whose font tsdraft does not measure it in: justify it left, or name another of the file's styles`, + ); + + const laidOut = laidOutIn(e); + + if (laidOut !== undefined) + refuse( + `The addition ${id} is laid out by tsdraft in the file's text style ${laidOut}, whose font tsdraft does not hold: only a text justified left and a paragraph of one column may be drawn in it`, + ); + // tsdraft draws a dimension's text along its line, which a style that keeps it level // draws otherwise wherever the line is not level. if (e.kind === "dimension" && levelText.has(layerKey(e.style))) { diff --git a/src/edit/marks.ts b/src/edit/marks.ts index 68fe845..c684dae 100644 --- a/src/edit/marks.ts +++ b/src/edit/marks.ts @@ -46,7 +46,8 @@ const DIMENSION = ["textAt", "text", "textBelow", "style"]; /** * The fields of each kind format 1 hashes. Left out: the handle, the mark, the order of drawing, and * what the leading CAD app works out again of itself: a justified text's start, a dimension's - * picture and measurement, a fit spline's knots, and the handles of what an entity names. + * picture and measurement, a fit spline's knots, a multileader's stored note box, which its save + * of a wrapped note sets, and the handles of what an entity names. */ const MARK_FIELDS = new Map( Object.entries({ diff --git a/src/edit/model.ts b/src/edit/model.ts index f7e88a9..1cedd88 100644 --- a/src/edit/model.ts +++ b/src/edit/model.ts @@ -96,6 +96,11 @@ export interface EntityChanges { /** A text's height, and paragraph text's height and the width it wraps at, 0 for none. */ readonly height?: number; readonly width?: number; + /** + * A multileader's note box, as tall as its text: the box the file stores can be taller, and the + * leading CAD app sizes the note's mask by it. Only 0, which fits the box to the text, is set. + */ + readonly boxHeight?: 0; /** An insert's attributes, by tag. */ readonly attributes?: Readonly>; readonly layer?: string; diff --git a/src/edit/moved.ts b/src/edit/moved.ts index c6e2a40..79dae0e 100644 --- a/src/edit/moved.ts +++ b/src/edit/moved.ts @@ -238,6 +238,7 @@ export function movedEntity(e: ReadEntity, t: Applied): ReadEntity { roots: e.roots.map((r) => root(r, t)), textAt: t.point(e.textAt), width: t.size(e.width), + ...(e.boxHeight !== undefined && { boxHeight: t.size(e.boxHeight) }), textHeight: t.size(e.textHeight), arrowSize: t.size(e.arrowSize), ...(e.block !== undefined && { diff --git a/src/read/check.ts b/src/read/check.ts index ffebe6a..0cd975c 100644 --- a/src/read/check.ts +++ b/src/read/check.ts @@ -378,6 +378,7 @@ const ENTITY: EntityFields = { textAt: "point", textAttachment: "number", width: "number", + "boxHeight?": "number", textHeight: "number", textStyle: "string", arrowSize: "number", diff --git a/src/read/entities.ts b/src/read/entities.ts index cdc7efa..13cf8ca 100644 --- a/src/read/entities.ts +++ b/src/read/entities.ts @@ -1409,10 +1409,19 @@ function multileader(r: Rec, base: ReadEntityBase, later: Later): Reading { const context: Record = {}; const after: Record = {}; const labels: { definition: string | undefined; text: string }[] = []; + let braces = false; for (let i = 0; i < c.length; i++) { const code = c[i]!; const value = v[i]!; + + // An application's groups in braces are its own, not the multileader's. + if (code === 102) { + braces = value.trim().startsWith("{"); + continue; + } + + if (braces) continue; // The groups that open and close a part, as the leading CAD app writes them, may carry spaces. const mark = code >= 300 && code <= 305 ? value.trim() : value; @@ -1520,6 +1529,7 @@ function multileader(r: Rec, base: ReadEntityBase, later: Later): Reading { textAt, textAttachment: hasText ? number(context, 171, 1) : 1, width: hasText ? number(context, 43, 0) : 0, + ...(hasText && number(context, 44, 0) > 0 && { boxHeight: number(context, 44, 0) }), textHeight: number(context, 41, 0), textStyle: "Standard", arrowSize: number(context, 140, 0), diff --git a/src/read/model.ts b/src/read/model.ts index d4b46e7..297defe 100644 --- a/src/read/model.ts +++ b/src/read/model.ts @@ -812,6 +812,11 @@ export interface ReadMultileader extends ReadEntityBase { readonly textAttachment: number; /** The width the note wraps at, or 0 when it does not wrap. */ readonly width: number; + /** + * The height of the note's box as the file stores it, when above 0. The leading CAD app sizes the + * note's mask by it, though it can be taller than the note. + */ + readonly boxHeight?: number; /** The height of the note's text. */ readonly textHeight: number; /** The note's text style. */ diff --git a/test/edit-additions.test.ts b/test/edit-additions.test.ts index 7016341..28621ba 100644 --- a/test/edit-additions.test.ts +++ b/test/edit-additions.test.ts @@ -314,3 +314,99 @@ test("an addition with no id on a layout named layers is erased by an edit of it [], ); }); + +test("a style of the file in a font tsdraft does not hold takes a paragraph, and refuses what tsdraft would lay out in it", async () => { + const d = createDrawing({ units: "in" }); + d.textStyle({ name: "TB-TITLE", font: "Arial" }); + d.textStyle({ name: "CAPS", font: "romans.shx" }); + d.dimensionStyle({ + name: "TB-DIMS", + textStyle: "TB-TITLE", + textHeight: 2, + arrowhead: "closed-filled", + arrowSize: 1, + extensionOffset: 0.5, + extensionBeyond: 1, + textGap: 0.5, + scale: 1, + precision: 1, + }); + d.block("TAG", {}, () => + d.attribute("NO", [0, 0], { value: "1", height: 2, style: "TB-TITLE", align: "center" }), + ); + d.layer("A-NOTE", { color: 2 }, () => d.line([0, 0], [100, 0])); + + // The file names Baskerville Old Face, and romans.shx in capitals, as an older file may. + const bytes = new TextEncoder().encode( + toDXF(d.build()) + .replace("\r\n3\r\narial.ttf\r\n", "\r\n3\r\nBASKVILL.TTF\r\n") + .replace("\r\n1000\r\nArial\r\n", "\r\n1000\r\nBaskerville Old Face\r\n") + .replace("\r\n3\r\nromans.shx\r\n", "\r\n3\r\nROMANS.SHX\r\n"), + ); + + const { errors, out } = await edited(bytes, "rev-1", (e) => + e.model((d) => + d.layer("A-NOTE", () => { + d.paragraph("{\\LNOTE:}", { at: [0, 10], height: 2, style: "TB-TITLE", id: "head" }); + d.text([0, 20], "CAPS", { height: 2, style: "CAPS", align: "center", id: "caps" }); + }), + ), + ); + + assert.deepEqual(errors, []); + const back = await geometry(out!); + + assert.equal(back.textStyles.find((s) => s.name === "TB-TITLE")?.family, "Baskerville Old Face"); + const head = added(back).find((e) => e.mark?.id === "head"); + + assert.equal(head?.kind === "mtext" && head.style, "TB-TITLE"); + + type Builder = Parameters[0]>[0]; + + const refused = async (draw: (d: Builder) => void): Promise => + (await edited(bytes, "rev-1", (e) => e.model((d) => d.layer("A-NOTE", () => draw(d))))) + .errors[0]?.message ?? ""; + + assert.match( + await refused((d) => + d.text([0, 10], "NOTE", { height: 2, style: "TB-TITLE", align: "center" }), + ), + /justified text in the file's TB-TITLE/, + ); + assert.match( + await refused((d) => d.insert("TAG", [0, 0], { attributes: { NO: "WIDE VALUE" } })), + /laid out by tsdraft in the file's text style TB-TITLE/, + ); + assert.match( + await refused((d) => d.linearDimension([0, 0], [8, 0], [0, -5], { style: "TB-DIMS" })), + /laid out by tsdraft in the file's text style TB-TITLE/, + ); + // A style and a block the edit defines itself are held to the same. + assert.match( + await refused((d) => { + d.dimensionStyle({ + name: "NEW-DIMS", + textStyle: "TB-TITLE", + textHeight: 2, + arrowhead: "closed-filled", + arrowSize: 1, + extensionOffset: 0.5, + extensionBeyond: 1, + textGap: 0.5, + scale: 1, + precision: 1, + }); + d.linearDimension([0, 0], [8, 0], [0, -5], { style: "NEW-DIMS" }); + }), + /laid out by tsdraft in the file's text style TB-TITLE/, + ); + assert.match( + await refused((d) => { + d.block("SIGN", {}, () => + d.text([0, 0], "SIGN", { height: 2, style: "TB-TITLE", align: "center" }), + ); + d.insert("SIGN", [0, 0]); + }), + /block SIGN the edit adds holds a justified text laid out by tsdraft in the file's text style TB-TITLE/, + ); +});