michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "MathMLTextRunFactory.h" michael@0: michael@0: #include "mozilla/ArrayUtils.h" michael@0: michael@0: #include "nsStyleConsts.h" michael@0: #include "nsStyleContext.h" michael@0: #include "nsTextFrameUtils.h" michael@0: michael@0: using namespace mozilla; michael@0: michael@0: /* michael@0: Entries for the mathvariant lookup tables. mKey represents the Unicode michael@0: character to be transformed and is used for searching the tables. michael@0: mReplacement represents the mapped mathvariant Unicode character. michael@0: */ michael@0: typedef struct michael@0: { michael@0: uint32_t mKey; michael@0: uint32_t mReplacement; michael@0: } MathVarMapping; michael@0: michael@0: /* michael@0: Lookup tables for use with mathvariant mappings to transform a unicode michael@0: character point to another unicode character that indicates the proper output. michael@0: mKey represents one of two concepts. michael@0: 1. In the Latin table it represents a hole in the mathematical alphanumeric michael@0: block, where the character that should occupy that position is located michael@0: elsewhere. michael@0: 2. It represents an Arabic letter. michael@0: michael@0: As a replacement, 0 is reserved to indicate no mapping was found. michael@0: */ michael@0: static const MathVarMapping gArabicInitialMapTable[] = { michael@0: { 0x628, 0x1EE21 }, michael@0: { 0x62A, 0x1EE35 }, michael@0: { 0x62B, 0x1EE36 }, michael@0: { 0x62C, 0x1EE22 }, michael@0: { 0x62D, 0x1EE27 }, michael@0: { 0x62E, 0x1EE37 }, michael@0: { 0x633, 0x1EE2E }, michael@0: { 0x634, 0x1EE34 }, michael@0: { 0x635, 0x1EE31 }, michael@0: { 0x636, 0x1EE39 }, michael@0: { 0x639, 0x1EE2F }, michael@0: { 0x63A, 0x1EE3B }, michael@0: { 0x641, 0x1EE30 }, michael@0: { 0x642, 0x1EE32 }, michael@0: { 0x643, 0x1EE2A }, michael@0: { 0x644, 0x1EE2B }, michael@0: { 0x645, 0x1EE2C }, michael@0: { 0x646, 0x1EE2D }, michael@0: { 0x647, 0x1EE24 }, michael@0: { 0x64A, 0x1EE29 } michael@0: }; michael@0: michael@0: static const MathVarMapping gArabicTailedMapTable[] = { michael@0: { 0x62C, 0x1EE42 }, michael@0: { 0x62D, 0x1EE47 }, michael@0: { 0x62E, 0x1EE57 }, michael@0: { 0x633, 0x1EE4E }, michael@0: { 0x634, 0x1EE54 }, michael@0: { 0x635, 0x1EE51 }, michael@0: { 0x636, 0x1EE59 }, michael@0: { 0x639, 0x1EE4F }, michael@0: { 0x63A, 0x1EE5B }, michael@0: { 0x642, 0x1EE52 }, michael@0: { 0x644, 0x1EE4B }, michael@0: { 0x646, 0x1EE4D }, michael@0: { 0x64A, 0x1EE49 }, michael@0: { 0x66F, 0x1EE5F }, michael@0: { 0x6BA, 0x1EE5D } michael@0: }; michael@0: michael@0: static const MathVarMapping gArabicStretchedMapTable[] = { michael@0: { 0x628, 0x1EE61 }, michael@0: { 0x62A, 0x1EE75 }, michael@0: { 0x62B, 0x1EE76 }, michael@0: { 0x62C, 0x1EE62 }, michael@0: { 0x62D, 0x1EE67 }, michael@0: { 0x62E, 0x1EE77 }, michael@0: { 0x633, 0x1EE6E }, michael@0: { 0x634, 0x1EE74 }, michael@0: { 0x635, 0x1EE71 }, michael@0: { 0x636, 0x1EE79 }, michael@0: { 0x637, 0x1EE68 }, michael@0: { 0x638, 0x1EE7A }, michael@0: { 0x639, 0x1EE6F }, michael@0: { 0x63A, 0x1EE7B }, michael@0: { 0x641, 0x1EE70 }, michael@0: { 0x642, 0x1EE72 }, michael@0: { 0x643, 0x1EE6A }, michael@0: { 0x645, 0x1EE6C }, michael@0: { 0x646, 0x1EE6D }, michael@0: { 0x647, 0x1EE64 }, michael@0: { 0x64A, 0x1EE69 }, michael@0: { 0x66E, 0x1EE7C }, michael@0: { 0x6A1, 0x1EE7E } michael@0: }; michael@0: michael@0: static const MathVarMapping gArabicLoopedMapTable[] = { michael@0: { 0x627, 0x1EE80 }, michael@0: { 0x628, 0x1EE81 }, michael@0: { 0x62A, 0x1EE95 }, michael@0: { 0x62B, 0x1EE96 }, michael@0: { 0x62C, 0x1EE82 }, michael@0: { 0x62D, 0x1EE87 }, michael@0: { 0x62E, 0x1EE97 }, michael@0: { 0x62F, 0x1EE83 }, michael@0: { 0x630, 0x1EE98 }, michael@0: { 0x631, 0x1EE93 }, michael@0: { 0x632, 0x1EE86 }, michael@0: { 0x633, 0x1EE8E }, michael@0: { 0x634, 0x1EE94 }, michael@0: { 0x635, 0x1EE91 }, michael@0: { 0x636, 0x1EE99 }, michael@0: { 0x637, 0x1EE88 }, michael@0: { 0x638, 0x1EE9A }, michael@0: { 0x639, 0x1EE8F }, michael@0: { 0x63A, 0x1EE9B }, michael@0: { 0x641, 0x1EE90 }, michael@0: { 0x642, 0x1EE92 }, michael@0: { 0x644, 0x1EE8B }, michael@0: { 0x645, 0x1EE8C }, michael@0: { 0x646, 0x1EE8D }, michael@0: { 0x647, 0x1EE84 }, michael@0: { 0x648, 0x1EE85 }, michael@0: { 0x64A, 0x1EE89 } michael@0: }; michael@0: michael@0: static const MathVarMapping gArabicDoubleMapTable[] = { michael@0: { 0x628, 0x1EEA1 }, michael@0: { 0x62A, 0x1EEB5 }, michael@0: { 0x62B, 0x1EEB6 }, michael@0: { 0x62C, 0x1EEA2 }, michael@0: { 0x62D, 0x1EEA7 }, michael@0: { 0x62E, 0x1EEB7 }, michael@0: { 0x62F, 0x1EEA3 }, michael@0: { 0x630, 0x1EEB8 }, michael@0: { 0x631, 0x1EEB3 }, michael@0: { 0x632, 0x1EEA6 }, michael@0: { 0x633, 0x1EEAE }, michael@0: { 0x634, 0x1EEB4 }, michael@0: { 0x635, 0x1EEB1 }, michael@0: { 0x636, 0x1EEB9 }, michael@0: { 0x637, 0x1EEA8 }, michael@0: { 0x638, 0x1EEBA }, michael@0: { 0x639, 0x1EEAF }, michael@0: { 0x63A, 0x1EEBB }, michael@0: { 0x641, 0x1EEB0 }, michael@0: { 0x642, 0x1EEB2 }, michael@0: { 0x644, 0x1EEAB }, michael@0: { 0x645, 0x1EEAC }, michael@0: { 0x646, 0x1EEAD }, michael@0: { 0x648, 0x1EEA5 }, michael@0: { 0x64A, 0x1EEA9 } michael@0: }; michael@0: michael@0: static const MathVarMapping gLatinExceptionMapTable[] = { michael@0: { 0x1D455, 0x210E }, michael@0: { 0x1D49D, 0x212C }, michael@0: { 0x1D4A0, 0x2130 }, michael@0: { 0x1D4A1, 0x2131 }, michael@0: { 0x1D4A3, 0x210B }, michael@0: { 0x1D4A4, 0x2110 }, michael@0: { 0x1D4A7, 0x2112 }, michael@0: { 0x1D4A8, 0x2133 }, michael@0: { 0x1D4AD, 0x211B }, michael@0: { 0x1D4BA, 0x212F }, michael@0: { 0x1D4BC, 0x210A }, michael@0: { 0x1D4C4, 0x2134 }, michael@0: { 0x1D506, 0x212D }, michael@0: { 0x1D50B, 0x210C }, michael@0: { 0x1D50C, 0x2111 }, michael@0: { 0x1D515, 0x211C }, michael@0: { 0x1D51D, 0x2128 }, michael@0: { 0x1D53A, 0x2102 }, michael@0: { 0x1D53F, 0x210D }, michael@0: { 0x1D545, 0x2115 }, michael@0: { 0x1D547, 0x2119 }, michael@0: { 0x1D548, 0x211A }, michael@0: { 0x1D549, 0x211D }, michael@0: { 0x1D551, 0x2124 } michael@0: }; michael@0: michael@0: // Finds a MathVarMapping struct with the specified key (aKey) within aTable. michael@0: // aTable must be an array, whose length is specified by aNumElements michael@0: static uint32_t michael@0: MathvarMappingSearch(uint32_t aKey, const MathVarMapping* aTable, uint32_t aNumElements) michael@0: { michael@0: uint32_t low = 0; michael@0: uint32_t high = aNumElements; michael@0: while (high > low) { michael@0: uint32_t midPoint = (low+high) >> 1; michael@0: if (aKey == aTable[midPoint].mKey) { michael@0: return aTable[midPoint].mReplacement; michael@0: } michael@0: if (aKey > aTable[midPoint].mKey) { michael@0: low = midPoint + 1; michael@0: } else { michael@0: high = midPoint; michael@0: } michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: #define GREEK_UPPER_THETA 0x03F4 michael@0: #define HOLE_GREEK_UPPER_THETA 0x03A2 michael@0: #define NABLA 0x2207 michael@0: #define PARTIAL_DIFFERENTIAL 0x2202 michael@0: #define GREEK_UPPER_ALPHA 0x0391 michael@0: #define GREEK_UPPER_OMEGA 0x03A9 michael@0: #define GREEK_LOWER_ALPHA 0x03B1 michael@0: #define GREEK_LOWER_OMEGA 0x03C9 michael@0: #define GREEK_LUNATE_EPSILON_SYMBOL 0x03F5 michael@0: #define GREEK_THETA_SYMBOL 0x03D1 michael@0: #define GREEK_KAPPA_SYMBOL 0x03F0 michael@0: #define GREEK_PHI_SYMBOL 0x03D5 michael@0: #define GREEK_RHO_SYMBOL 0x03F1 michael@0: #define GREEK_PI_SYMBOL 0x03D6 michael@0: #define GREEK_LETTER_DIGAMMA 0x03DC michael@0: #define GREEK_SMALL_LETTER_DIGAMMA 0x03DD michael@0: #define MATH_BOLD_CAPITAL_DIGAMMA 0x1D7CA michael@0: #define MATH_BOLD_SMALL_DIGAMMA 0x1D7CB michael@0: michael@0: #define LATIN_SMALL_LETTER_DOTLESS_I 0x0131 michael@0: #define LATIN_SMALL_LETTER_DOTLESS_J 0x0237 michael@0: michael@0: #define MATH_ITALIC_SMALL_DOTLESS_I 0x1D6A4 michael@0: #define MATH_ITALIC_SMALL_DOTLESS_J 0x1D6A5 michael@0: michael@0: #define MATH_BOLD_UPPER_A 0x1D400 michael@0: #define MATH_ITALIC_UPPER_A 0x1D434 michael@0: #define MATH_BOLD_SMALL_A 0x1D41A michael@0: #define MATH_BOLD_UPPER_ALPHA 0x1D6A8 michael@0: #define MATH_BOLD_SMALL_ALPHA 0x1D6C2 michael@0: #define MATH_ITALIC_UPPER_ALPHA 0x1D6E2 michael@0: #define MATH_BOLD_DIGIT_ZERO 0x1D7CE michael@0: #define MATH_DOUBLE_STRUCK_ZERO 0x1D7D8 michael@0: michael@0: #define MATH_BOLD_UPPER_THETA 0x1D6B9 michael@0: #define MATH_BOLD_NABLA 0x1D6C1 michael@0: #define MATH_BOLD_PARTIAL_DIFFERENTIAL 0x1D6DB michael@0: #define MATH_BOLD_EPSILON_SYMBOL 0x1D6DC michael@0: #define MATH_BOLD_THETA_SYMBOL 0x1D6DD michael@0: #define MATH_BOLD_KAPPA_SYMBOL 0x1D6DE michael@0: #define MATH_BOLD_PHI_SYMBOL 0x1D6DF michael@0: #define MATH_BOLD_RHO_SYMBOL 0x1D6E0 michael@0: #define MATH_BOLD_PI_SYMBOL 0x1D6E1 michael@0: michael@0: /* michael@0: Performs the character mapping needed to implement MathML's mathvariant michael@0: attribute. It takes a unicode character and maps it to its appropriate michael@0: mathvariant counterpart specified by aMathVar. The mapped character is michael@0: typically located within Unicode's mathematical blocks (0x1D***, 0x1EE**) but michael@0: there are exceptions which this function accounts for. michael@0: Characters without a valid mapping or valid aMathvar value are returned michael@0: unaltered. Characters already in the mathematical blocks (or are one of the michael@0: exceptions) are never transformed. michael@0: Acceptable values for aMathVar are specified in layout/style/nsStyleConsts.h. michael@0: The transformable characters can be found at: michael@0: http://lists.w3.org/Archives/Public/www-math/2013Sep/0012.html and michael@0: https://en.wikipedia.org/wiki/Mathematical_Alphanumeric_Symbols michael@0: */ michael@0: static uint32_t michael@0: MathVariant(uint32_t aCh, uint8_t aMathVar) michael@0: { michael@0: uint32_t baseChar; michael@0: enum CharacterType { michael@0: kIsLatin, michael@0: kIsGreekish, michael@0: kIsNumber, michael@0: kIsArabic, michael@0: }; michael@0: CharacterType varType; michael@0: michael@0: int8_t multiplier; michael@0: michael@0: if (aMathVar <= NS_MATHML_MATHVARIANT_NORMAL) { michael@0: // nothing to do here michael@0: return aCh; michael@0: } michael@0: if (aMathVar > NS_MATHML_MATHVARIANT_STRETCHED) { michael@0: NS_ASSERTION(false, "Illegal mathvariant value"); michael@0: return aCh; michael@0: } michael@0: michael@0: // Exceptional characters with at most one possible transformation michael@0: if (aCh == HOLE_GREEK_UPPER_THETA) { michael@0: // Nothing at this code point is transformed michael@0: return aCh; michael@0: } michael@0: if (aCh == GREEK_LETTER_DIGAMMA) { michael@0: if (aMathVar == NS_MATHML_MATHVARIANT_BOLD) { michael@0: return MATH_BOLD_CAPITAL_DIGAMMA; michael@0: } michael@0: return aCh; michael@0: } michael@0: if (aCh == GREEK_SMALL_LETTER_DIGAMMA) { michael@0: if (aMathVar == NS_MATHML_MATHVARIANT_BOLD) { michael@0: return MATH_BOLD_SMALL_DIGAMMA; michael@0: } michael@0: return aCh; michael@0: } michael@0: if (aCh == LATIN_SMALL_LETTER_DOTLESS_I) { michael@0: if (aMathVar == NS_MATHML_MATHVARIANT_ITALIC) { michael@0: return MATH_ITALIC_SMALL_DOTLESS_I; michael@0: } michael@0: return aCh; michael@0: } michael@0: if (aCh == LATIN_SMALL_LETTER_DOTLESS_J) { michael@0: if (aMathVar == NS_MATHML_MATHVARIANT_ITALIC) { michael@0: return MATH_ITALIC_SMALL_DOTLESS_J; michael@0: } michael@0: return aCh; michael@0: } michael@0: michael@0: // The Unicode mathematical blocks are divided into four segments: Latin, michael@0: // Greek, numbers and Arabic. In the case of the first three michael@0: // baseChar represents the relative order in which the characters are michael@0: // encoded in the Unicode mathematical block, normalised to the first michael@0: // character of that sequence. michael@0: // michael@0: if ('A' <= aCh && aCh <= 'Z') { michael@0: baseChar = aCh - 'A'; michael@0: varType = kIsLatin; michael@0: } else if ('a' <= aCh && aCh <= 'z') { michael@0: // Lowercase characters are placed immediately after the uppercase michael@0: // characters in the Unicode mathematical block. The constant subtraction michael@0: // represents the number of characters between the start of the sequence michael@0: // (capital A) and the first lowercase letter. michael@0: baseChar = MATH_BOLD_SMALL_A-MATH_BOLD_UPPER_A + aCh - 'a'; michael@0: varType = kIsLatin; michael@0: } else if ('0' <= aCh && aCh <= '9') { michael@0: baseChar = aCh - '0'; michael@0: varType = kIsNumber; michael@0: } else if (GREEK_UPPER_ALPHA <= aCh && aCh <= GREEK_UPPER_OMEGA) { michael@0: baseChar = aCh-GREEK_UPPER_ALPHA; michael@0: varType = kIsGreekish; michael@0: } else if (GREEK_LOWER_ALPHA <= aCh && aCh <= GREEK_LOWER_OMEGA) { michael@0: // Lowercase Greek comes after uppercase Greek. michael@0: // Note in this instance the presence of an additional character (Nabla) michael@0: // between the end of the uppercase Greek characters and the lowercase michael@0: // ones. michael@0: baseChar = MATH_BOLD_SMALL_ALPHA - MATH_BOLD_UPPER_ALPHA michael@0: + aCh-GREEK_LOWER_ALPHA; michael@0: varType = kIsGreekish; michael@0: } else if (0x0600 <= aCh && aCh <= 0x06FF) { michael@0: // Arabic characters are defined within this range michael@0: varType = kIsArabic; michael@0: } else { michael@0: switch (aCh) { michael@0: case GREEK_UPPER_THETA: michael@0: baseChar = MATH_BOLD_UPPER_THETA-MATH_BOLD_UPPER_ALPHA; michael@0: break; michael@0: case NABLA: michael@0: baseChar = MATH_BOLD_NABLA-MATH_BOLD_UPPER_ALPHA; michael@0: break; michael@0: case PARTIAL_DIFFERENTIAL: michael@0: baseChar = MATH_BOLD_PARTIAL_DIFFERENTIAL - MATH_BOLD_UPPER_ALPHA; michael@0: break; michael@0: case GREEK_LUNATE_EPSILON_SYMBOL: michael@0: baseChar = MATH_BOLD_EPSILON_SYMBOL - MATH_BOLD_UPPER_ALPHA; michael@0: break; michael@0: case GREEK_THETA_SYMBOL: michael@0: baseChar = MATH_BOLD_THETA_SYMBOL - MATH_BOLD_UPPER_ALPHA; michael@0: break; michael@0: case GREEK_KAPPA_SYMBOL: michael@0: baseChar = MATH_BOLD_KAPPA_SYMBOL - MATH_BOLD_UPPER_ALPHA; michael@0: break; michael@0: case GREEK_PHI_SYMBOL: michael@0: baseChar = MATH_BOLD_PHI_SYMBOL - MATH_BOLD_UPPER_ALPHA; michael@0: break; michael@0: case GREEK_RHO_SYMBOL: michael@0: baseChar = MATH_BOLD_RHO_SYMBOL - MATH_BOLD_UPPER_ALPHA; michael@0: break; michael@0: case GREEK_PI_SYMBOL: michael@0: baseChar = MATH_BOLD_PI_SYMBOL - MATH_BOLD_UPPER_ALPHA; michael@0: break; michael@0: default: michael@0: return aCh; michael@0: } michael@0: michael@0: varType = kIsGreekish; michael@0: } michael@0: michael@0: if (varType == kIsNumber) { michael@0: switch (aMathVar) { michael@0: // Each possible number mathvariant is encoded in a single, contiguous michael@0: // block. For example the beginning of the double struck number range michael@0: // follows immediately after the end of the bold number range. michael@0: // multiplier represents the order of the sequences relative to the first michael@0: // one. michael@0: case NS_MATHML_MATHVARIANT_BOLD: michael@0: multiplier = 0; michael@0: break; michael@0: case NS_MATHML_MATHVARIANT_DOUBLE_STRUCK: michael@0: multiplier = 1; michael@0: break; michael@0: case NS_MATHML_MATHVARIANT_SANS_SERIF: michael@0: multiplier = 2; michael@0: break; michael@0: case NS_MATHML_MATHVARIANT_BOLD_SANS_SERIF: michael@0: multiplier = 3; michael@0: break; michael@0: case NS_MATHML_MATHVARIANT_MONOSPACE: michael@0: multiplier = 4; michael@0: break; michael@0: default: michael@0: // This mathvariant isn't defined for numbers or is otherwise normal michael@0: return aCh; michael@0: } michael@0: // As the ranges are contiguous, to find the desired mathvariant range it michael@0: // is sufficient to multiply the position within the sequence order michael@0: // (multiplier) with the period of the sequence (which is constant for all michael@0: // number sequences) and to add the character point of the first character michael@0: // within the number mathvariant range. michael@0: // To this the baseChar calculated earlier is added to obtain the final michael@0: // code point. michael@0: return baseChar+multiplier*(MATH_DOUBLE_STRUCK_ZERO-MATH_BOLD_DIGIT_ZERO) michael@0: +MATH_BOLD_DIGIT_ZERO; michael@0: } else if (varType == kIsGreekish) { michael@0: switch (aMathVar) { michael@0: case NS_MATHML_MATHVARIANT_BOLD: michael@0: multiplier = 0; michael@0: break; michael@0: case NS_MATHML_MATHVARIANT_ITALIC: michael@0: multiplier = 1; michael@0: break; michael@0: case NS_MATHML_MATHVARIANT_BOLD_ITALIC: michael@0: multiplier = 2; michael@0: break; michael@0: case NS_MATHML_MATHVARIANT_BOLD_SANS_SERIF: michael@0: multiplier = 3; michael@0: break; michael@0: case NS_MATHML_MATHVARIANT_SANS_SERIF_BOLD_ITALIC: michael@0: multiplier = 4; michael@0: break; michael@0: default: michael@0: // This mathvariant isn't defined for Greek or is otherwise normal michael@0: return aCh; michael@0: } michael@0: // See the kIsNumber case for an explanation of the following calculation michael@0: return baseChar + MATH_BOLD_UPPER_ALPHA + michael@0: multiplier*(MATH_ITALIC_UPPER_ALPHA - MATH_BOLD_UPPER_ALPHA); michael@0: } michael@0: michael@0: uint32_t tempChar; michael@0: uint32_t newChar; michael@0: if (varType == kIsArabic) { michael@0: const MathVarMapping* mapTable; michael@0: uint32_t tableLength; michael@0: switch (aMathVar) { michael@0: /* The Arabic mathematical block is not continuous, nor does it have a michael@0: * monotonic mapping to the unencoded characters, requiring the use of a michael@0: * lookup table. michael@0: */ michael@0: case NS_MATHML_MATHVARIANT_INITIAL: michael@0: mapTable = gArabicInitialMapTable; michael@0: tableLength = ArrayLength(gArabicInitialMapTable); michael@0: break; michael@0: case NS_MATHML_MATHVARIANT_TAILED: michael@0: mapTable = gArabicTailedMapTable; michael@0: tableLength = ArrayLength(gArabicTailedMapTable); michael@0: break; michael@0: case NS_MATHML_MATHVARIANT_STRETCHED: michael@0: mapTable = gArabicStretchedMapTable; michael@0: tableLength = ArrayLength(gArabicStretchedMapTable); michael@0: break; michael@0: case NS_MATHML_MATHVARIANT_LOOPED: michael@0: mapTable = gArabicLoopedMapTable; michael@0: tableLength = ArrayLength(gArabicLoopedMapTable); michael@0: break; michael@0: case NS_MATHML_MATHVARIANT_DOUBLE_STRUCK: michael@0: mapTable = gArabicDoubleMapTable; michael@0: tableLength = ArrayLength(gArabicDoubleMapTable); michael@0: break; michael@0: default: michael@0: // No valid transformations exist michael@0: return aCh; michael@0: } michael@0: newChar = MathvarMappingSearch(aCh, mapTable, tableLength); michael@0: } else { michael@0: // Must be Latin michael@0: if (aMathVar > NS_MATHML_MATHVARIANT_MONOSPACE) { michael@0: // Latin doesn't support the Arabic mathvariants michael@0: return aCh; michael@0: } michael@0: multiplier = aMathVar - 2; michael@0: // This is possible because the values for NS_MATHML_MATHVARIANT_* are michael@0: // chosen to coincide with the order in which the encoded mathvariant michael@0: // characters are located within their unicode block (less an offset to michael@0: // avoid _NONE and _NORMAL variants) michael@0: // See the kIsNumber case for an explanation of the following calculation michael@0: tempChar = baseChar + MATH_BOLD_UPPER_A + michael@0: multiplier*(MATH_ITALIC_UPPER_A - MATH_BOLD_UPPER_A); michael@0: // There are roughly twenty characters that are located outside of the michael@0: // mathematical block, so the spaces where they ought to be are used michael@0: // as keys for a lookup table containing the correct character mappings. michael@0: newChar = MathvarMappingSearch(tempChar, gLatinExceptionMapTable, michael@0: ArrayLength(gLatinExceptionMapTable)); michael@0: } michael@0: michael@0: if (newChar) { michael@0: return newChar; michael@0: } else if (varType == kIsLatin) { michael@0: return tempChar; michael@0: } else { michael@0: // An Arabic character without a corresponding mapping michael@0: return aCh; michael@0: } michael@0: michael@0: } michael@0: michael@0: void michael@0: MathMLTextRunFactory::RebuildTextRun(nsTransformedTextRun* aTextRun, michael@0: gfxContext* aRefContext) michael@0: { michael@0: gfxFontGroup* fontGroup = aTextRun->GetFontGroup(); michael@0: gfxFontStyle fontStyle = *fontGroup->GetStyle(); michael@0: michael@0: nsAutoString convertedString; michael@0: nsAutoTArray charsToMergeArray; michael@0: nsAutoTArray deletedCharsArray; michael@0: nsAutoTArray styleArray; michael@0: nsAutoTArray canBreakBeforeArray; michael@0: bool mergeNeeded = false; michael@0: michael@0: bool singleCharMI = michael@0: aTextRun->GetFlags() & nsTextFrameUtils::TEXT_IS_SINGLE_CHAR_MI; michael@0: michael@0: uint32_t length = aTextRun->GetLength(); michael@0: const char16_t* str = aTextRun->mString.BeginReading(); michael@0: nsRefPtr* styles = aTextRun->mStyles.Elements(); michael@0: michael@0: if (mSSTYScriptLevel && length) { michael@0: bool found = false; michael@0: // We respect ssty settings explicitly set by the user michael@0: for (uint32_t i = 0; i < fontStyle.featureSettings.Length(); i++) { michael@0: if (fontStyle.featureSettings[i].mTag == TRUETYPE_TAG('s','s','t','y')) { michael@0: found = true; michael@0: break; michael@0: } michael@0: } michael@0: if (!found) { michael@0: uint8_t sstyLevel = 0; michael@0: float scriptScaling = pow(styles[0]->StyleFont()->mScriptSizeMultiplier, michael@0: mSSTYScriptLevel); michael@0: static_assert(NS_MATHML_DEFAULT_SCRIPT_SIZE_MULTIPLIER < 1, michael@0: "Shouldn't it make things smaller?"); michael@0: /* michael@0: An SSTY level of 2 is set if the scaling factor is less than or equal michael@0: to halfway between that for a scriptlevel of 1 (0.71) and that of a michael@0: scriptlevel of 2 (0.71^2), assuming the default script size multiplier. michael@0: An SSTY level of 1 is set if the script scaling factor is less than michael@0: or equal that for a scriptlevel of 1 assuming the default script size michael@0: multiplier. michael@0: michael@0: User specified values of script size multiplier will change the scaling michael@0: factor which mSSTYScriptLevel values correspond to. michael@0: michael@0: In the event that the script size multiplier actually makes things michael@0: larger, no change is made. michael@0: michael@0: If the user doesn't want this to happen, all they need to do is set michael@0: style="-moz-font-feature-settings: 'ssty' 0" michael@0: */ michael@0: if (scriptScaling <= (NS_MATHML_DEFAULT_SCRIPT_SIZE_MULTIPLIER + michael@0: (NS_MATHML_DEFAULT_SCRIPT_SIZE_MULTIPLIER* michael@0: NS_MATHML_DEFAULT_SCRIPT_SIZE_MULTIPLIER))/2) { michael@0: // Currently only the first two ssty settings are used, so two is large michael@0: // as we go michael@0: sstyLevel = 2; michael@0: } else if (scriptScaling <= NS_MATHML_DEFAULT_SCRIPT_SIZE_MULTIPLIER) { michael@0: sstyLevel = 1; michael@0: } michael@0: if (sstyLevel) { michael@0: gfxFontFeature settingSSTY; michael@0: settingSSTY.mTag = TRUETYPE_TAG('s','s','t','y'); michael@0: settingSSTY.mValue = sstyLevel; michael@0: fontStyle.featureSettings.AppendElement(settingSSTY); michael@0: } michael@0: } michael@0: } michael@0: michael@0: uint8_t mathVar; michael@0: bool doMathvariantStyling = true; michael@0: michael@0: for (uint32_t i = 0; i < length; ++i) { michael@0: int extraChars = 0; michael@0: nsStyleContext* styleContext = styles[i]; michael@0: mathVar = styleContext->StyleFont()->mMathVariant; michael@0: michael@0: if (singleCharMI && mathVar == NS_MATHML_MATHVARIANT_NONE) { michael@0: mathVar = NS_MATHML_MATHVARIANT_ITALIC; michael@0: } michael@0: michael@0: uint32_t ch = str[i]; michael@0: if (NS_IS_HIGH_SURROGATE(ch) && i < length - 1 && michael@0: NS_IS_LOW_SURROGATE(str[i + 1])) { michael@0: ch = SURROGATE_TO_UCS4(ch, str[i + 1]); michael@0: } michael@0: uint32_t ch2 = MathVariant(ch, mathVar); michael@0: michael@0: if (mathVar == NS_MATHML_MATHVARIANT_BOLD || michael@0: mathVar == NS_MATHML_MATHVARIANT_BOLD_ITALIC || michael@0: mathVar == NS_MATHML_MATHVARIANT_ITALIC) { michael@0: if (ch == ch2 && ch != 0x20 && ch != 0xA0) { michael@0: // Don't apply the CSS style if a character cannot be michael@0: // transformed. There is an exception for whitespace as it is both michael@0: // common and innocuous. michael@0: doMathvariantStyling = false; michael@0: } michael@0: if (ch2 != ch) { michael@0: // Bug 930504. Some platforms do not have fonts for Mathematical michael@0: // Alphanumeric Symbols. Hence we check whether the transformed michael@0: // character is actually available. michael@0: uint8_t matchType; michael@0: nsRefPtr mathFont = fontGroup-> michael@0: FindFontForChar(ch2, 0, HB_SCRIPT_COMMON, nullptr, &matchType); michael@0: if (mathFont) { michael@0: // Don't apply the CSS style if there is a math font for at least one michael@0: // of the transformed character in this text run. michael@0: doMathvariantStyling = false; michael@0: } else { michael@0: // We fallback to the original character. michael@0: ch2 = ch; michael@0: } michael@0: } michael@0: } michael@0: michael@0: deletedCharsArray.AppendElement(false); michael@0: charsToMergeArray.AppendElement(false); michael@0: styleArray.AppendElement(styleContext); michael@0: canBreakBeforeArray.AppendElement(aTextRun->CanBreakLineBefore(i)); michael@0: michael@0: if (IS_IN_BMP(ch2)) { michael@0: convertedString.Append(ch2); michael@0: } else { michael@0: convertedString.Append(H_SURROGATE(ch2)); michael@0: convertedString.Append(L_SURROGATE(ch2)); michael@0: ++extraChars; michael@0: if (!IS_IN_BMP(ch)) { michael@0: deletedCharsArray.AppendElement(true); // not exactly deleted, but michael@0: // the trailing surrogate is skipped michael@0: ++i; michael@0: } michael@0: } michael@0: michael@0: while (extraChars-- > 0) { michael@0: mergeNeeded = true; michael@0: charsToMergeArray.AppendElement(true); michael@0: styleArray.AppendElement(styleContext); michael@0: canBreakBeforeArray.AppendElement(false); michael@0: } michael@0: } michael@0: michael@0: uint32_t flags; michael@0: gfxTextRunFactory::Parameters innerParams = michael@0: GetParametersForInner(aTextRun, &flags, aRefContext); michael@0: michael@0: nsAutoPtr transformedChild; michael@0: nsAutoPtr cachedChild; michael@0: gfxTextRun* child; michael@0: michael@0: if (mathVar == NS_MATHML_MATHVARIANT_BOLD && doMathvariantStyling) { michael@0: fontStyle.style = NS_FONT_STYLE_NORMAL; michael@0: fontStyle.weight = NS_FONT_WEIGHT_BOLD; michael@0: } else if (mathVar == NS_MATHML_MATHVARIANT_ITALIC && doMathvariantStyling) { michael@0: fontStyle.style = NS_FONT_STYLE_ITALIC; michael@0: fontStyle.weight = NS_FONT_WEIGHT_NORMAL; michael@0: } else if (mathVar == NS_MATHML_MATHVARIANT_BOLD_ITALIC && michael@0: doMathvariantStyling) { michael@0: fontStyle.style = NS_FONT_STYLE_ITALIC; michael@0: fontStyle.weight = NS_FONT_WEIGHT_BOLD; michael@0: } else if (mathVar != NS_MATHML_MATHVARIANT_NONE) { michael@0: // Mathvariant overrides fontstyle and fontweight michael@0: // Need to check to see if mathvariant is actually applied as this function michael@0: // is used for other purposes. michael@0: fontStyle.style = NS_FONT_STYLE_NORMAL; michael@0: fontStyle.weight = NS_FONT_WEIGHT_NORMAL; michael@0: } michael@0: nsRefPtr newFontGroup = fontGroup->Copy(&fontStyle); michael@0: michael@0: if (!newFontGroup) michael@0: return; michael@0: michael@0: if (mInnerTransformingTextRunFactory) { michael@0: transformedChild = mInnerTransformingTextRunFactory->MakeTextRun( michael@0: convertedString.BeginReading(), convertedString.Length(), michael@0: &innerParams, newFontGroup, flags, styleArray.Elements(), false); michael@0: child = transformedChild.get(); michael@0: } else { michael@0: cachedChild = newFontGroup->MakeTextRun( michael@0: convertedString.BeginReading(), convertedString.Length(), michael@0: &innerParams, flags); michael@0: child = cachedChild.get(); michael@0: } michael@0: if (!child) michael@0: return; michael@0: // Copy potential linebreaks into child so they're preserved michael@0: // (and also child will be shaped appropriately) michael@0: NS_ASSERTION(convertedString.Length() == canBreakBeforeArray.Length(), michael@0: "Dropped characters or break-before values somewhere!"); michael@0: child->SetPotentialLineBreaks(0, canBreakBeforeArray.Length(), michael@0: canBreakBeforeArray.Elements(), aRefContext); michael@0: if (transformedChild) { michael@0: transformedChild->FinishSettingProperties(aRefContext); michael@0: } michael@0: michael@0: if (mergeNeeded) { michael@0: // Now merge multiple characters into one multi-glyph character as required michael@0: NS_ASSERTION(charsToMergeArray.Length() == child->GetLength(), michael@0: "source length mismatch"); michael@0: NS_ASSERTION(deletedCharsArray.Length() == aTextRun->GetLength(), michael@0: "destination length mismatch"); michael@0: MergeCharactersInTextRun(aTextRun, child, charsToMergeArray.Elements(), michael@0: deletedCharsArray.Elements()); michael@0: } else { michael@0: // No merging to do, so just copy; this produces a more optimized textrun. michael@0: // We can't steal the data because the child may be cached and stealing michael@0: // the data would break the cache. michael@0: aTextRun->ResetGlyphRuns(); michael@0: aTextRun->CopyGlyphDataFrom(child, 0, child->GetLength(), 0); michael@0: } michael@0: }