MathMLTextRunFactory.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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 "MathMLTextRunFactory.h"
  6. #include "mozilla/ArrayUtils.h"
  7. #include "mozilla/BinarySearch.h"
  8. #include "nsStyleConsts.h"
  9. #include "nsTextFrameUtils.h"
  10. #include "nsFontMetrics.h"
  11. #include "nsDeviceContext.h"
  12. #include "nsUnicodeScriptCodes.h"
  13. using namespace mozilla;
  14. /*
  15. Entries for the mathvariant lookup tables. mKey represents the Unicode
  16. character to be transformed and is used for searching the tables.
  17. mReplacement represents the mapped mathvariant Unicode character.
  18. */
  19. typedef struct
  20. {
  21. uint32_t mKey;
  22. uint32_t mReplacement;
  23. } MathVarMapping;
  24. /*
  25. Lookup tables for use with mathvariant mappings to transform a unicode
  26. character point to another unicode character that indicates the proper output.
  27. mKey represents one of two concepts.
  28. 1. In the Latin table it represents a hole in the mathematical alphanumeric
  29. block, where the character that should occupy that position is located
  30. elsewhere.
  31. 2. It represents an Arabic letter.
  32. As a replacement, 0 is reserved to indicate no mapping was found.
  33. */
  34. static const MathVarMapping gArabicInitialMapTable[] = {
  35. { 0x628, 0x1EE21 },
  36. { 0x62A, 0x1EE35 },
  37. { 0x62B, 0x1EE36 },
  38. { 0x62C, 0x1EE22 },
  39. { 0x62D, 0x1EE27 },
  40. { 0x62E, 0x1EE37 },
  41. { 0x633, 0x1EE2E },
  42. { 0x634, 0x1EE34 },
  43. { 0x635, 0x1EE31 },
  44. { 0x636, 0x1EE39 },
  45. { 0x639, 0x1EE2F },
  46. { 0x63A, 0x1EE3B },
  47. { 0x641, 0x1EE30 },
  48. { 0x642, 0x1EE32 },
  49. { 0x643, 0x1EE2A },
  50. { 0x644, 0x1EE2B },
  51. { 0x645, 0x1EE2C },
  52. { 0x646, 0x1EE2D },
  53. { 0x647, 0x1EE24 },
  54. { 0x64A, 0x1EE29 }
  55. };
  56. static const MathVarMapping gArabicTailedMapTable[] = {
  57. { 0x62C, 0x1EE42 },
  58. { 0x62D, 0x1EE47 },
  59. { 0x62E, 0x1EE57 },
  60. { 0x633, 0x1EE4E },
  61. { 0x634, 0x1EE54 },
  62. { 0x635, 0x1EE51 },
  63. { 0x636, 0x1EE59 },
  64. { 0x639, 0x1EE4F },
  65. { 0x63A, 0x1EE5B },
  66. { 0x642, 0x1EE52 },
  67. { 0x644, 0x1EE4B },
  68. { 0x646, 0x1EE4D },
  69. { 0x64A, 0x1EE49 },
  70. { 0x66F, 0x1EE5F },
  71. { 0x6BA, 0x1EE5D }
  72. };
  73. static const MathVarMapping gArabicStretchedMapTable[] = {
  74. { 0x628, 0x1EE61 },
  75. { 0x62A, 0x1EE75 },
  76. { 0x62B, 0x1EE76 },
  77. { 0x62C, 0x1EE62 },
  78. { 0x62D, 0x1EE67 },
  79. { 0x62E, 0x1EE77 },
  80. { 0x633, 0x1EE6E },
  81. { 0x634, 0x1EE74 },
  82. { 0x635, 0x1EE71 },
  83. { 0x636, 0x1EE79 },
  84. { 0x637, 0x1EE68 },
  85. { 0x638, 0x1EE7A },
  86. { 0x639, 0x1EE6F },
  87. { 0x63A, 0x1EE7B },
  88. { 0x641, 0x1EE70 },
  89. { 0x642, 0x1EE72 },
  90. { 0x643, 0x1EE6A },
  91. { 0x645, 0x1EE6C },
  92. { 0x646, 0x1EE6D },
  93. { 0x647, 0x1EE64 },
  94. { 0x64A, 0x1EE69 },
  95. { 0x66E, 0x1EE7C },
  96. { 0x6A1, 0x1EE7E }
  97. };
  98. static const MathVarMapping gArabicLoopedMapTable[] = {
  99. { 0x627, 0x1EE80 },
  100. { 0x628, 0x1EE81 },
  101. { 0x62A, 0x1EE95 },
  102. { 0x62B, 0x1EE96 },
  103. { 0x62C, 0x1EE82 },
  104. { 0x62D, 0x1EE87 },
  105. { 0x62E, 0x1EE97 },
  106. { 0x62F, 0x1EE83 },
  107. { 0x630, 0x1EE98 },
  108. { 0x631, 0x1EE93 },
  109. { 0x632, 0x1EE86 },
  110. { 0x633, 0x1EE8E },
  111. { 0x634, 0x1EE94 },
  112. { 0x635, 0x1EE91 },
  113. { 0x636, 0x1EE99 },
  114. { 0x637, 0x1EE88 },
  115. { 0x638, 0x1EE9A },
  116. { 0x639, 0x1EE8F },
  117. { 0x63A, 0x1EE9B },
  118. { 0x641, 0x1EE90 },
  119. { 0x642, 0x1EE92 },
  120. { 0x644, 0x1EE8B },
  121. { 0x645, 0x1EE8C },
  122. { 0x646, 0x1EE8D },
  123. { 0x647, 0x1EE84 },
  124. { 0x648, 0x1EE85 },
  125. { 0x64A, 0x1EE89 }
  126. };
  127. static const MathVarMapping gArabicDoubleMapTable[] = {
  128. { 0x628, 0x1EEA1 },
  129. { 0x62A, 0x1EEB5 },
  130. { 0x62B, 0x1EEB6 },
  131. { 0x62C, 0x1EEA2 },
  132. { 0x62D, 0x1EEA7 },
  133. { 0x62E, 0x1EEB7 },
  134. { 0x62F, 0x1EEA3 },
  135. { 0x630, 0x1EEB8 },
  136. { 0x631, 0x1EEB3 },
  137. { 0x632, 0x1EEA6 },
  138. { 0x633, 0x1EEAE },
  139. { 0x634, 0x1EEB4 },
  140. { 0x635, 0x1EEB1 },
  141. { 0x636, 0x1EEB9 },
  142. { 0x637, 0x1EEA8 },
  143. { 0x638, 0x1EEBA },
  144. { 0x639, 0x1EEAF },
  145. { 0x63A, 0x1EEBB },
  146. { 0x641, 0x1EEB0 },
  147. { 0x642, 0x1EEB2 },
  148. { 0x644, 0x1EEAB },
  149. { 0x645, 0x1EEAC },
  150. { 0x646, 0x1EEAD },
  151. { 0x648, 0x1EEA5 },
  152. { 0x64A, 0x1EEA9 }
  153. };
  154. static const MathVarMapping gLatinExceptionMapTable[] = {
  155. { 0x1D455, 0x210E },
  156. { 0x1D49D, 0x212C },
  157. { 0x1D4A0, 0x2130 },
  158. { 0x1D4A1, 0x2131 },
  159. { 0x1D4A3, 0x210B },
  160. { 0x1D4A4, 0x2110 },
  161. { 0x1D4A7, 0x2112 },
  162. { 0x1D4A8, 0x2133 },
  163. { 0x1D4AD, 0x211B },
  164. { 0x1D4BA, 0x212F },
  165. { 0x1D4BC, 0x210A },
  166. { 0x1D4C4, 0x2134 },
  167. { 0x1D506, 0x212D },
  168. { 0x1D50B, 0x210C },
  169. { 0x1D50C, 0x2111 },
  170. { 0x1D515, 0x211C },
  171. { 0x1D51D, 0x2128 },
  172. { 0x1D53A, 0x2102 },
  173. { 0x1D53F, 0x210D },
  174. { 0x1D545, 0x2115 },
  175. { 0x1D547, 0x2119 },
  176. { 0x1D548, 0x211A },
  177. { 0x1D549, 0x211D },
  178. { 0x1D551, 0x2124 }
  179. };
  180. namespace {
  181. struct MathVarMappingWrapper
  182. {
  183. const MathVarMapping* const mTable;
  184. explicit MathVarMappingWrapper(const MathVarMapping* aTable) : mTable(aTable) {}
  185. uint32_t operator[](size_t index) const {
  186. return mTable[index].mKey;
  187. }
  188. };
  189. } // namespace
  190. // Finds a MathVarMapping struct with the specified key (aKey) within aTable.
  191. // aTable must be an array, whose length is specified by aNumElements
  192. static uint32_t
  193. MathvarMappingSearch(uint32_t aKey, const MathVarMapping* aTable, uint32_t aNumElements)
  194. {
  195. size_t index;
  196. if (BinarySearch(MathVarMappingWrapper(aTable), 0, aNumElements, aKey, &index)) {
  197. return aTable[index].mReplacement;
  198. }
  199. return 0;
  200. }
  201. #define GREEK_UPPER_THETA 0x03F4
  202. #define HOLE_GREEK_UPPER_THETA 0x03A2
  203. #define NABLA 0x2207
  204. #define PARTIAL_DIFFERENTIAL 0x2202
  205. #define GREEK_UPPER_ALPHA 0x0391
  206. #define GREEK_UPPER_OMEGA 0x03A9
  207. #define GREEK_LOWER_ALPHA 0x03B1
  208. #define GREEK_LOWER_OMEGA 0x03C9
  209. #define GREEK_LUNATE_EPSILON_SYMBOL 0x03F5
  210. #define GREEK_THETA_SYMBOL 0x03D1
  211. #define GREEK_KAPPA_SYMBOL 0x03F0
  212. #define GREEK_PHI_SYMBOL 0x03D5
  213. #define GREEK_RHO_SYMBOL 0x03F1
  214. #define GREEK_PI_SYMBOL 0x03D6
  215. #define GREEK_LETTER_DIGAMMA 0x03DC
  216. #define GREEK_SMALL_LETTER_DIGAMMA 0x03DD
  217. #define MATH_BOLD_CAPITAL_DIGAMMA 0x1D7CA
  218. #define MATH_BOLD_SMALL_DIGAMMA 0x1D7CB
  219. #define LATIN_SMALL_LETTER_DOTLESS_I 0x0131
  220. #define LATIN_SMALL_LETTER_DOTLESS_J 0x0237
  221. #define MATH_ITALIC_SMALL_DOTLESS_I 0x1D6A4
  222. #define MATH_ITALIC_SMALL_DOTLESS_J 0x1D6A5
  223. #define MATH_BOLD_UPPER_A 0x1D400
  224. #define MATH_ITALIC_UPPER_A 0x1D434
  225. #define MATH_BOLD_SMALL_A 0x1D41A
  226. #define MATH_BOLD_UPPER_ALPHA 0x1D6A8
  227. #define MATH_BOLD_SMALL_ALPHA 0x1D6C2
  228. #define MATH_ITALIC_UPPER_ALPHA 0x1D6E2
  229. #define MATH_BOLD_DIGIT_ZERO 0x1D7CE
  230. #define MATH_DOUBLE_STRUCK_ZERO 0x1D7D8
  231. #define MATH_BOLD_UPPER_THETA 0x1D6B9
  232. #define MATH_BOLD_NABLA 0x1D6C1
  233. #define MATH_BOLD_PARTIAL_DIFFERENTIAL 0x1D6DB
  234. #define MATH_BOLD_EPSILON_SYMBOL 0x1D6DC
  235. #define MATH_BOLD_THETA_SYMBOL 0x1D6DD
  236. #define MATH_BOLD_KAPPA_SYMBOL 0x1D6DE
  237. #define MATH_BOLD_PHI_SYMBOL 0x1D6DF
  238. #define MATH_BOLD_RHO_SYMBOL 0x1D6E0
  239. #define MATH_BOLD_PI_SYMBOL 0x1D6E1
  240. /*
  241. Performs the character mapping needed to implement MathML's mathvariant
  242. attribute. It takes a unicode character and maps it to its appropriate
  243. mathvariant counterpart specified by aMathVar. The mapped character is
  244. typically located within Unicode's mathematical blocks (0x1D***, 0x1EE**) but
  245. there are exceptions which this function accounts for.
  246. Characters without a valid mapping or valid aMathvar value are returned
  247. unaltered. Characters already in the mathematical blocks (or are one of the
  248. exceptions) are never transformed.
  249. Acceptable values for aMathVar are specified in layout/style/nsStyleConsts.h.
  250. The transformable characters can be found at:
  251. http://lists.w3.org/Archives/Public/www-math/2013Sep/0012.html and
  252. https://en.wikipedia.org/wiki/Mathematical_Alphanumeric_Symbols
  253. */
  254. static uint32_t
  255. MathVariant(uint32_t aCh, uint8_t aMathVar)
  256. {
  257. uint32_t baseChar;
  258. enum CharacterType {
  259. kIsLatin,
  260. kIsGreekish,
  261. kIsNumber,
  262. kIsArabic,
  263. };
  264. CharacterType varType;
  265. int8_t multiplier;
  266. if (aMathVar <= NS_MATHML_MATHVARIANT_NORMAL) {
  267. // nothing to do here
  268. return aCh;
  269. }
  270. if (aMathVar > NS_MATHML_MATHVARIANT_STRETCHED) {
  271. NS_ASSERTION(false, "Illegal mathvariant value");
  272. return aCh;
  273. }
  274. // Exceptional characters with at most one possible transformation
  275. if (aCh == HOLE_GREEK_UPPER_THETA) {
  276. // Nothing at this code point is transformed
  277. return aCh;
  278. }
  279. if (aCh == GREEK_LETTER_DIGAMMA) {
  280. if (aMathVar == NS_MATHML_MATHVARIANT_BOLD) {
  281. return MATH_BOLD_CAPITAL_DIGAMMA;
  282. }
  283. return aCh;
  284. }
  285. if (aCh == GREEK_SMALL_LETTER_DIGAMMA) {
  286. if (aMathVar == NS_MATHML_MATHVARIANT_BOLD) {
  287. return MATH_BOLD_SMALL_DIGAMMA;
  288. }
  289. return aCh;
  290. }
  291. if (aCh == LATIN_SMALL_LETTER_DOTLESS_I) {
  292. if (aMathVar == NS_MATHML_MATHVARIANT_ITALIC) {
  293. return MATH_ITALIC_SMALL_DOTLESS_I;
  294. }
  295. return aCh;
  296. }
  297. if (aCh == LATIN_SMALL_LETTER_DOTLESS_J) {
  298. if (aMathVar == NS_MATHML_MATHVARIANT_ITALIC) {
  299. return MATH_ITALIC_SMALL_DOTLESS_J;
  300. }
  301. return aCh;
  302. }
  303. // The Unicode mathematical blocks are divided into four segments: Latin,
  304. // Greek, numbers and Arabic. In the case of the first three
  305. // baseChar represents the relative order in which the characters are
  306. // encoded in the Unicode mathematical block, normalised to the first
  307. // character of that sequence.
  308. //
  309. if ('A' <= aCh && aCh <= 'Z') {
  310. baseChar = aCh - 'A';
  311. varType = kIsLatin;
  312. } else if ('a' <= aCh && aCh <= 'z') {
  313. // Lowercase characters are placed immediately after the uppercase
  314. // characters in the Unicode mathematical block. The constant subtraction
  315. // represents the number of characters between the start of the sequence
  316. // (capital A) and the first lowercase letter.
  317. baseChar = MATH_BOLD_SMALL_A-MATH_BOLD_UPPER_A + aCh - 'a';
  318. varType = kIsLatin;
  319. } else if ('0' <= aCh && aCh <= '9') {
  320. baseChar = aCh - '0';
  321. varType = kIsNumber;
  322. } else if (GREEK_UPPER_ALPHA <= aCh && aCh <= GREEK_UPPER_OMEGA) {
  323. baseChar = aCh-GREEK_UPPER_ALPHA;
  324. varType = kIsGreekish;
  325. } else if (GREEK_LOWER_ALPHA <= aCh && aCh <= GREEK_LOWER_OMEGA) {
  326. // Lowercase Greek comes after uppercase Greek.
  327. // Note in this instance the presence of an additional character (Nabla)
  328. // between the end of the uppercase Greek characters and the lowercase
  329. // ones.
  330. baseChar = MATH_BOLD_SMALL_ALPHA - MATH_BOLD_UPPER_ALPHA
  331. + aCh-GREEK_LOWER_ALPHA;
  332. varType = kIsGreekish;
  333. } else if (0x0600 <= aCh && aCh <= 0x06FF) {
  334. // Arabic characters are defined within this range
  335. varType = kIsArabic;
  336. } else {
  337. switch (aCh) {
  338. case GREEK_UPPER_THETA:
  339. baseChar = MATH_BOLD_UPPER_THETA-MATH_BOLD_UPPER_ALPHA;
  340. break;
  341. case NABLA:
  342. baseChar = MATH_BOLD_NABLA-MATH_BOLD_UPPER_ALPHA;
  343. break;
  344. case PARTIAL_DIFFERENTIAL:
  345. baseChar = MATH_BOLD_PARTIAL_DIFFERENTIAL - MATH_BOLD_UPPER_ALPHA;
  346. break;
  347. case GREEK_LUNATE_EPSILON_SYMBOL:
  348. baseChar = MATH_BOLD_EPSILON_SYMBOL - MATH_BOLD_UPPER_ALPHA;
  349. break;
  350. case GREEK_THETA_SYMBOL:
  351. baseChar = MATH_BOLD_THETA_SYMBOL - MATH_BOLD_UPPER_ALPHA;
  352. break;
  353. case GREEK_KAPPA_SYMBOL:
  354. baseChar = MATH_BOLD_KAPPA_SYMBOL - MATH_BOLD_UPPER_ALPHA;
  355. break;
  356. case GREEK_PHI_SYMBOL:
  357. baseChar = MATH_BOLD_PHI_SYMBOL - MATH_BOLD_UPPER_ALPHA;
  358. break;
  359. case GREEK_RHO_SYMBOL:
  360. baseChar = MATH_BOLD_RHO_SYMBOL - MATH_BOLD_UPPER_ALPHA;
  361. break;
  362. case GREEK_PI_SYMBOL:
  363. baseChar = MATH_BOLD_PI_SYMBOL - MATH_BOLD_UPPER_ALPHA;
  364. break;
  365. default:
  366. return aCh;
  367. }
  368. varType = kIsGreekish;
  369. }
  370. if (varType == kIsNumber) {
  371. switch (aMathVar) {
  372. // Each possible number mathvariant is encoded in a single, contiguous
  373. // block. For example the beginning of the double struck number range
  374. // follows immediately after the end of the bold number range.
  375. // multiplier represents the order of the sequences relative to the first
  376. // one.
  377. case NS_MATHML_MATHVARIANT_BOLD:
  378. multiplier = 0;
  379. break;
  380. case NS_MATHML_MATHVARIANT_DOUBLE_STRUCK:
  381. multiplier = 1;
  382. break;
  383. case NS_MATHML_MATHVARIANT_SANS_SERIF:
  384. multiplier = 2;
  385. break;
  386. case NS_MATHML_MATHVARIANT_BOLD_SANS_SERIF:
  387. multiplier = 3;
  388. break;
  389. case NS_MATHML_MATHVARIANT_MONOSPACE:
  390. multiplier = 4;
  391. break;
  392. default:
  393. // This mathvariant isn't defined for numbers or is otherwise normal
  394. return aCh;
  395. }
  396. // As the ranges are contiguous, to find the desired mathvariant range it
  397. // is sufficient to multiply the position within the sequence order
  398. // (multiplier) with the period of the sequence (which is constant for all
  399. // number sequences) and to add the character point of the first character
  400. // within the number mathvariant range.
  401. // To this the baseChar calculated earlier is added to obtain the final
  402. // code point.
  403. return baseChar+multiplier*(MATH_DOUBLE_STRUCK_ZERO-MATH_BOLD_DIGIT_ZERO)
  404. +MATH_BOLD_DIGIT_ZERO;
  405. } else if (varType == kIsGreekish) {
  406. switch (aMathVar) {
  407. case NS_MATHML_MATHVARIANT_BOLD:
  408. multiplier = 0;
  409. break;
  410. case NS_MATHML_MATHVARIANT_ITALIC:
  411. multiplier = 1;
  412. break;
  413. case NS_MATHML_MATHVARIANT_BOLD_ITALIC:
  414. multiplier = 2;
  415. break;
  416. case NS_MATHML_MATHVARIANT_BOLD_SANS_SERIF:
  417. multiplier = 3;
  418. break;
  419. case NS_MATHML_MATHVARIANT_SANS_SERIF_BOLD_ITALIC:
  420. multiplier = 4;
  421. break;
  422. default:
  423. // This mathvariant isn't defined for Greek or is otherwise normal
  424. return aCh;
  425. }
  426. // See the kIsNumber case for an explanation of the following calculation
  427. return baseChar + MATH_BOLD_UPPER_ALPHA +
  428. multiplier*(MATH_ITALIC_UPPER_ALPHA - MATH_BOLD_UPPER_ALPHA);
  429. }
  430. uint32_t tempChar;
  431. uint32_t newChar;
  432. if (varType == kIsArabic) {
  433. const MathVarMapping* mapTable;
  434. uint32_t tableLength;
  435. switch (aMathVar) {
  436. /* The Arabic mathematical block is not continuous, nor does it have a
  437. * monotonic mapping to the unencoded characters, requiring the use of a
  438. * lookup table.
  439. */
  440. case NS_MATHML_MATHVARIANT_INITIAL:
  441. mapTable = gArabicInitialMapTable;
  442. tableLength = ArrayLength(gArabicInitialMapTable);
  443. break;
  444. case NS_MATHML_MATHVARIANT_TAILED:
  445. mapTable = gArabicTailedMapTable;
  446. tableLength = ArrayLength(gArabicTailedMapTable);
  447. break;
  448. case NS_MATHML_MATHVARIANT_STRETCHED:
  449. mapTable = gArabicStretchedMapTable;
  450. tableLength = ArrayLength(gArabicStretchedMapTable);
  451. break;
  452. case NS_MATHML_MATHVARIANT_LOOPED:
  453. mapTable = gArabicLoopedMapTable;
  454. tableLength = ArrayLength(gArabicLoopedMapTable);
  455. break;
  456. case NS_MATHML_MATHVARIANT_DOUBLE_STRUCK:
  457. mapTable = gArabicDoubleMapTable;
  458. tableLength = ArrayLength(gArabicDoubleMapTable);
  459. break;
  460. default:
  461. // No valid transformations exist
  462. return aCh;
  463. }
  464. newChar = MathvarMappingSearch(aCh, mapTable, tableLength);
  465. } else {
  466. // Must be Latin
  467. if (aMathVar > NS_MATHML_MATHVARIANT_MONOSPACE) {
  468. // Latin doesn't support the Arabic mathvariants
  469. return aCh;
  470. }
  471. multiplier = aMathVar - 2;
  472. // This is possible because the values for NS_MATHML_MATHVARIANT_* are
  473. // chosen to coincide with the order in which the encoded mathvariant
  474. // characters are located within their unicode block (less an offset to
  475. // avoid _NONE and _NORMAL variants)
  476. // See the kIsNumber case for an explanation of the following calculation
  477. tempChar = baseChar + MATH_BOLD_UPPER_A +
  478. multiplier*(MATH_ITALIC_UPPER_A - MATH_BOLD_UPPER_A);
  479. // There are roughly twenty characters that are located outside of the
  480. // mathematical block, so the spaces where they ought to be are used
  481. // as keys for a lookup table containing the correct character mappings.
  482. newChar = MathvarMappingSearch(tempChar, gLatinExceptionMapTable,
  483. ArrayLength(gLatinExceptionMapTable));
  484. }
  485. if (newChar) {
  486. return newChar;
  487. } else if (varType == kIsLatin) {
  488. return tempChar;
  489. } else {
  490. // An Arabic character without a corresponding mapping
  491. return aCh;
  492. }
  493. }
  494. #define TT_SSTY TRUETYPE_TAG('s', 's', 't', 'y')
  495. #define TT_DTLS TRUETYPE_TAG('d', 't', 'l', 's')
  496. void
  497. MathMLTextRunFactory::RebuildTextRun(nsTransformedTextRun* aTextRun,
  498. mozilla::gfx::DrawTarget* aRefDrawTarget,
  499. gfxMissingFontRecorder* aMFR)
  500. {
  501. gfxFontGroup* fontGroup = aTextRun->GetFontGroup();
  502. nsAutoString convertedString;
  503. AutoTArray<bool,50> charsToMergeArray;
  504. AutoTArray<bool,50> deletedCharsArray;
  505. AutoTArray<RefPtr<nsTransformedCharStyle>,50> styleArray;
  506. AutoTArray<uint8_t,50> canBreakBeforeArray;
  507. bool mergeNeeded = false;
  508. bool singleCharMI =
  509. aTextRun->GetFlags() & nsTextFrameUtils::TEXT_IS_SINGLE_CHAR_MI;
  510. uint32_t length = aTextRun->GetLength();
  511. const char16_t* str = aTextRun->mString.BeginReading();
  512. const nsTArray<RefPtr<nsTransformedCharStyle>>& styles = aTextRun->mStyles;
  513. nsFont font;
  514. if (length) {
  515. font = styles[0]->mFont;
  516. if (mSSTYScriptLevel || (mFlags & MATH_FONT_FEATURE_DTLS)) {
  517. bool foundSSTY = false;
  518. bool foundDTLS = false;
  519. // We respect ssty settings explicitly set by the user
  520. for (uint32_t i = 0; i < font.fontFeatureSettings.Length(); i++) {
  521. if (font.fontFeatureSettings[i].mTag == TT_SSTY) {
  522. foundSSTY = true;
  523. } else if (font.fontFeatureSettings[i].mTag == TT_DTLS) {
  524. foundDTLS = true;
  525. }
  526. }
  527. if (mSSTYScriptLevel && !foundSSTY) {
  528. uint8_t sstyLevel = 0;
  529. float scriptScaling = pow(styles[0]->mScriptSizeMultiplier,
  530. mSSTYScriptLevel);
  531. static_assert(NS_MATHML_DEFAULT_SCRIPT_SIZE_MULTIPLIER < 1,
  532. "Shouldn't it make things smaller?");
  533. /*
  534. An SSTY level of 2 is set if the scaling factor is less than or equal
  535. to halfway between that for a scriptlevel of 1 (0.71) and that of a
  536. scriptlevel of 2 (0.71^2), assuming the default script size multiplier.
  537. An SSTY level of 1 is set if the script scaling factor is less than
  538. or equal that for a scriptlevel of 1 assuming the default script size
  539. multiplier.
  540. User specified values of script size multiplier will change the scaling
  541. factor which mSSTYScriptLevel values correspond to.
  542. In the event that the script size multiplier actually makes things
  543. larger, no change is made.
  544. To opt out of this change, add the following to the stylesheet:
  545. "font-feature-settings: 'ssty' 0"
  546. */
  547. if (scriptScaling <= (NS_MATHML_DEFAULT_SCRIPT_SIZE_MULTIPLIER +
  548. (NS_MATHML_DEFAULT_SCRIPT_SIZE_MULTIPLIER *
  549. NS_MATHML_DEFAULT_SCRIPT_SIZE_MULTIPLIER))/2) {
  550. // Currently only the first two ssty settings are used, so two is large
  551. // as we go
  552. sstyLevel = 2;
  553. } else if (scriptScaling <= NS_MATHML_DEFAULT_SCRIPT_SIZE_MULTIPLIER) {
  554. sstyLevel = 1;
  555. }
  556. if (sstyLevel) {
  557. gfxFontFeature settingSSTY;
  558. settingSSTY.mTag = TT_SSTY;
  559. settingSSTY.mValue = sstyLevel;
  560. font.fontFeatureSettings.AppendElement(settingSSTY);
  561. }
  562. }
  563. /*
  564. Apply the dtls font feature setting (dotless).
  565. This gets applied to the base frame and all descendants of the base
  566. frame of certain <mover> and <munderover> frames.
  567. See nsMathMLmunderoverFrame.cpp for a full description.
  568. To opt out of this change, add the following to the stylesheet:
  569. "font-feature-settings: 'dtls' 0"
  570. */
  571. if ((mFlags & MATH_FONT_FEATURE_DTLS) && !foundDTLS) {
  572. gfxFontFeature settingDTLS;
  573. settingDTLS.mTag = TT_DTLS;
  574. settingDTLS.mValue = 1;
  575. font.fontFeatureSettings.AppendElement(settingDTLS);
  576. }
  577. }
  578. }
  579. uint8_t mathVar = NS_MATHML_MATHVARIANT_NONE;
  580. bool doMathvariantStyling = true;
  581. for (uint32_t i = 0; i < length; ++i) {
  582. int extraChars = 0;
  583. mathVar = styles[i]->mMathVariant;
  584. if (singleCharMI && mathVar == NS_MATHML_MATHVARIANT_NONE) {
  585. // If the user has explicitly set a non-default value for fontstyle or
  586. // fontweight, the italic mathvariant behaviour of <mi> is disabled
  587. // This overrides the initial values specified in fontStyle, to avoid
  588. // inconsistencies in which attributes allow CSS changes and which do not.
  589. if (mFlags & MATH_FONT_WEIGHT_BOLD) {
  590. font.weight = NS_FONT_WEIGHT_BOLD;
  591. if (mFlags & MATH_FONT_STYLING_NORMAL) {
  592. font.style = NS_FONT_STYLE_NORMAL;
  593. } else {
  594. font.style = NS_FONT_STYLE_ITALIC;
  595. }
  596. } else if (mFlags & MATH_FONT_STYLING_NORMAL) {
  597. font.style = NS_FONT_STYLE_NORMAL;
  598. font.weight = NS_FONT_WEIGHT_NORMAL;
  599. } else {
  600. mathVar = NS_MATHML_MATHVARIANT_ITALIC;
  601. }
  602. }
  603. uint32_t ch = str[i];
  604. if (NS_IS_HIGH_SURROGATE(ch) && i < length - 1 &&
  605. NS_IS_LOW_SURROGATE(str[i + 1])) {
  606. ch = SURROGATE_TO_UCS4(ch, str[i + 1]);
  607. }
  608. uint32_t ch2 = MathVariant(ch, mathVar);
  609. if (mathVar == NS_MATHML_MATHVARIANT_BOLD ||
  610. mathVar == NS_MATHML_MATHVARIANT_BOLD_ITALIC ||
  611. mathVar == NS_MATHML_MATHVARIANT_ITALIC) {
  612. if (ch == ch2 && ch != 0x20 && ch != 0xA0) {
  613. // Don't apply the CSS style if a character cannot be
  614. // transformed. There is an exception for whitespace as it is both
  615. // common and innocuous.
  616. doMathvariantStyling = false;
  617. }
  618. if (ch2 != ch) {
  619. // Bug 930504. Some platforms do not have fonts for Mathematical
  620. // Alphanumeric Symbols. Hence we check whether the transformed
  621. // character is actually available.
  622. uint8_t matchType;
  623. RefPtr<gfxFont> mathFont = fontGroup->
  624. FindFontForChar(ch2, 0, 0, unicode::Script::COMMON, nullptr, &matchType);
  625. if (mathFont) {
  626. // Don't apply the CSS style if there is a math font for at least one
  627. // of the transformed character in this text run.
  628. doMathvariantStyling = false;
  629. } else {
  630. // We fallback to the original character.
  631. ch2 = ch;
  632. if (aMFR) {
  633. aMFR->RecordScript(unicode::Script::MATHEMATICAL_NOTATION);
  634. }
  635. }
  636. }
  637. }
  638. deletedCharsArray.AppendElement(false);
  639. charsToMergeArray.AppendElement(false);
  640. styleArray.AppendElement(styles[i]);
  641. canBreakBeforeArray.AppendElement(aTextRun->CanBreakLineBefore(i));
  642. if (IS_IN_BMP(ch2)) {
  643. convertedString.Append(ch2);
  644. } else {
  645. convertedString.Append(H_SURROGATE(ch2));
  646. convertedString.Append(L_SURROGATE(ch2));
  647. ++extraChars;
  648. if (!IS_IN_BMP(ch)) {
  649. deletedCharsArray.AppendElement(true); // not exactly deleted, but
  650. // the trailing surrogate is skipped
  651. ++i;
  652. }
  653. }
  654. while (extraChars-- > 0) {
  655. mergeNeeded = true;
  656. charsToMergeArray.AppendElement(true);
  657. styleArray.AppendElement(styles[i]);
  658. canBreakBeforeArray.AppendElement(false);
  659. }
  660. }
  661. uint32_t flags;
  662. gfxTextRunFactory::Parameters innerParams =
  663. GetParametersForInner(aTextRun, &flags, aRefDrawTarget);
  664. RefPtr<nsTransformedTextRun> transformedChild;
  665. RefPtr<gfxTextRun> cachedChild;
  666. gfxTextRun* child;
  667. if (mathVar == NS_MATHML_MATHVARIANT_BOLD && doMathvariantStyling) {
  668. font.style = NS_FONT_STYLE_NORMAL;
  669. font.weight = NS_FONT_WEIGHT_BOLD;
  670. } else if (mathVar == NS_MATHML_MATHVARIANT_ITALIC && doMathvariantStyling) {
  671. font.style = NS_FONT_STYLE_ITALIC;
  672. font.weight = NS_FONT_WEIGHT_NORMAL;
  673. } else if (mathVar == NS_MATHML_MATHVARIANT_BOLD_ITALIC &&
  674. doMathvariantStyling) {
  675. font.style = NS_FONT_STYLE_ITALIC;
  676. font.weight = NS_FONT_WEIGHT_BOLD;
  677. } else if (mathVar != NS_MATHML_MATHVARIANT_NONE) {
  678. // Mathvariant overrides fontstyle and fontweight
  679. // Need to check to see if mathvariant is actually applied as this function
  680. // is used for other purposes.
  681. font.style = NS_FONT_STYLE_NORMAL;
  682. font.weight = NS_FONT_WEIGHT_NORMAL;
  683. }
  684. gfxFontGroup* newFontGroup = nullptr;
  685. // Get the correct gfxFontGroup that corresponds to the earlier font changes.
  686. if (length) {
  687. font.size = NSToCoordRound(font.size * mFontInflation);
  688. nsPresContext* pc = styles[0]->mPresContext;
  689. nsFontMetrics::Params params;
  690. params.language = styles[0]->mLanguage;
  691. params.explicitLanguage = styles[0]->mExplicitLanguage;
  692. params.userFontSet = pc->GetUserFontSet();
  693. params.textPerf = pc->GetTextPerfMetrics();
  694. RefPtr<nsFontMetrics> metrics =
  695. pc->DeviceContext()->GetMetricsFor(font, params);
  696. newFontGroup = metrics->GetThebesFontGroup();
  697. }
  698. if (!newFontGroup) {
  699. // If we can't get a new font group, fall back to the old one. Rendering
  700. // will be incorrect, but not significantly so.
  701. newFontGroup = fontGroup;
  702. }
  703. if (mInnerTransformingTextRunFactory) {
  704. transformedChild = mInnerTransformingTextRunFactory->MakeTextRun(
  705. convertedString.BeginReading(), convertedString.Length(),
  706. &innerParams, newFontGroup, flags, Move(styleArray), false);
  707. child = transformedChild.get();
  708. } else {
  709. cachedChild = newFontGroup->MakeTextRun(
  710. convertedString.BeginReading(), convertedString.Length(),
  711. &innerParams, flags, aMFR);
  712. child = cachedChild.get();
  713. }
  714. if (!child)
  715. return;
  716. typedef gfxTextRun::Range Range;
  717. // Copy potential linebreaks into child so they're preserved
  718. // (and also child will be shaped appropriately)
  719. NS_ASSERTION(convertedString.Length() == canBreakBeforeArray.Length(),
  720. "Dropped characters or break-before values somewhere!");
  721. Range range(0, uint32_t(canBreakBeforeArray.Length()));
  722. child->SetPotentialLineBreaks(range, canBreakBeforeArray.Elements());
  723. if (transformedChild) {
  724. transformedChild->FinishSettingProperties(aRefDrawTarget, aMFR);
  725. }
  726. if (mergeNeeded) {
  727. // Now merge multiple characters into one multi-glyph character as required
  728. NS_ASSERTION(charsToMergeArray.Length() == child->GetLength(),
  729. "source length mismatch");
  730. NS_ASSERTION(deletedCharsArray.Length() == aTextRun->GetLength(),
  731. "destination length mismatch");
  732. MergeCharactersInTextRun(aTextRun, child, charsToMergeArray.Elements(),
  733. deletedCharsArray.Elements());
  734. } else {
  735. // No merging to do, so just copy; this produces a more optimized textrun.
  736. // We can't steal the data because the child may be cached and stealing
  737. // the data would break the cache.
  738. aTextRun->ResetGlyphRuns();
  739. aTextRun->CopyGlyphDataFrom(child, Range(child), 0);
  740. }
  741. }