Source/WebCore/CMakeLists.txt

@@SET(WebCore_SOURCES
11561156 platform/text/BidiContext.cpp
11571157 platform/text/Hyphenation.cpp
11581158 platform/text/LineEnding.cpp
 1159 platform/text/LocaleToScriptMappingDefault.cpp
11591160 platform/text/LocalizedDateNone.cpp
11601161 platform/text/LocalizedNumberNone.cpp
11611162 platform/text/QuotedPrintable.cpp

Source/WebCore/ChangeLog

 12011-07-21 Matt Falkenhagen <falken@chromium.org>
 2
 3 Use -webkit-locale for font selection.
 4 https://bugs.webkit.org/show_bug.cgi?id=10874
 5
 6 Reviewed by NOBODY (OOPS!).
 7
 8 Infer a script from -webkit-locale to use to choose a font for generic
 9 font families and the default unstyled font. The font is retrieved
 10 from per-script font settings (see bug 20797). Since these settings
 11 have not changed there should be no visible effect yet.
 12
 13 * CMakeLists.txt:
 14 * GNUmakefile.list.am:
 15 * WebCore.gyp/WebCore.gyp:
 16 * WebCore.gypi:
 17 * WebCore.pro:
 18 * WebCore.vcproj/WebCore.vcproj:
 19 * WebCore.xcodeproj/project.pbxproj:
 20 * css/CSSFontSelector.cpp: Use script when getting font from Settings
 21 (WebCore::fontDataForGenericFamily):
 22 (WebCore::CSSFontSelector::getFontData):
 23 * css/CSSPropertyNames.in: Increase priority of -webkit-locale since it affects font
 24 * css/CSSStyleSelector.cpp:
 25 (WebCore::CSSStyleSelector::styleForDocument):
 26 (WebCore::CSSStyleSelector::applyDeclarations):
 27 (WebCore::CSSStyleSelector::applyProperty): Set script in font based on -webkit-locale
 28 * page/Settings.cpp:
 29 (WebCore::getGenericFontFamilyForScript): Fallback to USCRIPT_COMMON
 30 * platform/graphics/FontDescription.h: Add m_script
 31 (WebCore::FontDescription::FontDescription):
 32 (WebCore::FontDescription::script):
 33 (WebCore::FontDescription::setScript):
 34 (WebCore::FontDescription::operator==):
 35 * platform/text/LocaleToScriptMapping.h: Added.
 36 * platform/text/LocaleToScriptMappingDefault.cpp: Added.
 37 (WebCore::localeToScriptCode):
 38 (WebCore::LocaleScript::if):
 39 (WebCore::LocaleScript::while):
 40 * platform/text/LocaleToScriptMappingICU.cpp: Added.
 41 (WebCore::canonicalizeScriptCode):
 42 (WebCore::localeToScriptCode):
 43
1442011-07-21 Kenneth Russell <kbr@google.com>
245
346 Update webglcontextlost / webglcontextrestored delivery to match spec changes

Source/WebCore/GNUmakefile.list.am

@@webcore_sources += \
27912791 Source/WebCore/platform/text/LineBreakIteratorPoolICU.h \
27922792 Source/WebCore/platform/text/LineEnding.cpp \
27932793 Source/WebCore/platform/text/LineEnding.h \
 2794 Source/WebCore/platform/text/LocaleToScriptMapping.h \
 2795 Source/WebCore/platform/text/LocaleToScriptMappingDefault.cpp \
27942796 Source/WebCore/platform/text/LocalizedDate.h \
27952797 Source/WebCore/platform/text/LocalizedDateNone.cpp \
27962798 Source/WebCore/platform/text/LocalizedNumber.h \

Source/WebCore/WebCore.gyp/WebCore.gyp

13551355 ['exclude', 'platform/image-encoders/PNGImageEncoder\\.(cpp|h)$'],
13561356 ['exclude', 'platform/network/ResourceHandle\\.cpp$'],
13571357 ['exclude', 'platform/sql/SQLiteFileSystem\\.cpp$'],
 1358 ['exclude', 'platform/text/LocaleToScriptMappingDefault\\.cpp$'],
13581359 ['exclude', 'platform/text/LocalizedNumberNone\\.cpp$'],
13591360 ['exclude', 'platform/text/TextEncodingDetectorNone\\.cpp$'],
13601361 ],

Source/WebCore/WebCore.gypi

