Skip to content

Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.

Languages: the text functions once in NumPlay, not in every game; Chinese closer together in large text

Mason Chen07f50d45fc11
diff --git a/games/common/np_text.h b/games/common/np_text.h
index 6e05434f..8f734b7f 100644
--- a/games/common/np_text.h
+++ b/games/common/np_text.h
@@ -18,7 +18,9 @@
*
* Only NumPlay itself is built in other languages. NP_TEXT_EXTRA is 1 there
* (tools/lang.py sets it): code that handles letters beyond ASCII sits behind
- * it, so the English apps carry none of it. */
+ * it, so the English apps carry none of it. There the functions below are in
+ * NumPlay once for all its games (launcher/src/xfont.c); in English each file
+ * keeps its own, the little it uses. */
#ifndef NP_TEXT_H
#define NP_TEXT_H
#include <stdbool.h>
@@ -27,8 +29,48 @@
#define T(s) (s)
#define NP_TEXT_EXTRA 0
+/* Accents of np_latin() */
+enum { NP_ACC_NONE, NP_ACC_ACUTE, NP_ACC_GRAVE, NP_ACC_CIRC, NP_ACC_DIAER, NP_ACC_CEDIL, NP_ACC_TILDE, NP_ACC_RING };
+
+/* ---- the 12-pixel font
+ * Kept small, as the Chinese build holds a thousand letters and more: a letter is
+ * its code point, the index of its sizes (letters share a few dozen) and its
+ * bits, back to back with the others'; where they start is kept for every
+ * 16th letter, the others' found by adding the sizes before them. */
+typedef struct {
+ uint8_t adv, w, h; /* advance; the bitmap's size */
+ int8_t x, y; /* the bitmap's top-left from the pen, y down from the top of a 12-pixel line */
+} np_xglyph_t;
+typedef struct {
+ uint32_t count;
+ const uint16_t *cps; /* the letters, sorted (Chinese and its punctuation are all below 0x10000) */
+ const uint8_t *size; /* each one's in sizes */
+ const np_xglyph_t *sizes;
+ const uint32_t *start; /* the first bit of letters 0, 16, 32... */
+ const uint8_t *rows; /* w x h bits a letter, row by row, the first pixel first, lowest first in each byte */
+} np_xfont_t;
+
+extern const np_xfont_t np_xfont; /* the launcher's (launcher/src/np_xfont.h) */
+
+#if NP_TEXT_EXTRA && !defined(NP_TEXT_HERE)
+uint32_t np_utf8(const char **s);
+int np_text_cells(const char *s);
+int np_latin(uint32_t cp, char *base, char *second);
+void np_accent(int acc, int x, int y, int scale, void (*plot)(int x, int y, void *ctx), void *ctx);
+int np_xindex(uint32_t cp);
+const np_xglyph_t *np_xglyph(uint32_t cp);
+uint32_t np_xbit(uint32_t cp);
+int np_xdraw(uint32_t cp, int x, int top, int scale, void (*plot)(int x, int y, void *ctx), void *ctx);
+int np_xadvance(uint32_t cp, int scale);
+#else
+#if NP_TEXT_EXTRA
+#define NP_TEXT_FN
+#else
+#define NP_TEXT_FN static inline
+#endif
+
/* The next code point of a UTF-8 string, *s moved past it (0 at the end). */
-static inline uint32_t np_utf8(const char **s) {
+NP_TEXT_FN uint32_t np_utf8(const char **s) {
const uint8_t *p = (const uint8_t *)*s;
uint32_t c = p[0];
if (c < 0x80) {
@@ -50,7 +92,7 @@ static inline uint32_t np_utf8(const char **s) {
/* The cells a text takes in a fixed-width font (games drawing with the firmware's font, 7 or 10 pixels a
* letter): a letter each, two for a Chinese one (launcher/src/compat.c draws them in two cells). */
-static inline int np_text_cells(const char *s) {
+NP_TEXT_FN int np_text_cells(const char *s) {
int n = 0;
while (*s) {
uint32_t c = NP_TEXT_EXTRA ? np_utf8(&s) : (uint8_t)*s++;
@@ -59,14 +101,11 @@ static inline int np_text_cells(const char *s) {
return n;
}
-/* Accents of np_latin() */
-enum { NP_ACC_NONE, NP_ACC_ACUTE, NP_ACC_GRAVE, NP_ACC_CIRC, NP_ACC_DIAER, NP_ACC_CEDIL, NP_ACC_TILDE, NP_ACC_RING };
-
/* A Latin letter with an accent: returns the accent, *base the plain letter
* (0 when cp is no such letter). Ligatures (œ, æ, ß) and a few signs give
* *second too, a letter to draw after the first. Guillemets, typographic
* quotes and others give plain ASCII stand-ins. */
-static inline int np_latin(uint32_t cp, char *base, char *second) {
+NP_TEXT_FN int np_latin(uint32_t cp, char *base, char *second) {
static const char letters[] = "AAAAAAACEEEEIIIIDNOOOOOxOUUUUYTsaaaaaaaceeeeiiiidnooooo/ouuuuyty";
static const uint8_t accents[] = {
2, 1, 3, 6, 4, 7, 0, 5, 2, 1, 3, 4, 2, 1, 3, 4, 0, 6, 2, 1, 3, 6, 4, 0, 0, 2, 1, 3, 4, 1, 0, 0,
@@ -100,7 +139,7 @@ static inline int np_latin(uint32_t cp, char *base, char *second) {
* game knows its letters: the accent goes centered over a letter (or under
* it, for a cedilla), in the rows its font leaves free above lowercase
* letters, or on the first rows of a capital, as small capitals do. */
-static inline void np_accent(int acc, int x, int y, int scale, void (*plot)(int x, int y, void *ctx), void *ctx) {
+NP_TEXT_FN void np_accent(int acc, int x, int y, int scale, void (*plot)(int x, int y, void *ctx), void *ctx) {
/* bits 0-4 the upper row, 5-9 the lower one, left to right */
static const uint16_t marks[] = {0, 0x088, 0x082, 0x144, 0x140, 0x0C4, 0x154, 0x14E};
if (acc <= NP_ACC_NONE || acc > NP_ACC_RING) return;
@@ -111,28 +150,8 @@ static inline void np_accent(int acc, int x, int y, int scale, void (*plot)(int
for (int b = 0; b < scale; b++) plot(x + i * scale + b, y + j * scale + a, ctx);
}
-/* ---- the 12-pixel font
- * Kept small, as the Chinese build holds a thousand letters and more: a letter is
- * its code point, the index of its sizes (letters share a few dozen) and its
- * bits, back to back with the others'; where they start is kept for every
- * 16th letter, the others' found by adding the sizes before them. */
-typedef struct {
- uint8_t adv, w, h; /* advance; the bitmap's size */
- int8_t x, y; /* the bitmap's top-left from the pen, y down from the top of a 12-pixel line */
-} np_xglyph_t;
-typedef struct {
- uint32_t count;
- const uint16_t *cps; /* the letters, sorted (Chinese and its punctuation are all below 0x10000) */
- const uint8_t *size; /* each one's in sizes */
- const np_xglyph_t *sizes;
- const uint32_t *start; /* the first bit of letters 0, 16, 32... */
- const uint8_t *rows; /* w x h bits a letter, row by row, the first pixel first, lowest first in each byte */
-} np_xfont_t;
-
-extern const np_xfont_t np_xfont; /* the launcher's (launcher/src/np_xfont.h) */
-
/* The index of cp in the 12-pixel font, or -1. */
-static inline int np_xindex(uint32_t cp) {
+NP_TEXT_FN int np_xindex(uint32_t cp) {
uint32_t lo = 0, hi = np_xfont.count;
while (lo < hi) {
uint32_t m = (lo + hi) / 2;
@@ -143,13 +162,13 @@ static inline int np_xindex(uint32_t cp) {
}
/* The glyph of cp in the 12-pixel font, or NULL. */
-static inline const np_xglyph_t *np_xglyph(uint32_t cp) {
+NP_TEXT_FN const np_xglyph_t *np_xglyph(uint32_t cp) {
int k = np_xindex(cp);
return k < 0 ? 0 : &np_xfont.sizes[np_xfont.size[k]];
}
/* The first bit of cp's rows (cp in the font). */
-static inline uint32_t np_xbit(uint32_t cp) {
+NP_TEXT_FN uint32_t np_xbit(uint32_t cp) {
uint32_t k = (uint32_t)np_xindex(cp), bit = np_xfont.start[k / 16];
for (uint32_t i = k & ~15u; i < k; i++) {
const np_xglyph_t *g = &np_xfont.sizes[np_xfont.size[i]];
@@ -160,7 +179,7 @@ static inline uint32_t np_xbit(uint32_t cp) {
/* Draws cp from the 12-pixel font with its line's top at (x, top), each font
* pixel a scale x scale square; returns the advance (0 if the font has none). */
-static inline int np_xdraw(uint32_t cp, int x, int top, int scale, void (*plot)(int x, int y, void *ctx), void *ctx) {
+NP_TEXT_FN int np_xdraw(uint32_t cp, int x, int top, int scale, void (*plot)(int x, int y, void *ctx), void *ctx) {
const np_xglyph_t *g = np_xglyph(cp);
if (!g) return 0;
uint32_t bit = np_xbit(cp);
@@ -171,8 +190,9 @@ static inline int np_xdraw(uint32_t cp, int x, int top, int scale, void (*plot)(
for (int b = 0; b < scale; b++) plot(x + (g->x + i) * scale + b, top + (g->y + j) * scale + a, ctx);
return g->adv * scale;
}
-static inline int np_xadvance(uint32_t cp, int scale) {
+NP_TEXT_FN int np_xadvance(uint32_t cp, int scale) {
const np_xglyph_t *g = np_xglyph(cp);
return g ? g->adv * scale : 0;
}
#endif
+#endif
diff --git a/games/numdrive/src/gfx.c b/games/numdrive/src/gfx.c
index 3ae2d0d8..167fa3dc 100644
--- a/games/numdrive/src/gfx.c
+++ b/games/numdrive/src/gfx.c
@@ -90,14 +90,14 @@ int glyph_alpha(const Font *f, const Glyph *g, int x, int y) {
#if NP_TEXT_EXTRA
Letter g_letter(const Font *f, const char **s) {
Letter l = {0};
- uint32_t cp = np_utf8(s);
+ uint32_t cp = l.cp = np_utf8(s);
char b = (char)cp, b2 = 0;
if (cp >= 0x80) l.acc = np_latin(cp, &b, &b2);
if ((l.g = b ? font_glyph(f, (unsigned char)b) : 0)) {
l.g2 = b2 ? font_glyph(f, (unsigned char)b2) : 0;
l.adv = l.g->adv + (l.g2 ? l.g2->adv : 0);
} else if ((l.x = np_xglyph(cp))) {
- l.acc = 0, l.xs = f->h > 28 ? 2 : 1, l.adv = l.x->adv * l.xs;
+ l.acc = 0, l.xs = f->h > 28 ? 2 : 1, l.adv = l.x->adv * l.xs, l.bit = np_xbit(cp);
}
return l;
}
@@ -108,7 +108,7 @@ int g_letter_alpha(const Font *f, const Letter *l, int x, int y) {
if (l->x) { /* its 12 rows on the middle of the capitals */
int k = l->xs, dx = x - l->x->x * k, dy = y - ((f->cap_top + f->base) / 2 - 6 * k) - l->x->y * k;
if (dx < 0 || dy < 0 || dx >= l->x->w * k || dy >= l->x->h * k) return 0;
- uint32_t bit = (uint32_t)l->x->off * 8 + dy / k * l->x->w + dx / k;
+ uint32_t bit = l->bit + dy / k * l->x->w + dx / k;
return np_xfont.rows[bit >> 3] >> (bit & 7) & 1 ? 3 : 0;
}
if (!l->g) return 0;
diff --git a/games/numdrive/src/gfx.h b/games/numdrive/src/gfx.h
index fb50f747..ae708ff8 100644
--- a/games/numdrive/src/gfx.h
+++ b/games/numdrive/src/gfx.h
@@ -39,6 +39,7 @@ const Glyph *font_glyph(const Font *f, int ch);
typedef struct {
const Glyph *g, *g2;
const np_xglyph_t *x;
+ uint32_t cp, bit; /* the letter; where the 12-pixel font's starts */
int acc, adv, xs; /* the accent (np_latin), the advance, the 12-pixel font's scale */
} Letter;
Letter g_letter(const Font *f, const char **s);
diff --git a/games/numdrive/src/ui.c b/games/numdrive/src/ui.c
index d267d7ab..796eb324 100644
--- a/games/numdrive/src/ui.c
+++ b/games/numdrive/src/ui.c
@@ -545,7 +545,7 @@ void levels_draw(int sel, int scroll) {
const Font *ft = &font_l;
for (const char *s = tl; *s;) {
Letter l = g_letter(ft, &s);
- if (!l.g && (!l.x || l.x->cp < 0x2E80)) ft = &font_m;
+ if (!l.g && (!l.x || l.cp < 0x2E80)) ft = &font_m;
}
int ty = 22 - ft->base;
for (int d = 0; d < 8; d++) {
diff --git a/launcher/src/compat.c b/launcher/src/compat.c
index 0de921d4..e0eeaabe 100644
--- a/launcher/src/compat.c
+++ b/launcher/src/compat.c
@@ -49,14 +49,22 @@ static void cell_plot(int x, int y, void *ctx) {
if (x >= 0 && x < p->cw && y >= 0 && y < p->ch) p->cell[y * p->cw + x] = p->fg;
}
-/* a Chinese letter in two cells */
-static void draw_wide(uint32_t cp, int cw, int ch, int x, int y, color_t fg, color_t bg) {
+/* Chinese letters, two cells each: n of them from (x, y). Their 12-pixel letters go 14 pixels
+ * apart, centred in the cells (in the large font's 20-pixel pairs, a letter a pair looks spaced). */
+#define WIDE_STEP 14
+static void draw_wide(const uint32_t *cps, int n, int cw, int ch, int x, int y, color_t fg, color_t bg) {
color_t cell[20 * 18];
- for (int i = 0; i < 2 * cw * ch; i++) cell[i] = bg;
- cell_plot_t p = {cell, 2 * cw, ch, fg};
- np_xdraw(cp, (2 * cw - 12) / 2, (ch - 12) / 2, 1, cell_plot, &p);
- if (x + 2 * cw > 320 || y + ch > 240) return;
- eadk_display_push_rect((eadk_rect_t){(uint16_t)x, (uint16_t)y, (uint16_t)(2 * cw), (uint16_t)ch}, cell);
+ int start = (2 * cw - WIDE_STEP) * n / 2 + (WIDE_STEP - 12) / 2;
+ for (int k = 0; k < n; k++, x += 2 * cw) {
+ for (int i = 0; i < 2 * cw * ch; i++) cell[i] = bg;
+ cell_plot_t p = {cell, 2 * cw, ch, fg};
+ for (int i = 0; i < n; i++) {
+ int pen = start + WIDE_STEP * i - 2 * cw * k;
+ if (pen > -12 && pen < 2 * cw) np_xdraw(cps[i], pen, (ch - 12) / 2, 1, cell_plot, &p);
+ }
+ if (x + 2 * cw > 320 || y + ch > 240) return;
+ eadk_display_push_rect((eadk_rect_t){(uint16_t)x, (uint16_t)y, (uint16_t)(2 * cw), (uint16_t)ch}, cell);
+ }
}
static bool has_wide(const char *s) {
@@ -87,10 +95,19 @@ void np_display_draw_string(const char *text, eadk_point_t point, bool large_fon
s++, x += cw;
continue;
}
+ const char *at = s;
uint32_t cp = np_utf8(&s);
- if (cp >= 0x2E80 && np_xglyph(cp)) {
- draw_wide(cp, cw, ch, x, y, text_color, background_color);
- x += 2 * cw;
+ if (cp >= 0x2E80 && np_xglyph(cp)) { /* the run of them */
+ uint32_t run[16];
+ int n = 0;
+ for (s = at; *s && n < 16;) {
+ const char *next = s;
+ uint32_t c = np_utf8(&next);
+ if (c < 0x2E80 || !np_xglyph(c)) break;
+ run[n++] = c, s = next;
+ }
+ draw_wide(run, n, cw, ch, x, y, text_color, background_color);
+ x += 2 * cw * n;
continue;
}
char base = (char)cp, second = 0;
diff --git a/launcher/src/xfont.c b/launcher/src/xfont.c
index 82f54aa8..757eda48 100644
--- a/launcher/src/xfont.c
+++ b/launcher/src/xfont.c
@@ -1,4 +1,6 @@
/* The 12-pixel font's letters for every text of NumPlay and its games (Chinese), made by
- * tools/lang.py for a language build; none in English. See games/common/np_text.h. */
+ * tools/lang.py for a language build; none in English. See games/common/np_text.h.
+ * In a language build the text functions of np_text.h are here too, once for all the games. */
+#define NP_TEXT_HERE
#include "../../games/common/np_text.h"
#include "np_xfont.h"