GCC Code Coverage Report


Directory: src/gate/
File: src/gate/graphics/fonts.c
Date: 2025-12-12 23:40:09
Exec Total Coverage
Lines: 19 39 48.7%
Functions: 2 6 33.3%
Branches: 11 18 61.1%

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 typedef struct gate_font_descriptor_class
36 {
37 gate_uint8_t char_width;
38 gate_uint8_t char_height;
39 gate_font_char_row8_bits_t const* const* char_table;
40 } gate_font_descriptor_t;
41
42 static gate_font_descriptor_t const gate_font8_descriptor = { 6, 8, font8x8_map };
43
44 static gate_font_descriptor_t const gate_font16_descriptor = { 8, 16, font8x16_map };
45
46 13 static gate_result_t gate_font_print_char_generic(gate_font_descriptor_t const* descriptor, gate_rasterimage_t* image,
47 char character, int x, int y, gate_color_t color)
48 {
49 13 gate_result_t ret = GATE_RESULT_FAILED;
50 gate_font_char_row8_bits_t const* bits;
51 int row, col;
52 gate_uint8_t pixel_bits;
53 int cur_row;
54 13 int image_width = (int)gate_rasterimage_width(image);
55 13 int image_height = (int)gate_rasterimage_height(image);
56
57 do
58 {
59
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))
60 {
61 ret = GATE_RESULT_OUTOFBOUNDS;
62 break;
63 }
64
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 if ((y + descriptor->char_height >= image_height)
65
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 || (x + descriptor->char_width >= image_width))
66 {
67 ret = GATE_RESULT_OUTOFBOUNDS;
68 break;
69 }
70
71
72 13 bits = descriptor->char_table[(gate_uint8_t)character];
73
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (bits == NULL)
74 {
75 ret = GATE_RESULT_NOMATCH;
76 break;
77 }
78
79
2/2
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 13 times.
117 for (row = 0; row != descriptor->char_height; ++row)
80 {
81 104 pixel_bits = *((gate_uint8_t const*)&bits[row]);
82 104 cur_row = y + row;
83
84
2/2
✓ Branch 0 taken 624 times.
✓ Branch 1 taken 104 times.
728 for (col = 0; col != descriptor->char_width; ++col)
85 {
86
2/2
✓ Branch 0 taken 173 times.
✓ Branch 1 taken 451 times.
624 if (pixel_bits & (1 << col))
87 {
88 173 gate_rasterimage_set_pixel(image, x + col, cur_row, &color);
89 }
90 }
91 }
92 13 ret = GATE_RESULT_OK;
93 } while (0);
94 13 return ret;
95 }
96
97 static gate_result_t gate_font_print_text_generic(gate_font_descriptor_t const* descriptor, gate_rasterimage_t* image,
98 gate_string_t const* text, int x, int y, gate_color_t color)
99 {
100 char const* ptr = gate_string_ptr(text, 0);
101 gate_size_t len = gate_string_length(text);
102
103 while (len-- != 0)
104 {
105 gate_font_print_char_generic(descriptor, image, *ptr, x, y, color);
106 x += descriptor->char_width;
107 ++ptr;
108 }
109 return GATE_RESULT_OK;
110 }
111
112 13 gate_result_t gate_font8_print_char(gate_rasterimage_t* image, char character, int x, int y, gate_color_t color)
113 {
114 13 return gate_font_print_char_generic(&gate_font8_descriptor, image, character, x, y, color);
115 }
116 gate_result_t gate_font8_print_text(gate_rasterimage_t* image, gate_string_t const* text, int x, int y, gate_color_t color)
117 {
118 return gate_font_print_text_generic(&gate_font8_descriptor, image, text, x, y, color);
119 }
120
121 gate_result_t gate_font16_print_char(gate_rasterimage_t* image, char character, int x, int y, gate_color_t color)
122 {
123 return gate_font_print_char_generic(&gate_font16_descriptor, image, character, x, y, color);
124 }
125 gate_result_t gate_font16_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_font16_descriptor, image, text, x, y, color);
128 }
129