blob: 72b7620a61056d60dbb3a5e26a6abdac0c0c7447 [file] [log] [blame]
bigbiff bigbiffac6cc082019-11-01 19:00:27 -04001/*
2 Copyright 2013 to 2020 TeamWin
3 TWRP is free software: you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation, either version 3 of the License, or
6 (at your option) any later version.
7
8 TWRP is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
15*/
16
17#ifndef _TWRP_TRUETYPE_HPP
18#define _TWRP_TRUETYPE_HPP
19
20#include <map>
21#include <string>
22#include <ft2build.h>
23#include <pthread.h>
24#include FT_FREETYPE_H
25#include FT_GLYPH_H
26#include <pixelflinger/pixelflinger.h>
27#include "minui.h"
28
29typedef struct TrueTypeFontKey {
30 int size;
31 int dpi;
bigbifffdca6b82020-09-12 13:36:33 -040032 std::string path;
bigbiff bigbiffac6cc082019-11-01 19:00:27 -040033} TrueTypeFontKey;
34
35inline bool operator<(const TrueTypeFontKey &ttfkLeft, const TrueTypeFontKey &ttfkRight) {
bigbifffdca6b82020-09-12 13:36:33 -040036 return std::tie(ttfkLeft.size, ttfkLeft.dpi, ttfkLeft.path) < std::tie(ttfkRight.size, ttfkRight.dpi, ttfkRight.path);
bigbiff bigbiffac6cc082019-11-01 19:00:27 -040037}
38
39typedef struct {
40 FT_BBox bbox;
41 FT_BitmapGlyph glyph;
42} TrueTypeCacheEntry;
43
44typedef struct StringCacheKey {
45 int max_width;
46 std::string text;
47} StringCacheKey;
48
49inline bool operator<(const StringCacheKey &sckLeft, const StringCacheKey &sckRight) {
50 return std::tie(sckLeft.text, sckLeft.max_width) < std::tie(sckRight.text, sckRight.max_width);
51}
52
53typedef struct StringCacheEntry {
54 GGLSurface surface;
55 int rendered_bytes; // number of bytes from C string rendered, not number of UTF8 characters!
56 StringCacheKey *key;
57} StringCacheEntry;
58
59typedef struct {
60 int type;
61 int refcount;
62 int size;
63 int dpi;
64 int max_height;
65 int base;
66 FT_Face face;
67 std::map<int, TrueTypeCacheEntry*> glyph_cache;
68 std::map<StringCacheKey, StringCacheEntry*> string_cache;
69 pthread_mutex_t mutex;
70 TrueTypeFontKey *key;
71} TrueTypeFont;
72
73typedef struct {
74 FT_Library ft_library;
75 std::map<TrueTypeFontKey, TrueTypeFont*> fonts;
76 pthread_mutex_t mutex;
77} FontData;
78
79typedef std::map<StringCacheKey, StringCacheEntry*> StringCacheMap;
80typedef std::map<int, TrueTypeCacheEntry*> TrueTypeCacheEntryMap;
81typedef std::map<TrueTypeFontKey, TrueTypeFont*> TrueTypeFontMap;
82
83#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
84#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
85
86// 32bit FNV-1a hash algorithm
87// http://isthe.com/chongo/tech/comp/fnv/#FNV-1a
88static const uint32_t FNV_prime = 16777619U;
89static const uint32_t offset_basis = 2166136261U;
90
91#define STRING_CACHE_MAX_ENTRIES 400
92#define STRING_CACHE_TRUNCATE_ENTRIES 150
93
94class twrpTruetype {
95public:
96 twrpTruetype();
97 static int utf8_to_unicode(const char* pIn, unsigned int *pOut);
98 static void* gr_ttf_loadFont(const char *filename, int size, int dpi);
99 static void* gr_ttf_scaleFont(void *font, int max_width, int measured_width);
100 static void gr_ttf_freeStringCache(void *key, void *value, void *context __unused);
101 static void gr_ttf_freeFont(void *font);
102 static TrueTypeCacheEntry* gr_ttf_glyph_cache_peek(TrueTypeFont *font, int char_index);
103 static TrueTypeCacheEntry* gr_ttf_glyph_cache_get(TrueTypeFont *font, int char_index);
104 static int gr_ttf_copy_glyph_to_surface(GGLSurface *dest, FT_BitmapGlyph glyph, int offX, int offY, int base);
105 static void gr_ttf_calcMaxFontHeight(TrueTypeFont *f);
106 static int gr_ttf_render_text(TrueTypeFont *font, GGLSurface *surface, const std::string text, int max_width);
107 static StringCacheEntry* gr_ttf_string_cache_peek(TrueTypeFont *font, const std::string text, __attribute__((unused)) int max_width);
108 static StringCacheEntry* gr_ttf_string_cache_get(TrueTypeFont *font, const std::string text, int max_width);
109 static int gr_ttf_measureEx(const char *s, void *font);
110 static int gr_ttf_maxExW(const char *s, void *font, int max_width);
111 static int gr_ttf_textExWH(void *context, int x, int y,
112 const char *s, void *pFont,
113 int max_width, int max_height,
114 const gr_surface gr_draw_surface);
115 static int gr_ttf_getMaxFontHeight(void *font);
116 static void gr_ttf_string_cache_truncate(TrueTypeFont *font);
117};
118#endif // _TWRP_TRUETYPE_HPP