-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgfx_font.c
More file actions
131 lines (119 loc) · 3.05 KB
/
gfx_font.c
File metadata and controls
131 lines (119 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Pithesiser - a software synthesiser for Raspberry Pi
// Copyright (C) 2015 Nicholas Tuckett
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
/*
* gfx_font.c
*
* Created on: 8 Sep 2013
* Author: ntuckett
*
* Derived from code on https://github.com/ajstarks/openvg
*
*/
#include "gfx_font.h"
#include <string.h>
#include "VG/openvg.h"
static const int MAXFONTPATH = 256;
font_info_t gfx_load_font(
const int *points,
const int *point_indices,
const unsigned char *instructions_set,
const int *instruction_indices,
const int *instruction_counts,
const int *adv,
const short *cmap,
int ng
)
{
font_info_t f;
int i;
memset(f.glyphs, 0, MAXFONTPATH * sizeof(VGPath));
if (ng > MAXFONTPATH)
{
return f;
}
for (i = 0; i < ng; i++)
{
const int *p = &points[point_indices[i] * 2];
const unsigned char *instructions = &instructions_set[instruction_indices[i]];
int ic = instruction_counts[i];
VGPath path = vgCreatePath(
VG_PATH_FORMAT_STANDARD, VG_PATH_DATATYPE_S_32,
1.0f / 65536.0f, 0.0f, 0, 0,
VG_PATH_CAPABILITY_ALL
);
f.glyphs[i] = path;
if (ic)
{
vgAppendPathData(path, ic, instructions, p);
}
}
f.character_map = cmap;
f.glyph_advances = adv;
f.count = ng;
return f;
}
void gfx_unload_font(font_info_t* f)
{
VGPath * glyphs = f->glyphs;
int i;
for (i = 0; i < f->count; i++)
{
vgDestroyPath(glyphs[i]);
}
}
void gfx_render_text(float x, float y, const char *s, const font_info_t* f, int point_size)
{
VGfloat size = (VGfloat) point_size, xx = x, mm[9];
int i;
const VGfloat scaled_size = size / 65536.0f;
vgGetMatrix(mm);
VGfloat render_mat[9] = {
size, 0.0f, 0.0f,
0.0f, size, 0.0f,
xx, y, 1.0f
};
vgMultMatrix(render_mat);
vgGetMatrix(render_mat);
for (i = 0; i < (int)strlen(s); i++)
{
unsigned int character = (unsigned int)s[i];
int glyph = f->character_map[character];
if (glyph > -1)
{
vgLoadMatrix(render_mat);
vgDrawPath(f->glyphs[glyph], VG_FILL_PATH);
render_mat[6] += scaled_size * f->glyph_advances[glyph];
}
}
vgLoadMatrix(mm);
}
float gfx_text_width(const char *s, const font_info_t* f, int point_size)
{
int i;
VGfloat tw = 0.0;
VGfloat size = (VGfloat) point_size;
const VGfloat scaled_size = size / 65536.0f;
for (i = 0; i < (int)strlen(s); i++)
{
unsigned int character = (unsigned int)s[i];
int glyph = f->character_map[character];
if (glyph > -1)
{
tw += scaled_size * f->glyph_advances[glyph];
}
}
return tw;
}