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