2
* Copyright (c) 2012 Petr Koupy
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
9
* - Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* - Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
14
* - The name of the author may not be used to endorse or promote products
15
* derived from this software without specific prior written permission.
17
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40
#include "font/embedded.h"
43
void font_init(font_t *font, font_decoder_type_t decoder, char *path, uint16_t points)
45
font->points = points;
48
case FONT_DECODER_EMBEDDED:
49
font->decoder = &fd_embedded;
57
font->decoder->init(path, &font->glyph_count, &font->decoder_data);
59
if (font->glyph_count > 0) {
60
font->glyphs = (surface_t **) malloc(sizeof(surface_t *) * font->glyph_count);
66
for (size_t i = 0; i < font->glyph_count; ++i) {
67
font->glyphs[i] = NULL;
70
font->glyph_count = 0;
73
font->glyph_count = 0;
75
font->decoder_data = NULL;
79
void font_release(font_t *font)
82
for (size_t i = 0; i < font->glyph_count; ++i) {
83
if (font->glyphs[i]) {
84
surface_destroy(font->glyphs[i]);
91
font->decoder->release(font->decoder_data);
95
void font_get_box(font_t *font, char *text, sysarg_t *width, sysarg_t *height)
108
uint16_t glyph_idx = font->decoder->resolve(*text, font->decoder_data);
109
if (glyph_idx < font->glyph_count) {
110
if (!font->glyphs[glyph_idx]) {
111
font->glyphs[glyph_idx] =
112
font->decoder->render(glyph_idx, font->points);
115
surface_t *glyph = font->glyphs[glyph_idx];
119
surface_get_resolution(glyph, &w, &h);
121
(*height) = (*height) < h ? h : (*height);
128
void font_draw_text(font_t *font, drawctx_t *context, source_t *source,
129
const char *text, sysarg_t x, sysarg_t y)
134
drawctx_save(context);
135
drawctx_set_compose(context, compose_over);
138
uint16_t glyph_idx = font->decoder->resolve(*text, font->decoder_data);
139
if (glyph_idx < font->glyph_count) {
140
if (!font->glyphs[glyph_idx]) {
141
font->glyphs[glyph_idx] =
142
font->decoder->render(glyph_idx, font->points);
145
surface_t *glyph = font->glyphs[glyph_idx];
149
surface_get_resolution(glyph, &w, &h);
151
transform_t transform;
152
transform_identity(&transform);
153
transform_translate(&transform, x, y);
154
source_set_transform(source, transform);
155
source_set_mask(source, glyph, false);
156
drawctx_transfer(context, x, y, w, h);
164
drawctx_restore(context);
165
source_set_mask(source, NULL, false);