paul@78 | 1 | /* |
paul@78 | 2 | * Common font-related functions. |
paul@78 | 3 | * |
paul@78 | 4 | * Copyright (C) 2018 Paul Boddie <paul@boddie.org.uk> |
paul@78 | 5 | * |
paul@78 | 6 | * This program is free software: you can redistribute it and/or modify |
paul@78 | 7 | * it under the terms of the GNU General Public License as published by |
paul@78 | 8 | * the Free Software Foundation, either version 3 of the License, or |
paul@78 | 9 | * (at your option) any later version. |
paul@78 | 10 | * |
paul@78 | 11 | * This program is distributed in the hope that it will be useful, |
paul@78 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@78 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@78 | 14 | * GNU General Public License for more details. |
paul@78 | 15 | * |
paul@78 | 16 | * You should have received a copy of the GNU General Public License |
paul@78 | 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@78 | 18 | */ |
paul@78 | 19 | |
paul@78 | 20 | #include <stdlib.h> |
paul@78 | 21 | |
paul@78 | 22 | #include "display.h" |
paul@78 | 23 | #include "font.h" |
paul@78 | 24 | |
paul@78 | 25 | |
paul@78 | 26 | |
paul@110 | 27 | /* Initialise a font configuration. */ |
paul@110 | 28 | |
paul@110 | 29 | void init_font(font_config_t *cfg, uint8_t *chars, uint32_t *table, |
paul@110 | 30 | uint32_t base, uint32_t limit) |
paul@110 | 31 | { |
paul@110 | 32 | cfg->chars = (char_definition_t *) chars; |
paul@110 | 33 | cfg->table = table; |
paul@110 | 34 | cfg->base = base; |
paul@110 | 35 | cfg->limit = limit; |
paul@110 | 36 | } |
paul@110 | 37 | |
paul@110 | 38 | |
paul@110 | 39 | |
paul@78 | 40 | /* Return the character definition for the given character. */ |
paul@78 | 41 | |
paul@78 | 42 | char_definition_t *get_char_definition(font_config_t *fcfg, char c) |
paul@78 | 43 | { |
paul@78 | 44 | /* Obtain the offset from the table and combine it with the character |
paul@78 | 45 | definitions base address. */ |
paul@78 | 46 | |
paul@78 | 47 | if ((c >= fcfg->base) && (c < fcfg->limit)) |
paul@78 | 48 | return (char_definition_t *) ((uint8_t *) fcfg->chars + fcfg->table[c - fcfg->base]); |
paul@78 | 49 | else |
paul@78 | 50 | return NULL; |
paul@78 | 51 | } |
paul@78 | 52 | |
paul@78 | 53 | /* Write a character to the screen, returning the next character position. */ |
paul@78 | 54 | |
paul@78 | 55 | int write_char(display_config_t *cfg, font_config_t *fcfg, char c, |
paul@78 | 56 | int x, int y, uint8_t colour) |
paul@78 | 57 | { |
paul@78 | 58 | int sy, dx, dy; |
paul@78 | 59 | char_definition_t *chardef = get_char_definition(fcfg, c); |
paul@78 | 60 | uint8_t *imagerow = chardef->data, |
paul@91 | 61 | *displayline = cfg->screen_start + y * cfg->line_length, |
paul@78 | 62 | bitmap, mask; |
paul@78 | 63 | |
paul@78 | 64 | /* Space is a special case. */ |
paul@78 | 65 | |
paul@78 | 66 | if (c == ' ') |
paul@78 | 67 | return x + 3; |
paul@78 | 68 | |
paul@78 | 69 | for (sy = 0, dy = y; (sy < chardef->height) && (dy < cfg->line_count); sy++, dy++) |
paul@78 | 70 | { |
paul@91 | 71 | if (displayline >= cfg->screen_limit) |
paul@91 | 72 | displayline -= cfg->screen_size; |
paul@91 | 73 | |
paul@78 | 74 | bitmap = *imagerow; |
paul@78 | 75 | |
paul@78 | 76 | for (dx = x, mask = 1 << (chardef->width - 1); (mask) && (dx < cfg->line_length); dx++, mask >>= 1) |
paul@78 | 77 | { |
paul@78 | 78 | if (bitmap & mask) |
paul@78 | 79 | displayline[get_position(cfg, dx)] = colour; |
paul@78 | 80 | } |
paul@78 | 81 | |
paul@78 | 82 | imagerow += 1; |
paul@78 | 83 | displayline += cfg->line_length; |
paul@78 | 84 | } |
paul@78 | 85 | |
paul@78 | 86 | return x + chardef->width; |
paul@78 | 87 | } |