2 * Copyright 2019 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "src/utils/SkShaperJSONWriter.h"
10 #include "include/core/SkFont.h"
11 #include "include/core/SkSpan.h"
12 #include "include/core/SkString.h"
13 #include "include/core/SkTypeface.h"
14 #include "include/private/SkTo.h"
15 #include "src/utils/SkJSONWriter.h"
16 #include "src/utils/SkUTF.h"
22 #include <type_traits>
24 SkShaperJSONWriter::SkShaperJSONWriter(SkJSONWriter* JSONWriter, const char* utf8, size_t size)
25 : fJSONWriter{JSONWriter}
26 , fUTF8{utf8, size} {}
28 void SkShaperJSONWriter::beginLine() { }
30 void SkShaperJSONWriter::runInfo(const SkShaper::RunHandler::RunInfo& info) { }
32 void SkShaperJSONWriter::commitRunInfo() { }
34 SkShaper::RunHandler::Buffer
35 SkShaperJSONWriter::runBuffer(const SkShaper::RunHandler::RunInfo& info) {
36 fGlyphs.resize(info.glyphCount);
37 fPositions.resize(info.glyphCount);
38 fClusters.resize(info.glyphCount);
39 return {fGlyphs.data(), fPositions.data(), nullptr, fClusters.data(), {0, 0}};
42 static bool is_one_to_one(const char utf8[], size_t utf8Begin, size_t utf8End,
43 std::vector<uint32_t>& clusters) {
44 size_t lastUtf8Index = utf8End;
46 auto checkCluster = [&](size_t clusterIndex) {
47 if (clusters[clusterIndex] >= lastUtf8Index) {
50 size_t utf8ClusterSize = lastUtf8Index - clusters[clusterIndex];
51 if (SkUTF::CountUTF8(&utf8[clusters[clusterIndex]], utf8ClusterSize) != 1) {
54 lastUtf8Index = clusters[clusterIndex];
58 if (clusters.front() <= clusters.back()) {
59 // left-to-right clusters
60 size_t clusterCursor = clusters.size();
61 while (clusterCursor > 0) {
62 if (!checkCluster(--clusterCursor)) { return false; }
65 // right-to-left clusters
66 size_t clusterCursor = 0;
67 while (clusterCursor < clusters.size()) {
68 if (!checkCluster(clusterCursor++)) { return false; }
75 void SkShaperJSONWriter::commitRunBuffer(const SkShaper::RunHandler::RunInfo& info) {
76 fJSONWriter->beginObject("run", true);
80 info.fFont.getTypeface()->getFamilyName(&fontName);
81 fJSONWriter->appendString("font name", fontName.c_str());
84 fJSONWriter->appendFloat("font size", info.fFont.getSize());
86 if (info.fBidiLevel > 0) {
87 std::string bidiType = info.fBidiLevel % 2 == 0 ? "left-to-right" : "right-to-left";
88 std::string bidiOutput = bidiType + " lvl " + std::to_string(info.fBidiLevel);
89 fJSONWriter->appendString("BiDi", bidiOutput.c_str());
92 if (is_one_to_one(fUTF8.c_str(), info.utf8Range.begin(), info.utf8Range.end(), fClusters)) {
93 std::string utf8{&fUTF8[info.utf8Range.begin()], info.utf8Range.size()};
94 fJSONWriter->appendString("UTF8", utf8.c_str());
96 fJSONWriter->beginArray("glyphs", false);
97 for (auto glyphID : fGlyphs) {
98 fJSONWriter->appendU32(glyphID);
100 fJSONWriter->endArray();
102 fJSONWriter->beginArray("clusters", false);
103 for (auto cluster : fClusters) {
104 fJSONWriter->appendU32(cluster);
106 fJSONWriter->endArray();
108 VisualizeClusters(fUTF8.c_str(),
109 info.utf8Range.begin(), info.utf8Range.end(),
111 SkMakeSpan(fClusters),
112 [this](size_t codePointCount, SkSpan<const char> utf1to1,
113 SkSpan<const SkGlyphID> glyph1to1) {
114 this->displayMToN(codePointCount, utf1to1, glyph1to1);
118 if (info.glyphCount > 1) {
119 fJSONWriter->beginArray("horizontal positions", false);
120 for (auto position : fPositions) {
121 fJSONWriter->appendFloat(position.x());
123 fJSONWriter->endArray();
126 fJSONWriter->beginArray("advances", false);
127 for (size_t i = 1; i < info.glyphCount; i++) {
128 fJSONWriter->appendFloat(fPositions[i].fX - fPositions[i-1].fX);
130 SkPoint lastAdvance = info.fAdvance - (fPositions.back() - fPositions.front());
131 fJSONWriter->appendFloat(lastAdvance.fX);
132 fJSONWriter->endArray();
134 fJSONWriter->endObject();
137 void SkShaperJSONWriter::BreakupClusters(size_t utf8Begin, size_t utf8End,
138 SkSpan<const uint32_t> clusters,
139 const BreakupCluastersCallback& processMToN) {
141 if (clusters.front() <= clusters.back()) {
142 // Handle left-to-right text direction
143 size_t glyphStartIndex = 0;
144 for (size_t glyphEndIndex = 0; glyphEndIndex < clusters.size(); glyphEndIndex++) {
146 if (clusters[glyphStartIndex] == clusters[glyphEndIndex]) { continue; }
148 processMToN(glyphStartIndex, glyphEndIndex,
149 clusters[glyphStartIndex], clusters[glyphEndIndex]);
151 glyphStartIndex = glyphEndIndex;
154 processMToN(glyphStartIndex, clusters.size(), clusters[glyphStartIndex], utf8End);
157 // Handle right-to-left text direction.
158 SkASSERT(clusters.size() >= 2);
159 size_t glyphStartIndex = 0;
160 uint32_t utf8EndIndex = utf8End;
161 for (size_t glyphEndIndex = 0; glyphEndIndex < clusters.size(); glyphEndIndex++) {
163 if (clusters[glyphStartIndex] == clusters[glyphEndIndex]) { continue; }
165 processMToN(glyphStartIndex, glyphEndIndex,
166 clusters[glyphStartIndex], utf8EndIndex);
168 utf8EndIndex = clusters[glyphStartIndex];
169 glyphStartIndex = glyphEndIndex;
171 processMToN(glyphStartIndex, clusters.size(), utf8Begin, clusters[glyphStartIndex-1]);
175 void SkShaperJSONWriter::VisualizeClusters(const char* utf8, size_t utf8Begin, size_t utf8End,
176 SkSpan<const SkGlyphID> glyphIDs,
177 SkSpan<const uint32_t> clusters,
178 const VisualizeClustersCallback& processMToN) {
180 size_t glyphRangeStart, glyphRangeEnd;
181 uint32_t utf8RangeStart, utf8RangeEnd;
183 auto resetRanges = [&]() {
184 glyphRangeStart = std::numeric_limits<size_t>::max();
186 utf8RangeStart = std::numeric_limits<uint32_t>::max();
190 auto checkRangesAndProcess = [&]() {
191 if (glyphRangeStart < glyphRangeEnd) {
192 size_t glyphRangeCount = glyphRangeEnd - glyphRangeStart;
193 SkSpan<const char> utf8Span{&utf8[utf8RangeStart], utf8RangeEnd - utf8RangeStart};
194 SkSpan<const SkGlyphID> glyphSpan{&glyphIDs[glyphRangeStart], glyphRangeCount};
196 // Glyph count is the same as codepoint count for 1:1.
197 processMToN(glyphRangeCount, utf8Span, glyphSpan);
202 auto gatherRuns = [&](size_t glyphStartIndex, size_t glyphEndIndex,
203 uint32_t utf8StartIndex, uint32_t utf8EndIndex) {
204 int possibleCount = SkUTF::CountUTF8(&utf8[utf8StartIndex], utf8EndIndex - utf8StartIndex);
205 if (possibleCount == -1) { return; }
206 size_t codePointCount = SkTo<size_t>(possibleCount);
207 if (codePointCount == 1 && glyphEndIndex - glyphStartIndex == 1) {
208 glyphRangeStart = std::min(glyphRangeStart, glyphStartIndex);
209 glyphRangeEnd = std::max(glyphRangeEnd, glyphEndIndex );
210 utf8RangeStart = std::min(utf8RangeStart, utf8StartIndex );
211 utf8RangeEnd = std::max(utf8RangeEnd, utf8EndIndex );
213 checkRangesAndProcess();
215 SkSpan<const char> utf8Span{&utf8[utf8StartIndex], utf8EndIndex - utf8StartIndex};
216 SkSpan<const SkGlyphID> glyphSpan{&glyphIDs[glyphStartIndex],
217 glyphEndIndex - glyphStartIndex};
219 processMToN(codePointCount, utf8Span, glyphSpan);
224 BreakupClusters(utf8Begin, utf8End, clusters, gatherRuns);
225 checkRangesAndProcess();
228 void SkShaperJSONWriter::displayMToN(size_t codePointCount,
229 SkSpan<const char> utf8,
230 SkSpan<const SkGlyphID> glyphIDs) {
231 std::string nString = std::to_string(codePointCount);
232 std::string mString = std::to_string(glyphIDs.size());
233 std::string clusterName = "cluster " + nString + " to " + mString;
234 fJSONWriter->beginObject(clusterName.c_str(), true);
235 std::string utf8String{utf8.data(), utf8.size()};
236 fJSONWriter->appendString("UTF", utf8String.c_str());
237 fJSONWriter->beginArray("glyphsIDs", false);
238 for (auto glyphID : glyphIDs) {
239 fJSONWriter->appendU32(glyphID);
241 fJSONWriter->endArray();
242 fJSONWriter->endObject();