From 5c4d51077d3716f4fc18894ac02ae106deb1c7c5 Mon Sep 17 00:00:00 2001 From: zerico <71151164+ZERICO2005@users.noreply.github.com> Date: Sun, 14 Dec 2025 16:08:08 -0700 Subject: [PATCH] fixed fontlib_GetGlyphWidth --- src/fontlibc/fontlibc.asm | 39 +-- test/fontlibc/glyph_width/autotest.json | 40 +++ test/fontlibc/glyph_width/makefile | 28 ++ test/fontlibc/glyph_width/src/fonts/fonts.c | 8 + test/fontlibc/glyph_width/src/fonts/fonts.h | 8 + .../glyph_width/src/fonts/testfont.fnt | Bin 0 -> 5792 bytes test/fontlibc/glyph_width/src/main.c | 93 +++++++ test/fontlibc/glyph_width/src/truth.h | 263 ++++++++++++++++++ 8 files changed, 451 insertions(+), 28 deletions(-) create mode 100644 test/fontlibc/glyph_width/autotest.json create mode 100644 test/fontlibc/glyph_width/makefile create mode 100644 test/fontlibc/glyph_width/src/fonts/fonts.c create mode 100644 test/fontlibc/glyph_width/src/fonts/fonts.h create mode 100644 test/fontlibc/glyph_width/src/fonts/testfont.fnt create mode 100644 test/fontlibc/glyph_width/src/main.c create mode 100644 test/fontlibc/glyph_width/src/truth.h diff --git a/src/fontlibc/fontlibc.asm b/src/fontlibc/fontlibc.asm index 87f1b1076..2ab838fdd 100644 --- a/src/fontlibc/fontlibc.asm +++ b/src/fontlibc/fontlibc.asm @@ -1326,43 +1326,27 @@ fontlib_GetGlyphWidth: ld hl,arg0 add hl,sp ld a,(hl) -; jp util.GetGlyphWidth -assert $ = util.GetGlyphWidth - - -;------------------------------------------------------------------------------- -util.GetGlyphWidth: -; Internal-use version -; Input: -; A: Codepoint -; Output: -; A: Width -; C if invalid codepoint,NC if valid -; Destroys: -; DE -; HL -; Subtract out firstGlyph ld hl,_CurrentFontProperties.firstGlyph sub a,(hl) -; Validate that the glyph index is actually valid - jr nc,.checkMaxIndex -.invalidIndex: - xor a,a - scf - ret -.checkMaxIndex: - ld hl,_CurrentFontProperties.totalGlyphs - cp a,(hl) jr c,.invalidIndex -; Look up width - or a,a + ; Code >= firstGlyph sbc hl,hl ld l,a + ld a, (_CurrentFontProperties.totalGlyphs) + ; (0 - 1) becomes 255, handling the special case of 256 totalGlyphs + dec a + cp a, l + jr c, .invalidIndex + ; totalGlyphs - 1 >= Code + ; totalGlyphs > Code ld de,(_CurrentFontProperties.widthsTablePtr) add hl,de ld a,(hl) ret +.invalidIndex: + xor a, a + ret ;;------------------------------------------------------------------------------- fontlib_GetStringWidth: @@ -2100,4 +2084,3 @@ currentFontRoot := _CurrentFontRoot - DataBaseAddr DataBaseAddr: ; Embed the current font's properties as library variables _CurrentFontProperties strucFont - diff --git a/test/fontlibc/glyph_width/autotest.json b/test/fontlibc/glyph_width/autotest.json new file mode 100644 index 000000000..be5eeed3c --- /dev/null +++ b/test/fontlibc/glyph_width/autotest.json @@ -0,0 +1,40 @@ +{ + "transfer_files": [ + "bin/DEMO.8xp" + ], + "target": { + "name": "DEMO", + "isASM": true + }, + "sequence": [ + "action|launch", + "delay|1000", + "hashWait|1", + "key|enter", + "delay|300", + "hashWait|2" + ], + "hashes": { + "1": { + "description": "All tests passed", + "timeout": 5000, + "start": "vram_start", + "size": "vram_16_size", + "expected_CRCs": [ + "38E2AD5A" + ] + }, + "2": { + "description": "Exit", + "start": "vram_start", + "size": "vram_16_size", + "expected_CRCs": [ + "FFAF89BA", + "101734A5", + "9DA19F44", + "A32840C8", + "349F4775" + ] + } + } +} diff --git a/test/fontlibc/glyph_width/makefile b/test/fontlibc/glyph_width/makefile new file mode 100644 index 000000000..5b1e23bf7 --- /dev/null +++ b/test/fontlibc/glyph_width/makefile @@ -0,0 +1,28 @@ +# ---------------------------- +# Makefile Options +# ---------------------------- + +NAME = DEMO +ICON = icon.png +DESCRIPTION = "CE C Toolchain Demo" +COMPRESSED = NO + +CFLAGS = -Wall -Wextra -Oz +CXXFLAGS = -Wall -Wextra -Oz + +# fonts.c depends on testfont.inc, which is built from testfont.fnt +FONTDIR = $(SRCDIR)/fonts +FONT = $(FONTDIR)/testfont.fnt +FONT_INC = $(FONTDIR)/testfont.inc + +DEPS = $(FONT_INC) + +# ---------------------------- + +include $(shell cedev-config --makefile) + +# ---------------------------- + +$(FONT_INC): $(FONT) + $(Q)$(call MKDIR,$(@D)) + $(Q)convfont -o carray -f $< -a 1 -b 1 -w bold -c 2 -x 9 -l 0x0B -Z $@ diff --git a/test/fontlibc/glyph_width/src/fonts/fonts.c b/test/fontlibc/glyph_width/src/fonts/fonts.c new file mode 100644 index 000000000..420289603 --- /dev/null +++ b/test/fontlibc/glyph_width/src/fonts/fonts.c @@ -0,0 +1,8 @@ +#include "fonts.h" + +static unsigned char test_font_data[] = +{ + #include "testfont.inc" +}; + +fontlib_font_t *test_font = (fontlib_font_t *)test_font_data; diff --git a/test/fontlibc/glyph_width/src/fonts/fonts.h b/test/fontlibc/glyph_width/src/fonts/fonts.h new file mode 100644 index 000000000..6d450b431 --- /dev/null +++ b/test/fontlibc/glyph_width/src/fonts/fonts.h @@ -0,0 +1,8 @@ +#ifndef FONTS_H +#define FONTS_H + +#include + +extern fontlib_font_t *test_font; + +#endif /* FONTS_H */ diff --git a/test/fontlibc/glyph_width/src/fonts/testfont.fnt b/test/fontlibc/glyph_width/src/fonts/testfont.fnt new file mode 100644 index 0000000000000000000000000000000000000000..cb63e3dc981bb8b0f0da6b10e56942a3d8da9542 GIT binary patch literal 5792 zcmbtXeTY+67C)LyJ|=IHajL_k2A!3mNLeJ6BiinodKb9gQMMTQ7lu}+zc`fd96k8;_GYqLNT4X4pmXeh)+21+$eT|d! zpFNr1JNJCt&vWm&cWC|Tar!@Lm<;?V#v^n-9;1)q!*mtCOVBTaKY}fh7@~1tG7+Pli8$>}MCnDy z2NDrF1i1p708Rsc11dWdGy8dXs~n@-Ys;3~>X=_oA&kKmuk#A!T(cx4c; zOp3!fs zls^Xl61a`>f55*7XanN2VTiK8(;E;U;JFP+`rU>Il~Jxi|5y0Di}D5N{|?*&zJokG zhByIF0lxtD0xtotj%ic^{|j&i_yD*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4`Vmu_d@E5N7?K>fzJp)uriQ z4YXm@NHCy2_3-!^SdZHr{0g$VI7E2zT0BgoMB83;?0btWayWw*3x_k1IGlmR*JMaS za03aOFeGs&bqyJGKyFp9AYH4d-W~7VBfa~Q^zKW%nJT_Chi6xiS|W;l>T-JlU#4`$BI~@<3`jgi2Dm9?k|wIhd}Z?-FJ`r{|oOiY1#D*-b8+X`;P3G`0XENj!x`-qjb>M Ga^Zi4?-x-3 literal 0 HcmV?d00001 diff --git a/test/fontlibc/glyph_width/src/main.c b/test/fontlibc/glyph_width/src/main.c new file mode 100644 index 000000000..54c9f7782 --- /dev/null +++ b/test/fontlibc/glyph_width/src/main.c @@ -0,0 +1,93 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "fonts/fonts.h" + +#include "truth.h" + +#define C(expr) if (!(expr)) { return __LINE__; } + +int run_tests(void) { + + /* 256 glyph test */ + fontlib_SetFont(test_font, 0); + C(fontlib_GetFirstGlyph() == 0); + C(fontlib_GetTotalGlyphs() == 256); + C(fontlib_GetTotalGlyphs() == 256); + for (unsigned i = 0; i <= 255; i++) { + uint8_t width = fontlib_GetGlyphWidth((uint8_t)i); + C(width == truth_width[i - 0]); + } + + /* 255 glyph test */ + test_font->first_glyph = 1; + test_font->total_glyphs = 255; + fontlib_SetFont(test_font, 0); + C(fontlib_GetFirstGlyph() == 1); + C(fontlib_GetTotalGlyphs() == 255); + C(fontlib_GetGlyphWidth(0) == 0); + for (unsigned i = 1; i <= 255; i++) { + uint8_t width = fontlib_GetGlyphWidth((uint8_t)i); + C(width == truth_width[i - 1]); + } + + /* 128 glyph test */ + test_font->first_glyph = 0; + test_font->total_glyphs = 128; + fontlib_SetFont(test_font, 0); + C(fontlib_GetFirstGlyph() == 0); + C(fontlib_GetTotalGlyphs() == 128); + for (unsigned i = 0; i <= 127; i++) { + uint8_t width = fontlib_GetGlyphWidth((uint8_t)i); + C(width == truth_width[i - 0]); + } + for (unsigned i = 128; i <= 255; i++) { + uint8_t width = fontlib_GetGlyphWidth((uint8_t)i); + C(width == 0); + } + + /* 64 glyph test */ + test_font->first_glyph = 32; + test_font->total_glyphs = 64; + fontlib_SetFont(test_font, 0); + C(fontlib_GetFirstGlyph() == 32); + C(fontlib_GetTotalGlyphs() == 64); + for (unsigned i = 0; i <= 31; i++) { + uint8_t width = fontlib_GetGlyphWidth((uint8_t)i); + C(width == 0); + } + for (unsigned i = 32; i <= 95; i++) { + uint8_t width = fontlib_GetGlyphWidth((uint8_t)i); + C(width == truth_width[i - 32]); + } + for (unsigned i = 96; i <= 255; i++) { + uint8_t width = fontlib_GetGlyphWidth((uint8_t)i); + C(width == 0); + } + + return 0; +} + +int main(void) { + gfx_Begin(); + int failed_test = run_tests(); + gfx_End(); + + os_ClrHome(); + if (failed_test != 0) { + char buf[sizeof("Failed test L-8388608\n")]; + boot_sprintf(buf, "Failed test L%d\n", failed_test); + fputs(buf, stdout); + } else { + fputs("All tests passed", stdout); + } + + while (!os_GetCSC()); + + return 0; +} diff --git a/test/fontlibc/glyph_width/src/truth.h b/test/fontlibc/glyph_width/src/truth.h new file mode 100644 index 000000000..a68a769f0 --- /dev/null +++ b/test/fontlibc/glyph_width/src/truth.h @@ -0,0 +1,263 @@ +#ifndef TRUTH_H +#define TRUTH_H + +const unsigned char truth_width[] = { +/* 00 */ 9, +/* 01 */ 9, +/* 02 */ 9, +/* 03 */ 9, +/* 04 */ 9, +/* 05 */ 9, +/* 06 */ 9, +/* 07 */ 9, +/* 08 */ 9, +/* 09 */ 9, +/* 0A */ 9, +/* 0B */ 9, +/* 0C */ 9, +/* 0D */ 9, +/* 0E */ 9, +/* 0F */ 9, +/* 10 */ 9, +/* 11 */ 9, +/* 12 */ 9, +/* 13 */ 9, +/* 14 */ 9, +/* 15 */ 9, +/* 16 */ 9, +/* 17 */ 9, +/* 18 */ 8, +/* 19 */ 8, +/* 1A */ 7, +/* 1B */ 11, +/* 1C */ 7, +/* 1D */ 7, +/* 1E */ 7, +/* 1F */ 7, +/* 20 */ 4, +/* 21 */ 3, +/* 22 */ 6, +/* 23 */ 10, +/* 24 */ 8, +/* 25 */ 7, +/* 26 */ 8, +/* 27 */ 4, +/* 28 */ 5, +/* 29 */ 5, +/* 2A */ 6, +/* 2B */ 7, +/* 2C */ 4, +/* 2D */ 5, +/* 2E */ 3, +/* 2F */ 7, +/* 30 */ 7, +/* 31 */ 7, +/* 32 */ 7, +/* 33 */ 7, +/* 34 */ 7, +/* 35 */ 7, +/* 36 */ 7, +/* 37 */ 7, +/* 38 */ 7, +/* 39 */ 7, +/* 3A */ 3, +/* 3B */ 4, +/* 3C */ 7, +/* 3D */ 7, +/* 3E */ 7, +/* 3F */ 8, +/* 40 */ 9, +/* 41 */ 8, +/* 42 */ 8, +/* 43 */ 8, +/* 44 */ 8, +/* 45 */ 8, +/* 46 */ 8, +/* 47 */ 9, +/* 48 */ 9, +/* 49 */ 7, +/* 4A */ 8, +/* 4B */ 8, +/* 4C */ 8, +/* 4D */ 11, +/* 4E */ 9, +/* 4F */ 9, +/* 50 */ 8, +/* 51 */ 9, +/* 52 */ 8, +/* 53 */ 8, +/* 54 */ 9, +/* 55 */ 9, +/* 56 */ 8, +/* 57 */ 11, +/* 58 */ 9, +/* 59 */ 9, +/* 5A */ 9, +/* 5B */ 5, +/* 5C */ 7, +/* 5D */ 5, +/* 5E */ 8, +/* 5F */ 8, +/* 60 */ 4, +/* 61 */ 7, +/* 62 */ 7, +/* 63 */ 7, +/* 64 */ 7, +/* 65 */ 7, +/* 66 */ 6, +/* 67 */ 7, +/* 68 */ 7, +/* 69 */ 3, +/* 6A */ 6, +/* 6B */ 7, +/* 6C */ 3, +/* 6D */ 9, +/* 6E */ 7, +/* 6F */ 7, +/* 70 */ 7, +/* 71 */ 7, +/* 72 */ 7, +/* 73 */ 7, +/* 74 */ 5, +/* 75 */ 7, +/* 76 */ 8, +/* 77 */ 9, +/* 78 */ 8, +/* 79 */ 7, +/* 7A */ 6, +/* 7B */ 6, +/* 7C */ 3, +/* 7D */ 6, +/* 7E */ 8, +/* 7F */ 10, +/* 80 */ 8, +/* 81 */ 8, +/* 82 */ 4, +/* 83 */ 7, +/* 84 */ 7, +/* 85 */ 8, +/* 86 */ 7, +/* 87 */ 7, +/* 88 */ 8, +/* 89 */ 10, +/* 8A */ 8, +/* 8B */ 5, +/* 8C */ 12, +/* 8D */ 8, +/* 8E */ 9, +/* 8F */ 9, +/* 90 */ 8, +/* 91 */ 4, +/* 92 */ 4, +/* 93 */ 7, +/* 94 */ 7, +/* 95 */ 5, +/* 96 */ 6, +/* 97 */ 9, +/* 98 */ 8, +/* 99 */ 6, +/* 9A */ 7, +/* 9B */ 5, +/* 9C */ 11, +/* 9D */ 8, +/* 9E */ 6, +/* 9F */ 9, +/* A0 */ 4, +/* A1 */ 3, +/* A2 */ 8, +/* A3 */ 9, +/* A4 */ 9, +/* A5 */ 9, +/* A6 */ 9, +/* A7 */ 8, +/* A8 */ 8, +/* A9 */ 7, +/* AA */ 7, +/* AB */ 8, +/* AC */ 7, +/* AD */ 7, +/* AE */ 8, +/* AF */ 7, +/* B0 */ 7, +/* B1 */ 7, +/* B2 */ 5, +/* B3 */ 5, +/* B4 */ 8, +/* B5 */ 8, +/* B6 */ 9, +/* B7 */ 3, +/* B8 */ 8, +/* B9 */ 5, +/* BA */ 7, +/* BB */ 8, +/* BC */ 9, +/* BD */ 9, +/* BE */ 9, +/* BF */ 8, +/* C0 */ 8, +/* C1 */ 8, +/* C2 */ 8, +/* C3 */ 8, +/* C4 */ 8, +/* C5 */ 8, +/* C6 */ 12, +/* C7 */ 8, +/* C8 */ 8, +/* C9 */ 8, +/* CA */ 8, +/* CB */ 8, +/* CC */ 7, +/* CD */ 7, +/* CE */ 7, +/* CF */ 7, +/* D0 */ 9, +/* D1 */ 9, +/* D2 */ 9, +/* D3 */ 9, +/* D4 */ 9, +/* D5 */ 9, +/* D6 */ 9, +/* D7 */ 8, +/* D8 */ 9, +/* D9 */ 9, +/* DA */ 9, +/* DB */ 9, +/* DC */ 9, +/* DD */ 9, +/* DE */ 8, +/* DF */ 8, +/* E0 */ 7, +/* E1 */ 7, +/* E2 */ 7, +/* E3 */ 7, +/* E4 */ 7, +/* E5 */ 7, +/* E6 */ 11, +/* E7 */ 7, +/* E8 */ 7, +/* E9 */ 7, +/* EA */ 7, +/* EB */ 7, +/* EC */ 3, +/* ED */ 3, +/* EE */ 5, +/* EF */ 7, +/* F0 */ 7, +/* F1 */ 7, +/* F2 */ 7, +/* F3 */ 7, +/* F4 */ 7, +/* F5 */ 7, +/* F6 */ 7, +/* F7 */ 7, +/* F8 */ 9, +/* F9 */ 7, +/* FA */ 7, +/* FB */ 7, +/* FC */ 7, +/* FD */ 7, +/* FE */ 7, +/* FF */ 7, +}; + +#endif /* TRUTH_H */