wxMaxima
FontCache.h
Go to the documentation of this file.
1 // -*- mode: c++; c-file-style: "linux"; c-basic-offset: 2; indent-tabs-mode: nil -*-
2 //
3 // Copyright (C) 2020 Kuba Ober <kuba@bertec.com>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 //
20 // SPDX-License-Identifier: GPL-2.0+
21 
22 #ifndef FONTCACHE_H
23 #define FONTCACHE_H
24 
25 #include "precomp.h"
26 #include "TextStyle.h"
27 #include <wx/font.h>
28 #include <functional>
29 #include <list>
30 #include <unordered_map>
31 
36 class FontCache final
37 {
38  static constexpr size_t tempFontCount = 8;
39  using TempFonts = std::list<std::pair<const Style, wxFont>>;
40  FontCache(const FontCache &) = delete;
41  FontCache &operator=(const FontCache &) = delete;
42  std::unordered_map<Style, wxFont, StyleFontHasher, StyleFontEquals> m_cache;
44  TempFonts m_temporaryFonts;
45  int m_hits = 0;
46  int m_misses = 0;
47  const bool m_enabled = true;
48  const std::pair<const Style, wxFont> &GetStyleFont(const Style &style, const wxFont &withFont = {});
49  const std::pair<const Style, wxFont> &GetStyleFontUncached(const Style &style, const wxFont &withFont = {});
50 public:
51  FontCache();
52  ~FontCache();
53  const wxFont &GetFont(const Style &style);
54  const Style &AddFont(const wxFont &font);
55  bool IsEnabled() const { return m_enabled; }
56  int GetHits() const { return m_hits; }
57  int GetMisses() const { return m_misses; }
58  void Clear();
59  static FontCache &Get()
60  {
61 #ifdef _WIN32
62  static thread_local FontCache globalCache;
63  // Windows allows font access from multiple threads, as long as each font
64  // is built separately.
65 #else
66  static FontCache globalCache;
67 #endif // _WIN32
68  return globalCache;
69  }
70  static const std::pair<const Style, wxFont> &GetAStyleFont(const Style &style)
71  { return Get().GetStyleFont(style); }
72  static const wxFont &GetAFont(const Style &style) { return Get().GetFont(style); }
73  static const Style &AddAFont(const wxFont &font) { return Get().AddFont(font); }
74 };
75 
76 #endif // FONTCACHE_H
TextStyle.h
Style
Definition: TextStyle.h:130
FontCache
Definition: FontCache.h:36