/src/skia/src/pdf/SkClusterator.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
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 | | |
8 | | #include "src/pdf/SkClusterator.h" |
9 | | |
10 | | #include "include/core/SkSpan.h" |
11 | | #include "include/private/base/SkAssert.h" |
12 | | #include "include/private/base/SkTo.h" |
13 | | #include "src/text/GlyphRun.h" |
14 | | |
15 | 0 | static bool is_reversed(const uint32_t* clusters, uint32_t count) { |
16 | | // "ReversedChars" is how PDF deals with RTL text. |
17 | | // return true if more than one cluster and monotonicly decreasing to zero. |
18 | 0 | if (count < 2 || clusters[0] == 0 || clusters[count - 1] != 0) { |
19 | 0 | return false; |
20 | 0 | } |
21 | 0 | for (uint32_t i = 0; i + 1 < count; ++i) { |
22 | 0 | if (clusters[i + 1] > clusters[i]) { |
23 | 0 | return false; |
24 | 0 | } |
25 | 0 | } |
26 | 0 | return true; |
27 | 0 | } |
28 | | |
29 | | SkClusterator::SkClusterator(const sktext::GlyphRun& run) |
30 | | : fClusters(run.clusters().data()) |
31 | | , fUtf8Text(run.text().data()) |
32 | | , fGlyphCount(SkToU32(run.glyphsIDs().size())) |
33 | | , fTextByteLength(SkToU32(run.text().size())) |
34 | | , fReversedChars(fClusters ? is_reversed(fClusters, fGlyphCount) : false) |
35 | 0 | { |
36 | 0 | if (fClusters) { |
37 | 0 | SkASSERT(fUtf8Text && fTextByteLength > 0 && fGlyphCount > 0); |
38 | 0 | } else { |
39 | 0 | SkASSERT(!fUtf8Text && fTextByteLength == 0); |
40 | 0 | } |
41 | 0 | } |
42 | | |
43 | 0 | SkClusterator::Cluster SkClusterator::next() { |
44 | 0 | if (fCurrentGlyphIndex >= fGlyphCount) { |
45 | 0 | return Cluster{nullptr, 0, 0, 0}; |
46 | 0 | } |
47 | 0 | if (!fClusters || !fUtf8Text) { |
48 | 0 | return Cluster{nullptr, 0, fCurrentGlyphIndex++, 1}; |
49 | 0 | } |
50 | 0 | uint32_t clusterGlyphIndex = fCurrentGlyphIndex; |
51 | 0 | uint32_t cluster = fClusters[clusterGlyphIndex]; |
52 | 0 | do { |
53 | 0 | ++fCurrentGlyphIndex; |
54 | 0 | } while (fCurrentGlyphIndex < fGlyphCount && cluster == fClusters[fCurrentGlyphIndex]); |
55 | 0 | uint32_t clusterGlyphCount = fCurrentGlyphIndex - clusterGlyphIndex; |
56 | 0 | uint32_t clusterEnd = fTextByteLength; |
57 | 0 | for (unsigned i = 0; i < fGlyphCount; ++i) { |
58 | 0 | uint32_t c = fClusters[i]; |
59 | 0 | if (c > cluster && c < clusterEnd) { |
60 | 0 | clusterEnd = c; |
61 | 0 | } |
62 | 0 | } |
63 | 0 | uint32_t clusterLen = clusterEnd - cluster; |
64 | 0 | return Cluster{fUtf8Text + cluster, clusterLen, clusterGlyphIndex, clusterGlyphCount}; |
65 | 0 | } |