Skip to content

Commit a431916

Browse files
ZERICO2005mateoconlechuga
authored andcommitted
Add fontlib_LoadFont to replace fontlib_SetFont
1 parent d093e1b commit a431916

6 files changed

Lines changed: 61 additions & 35 deletions

File tree

docs/libraries/fontlibc.rst

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,13 @@ This file will get generated by your makefile, which will need to be modified to
163163
$(SRCDIR)/myfont2.inc: $(SRCDIR)/myfont2.fnt
164164
convfont -o carray -f $(SRCDIR)/myfont2.fnt $(SRCDIR)/myfont2.inc
165165
166-
Finally, somewhere else in your program, you can use :code:`fontlib_SetFont`:
166+
Finally, somewhere else in your program, you can use :code:`fontlib_LoadFont`:
167167

168168
.. code-block:: c
169169
170170
void main() {
171171
. . .
172-
fontlib_SetFont(my_font_1, 0);
172+
fontlib_LoadFont(my_font_1, 0);
173173
. . .
174174
}
175175
@@ -211,7 +211,7 @@ Create a new folder, place your :code:`.fnt` files in it, and then create a :cod
211211
Using Font Packs
212212
----------------
213213

214-
While using an embedded font is easy—just call :code:`fontlib_SetFont` directly on the pointer to the font data—,
214+
While using an embedded font is easy—just call :code:`fontlib_LoadFont` directly on the pointer to the font data—,
215215
using a font pack is a bit more involved.
216216

217217
**WARNING: FontLibC caches a pointer to the font's data when you use** :code:`SetFont`.
@@ -243,7 +243,7 @@ If you require a specific font pack with a specific appvar name, then opening a
243243
return;
244244
}
245245
/* Use font for whatever */
246-
fontlib_SetFont(my_font, 0);
246+
fontlib_LoadFont(my_font, 0);
247247
. . .
248248
}
249249
@@ -299,7 +299,7 @@ for a font pack by typeface name:
299299
Note that a direct pointer to the name is returned, which may be archived,
300300
so you cannot write to the string. */
301301
if (!strcmp(typeface_name, "My Font")) {
302-
fontlib_SetFont(fontlib_GetFontByIndex(var_name, 0), 0);
302+
fontlib_LoadFont(fontlib_GetFontByIndex(var_name, 0), 0);
303303
break;
304304
}
305305
}
@@ -346,7 +346,7 @@ The :code:`GetFontByStyle` routines help you automatically select a font given a
346346
/* Get a 9 or 10 pixel tall bold, serif font that isn't monospaced and isn't italic. */
347347
font = fontlib_GetFontByStyleRaw(font_pack, 9, 10, FONTLIB_BOLD, FONTLIB_BOLD, FONTLIB_SERIF, FONTLIB_MONOSPACED | FONTLIB_ITALIC);
348348
if (font)
349-
fontlib_SetFont(font, 0);
349+
fontlib_LoadFont(font, 0);
350350
351351
API Usage Notes
352352
---------------
@@ -404,9 +404,9 @@ on normal wrapping with :code:`fontlib_SetNewlineOptions`.
404404

405405
Additional blank vertical space around text can improve readability in large blocks of text.
406406
:code:`fontlib_SetLineSpacing` allows you to set this behavior.
407-
Fonts may specify default additional spacing that is automatically applied when calling :code:`fontlib_SetFont`.
407+
Fonts may specify default additional spacing that is automatically applied when calling :code:`fontlib_LoadFont`.
408408
In GUIs and games where the benefits of legibility are outweighed by more aggressive use of vertical space,
409-
you can force the default spacing to zero after using :code:`fontlib_SetFont` with :code:`fontlib_SetLineSpacing`.
409+
you can force the default spacing to zero after using :code:`fontlib_LoadFont` with :code:`fontlib_SetLineSpacing`.
410410

411411
API Documentation
412412
-----------------

examples/library_examples/fontlibc/example/src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ int main(void)
1717
gfx_ZeroScreen();
1818

1919
/* Set a font to use. DrawString will display garbage if you don't give it a font! */
20-
fontlib_SetFont(test_font, 0);
20+
fontlib_LoadFont(test_font, 0);
2121

2222
/* First, we'll display centered text in a window */
2323
/* Add some vertical padding around our text */

