blob: 189bc5ca8dcba62deb305db0461715f99a6c0087 [file] [log] [blame]
ctguil@chromium.orgf966fd32011-03-04 21:47:04 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
ctguil@chromium.orgf966fd32011-03-04 21:47:04 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
ctguil@chromium.orgf966fd32011-03-04 21:47:04 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
vandebo@chromium.org683001c2012-05-09 17:17:51 +00009#include "SkData.h"
halcanaryeb92cb32016-07-15 13:41:27 -070010#include "SkFixed.h"
reed@google.coma44ea512011-07-27 18:24:25 +000011#include "SkGeometry.h"
commit-bot@chromium.org47401352013-07-23 21:49:29 +000012#include "SkPDFResourceDict.h"
ctguil@chromium.orgf966fd32011-03-04 21:47:04 +000013#include "SkPDFUtils.h"
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000014#include "SkStream.h"
ctguil@chromium.orgf966fd32011-03-04 21:47:04 +000015#include "SkString.h"
16#include "SkPDFTypes.h"
17
halcanary8e9f5e32016-02-24 15:46:46 -080018#include <cmath>
19
halcanary8103a342016-03-08 15:10:16 -080020sk_sp<SkPDFArray> SkPDFUtils::RectToArray(const SkRect& rect) {
21 auto result = sk_make_sp<SkPDFArray>();
commit-bot@chromium.org93a2e212013-07-23 23:16:03 +000022 result->reserve(4);
23 result->appendScalar(rect.fLeft);
24 result->appendScalar(rect.fTop);
25 result->appendScalar(rect.fRight);
26 result->appendScalar(rect.fBottom);
27 return result;
28}
29
halcanary8103a342016-03-08 15:10:16 -080030sk_sp<SkPDFArray> SkPDFUtils::MatrixToArray(const SkMatrix& matrix) {
ctguil@chromium.orgf966fd32011-03-04 21:47:04 +000031 SkScalar values[6];
bungeman@google.com1ddd7c32011-07-13 19:41:55 +000032 if (!matrix.asAffine(values)) {
33 SkMatrix::SetAffineIdentity(values);
34 }
ctguil@chromium.orgf966fd32011-03-04 21:47:04 +000035
halcanary8103a342016-03-08 15:10:16 -080036 auto result = sk_make_sp<SkPDFArray>();
ctguil@chromium.orgf966fd32011-03-04 21:47:04 +000037 result->reserve(6);
38 for (size_t i = 0; i < SK_ARRAY_COUNT(values); i++) {
reed@google.comc789cf12011-07-20 12:14:33 +000039 result->appendScalar(values[i]);
ctguil@chromium.orgf966fd32011-03-04 21:47:04 +000040 }
41 return result;
42}
43
44// static
vandebo@chromium.org75f97e42011-04-11 23:24:18 +000045void SkPDFUtils::AppendTransform(const SkMatrix& matrix, SkWStream* content) {
46 SkScalar values[6];
bungeman@google.com1ddd7c32011-07-13 19:41:55 +000047 if (!matrix.asAffine(values)) {
48 SkMatrix::SetAffineIdentity(values);
49 }
vandebo@chromium.org75f97e42011-04-11 23:24:18 +000050 for (size_t i = 0; i < SK_ARRAY_COUNT(values); i++) {
halcanarybc4696b2015-05-06 10:56:04 -070051 SkPDFUtils::AppendScalar(values[i], content);
vandebo@chromium.org75f97e42011-04-11 23:24:18 +000052 content->writeText(" ");
53 }
54 content->writeText("cm\n");
55}
56
57// static
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000058void SkPDFUtils::MoveTo(SkScalar x, SkScalar y, SkWStream* content) {
halcanarybc4696b2015-05-06 10:56:04 -070059 SkPDFUtils::AppendScalar(x, content);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000060 content->writeText(" ");
halcanarybc4696b2015-05-06 10:56:04 -070061 SkPDFUtils::AppendScalar(y, content);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000062 content->writeText(" m\n");
ctguil@chromium.orgf966fd32011-03-04 21:47:04 +000063}
64
65// static
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000066void SkPDFUtils::AppendLine(SkScalar x, SkScalar y, SkWStream* content) {
halcanarybc4696b2015-05-06 10:56:04 -070067 SkPDFUtils::AppendScalar(x, content);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000068 content->writeText(" ");
halcanarybc4696b2015-05-06 10:56:04 -070069 SkPDFUtils::AppendScalar(y, content);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000070 content->writeText(" l\n");
ctguil@chromium.orgf966fd32011-03-04 21:47:04 +000071}
72
73// static
74void SkPDFUtils::AppendCubic(SkScalar ctl1X, SkScalar ctl1Y,
75 SkScalar ctl2X, SkScalar ctl2Y,
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000076 SkScalar dstX, SkScalar dstY, SkWStream* content) {
ctguil@chromium.orgf966fd32011-03-04 21:47:04 +000077 SkString cmd("y\n");
halcanarybc4696b2015-05-06 10:56:04 -070078 SkPDFUtils::AppendScalar(ctl1X, content);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000079 content->writeText(" ");
halcanarybc4696b2015-05-06 10:56:04 -070080 SkPDFUtils::AppendScalar(ctl1Y, content);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000081 content->writeText(" ");
ctguil@chromium.orgf966fd32011-03-04 21:47:04 +000082 if (ctl2X != dstX || ctl2Y != dstY) {
83 cmd.set("c\n");
halcanarybc4696b2015-05-06 10:56:04 -070084 SkPDFUtils::AppendScalar(ctl2X, content);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000085 content->writeText(" ");
halcanarybc4696b2015-05-06 10:56:04 -070086 SkPDFUtils::AppendScalar(ctl2Y, content);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000087 content->writeText(" ");
ctguil@chromium.orgf966fd32011-03-04 21:47:04 +000088 }
halcanarybc4696b2015-05-06 10:56:04 -070089 SkPDFUtils::AppendScalar(dstX, content);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000090 content->writeText(" ");
halcanarybc4696b2015-05-06 10:56:04 -070091 SkPDFUtils::AppendScalar(dstY, content);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +000092 content->writeText(" ");
93 content->writeText(cmd.c_str());
ctguil@chromium.orgf966fd32011-03-04 21:47:04 +000094}
95
reed40c85e42015-01-05 10:01:25 -080096static void append_quad(const SkPoint quad[], SkWStream* content) {
97 SkPoint cubic[4];
98 SkConvertQuadToCubic(quad, cubic);
99 SkPDFUtils::AppendCubic(cubic[1].fX, cubic[1].fY, cubic[2].fX, cubic[2].fY,
100 cubic[3].fX, cubic[3].fY, content);
101}
102
ctguil@chromium.orgf966fd32011-03-04 21:47:04 +0000103// static
vandebo@chromium.org9fbdf872011-05-09 07:55:58 +0000104void SkPDFUtils::AppendRectangle(const SkRect& rect, SkWStream* content) {
105 // Skia has 0,0 at top left, pdf at bottom left. Do the right thing.
106 SkScalar bottom = SkMinScalar(rect.fBottom, rect.fTop);
107
halcanarybc4696b2015-05-06 10:56:04 -0700108 SkPDFUtils::AppendScalar(rect.fLeft, content);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000109 content->writeText(" ");
halcanarybc4696b2015-05-06 10:56:04 -0700110 SkPDFUtils::AppendScalar(bottom, content);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000111 content->writeText(" ");
halcanarybc4696b2015-05-06 10:56:04 -0700112 SkPDFUtils::AppendScalar(rect.width(), content);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000113 content->writeText(" ");
halcanarybc4696b2015-05-06 10:56:04 -0700114 SkPDFUtils::AppendScalar(rect.height(), content);
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000115 content->writeText(" re\n");
ctguil@chromium.orgf966fd32011-03-04 21:47:04 +0000116}
117
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000118// static
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000119void SkPDFUtils::EmitPath(const SkPath& path, SkPaint::Style paintStyle,
halcanary8b2bc252015-10-06 09:41:47 -0700120 bool doConsumeDegerates, SkWStream* content) {
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000121 // Filling a path with no area results in a drawing in PDF renderers but
122 // Chrome expects to be able to draw some such entities with no visible
123 // result, so we detect those cases and discard the drawing for them.
124 // Specifically: moveTo(X), lineTo(Y) and moveTo(X), lineTo(X), lineTo(Y).
Hal Canary970225b2016-11-10 11:46:21 -0500125
126 SkRect rect;
127 bool isClosed; // Both closure and direction need to be checked.
128 SkPath::Direction direction;
129 if (path.isRect(&rect, &isClosed, &direction) &&
130 isClosed && SkPath::kCW_Direction == direction)
131 {
132 SkPDFUtils::AppendRectangle(rect, content);
133 return;
134 }
135
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000136 enum SkipFillState {
halcanary8b2bc252015-10-06 09:41:47 -0700137 kEmpty_SkipFillState,
138 kSingleLine_SkipFillState,
139 kNonSingleLine_SkipFillState,
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000140 };
141 SkipFillState fillState = kEmpty_SkipFillState;
halcanary8b2bc252015-10-06 09:41:47 -0700142 //if (paintStyle != SkPaint::kFill_Style) {
143 // fillState = kNonSingleLine_SkipFillState;
144 //}
sugoi@google.come2e81132013-03-05 18:35:55 +0000145 SkPoint lastMovePt = SkPoint::Make(0,0);
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000146 SkDynamicMemoryWStream currentSegment;
ctguil@chromium.orgf966fd32011-03-04 21:47:04 +0000147 SkPoint args[4];
148 SkPath::Iter iter(path, false);
halcanary8b2bc252015-10-06 09:41:47 -0700149 for (SkPath::Verb verb = iter.next(args, doConsumeDegerates);
150 verb != SkPath::kDone_Verb;
151 verb = iter.next(args, doConsumeDegerates)) {
ctguil@chromium.orgf966fd32011-03-04 21:47:04 +0000152 // args gets all the points, even the implicit first point.
153 switch (verb) {
154 case SkPath::kMove_Verb:
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000155 MoveTo(args[0].fX, args[0].fY, &currentSegment);
156 lastMovePt = args[0];
157 fillState = kEmpty_SkipFillState;
ctguil@chromium.orgf966fd32011-03-04 21:47:04 +0000158 break;
159 case SkPath::kLine_Verb:
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000160 AppendLine(args[1].fX, args[1].fY, &currentSegment);
halcanary8b2bc252015-10-06 09:41:47 -0700161 if ((fillState == kEmpty_SkipFillState) && (args[0] != lastMovePt)) {
162 fillState = kSingleLine_SkipFillState;
163 break;
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000164 }
halcanary8b2bc252015-10-06 09:41:47 -0700165 fillState = kNonSingleLine_SkipFillState;
ctguil@chromium.orgf966fd32011-03-04 21:47:04 +0000166 break;
reed40c85e42015-01-05 10:01:25 -0800167 case SkPath::kQuad_Verb:
168 append_quad(args, &currentSegment);
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000169 fillState = kNonSingleLine_SkipFillState;
ctguil@chromium.orgf966fd32011-03-04 21:47:04 +0000170 break;
reed40c85e42015-01-05 10:01:25 -0800171 case SkPath::kConic_Verb: {
172 const SkScalar tol = SK_Scalar1 / 4;
173 SkAutoConicToQuads converter;
174 const SkPoint* quads = converter.computeQuads(args, iter.conicWeight(), tol);
175 for (int i = 0; i < converter.countQuads(); ++i) {
176 append_quad(&quads[i * 2], &currentSegment);
177 }
halcanary8b2bc252015-10-06 09:41:47 -0700178 fillState = kNonSingleLine_SkipFillState;
reed40c85e42015-01-05 10:01:25 -0800179 } break;
ctguil@chromium.orgf966fd32011-03-04 21:47:04 +0000180 case SkPath::kCubic_Verb:
181 AppendCubic(args[1].fX, args[1].fY, args[2].fX, args[2].fY,
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000182 args[3].fX, args[3].fY, &currentSegment);
183 fillState = kNonSingleLine_SkipFillState;
ctguil@chromium.orgf966fd32011-03-04 21:47:04 +0000184 break;
185 case SkPath::kClose_Verb:
halcanary8b2bc252015-10-06 09:41:47 -0700186
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000187 ClosePath(&currentSegment);
halcanary8b2bc252015-10-06 09:41:47 -0700188
189 currentSegment.writeToStream(content);
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000190 currentSegment.reset();
ctguil@chromium.orgf966fd32011-03-04 21:47:04 +0000191 break;
ctguil@chromium.orgf966fd32011-03-04 21:47:04 +0000192 default:
193 SkASSERT(false);
194 break;
195 }
196 }
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000197 if (currentSegment.bytesWritten() > 0) {
halcanary7af21502015-02-23 12:17:59 -0800198 currentSegment.writeToStream(content);
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000199 }
ctguil@chromium.orgf966fd32011-03-04 21:47:04 +0000200}
201
202// static
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000203void SkPDFUtils::ClosePath(SkWStream* content) {
204 content->writeText("h\n");
ctguil@chromium.orgf966fd32011-03-04 21:47:04 +0000205}
206
207// static
208void SkPDFUtils::PaintPath(SkPaint::Style style, SkPath::FillType fill,
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000209 SkWStream* content) {
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000210 if (style == SkPaint::kFill_Style) {
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000211 content->writeText("f");
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000212 } else if (style == SkPaint::kStrokeAndFill_Style) {
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000213 content->writeText("B");
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000214 } else if (style == SkPaint::kStroke_Style) {
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000215 content->writeText("S");
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000216 }
ctguil@chromium.orgf966fd32011-03-04 21:47:04 +0000217
218 if (style != SkPaint::kStroke_Style) {
ctguil@chromium.orgf966fd32011-03-04 21:47:04 +0000219 NOT_IMPLEMENTED(fill == SkPath::kInverseEvenOdd_FillType, false);
220 NOT_IMPLEMENTED(fill == SkPath::kInverseWinding_FillType, false);
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000221 if (fill == SkPath::kEvenOdd_FillType) {
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000222 content->writeText("*");
ctguil@chromium.org769fa6a2011-08-20 00:36:18 +0000223 }
ctguil@chromium.orgf966fd32011-03-04 21:47:04 +0000224 }
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000225 content->writeText("\n");
ctguil@chromium.orgf966fd32011-03-04 21:47:04 +0000226}
227
228// static
vandebo@chromium.orgcae5fba2011-03-28 19:03:50 +0000229void SkPDFUtils::StrokePath(SkWStream* content) {
ctguil@chromium.orgf966fd32011-03-04 21:47:04 +0000230 SkPDFUtils::PaintPath(
231 SkPaint::kStroke_Style, SkPath::kWinding_FillType, content);
232}
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000233
234// static
235void SkPDFUtils::DrawFormXObject(int objectIndex, SkWStream* content) {
commit-bot@chromium.org47401352013-07-23 21:49:29 +0000236 content->writeText("/");
237 content->writeText(SkPDFResourceDict::getResourceName(
238 SkPDFResourceDict::kXObject_ResourceType,
239 objectIndex).c_str());
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000240 content->writeText(" Do\n");
241}
242
243// static
244void SkPDFUtils::ApplyGraphicState(int objectIndex, SkWStream* content) {
commit-bot@chromium.org47401352013-07-23 21:49:29 +0000245 content->writeText("/");
246 content->writeText(SkPDFResourceDict::getResourceName(
247 SkPDFResourceDict::kExtGState_ResourceType,
248 objectIndex).c_str());
vandebo@chromium.org6112c212011-05-13 03:50:38 +0000249 content->writeText(" gs\n");
250}
commit-bot@chromium.org93a2e212013-07-23 23:16:03 +0000251
252// static
253void SkPDFUtils::ApplyPattern(int objectIndex, SkWStream* content) {
254 // Select Pattern color space (CS, cs) and set pattern object as current
255 // color (SCN, scn)
256 SkString resourceName = SkPDFResourceDict::getResourceName(
257 SkPDFResourceDict::kPattern_ResourceType,
258 objectIndex);
259 content->writeText("/Pattern CS/Pattern cs/");
260 content->writeText(resourceName.c_str());
261 content->writeText(" SCN/");
262 content->writeText(resourceName.c_str());
263 content->writeText(" scn\n");
264}
halcanarybc4696b2015-05-06 10:56:04 -0700265
halcanaryeb92cb32016-07-15 13:41:27 -0700266size_t SkPDFUtils::ColorToDecimal(uint8_t value, char result[5]) {
267 if (value == 255 || value == 0) {
268 result[0] = value ? '1' : '0';
269 result[1] = '\0';
270 return 1;
271 }
272 // int x = 0.5 + (1000.0 / 255.0) * value;
273 int x = SkFixedRoundToInt((SK_Fixed1 * 1000 / 255) * value);
274 result[0] = '.';
275 for (int i = 3; i > 0; --i) {
276 result[i] = '0' + x % 10;
277 x /= 10;
278 }
279 int j;
280 for (j = 3; j > 1; --j) {
281 if (result[j] != '0') {
282 break;
283 }
284 }
285 result[j + 1] = '\0';
286 return j + 1;
287}
288
halcanarybc4696b2015-05-06 10:56:04 -0700289void SkPDFUtils::AppendScalar(SkScalar value, SkWStream* stream) {
halcanary8e9f5e32016-02-24 15:46:46 -0800290 char result[kMaximumFloatDecimalLength];
291 size_t len = SkPDFUtils::FloatToDecimal(SkScalarToFloat(value), result);
292 SkASSERT(len < kMaximumFloatDecimalLength);
293 stream->write(result, len);
294}
halcanarybc4696b2015-05-06 10:56:04 -0700295
halcanary56586b62016-07-25 13:59:30 -0700296// Return pow(10.0, e), optimized for common cases.
297inline double pow10(int e) {
298 switch (e) {
299 case 0: return 1.0; // common cases
300 case 1: return 10.0;
301 case 2: return 100.0;
302 case 3: return 1e+03;
303 case 4: return 1e+04;
304 case 5: return 1e+05;
305 case 6: return 1e+06;
306 case 7: return 1e+07;
307 case 8: return 1e+08;
308 case 9: return 1e+09;
309 case 10: return 1e+10;
310 case 11: return 1e+11;
311 case 12: return 1e+12;
312 case 13: return 1e+13;
313 case 14: return 1e+14;
314 case 15: return 1e+15;
315 default:
316 if (e > 15) {
317 double value = 1e+15;
318 while (e-- > 15) { value *= 10.0; }
319 return value;
320 } else {
321 SkASSERT(e < 0);
322 double value = 1.0;
323 while (e++ < 0) { value /= 10.0; }
324 return value;
325 }
326 }
327}
328
halcanary8e9f5e32016-02-24 15:46:46 -0800329/** Write a string into result, includeing a terminating '\0' (for
330 unit testing). Return strlen(result) (for SkWStream::write) The
331 resulting string will be in the form /[-]?([0-9]*.)?[0-9]+/ and
332 sscanf(result, "%f", &x) will return the original value iff the
333 value is finite. This function accepts all possible input values.
halcanarybc4696b2015-05-06 10:56:04 -0700334
halcanary8e9f5e32016-02-24 15:46:46 -0800335 Motivation: "PDF does not support [numbers] in exponential format
336 (such as 6.02e23)." Otherwise, this function would rely on a
337 sprintf-type function from the standard library. */
338size_t SkPDFUtils::FloatToDecimal(float value,
339 char result[kMaximumFloatDecimalLength]) {
340 /* The longest result is -FLT_MIN.
341 We serialize it as "-.0000000000000000000000000000000000000117549435"
342 which has 48 characters plus a terminating '\0'. */
halcanarybc4696b2015-05-06 10:56:04 -0700343
halcanary8e9f5e32016-02-24 15:46:46 -0800344 /* section C.1 of the PDF1.4 spec (http://goo.gl/0SCswJ) says that
345 most PDF rasterizers will use fixed-point scalars that lack the
346 dynamic range of floats. Even if this is the case, I want to
347 serialize these (uncommon) very small and very large scalar
348 values with enough precision to allow a floating-point
349 rasterizer to read them in with perfect accuracy.
350 Experimentally, rasterizers such as pdfium do seem to benefit
351 from this. Rasterizers that rely on fixed-point scalars should
352 gracefully ignore these values that they can not parse. */
353 char* output = &result[0];
354 const char* const end = &result[kMaximumFloatDecimalLength - 1];
355 // subtract one to leave space for '\0'.
356
357 /* This function is written to accept any possible input value,
358 including non-finite values such as INF and NAN. In that case,
359 we ignore value-correctness and and output a syntacticly-valid
360 number. */
361 if (value == SK_FloatInfinity) {
362 value = FLT_MAX; // nearest finite float.
halcanarybc4696b2015-05-06 10:56:04 -0700363 }
halcanary8e9f5e32016-02-24 15:46:46 -0800364 if (value == SK_FloatNegativeInfinity) {
365 value = -FLT_MAX; // nearest finite float.
halcanarybc4696b2015-05-06 10:56:04 -0700366 }
halcanary8e9f5e32016-02-24 15:46:46 -0800367 if (!std::isfinite(value) || value == 0.0f) {
368 // NAN is unsupported in PDF. Always output a valid number.
369 // Also catch zero here, as a special case.
370 *output++ = '0';
371 *output = '\0';
372 return output - result;
halcanarybc4696b2015-05-06 10:56:04 -0700373 }
halcanary8e9f5e32016-02-24 15:46:46 -0800374 if (value < 0.0) {
375 *output++ = '-';
376 value = -value;
halcanarybc4696b2015-05-06 10:56:04 -0700377 }
halcanary8e9f5e32016-02-24 15:46:46 -0800378 SkASSERT(value >= 0.0f);
379
halcanary56586b62016-07-25 13:59:30 -0700380 int binaryExponent;
381 (void)std::frexp(value, &binaryExponent);
382 static const double kLog2 = 0.3010299956639812; // log10(2.0);
383 int decimalExponent = static_cast<int>(std::floor(kLog2 * binaryExponent));
384 int decimalShift = decimalExponent - 8;
385 double power = pow10(-decimalShift);
386 int32_t d = static_cast<int32_t>(value * power + 0.5);
387 // SkASSERT(value == (float)(d * pow(10.0, decimalShift)));
388 SkASSERT(d <= 999999999);
389 if (d > 167772159) { // floor(pow(10,1+log10(1<<24)))
390 // need one fewer decimal digits for 24-bit precision.
391 decimalShift = decimalExponent - 7;
392 // SkASSERT(power * 0.1 = pow10(-decimalShift));
393 // recalculate to get rounding right.
394 d = static_cast<int32_t>(value * (power * 0.1) + 0.5);
395 SkASSERT(d <= 99999999);
halcanary8e9f5e32016-02-24 15:46:46 -0800396 }
halcanary56586b62016-07-25 13:59:30 -0700397 while (d % 10 == 0) {
398 d /= 10;
399 ++decimalShift;
400 }
401 SkASSERT(d > 0);
402 // SkASSERT(value == (float)(d * pow(10.0, decimalShift)));
403 uint8_t buffer[9]; // decimal value buffer.
404 int bufferIndex = 0;
405 do {
406 buffer[bufferIndex++] = d % 10;
407 d /= 10;
408 } while (d != 0);
409 SkASSERT(bufferIndex <= (int)sizeof(buffer) && bufferIndex > 0);
410 if (decimalShift >= 0) {
halcanary8e9f5e32016-02-24 15:46:46 -0800411 do {
halcanary56586b62016-07-25 13:59:30 -0700412 --bufferIndex;
413 *output++ = '0' + buffer[bufferIndex];
414 } while (bufferIndex);
415 for (int i = 0; i < decimalShift; ++i) {
416 *output++ = '0';
417 }
418 } else {
419 int placesBeforeDecimal = bufferIndex + decimalShift;
420 if (placesBeforeDecimal > 0) {
421 while (placesBeforeDecimal-- > 0) {
422 --bufferIndex;
423 *output++ = '0' + buffer[bufferIndex];
halcanary8e9f5e32016-02-24 15:46:46 -0800424 }
halcanary56586b62016-07-25 13:59:30 -0700425 *output++ = '.';
426 } else {
427 *output++ = '.';
428 int placesAfterDecimal = -placesBeforeDecimal;
429 while (placesAfterDecimal-- > 0) {
430 *output++ = '0';
431 }
432 }
433 while (bufferIndex > 0) {
434 --bufferIndex;
435 *output++ = '0' + buffer[bufferIndex];
436 if (output == end) {
437 break; // denormalized: don't need extra precision.
438 // Note: denormalized numbers will not have the same number of
439 // significantDigits, but do not need them to round-trip.
440 }
441 }
halcanary8e9f5e32016-02-24 15:46:46 -0800442 }
443 SkASSERT(output <= end);
444 *output = '\0';
445 return output - result;
halcanarybc4696b2015-05-06 10:56:04 -0700446}
447
halcanaryee41b752016-06-23 14:08:11 -0700448void SkPDFUtils::WriteString(SkWStream* wStream, const char* cin, size_t len) {
halcanarybc4696b2015-05-06 10:56:04 -0700449 SkDEBUGCODE(static const size_t kMaxLen = 65535;)
450 SkASSERT(len <= kMaxLen);
451
halcanaryee41b752016-06-23 14:08:11 -0700452 size_t extraCharacterCount = 0;
halcanarybc4696b2015-05-06 10:56:04 -0700453 for (size_t i = 0; i < len; i++) {
454 if (cin[i] > '~' || cin[i] < ' ') {
halcanaryee41b752016-06-23 14:08:11 -0700455 extraCharacterCount += 3;
halcanarybc4696b2015-05-06 10:56:04 -0700456 }
457 if (cin[i] == '\\' || cin[i] == '(' || cin[i] == ')') {
halcanaryee41b752016-06-23 14:08:11 -0700458 ++extraCharacterCount;
halcanarybc4696b2015-05-06 10:56:04 -0700459 }
460 }
halcanaryee41b752016-06-23 14:08:11 -0700461 if (extraCharacterCount <= len) {
462 wStream->writeText("(");
halcanarybc4696b2015-05-06 10:56:04 -0700463 for (size_t i = 0; i < len; i++) {
halcanaryee41b752016-06-23 14:08:11 -0700464 if (cin[i] > '~' || cin[i] < ' ') {
465 uint8_t c = static_cast<uint8_t>(cin[i]);
466 uint8_t octal[4];
467 octal[0] = '\\';
468 octal[1] = '0' + ( c >> 6 );
469 octal[2] = '0' + ((c >> 3) & 0x07);
470 octal[3] = '0' + ( c & 0x07);
471 wStream->write(octal, 4);
472 } else {
473 if (cin[i] == '\\' || cin[i] == '(' || cin[i] == ')') {
474 wStream->writeText("\\");
475 }
476 wStream->write(&cin[i], 1);
halcanarybc4696b2015-05-06 10:56:04 -0700477 }
halcanarybc4696b2015-05-06 10:56:04 -0700478 }
halcanaryee41b752016-06-23 14:08:11 -0700479 wStream->writeText(")");
halcanarybc4696b2015-05-06 10:56:04 -0700480 } else {
halcanaryee41b752016-06-23 14:08:11 -0700481 wStream->writeText("<");
halcanarybc4696b2015-05-06 10:56:04 -0700482 for (size_t i = 0; i < len; i++) {
483 uint8_t c = static_cast<uint8_t>(cin[i]);
484 static const char gHex[] = "0123456789ABCDEF";
halcanaryee41b752016-06-23 14:08:11 -0700485 char hexValue[2];
486 hexValue[0] = gHex[(c >> 4) & 0xF];
487 hexValue[1] = gHex[ c & 0xF];
488 wStream->write(hexValue, 2);
halcanarybc4696b2015-05-06 10:56:04 -0700489 }
halcanaryee41b752016-06-23 14:08:11 -0700490 wStream->writeText(">");
halcanarybc4696b2015-05-06 10:56:04 -0700491 }
halcanarybc4696b2015-05-06 10:56:04 -0700492}