1 /** @file lvfntman.cpp
2     @brief font manager implementation
3 
4     CoolReader Engine
5 
6 
7     (c) Vadim Lopatin, 2000-2006
8     This source code is distributed under the terms of
9     GNU General Public License.
10 
11     See LICENSE file for details.
12 
13 */
14 
15 #include "../include/lvfntman.h"
16 #include "../include/lvstyles.h"
17 #include "../include/crlog.h"
18 #include "private/lvfreetypefontman.h"
19 #include "private/lvwin32fontman.h"
20 #include "private/lvbitmapfontman.h"
21 
22 #define GAMMA_TABLES_IMPL
23 #include "../include/gammatbl.h"
24 
25 LVFontManager *fontMan = NULL;
26 
27 static double gammaLevel = 1.0;
28 int gammaIndex = GAMMA_NO_CORRECTION_INDEX;
29 
30 /// returns first found face from passed list, or return face for font found by family only
findFontFace(lString8 commaSeparatedFaceList,css_font_family_t fallbackByFamily)31 lString8 LVFontManager::findFontFace(lString8 commaSeparatedFaceList, css_font_family_t fallbackByFamily) {
32     // faces we want
33     lString8Collection list;
34     splitPropertyValueList(commaSeparatedFaceList.c_str(), list);
35     // faces we have
36     lString32Collection faces;
37     getFaceList(faces);
38     // find first matched
39     for (int i = 0; i < list.length(); i++) {
40         lString8 wantFace = list[i];
41         for (int j = 0; j < faces.length(); j++) {
42             lString32 haveFace = faces[j];
43             if (wantFace == haveFace)
44                 return wantFace;
45         }
46     }
47     // not matched - get by family name
48     LVFontRef fnt = GetFont(10, 400, false, fallbackByFamily, lString8("Arial"));
49     if (fnt.isNull())
50         return lString8::empty_str; // not found
51     // get face from found font
52     return fnt->getTypeFace();
53 }
54 
55 /// fills array with list of available gamma levels
GetGammaLevels(LVArray<double> dst)56 void LVFontManager::GetGammaLevels(LVArray<double> dst) {
57     dst.clear();
58     for (int i = 0; i < GAMMA_LEVELS; i++)
59         dst.add(cr_gamma_levels[i]);
60 }
61 
62 /// returns current gamma level index
GetGammaIndex()63 int LVFontManager::GetGammaIndex() {
64     return gammaIndex;
65 }
66 
67 /// sets current gamma level index
SetGammaIndex(int index)68 void LVFontManager::SetGammaIndex(int index) {
69     if (index < 0)
70         index = 0;
71     if (index >= GAMMA_LEVELS)
72         index = GAMMA_LEVELS - 1;
73     if (gammaIndex != index) {
74         CRLog::trace("FontManager gamma index changed from %d to %d", gammaIndex, index);
75         gammaIndex = index;
76         gammaLevel = cr_gamma_levels[index];
77         gc();
78         clearGlyphCache();
79     }
80 }
81 
82 /// returns current gamma level
GetGamma()83 double LVFontManager::GetGamma() {
84     return gammaLevel;
85 }
86 
87 /// sets current gamma level
SetGamma(double gamma)88 void LVFontManager::SetGamma( double gamma ) {
89     // gammaLevel = cr_ft_gamma_levels[GAMMA_LEVELS/2];
90     // gammaIndex = GAMMA_LEVELS/2;
91     int oldGammaIndex = gammaIndex;
92     for (int i = 0; i < GAMMA_LEVELS; i++) {
93         double diff1 = cr_gamma_levels[i] - gamma;
94         if (diff1 < 0) diff1 = -diff1;
95         double diff2 = gammaLevel - gamma;
96         if (diff2 < 0) diff2 = -diff2;
97         if (diff1 < diff2) {
98             gammaLevel = cr_gamma_levels[i];
99             gammaIndex = i;
100         }
101     }
102     if (gammaIndex != oldGammaIndex) {
103         CRLog::trace("FontManager gamma index changed from %d to %d", oldGammaIndex, gammaIndex);
104         gc();
105         clearGlyphCache();
106     }
107 }
108 
InitFontManager(lString8 path)109 bool InitFontManager(lString8 path) {
110     if (fontMan) {
111         return true;
112         //delete fontMan;
113     }
114 #if (USE_WIN32_FONTS == 1)
115     fontMan = new LVWin32FontManager;
116 #elif (USE_FREETYPE == 1)
117     fontMan = new LVFreeTypeFontManager;
118 #else
119     fontMan = new LVBitmapFontManager;
120 #endif
121     return fontMan->Init(path);
122 }
123 
ShutdownFontManager()124 bool ShutdownFontManager() {
125     if (fontMan) {
126         delete fontMan;
127         fontMan = NULL;
128         return true;
129     }
130     return false;
131 }
132