examples/library_examples/fontlibc/font_pack/src/main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ int main(void)
2626
/* Set a font to use. */
2727
font_pack = fontlib_GetFontByIndex(font_pack_name, 1);
2828
if (font_pack) {
29-
fontlib_SetFont(font_pack, 0);
29+
fontlib_LoadFont(font_pack, 0);
3030
fontlib_DrawString(test_str);
3131
}
3232
else
@@ -38,7 +38,7 @@ int main(void)
3838
font_pack = fontlib_GetFontByStyle(font_pack_name, 9, 10, FONTLIB_NORMAL, FONTLIB_NORMAL, 0, FONTLIB_SERIF);
3939
if (font_pack)
4040
{
41-
fontlib_SetFont(font_pack, 0);
41+
fontlib_LoadFont(font_pack, 0);
4242
fontlib_DrawString(test_str);
4343
}
4444
else
@@ -50,7 +50,7 @@ int main(void)
5050
font_pack = fontlib_GetFontByStyle(font_pack_name, 30, 50, FONTLIB_NORMAL, FONTLIB_NORMAL, FONTLIB_SERIF, 0);
5151
if (font_pack)
5252
{
53-
fontlib_SetFont(font_pack, 0);
53+
fontlib_LoadFont(font_pack, 0);
5454
fontlib_DrawString(test_str);
5555
}
5656
else

src/fontlibc/fontlibc.asm

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ include '../include/library.inc'
33
include '../include/include_library.inc'
44
;-------------------------------------------------------------------------------
55

6-
library FONTLIBC,2
6+
library FONTLIBC,3
77

88
;-------------------------------------------------------------------------------
99
; Dependencies
@@ -78,7 +78,11 @@ include_library '../graphx/graphx.asm'
7878
export fontlib_ScrollWindowUp
7979
export fontlib_Home
8080
export fontlib_HomeUp
81-
81+
;-------------------------------------------------------------------------------
82+
; v3 functions
83+
;-------------------------------------------------------------------------------
84+
; replacement for fontlib_SetFont
85+
export fontlib_LoadFont
8286

8387
;-------------------------------------------------------------------------------
8488
CurrentBuffer := ti.mpLcdLpbase
@@ -140,6 +144,11 @@ virtual at 0
140144
strucFontPackHeader strucFontPackHeader
141145
end virtual
142146

147+
;-------------------------------------------------------------------------------
148+
; fontlib_load_options_t
149+
150+
bLoadOption_IgnoreLineSpacing := 0
151+
mLoadOption_IgnoreLineSpacing := 1 shl bLoadOption_IgnoreLineSpacing
143152

144153
;-------------------------------------------------------------------------------
145154
macro mIsHLLessThanDE?
@@ -405,6 +414,17 @@ fontlib_Home:
405414

406415
;-------------------------------------------------------------------------------
407416
fontlib_SetFont:
417+
; performs fontlib_LoadFont(font_data, FONTLIB_IGNORE_LINE_SPACING)
418+
ld hl,arg1
419+
add hl,sp
420+
ld bc,mLoadOption_IgnoreLineSpacing
421+
ld (hl),bc
422+
423+
; Fall through to fontlib_LoadFont
424+
assert $ = fontlib_LoadFont
425+
426+
;-------------------------------------------------------------------------------
427+
fontlib_LoadFont:
408428
; Sets the current font to the data at the pointer given
409429
; Arguments:
410430
; arg0: Pointer to font
@@ -435,12 +455,12 @@ fontlib_SetFont:
435455
or a,a
436456
ret z ; Also unreasonable: a zero-height font
437457
and a,$80
438-
jr nz,.false
458+
jr nz,.failure
439459
ld a,63
440460
cp a,(iy + strucFont.spaceAbove)
441-
jr c,.false
461+
jr c,.failure
442462
cp a,(iy + strucFont.spaceBelow)
443-
jr c,.false
463+
jr c,.failure
444464
.validateOffsets:
445465
; Now convert offsets into actual pointers
446466
; Validate that offset is at least semi-reasonable
@@ -459,21 +479,20 @@ fontlib_SetFont:
459479
add hl,bc
460480
ld (iy + strucFont.bitmapsTablePtr),hl
461481
; Check for the ignore line spacing flag
462-
; Due to a bug, flags must be ignored, and treated as FONTLIB_IGNORE_LINE_SPACING
463-
; ld hl,arg1
464-
; add hl,sp
465-
; ld a,(hl)
466-
; or a,a
467-
; jr z,.true
482+
ld hl,arg1
483+
add hl,sp
484+
bit bLoadOption_IgnoreLineSpacing,(hl)
485+
jr z,.finish_load
468486
lea hl,iy + strucFont.spaceAbove
469487
xor a,a
470488
ld (hl),a
471489
inc hl
472490
ld (hl),a
473-
.true:
491+
.finish_load:
474492
ld a,1
475493
ret
476-
.false:
494+
495+
.failure:
477496
xor a,a
478497
ret
479498

