Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : // Description:
21 : // Parse a string of features specified as & separated pairs.
22 : // e.g.
23 : // 1001=1&2002=2&fav1=0
24 :
25 :
26 : #include <sal/types.h>
27 :
28 : #ifdef WNT
29 : #include <windows.h>
30 : #endif
31 :
32 : #include <graphite_features.hxx>
33 :
34 : using namespace grutils;
35 : // These mustn't conflict with font name lists which use ; and ,
36 : const char GrFeatureParser::FEAT_PREFIX = ':';
37 : const char GrFeatureParser::FEAT_SEPARATOR = '&';
38 : const char GrFeatureParser::FEAT_ID_VALUE_SEPARATOR = '=';
39 :
40 0 : GrFeatureParser::GrFeatureParser(const gr_face * pFace, const ::rtl::OString lang)
41 0 : : mnNumSettings(0), mbErrors(false), mpSettings(NULL)
42 : {
43 0 : maLang.label[0] = maLang.label[1] = maLang.label[2] = maLang.label[3] = '\0';
44 0 : setLang(pFace, lang);
45 0 : }
46 :
47 0 : GrFeatureParser::GrFeatureParser(const gr_face * pFace, const ::rtl::OString features, const ::rtl::OString lang)
48 0 : : mnNumSettings(0), mbErrors(false), mpSettings(NULL)
49 : {
50 0 : sal_Int32 nEquals = 0;
51 0 : sal_Int32 nFeatEnd = 0;
52 0 : sal_Int32 pos = 0;
53 0 : maLang.num = 0u;
54 0 : setLang(pFace, lang);
55 0 : while ((pos < features.getLength()) && (mnNumSettings < MAX_FEATURES))
56 : {
57 0 : nEquals = features.indexOf(FEAT_ID_VALUE_SEPARATOR, pos);
58 0 : if (nEquals == -1)
59 : {
60 0 : mbErrors = true;
61 : break;
62 : }
63 : // check for a lang=xxx specification
64 0 : const ::rtl::OString aLangPrefix("lang");
65 0 : if (features.match(aLangPrefix, pos ))
66 : {
67 0 : pos = nEquals + 1;
68 0 : nFeatEnd = features.indexOf(FEAT_SEPARATOR, pos);
69 0 : if (nFeatEnd == -1)
70 : {
71 0 : nFeatEnd = features.getLength();
72 : }
73 0 : if (nFeatEnd - pos > 3)
74 0 : mbErrors = true;
75 : else
76 : {
77 0 : FeatId aLang = maLang;
78 0 : aLang.num = 0;
79 0 : for (sal_Int32 i = pos; i < nFeatEnd; i++)
80 0 : aLang.label[i-pos] = features[i];
81 :
82 : //ext_std::pair<gr::LanguageIterator,gr::LanguageIterator> aSupported
83 : // = font.getSupportedLanguages();
84 : //gr::LanguageIterator iL = aSupported.first;
85 0 : unsigned short i = 0;
86 0 : for (; i < gr_face_n_languages(pFace); i++)
87 : {
88 0 : gr_uint32 nFaceLang = gr_face_lang_by_index(pFace, i);
89 : FeatId aSupportedLang;
90 0 : aSupportedLang.num = nFaceLang;
91 : #ifdef __BIG_ENDIAN__
92 : // here we only expect full 3 letter codes
93 : if (aLang.label[0] == aSupportedLang.label[0] &&
94 : aLang.label[1] == aSupportedLang.label[1] &&
95 : aLang.label[2] == aSupportedLang.label[2] &&
96 : aLang.label[3] == aSupportedLang.label[3])
97 : #else
98 0 : if (aLang.label[0] == aSupportedLang.label[3] &&
99 0 : aLang.label[1] == aSupportedLang.label[2] &&
100 0 : aLang.label[2] == aSupportedLang.label[1] &&
101 0 : aLang.label[3] == aSupportedLang.label[0])
102 : #endif
103 : {
104 0 : maLang = aSupportedLang;
105 : break;
106 : }
107 : }
108 0 : if (i == gr_face_n_languages(pFace)) mbErrors = true;
109 : else
110 : {
111 0 : mnHash = maLang.num;
112 0 : mpSettings = gr_face_featureval_for_lang(pFace, maLang.num);
113 : }
114 : }
115 : }
116 : else
117 : {
118 0 : sal_uInt32 featId = 0;
119 0 : if (isCharId(features, pos, nEquals - pos))
120 : {
121 0 : featId = getCharId(features, pos, nEquals - pos);
122 : }
123 : else
124 : {
125 0 : featId = getIntValue(features, pos, nEquals - pos);
126 : }
127 0 : const gr_feature_ref * pFref = gr_face_find_fref(pFace, featId);
128 0 : pos = nEquals + 1;
129 0 : nFeatEnd = features.indexOf(FEAT_SEPARATOR, pos);
130 0 : if (nFeatEnd == -1)
131 : {
132 0 : nFeatEnd = features.getLength();
133 : }
134 0 : sal_Int16 featValue = 0;
135 0 : featValue = getIntValue(features, pos, nFeatEnd - pos);
136 0 : if (pFref && gr_fref_set_feature_value(pFref, featValue, mpSettings))
137 : {
138 0 : mnHash = (mnHash << 16) ^ ((featId << 8) | featValue);
139 0 : mnNumSettings++;
140 : }
141 : else
142 0 : mbErrors = true;
143 : }
144 0 : pos = nFeatEnd + 1;
145 0 : }
146 0 : }
147 :
148 0 : void GrFeatureParser::setLang(const gr_face * pFace, const rtl::OString & lang)
149 : {
150 : FeatId aLang;
151 0 : aLang.num = 0;
152 0 : if (lang.getLength() >= 2)
153 : {
154 0 : for (sal_Int32 i = 0; i < lang.getLength() && i < 3; i++)
155 : {
156 0 : if (lang[i] == '-') break;
157 0 : aLang.label[i] = lang[i];
158 : }
159 0 : unsigned short i = 0;
160 0 : for (; i < gr_face_n_languages(pFace); i++)
161 : {
162 0 : gr_uint32 nFaceLang = gr_face_lang_by_index(pFace, i);
163 : FeatId aSupportedLang;
164 0 : aSupportedLang.num = nFaceLang;
165 : // here we only expect full 2 & 3 letter codes
166 : #ifdef __BIG_ENDIAN__
167 : if (aLang.label[0] == aSupportedLang.label[0] &&
168 : aLang.label[1] == aSupportedLang.label[1] &&
169 : aLang.label[2] == aSupportedLang.label[2] &&
170 : aLang.label[3] == aSupportedLang.label[3])
171 : #else
172 0 : if (aLang.label[0] == aSupportedLang.label[3] &&
173 0 : aLang.label[1] == aSupportedLang.label[2] &&
174 0 : aLang.label[2] == aSupportedLang.label[1] &&
175 0 : aLang.label[3] == aSupportedLang.label[0])
176 : #endif
177 : {
178 0 : maLang = aSupportedLang;
179 : break;
180 : }
181 : }
182 0 : if (i != gr_face_n_languages(pFace))
183 : {
184 0 : if (mpSettings)
185 0 : gr_featureval_destroy(mpSettings);
186 0 : mpSettings = gr_face_featureval_for_lang(pFace, maLang.num);
187 0 : mnHash = maLang.num;
188 : }
189 : }
190 0 : if (!mpSettings)
191 0 : mpSettings = gr_face_featureval_for_lang(pFace, 0);
192 0 : }
193 :
194 0 : GrFeatureParser::~GrFeatureParser()
195 : {
196 0 : if (mpSettings)
197 : {
198 0 : gr_featureval_destroy(mpSettings);
199 0 : mpSettings = NULL;
200 : }
201 0 : }
202 :
203 0 : bool GrFeatureParser::isCharId(const rtl::OString & id, size_t offset, size_t length)
204 : {
205 0 : if (length > 4) return false;
206 0 : for (size_t i = 0; i < length; i++)
207 : {
208 0 : if (i > 0 && id[offset+i] == '\0') continue;
209 0 : if ((id[offset+i] < 0x20) || (id[offset+i] < 0))
210 0 : return false;
211 0 : if (i==0 && (id[offset+i] < 0x41))
212 0 : return false;
213 : }
214 0 : return true;
215 : }
216 :
217 0 : gr_uint32 GrFeatureParser::getCharId(const rtl::OString & id, size_t offset, size_t length)
218 : {
219 : FeatId charId;
220 0 : charId.num = 0;
221 : #ifdef WORDS_BIGENDIAN
222 : for (size_t i = 0; i < length; i++)
223 : {
224 : charId.label[i] = id[offset+i];
225 : }
226 : #else
227 0 : for (size_t i = 0; i < length; i++)
228 : {
229 0 : charId.label[3-i] = id[offset+i];
230 : }
231 : #endif
232 0 : return charId.num;
233 : }
234 :
235 0 : short GrFeatureParser::getIntValue(const rtl::OString & id, size_t offset, size_t length)
236 : {
237 0 : short value = 0;
238 0 : int sign = 1;
239 0 : for (size_t i = 0; i < length; i++)
240 : {
241 0 : switch (id[offset + i])
242 : {
243 : case '0':
244 : case '1':
245 : case '2':
246 : case '3':
247 : case '4':
248 : case '5':
249 : case '6':
250 : case '7':
251 : case '8':
252 : case '9':
253 0 : value *= 10;
254 0 : if (sign < 0)
255 : {
256 0 : value = -(id[offset + i] - '0');
257 0 : sign = 1;
258 : }
259 0 : value += (id[offset + i] - '0');
260 0 : break;
261 : case '-':
262 0 : if (i == 0)
263 0 : sign = -1;
264 : else
265 : {
266 0 : mbErrors = true;
267 0 : break;
268 : }
269 : default:
270 0 : mbErrors = true;
271 0 : break;
272 : }
273 : }
274 0 : return value;
275 : }
276 :
277 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|