2001-09-26 04:40:45 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2000-05-03 03:04:35 +04:00
|
|
|
|
|
|
|
#include "nsLanguageAtomService.h"
|
2015-01-14 08:30:49 +03:00
|
|
|
#include "nsUConvPropertySearch.h"
|
2001-12-17 10:14:49 +03:00
|
|
|
#include "nsUnicharUtils.h"
|
2017-10-03 01:05:19 +03:00
|
|
|
#include "nsAtom.h"
|
2019-03-30 23:18:33 +03:00
|
|
|
#include "nsGkAtoms.h"
|
2015-01-14 08:30:49 +03:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2015-04-03 22:39:23 +03:00
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2017-06-18 14:37:50 +03:00
|
|
|
#include "mozilla/Encoding.h"
|
2017-06-17 05:54:40 +03:00
|
|
|
#include "mozilla/intl/OSPreferences.h"
|
2017-05-26 01:04:13 +03:00
|
|
|
#include "mozilla/ServoBindings.h"
|
2019-03-30 23:18:33 +03:00
|
|
|
#include "mozilla/ServoUtils.h"
|
2000-05-03 03:04:35 +04:00
|
|
|
|
2015-01-14 08:30:49 +03:00
|
|
|
using namespace mozilla;
|
2017-03-22 02:20:07 +03:00
|
|
|
using mozilla::intl::OSPreferences;
|
2015-01-14 08:30:49 +03:00
|
|
|
|
2017-06-17 05:54:40 +03:00
|
|
|
static constexpr nsUConvProp encodingsGroups[] = {
|
|
|
|
#include "encodingsgroups.properties.h"
|
|
|
|
};
|
|
|
|
|
2018-11-26 14:31:37 +03:00
|
|
|
// List of mozilla internal x-* tags that map to themselves (see bug 256257)
|
2019-03-19 15:48:31 +03:00
|
|
|
static constexpr nsStaticAtom* kLangGroups[] = {
|
2018-11-26 14:31:37 +03:00
|
|
|
// This list must be sorted!
|
2019-03-19 15:48:31 +03:00
|
|
|
nsGkAtoms::x_armn, nsGkAtoms::x_cyrillic, nsGkAtoms::x_devanagari,
|
|
|
|
nsGkAtoms::x_geor, nsGkAtoms::x_math, nsGkAtoms::x_tamil,
|
|
|
|
nsGkAtoms::Unicode, nsGkAtoms::x_western
|
2018-11-26 14:31:37 +03:00
|
|
|
// These self-mappings are not necessary unless somebody use them to specify
|
|
|
|
// lang in (X)HTML/XML documents, which they shouldn't. (see bug 256257)
|
|
|
|
// x-beng=x-beng
|
|
|
|
// x-cans=x-cans
|
|
|
|
// x-ethi=x-ethi
|
|
|
|
// x-guru=x-guru
|
|
|
|
// x-gujr=x-gujr
|
|
|
|
// x-khmr=x-khmr
|
|
|
|
// x-mlym=x-mlym
|
|
|
|
};
|
|
|
|
|
|
|
|
// Map ISO 15924 script codes from BCP47 lang tag to mozilla's langGroups.
|
|
|
|
static constexpr struct {
|
|
|
|
const char* mTag;
|
2019-03-19 15:48:31 +03:00
|
|
|
nsStaticAtom* mAtom;
|
2018-11-26 14:31:37 +03:00
|
|
|
} kScriptLangGroup[] = {
|
|
|
|
// This list must be sorted by script code!
|
|
|
|
{"Arab", nsGkAtoms::ar},
|
|
|
|
{"Armn", nsGkAtoms::x_armn},
|
|
|
|
{"Beng", nsGkAtoms::x_beng},
|
|
|
|
{"Cans", nsGkAtoms::x_cans},
|
|
|
|
{"Cyrl", nsGkAtoms::x_cyrillic},
|
|
|
|
{"Deva", nsGkAtoms::x_devanagari},
|
|
|
|
{"Ethi", nsGkAtoms::x_ethi},
|
|
|
|
{"Geok", nsGkAtoms::x_geor},
|
|
|
|
{"Geor", nsGkAtoms::x_geor},
|
|
|
|
{"Grek", nsGkAtoms::el},
|
|
|
|
{"Gujr", nsGkAtoms::x_gujr},
|
|
|
|
{"Guru", nsGkAtoms::x_guru},
|
|
|
|
{"Hang", nsGkAtoms::ko},
|
2019-10-02 18:33:24 +03:00
|
|
|
// Hani is not mapped to a specific langGroup, we prefer to look at the
|
|
|
|
// primary language subtag in this case
|
2018-11-26 14:31:37 +03:00
|
|
|
{"Hans", nsGkAtoms::Chinese},
|
|
|
|
// Hant is special-cased in code
|
|
|
|
// Hant=zh-HK
|
|
|
|
// Hant=zh-TW
|
|
|
|
{"Hebr", nsGkAtoms::he},
|
|
|
|
{"Hira", nsGkAtoms::Japanese},
|
|
|
|
{"Jpan", nsGkAtoms::Japanese},
|
|
|
|
{"Kana", nsGkAtoms::Japanese},
|
|
|
|
{"Khmr", nsGkAtoms::x_khmr},
|
|
|
|
{"Knda", nsGkAtoms::x_knda},
|
|
|
|
{"Kore", nsGkAtoms::ko},
|
|
|
|
{"Latn", nsGkAtoms::x_western},
|
|
|
|
{"Mlym", nsGkAtoms::x_mlym},
|
|
|
|
{"Orya", nsGkAtoms::x_orya},
|
|
|
|
{"Sinh", nsGkAtoms::x_sinh},
|
|
|
|
{"Taml", nsGkAtoms::x_tamil},
|
|
|
|
{"Telu", nsGkAtoms::x_telu},
|
|
|
|
{"Thai", nsGkAtoms::th},
|
|
|
|
{"Tibt", nsGkAtoms::x_tibt}};
|
2015-01-14 08:30:49 +03:00
|
|
|
|
2015-04-03 22:39:23 +03:00
|
|
|
// static
|
|
|
|
nsLanguageAtomService* nsLanguageAtomService::GetService() {
|
|
|
|
static UniquePtr<nsLanguageAtomService> gLangAtomService;
|
|
|
|
if (!gLangAtomService) {
|
|
|
|
gLangAtomService = MakeUnique<nsLanguageAtomService>();
|
|
|
|
ClearOnShutdown(&gLangAtomService);
|
|
|
|
}
|
|
|
|
return gLangAtomService.get();
|
2000-05-03 03:04:35 +04:00
|
|
|
}
|
|
|
|
|
2019-03-19 15:48:31 +03:00
|
|
|
nsStaticAtom* nsLanguageAtomService::LookupLanguage(
|
|
|
|
const nsACString& aLanguage) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString lowered(aLanguage);
|
2001-12-17 10:14:49 +03:00
|
|
|
ToLowerCase(lowered);
|
2004-06-09 03:53:59 +04:00
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
RefPtr<nsAtom> lang = NS_Atomize(lowered);
|
2015-04-03 22:39:23 +03:00
|
|
|
return GetLanguageGroup(lang);
|
2000-05-03 03:04:35 +04:00
|
|
|
}
|
|
|
|
|
2017-06-18 14:37:50 +03:00
|
|
|
already_AddRefed<nsAtom> nsLanguageAtomService::LookupCharSet(
|
|
|
|
NotNull<const Encoding*> aEncoding) {
|
|
|
|
nsAutoCString charset;
|
|
|
|
aEncoding->Name(charset);
|
2014-05-08 13:32:00 +04:00
|
|
|
nsAutoCString group;
|
2017-06-17 05:54:40 +03:00
|
|
|
if (NS_FAILED(nsUConvPropertySearch::SearchPropertyValue(
|
2017-06-18 14:37:50 +03:00
|
|
|
encodingsGroups, ArrayLength(encodingsGroups), charset, group))) {
|
2017-10-03 01:05:19 +03:00
|
|
|
return RefPtr<nsAtom>(nsGkAtoms::Unicode).forget();
|
2017-06-17 05:54:40 +03:00
|
|
|
}
|
2016-03-29 02:09:43 +03:00
|
|
|
return NS_Atomize(group);
|
2000-05-03 03:04:35 +04:00
|
|
|
}
|
2000-07-14 01:10:34 +04:00
|
|
|
|
2017-03-13 06:39:22 +03:00
|
|
|
nsAtom* nsLanguageAtomService::GetLocaleLanguage() {
|
2004-06-09 03:53:59 +04:00
|
|
|
do {
|
2010-02-24 20:57:44 +03:00
|
|
|
if (!mLocaleLanguage) {
|
2017-08-30 11:33:12 +03:00
|
|
|
AutoTArray<nsCString, 10> regionalPrefsLocales;
|
2018-09-22 10:49:05 +03:00
|
|
|
if (NS_SUCCEEDED(OSPreferences::GetInstance()->GetRegionalPrefsLocales(
|
|
|
|
regionalPrefsLocales))) {
|
2017-08-30 11:33:12 +03:00
|
|
|
// use lowercase for all language atoms
|
|
|
|
ToLowerCase(regionalPrefsLocales[0]);
|
|
|
|
mLocaleLanguage = NS_Atomize(regionalPrefsLocales[0]);
|
|
|
|
} else {
|
|
|
|
nsAutoCString locale;
|
|
|
|
OSPreferences::GetInstance()->GetSystemLocale(locale);
|
2004-06-09 03:53:59 +04:00
|
|
|
|
2017-08-30 11:33:12 +03:00
|
|
|
ToLowerCase(locale); // use lowercase for all language atoms
|
|
|
|
mLocaleLanguage = NS_Atomize(locale);
|
|
|
|
}
|
2004-06-09 03:53:59 +04:00
|
|
|
}
|
|
|
|
} while (0);
|
2000-07-14 01:10:34 +04:00
|
|
|
|
2010-02-24 20:57:44 +03:00
|
|
|
return mLocaleLanguage;
|
|
|
|
}
|
|
|
|
|
2019-03-19 15:48:31 +03:00
|
|
|
nsStaticAtom* nsLanguageAtomService::GetLanguageGroup(nsAtom* aLanguage,
|
|
|
|
bool* aNeedsToCache) {
|
|
|
|
if (nsStaticAtom* group = mLangToGroup.Get(aLanguage)) {
|
|
|
|
return group;
|
2017-04-04 21:11:27 +03:00
|
|
|
}
|
2019-03-19 15:48:31 +03:00
|
|
|
if (aNeedsToCache) {
|
|
|
|
*aNeedsToCache = true;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
AssertIsMainThreadOrServoFontMetricsLocked();
|
|
|
|
nsStaticAtom* group = GetUncachedLanguageGroup(aLanguage);
|
|
|
|
mLangToGroup.Put(aLanguage, group);
|
|
|
|
return group;
|
2017-04-04 21:11:27 +03:00
|
|
|
}
|
|
|
|
|
2019-03-19 15:48:31 +03:00
|
|
|
nsStaticAtom* nsLanguageAtomService::GetUncachedLanguageGroup(
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* aLanguage) const {
|
2017-04-04 21:11:27 +03:00
|
|
|
nsAutoCString langStr;
|
|
|
|
aLanguage->ToUTF8String(langStr);
|
2017-08-28 10:06:11 +03:00
|
|
|
ToLowerCase(langStr);
|
2017-04-04 21:11:27 +03:00
|
|
|
|
2018-11-26 14:31:37 +03:00
|
|
|
if (langStr[0] == 'x' && langStr[1] == '-') {
|
|
|
|
// Internal x-* langGroup codes map to themselves (see bug 256257)
|
2019-03-19 15:48:31 +03:00
|
|
|
for (nsStaticAtom* langGroup : kLangGroups) {
|
|
|
|
if (langGroup == aLanguage) {
|
|
|
|
return langGroup;
|
|
|
|
}
|
|
|
|
if (aLanguage->IsAsciiLowercase()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Do the slow ascii-case-insensitive comparison just if needed.
|
|
|
|
nsDependentAtomString string(langGroup);
|
|
|
|
if (string.EqualsASCII(langStr.get(), langStr.Length())) {
|
|
|
|
return langGroup;
|
|
|
|
}
|
2018-11-26 14:31:37 +03:00
|
|
|
}
|
2018-11-26 22:25:28 +03:00
|
|
|
} else {
|
2018-11-26 14:31:37 +03:00
|
|
|
// If the lang code can be parsed as BCP47, look up its (likely) script
|
|
|
|
Locale loc(langStr);
|
|
|
|
if (loc.IsWellFormed()) {
|
2019-10-02 18:33:24 +03:00
|
|
|
// Fill in script subtag if not present.
|
2018-11-26 14:31:37 +03:00
|
|
|
if (loc.GetScript().IsEmpty()) {
|
|
|
|
loc.AddLikelySubtags();
|
|
|
|
}
|
2019-10-02 18:33:24 +03:00
|
|
|
// Traditional Chinese has separate prefs for Hong Kong / Taiwan;
|
|
|
|
// check the region subtag.
|
2018-11-26 14:31:37 +03:00
|
|
|
if (loc.GetScript().EqualsLiteral("Hant")) {
|
|
|
|
if (loc.GetRegion().EqualsLiteral("HK")) {
|
2019-03-19 15:48:31 +03:00
|
|
|
return nsGkAtoms::HongKongChinese;
|
2018-11-26 14:31:37 +03:00
|
|
|
}
|
2019-03-19 15:48:31 +03:00
|
|
|
return nsGkAtoms::Taiwanese;
|
2019-10-02 18:33:24 +03:00
|
|
|
}
|
|
|
|
// Search list of known script subtags that map to langGroup codes.
|
|
|
|
size_t foundIndex;
|
2019-11-20 02:04:06 +03:00
|
|
|
const nsDependentCSubstring& script = loc.GetScript();
|
2019-10-02 18:33:24 +03:00
|
|
|
if (BinarySearchIf(
|
|
|
|
kScriptLangGroup, 0, ArrayLength(kScriptLangGroup),
|
|
|
|
[script](const auto& entry) -> int {
|
|
|
|
return script.Compare(entry.mTag);
|
|
|
|
},
|
|
|
|
&foundIndex)) {
|
|
|
|
return kScriptLangGroup[foundIndex].mAtom;
|
|
|
|
}
|
|
|
|
// Script subtag was not recognized (includes "Hani"); check the language
|
|
|
|
// subtag for CJK possibilities so that we'll prefer the appropriate font
|
|
|
|
// rather than falling back to the browser's hardcoded preference.
|
|
|
|
if (loc.GetLanguage().EqualsLiteral("zh")) {
|
|
|
|
if (loc.GetRegion().EqualsLiteral("HK")) {
|
|
|
|
return nsGkAtoms::HongKongChinese;
|
|
|
|
}
|
|
|
|
if (loc.GetRegion().EqualsLiteral("TW")) {
|
|
|
|
return nsGkAtoms::Taiwanese;
|
2018-11-26 14:31:37 +03:00
|
|
|
}
|
2019-10-02 18:33:24 +03:00
|
|
|
return nsGkAtoms::Chinese;
|
|
|
|
}
|
|
|
|
if (loc.GetLanguage().EqualsLiteral("ja")) {
|
|
|
|
return nsGkAtoms::Japanese;
|
|
|
|
}
|
|
|
|
if (loc.GetLanguage().EqualsLiteral("ko")) {
|
|
|
|
return nsGkAtoms::ko;
|
2018-11-26 14:31:37 +03:00
|
|
|
}
|
2017-04-04 21:11:27 +03:00
|
|
|
}
|
2017-04-09 18:24:54 +03:00
|
|
|
}
|
2017-04-04 21:11:27 +03:00
|
|
|
|
2018-11-26 14:31:37 +03:00
|
|
|
// Fall back to x-unicode if no match was found
|
2019-03-19 15:48:31 +03:00
|
|
|
return nsGkAtoms::Unicode;
|
2000-07-14 01:10:34 +04:00
|
|
|
}
|