|
|
|
|
|
|
1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
#include "gfxMathTable.h" |
| 6 |
|
| 7 |
#include "MathTableStructures.h" |
| 8 |
#include "harfbuzz/hb.h" |
| 9 |
#include <algorithm> |
| 10 |
|
| 11 |
using namespace mozilla; |
| 12 |
|
| 13 |
gfxMathTable::gfxMathTable(hb_blob_t* aMathTable) |
| 14 |
: mMathTable(aMathTable) |
| 15 |
, mGlyphConstruction(nullptr) |
| 16 |
, mGlyphID(-1) |
| 17 |
, mVertical(false) |
| 18 |
{ |
| 19 |
} |
| 20 |
|
| 21 |
gfxMathTable::~gfxMathTable() |
| 22 |
{ |
| 23 |
hb_blob_destroy(mMathTable); |
| 24 |
} |
| 25 |
|
| 26 |
bool |
| 27 |
gfxMathTable::HasValidHeaders() |
| 28 |
{ |
| 29 |
const char* mathData = hb_blob_get_data(mMathTable, nullptr); |
| 30 |
// Verify the MATH table header. |
| 31 |
if (!ValidStructure(mathData, sizeof(MATHTableHeader))) { |
| 32 |
return false; |
| 33 |
} |
| 34 |
const MATHTableHeader* header = GetMATHTableHeader(); |
| 35 |
if (uint32_t(header->mVersion) != 0x00010000 || |
| 36 |
!ValidOffset(mathData, uint16_t(header->mMathConstants)) || |
| 37 |
!ValidOffset(mathData, uint16_t(header->mMathGlyphInfo)) || |
| 38 |
!ValidOffset(mathData, uint16_t(header->mMathVariants))) { |
| 39 |
return false; |
| 40 |
} |
| 41 |
|
| 42 |
// Verify the MathConstants header. |
| 43 |
const MathConstants* mathconstants = GetMathConstants(); |
| 44 |
const char* start = reinterpret_cast<const char*>(mathconstants); |
| 45 |
if (!ValidStructure(start, sizeof(MathConstants))) { |
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
// Verify the MathGlyphInfo header. |
| 50 |
const MathGlyphInfo* mathglyphinfo = GetMathGlyphInfo(); |
| 51 |
start = reinterpret_cast<const char*>(mathglyphinfo); |
| 52 |
if (!ValidStructure(start, sizeof(MathGlyphInfo))) { |
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
// Verify the MathVariants header. |
| 57 |
const MathVariants* mathvariants = GetMathVariants(); |
| 58 |
start = reinterpret_cast<const char*>(mathvariants); |
| 59 |
if (!ValidStructure(start, sizeof(MathVariants)) || |
| 60 |
!ValidStructure(start, |
| 61 |
sizeof(MathVariants) + sizeof(Offset) * |
| 62 |
(uint16_t(mathvariants->mVertGlyphCount) + |
| 63 |
uint16_t(mathvariants->mHorizGlyphCount))) || |
| 64 |
!ValidOffset(start, uint16_t(mathvariants->mVertGlyphCoverage)) || |
| 65 |
!ValidOffset(start, uint16_t(mathvariants->mHorizGlyphCoverage))) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
return true; |
| 70 |
} |
| 71 |
|
| 72 |
int32_t |
| 73 |
gfxMathTable::GetMathConstant(gfxFontEntry::MathConstant aConstant) |
| 74 |
{ |
| 75 |
const MathConstants* mathconstants = GetMathConstants(); |
| 76 |
|
| 77 |
if (aConstant <= gfxFontEntry::ScriptScriptPercentScaleDown) { |
| 78 |
return int16_t(mathconstants->mInt16[aConstant]); |
| 79 |
} |
| 80 |
|
| 81 |
if (aConstant <= gfxFontEntry::DisplayOperatorMinHeight) { |
| 82 |
return uint16_t(mathconstants->mUint16[aConstant]); |
| 83 |
} |
| 84 |
|
| 85 |
if (aConstant <= gfxFontEntry::RadicalKernAfterDegree) { |
| 86 |
return int16_t(mathconstants-> |
| 87 |
mMathValues[aConstant - gfxFontEntry::MathLeading].mValue); |
| 88 |
} |
| 89 |
|
| 90 |
return uint16_t(mathconstants->mRadicalDegreeBottomRaisePercent); |
| 91 |
} |
| 92 |
|
| 93 |
bool |
| 94 |
gfxMathTable::GetMathItalicCorrection(uint32_t aGlyphID, |
| 95 |
int16_t* aItalicCorrection) |
| 96 |
{ |
| 97 |
const MathGlyphInfo* mathglyphinfo = GetMathGlyphInfo(); |
| 98 |
|
| 99 |
// Get the offset of the italic correction and verify whether it is valid. |
| 100 |
const char* start = reinterpret_cast<const char*>(mathglyphinfo); |
| 101 |
uint16_t offset = mathglyphinfo->mMathItalicsCorrectionInfo; |
| 102 |
if (offset == 0 || !ValidOffset(start, offset)) { |
| 103 |
return false; |
| 104 |
} |
| 105 |
start += offset; |
| 106 |
|
| 107 |
// Verify the validity of the MathItalicsCorrectionInfo and retrieve it. |
| 108 |
if (!ValidStructure(start, sizeof(MathItalicsCorrectionInfo))) { |
| 109 |
return false; |
| 110 |
} |
| 111 |
const MathItalicsCorrectionInfo* italicsCorrectionInfo = |
| 112 |
reinterpret_cast<const MathItalicsCorrectionInfo*>(start); |
| 113 |
|
| 114 |
// Get the coverage index for the glyph. |
| 115 |
offset = italicsCorrectionInfo->mCoverage; |
| 116 |
const Coverage* coverage = |
| 117 |
reinterpret_cast<const Coverage*>(start + offset); |
| 118 |
int32_t i = GetCoverageIndex(coverage, aGlyphID); |
| 119 |
|
| 120 |
// Get the ItalicsCorrection. |
| 121 |
uint16_t count = italicsCorrectionInfo->mItalicsCorrectionCount; |
| 122 |
if (i < 0 || i >= count) { |
| 123 |
return false; |
| 124 |
} |
| 125 |
start = reinterpret_cast<const char*>(italicsCorrectionInfo + 1); |
| 126 |
if (!ValidStructure(start, count * sizeof(MathValueRecord))) { |
| 127 |
return false; |
| 128 |
} |
| 129 |
const MathValueRecord* mathValueRecordArray = |
| 130 |
reinterpret_cast<const MathValueRecord*>(start); |
| 131 |
|
| 132 |
*aItalicCorrection = int16_t(mathValueRecordArray[i].mValue); |
| 133 |
return true; |
| 134 |
} |
| 135 |
|
| 136 |
uint32_t |
| 137 |
gfxMathTable::GetMathVariantsSize(uint32_t aGlyphID, bool aVertical, |
| 138 |
uint16_t aSize) |
| 139 |
{ |
| 140 |
// Select the glyph construction. |
| 141 |
SelectGlyphConstruction(aGlyphID, aVertical); |
| 142 |
if (!mGlyphConstruction) { |
| 143 |
return 0; |
| 144 |
} |
| 145 |
|
| 146 |
// Verify the validity of the array of the MathGlyphVariantRecord's and |
| 147 |
// whether there is a variant of the requested size. |
| 148 |
uint16_t count = mGlyphConstruction->mVariantCount; |
| 149 |
const char* start = reinterpret_cast<const char*>(mGlyphConstruction + 1); |
| 150 |
if (aSize >= count || |
| 151 |
!ValidStructure(start, count * sizeof(MathGlyphVariantRecord))) { |
| 152 |
return 0; |
| 153 |
} |
| 154 |
|
| 155 |
// Return the glyph index of the requested size variant. |
| 156 |
const MathGlyphVariantRecord* recordArray = |
| 157 |
reinterpret_cast<const MathGlyphVariantRecord*>(start); |
| 158 |
return uint32_t(recordArray[aSize].mVariantGlyph); |
| 159 |
} |
| 160 |
|
| 161 |
bool |
| 162 |
gfxMathTable::GetMathVariantsParts(uint32_t aGlyphID, bool aVertical, |
| 163 |
uint32_t aGlyphs[4]) |
| 164 |
{ |
| 165 |
// Get the glyph assembly corresponding to that (aGlyphID, aVertical) pair. |
| 166 |
const GlyphAssembly* glyphAssembly = GetGlyphAssembly(aGlyphID, aVertical); |
| 167 |
if (!glyphAssembly) { |
| 168 |
return false; |
| 169 |
} |
| 170 |
|
| 171 |
// Verify the validity of the array of GlyphPartRecord's and retrieve it. |
| 172 |
uint16_t count = glyphAssembly->mPartCount; |
| 173 |
const char* start = reinterpret_cast<const char*>(glyphAssembly + 1); |
| 174 |
if (!ValidStructure(start, count * sizeof(GlyphPartRecord))) { |
| 175 |
return false; |
| 176 |
} |
| 177 |
const GlyphPartRecord* recordArray = |
| 178 |
reinterpret_cast<const GlyphPartRecord*>(start); |
| 179 |
|
| 180 |
// XXXfredw The structure of the Open Type Math table is a bit more general |
| 181 |
// than the one currently used by the nsMathMLChar code, so we try to fallback |
| 182 |
// in reasonable way. We use the approach of the copyComponents function in |
| 183 |
// github.com/mathjax/MathJax-dev/blob/master/fonts/OpenTypeMath/fontUtil.py |
| 184 |
// |
| 185 |
// The nsMathMLChar code can use at most 3 non extender pieces (aGlyphs[0], |
| 186 |
// aGlyphs[1] and aGlyphs[2]) and the extenders between these pieces should |
| 187 |
// all be the same (aGlyphs[4]). Also, the parts of vertical assembly are |
| 188 |
// stored from bottom to top in the Open Type MATH table while they are |
| 189 |
// stored from top to bottom in nsMathMLChar. |
| 190 |
|
| 191 |
// Count the number of non extender pieces |
| 192 |
uint16_t nonExtenderCount = 0; |
| 193 |
for (uint16_t i = 0; i < count; i++) { |
| 194 |
if (!(uint16_t(recordArray[i].mPartFlags) & PART_FLAG_EXTENDER)) { |
| 195 |
nonExtenderCount++; |
| 196 |
} |
| 197 |
} |
| 198 |
if (nonExtenderCount > 3) { |
| 199 |
// Not supported: too many pieces |
| 200 |
return false; |
| 201 |
} |
| 202 |
|
| 203 |
// Now browse the list of pieces |
| 204 |
|
| 205 |
// 0 = look for a left/bottom glyph |
| 206 |
// 1 = look for an extender between left/bottom and mid |
| 207 |
// 2 = look for a middle glyph |
| 208 |
// 3 = look for an extender between middle and right/top |
| 209 |
// 4 = look for a right/top glyph |
| 210 |
// 5 = no more piece expected |
| 211 |
uint8_t state = 0; |
| 212 |
|
| 213 |
// First extender char found. |
| 214 |
uint32_t extenderChar = 0; |
| 215 |
|
| 216 |
// Clear the aGlyphs table. |
| 217 |
memset(aGlyphs, 0, sizeof(uint32_t) * 4); |
| 218 |
|
| 219 |
for (uint16_t i = 0; i < count; i++) { |
| 220 |
|
| 221 |
bool isExtender = uint16_t(recordArray[i].mPartFlags) & PART_FLAG_EXTENDER; |
| 222 |
uint32_t glyph = recordArray[i].mGlyph; |
| 223 |
|
| 224 |
if ((state == 1 || state == 2) && nonExtenderCount < 3) { |
| 225 |
// do not try to find a middle glyph |
| 226 |
state += 2; |
| 227 |
} |
| 228 |
|
| 229 |
if (isExtender) { |
| 230 |
if (!extenderChar) { |
| 231 |
extenderChar = glyph; |
| 232 |
aGlyphs[3] = extenderChar; |
| 233 |
} else if (extenderChar != glyph) { |
| 234 |
// Not supported: different extenders |
| 235 |
return false; |
| 236 |
} |
| 237 |
|
| 238 |
if (state == 0) { // or state == 1 |
| 239 |
// ignore left/bottom piece and multiple successive extenders |
| 240 |
state = 1; |
| 241 |
} else if (state == 2) { // or state == 3 |
| 242 |
// ignore middle piece and multiple successive extenders |
| 243 |
state = 3; |
| 244 |
} else if (state >= 4) { |
| 245 |
// Not supported: unexpected extender |
| 246 |
return false; |
| 247 |
} |
| 248 |
|
| 249 |
continue; |
| 250 |
} |
| 251 |
|
| 252 |
if (state == 0) { |
| 253 |
// copy left/bottom part |
| 254 |
aGlyphs[mVertical ? 2 : 0] = glyph; |
| 255 |
state = 1; |
| 256 |
continue; |
| 257 |
} |
| 258 |
|
| 259 |
if (state == 1 || state == 2) { |
| 260 |
// copy middle part |
| 261 |
aGlyphs[1] = glyph; |
| 262 |
state = 3; |
| 263 |
continue; |
| 264 |
} |
| 265 |
|
| 266 |
if (state == 3 || state == 4) { |
| 267 |
// copy right/top part |
| 268 |
aGlyphs[mVertical ? 0 : 2] = glyph; |
| 269 |
state = 5; |
| 270 |
} |
| 271 |
|
| 272 |
} |
| 273 |
|
| 274 |
return true; |
| 275 |
} |
| 276 |
|
| 277 |
bool |
| 278 |
gfxMathTable::ValidStructure(const char* aStart, uint16_t aSize) |
| 279 |
{ |
| 280 |
unsigned int mathDataLength; |
| 281 |
const char* mathData = hb_blob_get_data(mMathTable, &mathDataLength); |
| 282 |
return (mathData <= aStart && |
| 283 |
aStart + aSize <= mathData + mathDataLength); |
| 284 |
} |
| 285 |
|
| 286 |
bool |
| 287 |
gfxMathTable::ValidOffset(const char* aStart, uint16_t aOffset) |
| 288 |
{ |
| 289 |
unsigned int mathDataLength; |
| 290 |
const char* mathData = hb_blob_get_data(mMathTable, &mathDataLength); |
| 291 |
return (mathData <= aStart + aOffset && |
| 292 |
aStart + aOffset < mathData + mathDataLength); |
| 293 |
} |
| 294 |
|
| 295 |
const MATHTableHeader* |
| 296 |
gfxMathTable::GetMATHTableHeader() |
| 297 |
{ |
| 298 |
const char* mathData = hb_blob_get_data(mMathTable, nullptr); |
| 299 |
return reinterpret_cast<const MATHTableHeader*>(mathData); |
| 300 |
} |
| 301 |
|
| 302 |
const MathConstants* |
| 303 |
gfxMathTable::GetMathConstants() |
| 304 |
{ |
| 305 |
const char* mathData = hb_blob_get_data(mMathTable, nullptr); |
| 306 |
return |
| 307 |
reinterpret_cast<const MathConstants*>(mathData + |
| 308 |
uint16_t(GetMATHTableHeader()-> |
| 309 |
mMathConstants)); |
| 310 |
} |
| 311 |
|
| 312 |
const MathGlyphInfo* |
| 313 |
gfxMathTable::GetMathGlyphInfo() |
| 314 |
{ |
| 315 |
const char* mathData = hb_blob_get_data(mMathTable, nullptr); |
| 316 |
return |
| 317 |
reinterpret_cast<const MathGlyphInfo*>(mathData + |
| 318 |
uint16_t(GetMATHTableHeader()-> |
| 319 |
mMathGlyphInfo)); |
| 320 |
} |
| 321 |
|
| 322 |
const MathVariants* |
| 323 |
gfxMathTable::GetMathVariants() |
| 324 |
{ |
| 325 |
const char* mathData = hb_blob_get_data(mMathTable, nullptr); |
| 326 |
return |
| 327 |
reinterpret_cast<const MathVariants*>(mathData + |
| 328 |
uint16_t(GetMATHTableHeader()-> |
| 329 |
mMathVariants)); |
| 330 |
} |
| 331 |
|
| 332 |
const GlyphAssembly* |
| 333 |
gfxMathTable::GetGlyphAssembly(uint32_t aGlyphID, bool aVertical) |
| 334 |
{ |
| 335 |
// Select the glyph construction. |
| 336 |
SelectGlyphConstruction(aGlyphID, aVertical); |
| 337 |
if (!mGlyphConstruction) { |
| 338 |
return nullptr; |
| 339 |
} |
| 340 |
|
| 341 |
// Get the offset of the glyph assembly and verify whether it is valid. |
| 342 |
const char* start = reinterpret_cast<const char*>(mGlyphConstruction); |
| 343 |
uint16_t offset = mGlyphConstruction->mGlyphAssembly; |
| 344 |
if (offset == 0 || !ValidOffset(start, offset)) { |
| 345 |
return nullptr; |
| 346 |
} |
| 347 |
start += offset; |
| 348 |
|
| 349 |
// Verify the validity of the GlyphAssembly and return it. |
| 350 |
if (!ValidStructure(start, sizeof(GlyphAssembly))) { |
| 351 |
return nullptr; |
| 352 |
} |
| 353 |
return reinterpret_cast<const GlyphAssembly*>(start); |
| 354 |
} |
| 355 |
|
| 356 |
int32_t |
| 357 |
gfxMathTable::GetCoverageIndex(const Coverage* aCoverage, uint32_t aGlyph) |
| 358 |
{ |
| 359 |
if (uint16_t(aCoverage->mFormat) == 1) { |
| 360 |
// Coverage Format 1: list of individual glyph indices in the glyph set. |
| 361 |
const CoverageFormat1* table = |
| 362 |
reinterpret_cast<const CoverageFormat1*>(aCoverage); |
| 363 |
uint16_t count = table->mGlyphCount; |
| 364 |
const char* start = reinterpret_cast<const char*>(table + 1); |
| 365 |
if (ValidStructure(start, count * sizeof(GlyphID))) { |
| 366 |
const GlyphID* glyphArray = |
| 367 |
reinterpret_cast<const GlyphID*>(start); |
| 368 |
uint32_t imin = 0, imax = count; |
| 369 |
while (imin < imax) { |
| 370 |
uint32_t imid = (imin + imax) >> 1; |
| 371 |
uint16_t glyphMid = glyphArray[imid]; |
| 372 |
if (glyphMid == aGlyph) { |
| 373 |
return imid; |
| 374 |
} |
| 375 |
if (glyphMid < aGlyph) { |
| 376 |
imin = imid + 1; |
| 377 |
} else { |
| 378 |
imax = imid; |
| 379 |
} |
| 380 |
} |
| 381 |
} |
| 382 |
} else if (uint16_t(aCoverage->mFormat) == 2) { |
| 383 |
// Coverage Format 2: ranges of consecutive indices. |
| 384 |
const CoverageFormat2* table = |
| 385 |
reinterpret_cast<const CoverageFormat2*>(aCoverage); |
| 386 |
uint16_t count = table->mRangeCount; |
| 387 |
const char* start = reinterpret_cast<const char*>(table + 1); |
| 388 |
if (ValidStructure(start, count * sizeof(RangeRecord))) { |
| 389 |
const RangeRecord* rangeArray = |
| 390 |
reinterpret_cast<const RangeRecord*>(start); |
| 391 |
uint32_t imin = 0, imax = count; |
| 392 |
while (imin < imax) { |
| 393 |
uint32_t imid = (imin + imax) >> 1; |
| 394 |
uint16_t rStart = rangeArray[imid].mStart; |
| 395 |
uint16_t rEnd = rangeArray[imid].mEnd; |
| 396 |
if (rEnd < aGlyph) { |
| 397 |
imin = imid + 1; |
| 398 |
} else if (aGlyph < rStart) { |
| 399 |
imax = imid; |
| 400 |
} else { |
| 401 |
return (uint16_t(rangeArray[imid].mStartCoverageIndex) + |
| 402 |
aGlyph - rStart); |
| 403 |
} |
| 404 |
} |
| 405 |
} |
| 406 |
} |
| 407 |
return -1; |
| 408 |
} |
| 409 |
|
| 410 |
void |
| 411 |
gfxMathTable::SelectGlyphConstruction(uint32_t aGlyphID, bool aVertical) |
| 412 |
{ |
| 413 |
if (mGlyphID == aGlyphID && mVertical == aVertical) { |
| 414 |
// The (glyph, direction) pair is already selected: nothing to do. |
| 415 |
return; |
| 416 |
} |
| 417 |
|
| 418 |
// Update our cached values. |
| 419 |
mVertical = aVertical; |
| 420 |
mGlyphID = aGlyphID; |
| 421 |
mGlyphConstruction = nullptr; |
| 422 |
|
| 423 |
// Get the coverage index for the new values. |
| 424 |
const MathVariants* mathvariants = GetMathVariants(); |
| 425 |
const char* start = reinterpret_cast<const char*>(mathvariants); |
| 426 |
uint16_t offset = (aVertical ? |
| 427 |
mathvariants->mVertGlyphCoverage : |
| 428 |
mathvariants->mHorizGlyphCoverage); |
| 429 |
const Coverage* coverage = |
| 430 |
reinterpret_cast<const Coverage*>(start + offset); |
| 431 |
int32_t i = GetCoverageIndex(coverage, aGlyphID); |
| 432 |
|
| 433 |
// Get the offset to the glyph construction. |
| 434 |
uint16_t count = (aVertical ? |
| 435 |
mathvariants->mVertGlyphCount : |
| 436 |
mathvariants->mHorizGlyphCount); |
| 437 |
start = reinterpret_cast<const char*>(mathvariants + 1); |
| 438 |
if (i < 0 || i >= count) { |
| 439 |
return; |
| 440 |
} |
| 441 |
if (!aVertical) { |
| 442 |
start += uint16_t(mathvariants->mVertGlyphCount) * sizeof(Offset); |
| 443 |
} |
| 444 |
if (!ValidStructure(start, count * sizeof(Offset))) { |
| 445 |
return; |
| 446 |
} |
| 447 |
const Offset* offsetArray = reinterpret_cast<const Offset*>(start); |
| 448 |
offset = uint16_t(offsetArray[i]); |
| 449 |
|
| 450 |
// Make mGlyphConstruction point to the desired glyph construction. |
| 451 |
start = reinterpret_cast<const char*>(mathvariants); |
| 452 |
if (!ValidStructure(start + offset, sizeof(MathGlyphConstruction))) { |
| 453 |
return; |
| 454 |
} |
| 455 |
mGlyphConstruction = |
| 456 |
reinterpret_cast<const MathGlyphConstruction*>(start + offset); |
| 457 |
} |