roo_display
API Documentation for roo_display
Loading...
Searching...
No Matches
font.h
Go to the documentation of this file.
1#pragma once
2
3#include <assert.h>
4#include <inttypes.h>
5
6#include "roo_backport.h"
7#include "roo_backport/string_view.h"
10
11#include "roo_logging.h"
12
13namespace roo_display {
14
15/// Glyph layout direction.
17
18[[deprecated("Use `FontLayout::kHorizontal` instead.")]]
20[[deprecated("Use `FontLayout::kVertical` instead.")]]
22
23/// Basic font metrics (ascent, descent, bounding box, and line spacing).
25 public:
26 /// Construct font metrics.
27 FontMetrics(int ascent, int descent, int linegap, int16_t xMin, int16_t yMin,
28 int16_t xMax, int16_t yMax, int max_right_overhang)
29 : ascent_(ascent),
30 descent_(descent),
31 linegap_(linegap),
32 bbox_(xMin, yMin, xMax, yMax),
33 max_right_overhang_(max_right_overhang) {}
34
35 FontMetrics() = default;
36 FontMetrics(FontMetrics &&) = default;
38
39 /// Font ascent (positive).
40 int16_t ascent() const { return ascent_; }
41 /// Font descent (usually negative).
42 int16_t descent() const { return descent_; } // Usually a negative value.
43 /// Additional gap between lines.
44 int16_t linegap() const { return linegap_; }
45 /// Line advance in pixels.
46 int16_t linespace() const { return ascent() - descent() + linegap(); }
47
48 /// Return maximum glyph extents in FreeType coordinates (Y up).
49 int16_t glyphXMin() const { return bbox_.xMin(); }
50 int16_t glyphYMin() const { return bbox_.yMin(); }
51 int16_t glyphXMax() const { return bbox_.xMax(); }
52 int16_t glyphYMax() const { return bbox_.yMax(); }
53
54 /// Maximum glyph width.
55 int maxWidth() const { return bbox_.width(); }
56 /// Maximum glyph height.
57 int maxHeight() const { return bbox_.height(); }
58
59 /// Minimum left side bearing across glyphs.
60 int minLsb() const { return bbox_.xMin(); }
61 /// Minimum right side bearing across glyphs.
62 int minRsb() const { return -max_right_overhang_; }
63
64 private:
65 int16_t ascent_;
66 int16_t descent_;
67 int16_t linegap_;
68 Box bbox_; // In freeType coordinates; i.e. Y grows up the screen
69 int16_t max_right_overhang_;
70};
71
72/// Metadata describing a font's encoding and spacing behavior.
74 public:
75 /// Character set supported by the font.
76 enum class Charset {
77 kAscii, // 7-bit
78 kUnicodeBmp // 16-bit codes, usually UTF-encoded.
79 };
80
81 /// Spacing behavior for glyph advances.
83
84 /// Smoothing/anti-aliasing mode.
85 enum class Smoothing { kNone, kGrayscale };
86
87 /// Kerning information availability.
88 enum class Kerning { kNone, kPairs };
89
90 [[deprecated("Use `FontProperties::Charset::kAscii` instead.")]]
92 [[deprecated("Use `FontProperties::Charset::kUnicodeBmp` instead.")]]
94
95 [[deprecated("Use `FontProperties::Spacing::kProportional` instead.")]]
97 [[deprecated("Use `FontProperties::Spacing::kMonospace` instead.")]]
99
100 [[deprecated("Use `FontProperties::Smoothing::kNone` instead.")]]
102 [[deprecated("Use `FontProperties::Smoothing::kGrayscale` instead.")]]
104
105 [[deprecated("Use `FontProperties::Kerning::kNone` instead.")]]
107 [[deprecated("Use `FontProperties::Kerning::kPairs` instead.")]]
109
110 FontProperties() = default;
113
114 /// Construct font properties.
117 : charset_(charset),
118 spacing_(spacing),
119 smoothing_(smoothing),
120 kerning_(kerning) {}
121
122 /// Character set supported.
123 Charset charset() const { return charset_; }
124 /// Spacing behavior.
125 Spacing spacing() const { return spacing_; }
126 /// Smoothing/anti-aliasing mode.
127 Smoothing smoothing() const { return smoothing_; }
128 /// Kerning mode.
129 Kerning kerning() const { return kerning_; }
130
131 private:
132 Charset charset_;
133 Spacing spacing_;
134 Smoothing smoothing_;
135 Kerning kerning_;
136};
137
138/// Per-glyph metrics (bounding box and advance).
140 public:
141 /// Construct metrics from FreeType coordinates (Y up).
146
147 GlyphMetrics() = default;
148 GlyphMetrics(const GlyphMetrics &) = default;
152
153 /// Bounding box in screen coordinates (Y down).
154 const Box &screen_extents() const { return bbox_; }
155
156 /// Bounding box in FreeType coordinates (Y up).
157
158 int glyphXMin() const { return bbox_.xMin(); }
159 int glyphXMax() const { return bbox_.xMax(); }
160 int glyphYMin() const { return -bbox_.yMax(); }
161 int glyphYMax() const { return -bbox_.yMin(); }
162
163 /// Left side bearing.
164 int bearingX() const { return bbox_.xMin(); }
165 /// Top side bearing.
166 int bearingY() const { return -bbox_.yMin(); }
167 /// Right side bearing.
168 int rsb() const { return advance() - (bbox_.xMax() + 1); }
169 /// Left side bearing (alias).
170 int lsb() const { return bbox_.xMin(); }
171 /// Glyph width.
172 int width() const { return bbox_.width(); }
173 /// Glyph height.
174 int height() const { return bbox_.height(); }
175
176 /// Advance in pixels.
177 int advance() const { return advance_; }
178
179 private:
180 Box bbox_; // In screen coordinates; i.e. positive Y down.
181 int advance_;
182};
183
184/// Abstract font interface.
185class Font {
186 public:
187 /// Return font metrics.
188 const FontMetrics &metrics() const { return metrics_; }
189 /// Return font properties.
190 const FontProperties &properties() const { return properties_; }
191
192 /// Retrieve glyph metrics for a code point and layout.
193 virtual bool getGlyphMetrics(char32_t code, FontLayout layout,
194 GlyphMetrics *result) const = 0;
195
196 /// Return kerning adjustment for a pair of code points.
197 ///
198 /// The returned value is in pixels and should be added to the base advance of
199 /// the left glyph. Fonts without kerning can keep the default implementation.
200 virtual int16_t getKerning(char32_t left, char32_t right) const {
201 (void)left;
202 (void)right;
203 return 0;
204 }
205
206 /// Draw a UTF-8 string horizontally using a string view.
207 ///
208 /// See https://www.freetype.org/freetype2/docs/glyphs/glyphs-3.html
209 void drawHorizontalString(const Surface &s, roo::string_view text,
210 Color color) const {
211 drawHorizontalString(s, text.data(), text.size(), color);
212 }
213
214 /// Draw a UTF-8 string horizontally.
215 virtual void drawHorizontalString(const Surface &s, const char *utf8_data,
216 uint32_t size, Color color) const = 0;
217
218 /// Draw a single glyph.
219 ///
220 /// The default implementation supports horizontal layout and delegates to
221 /// `drawHorizontalString`. Font implementations can override this for a
222 /// faster path that avoids UTF-8 encoding.
223 virtual void drawGlyph(const Surface& s, char32_t code, FontLayout layout,
224 Color color) const;
225
226 /// Return metrics of the specified UTF-8 string as if it were a single
227 /// glyph.
228 GlyphMetrics getHorizontalStringMetrics(roo::string_view text) const {
229 return getHorizontalStringMetrics(text.data(), text.size());
230 }
231
232 /// Return metrics of the specified UTF-8 string as if it were a single
233 /// glyph.
235 uint32_t size) const = 0;
236
237 /// Return metrics for consecutive glyphs in the UTF-8 string.
238 ///
239 /// Glyphs may overlap due to kerning. The number of glyphs written is
240 /// limited by `max_count`. Returns the number of glyphs measured, which
241 /// may be smaller than `max_count` if the input string is shorter.
244 uint32_t offset,
245 uint32_t max_count) const {
246 return getHorizontalStringGlyphMetrics(text.data(), text.size(), result,
247 offset, max_count);
248 }
249
250 /// Return metrics for consecutive glyphs in the UTF-8 string.
252 const char *utf8_data, uint32_t size, GlyphMetrics *result,
253 uint32_t offset, uint32_t max_count) const = 0;
254
255 virtual ~Font() {}
256
257 protected:
259 metrics_ = std::move(metrics);
260 properties_ = std::move(properties);
261 }
262
263 private:
264 FontMetrics metrics_;
265 FontProperties properties_;
266};
267
268roo_logging::Stream& operator<<(roo_logging::Stream& stream, FontLayout layout);
269roo_logging::Stream& operator<<(roo_logging::Stream& stream, FontProperties::Charset charset);
270roo_logging::Stream& operator<<(roo_logging::Stream& stream, FontProperties::Spacing spacing);
271roo_logging::Stream& operator<<(roo_logging::Stream& stream, FontProperties::Smoothing smoothing);
272roo_logging::Stream& operator<<(roo_logging::Stream& stream, FontProperties::Kerning kerning);
273
274} // namespace roo_display
Axis-aligned integer rectangle.
Definition box.h:12
int16_t width() const
Width in pixels (inclusive coordinates).
Definition box.h:77
int16_t xMin() const
Minimum x (inclusive).
Definition box.h:65
int16_t xMax() const
Maximum x (inclusive).
Definition box.h:71
int16_t height() const
Height in pixels (inclusive coordinates).
Definition box.h:80
int16_t yMax() const
Maximum y (inclusive).
Definition box.h:74
int16_t yMin() const
Minimum y (inclusive).
Definition box.h:68
ARGB8888 color stored as a 32-bit unsigned integer.
Definition color.h:16
Basic font metrics (ascent, descent, bounding box, and line spacing).
Definition font.h:24
int16_t linespace() const
Line advance in pixels.
Definition font.h:46
FontMetrics(int ascent, int descent, int linegap, int16_t xMin, int16_t yMin, int16_t xMax, int16_t yMax, int max_right_overhang)
Construct font metrics.
Definition font.h:27
FontMetrics & operator=(FontMetrics &&)=default
int16_t linegap() const
Additional gap between lines.
Definition font.h:44
int16_t glyphXMax() const
Definition font.h:51
int16_t descent() const
Font descent (usually negative).
Definition font.h:42
int minRsb() const
Minimum right side bearing across glyphs.
Definition font.h:62
int16_t glyphYMin() const
Definition font.h:50
int16_t glyphXMin() const
Return maximum glyph extents in FreeType coordinates (Y up).
Definition font.h:49
int minLsb() const
Minimum left side bearing across glyphs.
Definition font.h:60
int maxWidth() const
Maximum glyph width.
Definition font.h:55
FontMetrics(FontMetrics &&)=default
int16_t glyphYMax() const
Definition font.h:52
int maxHeight() const
Maximum glyph height.
Definition font.h:57
int16_t ascent() const
Font ascent (positive).
Definition font.h:40
Metadata describing a font's encoding and spacing behavior.
Definition font.h:73
static constexpr Spacing SPACING_PROPORTIONAL
Definition font.h:96
FontProperties(FontProperties &&)=default
FontProperties & operator=(FontProperties &&)=default
Smoothing
Smoothing/anti-aliasing mode.
Definition font.h:85
Spacing
Spacing behavior for glyph advances.
Definition font.h:82
Charset charset() const
Character set supported.
Definition font.h:123
Charset
Character set supported by the font.
Definition font.h:76
static constexpr Kerning KERNING_PAIRS
Definition font.h:108
static constexpr Kerning KERNING_NONE
Definition font.h:106
Spacing spacing() const
Spacing behavior.
Definition font.h:125
Smoothing smoothing() const
Smoothing/anti-aliasing mode.
Definition font.h:127
static constexpr Smoothing SMOOTHING_NONE
Definition font.h:101
static constexpr Charset CHARSET_ASCII
Definition font.h:91
static constexpr Smoothing SMOOTHING_GRAYSCALE
Definition font.h:103
Kerning
Kerning information availability.
Definition font.h:88
FontProperties(Charset charset, Spacing spacing, Smoothing smoothing, Kerning kerning)
Construct font properties.
Definition font.h:115
Kerning kerning() const
Kerning mode.
Definition font.h:129
static constexpr Spacing SPACING_MONOSPACE
Definition font.h:98
static constexpr Charset CHARSET_UNICODE_BMP
Definition font.h:93
Abstract font interface.
Definition font.h:185
void drawHorizontalString(const Surface &s, roo::string_view text, Color color) const
Draw a UTF-8 string horizontally using a string view.
Definition font.h:209
void init(FontMetrics metrics, FontProperties properties)
Definition font.h:258
virtual void drawHorizontalString(const Surface &s, const char *utf8_data, uint32_t size, Color color) const =0
Draw a UTF-8 string horizontally.
const FontProperties & properties() const
Return font properties.
Definition font.h:190
virtual int16_t getKerning(char32_t left, char32_t right) const
Return kerning adjustment for a pair of code points.
Definition font.h:200
virtual bool getGlyphMetrics(char32_t code, FontLayout layout, GlyphMetrics *result) const =0
Retrieve glyph metrics for a code point and layout.
virtual uint32_t getHorizontalStringGlyphMetrics(const char *utf8_data, uint32_t size, GlyphMetrics *result, uint32_t offset, uint32_t max_count) const =0
Return metrics for consecutive glyphs in the UTF-8 string.
virtual ~Font()
Definition font.h:255
virtual void drawGlyph(const Surface &s, char32_t code, FontLayout layout, Color color) const
Draw a single glyph.
Definition font.cpp:8
virtual GlyphMetrics getHorizontalStringMetrics(const char *utf8_data, uint32_t size) const =0
Return metrics of the specified UTF-8 string as if it were a single glyph.
const FontMetrics & metrics() const
Return font metrics.
Definition font.h:188
uint32_t getHorizontalStringGlyphMetrics(roo::string_view text, GlyphMetrics *result, uint32_t offset, uint32_t max_count) const
Return metrics for consecutive glyphs in the UTF-8 string.
Definition font.h:242
GlyphMetrics getHorizontalStringMetrics(roo::string_view text) const
Return metrics of the specified UTF-8 string as if it were a single glyph.
Definition font.h:228
Per-glyph metrics (bounding box and advance).
Definition font.h:139
int glyphYMax() const
Definition font.h:161
int glyphXMin() const
Bounding box in FreeType coordinates (Y up).
Definition font.h:158
int bearingY() const
Top side bearing.
Definition font.h:166
int bearingX() const
Left side bearing.
Definition font.h:164
GlyphMetrics & operator=(GlyphMetrics &&)=default
int lsb() const
Left side bearing (alias).
Definition font.h:170
int advance() const
Advance in pixels.
Definition font.h:177
const Box & screen_extents() const
Bounding box in screen coordinates (Y down).
Definition font.h:154
GlyphMetrics & operator=(const GlyphMetrics &)=default
int width() const
Glyph width.
Definition font.h:172
GlyphMetrics(GlyphMetrics &&)=default
int rsb() const
Right side bearing.
Definition font.h:168
int glyphXMax() const
Definition font.h:159
GlyphMetrics(int16_t glyphXMin, int16_t glyphYMin, int16_t glyphXMax, int16_t glyphYMax, int advance)
Construct metrics from FreeType coordinates (Y up).
Definition font.h:142
int height() const
Glyph height.
Definition font.h:174
GlyphMetrics(const GlyphMetrics &)=default
int glyphYMin() const
Definition font.h:160
Low-level handle used to draw to an underlying device.
Definition drawable.h:60
Defines 140 opaque HTML named colors.
constexpr FontLayout FONT_LAYOUT_VERTICAL
Definition font.h:21
FontLayout
Glyph layout direction.
Definition font.h:16
roo_logging::Stream & operator<<(roo_logging::Stream &os, BlendingMode mode)
Definition blending.cpp:54
constexpr FontLayout FONT_LAYOUT_HORIZONTAL
Definition font.h:19