font.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2015 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 ** Mark Page
28 */
29 
30 #pragma once
31 
32 #include <memory>
33 #include "../Render/graphic_context.h"
34 #include "../Image/pixel_buffer.h"
35 #include "../2D/sprite.h"
36 #include "font_description.h"
37 #include "glyph_metrics.h"
38 
39 namespace clan
40 {
43 
44 class FontProvider;
45 class FontFamily;
46 class Canvas;
47 class Font_Impl;
48 class GlyphMetrics;
49 
53 class Font
54 {
57 
58 public:
60  Font();
61 
62  // \brief Create font using the specified font family
63  Font(FontFamily &font_family, float height);
64 
65  // \brief Create font using the specified font family
66  Font(FontFamily &font_family, const FontDescription &desc);
67 
69  Font(const std::string &typeface_name, float height);
70 
71  // \brief Constructs standard font
72  Font(const std::string &typeface_name, const FontDescription &desc);
73 
74  // \brief Constructs standard font
75  Font(const FontDescription &desc, const std::string &ttf_filename);
76 
77  // \brief Constructs standard font
78  Font(const FontDescription &desc, const std::string &ttf_filename, FileSystem fs);
79 
87  Font(Canvas &canvas, const std::string &typeface_name, Sprite &sprite, const std::string &glyph_list, float spacelen, bool monospace, const FontMetrics &metrics);
88 
90 
93 public:
99  static Resource<Font> resource(Canvas &canvas, const std::string &family_name, const FontDescription &desc, const ResourceManager &resources);
100 
102  static Font load(Canvas &canvas, const std::string &family_name, const FontDescription &reference_desc, FontFamily &font_family, const XMLResourceDocument &doc, std::function<Resource<Sprite>(Canvas &, const std::string &)> cb_get_sprite = std::function<Resource<Sprite>(Canvas &, const std::string &)>());
103 
105 
108 public:
109 
111  bool is_null() const { return !impl; }
112 
114  void throw_if_null() const;
115 
119 
120 public:
122  void set_height(float value);
123 
126 
128  void set_line_height(float height);
129 
132 
136  void set_scalable(float height_threshold = 64.0f);
137 
144  void draw_text(Canvas &canvas, const Pointf &position, const std::string &text, const Colorf &color = Colorf::white);
145  void draw_text(Canvas &canvas, float xpos, float ypos, const std::string &text, const Colorf &color = Colorf::white) { draw_text(canvas, Pointf(xpos, ypos), text, color); }
146 
151  GlyphMetrics get_metrics(Canvas &canvas, unsigned int glyph);
152 
157  GlyphMetrics measure_text(Canvas &canvas, const std::string &string);
158 
161 
165  std::string get_clipped_text(Canvas &canvas, const Sizef &box_size, const std::string &text, const std::string &ellipsis_text = "...");
166 
173  int get_character_index(Canvas &canvas, const std::string &text, const Pointf &point);
174 
178  std::vector<Rectf> get_character_indices(Canvas &canvas, const std::string &text);
179 
180  // Finds the offset for the last visible character when clipping the head
181  size_t clip_from_left(Canvas &canvas, const std::string &text, float width);
182 
183  // Finds the offset for the first visible character when clipping the tail
184  size_t clip_from_right(Canvas &canvas, const std::string &text, float width);
185 
189 
190 private:
191  std::shared_ptr<Font_Impl> impl;
192 
193  friend class Path;
194 
196 };
197 
198 }
199 
201 
Font(const std::string &typeface_name, float height)
Constructs standard font.
Font description class.
Definition: font_description.h:67
Font(const FontDescription &desc, const std::string &ttf_filename, FileSystem fs)
Font(Canvas &canvas, const std::string &typeface_name, Sprite &sprite, const std::string &glyph_list, float spacelen, bool monospace, const FontMetrics &metrics)
Constructs a Font based on a sprite.
Resource manager.
Definition: resource_manager.h:45
void set_height(float value)
Sets the font height.
void throw_if_null() const
Throw an exception if this object is invalid.
void set_line_height(float height)
Sets the distance between each line.
GlyphMetrics get_metrics(Canvas &canvas, unsigned int glyph)
Gets the glyph metrics.
2D (x,y) point structure - Float
Definition: point.h:73
std::vector< Rectf > get_character_indices(Canvas &canvas, const std::string &text)
Get the rectangles of each glyph in a string of text.
std::string get_clipped_text(Canvas &canvas, const Sizef &box_size, const std::string &text, const std::string &ellipsis_text="...")
Retrieves clipped version of the text that will fit into a box.
Floating point color description class (for float).
Definition: color.h:661
GlyphMetrics measure_text(Canvas &canvas, const std::string &string)
Measure text size.
Font()
Constructs a null font.
Font(const std::string &typeface_name, const FontDescription &desc)
2D Graphics Canvas
Definition: canvas.h:73
void set_style(FontStyle setting=FontStyle::normal)
Sets the font style setting.
Font(FontFamily &font_family, const FontDescription &desc)
void draw_text(Canvas &canvas, const Pointf &position, const std::string &text, const Colorf &color=Colorf::white)
Print text.
FontStyle
Definition: font_description.h:57
size_t clip_from_right(Canvas &canvas, const std::string &text, float width)
Definition: path.h:53
static Resource< Font > resource(Canvas &canvas, const std::string &family_name, const FontDescription &desc, const ResourceManager &resources)
Retrieves a Font resource from the resource manager.
Font class.
Definition: font.h:54
void set_weight(FontWeight value=FontWeight::normal)
Sets the font weight.
void draw_text(Canvas &canvas, float xpos, float ypos, const std::string &text, const Colorf &color=Colorf::white)
Definition: font.h:145
FontMetrics get_font_metrics(Canvas &canvas)
Retrieves font metrics description for the selected font.
Font(FontFamily &font_family, float height)
Sprite class.
Definition: sprite.h:56
Definition: clanapp.h:36
Font(const FontDescription &desc, const std::string &ttf_filename)
FontWeight
Definition: font_description.h:44
Virtual File System (VFS).
Definition: file_system.h:48
XML Resource Document.
Definition: xml_resource_document.h:49
void set_scalable(float height_threshold=64.0f)
Sets the threshold to determine if the font can be drawn scaled.
bool is_null() const
Returns true if this object is invalid.
Definition: font.h:111
static Font load(Canvas &canvas, const std::string &family_name, const FontDescription &reference_desc, FontFamily &font_family, const XMLResourceDocument &doc, std::function< Resource< Sprite >(Canvas &, const std::string &)> cb_get_sprite=std::function< Resource< Sprite >(Canvas &, const std::string &)>())
Loads a Font from a XML resource definition.
size_t clip_from_left(Canvas &canvas, const std::string &text, float width)
FontFamily class.
Definition: font_family.h:53
Resource proxy of a specific type.
Definition: resource.h:59
2D (width,height) size structure - Float
Definition: size.h:170
@ color
value is an url
int get_character_index(Canvas &canvas, const std::string &text, const Pointf &point)
Get the character index at a specified point.
static Colorf white
Definition: color.h:1251
Glyph metrics class.
Definition: glyph_metrics.h:42
Font metrics class.
Definition: font_metrics.h:47