@@ -2648,7 +2648,6 @@ JsVar *jswrap_graphics_setFont4x6(JsVar *parent, int scale) {
2648
2648
return jswrap_graphics_setFontSizeX (parent , 1 + JSGRAPHICS_FONTSIZE_4X6 , false);
2649
2649
}
2650
2650
2651
-
2652
2651
JsVar * jswrap_graphics_findFont (JsVar * parent , JsVar * text , JsVar * options ) {
2653
2652
JsGraphics gfx ; if (!graphicsGetFromVar (& gfx , parent )) return 0 ;
2654
2653
int width = gfx .data .width , height = gfx .data .height ;
@@ -2687,6 +2686,7 @@ JsVar *jswrap_graphics_findFont(JsVar *parent, JsVar *text, JsVar *options) {
2687
2686
{"4x6" , 6 , 1 , jswrap_graphics_setFont4x6 }
2688
2687
#endif
2689
2688
};
2689
+
2690
2690
int fontIdx = 0 ;
2691
2691
// check max font size
2692
2692
while (fontIdx < FONTS - 1 && FONT [fontIdx ].height > maxHeight )
@@ -2701,26 +2701,45 @@ JsVar *jswrap_graphics_findFont(JsVar *parent, JsVar *text, JsVar *options) {
2701
2701
graphicsGetFromVar (& gfx , parent );
2702
2702
if (wrap ) {
2703
2703
jsvUnLock2 (finalText , finalLines );
2704
- finalLines = jswrap_graphics_wrapString (parent , text , width );
2705
- finalText = jsvArrayJoin (finalLines ,newline ,true);
2706
- _jswrap_graphics_stringMetrics (& gfx , finalText , -1 , & stringMetrics );
2707
- } else
2708
- _jswrap_graphics_stringMetrics (& gfx , text , -1 , & stringMetrics );
2704
+ finalLines = jswrap_graphics_wrapString (parent , text , width );
2705
+ finalText = jsvArrayJoin (finalLines , newline , true);
2706
+ }
2707
+ _jswrap_graphics_stringMetrics (& gfx , finalText , -1 , & stringMetrics );
2709
2708
if (((stringMetrics .stringWidth <= width ) && (stringMetrics .stringHeight <= height )) || // all good!
2710
2709
fontIdx == FONTS - 1 || // no more fonts
2711
2710
FONT [fontIdx + 1 ].height < minHeight // next font is too small
2712
2711
) break ;
2713
2712
fontIdx ++ ;
2714
2713
}
2715
2714
const char * fontName = FONT [fontIdx ].name ;
2716
- // if there were unrenderable characters, use the international font instead if we have one
2715
+ // if there were unrenderable characters, use the international font instead if we have one (Intl:2, then Intl)
2717
2716
if (stringMetrics .unrenderableChars ) {
2718
2717
JsVar * intlFont = jspGetNamedField (parent , "setFontIntl" , false);
2719
2718
if (intlFont ) {
2720
- fontName = "Intl" ;
2719
+ fontName = "Intl:2" ; // start off using double-size Intl
2721
2720
jsvUnLock (jspExecuteFunction (intlFont , parent , 0 , NULL ));
2722
2721
graphicsGetFromVar (& gfx , parent );
2723
- _jswrap_graphics_stringMetrics (& gfx , text , -1 , & stringMetrics );
2722
+ gfx .data .fontSize = (gfx .data .fontSize & ~JSGRAPHICS_FONTSIZE_SCALE_MASK ) | 2 ; // scale by 2
2723
+ if (wrap ) {
2724
+ jsvUnLock2 (finalText , finalLines );
2725
+ finalLines = jswrap_graphics_wrapString (parent , text , width );
2726
+ finalText = jsvArrayJoin (finalLines , newline , true);
2727
+ }
2728
+ _jswrap_graphics_stringMetrics (& gfx , finalText , -1 , & stringMetrics );
2729
+ // If it's too big, try again with normal size
2730
+ if (((stringMetrics .stringWidth > width ) || (stringMetrics .stringHeight > height )) &&
2731
+ stringMetrics .lineHeight >= minHeight * 2 ) { // next font is too small
2732
+ fontName = "Intl" ;
2733
+ gfx .data .fontSize = (gfx .data .fontSize & ~JSGRAPHICS_FONTSIZE_SCALE_MASK ) | 1 ; // scale by 1
2734
+ if (wrap ) {
2735
+ jsvUnLock2 (finalText , finalLines );
2736
+ finalLines = jswrap_graphics_wrapString (parent , text , width );
2737
+ finalText = jsvArrayJoin (finalLines , newline , true);
2738
+ }
2739
+ _jswrap_graphics_stringMetrics (& gfx , finalText , -1 , & stringMetrics );
2740
+ }
2741
+ graphicsSetVar (& gfx ); // gfx data changed because of font scale
2742
+ jsvUnLock (intlFont );
2724
2743
}
2725
2744
}
2726
2745
if (trim && stringMetrics .stringHeight > height ) { // do we have to trim these lines to length?
@@ -2740,7 +2759,6 @@ JsVar *jswrap_graphics_findFont(JsVar *parent, JsVar *text, JsVar *options) {
2740
2759
jsvObjectSetChildAndUnLock (result , "font" , jsvNewFromString (fontName ));
2741
2760
jsvObjectSetChildAndUnLock (result , "w" , jsvNewFromInteger (stringMetrics .stringWidth ));
2742
2761
jsvObjectSetChildAndUnLock (result , "h" , jsvNewFromInteger (stringMetrics .stringHeight ));
2743
-
2744
2762
return result ;
2745
2763
}
2746
2764
#endif
0 commit comments