3232import Typr from './lib/Typr.js' ;
3333import { createFromCommands } from '@davepagurek/bezier-path' ;
3434
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-
4335function font ( p5 , fn ) {
4436
4537 const pathArgCounts = { M : 2 , L : 2 , C : 6 , Q : 4 } ;
@@ -62,53 +54,18 @@ function font(p5, fn) {
6254 this . face = fontFace ;
6355 }
6456
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 ) ;
10762 }
10863
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 ) ;
11269 }
11370
11471 textToPaths ( str , x , y , width , height , options ) {
@@ -208,6 +165,35 @@ function font(p5, fn) {
208165 return geom ;
209166 }
210167
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+
211197 static async list ( log = false ) { // tmp
212198 if ( log ) {
213199 console . log ( 'There are' , document . fonts . size , 'font-faces\n' ) ;
@@ -228,6 +214,16 @@ function font(p5, fn) {
228214 }
229215
230216 /////////////////////////////// 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+ }
231227
232228 /*
233229 Returns an array of line objects, each containing { text, x, y, glyphs: [ {g, path} ] }
@@ -579,7 +575,7 @@ function font(p5, fn) {
579575
580576 // parse the font data
581577 let fonts = Typr . parse ( result ) ;
582- console . log ( fonts [ 0 ] )
578+
583579 // TODO: generate descriptors from font in the future
584580
585581 if ( fonts . length !== 1 || fonts [ 0 ] . cmap === undefined ) {
@@ -635,6 +631,14 @@ function font(p5, fn) {
635631 return new p5 . Font ( pInst , face , name , path , rawFont ) ;
636632 }
637633
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+
638642 function createFontFace ( name , path , descriptors , rawFont ) {
639643 let fontArg = rawFont ?. _data ;
640644 if ( ! fontArg ) {
0 commit comments