@@ -18,18 +18,14 @@ import {
18
18
$getParent ,
19
19
$getSubformParent ,
20
20
$nodeName ,
21
+ $pushGlyphs ,
21
22
$toStyle ,
22
23
XFAObject ,
23
24
} from "./xfa_object.js" ;
24
25
import { getMeasurement } from "./utils.js" ;
26
+ import { TextMeasure } from "./text.js" ;
25
27
import { warn } from "../../shared/util.js" ;
26
28
27
- const wordNonWordRegex = new RegExp (
28
- "([\\p{N}\\p{L}\\p{M}]+)|([^\\p{N}\\p{L}\\p{M}]+)" ,
29
- "gu"
30
- ) ;
31
- const wordFirstRegex = new RegExp ( "^[\\p{N}\\p{L}\\p{M}]" , "u" ) ;
32
-
33
29
function measureToString ( m ) {
34
30
if ( typeof m === "string" ) {
35
31
return "0px" ;
@@ -192,65 +188,15 @@ const converters = {
192
188
} ,
193
189
} ;
194
190
195
- function layoutText ( text , fontSize , space ) {
196
- // Try to guess width and height for the given text in taking into
197
- // account the space where the text should fit.
198
- // The computed dimensions are just an overestimation.
199
- // TODO: base this estimation on real metrics.
200
- let width = 0 ;
201
- let height = 0 ;
202
- let totalWidth = 0 ;
203
- const lineHeight = fontSize * 1.5 ;
204
- const averageCharSize = fontSize * 0.4 ;
205
- const maxCharOnLine = Math . floor ( space . width / averageCharSize ) ;
206
- const chunks = text . match ( wordNonWordRegex ) ;
207
- let treatedChars = 0 ;
208
-
209
- let i = 0 ;
210
- let chunk = chunks [ 0 ] ;
211
- while ( chunk ) {
212
- const w = chunk . length * averageCharSize ;
213
- if ( width + w <= space . width ) {
214
- width += w ;
215
- treatedChars += chunk . length ;
216
- chunk = chunks [ i ++ ] ;
217
- continue ;
218
- }
219
-
220
- if ( ! wordFirstRegex . test ( chunk ) || chunk . length > maxCharOnLine ) {
221
- const numOfCharOnLine = Math . floor (
222
- ( space . width - width ) / averageCharSize
223
- ) ;
224
- chunk = chunk . slice ( numOfCharOnLine ) ;
225
- treatedChars += numOfCharOnLine ;
226
- if ( height + lineHeight > space . height ) {
227
- return { width : 0 , height : 0 , splitPos : treatedChars } ;
228
- }
229
- totalWidth = Math . max ( width , totalWidth ) ;
230
- width = 0 ;
231
- height += lineHeight ;
232
- continue ;
233
- }
234
-
235
- if ( height + lineHeight > space . height ) {
236
- return { width : 0 , height : 0 , splitPos : treatedChars } ;
237
- }
238
-
239
- totalWidth = Math . max ( width , totalWidth ) ;
240
- width = w ;
241
- height += lineHeight ;
242
- chunk = chunks [ i ++ ] ;
243
- }
244
-
245
- if ( totalWidth === 0 ) {
246
- totalWidth = width ;
247
- }
248
-
249
- if ( totalWidth !== 0 ) {
250
- height += lineHeight ;
191
+ function layoutText ( text , xfaFont , fonts , width ) {
192
+ const measure = new TextMeasure ( xfaFont , fonts ) ;
193
+ if ( typeof text === "string" ) {
194
+ measure . addString ( text ) ;
195
+ } else {
196
+ text [ $pushGlyphs ] ( measure ) ;
251
197
}
252
198
253
- return { width : totalWidth , height , splitPos : - 1 } ;
199
+ return measure . compute ( width ) ;
254
200
}
255
201
256
202
function computeBbox ( node , html , availableSpace ) {
0 commit comments