aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/SkGlyphRun.cpp
blob: 583d676940efd35b89779e12b1b8f3aa9fe61375 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
 * Copyright 2018 The Android Open Source Project
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkGlyphRun.h"

#include <algorithm>
#include <new>
#include <tuple>

#include "SkDevice.h"
#include "SkDrawFilter.h"
#include "SkGlyphCache.h"
#include "SkMSAN.h"
#include "SkMakeUnique.h"
#include "SkPaint.h"
#include "SkPaintPriv.h"
#include "SkStrikeCache.h"
#include "SkTextBlob.h"
#include "SkTextBlobRunIterator.h"
#include "SkTo.h"
#include "SkUtils.h"

namespace {
static SkTypeface::Encoding convert_encoding(SkPaint::TextEncoding encoding) {
    switch (encoding) {
        case  SkPaint::kUTF8_TextEncoding: return SkTypeface::kUTF8_Encoding;
        case SkPaint::kUTF16_TextEncoding: return SkTypeface::kUTF16_Encoding;
        case SkPaint::kUTF32_TextEncoding: return SkTypeface::kUTF32_Encoding;
        default: return SkTypeface::kUTF32_Encoding;
    }
}
}  // namespace

// -- SkGlyphSet ----------------------------------------------------------------------------------
uint32_t SkGlyphSet::uniqueSize() {
    // The size is how big the vector is grown since being passed into reuse.
    return fUniqueGlyphIDs->size() - fStartOfUniqueIDs;
}

uint16_t SkGlyphSet::add(SkGlyphID glyphID) {
    static constexpr SkGlyphID  kUndefGlyph{0};

    if (glyphID >= fUniverseSize) {
        glyphID = kUndefGlyph;
    }

    if (glyphID >= fIndices.size()) {
        fIndices.resize(glyphID + 1);
    }

    auto index = fIndices[glyphID];

    // Remember we start at the end of what ever was passed in.
    if (index < this->uniqueSize() && (*fUniqueGlyphIDs)[fStartOfUniqueIDs + index] == glyphID) {
        return index;
    }

    uint16_t newIndex = SkTo<uint16_t>(this->uniqueSize());
    fUniqueGlyphIDs->push_back(glyphID);
    fIndices[glyphID] = newIndex;
    return newIndex;
}

void SkGlyphSet::reuse(uint32_t glyphUniverseSize, std::vector<SkGlyphID>* uniqueGlyphIDs) {
    SkASSERT(glyphUniverseSize <= (1 << 16));
    fUniverseSize = glyphUniverseSize;
    fUniqueGlyphIDs = uniqueGlyphIDs;

    // Capture the vector end to act as the start of a new unique id vector.
    fStartOfUniqueIDs = uniqueGlyphIDs->size();

    // If we're hanging onto these arrays for a long time, we don't want their size to drift
    // endlessly upwards. It's unusual to see a typeface with more than 4096 possible glyphs.
    if (glyphUniverseSize < 4096 && fIndices.size() > 4096) {
        fIndices.resize(4096);
        fIndices.shrink_to_fit();
    }

    // No need to clear fIndices here... SkGlyphSet's set insertion algorithm is designed to work
    // correctly even when the fIndexes buffer is uninitialized!
}

// -- SkGlyphRun -----------------------------------------------------------------------------------
SkGlyphRun::SkGlyphRun(const SkIndexedRunInfo& runInfo,
                       size_t denseOffset, size_t denseSize,
                       size_t uniqueOffset, uint16_t uniqueSize,
                       SkSpan<SkGlyphID>  scratchGlyphs,
                       SkSpan<const char> text,
                       SkSpan<const uint32_t> clusters,
                       SkPaint&&          runPaint)
        : fRunInfo{runInfo}
        , fDenseOffset{denseOffset}, fDenseSize{denseSize}
        , fUniqueOffset{uniqueOffset}, fUniqueSize{uniqueSize}
        , fTemporaryShuntGlyphIDs{scratchGlyphs}
        , fText{text}
        , fClusters{clusters}
        , fRunPaint{std::move(runPaint)} {}

