|
@@ -130,9 +130,25 @@ void ngui_font_detach(struct ngui *ui)
|
|
|
ui->font_italic = 0;
|
|
ui->font_italic = 0;
|
|
|
ui->font_bold_italic = 0;
|
|
ui->font_bold_italic = 0;
|
|
|
ui->font_mono = 0;
|
|
ui->font_mono = 0;
|
|
|
|
|
+ ui->font_emoji = 0;
|
|
|
|
|
+ ui->emoji_loaded = 0;
|
|
|
ufont_reset();
|
|
ufont_reset();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+/* Emoji fallback is opened on first use so apps that never draw UTF-8 text
|
|
|
|
|
+ * do not pay for it. ufont_face_open_path() may clear the glyph cache, which
|
|
|
|
|
+ * is safe here: callers never keep a glyph pointer across this call. */
|
|
|
|
|
+static struct ufont_face *ngui_emoji_face(struct ngui *ui)
|
|
|
|
|
+{
|
|
|
|
|
+ if (!ui)
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ if (!ui->emoji_loaded) {
|
|
|
|
|
+ ui->emoji_loaded = 1;
|
|
|
|
|
+ ui->font_emoji = ufont_face_open_path("/ramfs/fonts/emoji.ttf");
|
|
|
|
|
+ }
|
|
|
|
|
+ return ui->font_emoji;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
void ngui_invalidate(struct ngui *ui)
|
|
void ngui_invalidate(struct ngui *ui)
|
|
|
{
|
|
{
|
|
|
ui->dirty = 1;
|
|
ui->dirty = 1;
|
|
@@ -845,6 +861,172 @@ void ngui_text_px(struct ngui *ui, int x, int y, const char *s,
|
|
|
ngui_text_px_common(ui, 0, x, y, s, n, color, px, bold, italic, mono);
|
|
ngui_text_px_common(ui, 0, x, y, s, n, color, px, bold, italic, mono);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+/* ---- UTF-8 text with an emoji fallback face --------------------------- */
|
|
|
|
|
+
|
|
|
|
|
+static int utf8_skip_cp(unsigned int cp)
|
|
|
|
|
+{
|
|
|
|
|
+ return cp == 0x200B || cp == 0x200C || cp == 0x200D ||
|
|
|
|
|
+ cp == 0xFE0E || cp == 0xFE0F;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/* Advance of one codepoint in 26.6 pixels, choosing the primary face, then
|
|
|
|
|
+ * the lazily loaded emoji face, then '?'. */
|
|
|
|
|
+static int ngui_utf8_advance(struct ngui *ui, struct ufont_face *f,
|
|
|
|
|
+ int px, unsigned int cp)
|
|
|
|
|
+{
|
|
|
|
|
+ struct ufont_face *emoji;
|
|
|
|
|
+
|
|
|
|
|
+ if (utf8_skip_cp(cp) || !f)
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ if (ufont_has_glyph(f, cp))
|
|
|
|
|
+ return ufont_advance_cp(f, px, cp);
|
|
|
|
|
+ emoji = ngui_emoji_face(ui);
|
|
|
|
|
+ if (emoji && ufont_has_glyph(emoji, cp))
|
|
|
|
|
+ return ufont_advance_cp(emoji, px, cp);
|
|
|
|
|
+ return ufont_advance_cp(f, px, '?');
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static unsigned int ngui_text_width_face_utf8(struct ngui *ui,
|
|
|
|
|
+ struct ufont_face *face,
|
|
|
|
|
+ const char *s, unsigned int n,
|
|
|
|
|
+ int px, int bold, int italic,
|
|
|
|
|
+ int mono)
|
|
|
|
|
+{
|
|
|
|
|
+ struct ufont_face *f = face ? face : ngui_face(ui, bold, italic, mono);
|
|
|
|
|
+ int i = 0, total = 0;
|
|
|
|
|
+
|
|
|
|
|
+ if (!f)
|
|
|
|
|
+ return n * 8u * (px >= 24 ? 2u : 1u);
|
|
|
|
|
+ while (i < (int)n) {
|
|
|
|
|
+ unsigned int cp = ufont_utf8_next(s, &i, (int)n);
|
|
|
|
|
+
|
|
|
|
|
+ total += ngui_utf8_advance(ui, f, px, cp);
|
|
|
|
|
+ }
|
|
|
|
|
+ return (unsigned int)((total + 32) >> 6);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static const struct ufont_glyph *ngui_utf8_glyph(struct ngui *ui,
|
|
|
|
|
+ struct ufont_face *f,
|
|
|
|
|
+ int px, unsigned int cp,
|
|
|
|
|
+ struct ufont_face **used)
|
|
|
|
|
+{
|
|
|
|
|
+ struct ufont_face *emoji;
|
|
|
|
|
+ const struct ufont_glyph *g;
|
|
|
|
|
+
|
|
|
|
|
+ *used = f;
|
|
|
|
|
+ if (utf8_skip_cp(cp) || !f)
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ if (ufont_has_glyph(f, cp)) {
|
|
|
|
|
+ g = ufont_glyph(f, px, cp);
|
|
|
|
|
+ if (g)
|
|
|
|
|
+ return g;
|
|
|
|
|
+ }
|
|
|
|
|
+ emoji = ngui_emoji_face(ui);
|
|
|
|
|
+ if (emoji && ufont_has_glyph(emoji, cp)) {
|
|
|
|
|
+ g = ufont_glyph(emoji, px, cp);
|
|
|
|
|
+ if (g) {
|
|
|
|
|
+ *used = emoji;
|
|
|
|
|
+ return g;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ *used = f;
|
|
|
|
|
+ return ufont_glyph(f, px, '?');
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+static void ngui_text_px_utf8_common(struct ngui *ui, struct ufont_face *face,
|
|
|
|
|
+ int x, int y, const char *s,
|
|
|
|
|
+ unsigned int n, unsigned short color,
|
|
|
|
|
+ int px, int bold, int italic, int mono)
|
|
|
|
|
+{
|
|
|
|
|
+ struct ufont_face *f = face ? face : ngui_face(ui, bold, italic, mono);
|
|
|
|
|
+ int i = 0, pen = 0, ascent;
|
|
|
|
|
+ int cx, cy, cw, ch;
|
|
|
|
|
+
|
|
|
|
|
+ if (!ui || !ui->px || !s)
|
|
|
|
|
+ return;
|
|
|
|
|
+ if (!f) {
|
|
|
|
|
+ ngui_text_px_common(ui, face, x, y, s, n, color, px, bold,
|
|
|
|
|
+ italic, mono);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ ascent = ngui_text_ascent_face(ui, face, px, mono);
|
|
|
|
|
+ ngui_clip_bounds(ui, &cx, &cy, &cw, &ch);
|
|
|
|
|
+ while (i < (int)n) {
|
|
|
|
|
+ unsigned int cp = ufont_utf8_next(s, &i, (int)n);
|
|
|
|
|
+ struct ufont_face *used;
|
|
|
|
|
+ const struct ufont_glyph *g;
|
|
|
|
|
+
|
|
|
|
|
+ if (utf8_skip_cp(cp))
|
|
|
|
|
+ continue;
|
|
|
|
|
+ g = ngui_utf8_glyph(ui, f, px, cp, &used);
|
|
|
|
|
+ if (!g) {
|
|
|
|
|
+ pen += 8 << 6;
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ ufont_blit_rgb565(ui->px, ui->w, ui->h,
|
|
|
|
|
+ x + (pen >> 6), y + ascent, g, color,
|
|
|
|
|
+ cx, cy, cw, ch);
|
|
|
|
|
+ pen += g->advance;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void ngui_text_n_utf8(struct ngui *ui, int x, int y, const char *s,
|
|
|
|
|
+ unsigned int n, unsigned short color)
|
|
|
|
|
+{
|
|
|
|
|
+ ngui_text_px_utf8_common(ui, 0, x, y, s, n, color,
|
|
|
|
|
+ ui ? ui->font_px : 14, 0, 0, 0);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void ngui_text_utf8(struct ngui *ui, int x, int y, const char *s,
|
|
|
|
|
+ unsigned short color)
|
|
|
|
|
+{
|
|
|
|
|
+ ngui_text_n_utf8(ui, x, y, s, s ? slen(s) : 0, color);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+unsigned int ngui_text_width_utf8(struct ngui *ui, const char *s)
|
|
|
|
|
+{
|
|
|
|
|
+ return ngui_text_width_face_utf8(ui, 0, s, s ? slen(s) : 0,
|
|
|
|
|
+ ui ? ui->font_px : 14, 0, 0, 0);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void ngui_text_ellipsis_utf8(struct ngui *ui, struct ngui_rect r,
|
|
|
|
|
+ int x, int y, const char *s,
|
|
|
|
|
+ unsigned short color)
|
|
|
|
|
+{
|
|
|
|
|
+ unsigned int n = s ? slen(s) : 0;
|
|
|
|
|
+ unsigned int end = 0, i = 0;
|
|
|
|
|
+ int avail = r.x + r.w - x;
|
|
|
|
|
+ int dots, width;
|
|
|
|
|
+
|
|
|
|
|
+ if (!ui || !s)
|
|
|
|
|
+ return;
|
|
|
|
|
+ if (avail <= 0)
|
|
|
|
|
+ return;
|
|
|
|
|
+ width = (int)ngui_text_width_utf8(ui, s);
|
|
|
|
|
+ if (width <= avail) {
|
|
|
|
|
+ ngui_text_utf8(ui, x, y, s, color);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ dots = (int)ngui_text_width_utf8(ui, "...");
|
|
|
|
|
+ if (dots >= avail)
|
|
|
|
|
+ return;
|
|
|
|
|
+ while (i < n) {
|
|
|
|
|
+ int next = i;
|
|
|
|
|
+
|
|
|
|
|
+ (void)ufont_utf8_next(s, &next, (int)n);
|
|
|
|
|
+ if ((int)ngui_text_width_face_utf8(ui, 0, s, (unsigned int)next,
|
|
|
|
|
+ ui->font_px, 0, 0, 0) +
|
|
|
|
|
+ dots > avail)
|
|
|
|
|
+ break;
|
|
|
|
|
+ i = (unsigned int)next;
|
|
|
|
|
+ end = i;
|
|
|
|
|
+ }
|
|
|
|
|
+ ngui_text_n_utf8(ui, x, y, s, end, color);
|
|
|
|
|
+ ngui_text_utf8(ui, x + (int)ngui_text_width_face_utf8(
|
|
|
|
|
+ ui, 0, s, end, ui->font_px, 0, 0, 0),
|
|
|
|
|
+ y, "...", color);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
void ngui_text_px_face(struct ngui *ui, struct ufont_face *face, int x,
|
|
void ngui_text_px_face(struct ngui *ui, struct ufont_face *face, int x,
|
|
|
int y, const char *s, unsigned int n,
|
|
int y, const char *s, unsigned int n,
|
|
|
unsigned short color, int px, int bold, int italic,
|
|
unsigned short color, int px, int bold, int italic,
|
|
@@ -1078,8 +1260,9 @@ int ngui_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
|
|
|
/* One row of a drop-down menu: left-aligned label, optional check mark and
|
|
/* One row of a drop-down menu: left-aligned label, optional check mark and
|
|
|
* right-aligned shortcut. Uses the same press/release/active semantics as
|
|
* right-aligned shortcut. Uses the same press/release/active semantics as
|
|
|
* ngui_button so a menu and its caller never disagree about a click. */
|
|
* ngui_button so a menu and its caller never disagree about a click. */
|
|
|
-int ngui_menu_item(struct ngui *ui, unsigned int id, struct ngui_rect r,
|
|
|
|
|
- const char *label, const char *shortcut, int checked)
|
|
|
|
|
|
|
+static int ngui_menu_item_common(struct ngui *ui, unsigned int id,
|
|
|
|
|
+ struct ngui_rect r, const char *label,
|
|
|
|
|
+ const char *shortcut, int checked, int utf8)
|
|
|
{
|
|
{
|
|
|
int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
|
|
int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
|
|
|
int press_hot = ui->pressed &&
|
|
int press_hot = ui->pressed &&
|
|
@@ -1113,12 +1296,21 @@ int ngui_menu_item(struct ngui *ui, unsigned int id, struct ngui_rect r,
|
|
|
ngui_line(ui, r.x + 8, ty + 10, r.x + 10, ty + 13, fg);
|
|
ngui_line(ui, r.x + 8, ty + 10, r.x + 10, ty + 13, fg);
|
|
|
ngui_line(ui, r.x + 10, ty + 13, r.x + 15, ty + 5, fg);
|
|
ngui_line(ui, r.x + 10, ty + 13, r.x + 15, ty + 5, fg);
|
|
|
}
|
|
}
|
|
|
- if (label)
|
|
|
|
|
- ngui_text(ui, r.x + 22, ty, label, fg);
|
|
|
|
|
|
|
+ if (label) {
|
|
|
|
|
+ if (utf8)
|
|
|
|
|
+ ngui_text_utf8(ui, r.x + 22, ty, label, fg);
|
|
|
|
|
+ else
|
|
|
|
|
+ ngui_text(ui, r.x + 22, ty, label, fg);
|
|
|
|
|
+ }
|
|
|
if (shortcut) {
|
|
if (shortcut) {
|
|
|
- int sw = (int)ngui_text_width_ui(ui, shortcut);
|
|
|
|
|
-
|
|
|
|
|
- ngui_text(ui, r.x + r.w - 8 - sw, ty, shortcut, fg);
|
|
|
|
|
|
|
+ int sw = (int)(utf8 ? ngui_text_width_utf8(ui, shortcut) :
|
|
|
|
|
+ ngui_text_width_ui(ui, shortcut));
|
|
|
|
|
+
|
|
|
|
|
+ if (utf8)
|
|
|
|
|
+ ngui_text_utf8(ui, r.x + r.w - 8 - sw, ty, shortcut,
|
|
|
|
|
+ fg);
|
|
|
|
|
+ else
|
|
|
|
|
+ ngui_text(ui, r.x + r.w - 8 - sw, ty, shortcut, fg);
|
|
|
}
|
|
}
|
|
|
ngui_pop_clip(ui);
|
|
ngui_pop_clip(ui);
|
|
|
if (hot)
|
|
if (hot)
|
|
@@ -1126,6 +1318,18 @@ int ngui_menu_item(struct ngui *ui, unsigned int id, struct ngui_rect r,
|
|
|
return fired;
|
|
return fired;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+int ngui_menu_item(struct ngui *ui, unsigned int id, struct ngui_rect r,
|
|
|
|
|
+ const char *label, const char *shortcut, int checked)
|
|
|
|
|
+{
|
|
|
|
|
+ return ngui_menu_item_common(ui, id, r, label, shortcut, checked, 0);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+int ngui_menu_item_utf8(struct ngui *ui, unsigned int id, struct ngui_rect r,
|
|
|
|
|
+ const char *label, const char *shortcut, int checked)
|
|
|
|
|
+{
|
|
|
|
|
+ return ngui_menu_item_common(ui, id, r, label, shortcut, checked, 1);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
int ngui_icon_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
|
|
int ngui_icon_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
|
|
|
enum ngui_icon icon){
|
|
enum ngui_icon icon){
|
|
|
int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
|
|
int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
|