@@ -326,6 +326,7 @@ typedef struct {
326326 FxFace * face ;
327327 u32 px ;
328328 long style ;
329+ FxFace * primary ; // fixed/proportional policy, cell and line-box metrics
329330} FxFallback ;
330331
331332static FxFallback fx_fb [FX_FB_MAX ];
@@ -340,7 +341,52 @@ static unsigned fx_has(FxFace* f, u32 cp) {
340341 return gi ;
341342}
342343
343- static FxFace * fx_fb_open (void * pat , u32 cp , u32 px , long style ) {
344+ // Read preference once. Locale precedence follows LC_ALL, LC_MESSAGES,
345+ // LANG; a non-CJK locale deliberately defaults to Japanese glyph forms.
346+ static const char * fx_cjk_lang (void ) {
347+ static const char * lang = NULL ;
348+ if (lang != NULL ) return lang ;
349+ const char * override = getenv ("BACKPLANE_CJK_LANG" );
350+ const char * loc = override ;
351+ char tag [64 ];
352+ for (int attempt = 0 ; attempt < 2 ; attempt += 1 ) {
353+ if (attempt || loc == NULL || !* loc ) {
354+ loc = getenv ("LC_ALL" );
355+ if (loc == NULL || !* loc ) loc = getenv ("LC_MESSAGES" );
356+ if (loc == NULL || !* loc ) loc = getenv ("LANG" );
357+ }
358+ unsigned i = 0 ;
359+ while (loc && loc [i ] && loc [i ] != '.' && loc [i ] != '@' && i < sizeof (tag ) - 1 ) {
360+ char ch = loc [i ];
361+ tag [i ++ ] = ch == '_' ? '-' : (ch >= 'A' && ch <= 'Z' ? ch + 'a' - 'A' : ch );
362+ }
363+ tag [i ] = 0 ;
364+ if (override && * override && !attempt ) {
365+ if (!strcmp (tag , "ja" ) || !strcmp (tag , "zh-cn" ) || !strcmp (tag , "zh-tw" ) || !strcmp (tag , "ko" )) break ;
366+ } else break ;
367+ }
368+ lang = "ja" ;
369+ if (!strncmp (tag , "ko" , 2 ) && (!tag [2 ] || tag [2 ] == '-' )) lang = "ko" ;
370+ if (!strncmp (tag , "zh" , 2 ) && (!tag [2 ] || tag [2 ] == '-' )) {
371+ lang = strstr (tag , "-tw" ) || strstr (tag , "-hk" ) || strstr (tag , "-mo" ) || strstr (tag , "-hant" ) ? "zh-tw" : "zh-cn" ;
372+ }
373+ return lang ;
374+ }
375+
376+ // Same ranges as core/cjk.bend and Vt.wide (no Unicode library needed).
377+ static int fx_cjk_wide (u32 cp ) {
378+ static const u32 ranges [][2 ] = {
379+ {4352 ,4447 }, {11904 ,12350 }, {12353 ,13311 }, {13312 ,19903 },
380+ {19968 ,40959 }, {40960 ,42191 }, {44032 ,55203 }, {63744 ,64255 },
381+ {65072 ,65103 }, {65280 ,65376 }, {65504 ,65510 }, {127744 ,128591 },
382+ {128640 ,128767 }, {129280 ,129535 }, {131072 ,262141 }
383+ };
384+ for (unsigned i = 0 ; i < sizeof (ranges ) / sizeof (ranges [0 ]); i += 1 )
385+ if (cp >= ranges [i ][0 ] && cp <= ranges [i ][1 ]) return 1 ;
386+ return 0 ;
387+ }
388+
389+ static FxFace * fx_fb_open (void * pat , u32 cp , u32 px , long style , FxFace * primary ) {
344390 unsigned char * file = NULL ;
345391 int index = 0 ;
346392 int color = 0 ;
@@ -365,6 +411,7 @@ static FxFace* fx_fb_open(void* pat, u32 cp, u32 px, long style) {
365411 fx_fb [fx_nfb ].face = f ;
366412 fx_fb [fx_nfb ].px = px ;
367413 fx_fb [fx_nfb ].style = style ;
414+ fx_fb [fx_nfb ].primary = primary ;
368415 fx_nfb += 1 ;
369416 return f ;
370417}
@@ -374,7 +421,7 @@ static FxFace* fx_fallback(FxFace* face, u32 cp) {
374421 u32 px = face -> size -> metrics .y_ppem ;
375422 long style = face -> style_flags & (FX_STYLE_ITALIC | FX_STYLE_BOLD );
376423 for (u32 i = 0 ; i < fx_nfb ; i += 1 ) {
377- if (fx_fb [i ].px == px && fx_fb [i ].style == style && fx_has (fx_fb [i ].face , cp ) != 0 ) {
424+ if (fx_fb [i ].primary == face && fx_fb [ i ]. px == px && fx_fb [i ].style == style && fx_has (fx_fb [i ].face , cp ) != 0 ) {
378425 return fx_fb [i ].face ;
379426 }
380427 }
@@ -394,6 +441,7 @@ static FxFace* fx_fallback(FxFace* face, u32 cp) {
394441 return NULL ;
395442 }
396443 fx_FcCharSetAddChar (cs , cp );
444+ fx_FcPatternAddString (p , "lang" , (const unsigned char * )fx_cjk_lang ());
397445 fx_FcPatternAddCharSet (p , "charset" , cs );
398446 fx_FcPatternAddString (p , "family" , (const unsigned char * )((face -> face_flags & FX_FACE_FIXED_WIDTH ) ? "monospace" : "sans-serif" ));
399447 if (style & FX_STYLE_BOLD ) {
@@ -409,7 +457,7 @@ static FxFace* fx_fallback(FxFace* face, u32 cp) {
409457 FxFace * found = NULL ;
410458 if (fs != NULL ) {
411459 for (int i = 0 ; i < fs -> nfont && i < 24 && found == NULL ; i += 1 ) {
412- found = fx_fb_open (fs -> fonts [i ], cp , px , style );
460+ found = fx_fb_open (fs -> fonts [i ], cp , px , style , face );
413461 }
414462 fx_FcFontSetDestroy (fs );
415463 }
@@ -434,14 +482,29 @@ Term font_glyph_run(Env e, Term* f, IoWork* w) {
434482 if (face == NULL ) {
435483 return fx_list (e , NULL , 0 );
436484 }
485+ FxFace * primary = face ;
486+ long cell = 0 ;
437487 unsigned gi = fx_FT_Get_Char_Index (face , cp );
488+ if (gi == 0 && (primary -> face_flags & FX_FACE_FIXED_WIDTH )) {
489+ unsigned zero = fx_FT_Get_Char_Index (primary , '0' );
490+ if (zero && fx_FT_Load_Glyph (primary , zero , FX_LOAD_TARGET_LIGHT | FX_LOAD_NO_BITMAP ) == 0 )
491+ cell = primary -> glyph -> advance .x ;
492+ }
438493 if (gi == 0 && cp >= 128 ) {
439494 FxFace * fb = fx_fallback (face , cp );
440495 if (fb != NULL ) {
441496 face = fb ;
442497 gi = fx_FT_Get_Char_Index (face , cp );
443498 }
444499 }
500+ if (gi == 0 && fx_cjk_wide (cp )) {
501+ static int warned = 0 ;
502+ if (!warned ) {
503+ fprintf (stderr , "no CJK font: install fonts-noto-cjk\n" );
504+ warned = 1 ;
505+ }
506+ return fx_list (e , NULL , 0 );
507+ }
445508 if (fx_FT_Load_Glyph (face , gi , FX_LOAD_TARGET_LIGHT | FX_LOAD_NO_BITMAP ) != 0
446509 || fx_FT_Render_Glyph (face -> glyph , FX_RENDER_MODE_NORMAL ) != 0 ) {
447510 return fx_list (e , NULL , 0 );
@@ -456,6 +519,15 @@ Term font_glyph_run(Env e, Term* f, IoWork* w) {
456519 ws [0 ] = gi != 0 ;
457520 ws [1 ] = (u32 )s -> advance .x ;
458521 ws [2 ] = (u32 )(s -> bitmap_left + FX_BIAS );
522+ if (face != primary && cell > 0 ) {
523+ long advance = cell * (fx_cjk_wide (cp ) ? 2 : 1 );
524+ ws [1 ] = (u32 )advance ;
525+ // Centre the advance box, preserving the font's own bearing. CJK
526+ // punctuation intentionally sits at an edge of its em box.
527+ long delta = advance - s -> advance .x ;
528+ long shift = delta >= 0 ? (delta + 64 ) / 128 : - ((- delta + 64 ) / 128 );
529+ ws [2 ] = (u32 )(s -> bitmap_left + shift + FX_BIAS );
530+ }
459531 ws [3 ] = (u32 )(s -> bitmap_top + FX_BIAS );
460532 ws [4 ] = bw ;
461533 ws [5 ] = bh ;
0 commit comments