blob: ab2fceba719b9e561023a2b201aca6249f9979f7 [file] [log] [blame]
Hal Canary1521c8a2018-03-28 09:51:00 -07001/*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/pdf/SkClusterator.h"
Hal Canary1521c8a2018-03-28 09:51:00 -07009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/private/SkTo.h"
11#include "src/core/SkGlyphRun.h"
12#include "src/utils/SkUTF.h"
Hal Canary1521c8a2018-03-28 09:51:00 -070013
14static bool is_reversed(const uint32_t* clusters, uint32_t count) {
15 // "ReversedChars" is how PDF deals with RTL text.
16 // return true if more than one cluster and monotonicly decreasing to zero.
17 if (count < 2 || clusters[0] == 0 || clusters[count - 1] != 0) {
18 return false;
19 }
20 for (uint32_t i = 0; i + 1 < count; ++i) {
21 if (clusters[i + 1] > clusters[i]) {
22 return false;
23 }
24 }
25 return true;
26}
27
Hal Canary98caedd2018-07-23 10:50:49 -040028SkClusterator::SkClusterator(const SkGlyphRun& run)
29 : fClusters(run.clusters().data())
30 , fUtf8Text(run.text().data())
Herb Derbyaedc9d22018-10-25 12:27:07 -040031 , fGlyphCount(SkToU32(run.glyphsIDs().size()))
Hal Canary98caedd2018-07-23 10:50:49 -040032 , fTextByteLength(SkToU32(run.text().size()))
Ben Wagner454e5fb2019-02-08 17:46:38 -050033 , fReversedChars(fClusters ? is_reversed(fClusters, fGlyphCount) : false)
Hal Canary98caedd2018-07-23 10:50:49 -040034{
Hal Canary98caedd2018-07-23 10:50:49 -040035 if (fClusters) {
36 SkASSERT(fUtf8Text && fTextByteLength > 0 && fGlyphCount > 0);
Hal Canary98caedd2018-07-23 10:50:49 -040037 } else {
38 SkASSERT(!fUtf8Text && fTextByteLength == 0);
Hal Canary1521c8a2018-03-28 09:51:00 -070039 }
40}
41
42SkClusterator::Cluster SkClusterator::next() {
43 if (fCurrentGlyphIndex >= fGlyphCount) {
44 return Cluster{nullptr, 0, 0, 0};
45 }
46 if (!fClusters || !fUtf8Text) {
47 return Cluster{nullptr, 0, fCurrentGlyphIndex++, 1};
48 }
49 uint32_t clusterGlyphIndex = fCurrentGlyphIndex;
50 uint32_t cluster = fClusters[clusterGlyphIndex];
51 do {
52 ++fCurrentGlyphIndex;
53 } while (fCurrentGlyphIndex < fGlyphCount && cluster == fClusters[fCurrentGlyphIndex]);
54 uint32_t clusterGlyphCount = fCurrentGlyphIndex - clusterGlyphIndex;
55 uint32_t clusterEnd = fTextByteLength;
56 for (unsigned i = 0; i < fGlyphCount; ++i) {
57 uint32_t c = fClusters[i];
58 if (c > cluster && c < clusterEnd) {
59 clusterEnd = c;
60 }
61 }
62 uint32_t clusterLen = clusterEnd - cluster;
63 return Cluster{fUtf8Text + cluster, clusterLen, clusterGlyphIndex, clusterGlyphCount};
64}