void SkGlyphRun::temporaryShuntToDrawPosText(SkBaseDevice* device, SkPoint origin) {

    auto pos = (const SkScalar*) this->positions().data();

    device->drawPosText(
            fTemporaryShuntGlyphIDs.data(), fDenseSize * sizeof(SkGlyphID),
            pos, 2, origin, fRunPaint);
}

void SkGlyphRun::temporaryShuntToCallback(TemporaryShuntCallback callback) {
    auto bytes = (const char *)fTemporaryShuntGlyphIDs.data();
    auto pos = (const SkScalar*) this->positions().data();
    callback(this->runSize(), bytes, pos);
}

// -- SkGlyphRunList -------------------------------------------------------------------------------
SkGlyphRunList::SkGlyphRunList(
        SkSpan<SkGlyphRun> glyphRuns, SkPoint origin, const SkTextBlob* textBlob)
        : fGlyphRuns{glyphRuns}
        , fOrigin{origin}
        , fTemporaryTextBlobShunt{textBlob} { }

uint64_t SkGlyphRunList::uniqueID() const {
    return fTemporaryTextBlobShunt != nullptr ? fTemporaryTextBlobShunt->uniqueID()
                                              : SK_InvalidUniqueID;
}

bool SkGlyphRunList::anyRunsLCD() const {
    for (const auto& r : fGlyphRuns) {
        if (r.paint().isLCDRenderText()) {
            return true;
        }
    }
    return false;
}

void SkGlyphRunList::temporaryShuntBlobnotifyAddedToCache(uint32_t cacheID) const {
    SkASSERT(fTemporaryTextBlobShunt != nullptr);
    fTemporaryTextBlobShunt->notifyAddedToCache(cacheID);
}

// -- SkGlyphRunListIterator -----------------------------------------------------------------------
constexpr SkPoint SkGlyphRunListIterator::fZero;

// -- SkGlyphRunBuilder ----------------------------------------------------------------------------
void SkGlyphRunBuilder::prepareDrawText(
        const SkPaint& paint, const void* bytes, size_t byteLength, SkPoint origin) {
    this->initialize();
    this->drawText(
            paint, bytes, byteLength, origin, SkSpan<const char>(), SkSpan<const uint32_t>());
}

void SkGlyphRunBuilder::prepareDrawPosTextH(const SkPaint& paint, const void* bytes,
                                            size_t byteLength, const SkScalar* xpos,
                                            SkScalar constY) {
    this->initialize();
    this->drawPosTextH(
            paint, bytes, byteLength, xpos, constY,
            SkSpan<const char>(), SkSpan<const uint32_t>());
}

void SkGlyphRunBuilder::prepareDrawPosText(const SkPaint& paint, const void* bytes,
                                           size_t byteLength, const SkPoint* pos) {
    this->initialize();
    this->drawPosText(paint, bytes, byteLength, pos,
            SkSpan<const char>(), SkSpan<const uint32_t>());
}

