LineBreaker.h (3730B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 #ifndef mozilla_intl_LineBreaker_h__ 6 #define mozilla_intl_LineBreaker_h__ 7 8 #include <cstdint> 9 10 #define NS_LINEBREAKER_NEED_MORE_TEXT -1 11 12 namespace mozilla { 13 namespace intl { 14 enum class LineBreakRule : uint8_t; 15 enum class WordBreakRule : uint8_t; 16 17 class LineBreaker final { 18 public: 19 // LineBreaker is a utility class with only static methods. No need to 20 // instantiate it. 21 LineBreaker() = delete; 22 ~LineBreaker() = delete; 23 24 // Find the next line break opportunity starting from aPos + 1. It can return 25 // aLen if there's no break opportunity between [aPos + 1, aLen - 1]. 26 // 27 // If aPos is already at the end of aText or beyond, i.e. aPos >= aLen, return 28 // NS_LINEBREAKER_NEED_MORE_TEXT. 29 // 30 // DEPRECATED: Use LineBreakIteratorUtf16 instead. 31 static int32_t Next(const char16_t* aText, uint32_t aLen, uint32_t aPos); 32 33 // Call this on a word with whitespace at either end. We will apply JISx4051 34 // rules to find breaks inside the word. aBreakBefore is set to the break- 35 // before status of each character; aBreakBefore[0] will always be false 36 // because we never return a break before the first character. 37 // aLength is the length of the aText array and also the length of the 38 // aBreakBefore output array. 39 static void ComputeBreakPositions(const char16_t* aText, uint32_t aLength, 40 WordBreakRule aWordBreak, 41 LineBreakRule aLevel, 42 bool aIsChineseOrJapanese, 43 uint8_t* aBreakBefore); 44 static void ComputeBreakPositions(const uint8_t* aText, uint32_t aLength, 45 WordBreakRule aWordBreak, 46 LineBreakRule aLevel, 47 bool aIsChineseOrJapanese, 48 uint8_t* aBreakBefore); 49 50 static void Shutdown(); 51 }; 52 53 static inline bool NS_IsSpace(char16_t u) { 54 return u == 0x0020 || // SPACE 55 u == 0x0009 || // CHARACTER TABULATION 56 u == 0x000D || // CARRIAGE RETURN 57 (0x2000 <= u && u <= 0x2006) || // EN QUAD, EM QUAD, EN SPACE, 58 // EM SPACE, THREE-PER-EM SPACE, 59 // FOUR-PER-SPACE, SIX-PER-EM SPACE, 60 (0x2008 <= u && u <= 0x200B) || // PUNCTUATION SPACE, THIN SPACE, 61 // HAIR SPACE, ZERO WIDTH SPACE 62 u == 0x1361 || // ETHIOPIC WORDSPACE 63 u == 0x1680 || // OGHAM SPACE MARK 64 u == 0x205F; // MEDIUM MATHEMATICAL SPACE 65 } 66 67 static inline bool NS_NeedsPlatformNativeHandling(char16_t aChar) { 68 return 69 #if ANDROID || XP_WIN // Bug 1647377/1736393: no "platform native" support for 70 // Tibetan; better to just use our class-based breaker. 71 (0x0e01 <= aChar && aChar <= 0x0eff) || // Thai, Lao 72 #else 73 // Routing Tibetan to the platform-native breaker currently results in 74 // WPT failures in a few css3-text-line-break-opclns-* testcases that mix 75 // a Tibetan character with other-script context. 76 (0x0e01 <= aChar && aChar <= 0x0fff) || // Thai, Lao, Tibetan 77 #endif 78 (0x1780 <= aChar && aChar <= 0x17ff); // Khmer 79 } 80 81 } // namespace intl 82 } // namespace mozilla 83 84 #endif /* mozilla_intl_LineBreaker_h__ */