gfx/thebes/gfxDWriteShaper.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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 "gfxDWriteShaper.h"
michael@0 7 #include "gfxWindowsPlatform.h"
michael@0 8
michael@0 9 #include <dwrite.h>
michael@0 10
michael@0 11 #include "gfxDWriteTextAnalysis.h"
michael@0 12
michael@0 13 #include "nsCRT.h"
michael@0 14
michael@0 15 bool
michael@0 16 gfxDWriteShaper::ShapeText(gfxContext *aContext,
michael@0 17 const char16_t *aText,
michael@0 18 uint32_t aOffset,
michael@0 19 uint32_t aLength,
michael@0 20 int32_t aScript,
michael@0 21 gfxShapedText *aShapedText)
michael@0 22 {
michael@0 23 HRESULT hr;
michael@0 24 // TODO: Handle TEXT_DISABLE_OPTIONAL_LIGATURES
michael@0 25
michael@0 26 DWRITE_READING_DIRECTION readingDirection =
michael@0 27 aShapedText->IsRightToLeft()
michael@0 28 ? DWRITE_READING_DIRECTION_RIGHT_TO_LEFT
michael@0 29 : DWRITE_READING_DIRECTION_LEFT_TO_RIGHT;
michael@0 30
michael@0 31 gfxDWriteFont *font = static_cast<gfxDWriteFont*>(mFont);
michael@0 32
michael@0 33 gfxShapedText::CompressedGlyph g;
michael@0 34
michael@0 35 IDWriteTextAnalyzer *analyzer =
michael@0 36 gfxWindowsPlatform::GetPlatform()->GetDWriteAnalyzer();
michael@0 37 if (!analyzer) {
michael@0 38 return false;
michael@0 39 }
michael@0 40
michael@0 41 /**
michael@0 42 * There's an internal 16-bit limit on some things inside the analyzer,
michael@0 43 * but we never attempt to shape a word longer than 32K characters
michael@0 44 * in a single call, so we cannot exceed that limit.
michael@0 45 */
michael@0 46 UINT32 length = aLength;
michael@0 47 char16ptr_t text = aText;
michael@0 48
michael@0 49 TextAnalysis analysis(text, length, nullptr, readingDirection);
michael@0 50 TextAnalysis::Run *runHead;
michael@0 51 hr = analysis.GenerateResults(analyzer, &runHead);
michael@0 52
michael@0 53 if (FAILED(hr)) {
michael@0 54 NS_WARNING("Analyzer failed to generate results.");
michael@0 55 return false;
michael@0 56 }
michael@0 57
michael@0 58 int32_t appUnitsPerDevPixel = aShapedText->GetAppUnitsPerDevUnit();
michael@0 59
michael@0 60 UINT32 maxGlyphs = 0;
michael@0 61 trymoreglyphs:
michael@0 62 if ((UINT32_MAX - 3 * length / 2 + 16) < maxGlyphs) {
michael@0 63 // This isn't going to work, we're going to cross the UINT32 upper
michael@0 64 // limit. Give up.
michael@0 65 NS_WARNING("Shaper needs to generate more than 2^32 glyphs?!");
michael@0 66 return false;
michael@0 67 }
michael@0 68 maxGlyphs += 3 * length / 2 + 16;
michael@0 69
michael@0 70 AutoFallibleTArray<UINT16, 400> clusters;
michael@0 71 AutoFallibleTArray<UINT16, 400> indices;
michael@0 72 AutoFallibleTArray<DWRITE_SHAPING_TEXT_PROPERTIES, 400> textProperties;
michael@0 73 AutoFallibleTArray<DWRITE_SHAPING_GLYPH_PROPERTIES, 400> glyphProperties;
michael@0 74 if (!clusters.SetLength(length) ||
michael@0 75 !indices.SetLength(maxGlyphs) ||
michael@0 76 !textProperties.SetLength(maxGlyphs) ||
michael@0 77 !glyphProperties.SetLength(maxGlyphs)) {
michael@0 78 NS_WARNING("Shaper failed to allocate memory.");
michael@0 79 return false;
michael@0 80 }
michael@0 81
michael@0 82 UINT32 actualGlyphs;
michael@0 83
michael@0 84 hr = analyzer->GetGlyphs(text, length,
michael@0 85 font->GetFontFace(), FALSE,
michael@0 86 readingDirection == DWRITE_READING_DIRECTION_RIGHT_TO_LEFT,
michael@0 87 &runHead->mScript, nullptr, nullptr, nullptr, nullptr, 0,
michael@0 88 maxGlyphs, clusters.Elements(), textProperties.Elements(),
michael@0 89 indices.Elements(), glyphProperties.Elements(), &actualGlyphs);
michael@0 90
michael@0 91 if (hr == HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER)) {
michael@0 92 // We increase the amount of glyphs and try again.
michael@0 93 goto trymoreglyphs;
michael@0 94 }
michael@0 95 if (FAILED(hr)) {
michael@0 96 NS_WARNING("Analyzer failed to get glyphs.");
michael@0 97 return false;
michael@0 98 }
michael@0 99
michael@0 100 WORD gID = indices[0];
michael@0 101 AutoFallibleTArray<FLOAT, 400> advances;
michael@0 102 AutoFallibleTArray<DWRITE_GLYPH_OFFSET, 400> glyphOffsets;
michael@0 103 if (!advances.SetLength(actualGlyphs) ||
michael@0 104 !glyphOffsets.SetLength(actualGlyphs)) {
michael@0 105 NS_WARNING("Shaper failed to allocate memory.");
michael@0 106 return false;
michael@0 107 }
michael@0 108
michael@0 109 if (!static_cast<gfxDWriteFont*>(mFont)->mUseSubpixelPositions) {
michael@0 110 hr = analyzer->GetGdiCompatibleGlyphPlacements(
michael@0 111 text,
michael@0 112 clusters.Elements(),
michael@0 113 textProperties.Elements(),
michael@0 114 length,
michael@0 115 indices.Elements(),
michael@0 116 glyphProperties.Elements(),
michael@0 117 actualGlyphs,
michael@0 118 font->GetFontFace(),
michael@0 119 font->GetAdjustedSize(),
michael@0 120 1.0,
michael@0 121 nullptr,
michael@0 122 FALSE,
michael@0 123 FALSE,
michael@0 124 FALSE,
michael@0 125 &runHead->mScript,
michael@0 126 nullptr,
michael@0 127 nullptr,
michael@0 128 nullptr,
michael@0 129 0,
michael@0 130 advances.Elements(),
michael@0 131 glyphOffsets.Elements());
michael@0 132 } else {
michael@0 133 hr = analyzer->GetGlyphPlacements(text,
michael@0 134 clusters.Elements(),
michael@0 135 textProperties.Elements(),
michael@0 136 length,
michael@0 137 indices.Elements(),
michael@0 138 glyphProperties.Elements(),
michael@0 139 actualGlyphs,
michael@0 140 font->GetFontFace(),
michael@0 141 font->GetAdjustedSize(),
michael@0 142 FALSE,
michael@0 143 FALSE,
michael@0 144 &runHead->mScript,
michael@0 145 nullptr,
michael@0 146 nullptr,
michael@0 147 nullptr,
michael@0 148 0,
michael@0 149 advances.Elements(),
michael@0 150 glyphOffsets.Elements());
michael@0 151 }
michael@0 152 if (FAILED(hr)) {
michael@0 153 NS_WARNING("Analyzer failed to get glyph placements.");
michael@0 154 return false;
michael@0 155 }
michael@0 156
michael@0 157 nsAutoTArray<gfxShapedText::DetailedGlyph,1> detailedGlyphs;
michael@0 158 gfxShapedText::CompressedGlyph *charGlyphs =
michael@0 159 aShapedText->GetCharacterGlyphs();
michael@0 160
michael@0 161 for (unsigned int c = 0; c < length; c++) {
michael@0 162 uint32_t k = clusters[c];
michael@0 163 uint32_t absC = aOffset + c;
michael@0 164
michael@0 165 if (c > 0 && k == clusters[c - 1]) {
michael@0 166 // This is a cluster continuation. No glyph here.
michael@0 167 gfxShapedText::CompressedGlyph &g = charGlyphs[absC];
michael@0 168 NS_ASSERTION(!g.IsSimpleGlyph(), "overwriting a simple glyph");
michael@0 169 g.SetComplex(g.IsClusterStart(), false, 0);
michael@0 170 continue;
michael@0 171 }
michael@0 172
michael@0 173 // Count glyphs for this character
michael@0 174 uint32_t glyphCount = actualGlyphs - k;
michael@0 175 uint32_t nextClusterOffset;
michael@0 176 for (nextClusterOffset = c + 1;
michael@0 177 nextClusterOffset < length; ++nextClusterOffset) {
michael@0 178 if (clusters[nextClusterOffset] > k) {
michael@0 179 glyphCount = clusters[nextClusterOffset] - k;
michael@0 180 break;
michael@0 181 }
michael@0 182 }
michael@0 183 int32_t advance = (int32_t)(advances[k] * appUnitsPerDevPixel);
michael@0 184 if (glyphCount == 1 && advance >= 0 &&
michael@0 185 glyphOffsets[k].advanceOffset == 0 &&
michael@0 186 glyphOffsets[k].ascenderOffset == 0 &&
michael@0 187 charGlyphs[absC].IsClusterStart() &&
michael@0 188 gfxShapedText::CompressedGlyph::IsSimpleAdvance(advance) &&
michael@0 189 gfxShapedText::CompressedGlyph::IsSimpleGlyphID(indices[k])) {
michael@0 190 charGlyphs[absC].SetSimpleGlyph(advance, indices[k]);
michael@0 191 } else {
michael@0 192 if (detailedGlyphs.Length() < glyphCount) {
michael@0 193 if (!detailedGlyphs.AppendElements(
michael@0 194 glyphCount - detailedGlyphs.Length())) {
michael@0 195 continue;
michael@0 196 }
michael@0 197 }
michael@0 198 float totalAdvance = 0;
michael@0 199 for (unsigned int z = 0; z < glyphCount; z++) {
michael@0 200 detailedGlyphs[z].mGlyphID = indices[k + z];
michael@0 201 detailedGlyphs[z].mAdvance =
michael@0 202 (int32_t)(advances[k + z]
michael@0 203 * appUnitsPerDevPixel);
michael@0 204 if (readingDirection ==
michael@0 205 DWRITE_READING_DIRECTION_RIGHT_TO_LEFT) {
michael@0 206 detailedGlyphs[z].mXOffset =
michael@0 207 (totalAdvance +
michael@0 208 glyphOffsets[k + z].advanceOffset)
michael@0 209 * appUnitsPerDevPixel;
michael@0 210 } else {
michael@0 211 detailedGlyphs[z].mXOffset =
michael@0 212 glyphOffsets[k + z].advanceOffset *
michael@0 213 appUnitsPerDevPixel;
michael@0 214 }
michael@0 215 detailedGlyphs[z].mYOffset =
michael@0 216 -glyphOffsets[k + z].ascenderOffset *
michael@0 217 appUnitsPerDevPixel;
michael@0 218 totalAdvance += advances[k + z];
michael@0 219 }
michael@0 220 aShapedText->SetGlyphs(
michael@0 221 absC,
michael@0 222 g.SetComplex(charGlyphs[absC].IsClusterStart(),
michael@0 223 true,
michael@0 224 glyphCount),
michael@0 225 detailedGlyphs.Elements());
michael@0 226 }
michael@0 227 }
michael@0 228
michael@0 229 return true;
michael@0 230 }

mercurial