1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // Description:
29 // Parse a string of features specified as & separated pairs.
30 // e.g.
31 // 1001=1&2002=2&fav1=0
32 
33 // MARKER(update_precomp.py): autogen include statement, do not remove
34 #include "precompiled_vcl.hxx"
35 
36 #include <sal/types.h>
37 
38 #ifdef WNT
39 #include <tools/svwin.h>
40 #include <svsys.h>
41 #endif
42 
43 #include <graphite_features.hxx>
44 
45 using namespace grutils;
46 // These mustn't conflict with font name lists which use ; and ,
47 const char GrFeatureParser::FEAT_PREFIX = ':';
48 const char GrFeatureParser::FEAT_SEPARATOR = '&';
49 const char GrFeatureParser::FEAT_ID_VALUE_SEPARATOR = '=';
50 const std::string GrFeatureParser::ISO_LANG("lang");
51 
52 GrFeatureParser::GrFeatureParser(gr::Font & font, const std::string lang)
53     : mnNumSettings(0), mbErrors(false)
54 {
55     maLang.rgch[0] = maLang.rgch[1] = maLang.rgch[2] = maLang.rgch[3] = '\0';
56     setLang(font, lang);
57 }
58 
59 GrFeatureParser::GrFeatureParser(gr::Font & font, const std::string features, const std::string lang)
60     : mnNumSettings(0), mbErrors(false)
61 {
62     size_t nEquals = 0;
63     size_t nFeatEnd = 0;
64     size_t pos = 0;
65     maLang.rgch[0] = maLang.rgch[1] = maLang.rgch[2] = maLang.rgch[3] = '\0';
66     setLang(font, lang);
67     while (pos < features.length() && mnNumSettings < MAX_FEATURES)
68     {
69         nEquals = features.find(FEAT_ID_VALUE_SEPARATOR,pos);
70         if (nEquals == std::string::npos)
71         {
72             mbErrors = true;
73             break;
74         }
75         // check for a lang=xxx specification
76         if (features.compare(pos, nEquals - pos, ISO_LANG) == 0)
77         {
78             pos = nEquals + 1;
79             nFeatEnd = features.find(FEAT_SEPARATOR, pos);
80             if (nFeatEnd == std::string::npos)
81             {
82                 nFeatEnd = features.length();
83             }
84             if (nFeatEnd - pos > 3)
85                 mbErrors = true;
86             else
87             {
88                 gr::isocode aLang = maLang;
89                 for (size_t i = pos; i < nFeatEnd; i++)
90                     aLang.rgch[i-pos] = features[i];
91                 ext_std::pair<gr::LanguageIterator,gr::LanguageIterator> aSupported
92                     = font.getSupportedLanguages();
93                 gr::LanguageIterator iL = aSupported.first;
94                 while (iL != aSupported.second)
95                 {
96                     gr::isocode aSupportedLang = *iL;
97                     // here we only expect full 3 letter codes
98                     if (aLang.rgch[0] == aSupportedLang.rgch[0] &&
99                         aLang.rgch[1] == aSupportedLang.rgch[1] &&
100                         aLang.rgch[2] == aSupportedLang.rgch[2] &&
101                         aLang.rgch[3] == aSupportedLang.rgch[3]) break;
102                     ++iL;
103                 }
104                 if (iL == aSupported.second) mbErrors = true;
105                 else maLang = aLang;
106             }
107         }
108         else
109         {
110             if (isCharId(features, pos, nEquals - pos))
111                 maSettings[mnNumSettings].id = getCharId(features, pos, nEquals - pos);
112             else maSettings[mnNumSettings].id = getIntValue(features, pos, nEquals - pos);
113             pos = nEquals + 1;
114             nFeatEnd = features.find(FEAT_SEPARATOR, pos);
115             if (nFeatEnd == std::string::npos)
116             {
117                 nFeatEnd = features.length();
118             }
119             if (isCharId(features, pos, nFeatEnd - pos))
120                 maSettings[mnNumSettings].value = getCharId(features, pos, nFeatEnd - pos);
121             else
122                 maSettings[mnNumSettings].value= getIntValue(features, pos, nFeatEnd - pos);
123             if (isValid(font, maSettings[mnNumSettings]))
124                 mnNumSettings++;
125             else
126                 mbErrors = true;
127         }
128         pos = nFeatEnd + 1;
129     }
130 }
131 
132 void GrFeatureParser::setLang(gr::Font & font, const std::string & lang)
133 {
134     gr::isocode aLang = {{0,0,0,0}};
135     if (lang.length() > 2)
136     {
137         for (size_t i = 0; i < lang.length() && i < 3; i++)
138         {
139             if (lang[i] == '-') break;
140             aLang.rgch[i] = lang[i];
141         }
142         ext_std::pair<gr::LanguageIterator,gr::LanguageIterator> aSupported
143                     = font.getSupportedLanguages();
144         gr::LanguageIterator iL = aSupported.first;
145         while (iL != aSupported.second)
146         {
147             gr::isocode aSupportedLang = *iL;
148             if (aLang.rgch[0] == aSupportedLang.rgch[0] &&
149                 aLang.rgch[1] == aSupportedLang.rgch[1] &&
150                 aLang.rgch[2] == aSupportedLang.rgch[2] &&
151                 aLang.rgch[3] == aSupportedLang.rgch[3]) break;
152             ++iL;
153         }
154         if (iL != aSupported.second)
155             maLang = aLang;
156 #ifdef DEBUG
157         else
158             printf("%s has no features\n", aLang.rgch);
159 #endif
160     }
161 }
162 
163 GrFeatureParser::GrFeatureParser(const GrFeatureParser & aCopy)
164  : maLang(aCopy.maLang), mbErrors(aCopy.mbErrors)
165 {
166     mnNumSettings = aCopy.getFontFeatures(maSettings);
167 }
168 
169 GrFeatureParser::~GrFeatureParser()
170 {
171 }
172 
173 size_t GrFeatureParser::getFontFeatures(gr::FeatureSetting settings[64]) const
174 {
175     if (settings)
176     {
177         std::copy(maSettings, maSettings + mnNumSettings, settings);
178     }
179     return mnNumSettings;
180 }
181 
182 bool GrFeatureParser::isValid(gr::Font & font, gr::FeatureSetting & setting)
183 {
184     gr::FeatureIterator i = font.featureWithID(setting.id);
185     if (font.getFeatures().second == i)
186     {
187         return false;
188     }
189     ext_std::pair< gr::FeatureSettingIterator, gr::FeatureSettingIterator >
190         validValues = font.getFeatureSettings(i);
191     gr::FeatureSettingIterator j = validValues.first;
192     while (j != validValues.second)
193     {
194         if (*j == setting.value) return true;
195         ++j;
196     }
197     return false;
198 }
199 
200 bool GrFeatureParser::isCharId(const std::string & id, size_t offset, size_t length)
201 {
202     if (length > 4) return false;
203     for (size_t i = 0; i < length; i++)
204     {
205         if (i > 0 && id[offset+i] == '\0') continue;
206         if ((id[offset+i]) < 0x20 || (id[offset+i]) < 0)
207             return false;
208         if (i==0 && id[offset+i] < 0x41)
209             return false;
210     }
211     return true;
212 }
213 
214 int GrFeatureParser::getCharId(const std::string & id, size_t offset, size_t length)
215 {
216     FeatId charId;
217     charId.num = 0;
218 #ifdef WORDS_BIGENDIAN
219     for (size_t i = 0; i < length; i++)
220     {
221         charId.label[i] = id[offset+i];
222     }
223 #else
224     for (size_t i = 0; i < length; i++)
225     {
226         charId.label[3-i] = id[offset+i];
227     }
228 #endif
229     return charId.num;
230 }
231 
232 int GrFeatureParser::getIntValue(const std::string & id, size_t offset, size_t length)
233 {
234     int value = 0;
235     int sign = 1;
236     for (size_t i = 0; i < length; i++)
237     {
238         switch (id[offset + i])
239         {
240         case '0':
241         case '1':
242         case '2':
243         case '3':
244         case '4':
245         case '5':
246         case '6':
247         case '7':
248         case '8':
249         case '9':
250             value *= 10;
251             if (sign < 0)
252             {
253                 value = -(id[offset + i] - '0');
254                 sign = 1;
255             }
256             value += (id[offset + i] - '0');
257             break;
258         case '-':
259             if (i == 0)
260                 sign = -1;
261             else
262             {
263                 mbErrors = true;
264                 break;
265             }
266         default:
267             mbErrors = true;
268             break;
269         }
270     }
271     return value;
272 }
273 
274 
275 sal_Int32 GrFeatureParser::hashCode() const
276 {
277     union IsoHash { sal_Int32 mInt; gr::isocode mCode; };
278     IsoHash isoHash;
279     isoHash.mCode = maLang;
280     sal_Int32 hash = isoHash.mInt;
281     for (size_t i = 0; i < mnNumSettings; i++)
282     {
283         hash = (hash << 16) ^ ((maSettings[i].id << 8) | maSettings[i].value);
284     }
285     return hash;
286 }
287