| 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 |
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 |
if (count < 2 || clusters[0] == 0 || clusters[count - 1] != 0) { |
| 19 |
return false; |
| 20 |
} |
| 21 |
for (uint32_t i = 0; i + 1 < count; ++i) { |
| 22 |
if (clusters[i + 1] > clusters[i]) { |
| 23 |
return false; |
| 24 |
} |
| 25 |
} |
| 26 |
return true; |
| 27 |
} |
| 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 |
{ |
| 36 |
if (fClusters) { |
| 37 |
SkASSERT(fUtf8Text && fTextByteLength > 0 && fGlyphCount > 0); |
| 38 |
} else { |
| 39 |
SkASSERT(!fUtf8Text && fTextByteLength == 0); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
SkClusterator::Cluster SkClusterator::next() { |
| 44 |
if (fCurrentGlyphIndex >= fGlyphCount) { |
| 45 |
return Cluster{nullptr, 0, 0, 0}; |
| 46 |
} |
| 47 |
if (!fClusters || !fUtf8Text) { |
| 48 |
return Cluster{nullptr, 0, fCurrentGlyphIndex++, 1}; |
| 49 |
} |
| 50 |
uint32_t clusterGlyphIndex = fCurrentGlyphIndex; |
| 51 |
uint32_t cluster = fClusters[clusterGlyphIndex]; |
| 52 |
do { |
| 53 |
++fCurrentGlyphIndex; |
| 54 |
} while (fCurrentGlyphIndex < fGlyphCount && cluster == fClusters[fCurrentGlyphIndex]); |
| 55 |
uint32_t clusterGlyphCount = fCurrentGlyphIndex - clusterGlyphIndex; |
| 56 |
uint32_t clusterEnd = fTextByteLength; |
| 57 |
for (unsigned i = 0; i < fGlyphCount; ++i) { |
| 58 |
uint32_t c = fClusters[i]; |
| 59 |
if (c > cluster && c < clusterEnd) { |
| 60 |
clusterEnd = c; |
| 61 |
} |
| 62 |
} |
| 63 |
uint32_t clusterLen = clusterEnd - cluster; |
| 64 |
return Cluster{fUtf8Text + cluster, clusterLen, clusterGlyphIndex, clusterGlyphCount}; |
| 65 |
} |