Skip to content

Commit

Permalink
Tolerate missing text height in MTEXT entity.
Browse files Browse the repository at this point in the history
  • Loading branch information
vagran committed Jun 14, 2024
1 parent 33cbb98 commit a5b68b2
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 17 deletions.
12 changes: 12 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,17 @@
}
}
},
{
"label": "npm publish",
"type": "process",
"command": "npm",
"args": ["publish"],
"options": {
"cwd": "${workspaceFolder}",
"env": {
"PATH": "${config:env.nodeDir}/bin:${env:PATH}"
}
}
}
]
}
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dxf-viewer",
"version": "1.0.32",
"version": "1.0.33",
"description": "JavaScript DXF file viewer",
"main": "src/index.js",
"author": "Artyom Lebedev<artyom.lebedev@gmail.com>",
Expand All @@ -19,12 +19,13 @@
"src",
"README.md",
"LICENSE",
"CONTRIBUTORS"
"CONTRIBUTORS",
"CONTRIBUTING.md"
],
"dependencies": {
"three": "^0.161.0",
"loglevel": "^1.9.1",
"opentype.js": "^1.3.4"
"opentype.js": "^1.3.4",
"three": "^0.161.0"
},
"devDependencies": {
"@types/three": "^0.161.2"
Expand Down
17 changes: 15 additions & 2 deletions src/DxfScene.js
Original file line number Diff line number Diff line change
Expand Up @@ -855,9 +855,11 @@ export class DxfScene {
}
const layer = this._GetEntityLayer(entity, blockCtx)
const color = this._GetEntityColor(entity, blockCtx)
const style = this._GetEntityTextStyle(entity)
const fixedHeight = style?.fixedTextHeight === 0 ? null : style?.fixedTextHeight
yield* this.textRenderer.Render({
text: ParseSpecialChars(entity.text),
fontSize: entity.textHeight,
fontSize: entity.textHeight ?? (fixedHeight ?? 1),
startPos: entity.startPoint,
endPos: entity.endPoint,
rotation: entity.rotation,
Expand All @@ -874,11 +876,14 @@ export class DxfScene {
}
const layer = this._GetEntityLayer(entity, blockCtx)
const color = this._GetEntityColor(entity, blockCtx)
const style = this._GetEntityTextStyle(entity)
const fixedHeight = style?.fixedTextHeight === 0 ? null : style?.fixedTextHeight
const parser = new MTextFormatParser()
parser.Parse(ParseSpecialChars(entity.text))
yield* this.textRenderer.RenderMText({
formattedText: parser.GetContent(),
fontSize: entity.height,
// May still be overwritten by inline formatting codes
fontSize: entity.height ?? fixedHeight,
position: entity.position,
rotation: entity.rotation,
direction: entity.direction,
Expand Down Expand Up @@ -2030,6 +2035,14 @@ export class DxfScene {
return blockCtx ? null : "0"
}

/** @returns {TextStyle | null} */
_GetEntityTextStyle(entity) {
if (entity.hasOwnProperty("styleName")) {
return this.fontStyles.get(entity.styleName) ?? null
}
return null
}

/** Check extrusionDirection property of the entity and return corresponding transform matrix.
*
* @return {?Matrix3} Null if not transform required.
Expand Down
26 changes: 15 additions & 11 deletions src/TextRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,16 @@ export class TextRenderer {
}

/**
* @param text {string}
* @param startPos {{x,y}}
* @param endPos {?{x,y}} TEXT group second alignment point.
* @param rotation {?number} Rotation attribute, deg.
* @param widthFactor {?number} Relative X scale factor (group 41)
* @param hAlign {?number} Horizontal text justification type code (group 72)
* @param vAlign {?number} Vertical text justification type code (group 73).
* @param color {number}
* @param layer {?string}
* @param fontSize {number}
* @param {string} text
* @param {{x,y}} startPos
* @param {?{x,y}} endPos TEXT group second alignment point.
* @param {?number} rotation Rotation attribute, deg.
* @param {?number} widthFactor Relative X scale factor (group 41)
* @param {?number} hAlign Horizontal text justification type code (group 72)
* @param {?number} vAlign Vertical text justification type code (group 73).
* @param {number} color
* @param {?string} layer
* @param {number} fontSize Font size.
* @return {Generator<Entity>} Rendering entities. Currently just indexed triangles for each
* glyph.
*/
Expand All @@ -176,7 +176,8 @@ export class TextRenderer {
/**
* @param {MTextFormatEntity[]} formattedText Parsed formatted text.
* @param {{x, y}} position Insertion position.
* @param {Number} fontSize
* @param {?number} fontSize If not specified, then it still may be defined by inline
* formatting codes, otherwise 1 is used as fall-back value.
* @param {?Number} width Text block width, no wrapping if undefined.
* @param {?Number} rotation Text block rotation in degrees.
* @param {?{x, y}} direction Text block orientation defined as direction vector. Takes a
Expand All @@ -190,6 +191,9 @@ export class TextRenderer {
*/
*RenderMText({formattedText, position, fontSize, width = null, rotation = 0, direction = null,
attachment, lineSpacing = 1, color, layer = null}) {
if (!fontSize) {
fontSize = 1;
}
const box = new TextBox(fontSize, this._GetCharShape.bind(this))
box.FeedText(formattedText)
yield* box.Render(position, width, rotation, direction, attachment, lineSpacing, color,
Expand Down
3 changes: 3 additions & 0 deletions src/parser/entities/mtext.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ EntityParser.prototype.parseEntity = function(scanner, curr) {
case 50:
entity.rotation = curr.value;
break;
case 7: // Text style name
entity.styleName = curr.value;
break;
case 71:
entity.attachmentPoint = curr.value;
break;
Expand Down
3 changes: 3 additions & 0 deletions src/parser/entities/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ EntityParser.prototype.parseEntity = function(scanner, curr) {
case 50: // Rotation in degrees
entity.rotation = curr.value;
break;
case 7: // Text style name
entity.styleName = curr.value;
break;
case 1: // Text
entity.text = curr.value;
break;
Expand Down

0 comments on commit a5b68b2

Please sign in to comment.