1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger                                  |
| All rights reserved.                                                       |
|                                                                            |
| Redistribution and use in source and binary forms, with or without         |
| modification, are permitted provided that the following conditions are met:|
|                                                                            |
| 1. Redistributions of source code must retain the above copyright notice,  |
|    this list of conditions and the following disclaimer.                   |
| 2. Redistributions in binary form must reproduce the above copyright       |
|    notice, this list of conditions and the following disclaimer in the     |
|    documentation and/or other materials provided with the distribution.    |
|                                                                            |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE    |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR        |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF       |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS   |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN    |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)    |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF     |
| THE POSSIBILITY OF SUCH DAMAGE.                                            |
+----------------------------------------------------------------------------+
*/

#include "gate/graphics/fonts.h"
#include "gate/results.h"
#include "gate/graphics/font_types.h"
#include "gate/graphics/font_8x8.h"
#include "gate/graphics/font_8x16.h"


typedef struct gate_font_char_mapping
{
    char character;<--- struct member 'gate_font_char_mapping::character' is never used.
    gate_font_char_row8_bits_t const* bits;<--- struct member 'gate_font_char_mapping::bits' is never used.
} gate_font_char_mapping_t;

typedef struct gate_font_descriptor_class
{
    gate_uint8_t	char_width;
    gate_uint8_t	char_height;
    gate_font_char_row8_bits_t const* const* char_table;
} gate_font_descriptor_t;


static gate_font_descriptor_t const gate_font8_descriptor = { 6, 8, font8x8_map };

static gate_font_descriptor_t const gate_font16_descriptor = { 8, 16, font8x16_map };


static gate_result_t gate_font_print_char_generic(gate_font_descriptor_t const* descriptor, gate_rasterimage_t* image,
    char character, int x, int y, gate_color_t color)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    gate_font_char_row8_bits_t const* bits;
    int row, col;
    gate_uint8_t pixel_bits;
    int cur_row;
    int image_width = (int)gate_rasterimage_width(image);
    int image_height = (int)gate_rasterimage_height(image);

    do
    {
        if ((y < 0) || (x < 0))
        {
            ret = GATE_RESULT_OUTOFBOUNDS;
            break;
        }
        if ((y + descriptor->char_height >= image_height)
            || (x + descriptor->char_width >= image_width))
        {
            ret = GATE_RESULT_OUTOFBOUNDS;
            break;
        }


        bits = descriptor->char_table[(gate_uint8_t)character];
        if (bits == NULL)
        {
            ret = GATE_RESULT_NOMATCH;
            break;
        }

        for (row = 0; row != descriptor->char_height; ++row)
        {
            pixel_bits = *((gate_uint8_t const*)&bits[row]);
            cur_row = y + row;

            for (col = 0; col != descriptor->char_width; ++col)
            {
                if (pixel_bits & (1 << col))
                {
                    gate_rasterimage_set_pixel(image, x + col, cur_row, &color);
                }
            }
        }
        ret = GATE_RESULT_OK;
    } while (0);
    return ret;
}

static gate_result_t gate_font_print_text_generic(gate_font_descriptor_t const* descriptor, gate_rasterimage_t* image,
    gate_string_t const* text, int x, int y, gate_color_t color)
{
    char const* ptr = gate_string_ptr(text, 0);
    gate_size_t len = gate_string_length(text);

    while (len-- != 0)
    {
        gate_font_print_char_generic(descriptor, image, *ptr, x, y, color);
        x += descriptor->char_width;
        ++ptr;
    }
    return GATE_RESULT_OK;
}

gate_result_t gate_font8_print_char(gate_rasterimage_t* image, char character, int x, int y, gate_color_t color)
{
    return gate_font_print_char_generic(&gate_font8_descriptor, image, character, x, y, color);
}
gate_result_t gate_font8_print_text(gate_rasterimage_t* image, gate_string_t const* text, int x, int y, gate_color_t color)
{
    return gate_font_print_text_generic(&gate_font8_descriptor, image, text, x, y, color);
}

gate_result_t gate_font16_print_char(gate_rasterimage_t* image, char character, int x, int y, gate_color_t color)
{
    return gate_font_print_char_generic(&gate_font16_descriptor, image, character, x, y, color);
}
gate_result_t gate_font16_print_text(gate_rasterimage_t* image, gate_string_t const* text, int x, int y, gate_color_t color)
{
    return gate_font_print_text_generic(&gate_font16_descriptor, image, text, x, y, color);
}