forked from ieskudero/three-dxf-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextEntity.js
266 lines (212 loc) · 7.87 KB
/
textEntity.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import { BaseEntity } from './baseEntity/baseEntity';
import { Mesh, Group,
ShapeGeometry,
Quaternion } from 'three';
import { MTextFormatParser } from '../text/MTextFormatParser.js';
import { Properties } from './baseEntity/properties';
/**
* @class textEntity
* @see {@link baseEntity/BaseEntity.md}
* @classdesc DXF text, mtext && attrib entity class.
*/
export class TextEntity extends BaseEntity {
constructor( data, font ) {
super( data );
this._font = font;
this._textParser = new MTextFormatParser();
}
/**
* It filters all the text, mtext and attrib entities and draw them.
* @param data {DXFData} dxf parsed data.
* @return {THREE.Group} ThreeJS object with all the generated geometry. DXF entity is added into userData
*/
draw( data ) {
//get all texts
let entities = data.entities.filter( entity => entity.type === 'MTEXT' || entity.type === 'TEXT' || entity.type === 'ATTRIB' );
if( entities.length === 0 ) return null;
let group = new Group();
group.name = 'TEXTS';
for ( let i = 0; i < entities.length; i++ ) {
let entity = entities[i];
if( this._hideEntity( entity ) ) continue;
let cached = this._getCached( entity );
let geometry = null;
let material = null;
if( cached ) {
geometry = cached.geometry;
material = cached.material;
} else {
let _drawData = this.drawText( entity );
geometry = _drawData.geometry;
material = _drawData.material;
if( !Properties.onBeforeTextDraw ) this._setCache( entity, _drawData );
}
//create mesh
let mesh = new Mesh( geometry, material );
mesh.userData = { entity: entity };
//add to group
group.add( mesh );
}
return group;
}
/**
* Draws text, mtext and attrib entities.
* @param entity {entity} dxf parsed text, mtext or attrib entity.
* @return {Object} object composed as {geometry: THREE.Geometry, material: THREE.Material}
*/
drawText( entity ) {
if( entity.type === 'ATTRIB' ) {
if( Object.keys( entity.mtext ).length !== 0 )
return this.drawText( entity.mtext );
else
return this.drawText( entity.text );
}
//get string
let geometry = this._getTextGeometry( entity );
this._scaleText( geometry, entity );
let posAndRot = this._getPosAndRotation( entity );
if( posAndRot.rotation ) geometry.applyQuaternion( posAndRot.rotation );
geometry.translate( posAndRot.pos.x , posAndRot.pos.y, posAndRot.pos.z );
this._translateCenter( geometry, entity, posAndRot.pos );
//get material
let material = this._colorHelper.getMaterial( entity, 'shape', this.data.tables );
return { geometry: geometry, material: material };
}
_getTextGeometry( entity ) {
let strings = this._getTextStrings( entity );
//TODO: if any string's width exceeds entity.refRectangleWidth we must cut it
let text = strings.join( '' );
if( Properties.onBeforeTextDraw ) {
let json = { text: text };
Properties.onBeforeTextDraw( json );
text = json.text;
}
//get size
let textSize = this._getTextHeight( entity );
//generate shape
let shapes = this._font.generateShapes( text, textSize );
return new ShapeGeometry( shapes );
}
_getTextHeight( entity ) {
let textSize = 12;
if ( typeof entity.nominalTextHeight !== 'undefined' )
textSize = entity.nominalTextHeight;
else if ( typeof entity.textHeight !== 'undefined' )
textSize = entity.textHeight;
return textSize;
}
_scaleText( geometry, entity ) {
geometry.computeBoundingBox();
let width = geometry.boundingBox.max.x - geometry.boundingBox.min.x;
let height = geometry.boundingBox.max.y - geometry.boundingBox.min.y;
let scaleX = typeof entity.horizontalWidth !== 'undefined' ? entity.horizontalWidth / width : 1;
let scaleY = typeof entity.verticalHeight !== 'undefined' ? entity.verticalHeight / height : 1;
geometry.scale( scaleX, scaleY, 1 );
}
_getTextStrings( entity ) {
let getContent = ( entities ) => {
let strs = [];
for ( let i = 0; i < entities.length; i++ ) {
const content = entities[i];
if( content.type === MTextFormatParser.EntityType.PARAGRAPH || content.type === MTextFormatParser.EntityType.PARAGRAPH_ALIGNMENT )
strs.push( '\n' );
if( typeof content.content === 'string' )
strs.push( content.content );
else if( content.content instanceof Array ) {
let temp = getContent( content.content );
if( temp ) strs.push( temp );
}
}
return strs;
};
let parser = new MTextFormatParser().Parse( entity.string );
let text = getContent( parser.entities );
return text;
}
_getPosAndRotation( entity ) {
let result = { pos: { x: 0, y: 0, z: 0 }, rotation: null };
let x = entity.x;
let y = entity.y;
let z = entity.z;
let rotation = entity.rotation;
let xAxisX = entity.xAxisX;
let xAxisY = entity.xAxisY;
let xAxisZ = entity.xAxisZ;
let drawingDirection = entity.drawingDirection;
let xt = x.toFixed( Properties.decimals );
let yt = y.toFixed( Properties.decimals );
let zt = z.toFixed( Properties.decimals );
let zeros = '0'.repeat( Properties.decimals );
let zero = '0.' + zeros;
let one = '1.' + zeros;
let axisOnXYZ = ( ( xt === one && yt === zero && zt === zero ) ||
( xt === zero && yt === one && zt === zero ) ||
( xt === zero && yt === zero && zt === one ) );
result.pos.x = axisOnXYZ ? xAxisX : x;
result.pos.y = axisOnXYZ ? xAxisY : y;
result.pos.z = axisOnXYZ ? xAxisZ : z;
if( rotation )
result.rotation = new Quaternion().setFromAxisAngle( this._geometryHelper.zAxis, ( rotation * Math.PI ) / 180 );
else if ( drawingDirection === 3 || ( ( axisOnXYZ && y === 1 ) || ( !axisOnXYZ && xAxisY === 1 ) ) )
result.rotation = new Quaternion().setFromAxisAngle( this._geometryHelper.zAxis, Math.PI / 2 );
return result;
}
_translateCenter( geometry, entity, center ) {
let attachmentPoint = entity.attachmentPoint;
if( !attachmentPoint ) return;
geometry.computeBoundingBox();
let width = geometry.boundingBox.max.x - geometry.boundingBox.min.x;
let height = geometry.boundingBox.max.y - geometry.boundingBox.min.y;
let depth = geometry.boundingBox.max.z - geometry.boundingBox.min.z;
let currentCenter = {
x: geometry.boundingBox.min.x + width / 2,
y: geometry.boundingBox.min.y + height / 2
};
switch ( attachmentPoint ) {
case 1:
// Top Left
geometry.translate( center.x - geometry.boundingBox.min.x , center.y - geometry.boundingBox.max.y , -0.5 * depth );
break;
case 2:
// Top Center
geometry.translate( center.x - currentCenter.x, center.y - geometry.boundingBox.max.y , -0.5 * depth );
break;
case 3:
// Top Right
geometry.translate( center.x - geometry.boundingBox.max.x , center.y - geometry.boundingBox.max.y , -0.5 * depth );
break;
case 4:
// Middle Left
geometry.translate( center.x - geometry.boundingBox.min.x , center.y - currentCenter.y , -0.5 * depth );
break;
case 5:
// Middle Center
geometry.translate( center.x - currentCenter.x , center.y - currentCenter.y , -0.5 * depth );
break;
case 6:
// Middle Right
geometry.translate( center.x - geometry.boundingBox.max.x, center.y - currentCenter.y , -0.5 * depth );
break;
case 7:
// Bottom Left
geometry.translate( center.x - geometry.boundingBox.min.x , center.y - geometry.boundingBox.min.y , -0.5 * depth );
break;
case 8:
// Bottom Center
geometry.translate( center.x - currentCenter.x , geometry.boundingBox.min.y, -0.5 * depth );
break;
case 9:
// Bottom Right
geometry.translate( center.x - geometry.boundingBox.max.x, geometry.boundingBox.min.y , -0.5 * depth );
break;
}
}
_replaceSpecialChars( str ) {
return str.replaceAll( '\\P', '\n' ).
replaceAll( '\\X', '\n' ).
replaceAll( '%%d', '\u00B0' ).
replaceAll( '%%p', '\u00B1' ).
replaceAll( '%%c', '\u2205' ).
replaceAll( '%%%', '%' );
}
}