DUDS
Distributed Update of Data from Something
BppFontPool.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of the DUDS project. It is subject to the BSD-style
3  * license terms in the LICENSE file found in the top-level directory of this
4  * distribution and at https://github.com/jjackowski/duds/blob/master/LICENSE.
5  * No part of DUDS, including this file, may be copied, modified, propagated,
6  * or distributed except according to the terms contained in the LICENSE file.
7  *
8  * Copyright (C) 2023 Jeff Jackowski
9  */
10 #ifndef BPPFONTPOOL_HPP
11 #define BPPFONTPOOL_HPP
14 
15 namespace duds { namespace ui { namespace graphics {
16 
23 class BppFontPool : boost::noncopyable {
27  struct FontAndCache {
30  };
34  std::unordered_map<std::string, FontAndCache> fonts;
45  void getFc(FontAndCache &fc, const std::string &font) const;
46 public:
56  void add(
57  const std::string &name,
58  const BppFontSptr &font,
59  const BppStringCacheSptr &cache
60  );
71  void add(
72  const std::string &name,
73  BppFontSptr &&font,
74  BppStringCacheSptr &&cache
75  );
82  void addWithoutCache(const std::string &name, const BppFontSptr &font);
90  void addWithoutCache(const std::string &name, const std::string &fontpath);
98  void addWithCache(const std::string &name, const BppFontSptr &font);
106  void addWithCache(const std::string &name, const std::string &fontpath);
115  void alias(const std::string &existing, const std::string &newname);
123  BppFontSptr getFont(const std::string &font) const;
131  BppStringCacheSptr getStringCache(const std::string &font) const;
143  template <class String>
145  const std::string &font,
146  const String &str,
148  ) const {
149  FontAndCache fc;
150  getFc(fc, font);
151  try {
152  return fc.fnt->render(str, flags);
153  } catch (boost::exception &be) {
154  be << FontName(font);
155  throw;
156  }
157  }
169  template <class String>
171  const std::string &font,
172  const String &str,
174  ) const {
175  FontAndCache fc;
176  getFc(fc, font);
177  try {
178  if (fc.sc) {
179  return fc.sc->text(str, flags);
180  }
181  return fc.fnt->render(str, flags);
182  } catch (boost::exception &be) {
183  be << FontName(font);
184  throw;
185  }
186  }
187 };
188 
189 } } }
190 
191 #endif // #ifndef BPPFONTPOOL_HPP
std::shared_ptr< BppStringCache > BppStringCacheSptr
A std::shared_ptr to a BppStringCache object.
std::shared_ptr< BppFont > BppFontSptr
Definition: BppFont.hpp:263
std::shared_ptr< const BppImage > ConstBppImageSptr
Definition: BppImage.hpp:1778
void addWithoutCache(const std::string &name, const BppFontSptr &font)
Adds an existing font without a corresponding string cache.
Definition: BppFontPool.cpp:45
Handles a pool of fonts and their associated string caches to make it easier to use fonts across vari...
Definition: BppFontPool.hpp:23
void alias(const std::string &existing, const std::string &newname)
Adds a new name for an already added font.
Definition: BppFontPool.cpp:91
BppFontSptr getFont(const std::string &font) const
Returns a shared pointer to a stored font, or an empty shared pointer if the font is not present...
void getFc(FontAndCache &fc, const std::string &font) const
An internal function to get a FontAndCache struct for the given font.
BppImageSptr render(const std::string &font, const String &str, BppFont::Flags flags=BppFont::AlignLeft) const
Renders text without going through a string cache.
std::unordered_map< std::string, FontAndCache > fonts
The glyph images keyed by character.
Definition: BppFontPool.hpp:34
boost::error_info< struct Info_String, std::string > String
A string, like one requested for rendering in a specific font.
std::shared_ptr< BppImage > BppImageSptr
Definition: BppImage.hpp:1777
ConstBppImageSptr text(const std::string &font, const String &str, BppFont::Flags flags=BppFont::AlignLeft) const
Gets text from a string cache if present, or renders from the font otherwise.
void addWithCache(const std::string &name, const BppFontSptr &font)
Adds an existing font along with a newly created corresponding string cache.
Definition: BppFontPool.cpp:66
duds::general::Spinlock block
Used for thread safety.
Definition: BppFontPool.hpp:38
Internal struct to hold a font and its string cache.
Definition: BppFontPool.hpp:27
void add(const std::string &name, const BppFontSptr &font, const BppStringCacheSptr &cache)
Adds an existing font and string cache pair to the pool.
Definition: BppFontPool.cpp:15
A simple spinlock following the lockable and timed lockable concepts so that it can be used with std:...
Definition: Spinlock.hpp:52
General graphics related code.
Definition: HD44780.hpp:15
static constexpr Flags AlignLeft
Align each line to the left.
Definition: BppFont.hpp:184
boost::error_info< struct Info_FontName, std::string > FontName
The name of the font and is not present in the font pool.
BppStringCacheSptr getStringCache(const std::string &font) const
Returns a shared pointer to a string cache, or an empty shared pointer if the font is not present...