Skip to content

Commit bc1ab5d

Browse files
ZERICO2005mateoconlechuga
authored andcommitted
fixed fontlib_GetGlyphWidth
1 parent d86552b commit bc1ab5d

File tree

8 files changed

+451
-28
lines changed

8 files changed

+451
-28
lines changed

src/fontlibc/fontlibc.asm

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,43 +1326,27 @@ fontlib_GetGlyphWidth:
13261326
ld hl,arg0
13271327
add hl,sp
13281328
ld a,(hl)
1329-
; jp util.GetGlyphWidth
1330-
assert $ = util.GetGlyphWidth
1331-
1332-
1333-
;-------------------------------------------------------------------------------
1334-
util.GetGlyphWidth:
1335-
; Internal-use version
1336-
; Input:
1337-
; A: Codepoint
1338-
; Output:
1339-
; A: Width
1340-
; C if invalid codepoint,NC if valid
1341-
; Destroys:
1342-
; DE
1343-
; HL
1344-
; Subtract out firstGlyph
13451329
ld hl,_CurrentFontProperties.firstGlyph
13461330
sub a,(hl)
1347-
; Validate that the glyph index is actually valid
1348-
jr nc,.checkMaxIndex
1349-
.invalidIndex:
1350-
xor a,a
1351-
scf
1352-
ret
1353-
.checkMaxIndex:
1354-
ld hl,_CurrentFontProperties.totalGlyphs
1355-
cp a,(hl)
13561331
jr c,.invalidIndex
1357-
; Look up width
1358-
or a,a
1332+
; Code >= firstGlyph
13591333
sbc hl,hl
13601334
ld l,a
1335+
ld a, (_CurrentFontProperties.totalGlyphs)
1336+
; (0 - 1) becomes 255, handling the special case of 256 totalGlyphs
1337+
dec a
1338+
cp a, l
1339+
jr c, .invalidIndex
1340+
; totalGlyphs - 1 >= Code
1341+
; totalGlyphs > Code
13611342
ld de,(_CurrentFontProperties.widthsTablePtr)
13621343
add hl,de
13631344
ld a,(hl)
13641345
ret
13651346

1347+
.invalidIndex:
1348+
xor a, a
1349+
ret
13661350

13671351
;;-------------------------------------------------------------------------------
13681352
fontlib_GetStringWidth:
@@ -2100,4 +2084,3 @@ currentFontRoot := _CurrentFontRoot - DataBaseAddr
21002084
DataBaseAddr:
21012085
; Embed the current font's properties as library variables
21022086
_CurrentFontProperties strucFont
2103-
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"transfer_files": [
3+
"bin/DEMO.8xp"
4+
],
5+
"target": {
6+
"name": "DEMO",
7+
"isASM": true
8+
},
9+
"sequence": [
10+
"action|launch",
11+
"delay|1000",
12+
"hashWait|1",
13+
"key|enter",
14+
"delay|300",
15+
"hashWait|2"
16+
],
17+
"hashes": {
18+
"1": {
19+
"description": "All tests passed",
20+
"timeout": 5000,
21+
"start": "vram_start",
22+
"size": "vram_16_size",
23+
"expected_CRCs": [
24+
"38E2AD5A"
25+
]
26+
},
27+
"2": {
28+
"description": "Exit",
29+
"start": "vram_start",
30+
"size": "vram_16_size",
31+
"expected_CRCs": [
32+
"FFAF89BA",
33+
"101734A5",
34+
"9DA19F44",
35+
"A32840C8",
36+
"349F4775"
37+
]
38+
}
39+
}
40+
}

