1 /**
2  * @file test_font_loader.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 
10 #if LV_BUILD_TEST
11 #include "../../lvgl.h"
12 
13 #include "unity/unity.h"
14 
15 /*********************
16  *      DEFINES
17  *********************/
18 
19 /**********************
20  *      TYPEDEFS
21  **********************/
22 
23 /**********************
24  *  STATIC PROTOTYPES
25  **********************/
26 
27 static int compare_fonts(lv_font_t * f1, lv_font_t * f2);
28 void test_font_loader(void);
29 
30 /**********************
31  *  STATIC VARIABLES
32  **********************/
33 
34 /**********************
35  *      MACROS
36  **********************/
37 
38 /**********************
39  *   GLOBAL FUNCTIONS
40  **********************/
41 
42 extern lv_font_t font_1;
43 extern lv_font_t font_2;
44 extern lv_font_t font_3;
45 
test_font_loader(void)46 void test_font_loader(void)
47 {
48     /*Test with cahce ('A' has cache)*/
49     lv_font_t * font_1_bin = lv_font_load("A:src/test_fonts/font_1.fnt");
50     lv_font_t * font_2_bin = lv_font_load("A:src/test_fonts/font_2.fnt");
51     lv_font_t * font_3_bin = lv_font_load("A:src/test_fonts/font_3.fnt");
52 
53     compare_fonts(&font_1, font_1_bin);
54     compare_fonts(&font_2, font_2_bin);
55     compare_fonts(&font_3, font_3_bin);
56 
57     lv_font_free(font_1_bin);
58     lv_font_free(font_2_bin);
59     lv_font_free(font_3_bin);
60 
61     /*Test with cahce ('B' has NO cache)*/
62     font_1_bin = lv_font_load("B:src/test_fonts/font_1.fnt");
63     font_2_bin = lv_font_load("B:src/test_fonts/font_2.fnt");
64     font_3_bin = lv_font_load("B:src/test_fonts/font_3.fnt");
65 
66     compare_fonts(&font_1, font_1_bin);
67     compare_fonts(&font_2, font_2_bin);
68     compare_fonts(&font_3, font_3_bin);
69 
70     lv_font_free(font_1_bin);
71     lv_font_free(font_2_bin);
72     lv_font_free(font_3_bin);
73 }
74 
compare_fonts(lv_font_t * f1,lv_font_t * f2)75 static int compare_fonts(lv_font_t * f1, lv_font_t * f2)
76 {
77     TEST_ASSERT_NOT_NULL_MESSAGE(f1, "font not null");
78     TEST_ASSERT_NOT_NULL_MESSAGE(f2, "font not null");
79 
80     //    Skip these test because -Wpedantic tells
81     //    ISO C forbids passing argument 1 of ‘TEST_ASSERT_EQUAL_PTR_MESSAGE’ between function pointer and ‘void *’
82     //    TEST_ASSERT_EQUAL_PTR_MESSAGE(f1->get_glyph_dsc, f2->get_glyph_dsc, "glyph_dsc");
83     //    TEST_ASSERT_EQUAL_PTR_MESSAGE(f1->get_glyph_bitmap, f2->get_glyph_bitmap, "glyph_bitmap");
84 
85     TEST_ASSERT_EQUAL_INT_MESSAGE(f1->line_height, f2->line_height, "line_height");
86     TEST_ASSERT_EQUAL_INT_MESSAGE(f1->base_line, f2->base_line, "base_line");
87     TEST_ASSERT_EQUAL_INT_MESSAGE(f1->subpx, f2->subpx, "subpx");
88     lv_font_fmt_txt_dsc_t * dsc1 = (lv_font_fmt_txt_dsc_t *)f1->dsc;
89     lv_font_fmt_txt_dsc_t * dsc2 = (lv_font_fmt_txt_dsc_t *)f2->dsc;
90 
91     TEST_ASSERT_EQUAL_INT_MESSAGE(dsc1->kern_scale, dsc2->kern_scale, "kern_scale");
92     TEST_ASSERT_EQUAL_INT_MESSAGE(dsc1->cmap_num, dsc2->cmap_num, "cmap_num");
93     TEST_ASSERT_EQUAL_INT_MESSAGE(dsc1->bpp, dsc2->bpp, "bpp");
94     TEST_ASSERT_EQUAL_INT_MESSAGE(dsc1->kern_classes, dsc2->kern_classes, "kern_classes");
95     TEST_ASSERT_EQUAL_INT_MESSAGE(dsc1->bitmap_format, dsc2->bitmap_format, "bitmap_format");
96 
97     // cmaps
98     int total_glyphs = 0;
99     for(int i = 0; i < dsc1->cmap_num; ++i) {
100         lv_font_fmt_txt_cmap_t * cmaps1 = (lv_font_fmt_txt_cmap_t *)&dsc1->cmaps[i];
101         lv_font_fmt_txt_cmap_t * cmaps2 = (lv_font_fmt_txt_cmap_t *)&dsc2->cmaps[i];
102 
103         TEST_ASSERT_EQUAL_INT_MESSAGE(cmaps1->range_start, cmaps2->range_start, "range_start");
104         TEST_ASSERT_EQUAL_INT_MESSAGE(cmaps1->range_length, cmaps2->range_length, "range_length");
105         TEST_ASSERT_EQUAL_INT_MESSAGE(cmaps1->glyph_id_start, cmaps2->glyph_id_start, "glyph_id_start");
106         TEST_ASSERT_EQUAL_INT_MESSAGE(cmaps1->type, cmaps2->type, "type");
107         TEST_ASSERT_EQUAL_INT_MESSAGE(cmaps1->list_length, cmaps2->list_length, "list_length");
108 
109         if(cmaps1->unicode_list != NULL && cmaps2->unicode_list != NULL) {
110             TEST_ASSERT_TRUE_MESSAGE(cmaps1->unicode_list && cmaps2->unicode_list, "unicode_list");
111 
112             TEST_ASSERT_EQUAL_UINT8_ARRAY_MESSAGE(
113                 (uint8_t *)cmaps1->unicode_list,
114                 (uint8_t *)cmaps2->unicode_list,
115                 sizeof(uint16_t) * cmaps1->list_length,
116                 "unicode_list");
117             total_glyphs += cmaps1->list_length;
118         }
119         else {
120             total_glyphs += cmaps1->range_length;
121             TEST_ASSERT_EQUAL_PTR_MESSAGE(cmaps1->unicode_list, cmaps2->unicode_list, "unicode_list");
122         }
123 
124         if(cmaps1->glyph_id_ofs_list != NULL && cmaps2->glyph_id_ofs_list != NULL) {
125             uint8_t * ids1 = (uint8_t *)cmaps1->glyph_id_ofs_list;
126             uint8_t * ids2 = (uint8_t *)cmaps2->glyph_id_ofs_list;
127 
128             TEST_ASSERT_EQUAL_UINT8_ARRAY_MESSAGE(ids1, ids2, cmaps1->list_length, "glyph_id_ofs_list");
129         }
130         else {
131             TEST_ASSERT_EQUAL_PTR_MESSAGE(cmaps1->glyph_id_ofs_list, cmaps2->glyph_id_ofs_list, "glyph_id_ofs_list");
132         }
133     }
134 
135     // kern_dsc
136     if(dsc1->kern_classes == 1 && dsc2->kern_classes == 1) {
137         lv_font_fmt_txt_kern_classes_t * kern1 = (lv_font_fmt_txt_kern_classes_t *)dsc1->kern_dsc;
138         lv_font_fmt_txt_kern_classes_t * kern2 = (lv_font_fmt_txt_kern_classes_t *)dsc2->kern_dsc;
139         if(kern1 != NULL && kern2 != NULL) {
140             TEST_ASSERT_EQUAL_INT_MESSAGE(kern1->right_class_cnt, kern2->right_class_cnt, "right_class_cnt");
141             TEST_ASSERT_EQUAL_INT_MESSAGE(kern1->left_class_cnt, kern2->left_class_cnt, "left_class_cnt");
142 
143             TEST_ASSERT_EQUAL_UINT8_ARRAY_MESSAGE(
144                 (uint8_t *)kern1->left_class_mapping,
145                 (uint8_t *)kern2->left_class_mapping,
146                 kern1->left_class_cnt,
147                 "left_class_mapping");
148 
149             TEST_ASSERT_EQUAL_UINT8_ARRAY_MESSAGE(
150                 (uint8_t *)kern1->right_class_mapping,
151                 (uint8_t *)kern2->right_class_mapping,
152                 kern1->right_class_cnt,
153                 "right_class_mapping");
154 
155             TEST_ASSERT_EQUAL_UINT8_ARRAY_MESSAGE(
156                 (uint8_t *)kern1->class_pair_values,
157                 (uint8_t *)kern2->class_pair_values,
158                 kern1->right_class_cnt * kern1->left_class_cnt,
159                 "class_pair_values");
160         }
161         else {
162             TEST_ASSERT_EQUAL_PTR_MESSAGE(kern1, kern2, "kern");
163         }
164     }
165     else if(dsc1->kern_classes == 0 && dsc2->kern_classes == 0) {
166         lv_font_fmt_txt_kern_pair_t * kern1 = (lv_font_fmt_txt_kern_pair_t *)dsc1->kern_dsc;
167         lv_font_fmt_txt_kern_pair_t * kern2 = (lv_font_fmt_txt_kern_pair_t *)dsc2->kern_dsc;
168         if(kern1 != NULL && kern2 != NULL) {
169             TEST_ASSERT_EQUAL_INT_MESSAGE(kern1->glyph_ids_size, kern2->glyph_ids_size, "glyph_ids_size");
170             TEST_ASSERT_EQUAL_INT_MESSAGE(kern1->pair_cnt, kern2->pair_cnt, "pair_cnt");
171 
172             int ids_size;
173 
174             if(kern1->glyph_ids_size == 0) {
175                 ids_size = sizeof(int8_t) * 2 * kern1->pair_cnt;
176             }
177             else {
178                 ids_size = sizeof(int16_t) * 2 * kern1->pair_cnt;
179             }
180 
181             TEST_ASSERT_EQUAL_UINT8_ARRAY_MESSAGE(kern1->glyph_ids, kern2->glyph_ids, ids_size, "glyph_ids");
182             TEST_ASSERT_EQUAL_UINT8_ARRAY_MESSAGE(
183                 (uint8_t *) kern1->values,
184                 (uint8_t *) kern2->values,
185                 kern1->pair_cnt,
186                 "glyph_values");
187         }
188     }
189 
190     lv_font_fmt_txt_glyph_dsc_t * glyph_dsc1 = (lv_font_fmt_txt_glyph_dsc_t *)dsc1->glyph_dsc;
191     lv_font_fmt_txt_glyph_dsc_t * glyph_dsc2 = (lv_font_fmt_txt_glyph_dsc_t *)dsc2->glyph_dsc;
192 
193     for(int i = 0; i < total_glyphs; ++i) {
194         if(i < total_glyphs - 1) {
195             int size1 = glyph_dsc1[i + 1].bitmap_index - glyph_dsc1[i].bitmap_index;
196 
197             if(size1 > 0) {
198                 TEST_ASSERT_EQUAL_UINT8_ARRAY_MESSAGE(
199                     dsc1->glyph_bitmap + glyph_dsc1[i].bitmap_index,
200                     dsc2->glyph_bitmap + glyph_dsc2[i].bitmap_index,
201                     size1 - 1, "glyph_bitmap");
202             }
203         }
204         TEST_ASSERT_EQUAL_INT_MESSAGE(glyph_dsc1[i].adv_w, glyph_dsc2[i].adv_w, "adv_w");
205         TEST_ASSERT_EQUAL_INT_MESSAGE(glyph_dsc1[i].box_w, glyph_dsc2[i].box_w, "box_w");
206         TEST_ASSERT_EQUAL_INT_MESSAGE(glyph_dsc1[i].box_h, glyph_dsc2[i].box_h, "box_h");
207         TEST_ASSERT_EQUAL_INT_MESSAGE(glyph_dsc1[i].ofs_x, glyph_dsc2[i].ofs_x, "ofs_x");
208         TEST_ASSERT_EQUAL_INT_MESSAGE(glyph_dsc1[i].ofs_y, glyph_dsc2[i].ofs_y, "ofs_y");
209     }
210 
211     LV_LOG_INFO("No differences found!");
212     return 0;
213 }
214 
215 /**********************
216  *   STATIC FUNCTIONS
217  **********************/
218 
219 #endif // LV_BUILD_TEST
220 
221