DUDS
Distributed Update of Data from Something
BppFont.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) 2019 Jeff Jackowski
9  */
10 #ifndef BPPFONT_HPP
11 #define BPPFONT_HPP
15 #include <unordered_map>
16 
17 namespace duds { namespace ui { namespace graphics {
18 
30 class BppFont : boost::noncopyable {
31 protected:
35  std::unordered_map<char32_t, ConstBppImageSptr> glyphs;
53  virtual ConstBppImageSptr renderGlyph(char32_t gc);
54 public:
55  BppFont() = default;
59  BppFont(const std::string &path) {
60  load(path);
61  }
65  BppFont(std::istream &is) {
66  load(is);
67  }
71  static std::shared_ptr<BppFont> make() {
72  return std::make_shared<BppFont>();
73  }
78  static std::shared_ptr<BppFont> make(const std::string &path) {
79  return std::make_shared<BppFont>(path);
80  }
85  static std::shared_ptr<BppFont> make(std::istream &is) {
86  return std::make_shared<BppFont>(is);
87  }
106  void load(const std::string &path);
121  void load(std::istream &is);
128  void add(char32_t gc, const ConstBppImageSptr &img);
134  void add(char32_t gc, ConstBppImageSptr &&img);
144  const ConstBppImageSptr &get(char32_t gc);
151  ConstBppImageSptr tryGet(char32_t gc);
170  static constexpr Flags FixedWidth = Flags::Bit(0);
175  static constexpr Flags FixedWidthPerLine = Flags::Bit(1);
180  static constexpr Flags VariableHeight = Flags::Bit(2);
184  static constexpr Flags AlignLeft = Flags::Zero();
188  static constexpr Flags AlignCenter = Flags::Bit(3);
192  static constexpr Flags AlignRight = Flags::Bit(4);
196  static constexpr Flags AlignMask = AlignCenter | AlignRight;
214  BppImageSptr render(const std::string &text, Flags flags = AlignLeft);
232  BppImageSptr render(const std::u32string &text, Flags flags = AlignLeft);
244  const std::string &text,
245  Flags flags = Flags::Zero()
246  );
258  const std::u32string &text,
259  Flags flags = Flags::Zero()
260  );
261 };
262 
263 typedef std::shared_ptr<BppFont> BppFontSptr;
264 
265 } } }
266 
267 #endif // #ifndef BPPFONT_HPP
static std::shared_ptr< BppFont > make(std::istream &is)
Returns a shared pointer to a new BppFont object constructed using the BppFont(std::istream &) constr...
Definition: BppFont.hpp:85
BppFont(std::istream &is)
Loads glyphs from an input stream.
Definition: BppFont.hpp:65
void add(char32_t gc, const ConstBppImageSptr &img)
Adds or replaces a glyph in the font.
Definition: BppFont.cpp:78
ImageDimensions lineDimensions(const std::string &text, Flags flags=Flags::Zero())
Returns the dimensions of a single-line string without the overhead of rendering the string...
Definition: BppFont.cpp:317
ConstBppImageSptr tryGet(char32_t gc)
Returns the glyph of the specified character code.
Definition: BppFont.cpp:100
std::shared_ptr< BppFont > BppFontSptr
Definition: BppFont.hpp:263
static constexpr Flags AlignRight
Align each line to the right.
Definition: BppFont.hpp:192
std::shared_ptr< const BppImage > ConstBppImageSptr
Definition: BppImage.hpp:1778
static constexpr BitFlags Zero()
Makes a bit flags container with all flags cleared.
Definition: BitFlags.hpp:133
static constexpr Flags AlignMask
All alignment flags.
Definition: BppFont.hpp:196
static constexpr Flags AlignCenter
Center each line in the resulting image.
Definition: BppFont.hpp:188
Stores the dimensions of an image.
Definition: BppImage.hpp:125
static constexpr Flags FixedWidthPerLine
Compute fixed width individually for each line, so each line may have a different width per glyph...
Definition: BppFont.hpp:175
static constexpr BitFlags Bit(int b)
Makes a bit flags container with a single bit set that is specified by digit number rather than value...
Definition: BitFlags.hpp:141
static constexpr Flags FixedWidth
All glyphs rendered with the same width using the maximum width of the glyphs used in the string...
Definition: BppFont.hpp:170
std::shared_ptr< BppImage > BppImageSptr
Definition: BppImage.hpp:1777
static std::shared_ptr< BppFont > make()
Returns a shared pointer to a new BppFont object.
Definition: BppFont.hpp:71
duds::general::Spinlock block
Used for thread safety.
Definition: BppFont.hpp:39
virtual ConstBppImageSptr renderGlyph(char32_t gc)
Called to render the requested glyph when it is not present in the glyphs map.
Definition: BppFont.cpp:26
static constexpr Flags VariableHeight
Each line will have the height of its tallest glyph rather than the tallest glyph of the entire strin...
Definition: BppFont.hpp:180
ImageDimensions estimatedMaxCharacterSize()
Returns a somewhat decent estimate of the largest size of a character without actually inspecting all...
Definition: BppFont.cpp:115
A simple spinlock following the lockable and timed lockable concepts so that it can be used with std:...
Definition: Spinlock.hpp:52
BppFont(const std::string &path)
Loads glyphs from an image archive in the specified file.
Definition: BppFont.hpp:59
duds::general::BitFlags< struct BppFontRenderingFlags > Flags
Option flags that affect how text is rendered.
Definition: BppFont.hpp:165
void load(const std::string &path)
Loads glyphs from an image archive in the specified file.
Definition: BppFont.cpp:40
General graphics related code.
Definition: HD44780.hpp:15
static constexpr Flags AlignLeft
Align each line to the left.
Definition: BppFont.hpp:184
BppImageSptr render(const std::string &text, Flags flags=AlignLeft)
Renders the given text using this object&#39;s font.
Definition: BppFont.cpp:167
std::unordered_map< char32_t, ConstBppImageSptr > glyphs
The glyph images keyed by character.
Definition: BppFont.hpp:35
static std::shared_ptr< BppFont > make(const std::string &path)
Returns a shared pointer to a new BppFont object constructed using the BppFont(const std::string &) c...
Definition: BppFont.hpp:78
Renders strings using a font made of BppImage objects for glyphs.
Definition: BppFont.hpp:30