@@ -816,6 +816,32 @@ function font(p5, fn) {
816
816
*/
817
817
p5 . Font = Font ;
818
818
819
+ /**
820
+ * @private
821
+ */
822
+ fn . parseFontData = async function ( pathOrData ) {
823
+ // load the raw font bytes
824
+ let result = pathOrData instanceof Uint8Array
825
+ ? pathOrData
826
+ : await fn . loadBytes ( path ) ;
827
+ //console.log('result:', result);
828
+
829
+ if ( ! result ) {
830
+ throw Error ( 'Failed to load font data' ) ;
831
+ }
832
+
833
+ // parse the font data
834
+ let fonts = Typr . parse ( result ) ;
835
+
836
+ // TODO: generate descriptors from font in the future
837
+
838
+ if ( fonts . length === 0 || fonts [ 0 ] . cmap === undefined ) {
839
+ throw Error ( 'parsing font data' ) ;
840
+ }
841
+
842
+ return fonts [ 0 ] ;
843
+ } ;
844
+
819
845
/**
820
846
* Loads a font and creates a <a href="#/p5.Font">p5.Font</a> object.
821
847
* `loadFont()` can load fonts in either .otf or .ttf format. Loaded fonts can
@@ -996,37 +1022,39 @@ function font(p5, fn) {
996
1022
. join ( '' ) ;
997
1023
fontDescriptors [ camelCaseKey ] = style . getPropertyValue ( key ) ;
998
1024
}
999
- fontPromises . push ( create ( this , name , src , fontDescriptors ) ) ;
1025
+ fontPromises . push ( ( async ( ) => {
1026
+ let fontData ;
1027
+ try {
1028
+ const urlMatch = / u r l \( ( [ ^ \) ] + ) \) / . exec ( src ) ;
1029
+ if ( urlMatch ) {
1030
+ let url = urlMatch [ 1 ] ;
1031
+ if ( / ^ [ ' " ] / . exec ( url ) && url . at ( 0 ) === url . at ( - 1 ) ) {
1032
+ url = url . slice ( 1 , - 1 )
1033
+ }
1034
+ fontData = await fn . parseFontData ( url ) ;
1035
+ }
1036
+ } catch ( _e ) {
1037
+ console . log (
1038
+ 'This font can only be drawn and will not work with textToPoints/Contours/Model.'
1039
+ ) ;
1040
+ }
1041
+ return create ( this , name , src , fontDescriptors , fontData )
1042
+ } ) ( ) ) ;
1000
1043
}
1001
1044
}
1002
1045
const fonts = await Promise . all ( fontPromises ) ;
1003
- return fonts [ 0 ] ; // TODO: handle multiple faces?
1046
+ return fonts . find ( f => f . data ) || fonts [ 0 ] ; // TODO: handle multiple faces?
1004
1047
}
1005
1048
1006
1049
let pfont ;
1007
1050
try {
1008
- // load the raw font bytes
1009
- let result = await fn . loadBytes ( path ) ;
1010
- //console.log('result:', result);
1011
-
1012
- if ( ! result ) {
1013
- throw Error ( 'Failed to load font data' ) ;
1014
- }
1015
-
1016
- // parse the font data
1017
- let fonts = Typr . parse ( result ) ;
1018
-
1019
- // TODO: generate descriptors from font in the future
1020
-
1021
- if ( fonts . length === 0 || fonts [ 0 ] . cmap === undefined ) {
1022
- throw Error ( 'parsing font data' ) ;
1023
- }
1051
+ const fontData = await fn . parseFontData ( path ) ;
1024
1052
1025
1053
// make sure we have a valid name
1026
- name = name || extractFontName ( fonts [ 0 ] , path ) ;
1054
+ name = name || extractFontName ( font , path ) ;
1027
1055
1028
1056
// create a FontFace object and pass it to the p5.Font constructor
1029
- pfont = await create ( this , name , path , descriptors , fonts [ 0 ] ) ;
1057
+ pfont = await create ( this , name , path , descriptors , fontData ) ;
1030
1058
1031
1059
} catch ( err ) {
1032
1060
// failed to parse the font, load it as a simple FontFace
0 commit comments