Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 The Chromium OS Authors. All rights reserved. |
| 3 | * Use of this source code is governed by a BSD-style license that can be |
| 4 | * found in the LICENSE file. |
| 5 | */ |
| 6 | |
| 7 | #include <stdint.h> |
| 8 | |
| 9 | #include "font.h" |
Zach Reizner | a752c10 | 2015-04-15 15:03:11 -0700 | [diff] [blame] | 10 | #include "glyphs.h" |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 11 | #include "util.h" |
| 12 | |
Zach Reizner | a752c10 | 2015-04-15 15:03:11 -0700 | [diff] [blame] | 13 | #define UNICODE_REPLACEMENT_CHARACTER_CODE_POINT 0xFFFD |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 14 | |
Stéphane Marchesin | af4423b | 2014-08-13 16:39:24 -0700 | [diff] [blame] | 15 | static int font_scaling; |
| 16 | |
Stéphane Marchesin | af4423b | 2014-08-13 16:39:24 -0700 | [diff] [blame] | 17 | void font_init(int scaling) |
| 18 | { |
| 19 | font_scaling = scaling; |
| 20 | } |
| 21 | |
Stéphane Marchesin | f8807af | 2014-08-18 10:34:41 -0700 | [diff] [blame] | 22 | void font_get_size(uint32_t *char_width, uint32_t *char_height) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 23 | { |
Stéphane Marchesin | af4423b | 2014-08-13 16:39:24 -0700 | [diff] [blame] | 24 | *char_width = GLYPH_WIDTH * font_scaling; |
| 25 | *char_height = GLYPH_HEIGHT * font_scaling; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 26 | } |
| 27 | |
Stéphane Marchesin | f8807af | 2014-08-18 10:34:41 -0700 | [diff] [blame] | 28 | void font_fillchar(uint32_t *dst_pointer, int dst_char_x, int dst_char_y, |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 29 | int32_t pitch, uint32_t front_color, uint32_t back_color) |
| 30 | { |
Stéphane Marchesin | af4423b | 2014-08-13 16:39:24 -0700 | [diff] [blame] | 31 | int dst_x = dst_char_x * GLYPH_WIDTH * font_scaling; |
| 32 | int dst_y = dst_char_y * GLYPH_HEIGHT * font_scaling; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 33 | |
Stéphane Marchesin | af4423b | 2014-08-13 16:39:24 -0700 | [diff] [blame] | 34 | for (int j = 0; j < GLYPH_HEIGHT * font_scaling; j++) |
| 35 | for (int i = 0; i < GLYPH_WIDTH * font_scaling; i++) |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 36 | dst_pointer[dst_x + i + (dst_y + j) * pitch / 4] = |
| 37 | back_color; |
| 38 | } |
| 39 | |
Stéphane Marchesin | f8807af | 2014-08-18 10:34:41 -0700 | [diff] [blame] | 40 | void font_render(uint32_t *dst_pointer, int dst_char_x, int dst_char_y, |
Dominik Behr | 0000350 | 2014-08-15 16:42:37 -0700 | [diff] [blame] | 41 | int32_t pitch, uint32_t ch, uint32_t front_color, |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 42 | uint32_t back_color) |
| 43 | { |
Stéphane Marchesin | af4423b | 2014-08-13 16:39:24 -0700 | [diff] [blame] | 44 | int dst_x = dst_char_x * GLYPH_WIDTH * font_scaling; |
| 45 | int dst_y = dst_char_y * GLYPH_HEIGHT * font_scaling; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 46 | |
Haixia Shi | 7795552 | 2015-04-22 15:21:03 -0700 | [diff] [blame^] | 47 | int32_t glyph_index = code_point_to_glyph_index(ch); |
Zach Reizner | a752c10 | 2015-04-15 15:03:11 -0700 | [diff] [blame] | 48 | if (glyph_index < 0) { |
Haixia Shi | 7795552 | 2015-04-22 15:21:03 -0700 | [diff] [blame^] | 49 | glyph_index = code_point_to_glyph_index( |
Zach Reizner | a752c10 | 2015-04-15 15:03:11 -0700 | [diff] [blame] | 50 | UNICODE_REPLACEMENT_CHARACTER_CODE_POINT); |
| 51 | if (glyph_index < 0) { |
| 52 | return; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | const uint8_t *glyph = glyphs[glyph_index]; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 57 | |
| 58 | for (int j = 0; j < GLYPH_HEIGHT; j++) |
| 59 | for (int i = 0; i < GLYPH_WIDTH; i++) { |
Zach Reizner | a752c10 | 2015-04-15 15:03:11 -0700 | [diff] [blame] | 60 | uint8_t glyph_pixel = glyph[i / 8 + |
| 61 | j * GLYPH_BYTES_PER_ROW]; |
| 62 | |
| 63 | uint32_t pixel = glyph_pixel & (0x1 << (7 - (i % 8))) ? |
| 64 | front_color : back_color; |
| 65 | |
Stéphane Marchesin | af4423b | 2014-08-13 16:39:24 -0700 | [diff] [blame] | 66 | for(int sx = 0; sx < font_scaling; sx++) |
| 67 | for(int sy = 0; sy < font_scaling; sy++) |
| 68 | dst_pointer[dst_x + font_scaling * i + |
| 69 | sx + (dst_y + font_scaling * |
| 70 | j + sy) * pitch / 4] = pixel; |
Stéphane Marchesin | ae37e6c | 2014-08-08 18:19:40 -0700 | [diff] [blame] | 71 | } |
| 72 | } |