1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-25 11:26:47 +01:00

Bangle.js: Ensure we split Chinese words every 2 chars when converting them to bitmaps to give us the opportunity to wrap text better

Also make sure we never create a bitmap >255 width/height as this wouldn't work on Espruino
This commit is contained in:
Gordon Williams 2023-11-09 15:12:59 +00:00
parent f4707c15f4
commit cb0962e0f6

View File

@ -1114,7 +1114,7 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
return true; return true;
} }
private String renderUnicodeWordAsImage(String word) { private String renderUnicodeWordPartAsImage(String word) {
// check for emoji // check for emoji
boolean hasEmoji = false; boolean hasEmoji = false;
if (EmojiUtils.getAllEmojis() == null) if (EmojiUtils.getAllEmojis() == null)
@ -1125,7 +1125,31 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
break; break;
} }
// if we had emoji, ensure we create 3 bit color (not 1 bit B&W) // if we had emoji, ensure we create 3 bit color (not 1 bit B&W)
return "\0"+bitmapToEspruinoString(textToBitmap(word), hasEmoji ? BangleJSBitmapStyle.RGB_3BPP_TRANSPARENT : BangleJSBitmapStyle.MONOCHROME_TRANSPARENT); BangleJSBitmapStyle style = hasEmoji ? BangleJSBitmapStyle.RGB_3BPP_TRANSPARENT : BangleJSBitmapStyle.MONOCHROME_TRANSPARENT;
return "\0"+bitmapToEspruinoString(textToBitmap(word), style);
}
private String renderUnicodeWordAsImage(String word) {
// if we have Chinese/Japanese/Korean chars, split into 2 char chunks to allow easier text wrapping
// it's not perfect but better than nothing
boolean hasCJK = false;
for (int i=0;i<word.length();i++) {
char ch = word.charAt(i);
hasCJK |= ch>=0x4E00 && ch<=0x9FFF; // "CJK Unified Ideographs" block
}
if (hasCJK) {
// split every 2 chars
String result = "";
for (int i=0;i<word.length();i+=2) {
int len = 2;
if (i+len > word.length())
len = word.length()-i;
result += renderUnicodeWordPartAsImage(word.substring(i, i+len));
}
return result;
}
// else just render the word as-is
return renderUnicodeWordPartAsImage(word);
} }
public String renderUnicodeAsImage(String txt) { public String renderUnicodeAsImage(String txt) {
@ -1596,6 +1620,14 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
public static byte[] bitmapToEspruinoArray(Bitmap bitmap, BangleJSBitmapStyle style) { public static byte[] bitmapToEspruinoArray(Bitmap bitmap, BangleJSBitmapStyle style) {
int width = bitmap.getWidth(); int width = bitmap.getWidth();
int height = bitmap.getHeight(); int height = bitmap.getHeight();
if (width>255) {
LOG.warn("bitmapToEspruinoArray width of "+width+" > 255 (Espruino max) - cropping");
width = 255;
}
if (height>255) {
LOG.warn("bitmapToEspruinoArray height of "+height+" > 255 (Espruino max) - cropping");
height = 255;
}
int bpp = (style==BangleJSBitmapStyle.RGB_3BPP || int bpp = (style==BangleJSBitmapStyle.RGB_3BPP ||
style==BangleJSBitmapStyle.RGB_3BPP_TRANSPARENT) ? 3 : 1; style==BangleJSBitmapStyle.RGB_3BPP_TRANSPARENT) ? 3 : 1;
byte[] pixels = new byte[width * height]; byte[] pixels = new byte[width * height];