blob: 2c2f49b544c161ab4605cdabccaadafc6035846d [file] [log] [blame]
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -07001/*
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 Reiznera752c102015-04-15 15:03:11 -070010#include "glyphs.h"
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -070011#include "util.h"
12
Zach Reiznera752c102015-04-15 15:03:11 -070013#define UNICODE_REPLACEMENT_CHARACTER_CODE_POINT 0xFFFD
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -070014
Stéphane Marchesinaf4423b2014-08-13 16:39:24 -070015static int font_scaling;
16
Stéphane Marchesinaf4423b2014-08-13 16:39:24 -070017void font_init(int scaling)
18{
19 font_scaling = scaling;
20}
21
Stéphane Marchesinf8807af2014-08-18 10:34:41 -070022void font_get_size(uint32_t *char_width, uint32_t *char_height)
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -070023{
Stéphane Marchesinaf4423b2014-08-13 16:39:24 -070024 *char_width = GLYPH_WIDTH * font_scaling;
25 *char_height = GLYPH_HEIGHT * font_scaling;
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -070026}
27
Stéphane Marchesinf8807af2014-08-18 10:34:41 -070028void font_fillchar(uint32_t *dst_pointer, int dst_char_x, int dst_char_y,
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -070029 int32_t pitch, uint32_t front_color, uint32_t back_color)
30{
Stéphane Marchesinaf4423b2014-08-13 16:39:24 -070031 int dst_x = dst_char_x * GLYPH_WIDTH * font_scaling;
32 int dst_y = dst_char_y * GLYPH_HEIGHT * font_scaling;
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -070033
Stéphane Marchesinaf4423b2014-08-13 16:39:24 -070034 for (int j = 0; j < GLYPH_HEIGHT * font_scaling; j++)
35 for (int i = 0; i < GLYPH_WIDTH * font_scaling; i++)
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -070036 dst_pointer[dst_x + i + (dst_y + j) * pitch / 4] =
37 back_color;
38}
39
Stéphane Marchesinf8807af2014-08-18 10:34:41 -070040void font_render(uint32_t *dst_pointer, int dst_char_x, int dst_char_y,
Dominik Behr00003502014-08-15 16:42:37 -070041 int32_t pitch, uint32_t ch, uint32_t front_color,
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -070042 uint32_t back_color)
43{
Stéphane Marchesinaf4423b2014-08-13 16:39:24 -070044 int dst_x = dst_char_x * GLYPH_WIDTH * font_scaling;
45 int dst_y = dst_char_y * GLYPH_HEIGHT * font_scaling;
Stéphane Marchesinae37e6c2014-08-08 18:19:40 -070046
Haixia Shi77955522015-04-22 15:21:03 -070047 int32_t glyph_index = code_point_to_glyph_index(ch);
Zach Reiznera752c102015-04-15 15:03:11 -070048 if (glyph_index < 0) {
Haixia Shi77955522015-04-22 15:21:03 -070049 glyph_index = code_point_to_glyph_index(
Zach Reiznera752c102015-04-15 15:03:11 -070050 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 Marchesinae37e6c2014-08-08 18:19:40 -070057
58 for (int j = 0; j < GLYPH_HEIGHT; j++)
59 for (int i = 0; i < GLYPH_WIDTH; i++) {
Zach Reiznera752c102015-04-15 15:03:11 -070060 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 Marchesinaf4423b2014-08-13 16:39:24 -070066 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 Marchesinae37e6c2014-08-08 18:19:40 -070071 }
72}