STLL  0.0
Simple Text Layouting Library
layouterFont.h
Go to the documentation of this file.
1 /*
2  * STLL Simple Text Layouting Library
3  *
4  * STLL is the legal property of its developers, whose
5  * names are listed in the COPYRIGHT file, which is included
6  * within the source distribution.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1
11  * of the License, or (at your option) any later version.
12 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */
22 #ifndef STLL_LAYOUTER_FONT_H
23 #define STLL_LAYOUTER_FONT_H
24 
31 #include "internal/layouterFont_internal.h"
32 
33 #include <boost/utility.hpp>
34 
35 #include <string>
36 #include <memory>
37 #include <map>
38 #include <vector>
39 
40 #include <stdint.h>
41 #include <stdexcept>
42 
43 struct FT_FaceRec_;
44 struct FT_LibraryRec_;
45 struct FT_GlyphSlotRec_;
46 
47 namespace STLL {
48 
53 typedef uint32_t glyphIndex_t;
54 
55 
59 typedef enum
60 {
67 
70 class FreetypeException_c : public std::runtime_error
71 {
72  public:
73  explicit FreetypeException_c(const std::string & what_arg) : std::runtime_error(what_arg) {}
74 };
75 
76 class FreeTypeLibrary_c;
77 
88 {
89  private:
90  std::vector<internal::FontFileResource_c> resources;
91 
92  public:
95  template <typename... Args>
96  FontResource_c(Args... par) { addFont(par...); }
97 
102  void addFont(const std::string & pathname)
103  {
104  resources.emplace_back(internal::FontFileResource_c(pathname));
105  };
106 
113  void addFont(std::pair<std::shared_ptr<uint8_t>, size_t> data, const std::string & descr)
114  {
115  resources.emplace_back(internal::FontFileResource_c(data.first, data.second, descr));
116  }
117 
120  size_t size(void) const { return resources.size(); }
121 
123  auto begin(void) const { return resources.begin(); }
124  auto end(void) const { return resources.end(); }
125 
130  FontResource_c(void) {}
131 
134  internal::FontFileResource_c getRessource(size_t idx) const { return resources[idx]; }
135 
137  bool operator<(const FontResource_c & b) const
138  {
139  return std::lexicographical_compare(resources.begin(), resources.end(), b.resources.begin(), b.resources.end());
140  }
141 };
142 
145 class FontFace_c : boost::noncopyable
146 {
147  public:
148 
150  {
151  public:
152  int w, h;
153  int top, left;
154  int pitch;
155  const uint8_t * data;
156 
157  GlyphSlot_c(const FT_GlyphSlotRec_ * g);
158  GlyphSlot_c(int width, int height) : w(width), h(height), top(0), left(0), pitch(0), data(0) {}
159  };
160 
161  FontFace_c(std::shared_ptr<FreeTypeLibrary_c> l, const internal::FontFileResource_c & r, uint32_t size);
162  ~FontFace_c();
163 
168  FT_FaceRec_ * getFace(void) const { return f; }
169 
175  uint32_t getSize(void) const { return size; }
176 
179  const internal::FontFileResource_c & getResource(void) const { return rec; }
180 
184  uint32_t getHeight(void) const;
185 
189  int32_t getAscender(void) const;
190 
194  int32_t getDescender(void) const;
195 
199  int32_t getUnderlinePosition(void) const;
200 
204  int32_t getUnderlineThickness(void) const;
213  GlyphSlot_c renderGlyph(glyphIndex_t glyphIndex, SubPixelArrangement sp);
214 
219  bool containsGlyph(char32_t ch);
220 
221  private:
222  FT_FaceRec_ *f;
223  std::shared_ptr<FreeTypeLibrary_c> lib;
224  internal::FontFileResource_c rec;
225  uint32_t size;
226 };
227 
232 class Font_c
233 {
234  public:
235 
236  Font_c(void) {}
237 
239  void add(std::shared_ptr<FontFace_c> f) { fonts.emplace_back(std::move(f)); }
240 
242  auto begin(void) const { return fonts.begin(); }
243  auto end(void) const { return fonts.end(); }
244 
247  std::shared_ptr<FontFace_c> get(char32_t codepoint) const;
248 
249  // some functions that get metrics of this font, they are always taken from the
250  // first font face in the font
251  uint32_t getHeight(void) const { return fonts[0]->getHeight(); }
252  int32_t getAscender(void) const { return fonts[0]->getAscender(); }
253  int32_t getDescender(void) const { return fonts[0]->getDescender(); }
254  int32_t getUnderlinePosition(void) const { return fonts[0]->getUnderlinePosition(); }
255  int32_t getUnderlineThickness(void) const { return fonts[0]->getUnderlineThickness(); }
256 
258  explicit operator bool() const { return fonts.size() > 0; }
259 
261  bool operator==(const Font_c & rhs) const { return std::equal(fonts.begin(), fonts.end(), rhs.fonts.begin(), rhs.fonts.end()); }
262 
263  private:
264  std::vector<std::shared_ptr<FontFace_c>> fonts;
265 };
266 
275 class FreeTypeLibrary_c : boost::noncopyable
276 {
277  public:
278 
283 
292  FT_FaceRec_ * newFace(const internal::FontFileResource_c & r, uint32_t size);
293 
300  void doneFace(FT_FaceRec_ * f);
301 
302  private:
303 
304  FT_LibraryRec_ *lib;
305 };
306 
311 {
312  public:
313 
319  FontCache_c(std::shared_ptr<FreeTypeLibrary_c> l) : lib(l) {}
320 
327  FontCache_c(void) : lib(std::make_shared<FreeTypeLibrary_c>()) {}
328 
339  Font_c getFont(const FontResource_c & res, uint32_t size);
340 
341  std::shared_ptr<FontFace_c> getFont(const internal::FontFileResource_c & res, uint32_t size);
342 
346  void clear(void)
347  {
348  for(auto it = fonts.begin(); it != fonts.end(); )
349  {
350  if(it->second.use_count() == 1)
351  {
352  it = fonts.erase(it);
353  }
354  else
355  {
356  ++it;
357  }
358  }
359  }
360 
361  private:
362 
363  class FontFaceParameter_c
364  {
365  public:
366  internal::FontFileResource_c res;
367  uint32_t size;
368 
369  FontFaceParameter_c(const internal::FontFileResource_c r, uint32_t s) : res(std::move(r)), size(s) {}
370 
371  bool operator<(const FontFaceParameter_c & b) const
372  {
373  if (res < b.res) return true;
374  if (b.res < res) return false;
375 
376  if (size < b.size) return true;
377  return false;
378  }
379  };
380 
381  // all open fonts, used to check whether they have all been released
382  // on library destruction
383  std::map<FontFaceParameter_c, std::shared_ptr<FontFace_c> > fonts;
384 
385  // the library to use
386  std::shared_ptr<FreeTypeLibrary_c> lib;
387 };
388 
399 {
400  private:
401 
402  // a class containing all the variables for a
403  // font inside a family, it is used as the key inside the map
404  // for all font files
405  class FontFamilyParameter_c
406  {
407  public:
408  std::string style;
409  std::string variant;
410  std::string weight;
411  std::string stretch;
412 
413  bool operator<(const FontFamilyParameter_c & b) const
414  {
415  if (style < b.style) return true;
416  if (style > b.style) return false;
417  if (variant < b.variant) return true;
418  if (variant > b.variant) return false;
419  if (weight < b.weight) return true;
420  if (weight > b.weight) return false;
421  if (stretch < b.stretch) return true;
422  return false;
423  }
424  };
425 
426  public:
427 
430  FontFamily_c(std::shared_ptr<FontCache_c> c) : cache(c) {}
431 
438  FontFamily_c(void) : cache(std::make_shared<FontCache_c>()) {}
439 
449  Font_c getFont(uint32_t size,
450  const std::string & style = "normal",
451  const std::string & variant = "normal",
452  const std::string & weight = "normal",
453  const std::string & stretch = "normal");
454 
463  void addFont(const FontResource_c & res,
464  const std::string & style = "normal",
465  const std::string & variant = "normal",
466  const std::string & weight = "normal",
467  const std::string & stretch = "normal");
468 
469  private:
470  std::map<FontFamilyParameter_c, FontResource_c> fonts;
471  std::shared_ptr<FontCache_c> cache;
472 };
473 
474 
475 }
476 
477 #endif
int h
Definition: layouterFont.h:152
int left
Definition: layouterFont.h:153
auto end(void) const
Definition: layouterFont.h:243
Definition: layouterFont.h:149
This class represents a font resource.
Definition: layouterFont.h:87
int32_t getUnderlineThickness(void) const
Definition: layouterFont.h:255
uint32_t glyphIndex_t
type used for all glyph indices. Right now there is no font with more than 2^16 fonts, so 2^32 should be on the safe side. Also HarfBuzz also uses only 2^32 codepoints.
Definition: layouterFont.h:53
bool operator<(const FontResource_c &b) const
Definition: layouterFont.h:137
int pitch
Definition: layouterFont.h:154
Font_c getFont(const FontResource_c &res, uint32_t size)
Get a font face from this cache with the given resource and size.
contains all the FontFaces_c of one FontRessource_c
Definition: layouterFont.h:232
a class contains all resources for a family of fonts
Definition: layouterFont.h:398
int32_t getDescender(void) const
Get the descender of the font with multiplication factor of 64.
auto begin(void) const
Definition: layouterFont.h:242
FontResource_c(void)
Definition: layouterFont.h:130
FT_FaceRec_ * newFace(const internal::FontFileResource_c &r, uint32_t size)
Font_c getFont(uint32_t size, const std::string &style="normal", const std::string &variant="normal", const std::string &weight="normal", const std::string &stretch="normal")
Get a font instance from the family.
Font_c(void)
Definition: layouterFont.h:236
internal::FontFileResource_c getRessource(size_t idx) const
Definition: layouterFont.h:134
use vertical BGR (top to bottom)
Definition: layouterFont.h:65
FontFamily_c(void)
Initialize an empty family, using font cache created specifically for this family.
Definition: layouterFont.h:438
FontResource_c(Args...par)
Definition: layouterFont.h:96
void doneFace(FT_FaceRec_ *f)
uint32_t getHeight(void) const
Definition: layouterFont.h:251
int32_t getUnderlinePosition(void) const
Get the underline position of the font with multiplication factor of 64.
use horizontal RGB
Definition: layouterFont.h:62
FontFamily_c(std::shared_ptr< FontCache_c > c)
Initialize an empty family, using the given font cache to get the fonts.
Definition: layouterFont.h:430
GlyphSlot_c(const FT_GlyphSlotRec_ *g)
uint32_t getHeight(void) const
Get the height of the font with multiplication factor of 64.
bool containsGlyph(char32_t ch)
check if a given character is available within this font
this class encapsulates open fonts of a single library, it makes sure that each font is open only onc...
Definition: layouterFont.h:310
FreetypeException_c(const std::string &what_arg)
Definition: layouterFont.h:73
void addFont(std::pair< std::shared_ptr< uint8_t >, size_t > data, const std::string &descr)
Definition: layouterFont.h:113
SubPixelArrangement
define, which sub-pixel arrangement you want to use for sub-pixel output
Definition: layouterFont.h:59
use horizontal BGR
Definition: layouterFont.h:63
FontCache_c(std::shared_ptr< FreeTypeLibrary_c > l)
Create a cache using a specific library.
Definition: layouterFont.h:319
FontFace_c(std::shared_ptr< FreeTypeLibrary_c > l, const internal::FontFileResource_c &r, uint32_t size)
FontCache_c(void)
Create a cache using an instance of the FreeType library that is created specifically for this cache ...
Definition: layouterFont.h:327
auto begin(void) const
Definition: layouterFont.h:123
const uint8_t * data
Definition: layouterFont.h:155
int w
Definition: layouterFont.h:152
GlyphSlot_c renderGlyph(glyphIndex_t glyphIndex, SubPixelArrangement sp)
render a glyph of this font
int32_t getUnderlineThickness(void) const
Get the underline thickness of the font with multiplication factor of 64.
FT_FaceRec_ * getFace(void) const
Get the FreeType structure for this font.
Definition: layouterFont.h:168
The namespace for the library. Every function and class is within this namespace. ...
Definition: color.h:31
FreeTypeLibrary_c()
Create an instance of the FreeType library.
size_t size(void) const
Definition: layouterFont.h:120
int32_t getAscender(void) const
Definition: layouterFont.h:252
void addFont(const FontResource_c &res, const std::string &style="normal", const std::string &variant="normal", const std::string &weight="normal", const std::string &stretch="normal")
Add a font to the family.
void addFont(const std::string &pathname)
Definition: layouterFont.h:102
auto end(void) const
Definition: layouterFont.h:124
uint32_t getSize(void) const
get the size of the font
Definition: layouterFont.h:175
int top
Definition: layouterFont.h:153
bool operator==(const Font_c &rhs) const
Definition: layouterFont.h:261
int32_t getDescender(void) const
Definition: layouterFont.h:253
use vertical RGB (top to bottom)
Definition: layouterFont.h:64
GlyphSlot_c(int width, int height)
Definition: layouterFont.h:158
This class is thrown on problems with the FreeType library.
Definition: layouterFont.h:70
This class represents one font, made out of one font file resource with a certain size...
Definition: layouterFont.h:145
const internal::FontFileResource_c & getResource(void) const
get the font resource that was used to create this font
Definition: layouterFont.h:179
This class encapsulates an instance of the FreeType library.
Definition: layouterFont.h:275
don't use sub-pixel output (e.g. non LCD)
Definition: layouterFont.h:61
int32_t getAscender(void) const
Get the ascender of the font with multiplication factor of 64.
~FreeTypeLibrary_c()
Destroy the library instance.
void add(std::shared_ptr< FontFace_c > f)
Definition: layouterFont.h:239
int32_t getUnderlinePosition(void) const
Definition: layouterFont.h:254
void clear(void)
remove all fonts from the cache, fonts that are still in use will be kept, but all others are removed...
Definition: layouterFont.h:346