@@ -51,6 +51,44 @@ <h1>Slint Gallery</h1>
5151 var galleries = [ ] ;
5252 var currentGallery = undefined ;
5353
54+ // Get Noto CJK font URL from GitHub for the detected language
55+ function getNotoFontUrl ( lang ) {
56+ const langCode = lang . split ( '-' ) [ 0 ] . toLowerCase ( ) ;
57+
58+ // Direct URLs to OTF files from Noto CJK GitHub repository (using raw.githubusercontent.com for CORS)
59+ const fontMap = {
60+ 'ja' : 'https://raw.githubusercontent.com/notofonts/noto-cjk/main/Sans/OTF/Japanese/NotoSansCJKjp-Regular.otf' ,
61+ // 'zh': 'https://raw.githubusercontent.com/notofonts/noto-cjk/main/Sans/OTF/SimplifiedChinese/NotoSansCJKsc-Regular.otf',
62+ // 'ko': 'https://raw.githubusercontent.com/notofonts/noto-cjk/main/Sans/OTF/Korean/NotoSansCJKkr-Regular.otf',
63+ } ;
64+
65+ return fontMap [ langCode ] ;
66+ }
67+
68+ // Fetch font from GitHub
69+ async function fetchFont ( fontUrl ) {
70+ const fontResponse = await fetch ( fontUrl ) ;
71+ if ( ! fontResponse . ok ) {
72+ throw new Error ( `HTTP ${ fontResponse . status } : ${ fontResponse . statusText } ` ) ;
73+ }
74+ return await fontResponse . arrayBuffer ( ) ;
75+ }
76+
77+ // Load font for the detected language
78+ async function loadFontForLanguage ( module , lang ) {
79+ const fontUrl = getNotoFontUrl ( lang ) ;
80+
81+ if ( fontUrl ) {
82+ try {
83+ const fontData = await fetchFont ( fontUrl ) ;
84+ const uint8Array = new Uint8Array ( fontData ) ;
85+ const result = await module . load_font_from_bytes ( uint8Array ) ;
86+ } catch ( error ) {
87+ console . error ( `Failed to load font for language ${ lang } :` , error ) ;
88+ }
89+ }
90+ }
91+
5492 function initGallery ( gallery ) {
5593 document . getElementById ( "spinner" ) . hidden = false ;
5694
@@ -67,19 +105,31 @@ <h1>Slint Gallery</h1>
67105 document . getElementById ( "canvas-parent" ) . appendChild ( galleries [ gallery ] ) ;
68106 document . getElementById ( "spinner" ) . hidden = true ;
69107 } else {
70- import ( gallery ) . then ( module => {
108+ import ( gallery ) . then ( async module => {
71109 let canvas = document . createElement ( "canvas" ) ;
72110 canvas . id = "canvas" ;
73111 canvas . dataset . slintAutoResizeToPreferred = "true" ;
74112 currentGallery = gallery ;
75113 galleries [ gallery ] = canvas ;
76114
77115 document . getElementById ( "canvas-parent" ) . appendChild ( canvas ) ;
78- module . default ( ) . finally ( ( ) => {
79- document . getElementById ( "canvas" ) . hidden = false ;
80- document . getElementById ( "spinner" ) . hidden = true ;
81- } ) ;
82- } )
116+
117+ // Initialize WASM module first
118+ await module . default ( ) ;
119+
120+ // Detect browser language and load appropriate font
121+ const browserLang = ( navigator . languages && navigator . languages [ 0 ] ) || navigator . language || navigator . userLanguage || 'en' ;
122+ await loadFontForLanguage ( module , browserLang ) ;
123+
124+ // Start the application
125+ module . main ( ) ;
126+
127+ document . getElementById ( "canvas" ) . hidden = false ;
128+ document . getElementById ( "spinner" ) . hidden = true ;
129+ } ) . catch ( error => {
130+ console . error ( 'Failed to initialize gallery:' , error ) ;
131+ document . getElementById ( "spinner" ) . hidden = true ;
132+ } ) ;
83133 }
84134 }
85135
0 commit comments