kodi
GUIFont.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2003-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
16 #include "utils/ColorUtils.h"
17 
18 #include <assert.h>
19 #include <math.h>
20 #include <stdint.h>
21 #include <string>
22 #include <vector>
23 
24 typedef uint32_t character_t;
25 typedef std::vector<character_t> vecText;
26 
27 class CGUIFontTTF;
28 class CGraphicContext;
29 
38 // clang-format off
39 constexpr int XBFONT_LEFT = 0;
40 constexpr int XBFONT_RIGHT = (1 << 0);
41 constexpr int XBFONT_CENTER_X = (1 << 1);
42 constexpr int XBFONT_CENTER_Y = (1 << 2);
43 constexpr int XBFONT_TRUNCATED = (1 << 3);
44 constexpr int XBFONT_JUSTIFIED = (1 << 4);
45 constexpr int XBFONT_TRUNCATED_LEFT = (1 << 5);
46 // clang-format on
48 
49 // flags for font style. lower 16 bits are the unicode code
50 // points, 16-24 are color bits and 24-32 are style bits
51 constexpr int FONT_STYLE_NORMAL = 0;
52 constexpr int FONT_STYLE_BOLD = (1 << 0);
53 constexpr int FONT_STYLE_ITALICS = (1 << 1);
54 constexpr int FONT_STYLE_LIGHT = (1 << 2);
55 constexpr int FONT_STYLE_UPPERCASE = (1 << 3);
56 constexpr int FONT_STYLE_LOWERCASE = (1 << 4);
57 constexpr int FONT_STYLE_CAPITALIZE = (1 << 5);
58 constexpr int FONT_STYLE_MASK = 0xFF;
59 constexpr int FONT_STYLES_COUNT = 7;
60 
62 {
63 public:
64  CScrollInfo(unsigned int wait = 50,
65  float pos = 0,
66  int speed = defaultSpeed,
67  const std::string& scrollSuffix = " | ");
68 
69  void SetSpeed(int speed) { m_pixelSpeed = speed * 0.001f; }
70  void Reset()
71  {
72  m_waitTime = m_initialWait;
73  // pixelPos is where we start the current letter, so is measured
74  // to the left of the text rendering's left edge. Thus, a negative
75  // value will mean the text starts to the right
76  m_pixelPos = -m_initialPos;
77  // privates:
78  m_averageFrameTime = 1000.f / fabs((float)defaultSpeed);
79  m_lastFrameTime = 0;
80  m_textWidth = 0;
81  m_totalWidth = 0;
82  m_widthValid = false;
83  m_loopCount = 0;
84  }
85  float GetPixelsPerFrame();
86 
87  float m_pixelPos;
88  float m_pixelSpeed;
89  unsigned int m_waitTime;
90  unsigned int m_initialWait;
91  float m_initialPos;
92  vecText m_suffix;
93  mutable float m_textWidth;
94  mutable float m_totalWidth;
95  mutable bool m_widthValid;
96 
97  unsigned int m_loopCount;
98 
99  static const int defaultSpeed = 60;
100 
101 private:
102  float m_averageFrameTime;
103  uint32_t m_lastFrameTime;
104 };
105 
110 class CGUIFont
111 {
112 public:
113  CGUIFont(const std::string& strFontName,
114  uint32_t style,
115  UTILS::COLOR::Color textColor,
116  UTILS::COLOR::Color shadowColor,
117  float lineSpacing,
118  float origHeight,
119  CGUIFontTTF* font);
120  virtual ~CGUIFont();
121 
122  std::string& GetFontName();
123 
124  void DrawText(float x,
125  float y,
126  UTILS::COLOR::Color color,
127  UTILS::COLOR::Color shadowColor,
128  const vecText& text,
129  uint32_t alignment,
130  float maxPixelWidth)
131  {
132  std::vector<UTILS::COLOR::Color> colors;
133  colors.push_back(color);
134  DrawText(x, y, colors, shadowColor, text, alignment, maxPixelWidth);
135  };
136 
137  void DrawText(float x,
138  float y,
139  const std::vector<UTILS::COLOR::Color>& colors,
140  UTILS::COLOR::Color shadowColor,
141  const vecText& text,
142  uint32_t alignment,
143  float maxPixelWidth);
144 
145  void DrawScrollingText(float x,
146  float y,
147  const std::vector<UTILS::COLOR::Color>& colors,
148  UTILS::COLOR::Color shadowColor,
149  const vecText& text,
150  uint32_t alignment,
151  float maxPixelWidth,
152  const CScrollInfo& scrollInfo);
153 
154  bool UpdateScrollInfo(const vecText& text, CScrollInfo& scrollInfo);
155 
156  float GetTextWidth(const vecText& text);
157  float GetCharWidth(character_t ch);
158  float GetTextHeight(int numLines) const;
159  float GetTextBaseLine() const;
160  float GetLineHeight() const;
161 
163  float GetScaleFactor() const;
164 
165  void Begin();
166  void End();
167 
168  uint32_t GetStyle() const { return m_style; }
169 
170  CGUIFontTTF* GetFont() const { return m_font; }
171 
172  void SetFont(CGUIFontTTF* font);
173 
174 protected:
175  std::string m_strFontName;
176  uint32_t m_style;
177  UTILS::COLOR::Color m_shadowColor;
178  UTILS::COLOR::Color m_textColor;
179  float m_lineSpacing;
180  float m_origHeight;
181  CGUIFontTTF* m_font; // the font object has the size information
182 
183 private:
184  bool ClippedRegionIsEmpty(
185  CGraphicContext& context, float x, float y, float width, uint32_t alignment) const;
186 };
constexpr int XBFONT_LEFT
Align X left.
Definition: GUIFont.h:39
constexpr int XBFONT_CENTER_Y
Align Y center.
Definition: GUIFont.h:42
Definition: GUIFontTTF.h:76
Definition: GUIFont.h:61
constexpr int XBFONT_TRUNCATED
Truncated text from right (text end with ellipses)
Definition: GUIFont.h:43
Definition: LibInputPointer.h:13
constexpr int XBFONT_TRUNCATED_LEFT
Truncated text from left (text start with ellipses)
Definition: GUIFont.h:45
constexpr int XBFONT_CENTER_X
Align X center.
Definition: GUIFont.h:41
Definition: GUIFont.h:110
constexpr int XBFONT_RIGHT
Align X right.
Definition: GUIFont.h:40
Definition: GraphicContext.h:60
constexpr int XBFONT_JUSTIFIED
Justify text.
Definition: GUIFont.h:44