font.h Source File - ClanLib SDK
Description: font.h Source File

font.h
1/*
2** ClanLib SDK
3** Copyright (c) 1997-2020 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
39namespace clan
40{
43
44 class FontProvider;
45 class FontFamily;
46 class Canvas;
47 class Font_Impl;
48 class GlyphMetrics;
49
50 class FontHandle
51 {
52 public:
53 virtual ~FontHandle() = 0;
54 };
55
59 class Font
60 {
61 public:
63 Font();
64
65 // \brief Create font using the specified font family
66 Font(FontFamily &font_family, float height);
67
68 // \brief Create font using the specified font family
69 Font(FontFamily &font_family, const FontDescription &desc);
70
72 Font(const std::string &typeface_name, float height);
73
74 // \brief Constructs standard font
75 Font(const std::string &typeface_name, const FontDescription &desc);
76
77 // \brief Constructs standard font
78 Font(const FontDescription &desc, const std::string &ttf_filename);
79
80 // \brief Constructs standard font
81 Font(const FontDescription &desc, const std::string &ttf_filename, FileSystem fs);
82
90 Font(Canvas &canvas, const std::string &typeface_name, Sprite &sprite, const std::string &glyph_list, float spacelen, bool monospace, const FontMetrics &metrics);
91
97 static Resource<Font> resource(Canvas &canvas, const std::string &family_name, const FontDescription &desc, const ResourceManager &resources);
98
100 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 &)>());
101
103 bool is_null() const { return !impl; }
104 explicit operator bool() const { return bool(impl); }
105
107 void throw_if_null() const;
108
110 void set_height(float value);
111
113 void set_weight(FontWeight value = FontWeight::normal);
114
116 void set_line_height(float height);
117
119 void set_style(FontStyle setting = FontStyle::normal);
120
124 void set_scalable(float height_threshold = 64.0f);
125
132 void draw_text(Canvas &canvas, const Pointf &position, const std::string &text, const Colorf &color = StandardColorf::white());
133 void draw_text(Canvas &canvas, float xpos, float ypos, const std::string &text, const Colorf &color = StandardColorf::white()) { draw_text(canvas, Pointf(xpos, ypos), text, color); }
134
139 GlyphMetrics get_metrics(Canvas &canvas, unsigned int glyph) const;
140
145 GlyphMetrics measure_text(Canvas &canvas, const std::string &string) const;
146
148 FontMetrics get_font_metrics(Canvas &canvas) const;
149
153 std::string get_clipped_text(Canvas &canvas, const Sizef &box_size, const std::string &text, const std::string &ellipsis_text = "...") const;
154
161 int get_character_index(Canvas &canvas, const std::string &text, const Pointf &point) const;
162
166 std::vector<Rectf> get_character_indices(Canvas &canvas, const std::string &text) const;
167
168 // Finds the offset for the last visible character when clipping the head
169 size_t clip_from_left(Canvas &canvas, const std::string &text, float width) const;
170
171 // Finds the offset for the first visible character when clipping the tail
172 size_t clip_from_right(Canvas &canvas, const std::string &text, float width) const;
173
174 // Get the font description
175 FontDescription get_description() const;
176
182 FontHandle *get_handle(Canvas &canvas);
183
184 private:
185 std::shared_ptr<Font_Impl> impl;
186
187 friend class Path;
188 };
189
190 #ifdef WIN32
191 class FontEngine_Win32; // GCC needs to be explicitly told this is a class
192
193 class FontHandle_Win32 : public FontHandle
194 {
195 public:
197 HFONT hfont();
198 private:
199 friend class FontEngine_Win32;
200 FontEngine_Win32 *engine = nullptr;
201 };
202 #endif
203
205}
clan::Canvas
2D Graphics Canvas
Definition canvas.h:72
clan::Colorf
Floating point color description class (for float).
Definition color.h:799
clan::FileSystem
Virtual File System (VFS).
Definition file_system.h:47
clan::FontDescription
Font description class.
Definition font_description.h:66
clan::FontFamily
FontFamily class.
Definition font_family.h:54
clan::FontHandle
Definition font.h:51
clan::FontHandle::~FontHandle
virtual ~FontHandle()=0
clan::FontMetrics
Font metrics class.
Definition font_metrics.h:46
clan::Font::get_metrics
GlyphMetrics get_metrics(Canvas &canvas, unsigned int glyph) const
Gets the glyph metrics.
clan::Font::Font
Font(const FontDescription &desc, const std::string &ttf_filename, FileSystem fs)
clan::Font::Font
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.
clan::Font::Font
Font(const std::string &typeface_name, float height)
Constructs standard font.
clan::Font::set_scalable
void set_scalable(float height_threshold=64.0f)
Sets the threshold to determine if the font can be drawn scaled.
clan::Font::get_font_metrics
FontMetrics get_font_metrics(Canvas &canvas) const
Retrieves font metrics description for the selected font.
clan::Font::resource
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.
clan::Font::Path
friend class Path
Definition font.h:187
clan::Font::Font
Font(const std::string &typeface_name, const FontDescription &desc)
clan::Font::clip_from_right
size_t clip_from_right(Canvas &canvas, const std::string &text, float width) const
clan::Font::clip_from_left
size_t clip_from_left(Canvas &canvas, const std::string &text, float width) const
clan::Font::is_null
bool is_null() const
Returns true if this object is invalid.
Definition font.h:103
clan::Font::load
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.
clan::Font::measure_text
GlyphMetrics measure_text(Canvas &canvas, const std::string &string) const
Measure text size.
clan::Font::get_character_index
int get_character_index(Canvas &canvas, const std::string &text, const Pointf &point) const
Get the character index at a specified point.
clan::Font::draw_text
void draw_text(Canvas &canvas, float xpos, float ypos, const std::string &text, const Colorf &color=StandardColorf::white())
Definition font.h:133
clan::Font::get_handle
FontHandle * get_handle(Canvas &canvas)
Get the font handle interface.
clan::Font::set_style
void set_style(FontStyle setting=FontStyle::normal)
Sets the font style setting.
clan::Font::Font
Font(const FontDescription &desc, const std::string &ttf_filename)
clan::Font::Font
Font()
Constructs a null font.
clan::Font::get_character_indices
std::vector< Rectf > get_character_indices(Canvas &canvas, const std::string &text) const
Get the rectangles of each glyph in a string of text.
clan::Font::set_line_height
void set_line_height(float height)
Sets the distance between each line.
clan::Font::set_weight
void set_weight(FontWeight value=FontWeight::normal)
Sets the font weight.
clan::Font::set_height
void set_height(float value)
Sets the font height.
clan::Font::get_clipped_text
std::string get_clipped_text(Canvas &canvas, const Sizef &box_size, const std::string &text, const std::string &ellipsis_text="...") const
Retrieves clipped version of the text that will fit into a box.
clan::Font::Font
Font(FontFamily &font_family, const FontDescription &desc)
clan::Font::get_description
FontDescription get_description() const
clan::Font::draw_text
void draw_text(Canvas &canvas, const Pointf &position, const std::string &text, const Colorf &color=StandardColorf::white())
Print text.
clan::Font::Font
Font(FontFamily &font_family, float height)
clan::Font::throw_if_null
void throw_if_null() const
Throw an exception if this object is invalid.
clan::GlyphMetrics
Glyph metrics class.
Definition glyph_metrics.h:41
clan::Pointf
2D (x,y) point structure - Float
Definition point.h:72
clan::ResourceManager
Resource manager.
Definition resource_manager.h:44
clan::Resource
Resource proxy of a specific type.
Definition resource.h:58
clan::Sizef
2D (width,height) size structure - Float
Definition size.h:189
clan::Sprite
Sprite class.
Definition sprite.h:55
clan::StandardColorf::white
static Colorf white()
Definition color.h:1884
clan::XMLResourceDocument
XML Resource Document.
Definition xml_resource_document.h:48
clan::FillMode::point
@ point
Definition graphic_context.h:78
clan::StandardProgram::sprite
@ sprite
Definition graphic_context.h:242
clan::FontWeight
FontWeight
Definition font_description.h:43
clan::FontStyle
FontStyle
Definition font_description.h:56
clan::FontWeight::normal
@ normal
Definition font_description.h:47
clan::FontStyle::normal
@ normal
Definition font_description.h:57
clan
Definition clanapp.h:36
clan::StyleValueType::color
@ color
value is an url
Definition style_value_type.h:43