Skip to content

Commit 795de63

Browse files
authored
Merge pull request xbmc#27452 from neo1973/GUIFont_std_span
[GUI] Switch font releated APIs to `std::span`
2 parents a04703c + 2ebb9db commit 795de63

File tree

7 files changed

+78
-84
lines changed

7 files changed

+78
-84
lines changed

xbmc/guilib/GUIFont.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ std::string& CGUIFont::GetFontName()
8787

8888
void CGUIFont::DrawText(float x,
8989
float y,
90-
const std::vector<KODI::UTILS::COLOR::Color>& colors,
90+
std::span<const KODI::UTILS::COLOR::Color> colors,
9191
KODI::UTILS::COLOR::Color shadowColor,
92-
const vecText& text,
92+
std::span<const character_t> text,
9393
uint32_t alignment,
9494
float maxPixelWidth)
9595
{
@@ -126,7 +126,7 @@ void CGUIFont::DrawText(float x,
126126
context.RestoreClipRegion();
127127
}
128128

129-
bool CGUIFont::UpdateScrollInfo(const vecText& text, CScrollInfo& scrollInfo)
129+
bool CGUIFont::UpdateScrollInfo(std::span<const character_t> text, CScrollInfo& scrollInfo)
130130
{
131131
CWinSystemBase* const winSystem = CServiceBroker::GetWinSystem();
132132
if (!winSystem)
@@ -179,9 +179,9 @@ bool CGUIFont::UpdateScrollInfo(const vecText& text, CScrollInfo& scrollInfo)
179179

180180
void CGUIFont::DrawScrollingText(float x,
181181
float y,
182-
const std::vector<KODI::UTILS::COLOR::Color>& colors,
182+
std::span<const KODI::UTILS::COLOR::Color> colors,
183183
KODI::UTILS::COLOR::Color shadowColor,
184-
const vecText& text,
184+
std::span<const character_t> text,
185185
uint32_t alignment,
186186
float maxWidth,
187187
const CScrollInfo& scrollInfo)
@@ -262,7 +262,7 @@ bool CGUIFont::ClippedRegionIsEmpty(
262262
return !context.SetClipRegion(x, y, width, m_font->GetTextHeight(1, 2) * context.GetGUIScaleY());
263263
}
264264

265-
float CGUIFont::GetTextWidth(const vecText& text)
265+
float CGUIFont::GetTextWidth(std::span<const character_t> text)
266266
{
267267
CWinSystemBase* const winSystem = CServiceBroker::GetWinSystem();
268268
if (!m_font || !winSystem)

xbmc/guilib/GUIFont.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include <assert.h>
1919
#include <math.h>
20+
#include <span>
2021
#include <stdint.h>
2122
#include <string>
2223
#include <vector>
@@ -125,35 +126,33 @@ class CGUIFont
125126
float y,
126127
KODI::UTILS::COLOR::Color color,
127128
KODI::UTILS::COLOR::Color shadowColor,
128-
const vecText& text,
129+
std::span<const character_t> text,
129130
uint32_t alignment,
130131
float maxPixelWidth)
131132
{
132-
std::vector<KODI::UTILS::COLOR::Color> colors;
133-
colors.push_back(color);
134-
DrawText(x, y, colors, shadowColor, text, alignment, maxPixelWidth);
133+
DrawText(x, y, std::span(&color, 1), shadowColor, text, alignment, maxPixelWidth);
135134
};
136135

137136
void DrawText(float x,
138137
float y,
139-
const std::vector<KODI::UTILS::COLOR::Color>& colors,
138+
std::span<const KODI::UTILS::COLOR::Color> colors,
140139
KODI::UTILS::COLOR::Color shadowColor,
141-
const vecText& text,
140+
std::span<const character_t> text,
142141
uint32_t alignment,
143142
float maxPixelWidth);
144143

145144
void DrawScrollingText(float x,
146145
float y,
147-
const std::vector<KODI::UTILS::COLOR::Color>& colors,
146+
std::span<const KODI::UTILS::COLOR::Color> colors,
148147
KODI::UTILS::COLOR::Color shadowColor,
149-
const vecText& text,
148+
std::span<const character_t> text,
150149
uint32_t alignment,
151150
float maxPixelWidth,
152151
const CScrollInfo& scrollInfo);
153152

154-
bool UpdateScrollInfo(const vecText& text, CScrollInfo& scrollInfo);
153+
bool UpdateScrollInfo(std::span<const character_t> text, CScrollInfo& scrollInfo);
155154

156-
float GetTextWidth(const vecText& text);
155+
float GetTextWidth(std::span<const character_t> text);
157156
float GetCharWidth(character_t ch);
158157
float GetTextHeight(int numLines) const;
159158
float GetTextBaseLine() const;

xbmc/guilib/GUIFontCache.cpp

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ class CGUIFontCacheImpl
6565
{
6666
if (ageit->second == it)
6767
{
68-
ageMap.erase(ageit);
69-
ageMap.insert(typename AgeMap::value_type(now, it));
68+
auto node = ageMap.extract(ageit);
69+
node.key() = now;
70+
node.mapped() = it;
71+
ageMap.insert(std::move(node));
7072
it->second->m_lastUsed = now;
7173
return;
7274
}
@@ -84,8 +86,8 @@ class CGUIFontCacheImpl
8486
explicit CGUIFontCacheImpl(CGUIFontCache<Position, Value>* parent) : m_parent(parent) {}
8587
Value& Lookup(const CGraphicContext& context,
8688
Position& pos,
87-
const std::vector<KODI::UTILS::COLOR::Color>& colors,
88-
const vecText& text,
89+
std::span<const KODI::UTILS::COLOR::Color> colors,
90+
std::span<const character_t> text,
8991
uint32_t alignment,
9092
float maxPixelWidth,
9193
bool scrolling,
@@ -97,18 +99,18 @@ class CGUIFontCacheImpl
9799
template<class Position, class Value>
98100
CGUIFontCacheEntry<Position, Value>::~CGUIFontCacheEntry()
99101
{
100-
delete &m_key.m_colors;
101-
delete &m_key.m_text;
102102
m_value.clear();
103103
}
104104

105105
template<class Position, class Value>
106106
void CGUIFontCacheEntry<Position, Value>::Assign(const CGUIFontCacheKey<Position>& key,
107107
std::chrono::steady_clock::time_point now)
108108
{
109+
m_color.assign(key.m_colors.begin(), key.m_colors.end());
110+
m_text.assign(key.m_text.begin(), key.m_text.end());
109111
m_key.m_pos = key.m_pos;
110-
m_key.m_colors.assign(key.m_colors.begin(), key.m_colors.end());
111-
m_key.m_text.assign(key.m_text.begin(), key.m_text.end());
112+
m_key.m_colors = m_color;
113+
m_key.m_text = m_text;
112114
m_key.m_alignment = key.m_alignment;
113115
m_key.m_maxPixelWidth = key.m_maxPixelWidth;
114116
m_key.m_scrolling = key.m_scrolling;
@@ -131,8 +133,8 @@ CGUIFontCache<Position, Value>::~CGUIFontCache() = default;
131133
template<class Position, class Value>
132134
Value& CGUIFontCache<Position, Value>::Lookup(const CGraphicContext& context,
133135
Position& pos,
134-
const std::vector<KODI::UTILS::COLOR::Color>& colors,
135-
const vecText& text,
136+
std::span<const KODI::UTILS::COLOR::Color> colors,
137+
std::span<const character_t> text,
136138
uint32_t alignment,
137139
float maxPixelWidth,
138140
bool scrolling,
@@ -147,21 +149,19 @@ Value& CGUIFontCache<Position, Value>::Lookup(const CGraphicContext& context,
147149
}
148150

149151
template<class Position, class Value>
150-
Value& CGUIFontCacheImpl<Position, Value>::Lookup(
151-
const CGraphicContext& context,
152-
Position& pos,
153-
const std::vector<KODI::UTILS::COLOR::Color>& colors,
154-
const vecText& text,
155-
uint32_t alignment,
156-
float maxPixelWidth,
157-
bool scrolling,
158-
std::chrono::steady_clock::time_point now,
159-
bool& dirtyCache)
152+
Value& CGUIFontCacheImpl<Position, Value>::Lookup(const CGraphicContext& context,
153+
Position& pos,
154+
std::span<const KODI::UTILS::COLOR::Color> colors,
155+
std::span<const character_t> text,
156+
uint32_t alignment,
157+
float maxPixelWidth,
158+
bool scrolling,
159+
std::chrono::steady_clock::time_point now,
160+
bool& dirtyCache)
160161
{
161-
const CGUIFontCacheKey<Position> key(
162-
pos, const_cast<std::vector<KODI::UTILS::COLOR::Color>&>(colors), const_cast<vecText&>(text),
163-
alignment, maxPixelWidth, scrolling, context.GetGUIMatrix(), context.GetGUIScaleX(),
164-
context.GetGUIScaleY());
162+
const CGUIFontCacheKey<Position> key(pos, colors, text, alignment, maxPixelWidth, scrolling,
163+
context.GetGUIMatrix(), context.GetGUIScaleX(),
164+
context.GetGUIScaleY());
165165

166166
auto i = m_list.FindKey(key);
167167
if (i == m_list.hashMap.end())
@@ -227,8 +227,8 @@ template CGUIFontCacheStaticValue& CGUIFontCache<
227227
CGUIFontCacheStaticPosition,
228228
CGUIFontCacheStaticValue>::Lookup(const CGraphicContext& context,
229229
CGUIFontCacheStaticPosition&,
230-
const std::vector<KODI::UTILS::COLOR::Color>&,
231-
const vecText&,
230+
std::span<const KODI::UTILS::COLOR::Color> colors,
231+
std::span<const character_t> text,
232232
uint32_t,
233233
float,
234234
bool,
@@ -245,8 +245,8 @@ template CGUIFontCacheDynamicValue& CGUIFontCache<
245245
CGUIFontCacheDynamicPosition,
246246
CGUIFontCacheDynamicValue>::Lookup(const CGraphicContext& context,
247247
CGUIFontCacheDynamicPosition&,
248-
const std::vector<KODI::UTILS::COLOR::Color>&,
249-
const vecText&,
248+
std::span<const KODI::UTILS::COLOR::Color> colors,
249+
std::span<const character_t> text,
250250
uint32_t,
251251
float,
252252
bool,

xbmc/guilib/GUIFontCache.h

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <cstddef>
2323
#include <cstring>
2424
#include <memory>
25+
#include <span>
2526
#include <stdint.h>
2627
#include <vector>
2728

@@ -40,8 +41,8 @@ template<class Position>
4041
struct CGUIFontCacheKey
4142
{
4243
Position m_pos;
43-
std::vector<KODI::UTILS::COLOR::Color>& m_colors;
44-
vecText& m_text;
44+
std::span<const KODI::UTILS::COLOR::Color> m_colors;
45+
std::span<const character_t> m_text;
4546
uint32_t m_alignment;
4647
float m_maxPixelWidth;
4748
bool m_scrolling;
@@ -50,8 +51,8 @@ struct CGUIFontCacheKey
5051
float m_scaleY;
5152

5253
CGUIFontCacheKey(Position pos,
53-
std::vector<KODI::UTILS::COLOR::Color>& colors,
54-
vecText& text,
54+
std::span<const KODI::UTILS::COLOR::Color> colors,
55+
std::span<const character_t> text,
5556
uint32_t alignment,
5657
float maxPixelWidth,
5758
bool scrolling,
@@ -75,18 +76,23 @@ template<class Position, class Value>
7576
struct CGUIFontCacheEntry
7677
{
7778
const CGUIFontCache<Position, Value>& m_cache;
78-
CGUIFontCacheKey<Position> m_key;
79+
std::vector<KODI::UTILS::COLOR::Color> m_color;
80+
std::vector<character_t> m_text;
7981
TransformMatrix m_matrix;
82+
CGUIFontCacheKey<Position> m_key;
8083
std::chrono::steady_clock::time_point m_lastUsed;
8184
Value m_value;
8285

8386
CGUIFontCacheEntry(const CGUIFontCache<Position, Value>& cache,
8487
const CGUIFontCacheKey<Position>& key,
8588
std::chrono::steady_clock::time_point now)
8689
: m_cache(cache),
90+
m_color(key.m_colors.begin(), key.m_colors.end()),
91+
m_text(key.m_text.begin(), key.m_text.end()),
92+
m_matrix(key.m_matrix),
8793
m_key(key.m_pos,
88-
*new std::vector<KODI::UTILS::COLOR::Color>,
89-
*new vecText,
94+
m_color,
95+
m_text,
9096
key.m_alignment,
9197
key.m_maxPixelWidth,
9298
key.m_scrolling,
@@ -95,9 +101,6 @@ struct CGUIFontCacheEntry
95101
key.m_scaleY),
96102
m_lastUsed(now)
97103
{
98-
m_key.m_colors.assign(key.m_colors.begin(), key.m_colors.end());
99-
m_key.m_text.assign(key.m_text.begin(), key.m_text.end());
100-
m_matrix = key.m_matrix;
101104
}
102105

103106
~CGUIFontCacheEntry();
@@ -127,8 +130,8 @@ struct CGUIFontCacheKeysMatch
127130
bool operator()(const CGUIFontCacheKey<Position>& a, const CGUIFontCacheKey<Position>& b) const
128131
{
129132
// clang-format off
130-
return a.m_text == b.m_text &&
131-
a.m_colors == b.m_colors &&
133+
return std::ranges::equal(a.m_text, b.m_text) &&
134+
std::ranges::equal(a.m_colors, b.m_colors) &&
132135
a.m_alignment == b.m_alignment &&
133136
a.m_scrolling == b.m_scrolling &&
134137
a.m_maxPixelWidth == b.m_maxPixelWidth &&
@@ -157,8 +160,8 @@ class CGUIFontCache
157160

158161
Value& Lookup(const CGraphicContext& context,
159162
Position& pos,
160-
const std::vector<KODI::UTILS::COLOR::Color>& colors,
161-
const vecText& text,
163+
std::span<const KODI::UTILS::COLOR::Color> colors,
164+
std::span<const character_t> text,
162165
uint32_t alignment,
163166
float maxPixelWidth,
164167
bool scrolling,

xbmc/guilib/GUIFontTTF.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,8 @@ void CGUIFontTTF::End()
365365
void CGUIFontTTF::DrawTextInternal(CGraphicContext& context,
366366
float x,
367367
float y,
368-
const std::vector<KODI::UTILS::COLOR::Color>& colors,
369-
const vecText& text,
368+
std::span<const KODI::UTILS::COLOR::Color> colors,
369+
std::span<const character_t> text,
370370
uint32_t alignment,
371371
float maxPixelWidth,
372372
bool scrolling,
@@ -742,15 +742,15 @@ void CGUIFontTTF::DrawTextInternal(CGraphicContext& context,
742742
End();
743743
}
744744

745-
746-
float CGUIFontTTF::GetTextWidthInternal(const vecText& text)
745+
float CGUIFontTTF::GetTextWidthInternal(std::span<const character_t> text)
747746
{
748747
const std::vector<Glyph> glyphs = GetHarfBuzzShapedGlyphs(text);
749748
return GetTextWidthInternal(text, glyphs);
750749
}
751750

752751
// this routine assumes a single line (i.e. it was called from GUITextLayout)
753-
float CGUIFontTTF::GetTextWidthInternal(const vecText& text, const std::vector<Glyph>& glyphs)
752+
float CGUIFontTTF::GetTextWidthInternal(std::span<const character_t> text,
753+
const std::vector<Glyph>& glyphs)
754754
{
755755
float width = 0;
756756
for (auto it = glyphs.begin(); it != glyphs.end(); it++)
@@ -811,7 +811,8 @@ unsigned int CGUIFontTTF::GetMaxFontHeight() const
811811
return m_maxFontHeight + SPACING_BETWEEN_CHARACTERS_IN_TEXTURE;
812812
}
813813

814-
std::vector<CGUIFontTTF::Glyph> CGUIFontTTF::GetHarfBuzzShapedGlyphs(const vecText& text)
814+
std::vector<CGUIFontTTF::Glyph> CGUIFontTTF::GetHarfBuzzShapedGlyphs(
815+
std::span<const character_t> text)
815816
{
816817
std::vector<Glyph> glyphs;
817818
if (text.empty())

xbmc/guilib/GUIFontTTF.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,10 @@ class CGUIFontTTF
147147
void AddReference();
148148
void RemoveReference();
149149

150-
std::vector<Glyph> GetHarfBuzzShapedGlyphs(const vecText& text);
150+
std::vector<Glyph> GetHarfBuzzShapedGlyphs(std::span<const character_t> text);
151151

152-
float GetTextWidthInternal(const vecText& text);
153-
float GetTextWidthInternal(const vecText& text, const std::vector<Glyph>& glyph);
152+
float GetTextWidthInternal(std::span<const character_t> text);
153+
float GetTextWidthInternal(std::span<const character_t> text, const std::vector<Glyph>& glyph);
154154
float GetCharWidthInternal(character_t ch);
155155
float GetTextHeight(float lineSpacing, int numLines) const;
156156
float GetTextBaseLine() const { return static_cast<float>(m_cellBaseLine); }
@@ -160,8 +160,8 @@ class CGUIFontTTF
160160
void DrawTextInternal(CGraphicContext& context,
161161
float x,
162162
float y,
163-
const std::vector<KODI::UTILS::COLOR::Color>& colors,
164-
const vecText& text,
163+
std::span<const KODI::UTILS::COLOR::Color> colors,
164+
std::span<const character_t> text,
165165
uint32_t alignment,
166166
float maxPixelWidth,
167167
bool scrolling,

0 commit comments

Comments
 (0)