49834983 'platform/text/Hyphenation.cpp',
49844984 'platform/text/Hyphenation.h',
49854985 'platform/text/LineEnding.cpp',
 4986 'platform/text/LocaleToScriptMapping.h',
 4987 'platform/text/LocaleToScriptMappingDefault.cpp',
 4988 'platform/text/LocaleToScriptMappingICU.cpp',
49864989 'platform/text/LocalizedDate.h',
49874990 'platform/text/LocalizedDateNone.cpp',
49884991 'platform/text/LocalizedNumber.h',

Source/WebCore/WebCore.pro

@@SOURCES += \
959959 platform/text/Base64.cpp \
960960 platform/text/BidiContext.cpp \
961961 platform/text/Hyphenation.cpp \
 962 platform/text/LocaleToScriptMapping.cpp \
962963 platform/text/LocalizedDateNone.cpp \
963964 platform/text/LocalizedNumberNone.cpp \
964965 platform/text/QuotedPrintable.cpp \

@@HEADERS += \
20242025 platform/leveldb/LevelDBWriteBatch.h \
20252026 platform/text/BidiRunList.h \
20262027 platform/text/LineEnding.h \
 2028 platform/text/LocaleToScriptMapping.h \
20272029 platform/text/TextCheckerClient.h \
20282030 platform/text/TextChecking.h \
20292031 platform/text/UnicodeBidi.h \

Source/WebCore/WebCore.vcproj/WebCore.vcproj

3144531445 >
3144631446 </File>
3144731447 <File
 31448 RelativePath="..\platform\text\LocaleToScriptMappingDefault.cpp"
 31449 >
 31450 </File>
 31451 <File
 31452 RelativePath="..\platform\text\LocaleToScriptMapping.h"
 31453 >
 31454 </File>
 31455 <File
3144831456 RelativePath="..\platform\text\LocalizedDate.h"
3144931457 >
3145031458 </File>

Source/WebCore/WebCore.xcodeproj/project.pbxproj

17051705 75793ED40D0CE85B007FC0AC /* DOMMessageEvent.mm in Sources */ = {isa = PBXBuildFile; fileRef = 75793ED10D0CE85B007FC0AC /* DOMMessageEvent.mm */; };
17061706 75793ED50D0CE85B007FC0AC /* DOMMessageEventInternal.h in Headers */ = {isa = PBXBuildFile; fileRef = 75793ED20D0CE85B007FC0AC /* DOMMessageEventInternal.h */; };
17071707 758978EC127090D60076D5A9 /* SpeechInputResult.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 758978EA127090D60076D5A9 /* SpeechInputResult.cpp */; };
 1708 7633A72613D8B33A008501B6 /* LocaleToScriptMapping.h in Headers */ = {isa = PBXBuildFile; fileRef = 7633A72413D8B33A008501B6 /* LocaleToScriptMapping.h */; };
 1709 7633A72713D8B33A008501B6 /* LocaleToScriptMappingDefault.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7633A72513D8B33A008501B6 /* LocaleToScriptMappingDefault.cpp */; };
17081710 7637C541112E7B74003D6CDC /* WebSocketHandshakeRequest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7637C540112E7B74003D6CDC /* WebSocketHandshakeRequest.cpp */; };
17091711 7637C543112E7B7E003D6CDC /* WebSocketHandshakeRequest.h in Headers */ = {isa = PBXBuildFile; fileRef = 7637C542112E7B7E003D6CDC /* WebSocketHandshakeRequest.h */; };
17101712 767F99BE11A1194A0080C51D /* WebSocketHandshakeResponse.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 767F99BD11A1194A0080C51D /* WebSocketHandshakeResponse.cpp */; };

