|
| 1 | +import * as THREE from 'three' |
| 2 | +import { afterEach, describe, expect, it } from 'vitest' |
| 3 | +import fs from 'node:fs' |
| 4 | +import path from 'node:path' |
| 5 | + |
| 6 | +import { FontData } from '../../src/font/font' |
| 7 | +import { FontFactory } from '../../src/font/fontFactory' |
| 8 | +import { FontManager } from '../../src/font/fontManager' |
| 9 | +import { MText } from '../../src/renderer/mtext' |
| 10 | +import { |
| 11 | + createDefaultColorSettings, |
| 12 | + MTextAttachmentPoint, |
| 13 | + TextStyle |
| 14 | +} from '../../src/renderer/types' |
| 15 | + |
| 16 | +const FONT_BASE = 'https://cdn.jsdelivr.net/gh/mlightcad/cad-data/fonts/' |
| 17 | +const LOCAL_SIMFANG = path.resolve('D:/code/cad-data/fonts/simfang.woff') |
| 18 | + |
| 19 | +async function loadSimfangFont(): Promise<void> { |
| 20 | + let data: ArrayBuffer |
| 21 | + if (fs.existsSync(LOCAL_SIMFANG)) { |
| 22 | + const buffer = fs.readFileSync(LOCAL_SIMFANG) |
| 23 | + data = buffer.buffer.slice( |
| 24 | + buffer.byteOffset, |
| 25 | + buffer.byteOffset + buffer.byteLength |
| 26 | + ) |
| 27 | + } else { |
| 28 | + const response = await fetch(FONT_BASE + 'simfang.woff') |
| 29 | + if (!response.ok) throw new Error(`Failed to fetch simfang.woff`) |
| 30 | + data = await response.arrayBuffer() |
| 31 | + } |
| 32 | + |
| 33 | + const aliases = ['simfang', 'SIMFANG', '仿宋体', '仿宋'] |
| 34 | + const fontData: FontData = { |
| 35 | + name: 'simfang', |
| 36 | + type: 'mesh', |
| 37 | + data, |
| 38 | + alias: aliases |
| 39 | + } |
| 40 | + const font = FontFactory.instance.createFont(fontData) |
| 41 | + for (const alias of aliases) { |
| 42 | + font.names.add(alias) |
| 43 | + ;( |
| 44 | + FontManager.instance as unknown as { |
| 45 | + loadedFontMap: Map<string, unknown> |
| 46 | + } |
| 47 | + ).loadedFontMap.set(alias.toLowerCase(), font) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +const styleManager = { |
| 52 | + unsupportedTextStyles: {}, |
| 53 | + getMeshBasicMaterial: () => new THREE.MeshBasicMaterial(), |
| 54 | + getLineBasicMaterial: () => new THREE.LineBasicMaterial() |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Regression for title-block MTEXT that AutoCAD keeps as two explicit \\P lines |
| 59 | + * when the style uses a CJK TrueType face (SimFang). Latin-'A'-based mesh scale |
| 60 | + * previously inflated advances (~1.4×) and forced soft wraps inside the defined |
| 61 | + * width. |
| 62 | + */ |
| 63 | +describe('title-block MTEXT wrap (问题四)', () => { |
| 64 | + const style: TextStyle = { |
| 65 | + name: '仿宋体', |
| 66 | + standardFlag: 0, |
| 67 | + fixedTextHeight: 0, |
| 68 | + widthFactor: 1, |
| 69 | + obliqueAngle: 0, |
| 70 | + textGenerationFlag: 0, |
| 71 | + lastHeight: 67.5, |
| 72 | + font: 'SIMFANG', |
| 73 | + bigFont: '' |
| 74 | + } |
| 75 | + |
| 76 | + afterEach(() => { |
| 77 | + FontManager.instance.release() |
| 78 | + FontManager.instance.enableFontCache = true |
| 79 | + }) |
| 80 | + |
| 81 | + it( |
| 82 | + 'keeps each explicit paragraph line unwrapped inside AutoCAD defined width', |
| 83 | + async () => { |
| 84 | + FontManager.instance.release() |
| 85 | + FontManager.instance.enableFontCache = false |
| 86 | + FontManager.instance.setDefaultFonts('modern') |
| 87 | + await loadSimfangFont() |
| 88 | + |
| 89 | + expect(FontManager.instance.getFontScaleFactor('SIMFANG')).toBe(1) |
| 90 | + |
| 91 | + const text = |
| 92 | + '{\\T1.45; 熊集镇赵庙等6个村高标准农田建设项目规划图\\P(枣阳市2017年农业综合开发高标准农田建设项目)}' |
| 93 | + const width = 2252.7342659142396 |
| 94 | + const height = 67.5 |
| 95 | + |
| 96 | + const unconstrained = new MText( |
| 97 | + { |
| 98 | + text, |
| 99 | + height, |
| 100 | + width: 0, |
| 101 | + position: { x: 0, y: 0, z: 0 }, |
| 102 | + attachmentPoint: MTextAttachmentPoint.TopLeft, |
| 103 | + collectCharBoxes: true |
| 104 | + }, |
| 105 | + style, |
| 106 | + styleManager as any, |
| 107 | + FontManager.instance as any, |
| 108 | + createDefaultColorSettings() |
| 109 | + ) |
| 110 | + unconstrained.syncDraw() |
| 111 | + |
| 112 | + const wrapped = new MText( |
| 113 | + { |
| 114 | + text, |
| 115 | + height, |
| 116 | + width, |
| 117 | + position: { x: 0, y: 0, z: 0 }, |
| 118 | + attachmentPoint: MTextAttachmentPoint.TopLeft, |
| 119 | + collectCharBoxes: true |
| 120 | + }, |
| 121 | + style, |
| 122 | + styleManager as any, |
| 123 | + FontManager.instance as any, |
| 124 | + createDefaultColorSettings() |
| 125 | + ) |
| 126 | + wrapped.syncDraw() |
| 127 | + |
| 128 | + const softBreaks: number[] = [] |
| 129 | + let lineCount = 0 |
| 130 | + wrapped.traverse(obj => { |
| 131 | + const lines = obj.userData?.lineLayouts as |
| 132 | + | Array<{ breakIndex?: number }> |
| 133 | + | undefined |
| 134 | + if (!lines?.length) return |
| 135 | + lineCount = Math.max(lineCount, lines.length) |
| 136 | + lines.forEach(line => { |
| 137 | + if (line.breakIndex != null) softBreaks.push(line.breakIndex) |
| 138 | + }) |
| 139 | + }) |
| 140 | + |
| 141 | + const uBox = unconstrained.box |
| 142 | + const wBox = wrapped.box |
| 143 | + const unconstrainedWidth = uBox.max.x - uBox.min.x |
| 144 | + const wrappedHeight = wBox.max.y - wBox.min.y |
| 145 | + const unconstrainedHeight = uBox.max.y - uBox.min.y |
| 146 | + |
| 147 | + expect(FontManager.instance.getFontScaleFactor('SIMFANG')).toBe(1) |
| 148 | + expect(unconstrainedWidth).toBeLessThanOrEqual(width + 1) |
| 149 | + // Two explicit \\P lines only — soft wrap would add more height/lines. |
| 150 | + expect(lineCount).toBe(2) |
| 151 | + expect(wrappedHeight).toBeLessThanOrEqual(unconstrainedHeight + 1) |
| 152 | + // One breakIndex for the explicit \\P between the two visual lines. |
| 153 | + expect(softBreaks.length).toBe(1) |
| 154 | + }, |
| 155 | + 120_000 |
| 156 | + ) |
| 157 | +}) |
0 commit comments