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