OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/i18n/rtl.h" | 5 #include "base/i18n/rtl.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 } | 189 } |
190 | 190 |
191 // Handle the case of a string not containing any strong directionality | 191 // Handle the case of a string not containing any strong directionality |
192 // characters defaulting to LEFT_TO_RIGHT. | 192 // characters defaulting to LEFT_TO_RIGHT. |
193 if (result == UNKNOWN_DIRECTION) | 193 if (result == UNKNOWN_DIRECTION) |
194 return LEFT_TO_RIGHT; | 194 return LEFT_TO_RIGHT; |
195 | 195 |
196 return result; | 196 return result; |
197 } | 197 } |
198 | 198 |
199 #if defined(OS_WIN) | |
200 bool AdjustStringForLocaleDirection(string16* text) { | |
201 if (!IsRTL() || text->empty()) | |
202 return false; | |
203 | |
204 // Marking the string as LTR if the locale is RTL and the string does not | |
205 // contain strong RTL characters. Otherwise, mark the string as RTL. | |
206 bool has_rtl_chars = StringContainsStrongRTLChars(*text); | |
207 if (!has_rtl_chars) | |
208 WrapStringWithLTRFormatting(text); | |
209 else | |
210 WrapStringWithRTLFormatting(text); | |
211 | |
212 return true; | |
213 } | |
214 | |
215 bool UnadjustStringForLocaleDirection(string16* text) { | |
216 if (!IsRTL() || text->empty()) | |
217 return false; | |
218 | |
219 *text = StripWrappingBidiControlCharacters(*text); | |
220 return true; | |
221 } | |
222 #else | |
223 bool AdjustStringForLocaleDirection(string16* text) { | 199 bool AdjustStringForLocaleDirection(string16* text) { |
224 // On OS X & GTK the directionality of a label is determined by the first | 200 // On OS X & GTK the directionality of a label is determined by the first |
225 // strongly directional character. | 201 // strongly directional character. |
226 // However, we want to make sure that in an LTR-language-UI all strings are | 202 // However, we want to make sure that in an LTR-language-UI all strings are |
227 // left aligned and vice versa. | 203 // left aligned and vice versa. |
228 // A problem can arise if we display a string which starts with user input. | 204 // A problem can arise if we display a string which starts with user input. |
229 // User input may be of the opposite directionality to the UI. So the whole | 205 // User input may be of the opposite directionality to the UI. So the whole |
230 // string will be displayed in the opposite directionality, e.g. if we want to | 206 // string will be displayed in the opposite directionality, e.g. if we want to |
231 // display in an LTR UI [such as US English]: | 207 // display in an LTR UI [such as US English]: |
232 // | 208 // |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 end == kRightToLeftMark) { | 267 end == kRightToLeftMark) { |
292 --end_index; | 268 --end_index; |
293 } | 269 } |
294 | 270 |
295 string16 unmarked_text = | 271 string16 unmarked_text = |
296 text->substr(begin_index, end_index - begin_index + 1); | 272 text->substr(begin_index, end_index - begin_index + 1); |
297 *text = StripWrappingBidiControlCharacters(unmarked_text); | 273 *text = StripWrappingBidiControlCharacters(unmarked_text); |
298 return true; | 274 return true; |
299 } | 275 } |
300 | 276 |
301 #endif // !OS_WIN | |
302 | |
303 bool StringContainsStrongRTLChars(const string16& text) { | 277 bool StringContainsStrongRTLChars(const string16& text) { |
304 const UChar* string = text.c_str(); | 278 const UChar* string = text.c_str(); |
305 size_t length = text.length(); | 279 size_t length = text.length(); |
306 size_t position = 0; | 280 size_t position = 0; |
307 while (position < length) { | 281 while (position < length) { |
308 UChar32 character; | 282 UChar32 character; |
309 size_t next_position = position; | 283 size_t next_position = position; |
310 U16_NEXT(string, next_position, length, character); | 284 U16_NEXT(string, next_position, length, character); |
311 | 285 |
312 // Now that we have the character, we use ICU in order to query for the | 286 // Now that we have the character, we use ICU in order to query for the |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
346 } | 320 } |
347 | 321 |
348 void WrapPathWithLTRFormatting(const FilePath& path, | 322 void WrapPathWithLTRFormatting(const FilePath& path, |
349 string16* rtl_safe_path) { | 323 string16* rtl_safe_path) { |
350 // Wrap the overall path with LRE-PDF pair which essentialy marks the | 324 // Wrap the overall path with LRE-PDF pair which essentialy marks the |
351 // string as a Left-To-Right string. | 325 // string as a Left-To-Right string. |
352 // Inserting an LRE (Left-To-Right Embedding) mark as the first character. | 326 // Inserting an LRE (Left-To-Right Embedding) mark as the first character. |
353 rtl_safe_path->push_back(kLeftToRightEmbeddingMark); | 327 rtl_safe_path->push_back(kLeftToRightEmbeddingMark); |
354 #if defined(OS_MACOSX) | 328 #if defined(OS_MACOSX) |
355 rtl_safe_path->append(UTF8ToUTF16(path.value())); | 329 rtl_safe_path->append(UTF8ToUTF16(path.value())); |
356 #elif defined(OS_WIN) | |
357 rtl_safe_path->append(path.value()); | |
358 #else // defined(OS_POSIX) && !defined(OS_MACOSX) | 330 #else // defined(OS_POSIX) && !defined(OS_MACOSX) |
359 std::wstring wide_path = base::SysNativeMBToWide(path.value()); | 331 std::wstring wide_path = base::SysNativeMBToWide(path.value()); |
360 rtl_safe_path->append(WideToUTF16(wide_path)); | 332 rtl_safe_path->append(WideToUTF16(wide_path)); |
361 #endif | 333 #endif |
362 // Inserting a PDF (Pop Directional Formatting) mark as the last character. | 334 // Inserting a PDF (Pop Directional Formatting) mark as the last character. |
363 rtl_safe_path->push_back(kPopDirectionalFormatting); | 335 rtl_safe_path->push_back(kPopDirectionalFormatting); |
364 } | 336 } |
365 | 337 |
366 string16 GetDisplayStringInLTRDirectionality(const string16& text) { | 338 string16 GetDisplayStringInLTRDirectionality(const string16& text) { |
367 // Always wrap the string in RTL UI (it may be appended to RTL string). | 339 // Always wrap the string in RTL UI (it may be appended to RTL string). |
(...skipping 17 matching lines...) Expand all Loading... |
385 begin == kRightToLeftOverride) | 357 begin == kRightToLeftOverride) |
386 ++begin_index; | 358 ++begin_index; |
387 size_t end_index = text.length() - 1; | 359 size_t end_index = text.length() - 1; |
388 if (text[end_index] == kPopDirectionalFormatting) | 360 if (text[end_index] == kPopDirectionalFormatting) |
389 --end_index; | 361 --end_index; |
390 return text.substr(begin_index, end_index - begin_index + 1); | 362 return text.substr(begin_index, end_index - begin_index + 1); |
391 } | 363 } |
392 | 364 |
393 } // namespace i18n | 365 } // namespace i18n |
394 } // namespace base | 366 } // namespace base |
OLD | NEW |