DUDS
Distributed Update of Data from Something
BppFontPool.cpp
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  */
11 #include <duds/general/Errors.hpp>
12 
13 namespace duds { namespace ui { namespace graphics {
14 
16  const std::string &name,
17  const BppFontSptr &font,
18  const BppStringCacheSptr &cache
19 ) {
20  if (!font) {
22  }
23  if (cache && (cache->font() != font)) {
25  }
26  std::lock_guard<duds::general::Spinlock> lock(block);
27  fonts.emplace(name, FontAndCache{font, cache});
28 }
29 
31  const std::string &name,
32  BppFontSptr &&font,
33  BppStringCacheSptr &&cache
34 ) {
35  if (!font) {
37  }
38  if (cache && (cache->font() != font)) {
40  }
41  std::lock_guard<duds::general::Spinlock> lock(block);
42  fonts.emplace(name, FontAndCache{std::move(font), std::move(cache)});
43 }
44 
46  const std::string &name,
47  const BppFontSptr &font
48 ) {
49  if (!font) {
51  }
52  std::lock_guard<duds::general::Spinlock> lock(block);
53  fonts.emplace(name, FontAndCache{font, BppStringCacheSptr()});
54 }
55 
57  const std::string &name,
58  const std::string &fontpath
59 ) {
60  FontAndCache fc;
61  fc.fnt = BppFont::make(fontpath);
62  std::lock_guard<duds::general::Spinlock> lock(block);
63  fonts.emplace(name, std::move(fc));
64 }
65 
67  const std::string &name,
68  const BppFontSptr &font
69 ) {
70  if (!font) {
72  }
73  FontAndCache fc;
74  fc.fnt = font;
75  fc.sc = BppStringCache::make(fc.fnt);
76  std::lock_guard<duds::general::Spinlock> lock(block);
77  fonts.emplace(name, std::move(fc));
78 }
79 
81  const std::string &name,
82  const std::string &fontpath
83 ) {
84  FontAndCache fc;
85  fc.fnt = BppFont::make(fontpath);
86  fc.sc = BppStringCache::make(fc.fnt);
87  std::lock_guard<duds::general::Spinlock> lock(block);
88  fonts.emplace(name, std::move(fc));
89 }
90 
92  const std::string &existing,
93  const std::string &newname
94 ) {
95  std::lock_guard<duds::general::Spinlock> lock(block);
96  std::unordered_map<std::string, FontAndCache>::const_iterator iter =
97  fonts.find(existing);
98  if (iter == fonts.cend()) {
100  }
101  fonts.emplace(newname, FontAndCache{iter->second.fnt, iter->second.sc});
102 }
103 
104 BppFontSptr BppFontPool::getFont(const std::string &font) const {
105  std::lock_guard<duds::general::Spinlock> lock(block);
106  std::unordered_map<std::string, FontAndCache>::const_iterator iter =
107  fonts.find(font);
108  if (iter == fonts.cend()) {
109  return BppFontSptr();
110  }
111  return iter->second.fnt;
112 }
113 
114 BppStringCacheSptr BppFontPool::getStringCache(const std::string &font) const {
115  std::lock_guard<duds::general::Spinlock> lock(block);
116  std::unordered_map<std::string, FontAndCache>::const_iterator iter =
117  fonts.find(font);
118  if (iter == fonts.cend()) {
119  return BppStringCacheSptr();
120  }
121  return iter->second.sc;
122 }
123 
124 void BppFontPool::getFc(FontAndCache &fc, const std::string &font) const {
125  std::lock_guard<duds::general::Spinlock> lock(block);
126  std::unordered_map<std::string, FontAndCache>::const_iterator iter =
127  fonts.find(font);
128  if (iter == fonts.cend()) {
130  }
131  fc = iter->second;
132 }
133 
134 } } }
std::shared_ptr< BppStringCache > BppStringCacheSptr
A std::shared_ptr to a BppStringCache object.
std::shared_ptr< BppFont > BppFontSptr
Definition: BppFont.hpp:263
void addWithoutCache(const std::string &name, const BppFontSptr &font)
Adds an existing font without a corresponding string cache.
Definition: BppFontPool.cpp:45
move_impl move(unsigned int c, unsigned int r)
Display stream manipulator that moves the display cursor to the given location.
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...
A specified font is not present in the font pool.
A given string cache does not correspond to the given font.
void getFc(FontAndCache &fc, const std::string &font) const
An internal function to get a FontAndCache struct for the given font.
std::unordered_map< std::string, FontAndCache > fonts
The glyph images keyed by character.
Definition: BppFontPool.hpp:34
static std::shared_ptr< BppFont > make()
Returns a shared pointer to a new BppFont object.
Definition: BppFont.hpp:71
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
General graphics related code.
Definition: HD44780.hpp:15
boost::error_info< struct Info_FontName, std::string > FontName
The name of the font and is not present in the font pool.
General errors.
static std::shared_ptr< BppStringCache > make(const BppFontSptr &font, unsigned int maxBytes=256 *1024, unsigned int maxStrings=-1)
Creates a cache of rendered strings made using the given font.
#define DUDS_THROW_EXCEPTION(x)
Works like BOOST_THROW_EXCEPTION, but includes a stack trace if DUDS_ERRORS_VERBOSE is defined...
Definition: Errors.hpp:48
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...