src/fontlibc/fontlibc.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ typedef enum {
4949
} fontlib_newline_options_t;
5050

5151
/**
52-
* @warning Flags are currently ignored due to a bug, and treated as FONTLIB_IGNORE_LINE_SPACING
5352
* Options for controlling how SetFont functions.
54-
* @see fontlib_SetFont
53+
* @see fontlib_LoadFont
5554
*/
5655
typedef enum {
5756
/**
@@ -162,7 +161,7 @@ typedef struct fontlib_metadata_t {
162161
* unsigned char baseline;
163162
* fontlib_font_t *my_font = fontlib_GetFontByStyle("FONTPACK", 12, 12,
164163
* FONTLIB_NORMAL, FONTLIB_NORMAL, FONTLIB_SERIF, 0);
165-
* if (!my_font || !fontlib_SetFont(my_font))
164+
* if (!my_font || !fontlib_LoadFont(my_font, 0))
166165
* return;
167166
* baseline = my_font->baseline_height;
168167
* @endcode
@@ -382,16 +381,24 @@ void fontlib_HomeUp();
382381
*/
383382
void fontlib_Home();
384383

384+
/**
385+
* @warning fontlib_SetFont is deprecated, use fontlib_LoadFont instead.
386+
* @note due to a bug, fontlib_SetFont ignores flags and assumes
387+
* FONTLIB_IGNORE_LINE_SPACING. This has been fixed with fontlib_LoadFont
388+
*/
389+
bool fontlib_SetFont(const fontlib_font_t *font_data, fontlib_load_options_t flags);
390+
391+
#define fontlib_SetFont _Pragma("GCC warning \"'fontlib_SetFont' is deprecated, use 'fontlib_LoadFont' instead\"") fontlib_SetFont
392+
385393
/**
386394
* Sets the current font
387395
* @param[in] font_data Pointer to font data
388-
* @param[in] Unused and treated as FONTLIB_IGNORE_LINE_SPACING
396+
* @param[in] flags Information about how to process the font
389397
* @return Returns false if the font seems invalid for any reason
390398
* @warning If false is returned, no valid font is currently loaded and trying
391-
* @note Flags are currently ignored due to a bug, and treated as FONTLIB_IGNORE_LINE_SPACING
392399
* to print will print garbage!
393400
*/
394-
bool fontlib_SetFont(const fontlib_font_t *font_data, fontlib_load_options_t flags);
401+
bool fontlib_LoadFont(const fontlib_font_t *font_data, fontlib_load_options_t flags);
395402

396403
/**
397404
* Sets the current foreground color FontLibC will use for drawing.

test/fontlibc/glyph_width/src/main.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
int run_tests(void) {
1616

1717
/* 256 glyph test */
18-
fontlib_SetFont(test_font, 0);
18+
fontlib_LoadFont(test_font, 0);
1919
C(fontlib_GetFirstGlyph() == 0);
2020
C(fontlib_GetTotalGlyphs() == 256);
2121
C(fontlib_GetTotalGlyphs() == 256);
@@ -27,7 +27,7 @@ int run_tests(void) {
2727
/* 255 glyph test */
2828
test_font->first_glyph = 1;
2929
test_font->total_glyphs = 255;
30-
fontlib_SetFont(test_font, 0);
30+
fontlib_LoadFont(test_font, 0);
3131
C(fontlib_GetFirstGlyph() == 1);
3232
C(fontlib_GetTotalGlyphs() == 255);
3333
C(fontlib_GetGlyphWidth(0) == 0);
@@ -39,7 +39,7 @@ int run_tests(void) {
3939
/* 128 glyph test */
4040
test_font->first_glyph = 0;
4141
test_font->total_glyphs = 128;
42-
fontlib_SetFont(test_font, 0);
42+
fontlib_LoadFont(test_font, 0);
4343
C(fontlib_GetFirstGlyph() == 0);
4444
C(fontlib_GetTotalGlyphs() == 128);
4545
for (unsigned i = 0; i <= 127; i++) {
@@ -54,7 +54,7 @@ int run_tests(void) {
5454
/* 64 glyph test */
5555
test_font->first_glyph = 32;
5656
test_font->total_glyphs = 64;
57-
fontlib_SetFont(test_font, 0);
57+
fontlib_LoadFont(test_font, 0);
5858
C(fontlib_GetFirstGlyph() == 32);
5959
C(fontlib_GetTotalGlyphs() == 64);
6060
for (unsigned i = 0; i <= 31; i++) {

0 commit comments

Comments
 (0)