void SkGlyphRunBuilder::prepareTextBlob(
        const SkPaint& paint, const SkTextBlob& blob, SkPoint origin, SkDrawFilter* drawFilter) {
    this->initialize();
    fTemporaryTextBlobShunt = &blob;
    fOrigin = origin;

    SkPaint runPaint = paint;

    for (SkTextBlobRunIterator it(&blob); !it.done(); it.next()) {
        // applyFontToPaint() always overwrites the exact same attributes,
        // so it is safe to not re-seed the paint for this reason.
        it.applyFontToPaint(&runPaint);

        if (drawFilter != nullptr && !drawFilter->filter(&runPaint, SkDrawFilter::kText_Type)) {
            // A false return from filter() means we should abort the current draw.
            runPaint = paint;
            continue;
        }

        // These better be glyphs
        SkASSERT(runPaint.getTextEncoding() == SkPaint::kGlyphID_TextEncoding);

        auto text = SkSpan<const char>(it.text(), it.textSize());
        auto clusters = SkSpan<const uint32_t>(it.clusters(), it.glyphCount());
        size_t glyphLen = it.glyphCount() * sizeof(SkGlyphID);
        const SkPoint& offset = it.offset();

        switch (it.positioning()) {
            case SkTextBlob::kDefault_Positioning: {
                this->drawText(runPaint, it.glyphs(), glyphLen, offset, text, clusters);
            }
            break;
            case SkTextBlob::kHorizontal_Positioning: {
                auto constY = offset.y();
                this->drawPosTextH(
                        runPaint, it.glyphs(), glyphLen, it.pos(), constY, text, clusters);
            }
            break;
            case SkTextBlob::kFull_Positioning:
                this->drawPosText(
                        runPaint, it.glyphs(), glyphLen, (const SkPoint*)it.pos(), text,
                        clusters);
            break;
            default:
                SK_ABORT("unhandled positioning mode");
        }

        if (drawFilter != nullptr) {
            // A draw filter may change the paint arbitrarily, so we must re-seed in this case.
            runPaint = paint;
        }
    }
}

SkGlyphRun* SkGlyphRunBuilder::useGlyphRun() {
    auto glyphRunList = this->useGlyphRunList();
    SkASSERT(glyphRunList->size() == 1);
    return &(*glyphRunList)[0];
}

SkGlyphRunList* SkGlyphRunBuilder::useGlyphRunList() {
    new ((void*)&fScratchGlyphRunList)
        SkGlyphRunList{SkSpan<SkGlyphRun>(fGlyphRuns), fOrigin, fTemporaryTextBlobShunt};
    return &fScratchGlyphRunList;
}

void SkGlyphRunBuilder::draw(SkBaseDevice* device) {
    auto glyphRunList = this->useGlyphRunList();
    device->drawGlyphRunList(glyphRunList);
}

size_t SkGlyphRunBuilder::runSize() const { return fDenseIndex.size() - fLastDenseIndex; }

size_t SkGlyphRunBuilder::uniqueSize() const { return fUniqueGlyphIDs.size() - fLastUniqueIndex; }

void SkGlyphRunBuilder::initialize() {
    fTemporaryTextBlobShunt = nullptr;
    fDenseIndex.clear();
    fPositions.clear();
    fUniqueGlyphIDs.clear();
    fGlyphRuns.clear();
    fLastDenseIndex = 0;
    fLastUniqueIndex = 0;
    fOrigin = {0, 0};
}

SkGlyphID* SkGlyphRunBuilder::addDenseAndUnique(
        const SkPaint& paint, const void* bytes, size_t byteLength) {

    size_t runSize = 0;
    SkGlyphID* glyphIDs = nullptr;
    auto encoding = paint.getTextEncoding();
    auto typeface = SkPaintPriv::GetTypefaceOrDefault(paint);
    if (encoding != SkPaint::kGlyphID_TextEncoding) {
        auto tfEncoding = convert_encoding(encoding);
        int utfSize = SkUTFN_CountUnichars(tfEncoding, bytes, byteLength);
        if (utfSize > 0) {
            runSize = SkTo<size_t>(utfSize);
            fScratchGlyphIDs.resize(runSize);
            typeface->charsToGlyphs(bytes, tfEncoding, fScratchGlyphIDs.data(), runSize);
            glyphIDs = fScratchGlyphIDs.data();
        }
    } else {
        runSize = byteLength / 2;
        glyphIDs = (SkGlyphID*)bytes;
    }

    SkASSERT(glyphIDs != nullptr);

    if (runSize > 0) {
        fGlyphSet.reuse(typeface->countGlyphs(), &fUniqueGlyphIDs);
        for (size_t i = 0; i < runSize; i++) {
            fDenseIndex.push_back(fGlyphSet.add(glyphIDs[i]));
        }
    }

    return glyphIDs;
}

