gfx/graphite2/src/inc/locale2lcid.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/graphite2/src/inc/locale2lcid.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,450 @@
     1.4 +/*  GRAPHITE2 LICENSING
     1.5 +
     1.6 +    Copyright 2010, SIL International
     1.7 +    All rights reserved.
     1.8 +
     1.9 +    This library is free software; you can redistribute it and/or modify
    1.10 +    it under the terms of the GNU Lesser General Public License as published
    1.11 +    by the Free Software Foundation; either version 2.1 of License, or
    1.12 +    (at your option) any later version.
    1.13 +
    1.14 +    This program is distributed in the hope that it will be useful,
    1.15 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.16 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.17 +    Lesser General Public License for more details.
    1.18 +
    1.19 +    You should also have received a copy of the GNU Lesser General Public
    1.20 +    License along with this library in the file named "LICENSE".
    1.21 +    If not, write to the Free Software Foundation, 51 Franklin Street,
    1.22 +    Suite 500, Boston, MA 02110-1335, USA or visit their web page on the
    1.23 +    internet at http://www.fsf.org/licenses/lgpl.html.
    1.24 +
    1.25 +Alternatively, the contents of this file may be used under the terms of the
    1.26 +Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public
    1.27 +License, as published by the Free Software Foundation, either version 2
    1.28 +of the License or (at your option) any later version.
    1.29 +*/
    1.30 +#pragma once
    1.31 +#include <cstring>
    1.32 +#include <cassert>
    1.33 +
    1.34 +#include "inc/Main.h"
    1.35 +
    1.36 +
    1.37 +namespace graphite2 {
    1.38 +
    1.39 +struct IsoLangEntry
    1.40 +{
    1.41 +    unsigned short mnLang;
    1.42 +    const char maLangStr[4];
    1.43 +    const char maCountry[3];
    1.44 +};
    1.45 +
    1.46 +// Windows Language ID, Locale ISO-639 language, country code as used in
    1.47 +// naming table of OpenType fonts
    1.48 +const IsoLangEntry LANG_ENTRIES[] = {
    1.49 +    { 0x0401, "ar","SA" }, // Arabic Saudi Arabia
    1.50 +    { 0x0402, "bg","BG" }, // Bulgarian Bulgaria
    1.51 +    { 0x0403, "ca","ES" }, // Catalan Catalan
    1.52 +    { 0x0404, "zh","TW" }, // Chinese Taiwan
    1.53 +    { 0x0405, "cs","CZ" }, // Czech Czech Republic
    1.54 +    { 0x0406, "da","DK" }, // Danish Denmark
    1.55 +    { 0x0407, "de","DE" }, // German Germany
    1.56 +    { 0x0408, "el","GR" }, // Greek Greece
    1.57 +    { 0x0409, "en","US" }, // English United States
    1.58 +    { 0x040A, "es","ES" }, // Spanish (Traditional Sort) Spain
    1.59 +    { 0x040B, "fi","FI" }, // Finnish Finland
    1.60 +    { 0x040C, "fr","FR" }, // French France
    1.61 +    { 0x040D, "he","IL" }, // Hebrew Israel
    1.62 +    { 0x040E, "hu","HU" }, // Hungarian Hungary
    1.63 +    { 0x040F, "is","IS" }, // Icelandic Iceland
    1.64 +    { 0x0410, "it","IT" }, // Italian Italy
    1.65 +    { 0x0411, "jp","JP" }, // Japanese Japan
    1.66 +    { 0x0412, "ko","KR" }, // Korean Korea
    1.67 +    { 0x0413, "nl","NL" }, // Dutch Netherlands
    1.68 +    { 0x0414, "no","NO" }, // Norwegian (Bokmal) Norway
    1.69 +    { 0x0415, "pl","PL" }, // Polish Poland
    1.70 +    { 0x0416, "pt","BR" }, // Portuguese Brazil
    1.71 +    { 0x0417, "rm","CH" }, // Romansh Switzerland
    1.72 +    { 0x0418, "ro","RO" }, // Romanian Romania
    1.73 +    { 0x0419, "ru","RU" }, // Russian Russia
    1.74 +    { 0x041A, "hr","HR" }, // Croatian Croatia
    1.75 +    { 0x041B, "sk","SK" }, // Slovak Slovakia
    1.76 +    { 0x041C, "sq","AL" }, // Albanian Albania
    1.77 +    { 0x041D, "sv","SE" }, // Swedish Sweden
    1.78 +    { 0x041E, "th","TH" }, // Thai Thailand
    1.79 +    { 0x041F, "tr","TR" }, // Turkish Turkey
    1.80 +    { 0x0420, "ur","PK" }, // Urdu Islamic Republic of Pakistan
    1.81 +    { 0x0421, "id","ID" }, // Indonesian Indonesia
    1.82 +    { 0x0422, "uk","UA" }, // Ukrainian Ukraine
    1.83 +    { 0x0423, "be","BY" }, // Belarusian Belarus
    1.84 +    { 0x0424, "sl","SI" }, // Slovenian Slovenia
    1.85 +    { 0x0425, "et","EE" }, // Estonian Estonia
    1.86 +    { 0x0426, "lv","LV" }, // Latvian Latvia
    1.87 +    { 0x0427, "lt","LT" }, // Lithuanian Lithuania
    1.88 +    { 0x0428, "tg","TJ" }, // Tajik (Cyrillic) Tajikistan
    1.89 +    { 0x042A, "vi","VN" }, // Vietnamese Vietnam
    1.90 +    { 0x042B, "hy","AM" }, // Armenian Armenia
    1.91 +    { 0x042C, "az","AZ" }, // Azeri (Latin) Azerbaijan
    1.92 +    { 0x042D, "eu","" }, // Basque Basque
    1.93 +    { 0x042E, "hsb","DE" }, // Upper Sorbian Germany
    1.94 +    { 0x042F, "mk","MK" }, // Macedonian (FYROM) Former Yugoslav Republic of Macedonia
    1.95 +    { 0x0432, "tn","ZA" }, // Setswana South Africa
    1.96 +    { 0x0434, "xh","ZA" }, // isiXhosa South Africa
    1.97 +    { 0x0435, "zu","ZA" }, // isiZulu South Africa
    1.98 +    { 0x0436, "af","ZA" }, // Afrikaans South Africa
    1.99 +    { 0x0437, "ka","GE" }, // Georgian Georgia
   1.100 +    { 0x0438, "fo","FO" }, // Faroese Faroe Islands
   1.101 +    { 0x0439, "hi","IN" }, // Hindi India
   1.102 +    { 0x043A, "mt","MT" }, // Maltese Malta
   1.103 +    { 0x043B, "se","NO" }, // Sami (Northern) Norway
   1.104 +    { 0x043E, "ms","MY" }, // Malay Malaysia
   1.105 +    { 0x043F, "kk","KZ" }, // Kazakh Kazakhstan
   1.106 +    { 0x0440, "ky","KG" }, // Kyrgyz Kyrgyzstan
   1.107 +    { 0x0441, "sw","KE" }, // Kiswahili Kenya
   1.108 +    { 0x0442, "tk","TM" }, // Turkmen Turkmenistan
   1.109 +    { 0x0443, "uz","UZ" }, // Uzbek (Latin) Uzbekistan
   1.110 +    { 0x0444, "tt","RU" }, // Tatar Russia
   1.111 +    { 0x0445, "bn","IN" }, // Bengali India
   1.112 +    { 0x0446, "pa","IN" }, // Punjabi India
   1.113 +    { 0x0447, "gu","IN" }, // Gujarati India
   1.114 +    { 0x0448, "or","IN" }, // Oriya India
   1.115 +    { 0x0448, "wo","SN" }, // Wolof Senegal
   1.116 +    { 0x0449, "ta","IN" }, // Tamil India
   1.117 +    { 0x044A, "te","IN" }, // Telugu India
   1.118 +    { 0x044B, "kn","IN" }, // Kannada India
   1.119 +    { 0x044C, "ml","IN" }, // Malayalam India
   1.120 +    { 0x044D, "as","IN" }, // Assamese India
   1.121 +    { 0x044E, "mr","IN" }, // Marathi India
   1.122 +    { 0x044F, "sa","IN" }, // Sanskrit India
   1.123 +    { 0x0450, "mn","MN" }, // Mongolian (Cyrillic) Mongolia
   1.124 +    { 0x0451, "bo","CN" }, // Tibetan PRC
   1.125 +    { 0x0452, "cy","GB" }, // Welsh United Kingdom
   1.126 +    { 0x0453, "km","KH" }, // Khmer Cambodia
   1.127 +    { 0x0454, "lo","LA" }, // Lao Lao P.D.R.
   1.128 +    { 0x0455, "my","MM" }, // Burmese Myanmar - not listed in Microsoft docs anymore
   1.129 +    { 0x0456, "gl","ES" }, // Galician Galician
   1.130 +    { 0x0457, "kok","IN" }, // Konkani India
   1.131 +    { 0x045A, "syr","TR" }, // Syriac Syria
   1.132 +    { 0x045B, "si","LK" }, // Sinhala Sri Lanka
   1.133 +    { 0x045D, "iu","CA" }, // Inuktitut Canada
   1.134 +    { 0x045E, "am","ET" }, // Amharic Ethiopia
   1.135 +    { 0x0461, "ne","NP" }, // Nepali Nepal
   1.136 +    { 0x0462, "fy","NL" }, // Frisian Netherlands
   1.137 +    { 0x0463, "ps","AF" }, // Pashto Afghanistan
   1.138 +    { 0x0464, "fil","PH" }, // Filipino Philippines
   1.139 +    { 0x0465, "dv","MV" }, // Divehi Maldives
   1.140 +    { 0x0468, "ha","NG" }, // Hausa (Latin) Nigeria
   1.141 +    { 0x046A, "yo","NG" }, // Yoruba Nigeria
   1.142 +    { 0x046B, "qu","BO" }, // Quechua Bolivia
   1.143 +    { 0x046C, "st","ZA" }, // Sesotho sa Leboa South Africa
   1.144 +    { 0x046D, "ba","RU" }, // Bashkir Russia
   1.145 +    { 0x046E, "lb","LU" }, // Luxembourgish Luxembourg
   1.146 +    { 0x046F, "kl","GL" }, // Greenlandic Greenland
   1.147 +    { 0x0470, "ig","NG" }, // Igbo Nigeria
   1.148 +    { 0x0478, "ii","CN" }, // Yi PRC
   1.149 +    { 0x047A, "arn","CL" }, // Mapudungun Chile
   1.150 +    { 0x047C, "moh","CA" }, // Mohawk Mohawk
   1.151 +    { 0x047E, "br","FR" }, // Breton France
   1.152 +    { 0x0480, "ug","CN" }, // Uighur PRC
   1.153 +    { 0x0481, "mi","NZ" }, // Maori New Zealand
   1.154 +    { 0x0482, "oc","FR" }, // Occitan France
   1.155 +    { 0x0483, "co","FR" }, // Corsican France
   1.156 +    { 0x0484, "gsw","FR" }, // Alsatian France
   1.157 +    { 0x0485, "sah","RU" }, // Yakut Russia
   1.158 +    { 0x0486, "qut","GT" }, // K'iche Guatemala
   1.159 +    { 0x0487, "rw","RW" }, // Kinyarwanda Rwanda
   1.160 +    { 0x048C, "gbz","AF" }, // Dari Afghanistan
   1.161 +    { 0x0801, "ar","IQ" }, // Arabic Iraq
   1.162 +    { 0x0804, "zn","CH" }, // Chinese People's Republic of China
   1.163 +    { 0x0807, "de","CH" }, // German Switzerland
   1.164 +    { 0x0809, "en","GB" }, // English United Kingdom
   1.165 +    { 0x080A, "es","MX" }, // Spanish Mexico
   1.166 +    { 0x080C, "fr","BE" }, // French Belgium
   1.167 +    { 0x0810, "it","CH" }, // Italian Switzerland
   1.168 +    { 0x0813, "nl","BE" }, // Dutch Belgium
   1.169 +    { 0x0814, "nn","NO" }, // Norwegian (Nynorsk) Norway
   1.170 +    { 0x0816, "pt","PT" }, // Portuguese Portugal
   1.171 +    { 0x081A, "sh","RS" }, // Serbian (Latin) Serbia
   1.172 +    { 0x081D, "sv","FI" }, // Sweden Finland
   1.173 +    { 0x082C, "az","AZ" }, // Azeri (Cyrillic) Azerbaijan
   1.174 +    { 0x082E, "dsb","DE" }, // Lower Sorbian Germany
   1.175 +    { 0x083B, "se","SE" }, // Sami (Northern) Sweden
   1.176 +    { 0x083C, "ga","IE" }, // Irish Ireland
   1.177 +    { 0x083E, "ms","BN" }, // Malay Brunei Darussalam
   1.178 +    { 0x0843, "uz","UZ" }, // Uzbek (Cyrillic) Uzbekistan
   1.179 +    { 0x0845, "bn","BD" }, // Bengali Bangladesh
   1.180 +    { 0x0850, "mn","MN" }, // Mongolian (Traditional) People's Republic of China
   1.181 +    { 0x085D, "iu","CA" }, // Inuktitut (Latin) Canada
   1.182 +    { 0x085F, "ber","DZ" }, // Tamazight (Latin) Algeria
   1.183 +    { 0x086B, "es","EC" }, // Quechua Ecuador
   1.184 +    { 0x0C01, "ar","EG" }, // Arabic Egypt
   1.185 +    { 0x0C04, "zh","HK" }, // Chinese Hong Kong S.A.R.
   1.186 +    { 0x0C07, "de","AT" }, // German Austria
   1.187 +    { 0x0C09, "en","AU" }, // English Australia
   1.188 +    { 0x0C0A, "es","ES" }, // Spanish (Modern Sort) Spain
   1.189 +    { 0x0C0C, "fr","CA" }, // French Canada
   1.190 +    { 0x0C1A, "sr","CS" }, // Serbian (Cyrillic) Serbia
   1.191 +    { 0x0C3B, "se","FI" }, // Sami (Northern) Finland
   1.192 +    { 0x0C6B, "qu","PE" }, // Quechua Peru
   1.193 +    { 0x1001, "ar","LY" }, // Arabic Libya
   1.194 +    { 0x1004, "zh","SG" }, // Chinese Singapore
   1.195 +    { 0x1007, "de","LU" }, // German Luxembourg
   1.196 +    { 0x1009, "en","CA" }, // English Canada
   1.197 +    { 0x100A, "es","GT" }, // Spanish Guatemala
   1.198 +    { 0x100C, "fr","CH" }, // French Switzerland
   1.199 +    { 0x101A, "hr","BA" }, // Croatian (Latin) Bosnia and Herzegovina
   1.200 +    { 0x103B, "smj","NO" }, // Sami (Lule) Norway
   1.201 +    { 0x1401, "ar","DZ" }, // Arabic Algeria
   1.202 +    { 0x1404, "zh","MO" }, // Chinese Macao S.A.R.
   1.203 +    { 0x1407, "de","LI" }, // German Liechtenstein
   1.204 +    { 0x1409, "en","NZ" }, // English New Zealand
   1.205 +    { 0x140A, "es","CR" }, // Spanish Costa Rica
   1.206 +    { 0x140C, "fr","LU" }, // French Luxembourg
   1.207 +    { 0x141A, "bs","BA" }, // Bosnian (Latin) Bosnia and Herzegovina
   1.208 +    { 0x143B, "smj","SE" }, // Sami (Lule) Sweden
   1.209 +    { 0x1801, "ar","MA" }, // Arabic Morocco
   1.210 +    { 0x1809, "en","IE" }, // English Ireland
   1.211 +    { 0x180A, "es","PA" }, // Spanish Panama
   1.212 +    { 0x180C, "fr","MC" }, // French Principality of Monoco
   1.213 +    { 0x181A, "sh","BA" }, // Serbian (Latin) Bosnia and Herzegovina
   1.214 +    { 0x183B, "sma","NO" }, // Sami (Southern) Norway
   1.215 +    { 0x1C01, "ar","TN" }, // Arabic Tunisia
   1.216 +    { 0x1C09, "en","ZA" }, // English South Africa
   1.217 +    { 0x1C0A, "es","DO" }, // Spanish Dominican Republic
   1.218 +    { 0x1C1A, "sr","BA" }, // Serbian (Cyrillic) Bosnia and Herzegovina
   1.219 +    { 0x1C3B, "sma","SE" }, // Sami (Southern) Sweden
   1.220 +    { 0x2001, "ar","OM" }, // Arabic Oman
   1.221 +    { 0x2009, "en","JM" }, // English Jamaica
   1.222 +    { 0x200A, "es","VE" }, // Spanish Venezuela
   1.223 +    { 0x201A, "bs","BA" }, // Bosnian (Cyrillic) Bosnia and Herzegovina
   1.224 +    { 0x203B, "sms","FI" }, // Sami (Skolt) Finland
   1.225 +    { 0x2401, "ar","YE" }, // Arabic Yemen
   1.226 +    { 0x2409, "en","BS" }, // English Caribbean
   1.227 +    { 0x240A, "es","CO" }, // Spanish Colombia
   1.228 +    { 0x243B, "smn","FI" }, // Sami (Inari) Finland
   1.229 +    { 0x2801, "ar","SY" }, // Arabic Syria
   1.230 +    { 0x2809, "en","BZ" }, // English Belize
   1.231 +    { 0x280A, "es","PE" }, // Spanish Peru
   1.232 +    { 0x2C01, "ar","JO" }, // Arabic Jordan
   1.233 +    { 0x2C09, "en","TT" }, // English Trinidad and Tobago
   1.234 +    { 0x2C0A, "es","AR" }, // Spanish Argentina
   1.235 +    { 0x3001, "ar","LB" }, // Arabic Lebanon
   1.236 +    { 0x3009, "en","ZW" }, // English Zimbabwe
   1.237 +    { 0x300A, "es","EC" }, // Spanish Ecuador
   1.238 +    { 0x3401, "ar","KW" }, // Arabic Kuwait
   1.239 +    { 0x3409, "en","PH" }, // English Republic of the Philippines
   1.240 +    { 0x340A, "es","CL" }, // Spanish Chile
   1.241 +    { 0x3801, "ar","AE" }, // Arabic U.A.E.
   1.242 +    { 0x380A, "es","UY" }, // Spanish Uruguay
   1.243 +    { 0x3C01, "ar","BH" }, // Arabic Bahrain
   1.244 +    { 0x3C0A, "es","PY" }, // Spanish Paraguay
   1.245 +    { 0x4001, "ar","QA" }, // Arabic Qatar
   1.246 +    { 0x4009, "en","IN" }, // English India
   1.247 +    { 0x400A, "es","BO" }, // Spanish Bolivia
   1.248 +    { 0x4409, "en","MY" }, // English Malaysia
   1.249 +    { 0x440A, "es","SV" }, // Spanish El Salvador
   1.250 +    { 0x4809, "en","SG" }, // English Singapore
   1.251 +    { 0x480A, "es","HN" }, // Spanish Honduras
   1.252 +    { 0x4C0A, "es","NI" }, // Spanish Nicaragua
   1.253 +    { 0x500A, "es","PR" }, // Spanish Puerto Rico
   1.254 +    { 0x540A, "es","US" } // Spanish United States
   1.255 +};
   1.256 +
   1.257 +class Locale2Lang
   1.258 +{
   1.259 +    Locale2Lang(const Locale2Lang &);
   1.260 +    Locale2Lang & operator = (const Locale2Lang &);
   1.261 +
   1.262 +public:
   1.263 +    Locale2Lang() : mSeedPosition(128)
   1.264 +    {
   1.265 +        memset((void*)mLangLookup, 0, sizeof(mLangLookup));
   1.266 +        // create a tri lookup on first 2 letters of language code
   1.267 +        static const int maxIndex = sizeof(LANG_ENTRIES)/sizeof(IsoLangEntry);
   1.268 +        for (int i = 0; i < maxIndex; i++)
   1.269 +        {
   1.270 +            size_t a = LANG_ENTRIES[i].maLangStr[0] - 'a';
   1.271 +            size_t b = LANG_ENTRIES[i].maLangStr[1] - 'a';
   1.272 +            if (mLangLookup[a][b])
   1.273 +            {
   1.274 +                const IsoLangEntry ** old = mLangLookup[a][b];
   1.275 +                int len = 1;
   1.276 +                while (old[len]) len++;
   1.277 +                len += 2;
   1.278 +                mLangLookup[a][b] = gralloc<const IsoLangEntry *>(len);
   1.279 +                if (!mLangLookup[a][b])
   1.280 +                {
   1.281 +                    mLangLookup[a][b] = old;
   1.282 +                    continue;
   1.283 +                }
   1.284 +                mLangLookup[a][b][--len] = NULL;
   1.285 +                mLangLookup[a][b][--len] = &LANG_ENTRIES[i];
   1.286 +                while (--len >= 0)
   1.287 +                {
   1.288 +                    assert(len >= 0);
   1.289 +                    mLangLookup[a][b][len] = old[len];
   1.290 +                }
   1.291 +                free(old);
   1.292 +            }
   1.293 +            else
   1.294 +            {
   1.295 +                mLangLookup[a][b] = gralloc<const IsoLangEntry *>(2);
   1.296 +                if (!mLangLookup[a][b]) continue;
   1.297 +                mLangLookup[a][b][1] = NULL;
   1.298 +                mLangLookup[a][b][0] = &LANG_ENTRIES[i];
   1.299 +            }
   1.300 +        }
   1.301 +        while (2 * mSeedPosition < maxIndex)
   1.302 +            mSeedPosition *= 2;
   1.303 +    };
   1.304 +    ~Locale2Lang()
   1.305 +    {
   1.306 +        for (int i = 0; i != 26; ++i)
   1.307 +            for (int j = 0; j != 26; ++j)
   1.308 +                free(mLangLookup[i][j]);
   1.309 +    }
   1.310 +    unsigned short getMsId(const char * locale) const
   1.311 +    {
   1.312 +        size_t length = strlen(locale);
   1.313 +        size_t langLength = length;
   1.314 +        const char * language = locale;
   1.315 +        const char * script = NULL;
   1.316 +        const char * region = NULL;
   1.317 +        size_t regionLength = 0;
   1.318 +        const char * dash = strchr(locale, '-');
   1.319 +        if (dash && (dash != locale))
   1.320 +        {
   1.321 +            langLength = (dash - locale);
   1.322 +            size_t nextPartLength = length - langLength - 1;
   1.323 +            if (nextPartLength >= 2)
   1.324 +            {
   1.325 +                script = ++dash;
   1.326 +                dash = strchr(dash, '-');
   1.327 +                if (dash)
   1.328 +                {
   1.329 +                    nextPartLength = (dash - script);
   1.330 +                    region = ++dash;
   1.331 +                }
   1.332 +                if (nextPartLength == 2 &&
   1.333 +                    (locale[langLength+1] > 0x40) && (locale[langLength+1] < 0x5B) &&
   1.334 +                    (locale[langLength+2] > 0x40) && (locale[langLength+2] < 0x5B))
   1.335 +                {
   1.336 +                    region = script;
   1.337 +                    regionLength = nextPartLength;
   1.338 +                    script = NULL;
   1.339 +                }
   1.340 +                else if (nextPartLength == 4)
   1.341 +                {
   1.342 +                    if (dash)
   1.343 +                    {
   1.344 +                        dash = strchr(dash, '-');
   1.345 +                        if (dash)
   1.346 +                        {
   1.347 +                            nextPartLength = (dash - region);
   1.348 +                        }
   1.349 +                        else
   1.350 +                        {
   1.351 +                            nextPartLength = langLength - (region - locale);
   1.352 +                        }
   1.353 +                        regionLength = nextPartLength;
   1.354 +                    }
   1.355 +                }
   1.356 +            }
   1.357 +        }
   1.358 +        size_t a = 'e' - 'a';
   1.359 +        size_t b = 'n' - 'a';
   1.360 +        unsigned short langId = 0;
   1.361 +        int i = 0;
   1.362 +        switch (langLength)
   1.363 +        {
   1.364 +            case 2:
   1.365 +            {
   1.366 +                a = language[0] - 'a';
   1.367 +                b = language[1] - 'a';
   1.368 +                if ((a < 26) && (b < 26) && mLangLookup[a][b])
   1.369 +                {
   1.370 +                    while (mLangLookup[a][b][i])
   1.371 +                    {
   1.372 +                        if (mLangLookup[a][b][i]->maLangStr[2] != '\0')
   1.373 +                        {
   1.374 +                            ++i;
   1.375 +                            continue;
   1.376 +                        }
   1.377 +                        if (region && (strncmp(mLangLookup[a][b][i]->maCountry, region, regionLength) == 0))
   1.378 +                        {
   1.379 +                            langId = mLangLookup[a][b][i]->mnLang;
   1.380 +                            break;
   1.381 +                        }
   1.382 +                        else if (langId == 0)
   1.383 +                        {
   1.384 +                            // possible fallback code
   1.385 +                            langId = mLangLookup[a][b][i]->mnLang;
   1.386 +                        }
   1.387 +                        ++i;
   1.388 +                    }
   1.389 +                }
   1.390 +            }
   1.391 +            break;
   1.392 +            case 3:
   1.393 +            {
   1.394 +                a = language[0] - 'a';
   1.395 +                b = language[1] - 'a';
   1.396 +                if (mLangLookup[a][b])
   1.397 +                {
   1.398 +                    while (mLangLookup[a][b][i])
   1.399 +                    {
   1.400 +                        if (mLangLookup[a][b][i]->maLangStr[2] != language[2])
   1.401 +                        {
   1.402 +                            ++i;
   1.403 +                            continue;
   1.404 +                        }
   1.405 +                        if (region && (strncmp(mLangLookup[a][b][i]->maCountry, region, regionLength) == 0))
   1.406 +                        {
   1.407 +                            langId = mLangLookup[a][b][i]->mnLang;
   1.408 +                            break;
   1.409 +                        }
   1.410 +                        else if (langId == 0)
   1.411 +                        {
   1.412 +                            // possible fallback code
   1.413 +                            langId = mLangLookup[a][b][i]->mnLang;
   1.414 +                        }
   1.415 +                        ++i;
   1.416 +                    }
   1.417 +                }
   1.418 +            }
   1.419 +            break;
   1.420 +            default:
   1.421 +                break;
   1.422 +        }
   1.423 +        if (langId == 0) langId = 0x409;
   1.424 +        return langId;
   1.425 +    }
   1.426 +    const IsoLangEntry * findEntryById(unsigned short langId) const
   1.427 +    {
   1.428 +        static const int maxIndex = sizeof(LANG_ENTRIES)/sizeof(IsoLangEntry);
   1.429 +        int window = mSeedPosition;
   1.430 +        int guess = mSeedPosition - 1;
   1.431 +        while (LANG_ENTRIES[guess].mnLang != langId)
   1.432 +        {
   1.433 +            window /= 2;
   1.434 +            if (window == 0) return NULL;
   1.435 +            guess += (LANG_ENTRIES[guess].mnLang > langId)? -window : window;
   1.436 +            while (guess >= maxIndex)
   1.437 +            {
   1.438 +                window /= 2;
   1.439 +                guess -= window;
   1.440 +                assert(window);
   1.441 +            }
   1.442 +        }
   1.443 +        return &LANG_ENTRIES[guess];
   1.444 +    }
   1.445 +
   1.446 +    CLASS_NEW_DELETE;
   1.447 +
   1.448 +private:
   1.449 +    const IsoLangEntry ** mLangLookup[26][26];
   1.450 +    int mSeedPosition;
   1.451 +};
   1.452 +
   1.453 +} // namespace graphite2

mercurial