Flutter Engine
The Flutter Engine
Functions
SkPDFMakeCIDGlyphWidthsArray.cpp File Reference
#include "src/pdf/SkPDFMakeCIDGlyphWidthsArray.h"
#include "include/core/SkScalar.h"
#include "include/core/SkSpan.h"
#include "include/core/SkTypes.h"
#include "include/private/base/SkTo.h"
#include "src/core/SkGlyph.h"
#include "src/core/SkStrikeSpec.h"
#include "src/pdf/SkPDFGlyphUse.h"
#include "src/pdf/SkPDFTypes.h"
#include <algorithm>
#include <cstddef>
#include <utility>
#include <vector>

Go to the source code of this file.

Functions

std::unique_ptr< SkPDFArraySkPDFMakeCIDGlyphWidthsArray (const SkTypeface &typeface, const SkPDFGlyphUse &subset, int32_t *defaultAdvance)
 

Function Documentation

◆ SkPDFMakeCIDGlyphWidthsArray()

std::unique_ptr< SkPDFArray > SkPDFMakeCIDGlyphWidthsArray ( const SkTypeface typeface,
const SkPDFGlyphUse subset,
int32_t *  defaultAdvance 
)

Definition at line 62 of file SkPDFMakeCIDGlyphWidthsArray.cpp.

64 {
65 // There are two ways of expressing advances
66 //
67 // range: " gfid [adv.ances adv.ances ... adv.ances]"
68 // run: " gfid gfid adv.ances"
69 //
70 // Assuming that on average
71 // the ASCII representation of an advance plus a space is 10 characters
72 // the ASCII representation of a glyph id plus a space is 4 characters
73 // the ASCII representation of unused gid plus a space in a range is 2 characters
74 //
75 // When not in a range or run
76 // a. Skipping don't cares or defaults is a win (trivial)
77 // b. Run wins for 2+ repeats " gid gid adv.ances"
78 // " gid [adv.ances adv.ances]"
79 // rule: 2+ repeats create run as long as possible, else start range
80 //
81 // When in a range
82 // Cost of stopping and starting a range is 8 characters "] gid ["
83 // c. Skipping defaults is always a win " adv.ances"
84 // rule: end range if default seen
85 // d. Skipping 4+ don't cares is a win " 0 0 0 0"
86 // rule: end range if 4+ don't cares
87 // Cost of stop and start range plus run is 28 characters "] gid gid adv.ances gid ["
88 // e. Switching for 2+ repeats and 4+ don't cares wins " 0 0 adv.ances 0 0 adv.ances"
89 // rule: end range for 2+ repeats with 4+ don't cares
90 // f. Switching for 3+ repeats wins " adv.ances adv.ances adv.ances"
91 // rule: end range for 3+ repeats
92
93 int emSize;
94 SkStrikeSpec strikeSpec = SkStrikeSpec::MakePDFVector(typeface, &emSize);
95 SkBulkGlyphMetricsAndPaths paths{strikeSpec};
96
97 auto result = SkPDFMakeArray();
98
99 std::vector<SkGlyphID> glyphIDs;
100 subset.getSetValues([&](unsigned index) {
101 glyphIDs.push_back(SkToU16(index));
102 });
103 auto glyphs = paths.glyphs(SkSpan(glyphIDs));
104
105 // C++20 = make_unique_for_overwrite<SkScalar[]>(glyphs.size());
106 auto advances = std::unique_ptr<SkScalar[]>(new SkScalar[glyphs.size()]);
107
108 // Find the pdf integer mode (most common pdf integer advance).
109 // Unfortunately, poppler enforces DW (default width) must be an integer,
110 // so only consider integer pdf advances when finding the mode.
111 size_t numIntAdvances = 0;
112 for (const SkGlyph* glyph : glyphs) {
113 SkScalar currentAdvance = from_font_units(glyph->advanceX(), emSize);
114 if ((int32_t)currentAdvance == currentAdvance) {
115 advances[numIntAdvances++] = currentAdvance;
116 }
117 }
118 std::sort(advances.get(), advances.get() + numIntAdvances);
119 int32_t modeAdvance = (int32_t)find_mode_or_0(SkSpan(advances.get(), numIntAdvances));
120 *defaultAdvance = modeAdvance;
121
122 // Pre-convert to pdf advances.
123 for (size_t i = 0; i < glyphs.size(); ++i) {
124 advances[i] = from_font_units(glyphs[i]->advanceX(), emSize);
125 }
126
127 for (size_t i = 0; i < glyphs.size(); ++i) {
128 SkScalar advance = advances[i];
129
130 // a. Skipping don't cares or defaults is a win (trivial)
131 if (advance == modeAdvance) {
132 continue;
133 }
134
135 // b. 2+ repeats create run as long as possible, else start range
136 {
137 size_t j = i + 1; // j is always one past the last known repeat
138 for (; j < glyphs.size(); ++j) {
139 SkScalar next_advance = advances[j];
140 if (advance != next_advance) {
141 break;
142 }
143 }
144 if (j - i >= 2) {
145 result->appendInt(glyphs[i]->getGlyphID());
146 result->appendInt(glyphs[j - 1]->getGlyphID());
147 result->appendScalar(advance);
148 i = j - 1;
149 continue;
150 }
151 }
152
153 {
154 result->appendInt(glyphs[i]->getGlyphID());
155 auto advanceArray = SkPDFMakeArray();
156 advanceArray->appendScalar(advance);
157 size_t j = i + 1; // j is always one past the last output
158 for (; j < glyphs.size(); ++j) {
159 advance = advances[j];
160
161 // c. end range if default seen
162 if (advance == modeAdvance) {
163 break;
164 }
165
166 int dontCares = glyphs[j]->getGlyphID() - glyphs[j - 1]->getGlyphID() - 1;
167 // d. end range if 4+ don't cares
168 if (dontCares >= 4) {
169 break;
170 }
171
172 SkScalar next_advance = 0;
173 // e. end range for 2+ repeats with 4+ don't cares
174 if (j + 1 < glyphs.size()) {
175 next_advance = advances[j+1];
176 int next_dontCares = glyphs[j+1]->getGlyphID() - glyphs[j]->getGlyphID() - 1;
177 if (advance == next_advance && dontCares + next_dontCares >= 4) {
178 break;
179 }
180 }
181
182 // f. end range for 3+ repeats
183 if (j + 2 < glyphs.size() && advance == next_advance) {
184 next_advance = advances[j+2];
185 if (advance == next_advance) {
186 break;
187 }
188 }
189
190 while (dontCares --> 0) {
191 advanceArray->appendScalar(0);
192 }
193 advanceArray->appendScalar(advance);
194 }
195 result->appendObject(std::move(advanceArray));
196 i = j - 1;
197 }
198 }
199
200 return result;
201}
uint16_t glyphs[5]
Definition: FontMgrTest.cpp:46
static std::vector< SkPDFIndirectReference > sort(const THashSet< SkPDFIndirectReference > &src)
static SkScalar from_font_units(SkScalar scaled, uint16_t emSize)
static std::unique_ptr< SkPDFArray > SkPDFMakeArray(Args... args)
Definition: SkPDFTypes.h:125
SkSpan(Container &&) -> SkSpan< std::remove_pointer_t< decltype(std::data(std::declval< Container >()))> >
constexpr uint16_t SkToU16(S x)
Definition: SkTo.h:24
void getSetValues(FN f) const
Definition: SkPDFGlyphUse.h:26
static SkStrikeSpec MakePDFVector(const SkTypeface &typeface, int *size)
float SkScalar
Definition: extension.cpp:12
GAsyncResult * result