test/fontlibc/glyph_width/makefile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# ----------------------------
2+
# Makefile Options
3+
# ----------------------------
4+
5+
NAME = DEMO
6+
ICON = icon.png
7+
DESCRIPTION = "CE C Toolchain Demo"
8+
COMPRESSED = NO
9+
10+
CFLAGS = -Wall -Wextra -Oz
11+
CXXFLAGS = -Wall -Wextra -Oz
12+
13+
# fonts.c depends on testfont.inc, which is built from testfont.fnt
14+
FONTDIR = $(SRCDIR)/fonts
15+
FONT = $(FONTDIR)/testfont.fnt
16+
FONT_INC = $(FONTDIR)/testfont.inc
17+
18+
DEPS = $(FONT_INC)
19+
20+
# ----------------------------
21+
22+
include $(shell cedev-config --makefile)
23+
24+
# ----------------------------
25+
26+
$(FONT_INC): $(FONT)
27+
$(Q)$(call MKDIR,$(@D))
28+
$(Q)convfont -o carray -f $< -a 1 -b 1 -w bold -c 2 -x 9 -l 0x0B -Z $@
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include "fonts.h"
2+
3+
static unsigned char test_font_data[] =
4+
{
5+
#include "testfont.inc"
6+
};
7+
8+
fontlib_font_t *test_font = (fontlib_font_t *)test_font_data;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef FONTS_H
2+
#define FONTS_H
3+
4+
#include <fontlibc.h>
5+
6+
extern fontlib_font_t *test_font;
7+
8+
#endif /* FONTS_H */
5.66 KB
Binary file not shown.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#include <ti/getcsc.h>
2+
#include <ti/screen.h>
3+
#include <ti/sprintf.h>
4+
#include <stdio.h>
5+
#include <stdint.h>
6+
#include <graphx.h>
7+
#include <fontlibc.h>
8+
9+
#include "fonts/fonts.h"
10+
11+
#include "truth.h"
12+
13+
#define C(expr) if (!(expr)) { return __LINE__; }
14+
15+
int run_tests(void) {
16+
17+
/* 256 glyph test */
18+
fontlib_SetFont(test_font, 0);
19+
C(fontlib_GetFirstGlyph() == 0);
20+
C(fontlib_GetTotalGlyphs() == 256);
21+
C(fontlib_GetTotalGlyphs() == 256);
22+
for (unsigned i = 0; i <= 255; i++) {
23+
uint8_t width = fontlib_GetGlyphWidth((uint8_t)i);
24+
C(width == truth_width[i - 0]);
25+
}
26+
27+
/* 255 glyph test */
28+
test_font->first_glyph = 1;
29+
test_font->total_glyphs = 255;
30+
fontlib_SetFont(test_font, 0);
31+
C(fontlib_GetFirstGlyph() == 1);
32+
C(fontlib_GetTotalGlyphs() == 255);
33+
C(fontlib_GetGlyphWidth(0) == 0);
34+
for (unsigned i = 1; i <= 255; i++) {
35+
uint8_t width = fontlib_GetGlyphWidth((uint8_t)i);
36+
C(width == truth_width[i - 1]);
37+
}
38+
39+
/* 128 glyph test */
40+
test_font->first_glyph = 0;
41+
test_font->total_glyphs = 128;
42+
fontlib_SetFont(test_font, 0);
43+
C(fontlib_GetFirstGlyph() == 0);
44+
C(fontlib_GetTotalGlyphs() == 128);
45+
for (unsigned i = 0; i <= 127; i++) {
46+
uint8_t width = fontlib_GetGlyphWidth((uint8_t)i);
47+
C(width == truth_width[i - 0]);
48+
}
49+
for (unsigned i = 128; i <= 255; i++) {
50+
uint8_t width = fontlib_GetGlyphWidth((uint8_t)i);
51+
C(width == 0);
52+
}
53+
54+
/* 64 glyph test */
55+
test_font->first_glyph = 32;
56+
test_font->total_glyphs = 64;
57+
fontlib_SetFont(test_font, 0);
58+
C(fontlib_GetFirstGlyph() == 32);
59+
C(fontlib_GetTotalGlyphs() == 64);
60+
for (unsigned i = 0; i <= 31; i++) {
61+
uint8_t width = fontlib_GetGlyphWidth((uint8_t)i);
62+
C(width == 0);
63+
}
64+
for (unsigned i = 32; i <= 95; i++) {
65+
uint8_t width = fontlib_GetGlyphWidth((uint8_t)i);
66+
C(width == truth_width[i - 32]);
67+
}
68+
for (unsigned i = 96; i <= 255; i++) {
69+
uint8_t width = fontlib_GetGlyphWidth((uint8_t)i);
70+
C(width == 0);
71+
}
72+
73+
return 0;
74+
}
75+
76+
int main(void) {
77+
gfx_Begin();
78+
int failed_test = run_tests();
79+
gfx_End();
80+
81+
os_ClrHome();
82+
if (failed_test != 0) {
83+
char buf[sizeof("Failed test L-8388608\n")];
84+
boot_sprintf(buf, "Failed test L%d\n", failed_test);
85+
fputs(buf, stdout);
86+
} else {
87+
fputs("All tests passed", stdout);
88+
}
89+
90+
while (!os_GetCSC());
91+
92+
return 0;
93+
}

0 commit comments

Comments
 (0)