nsJISx4051LineBreaker.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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. #include "nsJISx4051LineBreaker.h"
  6. #include "jisx4051class.h"
  7. #include "nsComplexBreaker.h"
  8. #include "nsTArray.h"
  9. #include "nsUnicodeProperties.h"
  10. /*
  11. Simplification of Pair Table in JIS X 4051
  12. 1. The Origion Table - in 4.1.3
  13. In JIS x 4051. The pair table is defined as below
  14. Class of
  15. Leading Class of Trailing Char Class
  16. Char
  17. 1 2 3 4 5 6 7 8 9 10 11 12 13 13 14 14 15 16 17 18 19 20
  18. * # * #
  19. 1 X X X X X X X X X X X X X X X X X X X X X E
  20. 2 X X X X X X
  21. 3 X X X X X X
  22. 4 X X X X X X
  23. 5 X X X X X X
  24. 6 X X X X X X
  25. 7 X X X X X X X
  26. 8 X X X X X X E
  27. 9 X X X X X X
  28. 10 X X X X X X
  29. 11 X X X X X X
  30. 12 X X X X X X
  31. 13 X X X X X X X
  32. 14 X X X X X X X
  33. 15 X X X X X X X X X
  34. 16 X X X X X X X X
  35. 17 X X X X X E
  36. 18 X X X X X X X X X
  37. 19 X E E E E E X X X X X X X X X X X X E X E E
  38. 20 X X X X X E
  39. * Same Char
  40. # Other Char
  41. X Cannot Break
  42. The classes mean:
  43. 1: Open parenthesis
  44. 2: Close parenthesis
  45. 3: Prohibit a line break before
  46. 4: Punctuation for sentence end (except Full stop, e.g., "!" and "?")
  47. 5: Middle dot (e.g., U+30FB KATAKANA MIDDLE DOT)
  48. 6: Full stop
  49. 7: Non-breakable between same characters
  50. 8: Prefix (e.g., "$", "NO.")
  51. 9: Postfix (e.g., "%")
  52. 10: Ideographic space
  53. 11: Hiragana
  54. 12: Japanese characters (except class 11)
  55. 13: Subscript
  56. 14: Ruby
  57. 15: Numeric
  58. 16: Alphabet
  59. 17: Space for Western language
  60. 18: Western characters (except class 17)
  61. 19: Split line note (Warichu) begin quote
  62. 20: Split line note (Warichu) end quote
  63. 2. Simplified by remove the class which we do not care
  64. However, since we do not care about class 13(Subscript), 14(Ruby),
  65. 16 (Aphabet), 19(split line note begin quote), and 20(split line note end
  66. quote) we can simplify this par table into the following
  67. Class of
  68. Leading Class of Trailing Char Class
  69. Char
  70. 1 2 3 4 5 6 7 8 9 10 11 12 15 17 18
  71. 1 X X X X X X X X X X X X X X X
  72. 2 X X X X X
  73. 3 X X X X X
  74. 4 X X X X X
  75. 5 X X X X X
  76. 6 X X X X X
  77. 7 X X X X X X
  78. 8 X X X X X X
  79. 9 X X X X X
  80. 10 X X X X X
  81. 11 X X X X X
  82. 12 X X X X X
  83. 15 X X X X X X X X
  84. 17 X X X X X
  85. 18 X X X X X X X
  86. 3. Simplified by merged classes
  87. After the 2 simplification, the pair table have some duplication
  88. a. class 2, 3, 4, 5, 6, are the same- we can merged them
  89. b. class 10, 11, 12, 17 are the same- we can merged them
  90. Class of
  91. Leading Class of Trailing Char Class
  92. Char
  93. 1 [a] 7 8 9 [b]15 18
  94. 1 X X X X X X X X
  95. [a] X
  96. 7 X X
  97. 8 X X
  98. 9 X
  99. [b] X
  100. 15 X X X X
  101. 18 X X X
  102. 4. We add COMPLEX characters and make it breakable w/ all ther class
  103. except after class 1 and before class [a]
  104. Class of
  105. Leading Class of Trailing Char Class
  106. Char
  107. 1 [a] 7 8 9 [b]15 18 COMPLEX
  108. 1 X X X X X X X X X
  109. [a] X
  110. 7 X X
  111. 8 X X
  112. 9 X
  113. [b] X
  114. 15 X X X X
  115. 18 X X X
  116. COMPLEX X T
  117. T : need special handling
  118. 5. However, we need two special class for some punctuations/parentheses,
  119. theirs breaking rules like character class (18), see bug 389056.
  120. And also we need character like punctuation that is same behavior with 18,
  121. but the characters are not letters of all languages. (e.g., '_')
  122. [c]. Based on open parenthesis class (1), but it is not breakable after
  123. character class (18) or numeric class (15).
  124. [d]. Based on close parenthesis (or punctuation) class (2), but it is not
  125. breakable before character class (18) or numeric class (15).
  126. Class of
  127. Leading Class of Trailing Char Class
  128. Char
  129. 1 [a] 7 8 9 [b]15 18 COMPLEX [c] [d]
  130. 1 X X X X X X X X X X X
  131. [a] X X X
  132. 7 X X
  133. 8 X X
  134. 9 X
  135. [b] X X
  136. 15 X X X X X X
  137. 18 X X X X X
  138. COMPLEX X T
  139. [c] X X X X X X X X X X X
  140. [d] X X X X
  141. 6. And Unicode has "NON-BREAK" characters. The lines should be broken around
  142. them. But in JIS X 4051, such class is not, therefore, we create [e].
  143. Class of
  144. Leading Class of Trailing Char Class
  145. Char
  146. 1 [a] 7 8 9 [b]15 18 COMPLEX [c] [d] [e]
  147. 1 X X X X X X X X X X X X
  148. [a] X X X
  149. 7 X X X
  150. 8 X X X
  151. 9 X X
  152. [b] X X X
  153. 15 X X X X X X X
  154. 18 X X X X X X
  155. COMPLEX X T X
  156. [c] X X X X X X X X X X X X
  157. [d] X X X X X
  158. [e] X X X X X X X X X X X X
  159. 7. Now we use one bit to encode weather it is breakable, and use 2 bytes
  160. for one row, then the bit table will look like:
  161. 18 <- 1
  162. 1 0000 1111 1111 1111 = 0x0FFF
  163. [a] 0000 1100 0000 0010 = 0x0C02
  164. 7 0000 1000 0000 0110 = 0x0806
  165. 8 0000 1000 0100 0010 = 0x0842
  166. 9 0000 1000 0000 0010 = 0x0802
  167. [b] 0000 1100 0000 0010 = 0x0C02
  168. 15 0000 1110 1101 0010 = 0x0ED2
  169. 18 0000 1110 1100 0010 = 0x0EC2
  170. COMPLEX 0000 1001 0000 0010 = 0x0902
  171. [c] 0000 1111 1111 1111 = 0x0FFF
  172. [d] 0000 1100 1100 0010 = 0x0CC2
  173. [e] 0000 1111 1111 1111 = 0x0FFF
  174. */
  175. #define MAX_CLASSES 12
  176. static const uint16_t gPair[MAX_CLASSES] = {
  177. 0x0FFF,
  178. 0x0C02,
  179. 0x0806,
  180. 0x0842,
  181. 0x0802,
  182. 0x0C02,
  183. 0x0ED2,
  184. 0x0EC2,
  185. 0x0902,
  186. 0x0FFF,
  187. 0x0CC2,
  188. 0x0FFF
  189. };
  190. /*
  191. 8. And if the character is not enough far from word start, word end and
  192. another break point, we should not break in non-CJK languages.
  193. I.e., Don't break around 15, 18, [c] and [d], but don't change
  194. that if they are related to [b].
  195. Class of
  196. Leading Class of Trailing Char Class
  197. Char
  198. 1 [a] 7 8 9 [b]15 18 COMPLEX [c] [d] [e]
  199. 1 X X X X X X X X X X X X
  200. [a] X X X X X X
  201. 7 X X X X X X X
  202. 8 X X X X X X
  203. 9 X X X X X X
  204. [b] X X X
  205. 15 X X X X X X X X X X X
  206. 18 X X X X X X X X X X X
  207. COMPLEX X X X T X X X
  208. [c] X X X X X X X X X X X X
  209. [d] X X X X X X X X X X X
  210. [e] X X X X X X X X X X X X
  211. 18 <- 1
  212. 1 0000 1111 1111 1111 = 0x0FFF
  213. [a] 0000 1110 1100 0010 = 0x0EC2
  214. 7 0000 1110 1100 0110 = 0x0EC6
  215. 8 0000 1110 1100 0010 = 0x0EC2
  216. 9 0000 1110 1100 0010 = 0x0EC2
  217. [b] 0000 1100 0000 0010 = 0x0C02
  218. 15 0000 1111 1101 1111 = 0x0FDF
  219. 18 0000 1111 1101 1111 = 0x0FDF
  220. COMPLEX 0000 1111 1100 0010 = 0x0FC2
  221. [c] 0000 1111 1111 1111 = 0x0FFF
  222. [d] 0000 1111 1101 1111 = 0x0FDF
  223. [e] 0000 1111 1111 1111 = 0x0FFF
  224. */
  225. static const uint16_t gPairConservative[MAX_CLASSES] = {
  226. 0x0FFF,
  227. 0x0EC2,
  228. 0x0EC6,
  229. 0x0EC2,
  230. 0x0EC2,
  231. 0x0C02,
  232. 0x0FDF,
  233. 0x0FDF,
  234. 0x0FC2,
  235. 0x0FFF,
  236. 0x0FDF,
  237. 0x0FFF
  238. };
  239. /*
  240. 9. Now we map the class to number
  241. 0: 1
  242. 1: [a]- 2, 3, 4, 5, 6
  243. 2: 7
  244. 3: 8
  245. 4: 9
  246. 5: [b]- 10, 11, 12, 17
  247. 6: 15
  248. 7: 18
  249. 8: COMPLEX
  250. 9: [c]
  251. A: [d]
  252. B: [e]
  253. and they mean:
  254. 0: Open parenthesis
  255. 1: Punctuation that prohibits break before
  256. 2: Non-breakable between same classes
  257. 3: Prefix
  258. 4: Postfix
  259. 5: Breakable character (Spaces and Most Japanese characters)
  260. 6: Numeric
  261. 7: Characters
  262. 8: Need special handling characters (E.g., Thai)
  263. 9: Open parentheses like Character (See bug 389056)
  264. A: Close parenthese (or punctuations) like Character (See bug 389056)
  265. B: Non breakable (See bug 390920)
  266. */
  267. #define CLASS_NONE INT8_MAX
  268. #define CLASS_OPEN 0x00
  269. #define CLASS_CLOSE 0x01
  270. #define CLASS_NON_BREAKABLE_BETWEEN_SAME_CLASS 0x02
  271. #define CLASS_PREFIX 0x03
  272. #define CLASS_POSTFFIX 0x04
  273. #define CLASS_BREAKABLE 0x05
  274. #define CLASS_NUMERIC 0x06
  275. #define CLASS_CHARACTER 0x07
  276. #define CLASS_COMPLEX 0x08
  277. #define CLASS_OPEN_LIKE_CHARACTER 0x09
  278. #define CLASS_CLOSE_LIKE_CHARACTER 0x0A
  279. #define CLASS_NON_BREAKABLE 0x0B
  280. #define U_NULL char16_t(0x0000)
  281. #define U_SLASH char16_t('/')
  282. #define U_SPACE char16_t(' ')
  283. #define U_HYPHEN char16_t('-')
  284. #define U_EQUAL char16_t('=')
  285. #define U_PERCENT char16_t('%')
  286. #define U_AMPERSAND char16_t('&')
  287. #define U_SEMICOLON char16_t(';')
  288. #define U_BACKSLASH char16_t('\\')
  289. #define U_OPEN_SINGLE_QUOTE char16_t(0x2018)
  290. #define U_OPEN_DOUBLE_QUOTE char16_t(0x201C)
  291. #define U_OPEN_GUILLEMET char16_t(0x00AB)
  292. #define NEED_CONTEXTUAL_ANALYSIS(c) (IS_HYPHEN(c) || \
  293. (c) == U_SLASH || \
  294. (c) == U_PERCENT || \
  295. (c) == U_AMPERSAND || \
  296. (c) == U_SEMICOLON || \
  297. (c) == U_BACKSLASH || \
  298. (c) == U_OPEN_SINGLE_QUOTE || \
  299. (c) == U_OPEN_DOUBLE_QUOTE || \
  300. (c) == U_OPEN_GUILLEMET)
  301. #define IS_ASCII_DIGIT(u) (0x0030 <= (u) && (u) <= 0x0039)
  302. static inline int
  303. GETCLASSFROMTABLE(const uint32_t* t, uint16_t l)
  304. {
  305. return ((((t)[(l>>3)]) >> ((l & 0x0007)<<2)) & 0x000f);
  306. }
  307. static inline int
  308. IS_HALFWIDTH_IN_JISx4051_CLASS3(char16_t u)
  309. {
  310. return ((0xff66 <= (u)) && ((u) <= 0xff70));
  311. }
  312. static inline int
  313. IS_CJK_CHAR(char16_t u)
  314. {
  315. return ((0x1100 <= (u) && (u) <= 0x11ff) ||
  316. (0x2e80 <= (u) && (u) <= 0xd7ff) ||
  317. (0xf900 <= (u) && (u) <= 0xfaff) ||
  318. (0xff00 <= (u) && (u) <= 0xffef) );
  319. }
  320. static inline bool
  321. IS_NONBREAKABLE_SPACE(char16_t u)
  322. {
  323. return u == 0x00A0 || u == 0x2007; // NO-BREAK SPACE, FIGURE SPACE
  324. }
  325. static inline bool
  326. IS_HYPHEN(char16_t u)
  327. {
  328. return (u == U_HYPHEN ||
  329. u == 0x058A || // ARMENIAN HYPHEN
  330. u == 0x2010 || // HYPHEN
  331. u == 0x2012 || // FIGURE DASH
  332. u == 0x2013); // EN DASH
  333. }
  334. static int8_t
  335. GetClass(uint32_t u)
  336. {
  337. if (u < 0x10000) {
  338. uint16_t h = u & 0xFF00;
  339. uint16_t l = u & 0x00ff;
  340. // Handle 3 range table first
  341. if (0x0000 == h) {
  342. return GETCLASSFROMTABLE(gLBClass00, l);
  343. }
  344. if (0x1700 == h) {
  345. return GETCLASSFROMTABLE(gLBClass17, l);
  346. }
  347. if (NS_NeedsPlatformNativeHandling(u)) {
  348. return CLASS_COMPLEX;
  349. }
  350. if (0x0E00 == h) {
  351. return GETCLASSFROMTABLE(gLBClass0E, l);
  352. }
  353. if (0x2000 == h) {
  354. return GETCLASSFROMTABLE(gLBClass20, l);
  355. }
  356. if (0x2100 == h) {
  357. return GETCLASSFROMTABLE(gLBClass21, l);
  358. }
  359. if (0x3000 == h) {
  360. return GETCLASSFROMTABLE(gLBClass30, l);
  361. }
  362. if (0xff00 == h) {
  363. if (l < 0x0060) { // Fullwidth ASCII variant
  364. return GETCLASSFROMTABLE(gLBClass00, (l+0x20));
  365. }
  366. if (l < 0x00a0) { // Halfwidth Katakana variants
  367. switch (l) {
  368. case 0x61: return GetClass(0x3002);
  369. case 0x62: return GetClass(0x300c);
  370. case 0x63: return GetClass(0x300d);
  371. case 0x64: return GetClass(0x3001);
  372. case 0x65: return GetClass(0x30fb);
  373. case 0x9e: return GetClass(0x309b);
  374. case 0x9f: return GetClass(0x309c);
  375. default:
  376. if (IS_HALFWIDTH_IN_JISx4051_CLASS3(u)) {
  377. return CLASS_CLOSE; // jis x4051 class 3
  378. }
  379. return CLASS_BREAKABLE; // jis x4051 class 11
  380. }
  381. }
  382. if (l < 0x00e0) {
  383. return CLASS_CHARACTER; // Halfwidth Hangul variants
  384. }
  385. if (l < 0x00f0) {
  386. static char16_t NarrowFFEx[16] = {
  387. 0x00A2, 0x00A3, 0x00AC, 0x00AF, 0x00A6, 0x00A5, 0x20A9, 0x0000,
  388. 0x2502, 0x2190, 0x2191, 0x2192, 0x2193, 0x25A0, 0x25CB, 0x0000
  389. };
  390. return GetClass(NarrowFFEx[l - 0x00e0]);
  391. }
  392. } else if (0x3100 == h) {
  393. if (l <= 0xbf) { // Hangul Compatibility Jamo, Bopomofo, Kanbun
  394. // XXX: This is per UAX #14, but UAX #14 may change
  395. // the line breaking rules about Kanbun and Bopomofo.
  396. return CLASS_BREAKABLE;
  397. }
  398. if (l >= 0xf0) { // Katakana small letters for Ainu
  399. return CLASS_CLOSE;
  400. }
  401. } else if (0x0300 == h) {
  402. if (0x4F == l || (0x5C <= l && l <= 0x62)) {
  403. return CLASS_NON_BREAKABLE;
  404. }
  405. } else if (0x0500 == h) {
  406. // ARMENIAN HYPHEN (for "Breaking Hyphens" of UAX#14)
  407. if (l == 0x8A) {
  408. return GETCLASSFROMTABLE(gLBClass00, uint16_t(U_HYPHEN));
  409. }
  410. } else if (0x0F00 == h) {
  411. if (0x08 == l || 0x0C == l || 0x12 == l) {
  412. return CLASS_NON_BREAKABLE;
  413. }
  414. } else if (0x1800 == h) {
  415. if (0x0E == l) {
  416. return CLASS_NON_BREAKABLE;
  417. }
  418. } else if (0x1600 == h) {
  419. if (0x80 == l) { // U+1680 OGHAM SPACE MARK
  420. return CLASS_BREAKABLE;
  421. }
  422. } else if (u == 0xfeff) {
  423. return CLASS_NON_BREAKABLE;
  424. }
  425. }
  426. // Mapping for Unicode LineBreak.txt classes to the (simplified) set of
  427. // character classes used here.
  428. // XXX The mappings here were derived by comparing the Unicode LineBreak
  429. // values of BMP characters to the classes our existing GetClass returns
  430. // for the same codepoints; in cases where characters with the same
  431. // LineBreak class mapped to various classes here, I picked what seemed
  432. // the most prevalent equivalence.
  433. // Some of these are unclear to me, but currently they are ONLY used
  434. // for characters not handled by the old code above, so all the JISx405
  435. // special cases should already be accounted for.
  436. static const int8_t sUnicodeLineBreakToClass[] = {
  437. /* UNKNOWN = 0, [XX] */ CLASS_CHARACTER,
  438. /* AMBIGUOUS = 1, [AI] */ CLASS_CHARACTER,
  439. /* ALPHABETIC = 2, [AL] */ CLASS_CHARACTER,
  440. /* BREAK_BOTH = 3, [B2] */ CLASS_CHARACTER,
  441. /* BREAK_AFTER = 4, [BA] */ CLASS_CHARACTER,
  442. /* BREAK_BEFORE = 5, [BB] */ CLASS_OPEN_LIKE_CHARACTER,
  443. /* MANDATORY_BREAK = 6, [BK] */ CLASS_CHARACTER,
  444. /* CONTINGENT_BREAK = 7, [CB] */ CLASS_CHARACTER,
  445. /* CLOSE_PUNCTUATION = 8, [CL] */ CLASS_CHARACTER,
  446. /* COMBINING_MARK = 9, [CM] */ CLASS_CHARACTER,
  447. /* CARRIAGE_RETURN = 10, [CR] */ CLASS_BREAKABLE,
  448. /* EXCLAMATION = 11, [EX] */ CLASS_CHARACTER,
  449. /* GLUE = 12, [GL] */ CLASS_NON_BREAKABLE,
  450. /* HYPHEN = 13, [HY] */ CLASS_CHARACTER,
  451. /* IDEOGRAPHIC = 14, [ID] */ CLASS_BREAKABLE,
  452. /* INSEPARABLE = 15, [IN] */ CLASS_CLOSE_LIKE_CHARACTER,
  453. /* INFIX_NUMERIC = 16, [IS] */ CLASS_CHARACTER,
  454. /* LINE_FEED = 17, [LF] */ CLASS_BREAKABLE,
  455. /* NONSTARTER = 18, [NS] */ CLASS_CLOSE_LIKE_CHARACTER,
  456. /* NUMERIC = 19, [NU] */ CLASS_CHARACTER,
  457. /* OPEN_PUNCTUATION = 20, [OP] */ CLASS_CHARACTER,
  458. /* POSTFIX_NUMERIC = 21, [PO] */ CLASS_CHARACTER,
  459. /* PREFIX_NUMERIC = 22, [PR] */ CLASS_CHARACTER,
  460. /* QUOTATION = 23, [QU] */ CLASS_CHARACTER,
  461. /* COMPLEX_CONTEXT = 24, [SA] */ CLASS_CHARACTER,
  462. /* SURROGATE = 25, [SG] */ CLASS_CHARACTER,
  463. /* SPACE = 26, [SP] */ CLASS_BREAKABLE,
  464. /* BREAK_SYMBOLS = 27, [SY] */ CLASS_CHARACTER,
  465. /* ZWSPACE = 28, [ZW] */ CLASS_BREAKABLE,
  466. /* NEXT_LINE = 29, [NL] */ CLASS_CHARACTER,
  467. /* WORD_JOINER = 30, [WJ] */ CLASS_NON_BREAKABLE,
  468. /* H2 = 31, [H2] */ CLASS_BREAKABLE,
  469. /* H3 = 32, [H3] */ CLASS_BREAKABLE,
  470. /* JL = 33, [JL] */ CLASS_CHARACTER,
  471. /* JT = 34, [JT] */ CLASS_CHARACTER,
  472. /* JV = 35, [JV] */ CLASS_CHARACTER,
  473. /* CLOSE_PARENTHESIS = 36, [CP] */ CLASS_CLOSE_LIKE_CHARACTER,
  474. /* CONDITIONAL_JAPANESE_STARTER = 37, [CJ] */ CLASS_CLOSE,
  475. /* HEBREW_LETTER = 38, [HL] */ CLASS_CHARACTER,
  476. /* REGIONAL_INDICATOR = 39, [RI] */ CLASS_CHARACTER,
  477. /* E_BASE = 40, [EB] */ CLASS_BREAKABLE,
  478. /* E_MODIFIER = 41, [EM] */ CLASS_CHARACTER,
  479. /* ZWJ = 42, [ZWJ]*/ CLASS_CHARACTER
  480. };
  481. static_assert(U_LB_COUNT == mozilla::ArrayLength(sUnicodeLineBreakToClass),
  482. "Gecko vs ICU LineBreak class mismatch");
  483. auto cls = mozilla::unicode::GetLineBreakClass(u);
  484. MOZ_ASSERT(cls < mozilla::ArrayLength(sUnicodeLineBreakToClass));
  485. return sUnicodeLineBreakToClass[cls];
  486. }
  487. static bool
  488. GetPair(int8_t c1, int8_t c2)
  489. {
  490. NS_ASSERTION(c1 < MAX_CLASSES ,"illegal classes 1");
  491. NS_ASSERTION(c2 < MAX_CLASSES ,"illegal classes 2");
  492. return (0 == ((gPair[c1] >> c2) & 0x0001));
  493. }
  494. static bool
  495. GetPairConservative(int8_t c1, int8_t c2)
  496. {
  497. NS_ASSERTION(c1 < MAX_CLASSES ,"illegal classes 1");
  498. NS_ASSERTION(c2 < MAX_CLASSES ,"illegal classes 2");
  499. return (0 == ((gPairConservative[c1] >> c2) & 0x0001));
  500. }
  501. nsJISx4051LineBreaker::nsJISx4051LineBreaker()
  502. {
  503. }
  504. nsJISx4051LineBreaker::~nsJISx4051LineBreaker()
  505. {
  506. }
  507. NS_IMPL_ISUPPORTS(nsJISx4051LineBreaker, nsILineBreaker)
  508. class ContextState {
  509. public:
  510. ContextState(const char16_t* aText, uint32_t aLength) {
  511. mUniText = aText;
  512. mText = nullptr;
  513. mLength = aLength;
  514. Init();
  515. }
  516. ContextState(const uint8_t* aText, uint32_t aLength) {
  517. mUniText = nullptr;
  518. mText = aText;
  519. mLength = aLength;
  520. Init();
  521. }
  522. uint32_t Length() { return mLength; }
  523. uint32_t Index() { return mIndex; }
  524. char16_t GetCharAt(uint32_t aIndex) {
  525. NS_ASSERTION(aIndex < mLength, "Out of range!");
  526. return mUniText ? mUniText[aIndex] : char16_t(mText[aIndex]);
  527. }
  528. void AdvanceIndex() {
  529. ++mIndex;
  530. }
  531. void NotifyBreakBefore() { mLastBreakIndex = mIndex; }
  532. // A word of western language should not be broken. But even if the word has
  533. // only ASCII characters, non-natural context words should be broken, e.g.,
  534. // URL and file path. For protecting the natural words, we should use
  535. // conservative breaking rules at following conditions:
  536. // 1. at near the start of word
  537. // 2. at near the end of word
  538. // 3. at near the latest broken point
  539. // CONSERVATIVE_BREAK_RANGE define the 'near' in characters.
  540. #define CONSERVATIVE_BREAK_RANGE 6
  541. bool UseConservativeBreaking(uint32_t aOffset = 0) {
  542. if (mHasCJKChar)
  543. return false;
  544. uint32_t index = mIndex + aOffset;
  545. bool result = (index < CONSERVATIVE_BREAK_RANGE ||
  546. mLength - index < CONSERVATIVE_BREAK_RANGE ||
  547. index - mLastBreakIndex < CONSERVATIVE_BREAK_RANGE);
  548. if (result || !mHasNonbreakableSpace)
  549. return result;
  550. // This text has no-breakable space, we need to check whether the index
  551. // is near it.
  552. // Note that index is always larger than CONSERVATIVE_BREAK_RANGE here.
  553. for (uint32_t i = index; index - CONSERVATIVE_BREAK_RANGE < i; --i) {
  554. if (IS_NONBREAKABLE_SPACE(GetCharAt(i - 1)))
  555. return true;
  556. }
  557. // Note that index is always less than mLength - CONSERVATIVE_BREAK_RANGE.
  558. for (uint32_t i = index + 1; i < index + CONSERVATIVE_BREAK_RANGE; ++i) {
  559. if (IS_NONBREAKABLE_SPACE(GetCharAt(i)))
  560. return true;
  561. }
  562. return false;
  563. }
  564. bool HasPreviousEqualsSign() const {
  565. return mHasPreviousEqualsSign;
  566. }
  567. void NotifySeenEqualsSign() {
  568. mHasPreviousEqualsSign = true;
  569. }
  570. bool HasPreviousSlash() const {
  571. return mHasPreviousSlash;
  572. }
  573. void NotifySeenSlash() {
  574. mHasPreviousSlash = true;
  575. }
  576. bool HasPreviousBackslash() const {
  577. return mHasPreviousBackslash;
  578. }
  579. void NotifySeenBackslash() {
  580. mHasPreviousBackslash = true;
  581. }
  582. uint32_t GetPreviousNonHyphenCharacter() const {
  583. return mPreviousNonHyphenCharacter;
  584. }
  585. void NotifyNonHyphenCharacter(uint32_t ch) {
  586. mPreviousNonHyphenCharacter = ch;
  587. }
  588. private:
  589. void Init() {
  590. mIndex = 0;
  591. mLastBreakIndex = 0;
  592. mPreviousNonHyphenCharacter = U_NULL;
  593. mHasCJKChar = 0;
  594. mHasNonbreakableSpace = 0;
  595. mHasPreviousEqualsSign = false;
  596. mHasPreviousSlash = false;
  597. mHasPreviousBackslash = false;
  598. for (uint32_t i = 0; i < mLength; ++i) {
  599. char16_t u = GetCharAt(i);
  600. if (!mHasNonbreakableSpace && IS_NONBREAKABLE_SPACE(u))
  601. mHasNonbreakableSpace = 1;
  602. else if (mUniText && !mHasCJKChar && IS_CJK_CHAR(u))
  603. mHasCJKChar = 1;
  604. }
  605. }
  606. const char16_t* mUniText;
  607. const uint8_t* mText;
  608. uint32_t mIndex;
  609. uint32_t mLength; // length of text
  610. uint32_t mLastBreakIndex;
  611. uint32_t mPreviousNonHyphenCharacter; // The last character we have seen
  612. // which is not U_HYPHEN
  613. bool mHasCJKChar; // if the text has CJK character, this is true.
  614. bool mHasNonbreakableSpace; // if the text has no-breakable space,
  615. // this is true.
  616. bool mHasPreviousEqualsSign; // True if we have seen a U_EQUAL
  617. bool mHasPreviousSlash; // True if we have seen a U_SLASH
  618. bool mHasPreviousBackslash; // True if we have seen a U_BACKSLASH
  619. };
  620. static int8_t
  621. ContextualAnalysis(char16_t prev, char16_t cur, char16_t next,
  622. ContextState &aState)
  623. {
  624. // Don't return CLASS_OPEN/CLASS_CLOSE if aState.UseJISX4051 is FALSE.
  625. if (IS_HYPHEN(cur)) {
  626. // If next character is hyphen, we don't need to break between them.
  627. if (IS_HYPHEN(next))
  628. return CLASS_CHARACTER;
  629. // If prev and next characters are numeric, it may be in Math context.
  630. // So, we should not break here.
  631. bool prevIsNum = IS_ASCII_DIGIT(prev);
  632. bool nextIsNum = IS_ASCII_DIGIT(next);
  633. if (prevIsNum && nextIsNum)
  634. return CLASS_NUMERIC;
  635. // If one side is numeric and the other is a character, or if both sides are
  636. // characters, the hyphen should be breakable.
  637. if (!aState.UseConservativeBreaking(1)) {
  638. char16_t prevOfHyphen = aState.GetPreviousNonHyphenCharacter();
  639. if (prevOfHyphen && next) {
  640. int8_t prevClass = GetClass(prevOfHyphen);
  641. int8_t nextClass = GetClass(next);
  642. bool prevIsNumOrCharOrClose =
  643. prevIsNum ||
  644. (prevClass == CLASS_CHARACTER &&
  645. !NEED_CONTEXTUAL_ANALYSIS(prevOfHyphen)) ||
  646. prevClass == CLASS_CLOSE ||
  647. prevClass == CLASS_CLOSE_LIKE_CHARACTER;
  648. bool nextIsNumOrCharOrOpen =
  649. nextIsNum ||
  650. (nextClass == CLASS_CHARACTER && !NEED_CONTEXTUAL_ANALYSIS(next)) ||
  651. nextClass == CLASS_OPEN ||
  652. nextClass == CLASS_OPEN_LIKE_CHARACTER ||
  653. next == U_OPEN_SINGLE_QUOTE ||
  654. next == U_OPEN_DOUBLE_QUOTE ||
  655. next == U_OPEN_GUILLEMET;
  656. if (prevIsNumOrCharOrClose && nextIsNumOrCharOrOpen) {
  657. return CLASS_CLOSE;
  658. }
  659. }
  660. }
  661. } else {
  662. aState.NotifyNonHyphenCharacter(cur);
  663. if (cur == U_SLASH || cur == U_BACKSLASH) {
  664. // If this is immediately after same char, we should not break here.
  665. if (prev == cur)
  666. return CLASS_CHARACTER;
  667. // If this text has two or more (BACK)SLASHs, this may be file path or URL.
  668. // Make sure to compute shouldReturn before we notify on this slash.
  669. bool shouldReturn = !aState.UseConservativeBreaking() &&
  670. (cur == U_SLASH ?
  671. aState.HasPreviousSlash() : aState.HasPreviousBackslash());
  672. if (cur == U_SLASH) {
  673. aState.NotifySeenSlash();
  674. } else {
  675. aState.NotifySeenBackslash();
  676. }
  677. if (shouldReturn)
  678. return CLASS_OPEN;
  679. } else if (cur == U_PERCENT) {
  680. // If this is a part of the param of URL, we should break before.
  681. if (!aState.UseConservativeBreaking()) {
  682. if (aState.Index() >= 3 &&
  683. aState.GetCharAt(aState.Index() - 3) == U_PERCENT)
  684. return CLASS_OPEN;
  685. if (aState.Index() + 3 < aState.Length() &&
  686. aState.GetCharAt(aState.Index() + 3) == U_PERCENT)
  687. return CLASS_OPEN;
  688. }
  689. } else if (cur == U_AMPERSAND || cur == U_SEMICOLON) {
  690. // If this may be a separator of params of URL, we should break after.
  691. if (!aState.UseConservativeBreaking(1) &&
  692. aState.HasPreviousEqualsSign())
  693. return CLASS_CLOSE;
  694. } else if (cur == U_OPEN_SINGLE_QUOTE ||
  695. cur == U_OPEN_DOUBLE_QUOTE ||
  696. cur == U_OPEN_GUILLEMET) {
  697. // for CJK usage, we treat these as openers to allow a break before them,
  698. // but otherwise treat them as normal characters because quote mark usage
  699. // in various Western languages varies too much; see bug #450088 discussion.
  700. if (!aState.UseConservativeBreaking() && IS_CJK_CHAR(next))
  701. return CLASS_OPEN;
  702. } else {
  703. NS_ERROR("Forgot to handle the current character!");
  704. }
  705. }
  706. return GetClass(cur);
  707. }
  708. int32_t
  709. nsJISx4051LineBreaker::WordMove(const char16_t* aText, uint32_t aLen,
  710. uint32_t aPos, int8_t aDirection)
  711. {
  712. bool textNeedsJISx4051 = false;
  713. int32_t begin, end;
  714. for (begin = aPos; begin > 0 && !NS_IsSpace(aText[begin - 1]); --begin) {
  715. if (IS_CJK_CHAR(aText[begin]) || NS_NeedsPlatformNativeHandling(aText[begin])) {
  716. textNeedsJISx4051 = true;
  717. }
  718. }
  719. for (end = aPos + 1; end < int32_t(aLen) && !NS_IsSpace(aText[end]); ++end) {
  720. if (IS_CJK_CHAR(aText[end]) || NS_NeedsPlatformNativeHandling(aText[end])) {
  721. textNeedsJISx4051 = true;
  722. }
  723. }
  724. int32_t ret;
  725. AutoTArray<uint8_t, 2000> breakState;
  726. if (!textNeedsJISx4051 || !breakState.AppendElements(end - begin)) {
  727. // No complex text character, do not try to do complex line break.
  728. // (This is required for serializers. See Bug #344816.)
  729. // Also fall back to this when out of memory.
  730. if (aDirection < 0) {
  731. ret = (begin == int32_t(aPos)) ? begin - 1 : begin;
  732. } else {
  733. ret = end;
  734. }
  735. } else {
  736. GetJISx4051Breaks(aText + begin, end - begin, nsILineBreaker::kWordBreak_Normal,
  737. breakState.Elements());
  738. ret = aPos;
  739. do {
  740. ret += aDirection;
  741. } while (begin < ret && ret < end && !breakState[ret - begin]);
  742. }
  743. return ret;
  744. }
  745. int32_t
  746. nsJISx4051LineBreaker::Next(const char16_t* aText, uint32_t aLen,
  747. uint32_t aPos)
  748. {
  749. NS_ASSERTION(aText, "aText shouldn't be null");
  750. NS_ASSERTION(aLen > aPos, "Bad position passed to nsJISx4051LineBreaker::Next");
  751. int32_t nextPos = WordMove(aText, aLen, aPos, 1);
  752. return nextPos < int32_t(aLen) ? nextPos : NS_LINEBREAKER_NEED_MORE_TEXT;
  753. }
  754. int32_t
  755. nsJISx4051LineBreaker::Prev(const char16_t* aText, uint32_t aLen,
  756. uint32_t aPos)
  757. {
  758. NS_ASSERTION(aText, "aText shouldn't be null");
  759. NS_ASSERTION(aLen >= aPos && aPos > 0,
  760. "Bad position passed to nsJISx4051LineBreaker::Prev");
  761. int32_t prevPos = WordMove(aText, aLen, aPos, -1);
  762. return prevPos > 0 ? prevPos : NS_LINEBREAKER_NEED_MORE_TEXT;
  763. }
  764. void
  765. nsJISx4051LineBreaker::GetJISx4051Breaks(const char16_t* aChars, uint32_t aLength,
  766. uint8_t aWordBreak,
  767. uint8_t* aBreakBefore)
  768. {
  769. uint32_t cur;
  770. int8_t lastClass = CLASS_NONE;
  771. ContextState state(aChars, aLength);
  772. for (cur = 0; cur < aLength; ++cur, state.AdvanceIndex()) {
  773. uint32_t ch = aChars[cur];
  774. if (NS_IS_HIGH_SURROGATE(ch)) {
  775. if (cur + 1 < aLength && NS_IS_LOW_SURROGATE(aChars[cur + 1])) {
  776. ch = SURROGATE_TO_UCS4(ch, aChars[cur + 1]);
  777. }
  778. }
  779. int8_t cl;
  780. if (NEED_CONTEXTUAL_ANALYSIS(ch)) {
  781. cl = ContextualAnalysis(cur > 0 ? aChars[cur - 1] : U_NULL,
  782. ch,
  783. cur + 1 < aLength ? aChars[cur + 1] : U_NULL,
  784. state);
  785. } else {
  786. if (ch == U_EQUAL)
  787. state.NotifySeenEqualsSign();
  788. state.NotifyNonHyphenCharacter(ch);
  789. cl = GetClass(ch);
  790. }
  791. bool allowBreak = false;
  792. if (cur > 0) {
  793. NS_ASSERTION(CLASS_COMPLEX != lastClass || CLASS_COMPLEX != cl,
  794. "Loop should have prevented adjacent complex chars here");
  795. if (aWordBreak == nsILineBreaker::kWordBreak_Normal) {
  796. allowBreak = (state.UseConservativeBreaking()) ?
  797. GetPairConservative(lastClass, cl) : GetPair(lastClass, cl);
  798. } else if (aWordBreak == nsILineBreaker::kWordBreak_BreakAll) {
  799. allowBreak = true;
  800. }
  801. }
  802. aBreakBefore[cur] = allowBreak;
  803. if (allowBreak)
  804. state.NotifyBreakBefore();
  805. lastClass = cl;
  806. if (CLASS_COMPLEX == cl) {
  807. uint32_t end = cur + 1;
  808. while (end < aLength && CLASS_COMPLEX == GetClass(aChars[end])) {
  809. ++end;
  810. }
  811. NS_GetComplexLineBreaks(aChars + cur, end - cur, aBreakBefore + cur);
  812. // We have to consider word-break value again for complex characters
  813. if (aWordBreak != nsILineBreaker::kWordBreak_Normal) {
  814. // Respect word-break property
  815. for (uint32_t i = cur; i < end; i++)
  816. aBreakBefore[i] = (aWordBreak == nsILineBreaker::kWordBreak_BreakAll);
  817. }
  818. // restore breakability at chunk begin, which was always set to false
  819. // by the complex line breaker
  820. aBreakBefore[cur] = allowBreak;
  821. cur = end - 1;
  822. }
  823. if (ch > 0xffff) {
  824. // Supplementary-plane character: mark that we cannot break before the
  825. // trailing low surrogate, and advance past it.
  826. ++cur;
  827. aBreakBefore[cur] = false;
  828. state.AdvanceIndex();
  829. }
  830. }
  831. }
  832. void
  833. nsJISx4051LineBreaker::GetJISx4051Breaks(const uint8_t* aChars, uint32_t aLength,
  834. uint8_t aWordBreak,
  835. uint8_t* aBreakBefore)
  836. {
  837. uint32_t cur;
  838. int8_t lastClass = CLASS_NONE;
  839. ContextState state(aChars, aLength);
  840. for (cur = 0; cur < aLength; ++cur, state.AdvanceIndex()) {
  841. char16_t ch = aChars[cur];
  842. int8_t cl;
  843. if (NEED_CONTEXTUAL_ANALYSIS(ch)) {
  844. cl = ContextualAnalysis(cur > 0 ? aChars[cur - 1] : U_NULL,
  845. ch,
  846. cur + 1 < aLength ? aChars[cur + 1] : U_NULL,
  847. state);
  848. } else {
  849. if (ch == U_EQUAL)
  850. state.NotifySeenEqualsSign();
  851. state.NotifyNonHyphenCharacter(ch);
  852. cl = GetClass(ch);
  853. }
  854. bool allowBreak = false;
  855. if (cur > 0) {
  856. if (aWordBreak == nsILineBreaker::kWordBreak_Normal) {
  857. allowBreak = (state.UseConservativeBreaking()) ?
  858. GetPairConservative(lastClass, cl) : GetPair(lastClass, cl);
  859. } else if (aWordBreak == nsILineBreaker::kWordBreak_BreakAll) {
  860. allowBreak = true;
  861. }
  862. }
  863. aBreakBefore[cur] = allowBreak;
  864. if (allowBreak)
  865. state.NotifyBreakBefore();
  866. lastClass = cl;
  867. }
  868. }