Line | Branch | Exec | Source |
---|---|---|---|
1 | /* GATE PROJECT LICENSE: | ||
2 | +----------------------------------------------------------------------------+ | ||
3 | | Copyright(c) 2018-2025, Stefan Meislinger | | ||
4 | | All rights reserved. | | ||
5 | | | | ||
6 | | Redistribution and use in source and binary forms, with or without | | ||
7 | | modification, are permitted provided that the following conditions are met:| | ||
8 | | | | ||
9 | | 1. Redistributions of source code must retain the above copyright notice, | | ||
10 | | this list of conditions and the following disclaimer. | | ||
11 | | 2. Redistributions in binary form must reproduce the above copyright | | ||
12 | | notice, this list of conditions and the following disclaimer in the | | ||
13 | | documentation and/or other materials provided with the distribution. | | ||
14 | | | | ||
15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| | ||
16 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | ||
17 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | ||
18 | | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | | ||
19 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | ||
20 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | ||
21 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | ||
22 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | ||
23 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | ||
24 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | | ||
25 | | THE POSSIBILITY OF SUCH DAMAGE. | | ||
26 | +----------------------------------------------------------------------------+ | ||
27 | */ | ||
28 | |||
29 | #include "gate/graphics/fonts.h" | ||
30 | #include "gate/results.h" | ||
31 | #include "gate/graphics/font_types.h" | ||
32 | #include "gate/graphics/font_8x8.h" | ||
33 | #include "gate/graphics/font_8x16.h" | ||
34 | |||
35 | |||
36 | typedef struct gate_font_char_mapping | ||
37 | { | ||
38 | char character; | ||
39 | gate_font_char_row8_bits_t const* bits; | ||
40 | } gate_font_char_mapping_t; | ||
41 | |||
42 | typedef struct gate_font_descriptor_class | ||
43 | { | ||
44 | gate_uint8_t char_width; | ||
45 | gate_uint8_t char_height; | ||
46 | gate_font_char_row8_bits_t const* const* char_table; | ||
47 | } gate_font_descriptor_t; | ||
48 | |||
49 | |||
50 | static gate_font_descriptor_t const gate_font8_descriptor = { 6, 8, font8x8_map }; | ||
51 | |||
52 | static gate_font_descriptor_t const gate_font16_descriptor = { 8, 16, font8x16_map }; | ||
53 | |||
54 | |||
55 | 13 | static gate_result_t gate_font_print_char_generic(gate_font_descriptor_t const* descriptor, gate_rasterimage_t* image, | |
56 | char character, int x, int y, gate_color_t color) | ||
57 | { | ||
58 | 13 | gate_result_t ret = GATE_RESULT_FAILED; | |
59 | gate_font_char_row8_bits_t const* bits; | ||
60 | int row, col; | ||
61 | gate_uint8_t pixel_bits; | ||
62 | int cur_row; | ||
63 | 13 | int image_width = (int)gate_rasterimage_width(image); | |
64 | 13 | int image_height = (int)gate_rasterimage_height(image); | |
65 | |||
66 | do | ||
67 | { | ||
68 |
2/4✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
|
13 | if ((y < 0) || (x < 0)) |
69 | { | ||
70 | ✗ | ret = GATE_RESULT_OUTOFBOUNDS; | |
71 | ✗ | break; | |
72 | } | ||
73 |
1/2✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
|
13 | if ((y + descriptor->char_height >= image_height) |
74 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
|
13 | || (x + descriptor->char_width >= image_width)) |
75 | { | ||
76 | ✗ | ret = GATE_RESULT_OUTOFBOUNDS; | |
77 | ✗ | break; | |
78 | } | ||
79 | |||
80 | |||
81 | 13 | bits = descriptor->char_table[(gate_uint8_t)character]; | |
82 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
|
13 | if (bits == NULL) |
83 | { | ||
84 | ✗ | ret = GATE_RESULT_NOMATCH; | |
85 | ✗ | break; | |
86 | } | ||
87 | |||
88 |
2/2✓ Branch 0 taken 104 times.
✓ Branch 1 taken 13 times.
|
117 | for (row = 0; row != descriptor->char_height; ++row) |
89 | { | ||
90 | 104 | pixel_bits = *((gate_uint8_t const*)&bits[row]); | |
91 | 104 | cur_row = y + row; | |
92 | |||
93 |
2/2✓ Branch 0 taken 624 times.
✓ Branch 1 taken 104 times.
|
728 | for (col = 0; col != descriptor->char_width; ++col) |
94 | { | ||
95 |
2/2✓ Branch 0 taken 173 times.
✓ Branch 1 taken 451 times.
|
624 | if (pixel_bits & (1 << col)) |
96 | { | ||
97 | 173 | gate_rasterimage_set_pixel(image, x + col, cur_row, &color); | |
98 | } | ||
99 | } | ||
100 | } | ||
101 | 13 | ret = GATE_RESULT_OK; | |
102 | } while (0); | ||
103 | 13 | return ret; | |
104 | } | ||
105 | |||
106 | ✗ | static gate_result_t gate_font_print_text_generic(gate_font_descriptor_t const* descriptor, gate_rasterimage_t* image, | |
107 | gate_string_t const* text, int x, int y, gate_color_t color) | ||
108 | { | ||
109 | ✗ | char const* ptr = gate_string_ptr(text, 0); | |
110 | ✗ | gate_size_t len = gate_string_length(text); | |
111 | |||
112 | ✗ | while (len-- != 0) | |
113 | { | ||
114 | ✗ | gate_font_print_char_generic(descriptor, image, *ptr, x, y, color); | |
115 | ✗ | x += descriptor->char_width; | |
116 | ✗ | ++ptr; | |
117 | } | ||
118 | ✗ | return GATE_RESULT_OK; | |
119 | } | ||
120 | |||
121 | 13 | gate_result_t gate_font8_print_char(gate_rasterimage_t* image, char character, int x, int y, gate_color_t color) | |
122 | { | ||
123 | 13 | return gate_font_print_char_generic(&gate_font8_descriptor, image, character, x, y, color); | |
124 | } | ||
125 | ✗ | gate_result_t gate_font8_print_text(gate_rasterimage_t* image, gate_string_t const* text, int x, int y, gate_color_t color) | |
126 | { | ||
127 | ✗ | return gate_font_print_text_generic(&gate_font8_descriptor, image, text, x, y, color); | |
128 | } | ||
129 | |||
130 | ✗ | gate_result_t gate_font16_print_char(gate_rasterimage_t* image, char character, int x, int y, gate_color_t color) | |
131 | { | ||
132 | ✗ | return gate_font_print_char_generic(&gate_font16_descriptor, image, character, x, y, color); | |
133 | } | ||
134 | ✗ | gate_result_t gate_font16_print_text(gate_rasterimage_t* image, gate_string_t const* text, int x, int y, gate_color_t color) | |
135 | { | ||
136 | ✗ | return gate_font_print_text_generic(&gate_font16_descriptor, image, text, x, y, color); | |
137 | } | ||
138 |