void SkGlyphRunBuilder::addGlyphRunToList(
        const SkPaint& runPaint,
        SkGlyphID* temporaryShuntGlyphIDs,
        SkSpan<const char> text,
        SkSpan<const uint32_t> clusters) {

    // Ignore empty runs.
    if (fDenseIndex.size() != fLastDenseIndex) {
        SkPaint modifiedRunPaint{runPaint};

        // TODO: remove these once the text stack has all the encoding and align code removed.
        modifiedRunPaint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
        modifiedRunPaint.setTextAlign(SkPaint::kLeft_Align);

        auto runSize = this->runSize();
        auto uniqueSize = this->uniqueSize();

        fGlyphRuns.emplace_back(
                fIndexed,
                fLastDenseIndex, runSize,
                fLastUniqueIndex, SkTo<uint16_t>(uniqueSize),
                SkSpan<SkGlyphID>(temporaryShuntGlyphIDs, runSize),
                text,
                clusters,
                std::move(modifiedRunPaint));

        fLastDenseIndex = fDenseIndex.size();
        fLastUniqueIndex = fUniqueGlyphIDs.size();
    }
}

void SkGlyphRunBuilder::drawText(
        const SkPaint& paint, const void* bytes, size_t byteLength, SkPoint origin,
        SkSpan<const char> text, SkSpan<const uint32_t> clusters) {

    SkGlyphID* temporaryShuntGlyphIDs = this->addDenseAndUnique(paint, bytes, byteLength);

    fScratchAdvances.resize(this->uniqueSize());
    {
        auto cache = SkStrikeCache::FindOrCreateStrikeExclusive(paint);
        cache->getAdvances(
                fIndexed.uniqueGlyphIDs(fLastUniqueIndex, uniqueSize()), fScratchAdvances.data());
    }

    SkPoint endOfLastGlyph = origin;

    for (size_t i = 0; i < this->runSize(); i++) {
        fPositions.push_back(endOfLastGlyph);
        endOfLastGlyph += fScratchAdvances[fDenseIndex[fLastDenseIndex + i]];
    }

    if (paint.getTextAlign() != SkPaint::kLeft_Align) {
        SkVector runWidth = endOfLastGlyph - origin;
        if (paint.getTextAlign() == SkPaint::kCenter_Align) {
            runWidth.scale(SK_ScalarHalf);
        }
        for (size_t i = fLastDenseIndex; i < this->runSize(); i++) {
            fPositions[i] -= runWidth;
        }
    }

    this->addGlyphRunToList(paint, temporaryShuntGlyphIDs, text, clusters);
}

void SkGlyphRunBuilder::drawPosTextH(
        const SkPaint& paint, const void* bytes, size_t byteLength,
        const SkScalar* xpos, SkScalar constY,
        SkSpan<const char> text, SkSpan<const uint32_t> clusters) {

    SkGlyphID* temporaryShuntGlyphIDs = this->addDenseAndUnique(paint, bytes, byteLength);

    for (size_t i = 0; i < runSize(); i++) {
        fPositions.push_back(SkPoint::Make(xpos[i], constY));
    }

    this->addGlyphRunToList(paint, temporaryShuntGlyphIDs, text, clusters);
}

void SkGlyphRunBuilder::drawPosText(
        const SkPaint& paint, const void* bytes,
        size_t byteLength, const SkPoint* pos,
        SkSpan<const char> text, SkSpan<const uint32_t> clusters) {
    SkGlyphID* temporaryShuntGlyphIDs = this->addDenseAndUnique(paint, bytes, byteLength);

    for (size_t i = 0; i < runSize(); i++) {
        fPositions.push_back(pos[i]);
    }

    this->addGlyphRunToList(paint, temporaryShuntGlyphIDs, text, clusters);
}