32
32
import Typr from './lib/Typr.js' ;
33
33
import { createFromCommands } from '@davepagurek/bezier-path' ;
34
34
35
- function unquote ( name ) {
36
- // Unquote name from CSS
37
- if ( ( name . startsWith ( '"' ) || name . startsWith ( "'" ) ) && name . at ( 0 ) === name . at ( - 1 ) ) {
38
- return name . slice ( 1 , - 1 ) . replace ( / \/ ( [ ' " ] ) / g, '$1' ) ;
39
- }
40
- return name ;
41
- }
42
-
43
35
function font ( p5 , fn ) {
44
36
45
37
const pathArgCounts = { M : 2 , L : 2 , C : 6 , Q : 4 } ;
@@ -62,53 +54,18 @@ function font(p5, fn) {
62
54
this . face = fontFace ;
63
55
}
64
56
65
- verticalAlign ( size ) {
66
- const { sCapHeight } = this . data ?. [ 'OS/2' ] || { } ;
67
- const { unitsPerEm = 1000 } = this . data ?. head || { } ;
68
- const { ascender = 0 , descender = 0 } = this . data ?. hhea || { } ;
69
- const current = ascender / 2 ;
70
- const target = ( sCapHeight || ( ascender + descender ) ) / 2 ;
71
- const offset = target - current ;
72
- return offset * size / unitsPerEm ;
73
- }
74
-
75
- variations ( ) {
76
- let vars = { } ;
77
- if ( this . data ) {
78
- let axes = this . face ?. axes ;
79
- if ( axes ) {
80
- axes . forEach ( ax => {
81
- vars [ ax . tag ] = ax . value ;
82
- } ) ;
83
- }
84
- }
85
- fontFaceVariations . forEach ( v => {
86
- let val = this . face [ v ] ;
87
- if ( val !== 'normal' ) {
88
- vars [ v ] = vars [ v ] || val ;
89
- }
90
- } ) ;
91
- return vars ;
92
- }
93
-
94
- metadata ( ) {
95
- let meta = this . data ?. name || { } ;
96
- for ( let p in this . face ) {
97
- if ( ! / ^ l o a d / . test ( p ) ) {
98
- meta [ p ] = meta [ p ] || this . face [ p ] ;
99
- }
100
- }
101
- return meta ;
102
- }
103
-
104
- fontBounds ( ...args ) { // alias for p5.fontBounds
105
- if ( ! this . _pInst ) throw Error ( 'p5 required for fontBounds()' ) ;
106
- return this . _pInst . fontBounds ( ...args ) ;
57
+ fontBounds ( str , x , y , width , height , options ) {
58
+ ( { width, height, options } = this . _parseArgs ( width , height , options ) ) ;
59
+ let renderer = options ?. graphics ?. _renderer || this . _pInst . _renderer ;
60
+ if ( ! renderer ) throw Error ( 'p5 or graphics required for fontBounds()' ) ;
61
+ return renderer . fontBounds ( str , x , y , width , height ) ;
107
62
}
108
63
109
- textBounds ( ...args ) { // alias for p5.textBounds
110
- if ( ! this . _pInst ) throw Error ( 'p5 required for textBounds()' ) ; // TODO:
111
- return this . _pInst . textBounds ( ...args ) ;
64
+ textBounds ( str , x , y , width , height , options ) {
65
+ ( { width, height, options } = this . _parseArgs ( width , height , options ) ) ;
66
+ let renderer = options ?. graphics ?. _renderer || this . _pInst . _renderer ;
67
+ if ( ! renderer ) throw Error ( 'p5 or graphics required for fontBounds()' ) ;
68
+ return renderer . textBounds ( str , x , y , width , height ) ;
112
69
}
113
70
114
71
textToPaths ( str , x , y , width , height , options ) {
@@ -208,6 +165,35 @@ function font(p5, fn) {
208
165
return geom ;
209
166
}
210
167
168
+ variations ( ) {
169
+ let vars = { } ;
170
+ if ( this . data ) {
171
+ let axes = this . face ?. axes ;
172
+ if ( axes ) {
173
+ axes . forEach ( ax => {
174
+ vars [ ax . tag ] = ax . value ;
175
+ } ) ;
176
+ }
177
+ }
178
+ fontFaceVariations . forEach ( v => {
179
+ let val = this . face [ v ] ;
180
+ if ( val !== 'normal' ) {
181
+ vars [ v ] = vars [ v ] || val ;
182
+ }
183
+ } ) ;
184
+ return vars ;
185
+ }
186
+
187
+ metadata ( ) {
188
+ let meta = this . data ?. name || { } ;
189
+ for ( let p in this . face ) {
190
+ if ( ! / ^ l o a d / . test ( p ) ) {
191
+ meta [ p ] = meta [ p ] || this . face [ p ] ;
192
+ }
193
+ }
194
+ return meta ;
195
+ }
196
+
211
197
static async list ( log = false ) { // tmp
212
198
if ( log ) {
213
199
console . log ( 'There are' , document . fonts . size , 'font-faces\n' ) ;
@@ -228,6 +214,16 @@ function font(p5, fn) {
228
214
}
229
215
230
216
/////////////////////////////// HELPERS ////////////////////////////////
217
+
218
+ _verticalAlign ( size ) {
219
+ const { sCapHeight } = this . data ?. [ 'OS/2' ] || { } ;
220
+ const { unitsPerEm = 1000 } = this . data ?. head || { } ;
221
+ const { ascender = 0 , descender = 0 } = this . data ?. hhea || { } ;
222
+ const current = ascender / 2 ;
223
+ const target = ( sCapHeight || ( ascender + descender ) ) / 2 ;
224
+ const offset = target - current ;
225
+ return offset * size / unitsPerEm ;
226
+ }
231
227
232
228
/*
233
229
Returns an array of line objects, each containing { text, x, y, glyphs: [ {g, path} ] }
@@ -579,7 +575,7 @@ function font(p5, fn) {
579
575
580
576
// parse the font data
581
577
let fonts = Typr . parse ( result ) ;
582
- console . log ( fonts [ 0 ] )
578
+
583
579
// TODO: generate descriptors from font in the future
584
580
585
581
if ( fonts . length !== 1 || fonts [ 0 ] . cmap === undefined ) {
@@ -635,6 +631,14 @@ function font(p5, fn) {
635
631
return new p5 . Font ( pInst , face , name , path , rawFont ) ;
636
632
}
637
633
634
+ function unquote ( name ) {
635
+ // Unquote name from CSS
636
+ if ( ( name . startsWith ( '"' ) || name . startsWith ( "'" ) ) && name . at ( 0 ) === name . at ( - 1 ) ) {
637
+ return name . slice ( 1 , - 1 ) . replace ( / \/ ( [ ' " ] ) / g, '$1' ) ;
638
+ }
639
+ return name ;
640
+ }
641
+
638
642
function createFontFace ( name , path , descriptors , rawFont ) {
639
643
let fontArg = rawFont ?. _data ;
640
644
if ( ! fontArg ) {
0 commit comments