Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 11 additions & 28 deletions src/fontlibc/fontlibc.asm
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -2100,4 +2084,3 @@ currentFontRoot := _CurrentFontRoot - DataBaseAddr
DataBaseAddr:
; Embed the current font's properties as library variables
_CurrentFontProperties strucFont

40 changes: 40 additions & 0 deletions test/fontlibc/glyph_width/autotest.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
}
}
28 changes: 28 additions & 0 deletions test/fontlibc/glyph_width/makefile
Original file line number Diff line number Diff line change
@@ -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 $@
8 changes: 8 additions & 0 deletions test/fontlibc/glyph_width/src/fonts/fonts.c
Original file line number Diff line number Diff line change
@@ -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;
8 changes: 8 additions & 0 deletions test/fontlibc/glyph_width/src/fonts/fonts.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef FONTS_H
#define FONTS_H

#include <fontlibc.h>

extern fontlib_font_t *test_font;

#endif /* FONTS_H */
Binary file added test/fontlibc/glyph_width/src/fonts/testfont.fnt
Binary file not shown.
93 changes: 93 additions & 0 deletions test/fontlibc/glyph_width/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include <ti/getcsc.h>
#include <ti/screen.h>
#include <ti/sprintf.h>
#include <stdio.h>
#include <stdint.h>
#include <graphx.h>
#include <fontlibc.h>

#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;
}
Loading
Loading