Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "gfxTypes.h" |
michael@0 | 7 | |
michael@0 | 8 | #include "gfxContext.h" |
michael@0 | 9 | #include "gfxUniscribeShaper.h" |
michael@0 | 10 | #include "gfxWindowsPlatform.h" |
michael@0 | 11 | |
michael@0 | 12 | #include "gfxFontTest.h" |
michael@0 | 13 | |
michael@0 | 14 | #include "cairo.h" |
michael@0 | 15 | #include "cairo-win32.h" |
michael@0 | 16 | |
michael@0 | 17 | #include <windows.h> |
michael@0 | 18 | |
michael@0 | 19 | #include "nsTArray.h" |
michael@0 | 20 | |
michael@0 | 21 | #include "prinit.h" |
michael@0 | 22 | |
michael@0 | 23 | /********************************************************************** |
michael@0 | 24 | * |
michael@0 | 25 | * class gfxUniscribeShaper |
michael@0 | 26 | * |
michael@0 | 27 | **********************************************************************/ |
michael@0 | 28 | |
michael@0 | 29 | #define ESTIMATE_MAX_GLYPHS(L) (((3 * (L)) >> 1) + 16) |
michael@0 | 30 | |
michael@0 | 31 | class UniscribeItem |
michael@0 | 32 | { |
michael@0 | 33 | public: |
michael@0 | 34 | UniscribeItem(gfxContext *aContext, HDC aDC, |
michael@0 | 35 | gfxUniscribeShaper *aShaper, |
michael@0 | 36 | const char16_t *aString, uint32_t aLength, |
michael@0 | 37 | SCRIPT_ITEM *aItem, uint32_t aIVS) : |
michael@0 | 38 | mContext(aContext), mDC(aDC), |
michael@0 | 39 | mShaper(aShaper), |
michael@0 | 40 | mItemString(aString), mItemLength(aLength), |
michael@0 | 41 | mAlternativeString(nullptr), mScriptItem(aItem), |
michael@0 | 42 | mScript(aItem->a.eScript), |
michael@0 | 43 | mNumGlyphs(0), mMaxGlyphs(ESTIMATE_MAX_GLYPHS(aLength)), |
michael@0 | 44 | mFontSelected(false), mIVS(aIVS) |
michael@0 | 45 | { |
michael@0 | 46 | // See bug 394751 for details. |
michael@0 | 47 | NS_ASSERTION(mMaxGlyphs < 65535, |
michael@0 | 48 | "UniscribeItem is too big, ScriptShape() will fail!"); |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | ~UniscribeItem() { |
michael@0 | 52 | free(mAlternativeString); |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | bool AllocateBuffers() { |
michael@0 | 56 | return (mGlyphs.SetLength(mMaxGlyphs) && |
michael@0 | 57 | mClusters.SetLength(mItemLength + 1) && |
michael@0 | 58 | mAttr.SetLength(mMaxGlyphs)); |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | /* possible return values: |
michael@0 | 62 | * S_OK - things succeeded |
michael@0 | 63 | * GDI_ERROR - things failed to shape. Might want to try again after calling DisableShaping() |
michael@0 | 64 | */ |
michael@0 | 65 | |
michael@0 | 66 | HRESULT Shape() { |
michael@0 | 67 | HRESULT rv; |
michael@0 | 68 | HDC shapeDC = nullptr; |
michael@0 | 69 | |
michael@0 | 70 | char16ptr_t str = mAlternativeString ? mAlternativeString : mItemString; |
michael@0 | 71 | |
michael@0 | 72 | mScriptItem->a.fLogicalOrder = true; |
michael@0 | 73 | SCRIPT_ANALYSIS sa = mScriptItem->a; |
michael@0 | 74 | |
michael@0 | 75 | while (true) { |
michael@0 | 76 | |
michael@0 | 77 | rv = ScriptShape(shapeDC, mShaper->ScriptCache(), |
michael@0 | 78 | str, mItemLength, |
michael@0 | 79 | mMaxGlyphs, &sa, |
michael@0 | 80 | mGlyphs.Elements(), mClusters.Elements(), |
michael@0 | 81 | mAttr.Elements(), &mNumGlyphs); |
michael@0 | 82 | |
michael@0 | 83 | if (rv == E_OUTOFMEMORY) { |
michael@0 | 84 | mMaxGlyphs *= 2; |
michael@0 | 85 | if (!mGlyphs.SetLength(mMaxGlyphs) || |
michael@0 | 86 | !mAttr.SetLength(mMaxGlyphs)) { |
michael@0 | 87 | return E_OUTOFMEMORY; |
michael@0 | 88 | } |
michael@0 | 89 | continue; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | // Uniscribe can't do shaping with some fonts, so it sets the |
michael@0 | 93 | // fNoGlyphIndex flag in the SCRIPT_ANALYSIS structure to indicate |
michael@0 | 94 | // this. This occurs with CFF fonts loaded with |
michael@0 | 95 | // AddFontMemResourceEx but it's not clear what the other cases |
michael@0 | 96 | // are. We return an error so our caller can try fallback shaping. |
michael@0 | 97 | // see http://msdn.microsoft.com/en-us/library/ms776520(VS.85).aspx |
michael@0 | 98 | |
michael@0 | 99 | if (sa.fNoGlyphIndex) { |
michael@0 | 100 | return GDI_ERROR; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | if (rv == E_PENDING) { |
michael@0 | 104 | if (shapeDC == mDC) { |
michael@0 | 105 | // we already tried this once, something failed, give up |
michael@0 | 106 | return E_PENDING; |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | SelectFont(); |
michael@0 | 110 | |
michael@0 | 111 | shapeDC = mDC; |
michael@0 | 112 | continue; |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | // http://msdn.microsoft.com/en-us/library/dd368564(VS.85).aspx: |
michael@0 | 116 | // Uniscribe will return this if "the font corresponding to the |
michael@0 | 117 | // DC does not support the script required by the run...". |
michael@0 | 118 | // In this case, we'll set the script code to SCRIPT_UNDEFINED |
michael@0 | 119 | // and try again, so that we'll at least get glyphs even though |
michael@0 | 120 | // they won't necessarily have proper shaping. |
michael@0 | 121 | // (We probably shouldn't have selected this font at all, |
michael@0 | 122 | // but it's too late to fix that here.) |
michael@0 | 123 | if (rv == USP_E_SCRIPT_NOT_IN_FONT) { |
michael@0 | 124 | sa.eScript = SCRIPT_UNDEFINED; |
michael@0 | 125 | NS_WARNING("Uniscribe says font does not support script needed"); |
michael@0 | 126 | continue; |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | // Prior to Windows 7, Uniscribe didn't support Ideographic Variation |
michael@0 | 130 | // Selectors. Replace the UVS glyph manually. |
michael@0 | 131 | if (mIVS) { |
michael@0 | 132 | uint32_t lastChar = str[mItemLength - 1]; |
michael@0 | 133 | if (NS_IS_LOW_SURROGATE(lastChar) |
michael@0 | 134 | && NS_IS_HIGH_SURROGATE(str[mItemLength - 2])) { |
michael@0 | 135 | lastChar = SURROGATE_TO_UCS4(str[mItemLength - 2], lastChar); |
michael@0 | 136 | } |
michael@0 | 137 | uint16_t glyphId = mShaper->GetFont()->GetUVSGlyph(lastChar, mIVS); |
michael@0 | 138 | if (glyphId) { |
michael@0 | 139 | mGlyphs[mNumGlyphs - 1] = glyphId; |
michael@0 | 140 | } |
michael@0 | 141 | } |
michael@0 | 142 | |
michael@0 | 143 | return rv; |
michael@0 | 144 | } |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | bool ShapingEnabled() { |
michael@0 | 148 | return (mScriptItem->a.eScript != SCRIPT_UNDEFINED); |
michael@0 | 149 | } |
michael@0 | 150 | void DisableShaping() { |
michael@0 | 151 | mScriptItem->a.eScript = SCRIPT_UNDEFINED; |
michael@0 | 152 | // Note: If we disable the shaping by using SCRIPT_UNDEFINED and |
michael@0 | 153 | // the string has the surrogate pair, ScriptShape API is |
michael@0 | 154 | // *sometimes* crashed. Therefore, we should replace the surrogate |
michael@0 | 155 | // pair to U+FFFD. See bug 341500. |
michael@0 | 156 | GenerateAlternativeString(); |
michael@0 | 157 | } |
michael@0 | 158 | void EnableShaping() { |
michael@0 | 159 | mScriptItem->a.eScript = mScript; |
michael@0 | 160 | if (mAlternativeString) { |
michael@0 | 161 | free(mAlternativeString); |
michael@0 | 162 | mAlternativeString = nullptr; |
michael@0 | 163 | } |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | bool IsGlyphMissing(SCRIPT_FONTPROPERTIES *aSFP, uint32_t aGlyphIndex) { |
michael@0 | 167 | return (mGlyphs[aGlyphIndex] == aSFP->wgDefault); |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | |
michael@0 | 171 | HRESULT Place() { |
michael@0 | 172 | HRESULT rv; |
michael@0 | 173 | HDC placeDC = nullptr; |
michael@0 | 174 | |
michael@0 | 175 | if (!mOffsets.SetLength(mNumGlyphs) || |
michael@0 | 176 | !mAdvances.SetLength(mNumGlyphs)) { |
michael@0 | 177 | return E_OUTOFMEMORY; |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | SCRIPT_ANALYSIS sa = mScriptItem->a; |
michael@0 | 181 | |
michael@0 | 182 | while (true) { |
michael@0 | 183 | rv = ScriptPlace(placeDC, mShaper->ScriptCache(), |
michael@0 | 184 | mGlyphs.Elements(), mNumGlyphs, |
michael@0 | 185 | mAttr.Elements(), &sa, |
michael@0 | 186 | mAdvances.Elements(), mOffsets.Elements(), nullptr); |
michael@0 | 187 | |
michael@0 | 188 | if (rv == E_PENDING) { |
michael@0 | 189 | SelectFont(); |
michael@0 | 190 | placeDC = mDC; |
michael@0 | 191 | continue; |
michael@0 | 192 | } |
michael@0 | 193 | |
michael@0 | 194 | if (rv == USP_E_SCRIPT_NOT_IN_FONT) { |
michael@0 | 195 | sa.eScript = SCRIPT_UNDEFINED; |
michael@0 | 196 | continue; |
michael@0 | 197 | } |
michael@0 | 198 | |
michael@0 | 199 | break; |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | return rv; |
michael@0 | 203 | } |
michael@0 | 204 | |
michael@0 | 205 | void ScriptFontProperties(SCRIPT_FONTPROPERTIES *sfp) { |
michael@0 | 206 | HRESULT rv; |
michael@0 | 207 | |
michael@0 | 208 | memset(sfp, 0, sizeof(SCRIPT_FONTPROPERTIES)); |
michael@0 | 209 | sfp->cBytes = sizeof(SCRIPT_FONTPROPERTIES); |
michael@0 | 210 | rv = ScriptGetFontProperties(nullptr, mShaper->ScriptCache(), |
michael@0 | 211 | sfp); |
michael@0 | 212 | if (rv == E_PENDING) { |
michael@0 | 213 | SelectFont(); |
michael@0 | 214 | rv = ScriptGetFontProperties(mDC, mShaper->ScriptCache(), |
michael@0 | 215 | sfp); |
michael@0 | 216 | } |
michael@0 | 217 | } |
michael@0 | 218 | |
michael@0 | 219 | void SaveGlyphs(gfxShapedText *aShapedText, uint32_t aOffset) { |
michael@0 | 220 | uint32_t offsetInRun = mScriptItem->iCharPos; |
michael@0 | 221 | |
michael@0 | 222 | // XXX We should store this in the item and only fetch it once |
michael@0 | 223 | SCRIPT_FONTPROPERTIES sfp; |
michael@0 | 224 | ScriptFontProperties(&sfp); |
michael@0 | 225 | |
michael@0 | 226 | uint32_t offset = 0; |
michael@0 | 227 | nsAutoTArray<gfxShapedText::DetailedGlyph,1> detailedGlyphs; |
michael@0 | 228 | gfxShapedText::CompressedGlyph g; |
michael@0 | 229 | gfxShapedText::CompressedGlyph *charGlyphs = |
michael@0 | 230 | aShapedText->GetCharacterGlyphs(); |
michael@0 | 231 | const uint32_t appUnitsPerDevUnit = aShapedText->GetAppUnitsPerDevUnit(); |
michael@0 | 232 | while (offset < mItemLength) { |
michael@0 | 233 | uint32_t runOffset = aOffset + offsetInRun + offset; |
michael@0 | 234 | bool atClusterStart = charGlyphs[runOffset].IsClusterStart(); |
michael@0 | 235 | if (offset > 0 && mClusters[offset] == mClusters[offset - 1]) { |
michael@0 | 236 | gfxShapedText::CompressedGlyph &g = charGlyphs[runOffset]; |
michael@0 | 237 | NS_ASSERTION(!g.IsSimpleGlyph(), "overwriting a simple glyph"); |
michael@0 | 238 | g.SetComplex(atClusterStart, false, 0); |
michael@0 | 239 | } else { |
michael@0 | 240 | // Count glyphs for this character |
michael@0 | 241 | uint32_t k = mClusters[offset]; |
michael@0 | 242 | uint32_t glyphCount = mNumGlyphs - k; |
michael@0 | 243 | uint32_t nextClusterOffset; |
michael@0 | 244 | bool missing = IsGlyphMissing(&sfp, k); |
michael@0 | 245 | for (nextClusterOffset = offset + 1; nextClusterOffset < mItemLength; ++nextClusterOffset) { |
michael@0 | 246 | if (mClusters[nextClusterOffset] > k) { |
michael@0 | 247 | glyphCount = mClusters[nextClusterOffset] - k; |
michael@0 | 248 | break; |
michael@0 | 249 | } |
michael@0 | 250 | } |
michael@0 | 251 | uint32_t j; |
michael@0 | 252 | for (j = 1; j < glyphCount; ++j) { |
michael@0 | 253 | if (IsGlyphMissing(&sfp, k + j)) { |
michael@0 | 254 | missing = true; |
michael@0 | 255 | } |
michael@0 | 256 | } |
michael@0 | 257 | int32_t advance = mAdvances[k]*appUnitsPerDevUnit; |
michael@0 | 258 | WORD glyph = mGlyphs[k]; |
michael@0 | 259 | NS_ASSERTION(!gfxFontGroup::IsInvalidChar(mItemString[offset]), |
michael@0 | 260 | "invalid character detected"); |
michael@0 | 261 | if (missing) { |
michael@0 | 262 | if (NS_IS_HIGH_SURROGATE(mItemString[offset]) && |
michael@0 | 263 | offset + 1 < mItemLength && |
michael@0 | 264 | NS_IS_LOW_SURROGATE(mItemString[offset + 1])) { |
michael@0 | 265 | aShapedText->SetMissingGlyph(runOffset, |
michael@0 | 266 | SURROGATE_TO_UCS4(mItemString[offset], |
michael@0 | 267 | mItemString[offset + 1]), |
michael@0 | 268 | mShaper->GetFont()); |
michael@0 | 269 | } else { |
michael@0 | 270 | aShapedText->SetMissingGlyph(runOffset, mItemString[offset], |
michael@0 | 271 | mShaper->GetFont()); |
michael@0 | 272 | } |
michael@0 | 273 | } else if (glyphCount == 1 && advance >= 0 && |
michael@0 | 274 | mOffsets[k].dv == 0 && mOffsets[k].du == 0 && |
michael@0 | 275 | gfxShapedText::CompressedGlyph::IsSimpleAdvance(advance) && |
michael@0 | 276 | gfxShapedText::CompressedGlyph::IsSimpleGlyphID(glyph) && |
michael@0 | 277 | atClusterStart) |
michael@0 | 278 | { |
michael@0 | 279 | charGlyphs[runOffset].SetSimpleGlyph(advance, glyph); |
michael@0 | 280 | } else { |
michael@0 | 281 | if (detailedGlyphs.Length() < glyphCount) { |
michael@0 | 282 | if (!detailedGlyphs.AppendElements(glyphCount - detailedGlyphs.Length())) |
michael@0 | 283 | return; |
michael@0 | 284 | } |
michael@0 | 285 | uint32_t i; |
michael@0 | 286 | for (i = 0; i < glyphCount; ++i) { |
michael@0 | 287 | gfxTextRun::DetailedGlyph *details = &detailedGlyphs[i]; |
michael@0 | 288 | details->mGlyphID = mGlyphs[k + i]; |
michael@0 | 289 | details->mAdvance = mAdvances[k + i] * appUnitsPerDevUnit; |
michael@0 | 290 | details->mXOffset = float(mOffsets[k + i].du) * appUnitsPerDevUnit * |
michael@0 | 291 | aShapedText->GetDirection(); |
michael@0 | 292 | details->mYOffset = - float(mOffsets[k + i].dv) * appUnitsPerDevUnit; |
michael@0 | 293 | } |
michael@0 | 294 | aShapedText->SetGlyphs(runOffset, |
michael@0 | 295 | g.SetComplex(atClusterStart, true, |
michael@0 | 296 | glyphCount), |
michael@0 | 297 | detailedGlyphs.Elements()); |
michael@0 | 298 | } |
michael@0 | 299 | } |
michael@0 | 300 | ++offset; |
michael@0 | 301 | } |
michael@0 | 302 | } |
michael@0 | 303 | |
michael@0 | 304 | void SelectFont() { |
michael@0 | 305 | if (mFontSelected) |
michael@0 | 306 | return; |
michael@0 | 307 | |
michael@0 | 308 | cairo_t *cr = mContext->GetCairo(); |
michael@0 | 309 | |
michael@0 | 310 | cairo_set_font_face(cr, mShaper->GetFont()->CairoFontFace()); |
michael@0 | 311 | cairo_set_font_size(cr, mShaper->GetFont()->GetAdjustedSize()); |
michael@0 | 312 | cairo_scaled_font_t *scaledFont = mShaper->GetFont()->CairoScaledFont(); |
michael@0 | 313 | cairo_win32_scaled_font_select_font(scaledFont, mDC); |
michael@0 | 314 | |
michael@0 | 315 | mFontSelected = true; |
michael@0 | 316 | } |
michael@0 | 317 | |
michael@0 | 318 | private: |
michael@0 | 319 | |
michael@0 | 320 | void GenerateAlternativeString() { |
michael@0 | 321 | if (mAlternativeString) |
michael@0 | 322 | free(mAlternativeString); |
michael@0 | 323 | mAlternativeString = (char16_t *)malloc(mItemLength * sizeof(char16_t)); |
michael@0 | 324 | if (!mAlternativeString) |
michael@0 | 325 | return; |
michael@0 | 326 | memcpy((void *)mAlternativeString, (const void *)mItemString, |
michael@0 | 327 | mItemLength * sizeof(char16_t)); |
michael@0 | 328 | for (uint32_t i = 0; i < mItemLength; i++) { |
michael@0 | 329 | if (NS_IS_HIGH_SURROGATE(mItemString[i]) || NS_IS_LOW_SURROGATE(mItemString[i])) |
michael@0 | 330 | mAlternativeString[i] = char16_t(0xFFFD); |
michael@0 | 331 | } |
michael@0 | 332 | } |
michael@0 | 333 | |
michael@0 | 334 | private: |
michael@0 | 335 | nsRefPtr<gfxContext> mContext; |
michael@0 | 336 | HDC mDC; |
michael@0 | 337 | gfxUniscribeShaper *mShaper; |
michael@0 | 338 | |
michael@0 | 339 | SCRIPT_ITEM *mScriptItem; |
michael@0 | 340 | WORD mScript; |
michael@0 | 341 | |
michael@0 | 342 | public: |
michael@0 | 343 | // these point to the full string/length of the item |
michael@0 | 344 | const char16_t *mItemString; |
michael@0 | 345 | const uint32_t mItemLength; |
michael@0 | 346 | |
michael@0 | 347 | private: |
michael@0 | 348 | char16_t *mAlternativeString; |
michael@0 | 349 | |
michael@0 | 350 | #define AVERAGE_ITEM_LENGTH 40 |
michael@0 | 351 | |
michael@0 | 352 | AutoFallibleTArray<WORD, uint32_t(ESTIMATE_MAX_GLYPHS(AVERAGE_ITEM_LENGTH))> mGlyphs; |
michael@0 | 353 | AutoFallibleTArray<WORD, AVERAGE_ITEM_LENGTH + 1> mClusters; |
michael@0 | 354 | AutoFallibleTArray<SCRIPT_VISATTR, uint32_t(ESTIMATE_MAX_GLYPHS(AVERAGE_ITEM_LENGTH))> mAttr; |
michael@0 | 355 | |
michael@0 | 356 | AutoFallibleTArray<GOFFSET, 2 * AVERAGE_ITEM_LENGTH> mOffsets; |
michael@0 | 357 | AutoFallibleTArray<int, 2 * AVERAGE_ITEM_LENGTH> mAdvances; |
michael@0 | 358 | |
michael@0 | 359 | #undef AVERAGE_ITEM_LENGTH |
michael@0 | 360 | |
michael@0 | 361 | int mMaxGlyphs; |
michael@0 | 362 | int mNumGlyphs; |
michael@0 | 363 | uint32_t mIVS; |
michael@0 | 364 | |
michael@0 | 365 | bool mFontSelected; |
michael@0 | 366 | }; |
michael@0 | 367 | |
michael@0 | 368 | class Uniscribe |
michael@0 | 369 | { |
michael@0 | 370 | public: |
michael@0 | 371 | Uniscribe(const char16_t *aString, |
michael@0 | 372 | gfxShapedText *aShapedText, |
michael@0 | 373 | uint32_t aOffset, uint32_t aLength): |
michael@0 | 374 | mString(aString), mShapedText(aShapedText), |
michael@0 | 375 | mOffset(aOffset), mLength(aLength) |
michael@0 | 376 | { |
michael@0 | 377 | } |
michael@0 | 378 | |
michael@0 | 379 | void Init() { |
michael@0 | 380 | memset(&mControl, 0, sizeof(SCRIPT_CONTROL)); |
michael@0 | 381 | memset(&mState, 0, sizeof(SCRIPT_STATE)); |
michael@0 | 382 | // Lock the direction. Don't allow the itemizer to change directions |
michael@0 | 383 | // based on character type. |
michael@0 | 384 | mState.uBidiLevel = mShapedText->IsRightToLeft() ? 1 : 0; |
michael@0 | 385 | mState.fOverrideDirection = true; |
michael@0 | 386 | } |
michael@0 | 387 | |
michael@0 | 388 | public: |
michael@0 | 389 | int Itemize() { |
michael@0 | 390 | HRESULT rv; |
michael@0 | 391 | |
michael@0 | 392 | int maxItems = 5; |
michael@0 | 393 | |
michael@0 | 394 | Init(); |
michael@0 | 395 | |
michael@0 | 396 | // Allocate space for one more item than expected, to handle a rare |
michael@0 | 397 | // overflow in ScriptItemize (pre XP SP2). See bug 366643. |
michael@0 | 398 | if (!mItems.SetLength(maxItems + 1)) { |
michael@0 | 399 | return 0; |
michael@0 | 400 | } |
michael@0 | 401 | while ((rv = ScriptItemize(mString, mLength, |
michael@0 | 402 | maxItems, &mControl, &mState, |
michael@0 | 403 | mItems.Elements(), &mNumItems)) == E_OUTOFMEMORY) { |
michael@0 | 404 | maxItems *= 2; |
michael@0 | 405 | if (!mItems.SetLength(maxItems + 1)) { |
michael@0 | 406 | return 0; |
michael@0 | 407 | } |
michael@0 | 408 | Init(); |
michael@0 | 409 | } |
michael@0 | 410 | |
michael@0 | 411 | return mNumItems; |
michael@0 | 412 | } |
michael@0 | 413 | |
michael@0 | 414 | SCRIPT_ITEM *ScriptItem(uint32_t i) { |
michael@0 | 415 | NS_ASSERTION(i <= (uint32_t)mNumItems, "Trying to get out of bounds item"); |
michael@0 | 416 | return &mItems[i]; |
michael@0 | 417 | } |
michael@0 | 418 | |
michael@0 | 419 | private: |
michael@0 | 420 | char16ptr_t mString; |
michael@0 | 421 | gfxShapedText *mShapedText; |
michael@0 | 422 | uint32_t mOffset; |
michael@0 | 423 | uint32_t mLength; |
michael@0 | 424 | |
michael@0 | 425 | SCRIPT_CONTROL mControl; |
michael@0 | 426 | SCRIPT_STATE mState; |
michael@0 | 427 | FallibleTArray<SCRIPT_ITEM> mItems; |
michael@0 | 428 | int mNumItems; |
michael@0 | 429 | }; |
michael@0 | 430 | |
michael@0 | 431 | |
michael@0 | 432 | bool |
michael@0 | 433 | gfxUniscribeShaper::ShapeText(gfxContext *aContext, |
michael@0 | 434 | const char16_t *aText, |
michael@0 | 435 | uint32_t aOffset, |
michael@0 | 436 | uint32_t aLength, |
michael@0 | 437 | int32_t aScript, |
michael@0 | 438 | gfxShapedText *aShapedText) |
michael@0 | 439 | { |
michael@0 | 440 | DCFromContext aDC(aContext); |
michael@0 | 441 | |
michael@0 | 442 | bool result = true; |
michael@0 | 443 | HRESULT rv; |
michael@0 | 444 | |
michael@0 | 445 | Uniscribe us(aText, aShapedText, aOffset, aLength); |
michael@0 | 446 | |
michael@0 | 447 | /* itemize the string */ |
michael@0 | 448 | int numItems = us.Itemize(); |
michael@0 | 449 | |
michael@0 | 450 | uint32_t length = aLength; |
michael@0 | 451 | SaveDC(aDC); |
michael@0 | 452 | uint32_t ivs = 0; |
michael@0 | 453 | for (int i = 0; i < numItems; ++i) { |
michael@0 | 454 | int iCharPos = us.ScriptItem(i)->iCharPos; |
michael@0 | 455 | int iCharPosNext = us.ScriptItem(i+1)->iCharPos; |
michael@0 | 456 | |
michael@0 | 457 | if (ivs) { |
michael@0 | 458 | iCharPos += 2; |
michael@0 | 459 | if (iCharPos >= iCharPosNext) { |
michael@0 | 460 | ivs = 0; |
michael@0 | 461 | continue; |
michael@0 | 462 | } |
michael@0 | 463 | } |
michael@0 | 464 | |
michael@0 | 465 | if (i+1 < numItems && iCharPosNext <= length - 2 |
michael@0 | 466 | && aText[iCharPosNext] == H_SURROGATE(kUnicodeVS17) |
michael@0 | 467 | && uint32_t(aText[iCharPosNext + 1]) - L_SURROGATE(kUnicodeVS17) |
michael@0 | 468 | <= L_SURROGATE(kUnicodeVS256) - L_SURROGATE(kUnicodeVS17)) { |
michael@0 | 469 | |
michael@0 | 470 | ivs = SURROGATE_TO_UCS4(aText[iCharPosNext], |
michael@0 | 471 | aText[iCharPosNext + 1]); |
michael@0 | 472 | } else { |
michael@0 | 473 | ivs = 0; |
michael@0 | 474 | } |
michael@0 | 475 | |
michael@0 | 476 | UniscribeItem item(aContext, aDC, this, |
michael@0 | 477 | aText + iCharPos, |
michael@0 | 478 | iCharPosNext - iCharPos, |
michael@0 | 479 | us.ScriptItem(i), ivs); |
michael@0 | 480 | if (!item.AllocateBuffers()) { |
michael@0 | 481 | result = false; |
michael@0 | 482 | break; |
michael@0 | 483 | } |
michael@0 | 484 | |
michael@0 | 485 | if (!item.ShapingEnabled()) { |
michael@0 | 486 | item.EnableShaping(); |
michael@0 | 487 | } |
michael@0 | 488 | |
michael@0 | 489 | rv = item.Shape(); |
michael@0 | 490 | if (FAILED(rv)) { |
michael@0 | 491 | // we know we have the glyphs to display this font already |
michael@0 | 492 | // so Uniscribe just doesn't know how to shape the script. |
michael@0 | 493 | // Render the glyphs without shaping. |
michael@0 | 494 | item.DisableShaping(); |
michael@0 | 495 | rv = item.Shape(); |
michael@0 | 496 | } |
michael@0 | 497 | #ifdef DEBUG |
michael@0 | 498 | if (FAILED(rv)) { |
michael@0 | 499 | NS_WARNING("Uniscribe failed to shape with font"); |
michael@0 | 500 | } |
michael@0 | 501 | #endif |
michael@0 | 502 | |
michael@0 | 503 | if (SUCCEEDED(rv)) { |
michael@0 | 504 | rv = item.Place(); |
michael@0 | 505 | #ifdef DEBUG |
michael@0 | 506 | if (FAILED(rv)) { |
michael@0 | 507 | // crap fonts may fail when placing (e.g. funky free fonts) |
michael@0 | 508 | NS_WARNING("Uniscribe failed to place with font"); |
michael@0 | 509 | } |
michael@0 | 510 | #endif |
michael@0 | 511 | } |
michael@0 | 512 | |
michael@0 | 513 | if (FAILED(rv)) { |
michael@0 | 514 | // Uniscribe doesn't like this font for some reason. |
michael@0 | 515 | // Returning FALSE will make the gfxGDIFont retry with the |
michael@0 | 516 | // "dumb" GDI shaper, unless useUniscribeOnly was set. |
michael@0 | 517 | result = false; |
michael@0 | 518 | break; |
michael@0 | 519 | } |
michael@0 | 520 | |
michael@0 | 521 | item.SaveGlyphs(aShapedText, aOffset); |
michael@0 | 522 | } |
michael@0 | 523 | |
michael@0 | 524 | RestoreDC(aDC, -1); |
michael@0 | 525 | |
michael@0 | 526 | return result; |
michael@0 | 527 | } |