font.h
1 /*
2 ** UICore
3 ** Copyright (c) 1997-2015 The UICore 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 UICore 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 "UICore/Display/Render/graphic_context.h"
34 #include "../Image/pixel_buffer.h"
35 #include "font_description.h"
36 #include "glyph_metrics.h"
37 
38 namespace uicore
39 {
40  class FontProvider;
41  class FontFamily;
42  typedef std::shared_ptr<FontFamily> FontFamilyPtr;
43  class Canvas;
44  typedef std::shared_ptr<Canvas> CanvasPtr;
45  class GlyphMetrics;
46 
47  class FontHandle
48  {
49  public:
50  virtual ~FontHandle() { }
51  };
52 
56  class Font
57  {
58  public:
59  // \brief Create font using the specified font family
60  static std::shared_ptr<Font> create(const FontFamilyPtr &font_family, float height);
61 
62  // \brief Create font using the specified font family
63  static std::shared_ptr<Font> create(const FontFamilyPtr &font_family, const FontDescription &desc);
64 
66  static std::shared_ptr<Font> create(const std::string &typeface_name, float height);
67 
68  // \brief Constructs standard font
69  static std::shared_ptr<Font> create(const std::string &typeface_name, const FontDescription &desc);
70 
71  // \brief Constructs standard font
72  static std::shared_ptr<Font> create(const FontDescription &desc, const std::string &ttf_filename);
73 
75  virtual void set_height(float value) = 0;
76 
78  virtual void set_weight(FontWeight value = FontWeight::normal) = 0;
79 
81  virtual void set_line_height(float height) = 0;
82 
84  virtual void set_style(FontStyle setting = FontStyle::normal) = 0;
85 
89  virtual void set_scalable(float height_threshold = 64.0f) = 0;
90 
97  virtual void draw_text(const CanvasPtr &canvas, const Pointf &position, const std::string &text, const Colorf &color = Colorf::white) = 0;
98  void draw_text(const CanvasPtr &canvas, float xpos, float ypos, const std::string &text, const Colorf &color = Colorf::white) { draw_text(canvas, Pointf(xpos, ypos), text, color); }
99 
104  virtual GlyphMetrics metrics(const CanvasPtr &canvas, unsigned int glyph) = 0;
105 
110  virtual GlyphMetrics measure_text(const CanvasPtr &canvas, const std::string &string) = 0;
111 
113  virtual const FontMetrics &font_metrics(const CanvasPtr &canvas) = 0;
114 
118  std::string clipped_text(const CanvasPtr &canvas, const Sizef &box_size, const std::string &text, const std::string &ellipsis_text = "...");
119 
126  virtual int character_index(const CanvasPtr &canvas, const std::string &text, const Pointf &point) = 0;
127 
131  virtual std::vector<Rectf> character_indices(const CanvasPtr &canvas, const std::string &text) = 0;
132 
133  // Finds the offset for the last visible character when clipping the head
134  size_t clip_from_left(const CanvasPtr &canvas, const std::string &text, float width);
135 
136  // Finds the offset for the first visible character when clipping the tail
137  size_t clip_from_right(const CanvasPtr &canvas, const std::string &text, float width);
138 
144  virtual FontHandle *handle(const CanvasPtr &canvas) = 0;
145  };
146 
147  typedef std::shared_ptr<Font> FontPtr;
148 
149  #ifdef WIN32
150  class FontHandle_Win32 : public FontHandle
151  {
152  public:
154  HFONT hfont();
155  private:
156  friend class FontEngine_Win32;
157  FontEngine_Win32 *engine = nullptr;
158  };
159  #endif
160 }
static std::shared_ptr< Font > create(const FontFamilyPtr &font_family, float height)
virtual void set_line_height(float height)=0
Sets the distance between each line.
FontWeight
Definition: font_description.h:39
size_t clip_from_left(const CanvasPtr &canvas, const std::string &text, float width)
virtual int character_index(const CanvasPtr &canvas, const std::string &text, const Pointf &point)=0
Get the character index at a specified point.
static Colorf white
Definition: color.h:1207
FontFamily class.
Definition: font_family.h:43
2D (x,y) point structure - Float
Definition: point.h:68
std::shared_ptr< FontFamily > FontFamilyPtr
Definition: font.h:41
Floating point color description class (for float).
Definition: color.h:630
Font class.
Definition: font.h:56
virtual void set_weight(FontWeight value=FontWeight::normal)=0
Sets the font weight.
virtual const FontMetrics & font_metrics(const CanvasPtr &canvas)=0
Retrieves font metrics description for the selected font.
Font metrics class.
Definition: font_metrics.h:41
virtual GlyphMetrics metrics(const CanvasPtr &canvas, unsigned int glyph)=0
Gets the glyph metrics.
Glyph metrics class.
Definition: glyph_metrics.h:37
2D Graphics Canvas
Definition: canvas.h:52
virtual void set_height(float value)=0
Sets the font height.
2D (width,height) size structure - Float
Definition: size.h:180
Definition: font.h:47
size_t clip_from_right(const CanvasPtr &canvas, const std::string &text, float width)
std::shared_ptr< Font > FontPtr
Definition: path.h:39
virtual void set_scalable(float height_threshold=64.0f)=0
Sets the threshold to determine if the font can be drawn scaled.
virtual ~FontHandle()
Definition: font.h:50
FontStyle
Definition: font_description.h:52
virtual void draw_text(const CanvasPtr &canvas, const Pointf &position, const std::string &text, const Colorf &color=Colorf::white)=0
Print text.
std::string clipped_text(const CanvasPtr &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.
virtual void set_style(FontStyle setting=FontStyle::normal)=0
Sets the font style setting.
std::shared_ptr< Canvas > CanvasPtr
Definition: canvas.h:126
Font description class.
Definition: font_description.h:62
virtual std::vector< Rectf > character_indices(const CanvasPtr &canvas, const std::string &text)=0
Get the rectangles of each glyph in a string of text.
Definition: Application/application.h:35
void draw_text(const CanvasPtr &canvas, float xpos, float ypos, const std::string &text, const Colorf &color=Colorf::white)
Definition: font.h:98
virtual FontHandle * handle(const CanvasPtr &canvas)=0
Get the font handle interface.
virtual GlyphMetrics measure_text(const CanvasPtr &canvas, const std::string &string)=0
Measure text size.