1.1.0
font.hpp
1 #ifndef FONT_CHEF_FONT_HPP
2 #define FONT_CHEF_FONT_HPP
3 
9 #include "font-chef/font-chef-export.h"
10 #include "font-chef/font.h"
11 #include <cstring>
12 
13 #include <string>
14 #include <vector>
15 
16 #include "font-chef/font-size.hpp"
17 #include "font-chef/color.hpp"
18 #include "font-chef/render-result.hpp"
19 
20 namespace fc {
29  class FONT_CHEF_EXPORT font {
30  private:
31  fc_font * data;
32 
33  public:
38  font(font && other) noexcept : data(other.data) {
39  other.data = nullptr;
40  };
41 
47  font(font const & other)
48  : data(fc_construct(
49  fc_get_font_data(other.data),
50  fc_get_font_size(other.data),
51  fc_get_color(other.data))
52  ) {
53  size_t block_count = fc_get_block_count(other.data);
54  for (size_t i = 0; i < block_count; i++) add(fc_get_block_at(other.data, i));
55  if (fc_get_pixels(other.data) != nullptr) cook();
56  }
57 
65  font(uint8_t const * font_data, fc::font_size const & font_size, fc::color const & font_color)
66  : data(fc_construct(font_data, font_size.data, font_color.data)) { };
67 
72  ~font() {
73  if (data) fc_destruct(data);
74  }
75 
85  font & add(fc_unicode_block const & block) & {
86  return add(block.first, block.last);
87  }
88 
89 
99  font && add(fc_unicode_block const & block) && {
100  return std::move(add(block.first, block.last));
101  }
102 
113  font & add(uint32_t first, uint32_t last) & {
114  fc_add(data, first, last);
115  return *this;
116  }
117 
118 
129  font && add(uint32_t first, uint32_t last) && {
130  fc_add(data, first, last);
131  return std::move(*this);
132  }
133 
142  font & cook() & {
143  fc_cook(data);
144  return *this;
145  }
146 
147 
156  font && cook() && {
157  fc_cook(data);
158  return std::move(*this);
159  }
160 
169  fc_pixels pixels() const {
170  return *fc_get_pixels(data);
171  }
172 
190  fc::render_result render(std::string const & text) const {
191  fc::render_result result(data);
192  return std::move(render(text, result));
193  }
194 
208  fc::render_result & render(std::string const & text, fc::render_result & result) const {
209  result.font = data;
210  std::vector<fc_character_mapping> & mapping = result.mapping;
211  if (mapping.size() < text.length()) mapping.resize(text.length());
212  struct fc_render_result r = fc_render(data, (uint8_t *) text.data(), text.size(), mapping.data());
213  mapping.resize(r.glyph_count);
214  result.line_count = r.line_count;
215  return result;
216  }
217  };
218 
238  FONT_CHEF_EXPORT font from(uint8_t const * font_data, fc::font_size const & font_size, fc::color const & font_color) {
239  font r(font_data, font_size, font_color);
240  return r;
241  }
242 }
243 
244 #endif //FONT_CHEF_FONT_HPP
fc_get_font_data
const uint8_t * fc_get_font_data(struct fc_font const *font)
Returns the font data specified in fc_construct.
fc::render_result
Wraps a std::vector<fc_character_mapping>.
Definition: render-result.hpp:29
fc_render_result::line_count
uint32_t line_count
How many lines were produced.
Definition: font.h:53
fc_cook
void fc_cook(struct fc_font *font)
Generates a bitmap and corresponding character information for a font.
fc_construct
struct fc_font * fc_construct(uint8_t const *font_data, struct fc_font_size font_size, struct fc_color font_color)
Constructs a fc_font structure with the provided font, a size (either in pixels or points) and a font...
fc::color
A wrapper class for fc_color.
Definition: color.hpp:18
fc::font::add
font && add(fc_unicode_block const &block) &&
Adds a new unicode block to be cooked.
Definition: font.hpp:99
fc::font
A wrapper class for fc_font.
Definition: font.hpp:29
fc::font::add
font & add(uint32_t first, uint32_t last) &
Adds a new unicode range to be cooked.
Definition: font.hpp:113
fc_font
A fc_font struct represents all data and metadata about a font.
fc_unicode_block
Provides information about a specific unicode block, as specified in ftp://ftp.unicode....
Definition: unicode-block.h:21
fc_destruct
void fc_destruct(struct fc_font *font)
Destroys and frees all memory allocated by this library.
fc_get_color
struct fc_color fc_get_color(struct fc_font const *font)
Returns the font color set to this font at construction time.
fc_add
void fc_add(struct fc_font *font, uint32_t first, uint32_t last)
Adds the given unicode range to the list of blocks to be cooked. You must add blocks before calling f...
fc_render_result
A structure holding the result of a call to fc_render or fc_render_wrapped
Definition: font.h:49
fc::font::pixels
fc_pixels pixels() const
Obtains a structure containing a pointer to the pixel data and it's dimensions.
Definition: font.hpp:169
fc::render_result::font
fc_font * font
A pointer to the fc_font used to generate this mapping.
Definition: render-result.hpp:43
fc::font::font
font(font &&other) noexcept
Move constructor.
Definition: font.hpp:38
fc::font::render
fc::render_result & render(std::string const &text, fc::render_result &result) const
Produces clipping and target rectangles to render specified text reusing an instance of fc::render_re...
Definition: font.hpp:208
fc::font::cook
font & cook() &
Cooks all the added unicode ranges into a pixmap and clipping information.
Definition: font.hpp:142
fc_get_block_at
struct fc_unicode_block fc_get_block_at(struct fc_font const *font, size_t index)
Returns a unicode block at the index position.
fc::font::add
font && add(uint32_t first, uint32_t last) &&
Adds a new unicode range to be cooked.
Definition: font.hpp:129
fc::from
font from(uint8_t const *font_data, fc::font_size const &font_size, fc::color const &font_color)
A helper method to ease font cooking via method chaining.
Definition: font.hpp:238
fc::font::cook
font && cook() &&
Cooks all the added unicode ranges into a pixmap and clipping information.
Definition: font.hpp:156
fc::font::render
fc::render_result render(std::string const &text) const
Produces clipping and target rectangles to render specified text.
Definition: font.hpp:190
fc::render_result::line_count
uint32_t line_count
How many lines were produced.
Definition: render-result.hpp:38
fc_render_result::glyph_count
uint32_t glyph_count
How many glphs were produced.
Definition: font.h:58
fc_get_block_count
size_t fc_get_block_count(struct fc_font const *font)
Returns count of added blocks in this font.
fc::font::~font
~font()
Destructs a fc::font instance and frees all the memory associated with it.
Definition: font.hpp:72
fc_get_pixels
struct fc_pixels const * fc_get_pixels(struct fc_font const *font)
Returns the pixel data after for a font generated after a fc_cook was called.
fc_render
struct fc_render_result fc_render(struct fc_font const *font, unsigned char const *text, size_t byte_count, struct fc_character_mapping *mapping)
Produces a list of source and target rectangles that can be used as clipping rects (or UV maps) for t...
fc_get_font_size
struct fc_font_size fc_get_font_size(struct fc_font const *font)
Returns the font size set to this font at construction time.
fc::font_size
A wrapper class for fc_font_size.
Definition: font-size.hpp:18
fc::font::add
font & add(fc_unicode_block const &block) &
Adds a new unicode block to be cooked.
Definition: font.hpp:85
fc::font::font
font(font const &other)
Copy constructor.
Definition: font.hpp:47
fc_pixels
A structure holding pixel data and its dimensions.
Definition: font.h:34
fc::font::font
font(uint8_t const *font_data, fc::font_size const &font_size, fc::color const &font_color)
Constructs a fc::font instance from a font data, a font size and a font color.
Definition: font.hpp:65
fc::render_result::mapping
std::vector<::fc_character_mapping > mapping
A vector of character mappings produced after calling fc::font::render.
Definition: render-result.hpp:33