83098311 75793ED20D0CE85B007FC0AC /* DOMMessageEventInternal.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = DOMMessageEventInternal.h; sourceTree = "<group>"; };
83108312 758978EA127090D60076D5A9 /* SpeechInputResult.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SpeechInputResult.cpp; sourceTree = "<group>"; };
83118313 758978EB127090D60076D5A9 /* SpeechInputResult.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SpeechInputResult.h; sourceTree = "<group>"; };
 8314 7633A72413D8B33A008501B6 /* LocaleToScriptMapping.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LocaleToScriptMapping.h; sourceTree = "<group>"; };
 8315 7633A72513D8B33A008501B6 /* LocaleToScriptMappingDefault.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = LocaleToScriptMappingDefault.cpp; sourceTree = "<group>"; };
83128316 7637C540112E7B74003D6CDC /* WebSocketHandshakeRequest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WebSocketHandshakeRequest.cpp; sourceTree = "<group>"; };
83138317 7637C542112E7B7E003D6CDC /* WebSocketHandshakeRequest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebSocketHandshakeRequest.h; sourceTree = "<group>"; };
83148318 767F99BD11A1194A0080C51D /* WebSocketHandshakeResponse.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WebSocketHandshakeResponse.cpp; sourceTree = "<group>"; };

1823018234 A5ABB78613B904BC00F197E3 /* LineBreakIteratorPoolICU.h */,
1823118235 89B5EA9F11E8003D00F2367E /* LineEnding.cpp */,
1823218236 89B5EAA011E8003D00F2367E /* LineEnding.h */,
 18237 7633A72413D8B33A008501B6 /* LocaleToScriptMapping.h */,
 18238 7633A72513D8B33A008501B6 /* LocaleToScriptMappingDefault.cpp */,
1823318239 A5732B0C136A16C4005C8D7C /* LocalizedDate.h */,
1823418240 A5732B0E136A1715005C8D7C /* LocalizedDateNone.cpp */,
1823518241 F5142C68123F12B000F5BD4C /* LocalizedNumber.h */,

2221122217 7EE6846912D26E3800E79415 /* LoaderRunLoopCF.h in Headers */,
2221222218 06E81ED70AB5D5E900C87837 /* LocalCurrentGraphicsContext.h in Headers */,
2221322219 89878568122CA064003AABDA /* LocalFileSystem.h in Headers */,
 22220 7633A72613D8B33A008501B6 /* LocaleToScriptMapping.h in Headers */,
2221422221 BC5CFCA911F793320099ED09 /* LocalizationStrategy.h in Headers */,
2221522222 A5732B0D136A16C4005C8D7C /* LocalizedDate.h in Headers */,
2221622223 F5142C69123F12B000F5BD4C /* LocalizedNumber.h in Headers */,

2521025217 7EE6846812D26E3800E79415 /* LoaderRunLoopCF.cpp in Sources */,
2521125218 06E81EEC0AB5DA9700C87837 /* LocalCurrentGraphicsContext.mm in Sources */,
2521225219 89878567122CA064003AABDA /* LocalFileSystem.cpp in Sources */,
 25220 7633A72713D8B33A008501B6 /* LocaleToScriptMappingDefault.cpp in Sources */,
2521325221 A5732B0F136A1715005C8D7C /* LocalizedDateNone.cpp in Sources */,
2521425222 F5CC42DC12F801CA00D5F7E3 /* LocalizedNumberMac.mm in Sources */,
2521525223 C046E1AC1208A9FE00BA2CF7 /* LocalizedStrings.cpp in Sources */,

Source/WebCore/css/CSSFontSelector.cpp

