Skip to content

Commit cbaf1a8

Browse files
committed
Bangle.js: g.findFont now attempts to use Intl:2 if there's room. Also fix memory leak
1 parent 3f8e66e commit cbaf1a8

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
nRF5x: Remove handlers from our handlers array when a device is disconnected
33
Bangle.js: Fix wrapString when wrapping a non-UTF8 string containing UTF8 characters (fix #2633)
44
Ensure jshPushIOCharEvent is more stable when the FIFO is full
5+
Bangle.js: g.findFont now attempts to use Intl:2 if there's room. Also fix memory leak
56

67
2v26 : nRF5x: ensure TIMER1_IRQHandler doesn't always wake idle loop up (fix #1900)
78
Puck.js: On v2.1 ensure Puck.mag behaves like other variants - just returning the last reading (avoids glitches when used with Puck.magOn)

libs/graphics/jswrap_graphics.c

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2648,7 +2648,6 @@ JsVar *jswrap_graphics_setFont4x6(JsVar *parent, int scale) {
26482648
return jswrap_graphics_setFontSizeX(parent, 1+JSGRAPHICS_FONTSIZE_4X6, false);
26492649
}
26502650

2651-
26522651
JsVar *jswrap_graphics_findFont(JsVar *parent, JsVar *text, JsVar *options) {
26532652
JsGraphics gfx; if (!graphicsGetFromVar(&gfx, parent)) return 0;
26542653
int width = gfx.data.width, height = gfx.data.height;
@@ -2687,6 +2686,7 @@ JsVar *jswrap_graphics_findFont(JsVar *parent, JsVar *text, JsVar *options) {
26872686
{"4x6", 6, 1, jswrap_graphics_setFont4x6}
26882687
#endif
26892688
};
2689+
26902690
int fontIdx = 0;
26912691
// check max font size
26922692
while (fontIdx<FONTS-1 && FONT[fontIdx].height>maxHeight)
@@ -2701,26 +2701,45 @@ JsVar *jswrap_graphics_findFont(JsVar *parent, JsVar *text, JsVar *options) {
27012701
graphicsGetFromVar(&gfx, parent);
27022702
if (wrap) {
27032703
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);
27092708
if (((stringMetrics.stringWidth <= width) && (stringMetrics.stringHeight <= height)) || // all good!
27102709
fontIdx==FONTS-1 || // no more fonts
27112710
FONT[fontIdx+1].height<minHeight // next font is too small
27122711
) break;
27132712
fontIdx++;
27142713
}
27152714
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)
27172716
if (stringMetrics.unrenderableChars) {
27182717
JsVar *intlFont = jspGetNamedField(parent, "setFontIntl", false);
27192718
if (intlFont) {
2720-
fontName = "Intl";
2719+
fontName = "Intl:2"; // start off using double-size Intl
27212720
jsvUnLock(jspExecuteFunction(intlFont, parent, 0, NULL));
27222721
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);
27242743
}
27252744
}
27262745
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) {
27402759
jsvObjectSetChildAndUnLock(result, "font", jsvNewFromString(fontName));
27412760
jsvObjectSetChildAndUnLock(result, "w", jsvNewFromInteger(stringMetrics.stringWidth));
27422761
jsvObjectSetChildAndUnLock(result, "h", jsvNewFromInteger(stringMetrics.stringHeight));
2743-
27442762
return result;
27452763
}
27462764
#endif

0 commit comments

Comments
 (0)