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@78 | 27 | /* Return the character definition for the given character. */ |
paul@78 | 28 | |
paul@78 | 29 | char_definition_t *get_char_definition(font_config_t *fcfg, char c) |
paul@78 | 30 | { |
paul@78 | 31 | /* Obtain the offset from the table and combine it with the character |
paul@78 | 32 | definitions base address. */ |
paul@78 | 33 | |
paul@78 | 34 | if ((c >= fcfg->base) && (c < fcfg->limit)) |
paul@78 | 35 | return (char_definition_t *) ((uint8_t *) fcfg->chars + fcfg->table[c - fcfg->base]); |
paul@78 | 36 | else |
paul@78 | 37 | return NULL; |
paul@78 | 38 | } |
paul@78 | 39 | |
paul@78 | 40 | /* Write a character to the screen, returning the next character position. */ |
paul@78 | 41 | |
paul@78 | 42 | int write_char(display_config_t *cfg, font_config_t *fcfg, char c, |
paul@78 | 43 | int x, int y, uint8_t colour) |
paul@78 | 44 | { |
paul@78 | 45 | int sy, dx, dy; |
paul@78 | 46 | char_definition_t *chardef = get_char_definition(fcfg, c); |
paul@78 | 47 | uint8_t *imagerow = chardef->data, |
paul@91 | 48 | *displayline = cfg->screen_start + y * cfg->line_length, |
paul@78 | 49 | bitmap, mask; |
paul@78 | 50 | |
paul@78 | 51 | /* Space is a special case. */ |
paul@78 | 52 | |
paul@78 | 53 | if (c == ' ') |
paul@78 | 54 | return x + 3; |
paul@78 | 55 | |
paul@78 | 56 | for (sy = 0, dy = y; (sy < chardef->height) && (dy < cfg->line_count); sy++, dy++) |
paul@78 | 57 | { |
paul@91 | 58 | if (displayline >= cfg->screen_limit) |
paul@91 | 59 | displayline -= cfg->screen_size; |
paul@91 | 60 | |
paul@78 | 61 | bitmap = *imagerow; |
paul@78 | 62 | |
paul@78 | 63 | for (dx = x, mask = 1 << (chardef->width - 1); (mask) && (dx < cfg->line_length); dx++, mask >>= 1) |
paul@78 | 64 | { |
paul@78 | 65 | if (bitmap & mask) |
paul@78 | 66 | displayline[get_position(cfg, dx)] = colour; |
paul@78 | 67 | } |
paul@78 | 68 | |
paul@78 | 69 | imagerow += 1; |
paul@78 | 70 | displayline += cfg->line_length; |
paul@78 | 71 | } |
paul@78 | 72 | |
paul@78 | 73 | return x + chardef->width; |
paul@78 | 74 | } |