@@static FontData* fontDataForGenericFamily(Document* document, const FontDescript
389389 return 0;
390390
391391 AtomicString genericFamily;
 392 UScriptCode script = fontDescription.script();
392393 if (familyName == "-webkit-serif")
393  genericFamily = settings->serifFontFamily();
 394 genericFamily = settings->serifFontFamily(script);
394395 else if (familyName == "-webkit-sans-serif")
395  genericFamily = settings->sansSerifFontFamily();
 396 genericFamily = settings->sansSerifFontFamily(script);
396397 else if (familyName == "-webkit-cursive")
397  genericFamily = settings->cursiveFontFamily();
 398 genericFamily = settings->cursiveFontFamily(script);
398399 else if (familyName == "-webkit-fantasy")
399  genericFamily = settings->fantasyFontFamily();
 400 genericFamily = settings->fantasyFontFamily(script);
400401 else if (familyName == "-webkit-monospace")
401  genericFamily = settings->fixedFontFamily();
 402 genericFamily = settings->fixedFontFamily(script);
402403 else if (familyName == "-webkit-standard")
403  genericFamily = settings->standardFontFamily();
 404 genericFamily = settings->standardFontFamily(script);
404405
405406 if (!genericFamily.isEmpty())
406407 return fontCache()->getCachedFontData(fontDescription, genericFamily);

@@FontData* CSSFontSelector::getFontData(const FontDescription& fontDescription, c
495496 if (m_fontFaces.isEmpty()) {
496497 if (familyName.startsWith("-webkit-"))
497498 return fontDataForGenericFamily(m_document, fontDescription, familyName);
 499 if (fontDescription.genericFamily() == FontDescription::StandardFamily && !fontDescription.isSpecifiedFont())
 500 return fontDataForGenericFamily(m_document, fontDescription, "-webkit-standard");
498501 return 0;
499502 }
500503

Source/WebCore/css/CSSPropertyNames.in

@@font-variant
2121font-weight
2222text-rendering
2323-webkit-font-smoothing
 24-webkit-locale
2425-webkit-text-orientation
2526-epub-text-orientation = -webkit-text-orientation
2627-webkit-text-size-adjust

@@z-index
250251-webkit-line-box-contain
251252-webkit-line-break
252253-webkit-line-clamp
253 -webkit-locale
254254-webkit-logical-width
255255-webkit-logical-height
256256-webkit-margin-after-collapse

Source/WebCore/css/CSSStyleSelector.cpp

6969#include "HTMLTextAreaElement.h"
7070#include "KeyframeList.h"
7171#include "LinkHash.h"
 72#include "LocaleToScriptMapping.h"
7273#include "Matrix3DTransformOperation.h"
7374#include "MatrixTransformOperation.h"
7475#include "MediaList.h"

@@PassRefPtr<RenderStyle> CSSStyleSelector::styleForDocument(Document* document)
12761277 documentStyle->setForceBackgroundsToWhite(true);
12771278 const AtomicString& stdfont = settings->standardFontFamily();
12781279 if (!stdfont.isEmpty()) {
 1280 fontDescription.setGenericFamily(FontDescription::StandardFamily);
12791281 fontDescription.firstFamily().setFamily(stdfont);
12801282 fontDescription.firstFamily().appendFamily(0);
12811283 }

@@void CSSStyleSelector::applyDeclarations(bool isImportant, int startIndex, int e
34723474
34733475 if (applyFirst) {
34743476 COMPILE_ASSERT(firstCSSProperty == CSSPropertyColor, CSS_color_is_first_property);
3475  COMPILE_ASSERT(CSSPropertyZoom == CSSPropertyColor + 14, CSS_zoom_is_end_of_first_prop_range);
 3477 COMPILE_ASSERT(CSSPropertyZoom == CSSPropertyColor + 15, CSS_zoom_is_end_of_first_prop_range);
34763478 COMPILE_ASSERT(CSSPropertyLineHeight == CSSPropertyZoom + 1, CSS_line_height_is_after_zoom);
34773479
34783480 // give special priority to font-xxx, color properties, etc

@@void CSSStyleSelector::applyProperty(int id, CSSValue *value)
48594861 m_style->setLocale(nullAtom);
48604862 else
48614863 m_style->setLocale(primitiveValue->getStringValue());
 4864 FontDescription fontDescription = m_style->fontDescription();
 4865 fontDescription.setScript(localeToScriptCode(m_style->locale()));
 4866 if (m_style->setFontDescription(fontDescription))
 4867 m_fontDirty = true;
48624868 return;
48634869 }
48644870 case CSSPropertyWebkitBorderFit: {

Source/WebCore/page/Settings.cpp

@@static inline const AtomicString& getGenericFontFamilyForScript(const ScriptFont
6666 ScriptFontFamilyMap::const_iterator it = fontMap.find(static_cast<int>(script));
6767 if (it != fontMap.end())
6868 return it->second;
69  return emptyAtom;
 69 if (script == USCRIPT_COMMON)
 70 return emptyAtom;
 71 return getGenericFontFamilyForScript(fontMap, USCRIPT_COMMON);
7072}
7173
7274#if USE(SAFARI_THEME)

Source/WebCore/platform/graphics/FontDescription.h

@@public:
8383 , m_fontSmoothing(AutoSmoothing)
8484 , m_textRendering(AutoTextRendering)
8585 , m_isSpecifiedFont(false)
 86 , m_script(USCRIPT_COMMON)
8687 {
8788 }
8889

@@public:
108109 unsigned keywordSize() const { return m_keywordSize; }
109110 FontSmoothingMode fontSmoothing() const { return static_cast<FontSmoothingMode>(m_fontSmoothing); }
110111 TextRenderingMode textRenderingMode() const { return static_cast<TextRenderingMode>(m_textRendering); }
 112 UScriptCode script() const { return m_script; }
111113
112114 FontTraitsMask traitsMask() const;
113115 bool isSpecifiedFont() const { return m_isSpecifiedFont; }

@@public:
138140 void setOrientation(FontOrientation orientation) { m_orientation = orientation; }
139141 void setTextOrientation(TextOrientation textOrientation) { m_textOrientation = textOrientation; }
140142 void setWidthVariant(FontWidthVariant widthVariant) { m_widthVariant = widthVariant; }
 143 void setScript(UScriptCode s) { m_script = s; }
141144
142145private:
143146 FontFamily m_familyList; // The list of font families to be used.

@@private:
168171 unsigned m_fontSmoothing : 2; // FontSmoothingMode
169172 unsigned m_textRendering : 2; // TextRenderingMode
170173 bool m_isSpecifiedFont : 1; // True if a web page specifies a non-generic font family as the first font family.
 174 UScriptCode m_script; // Used to help choose an appropriate font for generic font families.
171175};
172176
173177inline bool FontDescription::operator==(const FontDescription& other) const

@@inline bool FontDescription::operator==(const FontDescription& other) const
188192 && m_isSpecifiedFont == other.m_isSpecifiedFont
189193 && m_orientation == other.m_orientation
190194 && m_textOrientation == other.m_textOrientation
191  && m_widthVariant == other.m_widthVariant;
 195 && m_widthVariant == other.m_widthVariant
 196 && m_script == other.m_script;
192197}
193198
194199}

Source/WebCore/platform/text/LocaleToScriptMapping.h

 1/*
 2 * Copyright (C) 2011 Google Inc. All rights reserved.
 3 *
 4 * Redistribution and use in source and binary forms, with or without
 5 * modification, are permitted provided that the following conditions are
 6 * met:
 7 *
 8 * * Redistributions of source code must retain the above copyright
 9 * notice, this list of conditions and the following disclaimer.
 10 * * Redistributions in binary form must reproduce the above
 11 * copyright notice, this list of conditions and the following disclaimer
 12 * in the documentation and/or other materials provided with the
 13 * distribution.
 14 * * Neither the name of Google Inc. nor the names of its
 15 * contributors may be used to endorse or promote products derived from
 16 * this software without specific prior written permission.
 17 *
 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 29 */
 30
 31#ifndef LocaleToScriptMapping_h
 32#define LocaleToScriptMapping_h
 33
 34#include <wtf/unicode/Unicode.h>
 35
 36namespace WTF {
 37
 38class String;
 39
 40}
 41
 42namespace WebCore {
 43
 44UScriptCode localeToScriptCode(const WTF::String& locale);
 45
 46}
 47
 48#endif

Source/WebCore/platform/text/LocaleToScriptMappingDefault.cpp

 1/*
 2 * Copyright (C) 2011 Google Inc. All rights reserved.
 3 *
 4 * Redistribution and use in source and binary forms, with or without
 5 * modification, are permitted provided that the following conditions are
 6 * met:
 7 *
 8 * * Redistributions of source code must retain the above copyright
 9 * notice, this list of conditions and the following disclaimer.
 10 * * Redistributions in binary form must reproduce the above
 11 * copyright notice, this list of conditions and the following disclaimer
 12 * in the documentation and/or other materials provided with the
 13 * distribution.
 14 * * Neither the name of Google Inc. nor the names of its
 15 * contributors may be used to endorse or promote products derived from
 16 * this software without specific prior written permission.
 17 *
 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 29 */
 30
 31#include "config.h"
 32#include "LocaleToScriptMapping.h"
 33
 34#include <wtf/HashMap.h>
 35#include <wtf/HashSet.h>
 36#include <wtf/text/StringHash.h>
 37
 38namespace WebCore {
 39
 40UScriptCode localeToScriptCode(const String& locale)
 41{
 42 struct LocaleScript {
 43 const char* locale;
 44 UScriptCode script;
 45 };
 46
 47 // FIXME: Add all languages from ICU.
 48 static const LocaleScript localeScriptList[] = {
 49 { "ja", USCRIPT_KATAKANA_OR_HIRAGANA },
 50 { "ko", USCRIPT_HANGUL },
 51 { "ru", USCRIPT_CYRILLIC },
 52 { "zh", USCRIPT_HAN },
 53 { "zh_cn", USCRIPT_SIMPLIFIED_HAN },
 54 { "zh_hans", USCRIPT_SIMPLIFIED_HAN },
 55 { "zh_hant", USCRIPT_TRADITIONAL_HAN },
 56 { "zh_hk", USCRIPT_TRADITIONAL_HAN },
 57 { "zh_tw", USCRIPT_TRADITIONAL_HAN }
 58 };
 59
 60 typedef HashMap<String, UScriptCode> LocaleScriptMap;
 61 DEFINE_STATIC_LOCAL(LocaleScriptMap, localeScriptMap, ());
 62 if (localeScriptMap.isEmpty()) {
 63 for (size_t i = 0; i < sizeof(localeScriptList) / sizeof(localeScriptList[0]); ++i)
 64 localeScriptMap.set(localeScriptList[i].locale, localeScriptList[i].script);
 65 }
 66
 67 String canonicalLocale = locale.lower().replace('-', '_');
 68 while (!canonicalLocale.isEmpty()) {
 69 HashMap<String, UScriptCode>::iterator it = localeScriptMap.find(canonicalLocale);
 70 if (it != localeScriptMap.end())
 71 return it->second;
 72 size_t pos = canonicalLocale.reverseFind('_');
 73 if (pos == notFound)
 74 break;
 75 canonicalLocale = canonicalLocale.substring(0, pos);
 76 }
 77 return USCRIPT_COMMON;
 78}
 79
 80} // namespace WebCore

Source/WebCore/platform/text/LocaleToScriptMappingICU.cpp

 1/*
 2 * Copyright (C) 2011 Google Inc. All rights reserved.
 3 *
 4 * Redistribution and use in source and binary forms, with or without
 5 * modification, are permitted provided that the following conditions are
 6 * met:
 7 *
 8 * * Redistributions of source code must retain the above copyright
 9 * notice, this list of conditions and the following disclaimer.
 10 * * Redistributions in binary form must reproduce the above
 11 * copyright notice, this list of conditions and the following disclaimer
 12 * in the documentation and/or other materials provided with the
 13 * distribution.
 14 * * Neither the name of Google Inc. nor the names of its
 15 * contributors may be used to endorse or promote products derived from
 16 * this software without specific prior written permission.
 17 *
 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 29 */
 30
 31#include "config.h"
 32#include "LocaleToScriptMapping.h"
 33
 34#include <unicode/uloc.h>
 35#include <wtf/text/CString.h>
 36#include <wtf/text/WTFString.h>
 37
 38namespace WebCore {
 39
 40// There are certain families of script codes that we want to treat as a single
 41// script for the purpose of assigning a per-script font in Settings. For
 42// example, USCRIPT_HIRAGANA, USCRIPT_KATAKANA, USCRIPT_JAPANESE, and
 43// USCRIPT_KATAKANA_OR_HIRAGANA are all "Japanese" and there is no value in
 44// distinguishing between them. So we just pick one script code to be the
 45// canonical one with this function, and in Settings assign a font for that
 46// script.
 47static UScriptCode canonicalizeScriptCode(UScriptCode scriptCode)
 48{
 49 switch (scriptCode) {
 50 case USCRIPT_HIRAGANA:
 51 case USCRIPT_KATAKANA:
 52 case USCRIPT_JAPANESE:
 53 return USCRIPT_KATAKANA_OR_HIRAGANA;
 54 case USCRIPT_KOREAN:
 55 return USCRIPT_HANGUL;
 56 default:
 57 return scriptCode;
 58 }
 59}
 60
 61UScriptCode localeToScriptCode(const String& locale)
 62{
 63 if (locale.isEmpty())
 64 return USCRIPT_COMMON;
 65
 66 char maximizedLocale[ULOC_FULLNAME_CAPACITY];
 67 UErrorCode status = U_ZERO_ERROR;
 68 uloc_addLikelySubtags(locale.utf8().data(), maximizedLocale, sizeof(maximizedLocale), &status);
 69 if (U_FAILURE(status))
 70 return USCRIPT_COMMON;
 71
 72 char script[ULOC_SCRIPT_CAPACITY];
 73 uloc_getScript(maximizedLocale, script, sizeof(script), &status);
 74 if (U_FAILURE(status))
 75 return USCRIPT_COMMON;
 76
 77 UScriptCode scriptCode;
 78 uscript_getCode(script, &scriptCode, 1, &status);
 79 // Ignore error that multiple scripts could be returned, since we only want
 80 // one script.
 81 if (U_FAILURE(status) && status != U_BUFFER_OVERFLOW_ERROR)
 82 return USCRIPT_COMMON;
 83
 84 return canonicalizeScriptCode(scriptCode);
 85}
 86
 87} // namespace WebCore