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