bump product version to 4.1.6.2
[LibreOffice.git] / vcl / source / glyphs / graphite_features.cxx
blob6801140683ef10877288b0265f04ea18813917e3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 // Description:
21 // Parse a string of features specified as & separated pairs.
22 // e.g.
23 // 1001=1&2002=2&fav1=0
26 #include <sal/types.h>
27 #include <osl/endian.h>
29 #ifdef WNT
30 #include <windows.h>
31 #endif
33 #include <graphite_features.hxx>
35 using namespace grutils;
36 // These mustn't conflict with font name lists which use ; and ,
37 const char GrFeatureParser::FEAT_PREFIX = ':';
38 const char GrFeatureParser::FEAT_SEPARATOR = '&';
39 const char GrFeatureParser::FEAT_ID_VALUE_SEPARATOR = '=';
41 GrFeatureParser::GrFeatureParser(const gr_face * pFace, const OString lang)
42 : mnNumSettings(0), mbErrors(false), mpSettings(NULL)
44 maLang.label[0] = maLang.label[1] = maLang.label[2] = maLang.label[3] = '\0';
45 setLang(pFace, lang);
48 GrFeatureParser::GrFeatureParser(const gr_face * pFace, const OString features, const OString lang)
49 : mnNumSettings(0), mbErrors(false), mpSettings(NULL)
51 sal_Int32 nEquals = 0;
52 sal_Int32 nFeatEnd = 0;
53 sal_Int32 pos = 0;
54 maLang.num = 0u;
55 setLang(pFace, lang);
56 while ((pos < features.getLength()) && (mnNumSettings < MAX_FEATURES))
58 nEquals = features.indexOf(FEAT_ID_VALUE_SEPARATOR, pos);
59 if (nEquals == -1)
61 mbErrors = true;
62 break;
64 // check for a lang=xxx specification
65 const OString aLangPrefix("lang");
66 if (features.match(aLangPrefix, pos ))
68 pos = nEquals + 1;
69 nFeatEnd = features.indexOf(FEAT_SEPARATOR, pos);
70 if (nFeatEnd == -1)
72 nFeatEnd = features.getLength();
74 if (nFeatEnd - pos > 3)
75 mbErrors = true;
76 else
78 FeatId aLang = maLang;
79 aLang.num = 0;
80 for (sal_Int32 i = pos; i < nFeatEnd; i++)
81 aLang.label[i-pos] = features[i];
83 //ext_std::pair<gr::LanguageIterator,gr::LanguageIterator> aSupported
84 // = font.getSupportedLanguages();
85 //gr::LanguageIterator iL = aSupported.first;
86 unsigned short i = 0;
87 for (; i < gr_face_n_languages(pFace); i++)
89 gr_uint32 nFaceLang = gr_face_lang_by_index(pFace, i);
90 FeatId aSupportedLang;
91 aSupportedLang.num = nFaceLang;
92 #ifdef OSL_BIGENDIAN
93 // here we only expect full 3 letter codes
94 if (aLang.label[0] == aSupportedLang.label[0] &&
95 aLang.label[1] == aSupportedLang.label[1] &&
96 aLang.label[2] == aSupportedLang.label[2] &&
97 aLang.label[3] == aSupportedLang.label[3])
98 #else
99 if (aLang.label[0] == aSupportedLang.label[3] &&
100 aLang.label[1] == aSupportedLang.label[2] &&
101 aLang.label[2] == aSupportedLang.label[1] &&
102 aLang.label[3] == aSupportedLang.label[0])
103 #endif
105 maLang = aSupportedLang;
106 break;
109 if (i == gr_face_n_languages(pFace)) mbErrors = true;
110 else
112 mnHash = maLang.num;
113 mpSettings = gr_face_featureval_for_lang(pFace, maLang.num);
117 else
119 sal_uInt32 featId = 0;
120 if (isCharId(features, pos, nEquals - pos))
122 featId = getCharId(features, pos, nEquals - pos);
124 else
126 featId = getIntValue(features, pos, nEquals - pos);
128 const gr_feature_ref * pFref = gr_face_find_fref(pFace, featId);
129 pos = nEquals + 1;
130 nFeatEnd = features.indexOf(FEAT_SEPARATOR, pos);
131 if (nFeatEnd == -1)
133 nFeatEnd = features.getLength();
135 sal_Int16 featValue = 0;
136 featValue = getIntValue(features, pos, nFeatEnd - pos);
137 if (pFref && gr_fref_set_feature_value(pFref, featValue, mpSettings))
139 mnHash = (mnHash << 16) ^ ((featId << 8) | featValue);
140 mnNumSettings++;
142 else
143 mbErrors = true;
145 pos = nFeatEnd + 1;
149 void GrFeatureParser::setLang(const gr_face * pFace, const OString & lang)
151 FeatId aLang;
152 aLang.num = 0;
153 if (lang.getLength() >= 2)
155 for (sal_Int32 i = 0; i < lang.getLength() && i < 3; i++)
157 if (lang[i] == '-') break;
158 aLang.label[i] = lang[i];
160 unsigned short i = 0;
161 for (; i < gr_face_n_languages(pFace); i++)
163 gr_uint32 nFaceLang = gr_face_lang_by_index(pFace, i);
164 FeatId aSupportedLang;
165 aSupportedLang.num = nFaceLang;
166 // here we only expect full 2 & 3 letter codes
167 #ifdef OSL_BIGENDIAN
168 if (aLang.label[0] == aSupportedLang.label[0] &&
169 aLang.label[1] == aSupportedLang.label[1] &&
170 aLang.label[2] == aSupportedLang.label[2] &&
171 aLang.label[3] == aSupportedLang.label[3])
172 #else
173 if (aLang.label[0] == aSupportedLang.label[3] &&
174 aLang.label[1] == aSupportedLang.label[2] &&
175 aLang.label[2] == aSupportedLang.label[1] &&
176 aLang.label[3] == aSupportedLang.label[0])
177 #endif
179 maLang = aSupportedLang;
180 break;
183 if (i != gr_face_n_languages(pFace))
185 if (mpSettings)
186 gr_featureval_destroy(mpSettings);
187 mpSettings = gr_face_featureval_for_lang(pFace, maLang.num);
188 mnHash = maLang.num;
191 if (!mpSettings)
192 mpSettings = gr_face_featureval_for_lang(pFace, 0);
195 GrFeatureParser::~GrFeatureParser()
197 if (mpSettings)
199 gr_featureval_destroy(mpSettings);
200 mpSettings = NULL;
204 bool GrFeatureParser::isCharId(const OString & id, size_t offset, size_t length)
206 if (length > 4) return false;
207 for (size_t i = 0; i < length; i++)
209 if (i > 0 && id[offset+i] == '\0') continue;
210 if ((id[offset+i] < 0x20) || (id[offset+i] < 0))
211 return false;
212 if (i==0 && (id[offset+i] < 0x41))
213 return false;
215 return true;
218 gr_uint32 GrFeatureParser::getCharId(const OString & id, size_t offset, size_t length)
220 FeatId charId;
221 charId.num = 0;
222 #ifdef OSL_BIGENDIAN
223 for (size_t i = 0; i < length; i++)
225 charId.label[i] = id[offset+i];
227 #else
228 for (size_t i = 0; i < length; i++)
230 charId.label[3-i] = id[offset+i];
232 #endif
233 return charId.num;
236 short GrFeatureParser::getIntValue(const OString & id, size_t offset, size_t length)
238 short value = 0;
239 int sign = 1;
240 for (size_t i = 0; i < length; i++)
242 switch (id[offset + i])
244 case '0':
245 case '1':
246 case '2':
247 case '3':
248 case '4':
249 case '5':
250 case '6':
251 case '7':
252 case '8':
253 case '9':
254 value *= 10;
255 if (sign < 0)
257 value = -(id[offset + i] - '0');
258 sign = 1;
260 value += (id[offset + i] - '0');
261 break;
262 case '-':
263 if (i == 0)
264 sign = -1;
265 else
267 mbErrors = true;
268 break;
270 default:
271 mbErrors = true;
272 break;
275 return value;
278 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */