forked from swissmicros/SDKdemo
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathfont.h
198 lines (176 loc) · 6.07 KB
/
font.h
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#ifndef FONT_H
#define FONT_H
// ****************************************************************************
// font.h DB48X project
// ****************************************************************************
//
// File Description:
//
// RPL font objects
//
//
//
//
//
//
//
//
// ****************************************************************************
// (C) 2022 Christophe de Dinechin <[email protected]>
// This software is licensed under the terms outlined in LICENSE.txt
// ****************************************************************************
// This file is part of DB48X.
//
// DB48X is free software: you can redistribute it and/or modify
// it under the terms outlined in the LICENSE.txt file
//
// DB48X 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.
// ****************************************************************************
#include "object.h"
#include "utf8.h"
RECORDER_DECLARE(fonts);
RECORDER_DECLARE(fonts_error);
struct font : object
// ----------------------------------------------------------------------------
// Shared by all font objects
// ----------------------------------------------------------------------------
{
typedef int16_t fint;
typedef uint16_t fuint;
font(id type): object(type) { }
struct glyph_info
{
byte_p bitmap; // Bitmap we get the glyph from
fint bx; // X position in bitmap
fint by; // Y position in bitmap (always 0 today?)
fuint bw; // Width of bitmap
fuint bh; // Height of bitmap
fint x; // X position of glyph when drawing
fint y; // Y position of glyph when drawing
fuint w; // Width of glyph
fuint h; // Height of glyph
fuint advance; // X advance to next character
fuint height; // Y advance to next line
};
bool glyph(unicode codepoint, glyph_info &g) const;
fuint width(unicode codepoint) const
{
glyph_info g;
if (glyph(codepoint, g))
return g.advance;
return 0;
}
fuint width(utf8 text) const
{
fuint result = 0;
for (utf8 p = text; *p; p = utf8_next(p))
result += width(utf8_codepoint(p));
return result;
}
fuint width(utf8 text, size_t len) const
{
fuint result = 0;
utf8 last = text + len;
for (utf8 p = text; p < last; p = utf8_next(p))
result += width(utf8_codepoint(p));
return result;
}
fuint height(unicode codepoint) const
{
glyph_info g;
if (glyph(codepoint, g))
return g.advance;
return 0;
}
fuint height() const;
public:
SIZE_DECL(font)
{
byte_p p = payload(o);
return ptrdiff(p, o) + leb128<size_t>(p);
}
};
typedef const font *font_p;
struct sparse_font : font
// ----------------------------------------------------------------------------
// An object representing a sparse font (one bitmap per character)
// ----------------------------------------------------------------------------
{
sparse_font(id type = ID_sparse_font): font(type) {}
OBJECT_DECL(sparse_font);
bool glyph(unicode codepoint, glyph_info &g) const;
fuint height();
};
typedef const sparse_font *sparse_font_p;
struct dense_font : font
// ----------------------------------------------------------------------------
// An object representing a dense font (a single bitmap for all characters)
// ----------------------------------------------------------------------------
{
dense_font(id type = ID_dense_font): font(type) {}
OBJECT_DECL(dense_font);
bool glyph(unicode codepoint, glyph_info &g) const;
fuint height();
};
typedef const dense_font *dense_font_p;
struct dmcp_font : font
// ----------------------------------------------------------------------------
// An object accessing the DMCP built-in fonts (and remapping to Unicode)
// ----------------------------------------------------------------------------
{
dmcp_font(fint index, id type = ID_dense_font): font(type)
{
byte_p p = payload(this);
leb128(p, index);
}
static size_t required_memory(id i, fint index)
{
return leb128size(i) + leb128size(index);
}
OBJECT_DECL(dense_font);
fint index() const { byte_p p = payload(this); return leb128<fint>(p); }
bool glyph(unicode codepoint, glyph_info &g) const;
fuint height();
};
typedef const dmcp_font *dmcp_font_p;
inline font::fuint font::height() const
// ----------------------------------------------------------------------------
// Dynamic dispatch to the available font classes
// ----------------------------------------------------------------------------
{
switch(type())
{
case ID_sparse_font: return ((sparse_font *)this)->height();
case ID_dense_font: return ((dense_font *)this)->height();
case ID_dmcp_font: return ((dmcp_font *)this)->height();
default:
record(fonts_error, "Unexpectd font type %d", type());
}
return false;
}
// Fonts for various parts of the user interface
extern font_p EditorFont;
extern font_p StackFont;
extern font_p ReducedFont;
extern font_p ErrorFont;
extern font_p MenuFont;
extern font_p HelpFont;
extern font_p HelpBoldFont;
extern font_p HelpItalicFont;
extern font_p HelpCodeFont;
extern font_p HelpTitleFont;
extern font_p HelpSubTitleFont;
void font_defaults();
// In the DM42 DMCP - Not fully Unicode capable
extern const dmcp_font_p LibMonoFont10x17;
extern const dmcp_font_p LibMonoFont11x18;
extern const dmcp_font_p LibMonoFont12x20;
extern const dmcp_font_p LibMonoFont14x22;
extern const dmcp_font_p LibMonoFont17x25;
extern const dmcp_font_p LibMonoFont17x28;
extern const dmcp_font_p SkrMono13x18;
extern const dmcp_font_p SkrMono18x24;
extern const dmcp_font_p Free42Font;
#endif // FONT_H