NAP
font.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // Local Includes
8 #include "glyph.h"
9 
10 // External Includes
11 #include <nap/resource.h>
12 #include <rtti/factory.h>
13 #include <utility>
14 #include <utility/errorstate.h>
15 #include <rect.h>
16 
17 // Forward Declares
18 namespace nap
19 {
20  class FontService;
21  class FontInstance;
22  class GlyphCache;
23 }
24 
25 
26 namespace nap
27 {
28  namespace font
29  {
30  inline constexpr float dpi = 96.0f;
31  }
32 
33 
38  struct NAPAPI FontProperties
39  {
43  FontProperties() = default;
44 
48  FontProperties(int size) : mSize(size) { }
49 
50  int mSize = 12;
51  };
52 
53 
55  // Font Resource
57 
58 
64  class NAPAPI Font : public Resource
65  {
66  RTTI_ENABLE(Resource)
67  public:
71  Font(FontService& service);
72 
73  // Destructor
74  virtual ~Font();
75 
81  virtual bool init(utility::ErrorState& errorState) override;
82 
87  FontInstance& getFontInstance();
88 
93  const FontInstance& getFontInstance() const;
94 
96  std::string mFont;
97 
98  private:
99  FontService* mService = nullptr;
100  std::unique_ptr<FontInstance> mInstance = nullptr;
101  };
102 
103  // Object creator used for constructing the the OSC receiver
105 
106 
108  // Font Instance
110 
111 
121  class NAPAPI FontInstance final
122  {
123  RTTI_ENABLE()
124  public:
128  FontInstance(FontService& service);
129 
133  ~FontInstance();
134 
138  FontInstance(const FontInstance& rhs) = delete;
139  FontInstance& operator=(const FontInstance& rhs) = delete;
140 
152  bool create(const std::string& font, FontProperties& properties, utility::ErrorState& error);
153 
157  const FontProperties& getProperties() const;
158 
162  const std::string& getFont() const;
163 
167  bool isValid() const;
168 
174  nap::uint getGlyphIndex(nap::uint character) const;
175 
188  template<typename T>
189  T* getOrCreateGlyphRepresentation(nap::uint index, float scale, utility::ErrorState& errorCode);
190 
204  IGlyphRepresentation* getOrCreateGlyphRepresentation(nap::uint index, float scale, const rtti::TypeInfo& type, utility::ErrorState& errorCode);
205 
214  const Glyph* getOrCreateGlyph(nap::uint index, float scale, utility::ErrorState& errorCode);
215 
224  void getBoundingBox(const std::string& text, float scale, math::Rect& outRect);
225 
230  int getCount();
231 
232  protected:
238  void* getFace() const;
239 
240  private:
246  bool loadGlyph(uint index);
247 
252  void* getGlyphHandle();
253 
258  void resetCache();
259 
268  GlyphCache* getOrCreateGlyphCache(nap::uint index, float scale, utility::ErrorState& errorCode);
269 
270 
271  void* mFace = nullptr;
272  void* mFreetypeLib = nullptr;
273  FontProperties mProperties = { -1 };
274  std::string mFont;
275  FontService* mService;
276 
277  using GlyphCacheSet = std::vector<std::unique_ptr<GlyphCache>>;
278  using GlyphCacheMap = std::unordered_map<int, GlyphCacheSet>;
279  mutable GlyphCacheMap mGlyphs;
280  };
281 
282 
284  // Glyph Cache
286 
291  class NAPAPI GlyphCache final
292  {
293  friend FontInstance;
294  public:
295  ~GlyphCache();
296 
297  // Copy is not allowed
298  GlyphCache(const GlyphCache& other) = delete;
299  GlyphCache& operator=(const GlyphCache&) = delete;
300 
301  // Move is not allowed
302  GlyphCache(GlyphCache&& other) = delete;
303  GlyphCache& operator=(GlyphCache&& other) = delete;
304 
308  IGlyphRepresentation* findRepresentation(const rtti::TypeInfo& type);
309 
313  template<typename T>
314  T* findRepresentation();
315 
319  const Glyph& getGlyph() const { return *mGlyph; }
320 
321  protected:
326  GlyphCache(std::unique_ptr<Glyph> parent) :
327  mGlyph(std::move(parent)) { }
328 
333  void addRepresentation(std::unique_ptr<IGlyphRepresentation> representation);
334 
335  private:
336  std::unique_ptr<Glyph> mGlyph = nullptr;
337  using GlyphRepresentationMap = std::unordered_map<rtti::TypeInfo, std::unique_ptr<IGlyphRepresentation>>;
338  GlyphRepresentationMap mRepresentations;
339  };
340 
341 
343  // Template definitions
345 
346  template<typename T>
348  {
349  IGlyphRepresentation* representation = this->getOrCreateGlyphRepresentation(index, scale, RTTI_OF(T), errorCode);
350  return rtti_cast<T>(representation);
351  }
352 
353 
354  template<typename T>
356  {
357  IGlyphRepresentation* presentation = findRepresentation(RTTI_OF(T));
358  return rtti_cast<T>(presentation);
359  }
360 }
nap::uint
unsigned int uint
Definition: numeric.h:23
nap::GlyphCache
Definition: font.h:291
nap::Font::mProperties
FontProperties mProperties
Property: 'Properties' the properties (size, dpi, path) that describe the font.
Definition: font.h:95
nap::GlyphCache::GlyphCache
GlyphCache(std::unique_ptr< Glyph > parent)
Definition: font.h:326
nap::math::Rect
Definition: rect.h:19
nap::IGlyphRepresentation
Definition: glyph.h:97
nap::rtti::ObjectCreator
Definition: factory.h:49
nap::font::dpi
constexpr float dpi
Default (reference) dpi for font elements.
Definition: font.h:30
nap::utility::ErrorState
Definition: errorstate.h:19
nap::FontProperties
Definition: font.h:38
nap::GlyphCache::getGlyph
const Glyph & getGlyph() const
Definition: font.h:319
nap::Glyph
Definition: glyph.h:24
nap::GlyphCache::findRepresentation
T * findRepresentation()
Definition: font.h:355
nap::FontInstance::getOrCreateGlyphRepresentation
T * getOrCreateGlyphRepresentation(nap::uint index, float scale, utility::ErrorState &errorCode)
Definition: font.h:347
nap::Font
Definition: font.h:64
nap
Definition: templateapp.h:17
nap::Resource
Definition: resource.h:19
nap::rtti::TypeInfo
rttr::type TypeInfo
Definition: typeinfo.h:140
nap::FontService
Definition: fontservice.h:18
nap::FontProperties::FontProperties
FontProperties(int size)
Definition: font.h:48
nap::Font::mFont
std::string mFont
Property: 'Font' path to the font on disk.
Definition: font.h:96
nap::FontInstance
Definition: font.h:121