55 < meta name ="description " content ="A JavaScript library to manipulate the letterforms of text from the browser or node.js. ">
66 < meta charset ="utf-8 ">
77 < link rel ="stylesheet " href ="site.css ">
8- < script src ="/dist/opentype.js "> </ script >
98</ head >
109< body >
1110< div class ="header ">
@@ -22,7 +21,7 @@ <h1>opentype.js</h1>
2221 </ div >
2322</ div >
2423
25- < div class ="container ">
24+ < form class ="container " name =" demo ">
2625
2726 < div class ="explain ">
2827 < h1 > Glyph Inspector</ h1 >
@@ -58,10 +57,14 @@ <h1>Free Software</h1>
5857 </ div >
5958
6059 < hr >
61- </ div >
60+ </ form >
61+
62+
63+ < script type ="module ">
64+ import * as opentype from "/dist/opentype.js" ;
6265
66+ const form = document . forms . demo ;
6367
64- < script >
6568var cellCount = 100 ,
6669 cellWidth = 44 ,
6770 cellHeight = 40 ,
@@ -122,6 +125,7 @@ <h1>Free Software</h1>
122125}
123126
124127function displayGlyphData ( glyphIndex ) {
128+ const font = window . font ;
125129 var container = document . getElementById ( 'glyph-data' ) ;
126130 if ( glyphIndex < 0 ) {
127131 container . innerHTML = '' ;
@@ -243,7 +247,7 @@ <h1>Free Software</h1>
243247 height = canvas . height / pixelRatio ;
244248 ctx . clearRect ( 0 , 0 , width , height ) ;
245249 if ( glyphIndex < 0 ) return ;
246- var glyph = font . glyphs . get ( glyphIndex ) ,
250+ var glyph = window . font . glyphs . get ( glyphIndex ) ,
247251 glyphWidth = glyph . advanceWidth * glyphScale ,
248252 xmin = ( width - glyphWidth ) / 2 ,
249253 xmax = ( width + glyphWidth ) / 2 ,
@@ -272,12 +276,12 @@ <h1>Free Software</h1>
272276 var cellMarkSize = 4 ;
273277 var ctx = canvas . getContext ( '2d' ) ;
274278 ctx . clearRect ( 0 , 0 , cellWidth , cellHeight ) ;
275- if ( glyphIndex >= font . numGlyphs ) return ;
279+ if ( glyphIndex >= window . font . numGlyphs ) return ;
276280
277281 ctx . fillStyle = '#606060' ;
278282 ctx . font = '9px sans-serif' ;
279283 ctx . fillText ( glyphIndex , 1 , cellHeight - 1 ) ;
280- var glyph = font . glyphs . get ( glyphIndex ) ,
284+ var glyph = window . font . glyphs . get ( glyphIndex ) ,
281285 glyphWidth = glyph . advanceWidth * fontScale ,
282286 xmin = ( cellWidth - glyphWidth ) / 2 ,
283287 xmax = ( cellWidth + glyphWidth ) / 2 ,
@@ -307,7 +311,7 @@ <h1>Free Software</h1>
307311 displayGlyphPage ( + event . target . id . substr ( 1 ) ) ;
308312}
309313
310- function initGlyphDisplay ( ) {
314+ function initGlyphDisplay ( font ) {
311315 var glyphBgCanvas = document . getElementById ( 'glyph-bg' ) ,
312316 w = glyphBgCanvas . width / pixelRatio ,
313317 h = glyphBgCanvas . height / pixelRatio ,
@@ -322,7 +326,7 @@ <h1>Free Software</h1>
322326 glyphBaseline = glyphMargin + glyphH * head . yMax / maxHeight ;
323327
324328 function hline ( text , yunits ) {
325- ypx = glyphBaseline - yunits * glyphScale ;
329+ const ypx = glyphBaseline - yunits * glyphScale ;
326330 ctx . fillText ( text , 2 , ypx + 3 ) ;
327331 ctx . fillRect ( 80 , ypx , w , 1 ) ;
328332 }
@@ -366,40 +370,18 @@ <h1>Free Software</h1>
366370 }
367371 pagination . appendChild ( fragment ) ;
368372
369- initGlyphDisplay ( ) ;
373+ initGlyphDisplay ( font ) ;
370374 displayGlyphPage ( 0 ) ;
371375 displayGlyph ( - 1 ) ;
372376 displayGlyphData ( - 1 ) ;
373377}
374378
375- function onReadFile ( e ) {
376- document . getElementById ( 'font-name' ) . innerHTML = '' ;
377- var file = e . target . files [ 0 ] ;
378- var reader = new FileReader ( ) ;
379- reader . onload = function ( e ) {
380- try {
381- font = opentype . parse ( e . target . result , { lowMemory :true } ) ;
382- showErrorMessage ( '' ) ;
383- onFontLoaded ( font ) ;
384- } catch ( err ) {
385- showErrorMessage ( err . toString ( ) ) ;
386- if ( err . stack ) console . log ( err . stack ) ;
387- throw ( err ) ;
388- }
389- } ;
390- reader . onerror = function ( err ) {
391- showErrorMessage ( err . toString ( ) ) ;
392- } ;
393-
394- reader . readAsArrayBuffer ( file ) ;
395- }
396-
397379function cellSelect ( event ) {
398- if ( ! font ) return ;
380+ if ( ! window . font ) return ;
399381 var firstGlyphIndex = pageSelected * cellCount ,
400382 cellIndex = + event . target . id . substr ( 1 ) ,
401383 glyphIndex = firstGlyphIndex + cellIndex ;
402- if ( glyphIndex < font . numGlyphs ) {
384+ if ( glyphIndex < window . font . numGlyphs ) {
403385 displayGlyph ( glyphIndex ) ;
404386 displayGlyphData ( glyphIndex ) ;
405387 }
@@ -423,21 +405,28 @@ <h1>Free Software</h1>
423405var fontFileName = 'fonts/FiraSansMedium.woff' ;
424406document . getElementById ( 'font-name' ) . innerHTML = fontFileName . split ( '/' ) [ 1 ] ;
425407
426- var fileButton = document . getElementById ( 'file' ) ;
427- fileButton . addEventListener ( 'change' , onReadFile , false ) ;
408+ form . file . onchange = async function ( e ) {
409+ form . fontname . innerText = '' ;
410+ try {
411+ const buf = e . target . files [ 0 ] . arrayBuffer ( ) ;
412+ onFontLoaded ( opentype . parse ( await buf , { lowMemory :true } ) ) ;
413+ showErrorMessage ( '' ) ;
414+ } catch ( err ) {
415+ showErrorMessage ( err . toString ( ) ) ;
416+ }
417+ } ;
428418
429419enableHighDPICanvas ( 'glyph-bg' ) ;
430420enableHighDPICanvas ( 'glyph' ) ;
431421
432422prepareGlyphList ( ) ;
433- opentype . load ( fontFileName , function ( err , font ) {
434- var amount , glyph , ctx , x , y , fontSize ;
435- if ( err ) {
436- showErrorMessage ( err . toString ( ) ) ;
437- return ;
438- }
439- onFontLoaded ( font ) ;
440- } , { lowMemory :true } ) ;
423+
424+ const buf = await fetch ( fontFileName ) . then ( res => res . arrayBuffer ( ) ) ;
425+ try {
426+ onFontLoaded ( opentype . parse ( buf , { lowMemory :true } ) ) ;
427+ } catch ( err ) {
428+ showErrorMessage ( err . toString ( ) ) ;
429+ }
441430</ script >
442431</ body >
443432</ html >
0 commit comments