OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "platform/LayoutLocale.h" | 5 #include "platform/LayoutLocale.h" |
6 | 6 |
7 #include "platform/Language.h" | 7 #include "platform/Language.h" |
8 #include "platform/fonts/AcceptLanguagesResolver.h" | 8 #include "platform/fonts/AcceptLanguagesResolver.h" |
| 9 #include "platform/text/ICUError.h" |
9 #include "platform/text/LocaleToScriptMapping.h" | 10 #include "platform/text/LocaleToScriptMapping.h" |
10 #include "wtf/HashMap.h" | 11 #include "wtf/HashMap.h" |
11 #include "wtf/text/AtomicStringHash.h" | 12 #include "wtf/text/AtomicStringHash.h" |
12 #include "wtf/text/StringHash.h" | 13 #include "wtf/text/StringHash.h" |
13 | 14 |
14 #include <hb.h> | 15 #include <hb.h> |
15 #include <unicode/locid.h> | 16 #include <unicode/locid.h> |
16 | 17 |
17 namespace blink { | 18 namespace blink { |
18 | 19 |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 } | 177 } |
177 | 178 |
178 void LayoutLocale::setHyphenationForTesting( | 179 void LayoutLocale::setHyphenationForTesting( |
179 const AtomicString& localeString, | 180 const AtomicString& localeString, |
180 PassRefPtr<Hyphenation> hyphenation) { | 181 PassRefPtr<Hyphenation> hyphenation) { |
181 const LayoutLocale& locale = valueOrDefault(get(localeString)); | 182 const LayoutLocale& locale = valueOrDefault(get(localeString)); |
182 locale.m_hyphenationComputed = true; | 183 locale.m_hyphenationComputed = true; |
183 locale.m_hyphenation = hyphenation; | 184 locale.m_hyphenation = hyphenation; |
184 } | 185 } |
185 | 186 |
| 187 AtomicString LayoutLocale::localeWithBreakKeyword( |
| 188 LineBreakIteratorMode mode) const { |
| 189 if (m_string.isEmpty()) |
| 190 return m_string; |
| 191 |
| 192 CString utf8Locale = m_string.utf8(); |
| 193 Vector<char> buffer(utf8Locale.length() + 11, 0); |
| 194 memcpy(buffer.data(), utf8Locale.data(), utf8Locale.length()); |
| 195 |
| 196 const char* keywordValue = nullptr; |
| 197 switch (mode) { |
| 198 default: |
| 199 NOTREACHED(); |
| 200 // Fall through. |
| 201 case LineBreakIteratorMode::Default: |
| 202 // nullptr will cause any existing values to be removed. |
| 203 break; |
| 204 case LineBreakIteratorMode::Normal: |
| 205 keywordValue = "normal"; |
| 206 break; |
| 207 case LineBreakIteratorMode::Strict: |
| 208 keywordValue = "strict"; |
| 209 break; |
| 210 case LineBreakIteratorMode::Loose: |
| 211 keywordValue = "loose"; |
| 212 break; |
| 213 } |
| 214 |
| 215 ICUError status; |
| 216 int32_t lengthNeeded = uloc_setKeywordValue("lb", keywordValue, buffer.data(), |
| 217 buffer.size(), &status); |
| 218 if (U_SUCCESS(status)) |
| 219 return AtomicString::fromUTF8(buffer.data(), lengthNeeded); |
| 220 |
| 221 if (status == U_BUFFER_OVERFLOW_ERROR) { |
| 222 buffer.grow(lengthNeeded + 1); |
| 223 memset(buffer.data() + utf8Locale.length(), 0, |
| 224 buffer.size() - utf8Locale.length()); |
| 225 status = U_ZERO_ERROR; |
| 226 int32_t lengthNeeded2 = uloc_setKeywordValue( |
| 227 "lb", keywordValue, buffer.data(), buffer.size(), &status); |
| 228 DCHECK_EQ(lengthNeeded, lengthNeeded2); |
| 229 if (U_SUCCESS(status) && lengthNeeded == lengthNeeded2) |
| 230 return AtomicString::fromUTF8(buffer.data(), lengthNeeded); |
| 231 } |
| 232 |
| 233 NOTREACHED(); |
| 234 return m_string; |
| 235 } |
| 236 |
186 } // namespace blink | 237 } // namespace blink |
OLD | NEW |