CGM Objects Library
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
FontCache.h
1 // FontCache.h: interface for the CFontCache class.
2 //
5 
6 #if !defined(LST_FONTCACHE_H__INCLUDED_)
7 #define LST_FONTCACHE_H__INCLUDED_
8 
9 #pragma once
10 
11 #include <string>
12 #include <vector>
13 #include <map>
14 
15 #include "FontProperties.h"
16 #include "FontCacheEntry.h"
17 #include "Utility/LSTException.h"
18 #include "Utility/Config.h"
19 #include "Utility/wstring.h"
20 
21 #include <ft2build.h>
22 #include FT_FREETYPE_H
23 
24 #ifdef WIN32
25 #define SLASHC '\\'
26 static const wchar_t *cvFonts = L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts";
27 static const wchar_t *cvT1fonts = L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Type 1 Installer\\Type 1 Fonts";
28 static const wchar_t *lstFontCacheDir = L"\\Larson Software Technology\\Common";
29 static const wchar_t *lstFontCacheFile = L"\\FontCache.dat";
30 static const wchar_t *baseFontPath = L""; // what else is needed after windows font registry
31 #endif
32 
33 #ifdef LINUX
34 #define SLASHC '/'
35 static const wchar_t *cvFonts = L"";
36 static const wchar_t *cvT1fonts = L"";
37 static const wchar_t *lstFontCacheDir = L"/common";
38 static const wchar_t *lstFontCacheFile = L"/FontCache.dat";
39 static const wchar_t *baseFontPath = L"~/.larson/common/fonts;/usr/local/lib/larson/fonts;/usr/local/lib64/larson/fonts;~/.fonts;/usr/share/fonts;/usr/share/X11/fonts";
40 #endif
41 
42 #ifdef EMSCRIPTEN
43 #define SLASHC '/'
44 static const wchar_t *cvFonts = L"";
45 static const wchar_t *cvT1fonts = L"";
46 static const wchar_t *lstFontCacheDir = L"/";
47 static const wchar_t *lstFontCacheFile = L"/FontCache.dat";
48 static const wchar_t *baseFontPath = L"/fonts";
49 #endif
50 
51 #ifdef IOS
52 #define SLASHC '/'
53 static const wchar_t *cvFonts = L"";
54 static const wchar_t *cvT1fonts = L"";
55 static const wchar_t *lstFontCacheDir = L"/Larson Software Technology/Common";
56 static const wchar_t *lstFontCacheFile = L"/FontCache.dat";
57 // On IOS devices, simulator directory tends to be ..Font, device is ,,Fonts/Cache so search both
58 // 14apr2015 dml- on IOS 7 or 8 this changed to /System/Library/Fonts/Core
59 static const wchar_t *baseFontPath = L"/System/Library/Fonts;/System/Library/Fonts/Cache;/System/Library/Fonts/Core";
60 #endif
61 
62 #ifdef ANDROID
63 #define SLASHC '/'
64 static const wchar_t *cvFonts = L"";
65 static const wchar_t *cvT1fonts = L"";
66 static const wchar_t *lstFontCacheDir = L""; // the "common data path" directory
67 static const wchar_t *lstFontCacheFile = L"/FontCache.dat";
68 static const wchar_t *baseFontPath = L"/system/fonts";
69 #endif
70 
71 
72 namespace Larson
73 {
75  class MapEntry {
76  public:
77  MapEntry() { m_unicodeName = false; matchName = NULL; fontEntry = NULL; };
78 
80  bool m_unicodeName;
81  std::wstring* matchName;
82  CFontCacheEntry *fontEntry;
83  };
84 
85  typedef multimap <wchar_t, MapEntry, less<wchar_t> > cache_type;
86 
87  class CLSTFont;
89  class CFontCache
90  {
91  //
93  //
94  public:
96  eUnableToInitializeFreeType = EXC_FONTCACHE,
97  eUnableToGenerateFontCache = EXC_FONTCACHE + 1,
98  eUnableToWriteFontCacheFile = EXC_FONTCACHE + 2,
99  eUnableToOpenFontCacheFile = EXC_FONTCACHE + 3
100  };
101 
102  typedef cache_type::value_type value_type;
103 
104  wchar_t *keys[2]; // = {
105 
106  static CFontCache* Instance();
107 
108  void serialize();
109  void restore(Larson::CConfig* config);
110 
111  FT_Face findFace(Larson::CConfig* config, CLSTFont *);
112  int getFacesSize() { return (int)m_fontEntries.size(); };
113  std::wstring getFaceFamilyByIndex(int index) { return m_fontEntries[index].m_familyName; };
114  std::wstring getFacePathByIndex(int index) { return m_fontEntries[index].m_path; };
115 
116  protected:
117  CFontCache() {
118 
119  keys[0] = (wchar_t*)cvFonts;
120  keys[1] = (wchar_t*)cvT1fonts;
121 
122  m_cacheLoaded = false;
123  m_library = NULL;
124  m_diskCacheOld = false;
125  m_version = LST_CACHE_VERSION;
126  m_checkvalue = 0;
127  };
128  ~CFontCache();
129 
130  private:
131  FT_Error newFace(std::string path, int faceIndex, FT_Face *face);
132  FT_Face loadFace(CFontCacheEntry* entry);
133  FT_Face matchSubstituteFont(const std::wstring &subName);
134  CFontCacheEntry* matchByProps(CFontProperties& props);
135  CFontCacheEntry::Weight type1WeightStringToValue(string weight);
136  FT_Face trySubstitution(vector<std::wstring> substitutes);
137 
138  void addFaceToFaceVector(FT_Face face, std::wstring path);
139  void organizeNamesInCache();
140  bool diskCacheExists();
141  unsigned long getFontCheckValue(Larson::CConfig* config);
142 
143  void generateCache(Larson::CConfig* config);
144  inline int nEquals(std::wstring str1, std::wstring str2)
145  {
146  int i = 0;
147  for (; i < (int)str1.size() && i < (int)str2.size(); i++)
148  {
149  if (str1[i] != str2[i])
150  break;
151  }
152  return i;
153  };
154 
155  FT_Library m_library;
156  cache_type m_cache;
157  vector<CFontCacheEntry> m_fontEntries;
158  MUTEX m_cacheMutex;
159  unsigned long m_checkvalue; // holds checkvalue of installed font filenames
160  unsigned long m_version; // font cache version number, triggers recaching when mismatched
161  bool m_diskCacheOld;
162  std::wstring m_fontCachePath;
163 
164  static void Destroy() {
165  delete m_pInstance;
166  };
167 
168  static bool m_cacheLoaded;
169  static CFontCache* m_pInstance;
170  static MUTEX m_mutex;
171  };
172 }
173 
174 #endif // LST_FONTCACHE_H__INCLUDED_
175 
CFontCache – generates and maintains font cache.
Definition: FontCache.h:89
ExceptionCodes
A container for all the available system fonts.
Definition: FontCache.h:95
bool m_unicodeName
is unicode name
Definition: FontCache.h:77
CLSTFont – Font object class, one per facename used in a CGM picture.
Definition: Font.h:28
FontCache map entry.
Definition: FontCache.h:75
CFontCacheEntry – defines the data in a font cache entry.
Definition: FontCacheEntry.h:20