reference

This documentation is automatically generated from the openFrameworks source code using doxygen and refers to the most recent release, version 0.12.0.

ofTrueTypeFont.h
Go to the documentation of this file.
1#pragma once
2
3#include "ofRectangle.h"
4#include "ofPath.h"
5#include "ofTexture.h"
6#include "ofMesh.h"
7#include "ofPixels.h"
8#include "ofConstants.h"
9#include <unordered_map>
10
23
24
28typedef struct FT_FaceRec_* FT_Face;
34static const std::string OF_TTF_SANS = "sans-serif";
35static const std::string OF_TTF_SERIF = "serif";
36static const std::string OF_TTF_MONO = "monospace";
43public:
44 struct range{
45 range() : begin(0), end(0) {
47 }
48 range(uint32_t be, uint32_t en) : begin(be), end(en) {
50 }
51 std::uint32_t begin = 0;
52 std::uint32_t end = 0;
54 std::uint32_t getNumGlyphs() const{
55 return end - begin + 1;
56 }
57 };
59 static const range Space;
60 static const range IdeographicSpace;
61 static const range Latin;
62 static const range Latin1Supplement;
63 static const range LatinA;
64 static const range Greek;
65 static const range Cyrillic;
66 static const range Arabic;
67 static const range ArabicSupplement;
68 static const range ArabicExtendedA;
69 static const range Devanagari;
70 static const range HangulJamo;
71 static const range VedicExtensions;
73 static const range GreekExtended;
76 static const range CurrencySymbols;
78 static const range NumberForms;
79 static const range Arrows;
80 static const range MathOperators;
81 static const range MiscTechnical;
82 static const range BoxDrawing;
83 static const range BlockElement;
84 static const range GeometricShapes;
85 static const range MiscSymbols;
86 static const range Dingbats;
87 static const range CJKSymbolAndPunctuation;
88 static const range Hiragana;
89 static const range Katakana;
90 static const range HangulCompatJamo;
91 static const range KatakanaPhoneticExtensions;
92 static const range CJKLettersAndMonths;
93 static const range CJKUnified;
94 static const range DevanagariExtended;
95 static const range HangulExtendedA;
96 static const range HangulSyllables;
97 static const range HangulExtendedB;
98 static const range AlphabeticPresentationForms;
99 static const range ArabicPresFormsA;
100 static const range ArabicPresFormsB;
101 static const range KatakanaHalfAndFullwidthForms;
102 static const range KanaSupplement;
103 static const range RumiNumericalSymbols;
104 static const range ArabicMath;
105 static const range MiscSymbolsAndPictographs;
106 static const range Emoticons;
107 static const range TransportAndMap;
108 static const range EnclosedCharacters;
109 static const range Uncategorized;
110 static const range AdditionalEmoticons;
111 static const range AdditionalTransportAndMap;
112 static const range OtherAdditionalSymbols;
113 static const range Numbers;
114 static const range UppercaseLatin;
115 static const range LowercaseLatin;
116 static const range Braces;
117 static const range Symbols;
118 static const range GenericSymbols;
119};
120
122public:
123 static const std::initializer_list<ofUnicode::range> Emoji;
124 static const std::initializer_list<ofUnicode::range> Japanese;
125 static const std::initializer_list<ofUnicode::range> Chinese;
126 static const std::initializer_list<ofUnicode::range> Korean;
127 static const std::initializer_list<ofUnicode::range> Arabic;
128 static const std::initializer_list<ofUnicode::range> Devanagari;
129 static const std::initializer_list<ofUnicode::range> Latin;
130 static const std::initializer_list<ofUnicode::range> Greek;
131 static const std::initializer_list<ofUnicode::range> Cyrillic;
132};
133
134enum ofTrueTypeFontDirection : uint32_t {
138
140
141 of::filesystem::path fontName;
142 int fontSize = 0;
143 bool antialiased = true;
144 bool contours = false;
145 float simplifyAmt = 0.0f;
146 int dpi = 0;
147 int index = 0;
149 std::vector<ofUnicode::range> ranges;
150
151 ofTrueTypeFontSettings(const of::filesystem::path & name, int size)
152 :fontName(name)
153 ,fontSize(size){}
154
155 void addRanges(std::initializer_list<ofUnicode::range> alphabet){
156 ranges.insert(ranges.end(), alphabet);
157 }
158
159 void addRange(const ofUnicode::range & range){
160 ranges.push_back(range);
161 }
162};
163
165
166public:
167
170
172 virtual ~ofTrueTypeFont();
173
174 ofTrueTypeFont(const ofTrueTypeFont& mom);
176
179
182
199 bool load(const of::filesystem::path& filename,
200 int fontsize,
201 bool _bAntiAliased=true,
202 bool _bFullCharacterSet=true,
203 bool makeContours=false,
204 float simplifyAmt=0.0f,
205 int dpi=0);
206
207 OF_DEPRECATED_MSG("Use load instead",bool loadFont(std::string filename,
208 int fontsize,
209 bool _bAntiAliased=true,
210 bool _bFullCharacterSet=false,
211 bool makeContours=false,
212 float simplifyAmt=0.0f,
213 int dpi=0));
214
215 bool load(const ofTrueTypeFontSettings & settings);
216
219 bool isLoaded() const;
220
224
226 static void setGlobalDpi(int newDpi);
227
230 bool isAntiAliased() const;
231
234 bool hasFullCharacterSet() const;
235
242 std::size_t getNumCharacters() const;
243
247
250 int getSize() const;
251
254 float getLineHeight() const;
255
261 void setLineHeight(float height);
262
270 float getAscenderHeight() const;
271
280 float getDescenderHeight() const;
281
289 const ofRectangle & getGlyphBBox() const;
290
297 float getLetterSpacing() const;
298
304 void setLetterSpacing(float spacing);
305
313 float getSpaceSize() const;
314
324 void setSpaceSize(float size);
325
332 float stringWidth(const std::string& s) const;
333
340 float stringHeight(const std::string& s) const;
341
347 ofRectangle getStringBoundingBox(const std::string& s, float x, float y, bool vflip=true) const;
348
352
357 void drawString(const std::string& s, float x, float y) const;
358
365 void drawStringAsShapes(const std::string& s, float x, float y) const;
366
368 ofPath getCharacterAsPoints(uint32_t character, bool vflip=true, bool filled=true) const;
369 std::vector<ofPath> getStringAsPoints(const std::string & str, bool vflip=true, bool filled=true) const;
370 const ofMesh & getStringMesh(const std::string & s, float x, float y, bool vflip=true) const;
371 const ofTexture & getFontTexture() const;
372 ofTexture getStringTexture(const std::string & s, bool vflip=true) const;
373 glm::vec2 getFirstGlyphPosForTexture(const std::string & str, bool vflip) const;
374 bool isValidGlyph(uint32_t) const;
376
379
380 float getCharWidth(uint32_t c) const {
381 return getGlyphProperties(c).width;
382 }
383 float getCharAdvance(uint32_t c) const {
384 return getGlyphProperties(c).advance;
385 }
386
387 static double int26p6_to_dbl(long p) {
388 return double(p) / 64.0;
389 }
390
391 static inline int dbl_to_int26p6(double p) {
392 return int(p * 64.0 + 0.5);
393 }
394
395protected:
397
398
399
400 bool bLoadedOk;
401
402 std::vector <ofPath> charOutlines;
403 std::vector <ofPath> charOutlinesNonVFlipped;
404 std::vector <ofPath> charOutlinesContour;
405 std::vector <ofPath> charOutlinesNonVFlippedContour;
406
407 float lineHeight;
408 float ascenderHeight;
409 float descenderHeight;
410 ofRectangle glyphBBox;
411 float letterSpacing;
412 float spaceSize;
413 float fontUnitScale;
414
415 struct glyphProps{
416 std::size_t characterIndex;
417 uint32_t glyph;
418 float height;
419 float width;
420 float bearingX, bearingY;
421 float xmin, xmax, ymin, ymax;
422 float advance;
423 float tW,tH;
424 float t1,t2,v1,v2;
425 };
426
427 struct glyph{
428 glyphProps props;
429 ofPixels pixels;
430 };
431
432 std::vector<glyphProps> cps; // properties for each character
433
434 ofTrueTypeFontSettings settings;
435 std::unordered_map<uint32_t,size_t> glyphIndexMap;
436
437 double getKerning(uint32_t leftC, uint32_t rightC) const;
438 void drawChar(uint32_t c, float x, float y, bool vFlipped) const;
439 void drawCharAsShape(uint32_t c, float x, float y, bool vFlipped, bool filled) const;
440 void createStringMesh(const std::string & s, float x, float y, bool vFlipped) const;
441 glyph loadGlyph(uint32_t utf8) const;
442 const glyphProps & getGlyphProperties(uint32_t glyph) const;
443 void iterateString(const std::string & str, float x, float y, bool vFlipped, std::function<void(uint32_t, glm::vec2)> f) const;
444 size_t indexForGlyph(uint32_t glyph) const;
445
446 ofTexture texAtlas;
447 mutable ofMesh stringQuads;
448
450
451private:
452#if defined(TARGET_ANDROID) || defined(TARGET_OF_IOS)
453 friend void ofUnloadAllFontTextures();
454 friend void ofReloadAllFontTextures();
455#endif
456 std::shared_ptr<struct FT_FaceRec_> face;
457 static const glyphProps invalidProps;
458 void unloadTextures();
459 void reloadTextures();
460 static bool initLibraries();
461 static void finishLibraries();
462
463 friend void ofExitCallback();
464};
Definition ofTrueTypeFont.h:121
static const std::initializer_list< ofUnicode::range > Emoji
Definition ofTrueTypeFont.h:86
static const std::initializer_list< ofUnicode::range > Devanagari
Definition ofTrueTypeFont.h:136
static const std::initializer_list< ofUnicode::range > Korean
Definition ofTrueTypeFont.h:116
static const std::initializer_list< ofUnicode::range > Chinese
Definition ofTrueTypeFont.h:108
static const std::initializer_list< ofUnicode::range > Arabic
Definition ofTrueTypeFont.h:127
static const std::initializer_list< ofUnicode::range > Japanese
Definition ofTrueTypeFont.h:97
static const std::initializer_list< ofUnicode::range > Greek
Definition ofTrueTypeFont.h:149
static const std::initializer_list< ofUnicode::range > Cyrillic
Definition ofTrueTypeFont.h:155
static const std::initializer_list< ofUnicode::range > Latin
Definition ofTrueTypeFont.h:142
ofPath is a way to create a path or multiple paths consisting of points. It allows you to combine mul...
Definition ofPath.h:38
A class representing a 2D rectangle.
Definition ofRectangle.h:87
A wrapper class for an OpenGL texture.
Definition ofTexture.h:253
Definition ofTrueTypeFont.h:164
bool isLoaded() const
Has the font been loaded successfully?
Definition ofTrueTypeFont.cpp:896
int getSize() const
Returns the size of the font.
Definition ofTrueTypeFont.cpp:911
glm::vec2 getFirstGlyphPosForTexture(const std::string &str, bool vflip) const
Definition ofTrueTypeFont.cpp:1240
bool hasFullCharacterSet() const
Does the font have a full character set?
Definition ofTrueTypeFont.cpp:906
static void setGlobalDpi(int newDpi)
Set the default dpi for all typefaces.
Definition ofTrueTypeFont.cpp:426
float getAscenderHeight() const
Get the ascender distance for this font.
Definition ofTrueTypeFont.cpp:926
void setLineHeight(float height)
Sets line height for text drawn on screen.
Definition ofTrueTypeFont.cpp:916
bool load(const of::filesystem::path &filename, int fontsize, bool _bAntiAliased=true, bool _bFullCharacterSet=true, bool makeContours=false, float simplifyAmt=0.0f, int dpi=0)
Loads the font specified by filename, allows you to control size, aliasing, and other parameters.
Definition ofTrueTypeFont.cpp:697
float getDescenderHeight() const
Get the descender distance for this font.
Definition ofTrueTypeFont.cpp:931
ofTrueTypeFont()
Construct a default ofTrueTypeFont.
Definition ofTrueTypeFont.cpp:461
float getCharAdvance(uint32_t c) const
Definition ofTrueTypeFont.h:383
void setLetterSpacing(float spacing)
Sets the letter spacing of the font object.
Definition ofTrueTypeFont.cpp:941
bool isAntiAliased() const
Is the font anti-aliased?
Definition ofTrueTypeFont.cpp:901
ofPath getCharacterAsPoints(uint32_t character, bool vflip=true, bool filled=true) const
Definition ofTrueTypeFont.cpp:961
OF_DEPRECATED_MSG("Use load instead", bool loadFont(std::string filename, int fontsize, bool _bAntiAliased=true, bool _bFullCharacterSet=false, bool makeContours=false, float simplifyAmt=0.0f, int dpi=0))
friend void ofExitCallback()
Definition ofAppRunner.cpp:240
float getLineHeight() const
Computes line height based on font size.
Definition ofTrueTypeFont.cpp:921
static double int26p6_to_dbl(long p)
Definition ofTrueTypeFont.h:387
float stringHeight(const std::string &s) const
Returns the string height.
Definition ofTrueTypeFont.cpp:1215
void drawString(const std::string &s, float x, float y) const
Draws a string s at position x,y.
Definition ofTrueTypeFont.cpp:1339
void drawStringAsShapes(const std::string &s, float x, float y) const
Draws the string as if it was geometrical shapes.
Definition ofTrueTypeFont.cpp:1351
float getSpaceSize() const
Returns a variable that represents how wide spaces are.
Definition ofTrueTypeFont.cpp:956
std::size_t getNumCharacters() const
Get the number of characters in the loaded character set.
Definition ofTrueTypeFont.cpp:1369
std::vector< ofPath > getStringAsPoints(const std::string &str, bool vflip=true, bool filled=true) const
Definition ofTrueTypeFont.cpp:1106
float getCharWidth(uint32_t c) const
Definition ofTrueTypeFont.h:380
bool isValidGlyph(uint32_t) const
Definition ofTrueTypeFont.cpp:1121
const ofRectangle & getGlyphBBox() const
Get the global bounding box for this font.
Definition ofTrueTypeFont.cpp:936
ofRectangle getStringBoundingBox(const std::string &s, float x, float y, bool vflip=true) const
Returns the bounding box of a string as a rectangle.
Definition ofTrueTypeFont.cpp:1173
static int dbl_to_int26p6(double p)
Definition ofTrueTypeFont.h:391
ofTexture getStringTexture(const std::string &s, bool vflip=true) const
Definition ofTrueTypeFont.cpp:1280
const ofMesh & getStringMesh(const std::string &s, float x, float y, bool vflip=true) const
Definition ofTrueTypeFont.cpp:1228
const ofTexture & getFontTexture() const
Definition ofTrueTypeFont.cpp:1235
ofTrueTypeFont & operator=(const ofTrueTypeFont &mom)
Definition ofTrueTypeFont.cpp:513
float stringWidth(const std::string &s) const
Returns the string width.
Definition ofTrueTypeFont.cpp:1159
float getLetterSpacing() const
Returns letter spacing of font object.
Definition ofTrueTypeFont.cpp:946
virtual ~ofTrueTypeFont()
Destroy the ofTrueTypeFont.
Definition ofTrueTypeFont.cpp:474
void setDirection(ofTrueTypeFontDirection direction)
Definition ofTrueTypeFont.cpp:1101
void setSpaceSize(float size)
Sets the width for the whitespace character for this font.
Definition ofTrueTypeFont.cpp:951
Definition ofTrueTypeFont.h:42
static const range IdeographicSpace
Definition ofTrueTypeFont.h:26
static const range LatinExtendedAdditional
Definition ofTrueTypeFont.h:38
static const range LatinA
Definition ofTrueTypeFont.h:29
static const range Greek
Definition ofTrueTypeFont.h:30
static const range NumberForms
Definition ofTrueTypeFont.h:44
static const range KatakanaHalfAndFullwidthForms
Definition ofTrueTypeFont.h:67
static const range SuperAndSubScripts
Definition ofTrueTypeFont.h:41
static const range AlphabeticPresentationForms
Definition ofTrueTypeFont.h:64
static const range Cyrillic
Definition ofTrueTypeFont.h:31
static const range Latin
Definition ofTrueTypeFont.h:27
static const range MiscSymbolsAndPictographs
Definition ofTrueTypeFont.h:71
static const range Latin1Supplement
Definition ofTrueTypeFont.h:28
static const range Katakana
Definition ofTrueTypeFont.h:55
static const range CurrencySymbols
Definition ofTrueTypeFont.h:42
static const range HangulExtendedA
Definition ofTrueTypeFont.h:61
static const range AdditionalTransportAndMap
Definition ofTrueTypeFont.h:77
static const range CJKLettersAndMonths
Definition ofTrueTypeFont.h:58
static const range CJKUnified
Definition ofTrueTypeFont.h:59
static const range HangulExtendedB
Definition ofTrueTypeFont.h:63
static const range LetterLikeSymbols
Definition ofTrueTypeFont.h:43
static const range Arabic
Definition ofTrueTypeFont.h:32
static const range Emoticons
Definition ofTrueTypeFont.h:72
static const range KanaSupplement
Definition ofTrueTypeFont.h:68
static const range ArabicMath
Definition ofTrueTypeFont.h:70
static const range ArabicPresFormsB
Definition ofTrueTypeFont.h:66
static const range GeneralPunctuation
Definition ofTrueTypeFont.h:40
static const range DevanagariExtended
Definition ofTrueTypeFont.h:60
static const range Devanagari
Definition ofTrueTypeFont.h:35
static const range KatakanaPhoneticExtensions
Definition ofTrueTypeFont.h:57
static const range LowercaseLatin
Definition ofTrueTypeFont.h:80
static const range CJKSymbolAndPunctuation
Definition ofTrueTypeFont.h:53
static const range Space
Definition ofTrueTypeFont.h:25
static const range GeometricShapes
Definition ofTrueTypeFont.h:50
static const range EnclosedCharacters
Definition ofTrueTypeFont.h:74
static const range AdditionalEmoticons
Definition ofTrueTypeFont.h:76
static const range HangulSyllables
Definition ofTrueTypeFont.h:62
static const range Numbers
Definition ofTrueTypeFont.h:82
static const range VedicExtensions
Definition ofTrueTypeFont.h:37
static const range ArabicPresFormsA
Definition ofTrueTypeFont.h:65
static const range UppercaseLatin
Definition ofTrueTypeFont.h:79
static const range ArabicSupplement
Definition ofTrueTypeFont.h:33
static const range Uncategorized
Definition ofTrueTypeFont.h:75
static const range MathOperators
Definition ofTrueTypeFont.h:46
static const range Hiragana
Definition ofTrueTypeFont.h:54
static const range TransportAndMap
Definition ofTrueTypeFont.h:73
static const range Arrows
Definition ofTrueTypeFont.h:45
static const range RumiNumericalSymbols
Definition ofTrueTypeFont.h:69
static const range Dingbats
Definition ofTrueTypeFont.h:52
static const range MiscTechnical
Definition ofTrueTypeFont.h:47
static const range MiscSymbols
Definition ofTrueTypeFont.h:51
static const range BlockElement
Definition ofTrueTypeFont.h:49
static const range HangulCompatJamo
Definition ofTrueTypeFont.h:56
static const range Braces
Definition ofTrueTypeFont.h:81
static const range HangulJamo
Definition ofTrueTypeFont.h:36
static const range ArabicExtendedA
Definition ofTrueTypeFont.h:34
static const range Symbols
Definition ofTrueTypeFont.h:83
static const range GenericSymbols
Definition ofTrueTypeFont.h:84
static const range GreekExtended
Definition ofTrueTypeFont.h:39
static const range OtherAdditionalSymbols
Definition ofTrueTypeFont.h:78
static const range BoxDrawing
Definition ofTrueTypeFont.h:48
unsigned int height
Definition ofAppEGLWindow.cpp:125
unsigned int width
Definition ofAppEGLWindow.cpp:124
#define c
ofTrueTypeFontDirection
Definition ofTrueTypeFont.h:134
@ OF_TTF_RIGHT_TO_LEFT
Definition ofTrueTypeFont.h:136
@ OF_TTF_LEFT_TO_RIGHT
Definition ofTrueTypeFont.h:135
void ofTrueTypeShutdown()
Definition ofTrueTypeFont.cpp:180
Definition ofTrueTypeFont.h:139
ofTrueTypeFontDirection direction
Definition ofTrueTypeFont.h:148
bool antialiased
Definition ofTrueTypeFont.h:143
void addRange(const ofUnicode::range &range)
Definition ofTrueTypeFont.h:159
void addRanges(std::initializer_list< ofUnicode::range > alphabet)
Definition ofTrueTypeFont.h:155
bool contours
Definition ofTrueTypeFont.h:144
int dpi
Definition ofTrueTypeFont.h:146
ofTrueTypeFontSettings(const of::filesystem::path &name, int size)
Definition ofTrueTypeFont.h:151
float simplifyAmt
Definition ofTrueTypeFont.h:145
std::vector< ofUnicode::range > ranges
Definition ofTrueTypeFont.h:149
int index
Definition ofTrueTypeFont.h:147
int fontSize
Definition ofTrueTypeFont.h:142
of::filesystem::path fontName
Definition ofTrueTypeFont.h:141
Definition ofTrueTypeFont.h:44
range()
Definition ofTrueTypeFont.h:45
std::uint32_t begin
Definition ofTrueTypeFont.h:51
std::uint32_t end
Definition ofTrueTypeFont.h:52
std::uint32_t getNumGlyphs() const
Definition ofTrueTypeFont.h:54