Open Font Render 1.2
Loading...
Searching...
No Matches
OpenFontRender.h
Go to the documentation of this file.
1// -------------------------------------------------------
2// OpenFontRender.h
3//
4// Copyright (c) 2021 takkaO
5//
6// If you use, modify or redistribute this file as part of
7// the original repository, please follow the repository's license.
8//
9// -------------------------------------------------------
10
11#ifndef OPEN_FONT_RENDER_H
12#define OPEN_FONT_RENDER_H
13
14#if defined(ARDUINO)
15 #include <Arduino.h>
16 #undef min
17 #undef max
18#endif
19
20#include <stdarg.h>
21#include <stdio.h>
22
23#include "ft2build.h"
24#include FT_CACHE_H
25#include FT_FREETYPE_H
26
27#include <functional>
28#include <queue>
29#include <string>
30#include <utility>
31#include <vector>
32
33#include "FileSupport.h"
34
35/*_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/*/
36//
37// Constant definition
38//
39/*_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/*/
40
49 OFR_RAW = 8,
50};
51
56enum class Align {
57 Left,
58 Center,
59 Right,
60 TopLeft,
61 TopCenter,
62 TopRight,
69};
70
74enum class BgFillMethod {
75 None,
76 Minimum,
77 Block,
78};
79
83enum class Layout {
86};
87
91enum class Drawing {
92 Execute,
93 Skip
94};
95
97namespace OFR {
98 /* USER DO NOT USE DIRECTORY IN THIS SCOPE ELEMENTS */
99 enum LoadFontFrom {
100 FROM_FILE,
101 FROM_MEMORY
102 };
103
104 typedef struct FaceRec_ {
105 char *filepath; // ttf file path
106 unsigned char *data; // ttf array
107 size_t data_size; // ttf array size
108 uint8_t face_index; // face index (default is 0)
109 } FaceRec, *Face;
110};
113/*_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/*/
114//
115// Class definition
116//
117/*_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/*/
118
133#define setDrawPixel(F) set_drawPixel([&](int32_t x, int32_t y, uint16_t c) { return F(x, y, c); })
149#define setDrawFastHLine(F) set_drawFastHLine([&](int32_t x, int32_t y, int32_t w, uint16_t c) { return F(x, y, w, c); })
160#define setStartWrite(F) set_startWrite([&](void) { return F(); })
171#define setEndWrite(F) set_endWrite([&](void) { return F(); })
181#define setPrintFunc(F) set_printFunc([&](const char *s) { return F(s); })
182
184public:
185 static const unsigned char MAIN_VERSION = 1;
186 static const unsigned char MINOR_VERSION = 2;
187
188 static const unsigned char CACHE_SIZE_NO_LIMIT = 0;
189 static const unsigned char CACHE_SIZE_MINIMUM = 1;
190 static const unsigned char FT_VERSION_STRING_SIZE = 32;
191 static const unsigned char CREDIT_STRING_SIZE = 128;
192
194 void setUseRenderTask(bool enable);
195 void setRenderTaskStackSize(unsigned int stack_size);
196
197 void setCursor(int32_t x, int32_t y);
198 int32_t getCursorX();
199 int32_t getCursorY();
200 void seekCursor(int32_t delta_x, int32_t delta_y);
201
202 void setFontColor(uint16_t font_color);
203 void setBackgroundColor(uint16_t font_bgcolor);
204 void setFontColor(uint16_t font_color, uint16_t font_bgcolor);
205
206 void setFontColor(uint8_t r, uint8_t g, uint8_t b);
207 void setBackgroundColor(uint8_t r, uint8_t g, uint8_t b);
208 void setFontColor(uint8_t fr,
209 uint8_t fg,
210 uint8_t fb,
211 uint8_t br,
212 uint8_t bg,
213 uint8_t bb);
214 uint16_t getFontColor();
215 uint16_t getBackgroundColor();
216 void setFontSize(unsigned int pixel);
217 unsigned int getFontSize();
218 double setLineSpaceRatio(double line_space_ratio);
219 double getLineSpaceRatio();
222 void setLayout(Layout layout);
224 void setAlignment(Align align);
226 void setCacheSize(unsigned int max_faces, unsigned int max_sizes, unsigned long max_bytes);
227
228 FT_Error loadFont(const unsigned char *data, size_t size, uint8_t target_face_index = 0);
229 FT_Error loadFont(const char *fpath, uint8_t target_face_index = 0);
230 void unloadFont();
231
232 uint16_t drawHString(const char *str,
233 int32_t x,
234 int32_t y,
235 uint16_t fg,
236 uint16_t bg,
237 Align align,
238 Drawing drawing,
239 FT_BBox &abbox,
240 FT_Error &error);
241 FT_Error drawChar(char character,
242 int32_t x = 0,
243 int32_t y = 0,
244 uint16_t fg = 0xFFFF,
245 uint16_t bg = 0x0000,
246 Align align = Align::Left);
247 uint16_t drawString(const char *str,
248 int32_t x = 0,
249 int32_t y = 0,
250 uint16_t fg = 0xFFFF,
251 uint16_t bg = 0x0000,
252 Layout layout = Layout::Horizontal);
253 uint16_t cdrawString(const char *str,
254 int32_t x = 0,
255 int32_t y = 0,
256 uint16_t fg = 0xFFFF,
257 uint16_t bg = 0x0000,
258 Layout layout = Layout::Horizontal);
259 uint16_t rdrawString(const char *str,
260 int32_t x = 0,
261 int32_t y = 0,
262 uint16_t fg = 0xFFFF,
263 uint16_t bg = 0x0000,
264 Layout layout = Layout::Horizontal);
265
266 uint16_t printf(const char *fmt, ...);
267 uint16_t cprintf(const char *fmt, ...);
268 uint16_t rprintf(const char *fmt, ...);
269
270 FT_BBox calculateBoundingBoxFmt(int32_t x, int32_t y, unsigned int font_size, Align align, Layout layout, const char *fmt, ...);
271 FT_BBox calculateBoundingBox(int32_t x, int32_t y, unsigned int font_size, Align align, Layout layout, const char *str);
272
273 uint32_t getTextWidth(const char *fmt, ...);
274 uint32_t getTextHeight(const char *fmt, ...);
275
276 unsigned int calculateFitFontSizeFmt(uint32_t limit_width, uint32_t limit_height, Layout layout, const char *fmt, ...);
277 unsigned int calculateFitFontSize(uint32_t limit_width, uint32_t limit_height, Layout layout, const char *str);
278
279 void showFreeTypeVersion();
280 void showCredit();
281 void getFreeTypeVersion(char *str);
282 void getCredit(char *str);
283 void setDebugLevel(uint8_t level);
284
295 template <typename T>
296 void setDrawer(T &drawer) {
297 set_drawPixel([&](int32_t x, int32_t y, uint16_t c) { return drawer.drawPixel(x, y, c); });
298 set_drawFastHLine([&](int32_t x, int32_t y, int32_t w, uint16_t c) { return drawer.drawFastHLine(x, y, w, c); });
299 set_startWrite([&](void) { return drawer.startWrite(); });
300 set_endWrite([&](void) { return drawer.endWrite(); });
301 }
302
303 // Direct calls are deprecated.
304 void set_drawPixel(std::function<void(int32_t, int32_t, uint16_t)> user_func);
305 void set_drawFastHLine(std::function<void(int32_t, int32_t, int32_t, uint16_t)> user_func);
306 void set_startWrite(std::function<void(void)> user_func);
307 void set_endWrite(std::function<void(void)> user_func);
308
309 /* Static member method */
318 template <typename T>
319 static void setSerial(T &output) {
320 set_printFunc([&](const char *s) { return output.print(s); });
321 }
322 // Direct calls are deprecated.
323 static void set_printFunc(std::function<void(const char *)> user_func);
324
328 struct Cursor {
329 int32_t x;
330 int32_t y;
331 };
332
333private:
334 FT_Error loadFont(enum OFR::LoadFontFrom from);
335 uint32_t getFontMaxHeight();
336 void draw2screen(FT_BitmapGlyph glyph, uint32_t x, uint32_t y, uint16_t fg, uint16_t bg);
337 uint16_t decodeUTF8(uint8_t *buf, uint16_t *index, uint16_t remaining);
338 uint16_t color565(uint8_t r, uint8_t g, uint8_t b);
339 uint16_t alphaBlend(uint8_t alpha, uint16_t fgc, uint16_t bgc);
340
341 std::function<void(int32_t, int32_t, uint16_t)> _drawPixel;
342 std::function<void(int32_t, int32_t, int32_t, uint16_t)> _drawFastHLine;
343 std::function<void(void)> _startWrite;
344 std::function<void(void)> _endWrite;
345
346 FTC_Manager _ftc_manager;
347 FTC_CMapCache _ftc_cmap_cache;
348 FTC_ImageCache _ftc_image_cache;
349
350 OFR::FaceRec _face_id;
351
352 struct Flags {
353 bool enable_optimized_drawing;
354 bool support_vertical;
355 };
356 struct Flags _flags;
357
358 struct CacheParameter {
359 unsigned int max_faces;
360 unsigned int max_sizes;
361 unsigned long max_bytes;
362 };
363 struct CacheParameter _cache;
364
365 struct SavedStateVariables {
366 struct Cursor drawn_bg_point;
367 uint32_t prev_max_font_height;
368 unsigned int prev_font_size;
369 };
370 struct SavedStateVariables _saved_state;
371
372 struct TextParameter {
373 double line_space_ratio;
374 unsigned int size;
375 uint16_t fg_color;
376 uint16_t bg_color;
377 struct Cursor cursor;
378 Align align;
379 BgFillMethod bg_fill_method;
380 Layout layout;
381 };
382 struct TextParameter _text;
383
384 uint8_t _debug_level;
385};
386
387#endif
OFR_DEBUG_LEVEL
An enumeration for specifying the debug log level.
Definition: OpenFontRender.h:44
@ OFR_RAW
Use for internal.
Definition: OpenFontRender.h:49
@ OFR_INFO
Output some informations.
Definition: OpenFontRender.h:47
@ OFR_ERROR
Output only errors.
Definition: OpenFontRender.h:46
@ OFR_DEBUG
Output character rendering result.
Definition: OpenFontRender.h:48
@ OFR_NONE
No output.
Definition: OpenFontRender.h:45
Layout
An enumeration for specifying the direction in which characters are written.
Definition: OpenFontRender.h:83
@ Vertical
Write from top to bottom.
@ Horizontal
Write from left to right.
BgFillMethod
An enumeration for specifying the background drawing method.
Definition: OpenFontRender.h:74
@ None
Does not fill the background.
@ Minimum
Fill in the smallest area that surrounds each character.
@ Block
Fill in the smallest area that surrounds the string.
Align
An enumeration for specifying the text alignment.
Definition: OpenFontRender.h:56
@ MiddleLeft
The cursor position is considered to be the middle-left of the text box.
@ TopRight
The cursor position is considered to be the top-right corner of the text box.
@ MiddleCenter
The cursor position is considered to be the middle-center of the text box.
@ MiddleRight
The cursor position is considered to be the middle-right of the text box.
@ Center
Alias of "TopCenter".
@ BottomRight
The cursor position is considered to be the bottom-right corner of the text box.
@ TopCenter
The cursor position is considered to be the top-center of the text box.
@ Right
Alias of "TopRight".
@ Left
Alias of "TopLeft".
@ BottomLeft
The cursor position is considered to be the bottom-left corner of the text box.
@ TopLeft
The cursor position is considered to be the top-left corner of the text box.
@ BottomCenter
The cursor position is considered to be the bottom-center of the text box.
Drawing
An enumeration for specifying the whether to draw to screen.
Definition: OpenFontRender.h:91
@ Execute
The drawing process is executed and the screen will be updated.
@ Skip
The drawing process is skiped and the screen will not be updated.
Definition: OpenFontRender.h:183
void set_drawPixel(std::function< void(int32_t, int32_t, uint16_t)> user_func)
static const unsigned char FT_VERSION_STRING_SIZE
Minimum string length for FreeType version.
Definition: OpenFontRender.h:190
static void set_printFunc(std::function< void(const char *)> user_func)
void set_startWrite(std::function< void(void)> user_func)
static const unsigned char CACHE_SIZE_NO_LIMIT
FreeType cache size alias.
Definition: OpenFontRender.h:188
static const unsigned char MINOR_VERSION
Open Font Render library minor version.
Definition: OpenFontRender.h:186
OpenFontRender()
Definition: OpenFontRender.cpp:100
static const unsigned char CREDIT_STRING_SIZE
Minimum string length for FreeType credit.
Definition: OpenFontRender.h:191
void set_endWrite(std::function< void(void)> user_func)
static const unsigned char MAIN_VERSION
Open Font Render library main version.
Definition: OpenFontRender.h:185
void set_drawFastHLine(std::function< void(int32_t, int32_t, int32_t, uint16_t)> user_func)
static const unsigned char CACHE_SIZE_MINIMUM
FreeType cache size alias.
Definition: OpenFontRender.h:189
int32_t getCursorX()
Get current cursor x-coordinate.
Definition: OpenFontRender.cpp:184
int32_t getCursorY()
Get current cursor y-coordinate.
Definition: OpenFontRender.cpp:193
void seekCursor(int32_t delta_x, int32_t delta_y)
Move cursor position.
Definition: OpenFontRender.cpp:203
void setCursor(int32_t x, int32_t y)
Set cursor positions.
Definition: OpenFontRender.cpp:175
unsigned int getFontSize()
Get the current font size.
Definition: OpenFontRender.cpp:308
double setLineSpaceRatio(double line_space_ratio)
Adjusts the width between lines of text.
Definition: OpenFontRender.cpp:319
uint16_t getFontColor()
Get the current text color.
Definition: OpenFontRender.cpp:279
void setBackgroundColor(uint16_t font_bgcolor)
Specify the background color.
Definition: OpenFontRender.cpp:222
uint16_t getBackgroundColor()
Get the current background color.
Definition: OpenFontRender.cpp:288
void setFontColor(uint16_t font_color)
Specify the text color.
Definition: OpenFontRender.cpp:213
void setFontSize(unsigned int pixel)
Set the font size for rendering.
Definition: OpenFontRender.cpp:299
double getLineSpaceRatio()
Get the current line space ratio.
Definition: OpenFontRender.cpp:333
Align getAlignment()
Get the current text alignment.
Definition: OpenFontRender.cpp:397
BgFillMethod getBackgroundFillMethod()
Get the current background fill method.
Definition: OpenFontRender.cpp:354
Layout getLayout()
Get the current direction of text writing.
Definition: OpenFontRender.cpp:376
void setAlignment(Align align)
Set the text alignment.
Definition: OpenFontRender.cpp:387
void setBackgroundFillMethod(BgFillMethod method)
Set the background fill method.
Definition: OpenFontRender.cpp:344
void setLayout(Layout layout)
Set the direction of text writing.
Definition: OpenFontRender.cpp:366
void setDrawer(T &drawer)
Collectively set up screen control functions.
Definition: OpenFontRender.h:296
unsigned int calculateFitFontSize(uint32_t limit_width, uint32_t limit_height, Layout layout, const char *str)
Calculates the maximum font size that will fit the specified string and the specified rectangle.
Definition: OpenFontRender.cpp:1150
unsigned int calculateFitFontSizeFmt(uint32_t limit_width, uint32_t limit_height, Layout layout, const char *fmt,...)
Calculates the maximum font size that will fit the specified format string and the specified rectangl...
Definition: OpenFontRender.cpp:1124
void unloadFont()
Unload font data.
Definition: OpenFontRender.cpp:457
uint16_t printf(const char *fmt,...)
Renders text with format specifier.
Definition: OpenFontRender.cpp:950
uint16_t cprintf(const char *fmt,...)
Renders text as Top-Center with format specifier.
Definition: OpenFontRender.cpp:970
uint16_t drawString(const char *str, int32_t x=0, int32_t y=0, uint16_t fg=0xFFFF, uint16_t bg=0x0000, Layout layout=Layout::Horizontal)
Renders text.
Definition: OpenFontRender.cpp:855
uint16_t rdrawString(const char *str, int32_t x=0, int32_t y=0, uint16_t fg=0xFFFF, uint16_t bg=0x0000, Layout layout=Layout::Horizontal)
Renders text as Top-Right.
Definition: OpenFontRender.cpp:923
FT_BBox calculateBoundingBox(int32_t x, int32_t y, unsigned int font_size, Align align, Layout layout, const char *str)
Calculate text bounding box.
Definition: OpenFontRender.cpp:1040
uint16_t rprintf(const char *fmt,...)
Renders text as Top-Right with format specifier.
Definition: OpenFontRender.cpp:990
FT_Error loadFont(const unsigned char *data, size_t size, uint8_t target_face_index=0)
Load font from memory.
Definition: OpenFontRender.cpp:424
uint16_t cdrawString(const char *str, int32_t x=0, int32_t y=0, uint16_t fg=0xFFFF, uint16_t bg=0x0000, Layout layout=Layout::Horizontal)
Renders text as Top-Center.
Definition: OpenFontRender.cpp:889
uint32_t getTextWidth(const char *fmt,...)
Calculate text width.
Definition: OpenFontRender.cpp:1079
FT_BBox calculateBoundingBoxFmt(int32_t x, int32_t y, unsigned int font_size, Align align, Layout layout, const char *fmt,...)
Calculate text bounding box with format specifier.
Definition: OpenFontRender.cpp:1013
uint16_t drawHString(const char *str, int32_t x, int32_t y, uint16_t fg, uint16_t bg, Align align, Drawing drawing, FT_BBox &abbox, FT_Error &error)
Renders text horizontally.
Definition: OpenFontRender.cpp:485
FT_Error drawChar(char character, int32_t x=0, int32_t y=0, uint16_t fg=0xFFFF, uint16_t bg=0x0000, Align align=Align::Left)
Render single character.
Definition: OpenFontRender.cpp:828
void setCacheSize(unsigned int max_faces, unsigned int max_sizes, unsigned long max_bytes)
Set FreeType cache size.
Definition: OpenFontRender.cpp:410
uint32_t getTextHeight(const char *fmt,...)
Calculate text height.
Definition: OpenFontRender.cpp:1099
void setRenderTaskStackSize(unsigned int stack_size)
Specify the stack size for independent rendering tasks.
Definition: OpenFontRender.cpp:163
void setUseRenderTask(bool enable)
Set whether the rendering task should be performed independently or not.
Definition: OpenFontRender.cpp:150
void showCredit()
Show FreeType credit.
Definition: OpenFontRender.cpp:1226
void getFreeTypeVersion(char *str)
Get using FreeType version text.
Definition: OpenFontRender.cpp:1239
void showFreeTypeVersion()
Show using FreeType version.
Definition: OpenFontRender.cpp:1213
void getCredit(char *str)
Get using FreeType credit text.
Definition: OpenFontRender.cpp:1251
void setDebugLevel(uint8_t level)
Set debug output level.
Definition: OpenFontRender.cpp:1264
static void setSerial(T &output)
Set up serial output control functions.
Definition: OpenFontRender.h:319
Structure for handling cursor position.
Definition: OpenFontRender.h:328
int32_t y
y-coordinate
Definition: OpenFontRender.h:330
int32_t x
x-coordinate
Definition: OpenFontRender.h:329