| 1 |
/* |
| 2 |
* Copyright 2011 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 "include/core/SkBitmap.h" |
| 9 |
#include "include/core/SkBlendMode.h" |
| 10 |
#include "include/core/SkImage.h" |
| 11 |
#include "include/core/SkPath.h" |
| 12 |
#include "include/core/SkPathTypes.h" |
| 13 |
#include "include/core/SkRect.h" |
| 14 |
#include "include/core/SkSize.h" |
| 15 |
#include "include/core/SkStream.h" |
| 16 |
#include "include/core/SkString.h" |
| 17 |
#include "include/docs/SkPDFDocument.h" |
| 18 |
#include "include/private/base/SkFixed.h" |
| 19 |
#include "include/private/base/SkFloatingPoint.h" |
| 20 |
#include "include/private/base/SkPoint_impl.h" |
| 21 |
#include "include/private/base/SkTo.h" |
| 22 |
#include "src/core/SkGeometry.h" |
| 23 |
#include "src/core/SkPathPriv.h" |
| 24 |
#include "src/image/SkImage_Base.h" |
| 25 |
#include "src/pdf/SkPDFResourceDict.h" |
| 26 |
#include "src/pdf/SkPDFTypes.h" |
| 27 |
#include "src/pdf/SkPDFUtils.h" |
| 28 |
|
| 29 |
#include <algorithm> |
| 30 |
#include <ctime> |
| 31 |
#include <utility> |
| 32 |
|
| 33 |
#if defined(SK_BUILD_FOR_WIN) |
| 34 |
#include "src/base/SkLeanWindows.h" |
| 35 |
#endif |
| 36 |
|
| 37 |
const char* SkPDFUtils::BlendModeName(SkBlendMode mode) { |
| 38 |
// PDF32000.book section 11.3.5 "Blend Mode" |
| 39 |
switch (mode) { |
| 40 |
case SkBlendMode::kSrcOver: return "Normal"; |
| 41 |
case SkBlendMode::kXor: return "Normal"; // (unsupported mode) |
| 42 |
case SkBlendMode::kPlus: return "Normal"; // (unsupported mode) |
| 43 |
case SkBlendMode::kScreen: return "Screen"; |
| 44 |
case SkBlendMode::kOverlay: return "Overlay"; |
| 45 |
case SkBlendMode::kDarken: return "Darken"; |
| 46 |
case SkBlendMode::kLighten: return "Lighten"; |
| 47 |
case SkBlendMode::kColorDodge: return "ColorDodge"; |
| 48 |
case SkBlendMode::kColorBurn: return "ColorBurn"; |
| 49 |
case SkBlendMode::kHardLight: return "HardLight"; |
| 50 |
case SkBlendMode::kSoftLight: return "SoftLight"; |
| 51 |
case SkBlendMode::kDifference: return "Difference"; |
| 52 |
case SkBlendMode::kExclusion: return "Exclusion"; |
| 53 |
case SkBlendMode::kMultiply: return "Multiply"; |
| 54 |
case SkBlendMode::kHue: return "Hue"; |
| 55 |
case SkBlendMode::kSaturation: return "Saturation"; |
| 56 |
case SkBlendMode::kColor: return "Color"; |
| 57 |
case SkBlendMode::kLuminosity: return "Luminosity"; |
| 58 |
// Other blendmodes are handled in SkPDFDevice::setUpContentEntry. |
| 59 |
default: return nullptr; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
std::unique_ptr<SkPDFArray> SkPDFUtils::RectToArray(const SkRect& r) { |
| 64 |
return SkPDFMakeArray(r.left(), r.top(), r.right(), r.bottom()); |
| 65 |
} |
| 66 |
|
| 67 |
std::unique_ptr<SkPDFArray> SkPDFUtils::MatrixToArray(const SkMatrix& matrix) { |
| 68 |
SkScalar a[6]; |
| 69 |
if (!matrix.asAffine(a)) { |
| 70 |
SkMatrix::SetAffineIdentity(a); |
| 71 |
} |
| 72 |
return SkPDFMakeArray(a[0], a[1], a[2], a[3], a[4], a[5]); |
| 73 |
} |
| 74 |
|
| 75 |
void SkPDFUtils::MoveTo(SkScalar x, SkScalar y, SkWStream* content) { |
| 76 |
SkPDFUtils::AppendScalar(x, content); |
| 77 |
content->writeText(" "); |
| 78 |
SkPDFUtils::AppendScalar(y, content); |
| 79 |
content->writeText(" m\n"); |
| 80 |
} |
| 81 |
|
| 82 |
void SkPDFUtils::AppendLine(SkScalar x, SkScalar y, SkWStream* content) { |
| 83 |
SkPDFUtils::AppendScalar(x, content); |
| 84 |
content->writeText(" "); |
| 85 |
SkPDFUtils::AppendScalar(y, content); |
| 86 |
content->writeText(" l\n"); |
| 87 |
} |
| 88 |
|
| 89 |
static void append_cubic(SkScalar ctl1X, SkScalar ctl1Y, |
| 90 |
SkScalar ctl2X, SkScalar ctl2Y, |
| 91 |
SkScalar dstX, SkScalar dstY, SkWStream* content) { |
| 92 |
SkString cmd("y\n"); |
| 93 |
SkPDFUtils::AppendScalar(ctl1X, content); |
| 94 |
content->writeText(" "); |
| 95 |
SkPDFUtils::AppendScalar(ctl1Y, content); |
| 96 |
content->writeText(" "); |
| 97 |
if (ctl2X != dstX || ctl2Y != dstY) { |
| 98 |
cmd.set("c\n"); |
| 99 |
SkPDFUtils::AppendScalar(ctl2X, content); |
| 100 |
content->writeText(" "); |
| 101 |
SkPDFUtils::AppendScalar(ctl2Y, content); |
| 102 |
content->writeText(" "); |
| 103 |
} |
| 104 |
SkPDFUtils::AppendScalar(dstX, content); |
| 105 |
content->writeText(" "); |
| 106 |
SkPDFUtils::AppendScalar(dstY, content); |
| 107 |
content->writeText(" "); |
| 108 |
content->writeText(cmd.c_str()); |
| 109 |
} |
| 110 |
|
| 111 |
static void append_quad(SkSpan<const SkPoint> quad, SkWStream* content) { |
| 112 |
SkPoint cubic[4]; |
| 113 |
SkConvertQuadToCubic(quad.data(), cubic); |
| 114 |
append_cubic(cubic[1].fX, cubic[1].fY, cubic[2].fX, cubic[2].fY, |
| 115 |
cubic[3].fX, cubic[3].fY, content); |
| 116 |
} |
| 117 |
|
| 118 |
void SkPDFUtils::AppendRectangle(const SkRect& rect, SkWStream* content) { |
| 119 |
// Skia has 0,0 at top left, pdf at bottom left. Do the right thing. |
| 120 |
SkScalar bottom = std::min(rect.fBottom, rect.fTop); |
| 121 |
|
| 122 |
SkPDFUtils::AppendScalar(rect.fLeft, content); |
| 123 |
content->writeText(" "); |
| 124 |
SkPDFUtils::AppendScalar(bottom, content); |
| 125 |
content->writeText(" "); |
| 126 |
SkPDFUtils::AppendScalar(rect.width(), content); |
| 127 |
content->writeText(" "); |
| 128 |
SkPDFUtils::AppendScalar(rect.height(), content); |
| 129 |
content->writeText(" re\n"); |
| 130 |
} |
| 131 |
|
| 132 |
bool SkPDFUtils::EmitPath(const SkPath& path, SkPaint::Style paintStyle, |
| 133 |
EmptyPath emptyPath, EmptyVerb emptyVerb, |
| 134 |
SkWStream* content, SkScalar tolerance) { |
| 135 |
if (path.isEmpty()) { |
| 136 |
if (emptyPath == EmptyPath::Preserve) { |
| 137 |
SkPDFUtils::AppendRectangle({0, 0, 0, 0}, content); |
| 138 |
return true; |
| 139 |
} |
| 140 |
return false; |
| 141 |
} |
| 142 |
// Filling a path with no area results in a drawing in PDF renderers but |
| 143 |
// Chrome expects to be able to draw some such entities with no visible |
| 144 |
// result, so we detect those cases and discard the drawing for them. |
| 145 |
// Specifically: moveTo(X), lineTo(Y) and moveTo(X), lineTo(X), lineTo(Y). |
| 146 |
|
| 147 |
SkRect rect; |
| 148 |
bool isClosed; // Both closure and direction need to be checked. |
| 149 |
SkPathDirection direction; |
| 150 |
if (path.isRect(&rect, &isClosed, &direction) && |
| 151 |
isClosed && |
| 152 |
(SkPathDirection::kCW == direction || |
| 153 |
SkPathFillType::kEvenOdd == path.getFillType())) |
| 154 |
{ |
| 155 |
SkPDFUtils::AppendRectangle(rect, content); |
| 156 |
return true; |
| 157 |
} |
| 158 |
|
| 159 |
enum SkipFillState { |
| 160 |
kEmpty_SkipFillState, |
| 161 |
kSingleLine_SkipFillState, |
| 162 |
kNonSingleLine_SkipFillState, |
| 163 |
}; |
| 164 |
SkipFillState fillState = kEmpty_SkipFillState; |
| 165 |
//if (paintStyle != SkPaint::kFill_Style) { |
| 166 |
// fillState = kNonSingleLine_SkipFillState; |
| 167 |
//} |
| 168 |
SkPoint lastMovePt = SkPoint::Make(0,0); |
| 169 |
SkDynamicMemoryWStream currentSegment; |
| 170 |
const bool preserveEmptyVerbs = emptyVerb == EmptyVerb::Preserve; |
| 171 |
bool wroteContent = false; |
| 172 |
|
| 173 |
SkPath::Iter iter(path, false); |
| 174 |
while (auto rec = iter.next()) { |
| 175 |
// args gets all the points, even the implicit first point. |
| 176 |
SkSpan<const SkPoint> args = rec->fPoints; |
| 177 |
switch (rec->fVerb) { |
| 178 |
case SkPathVerb::kMove: |
| 179 |
MoveTo(args[0].fX, args[0].fY, ¤tSegment); |
| 180 |
lastMovePt = args[0]; |
| 181 |
fillState = kEmpty_SkipFillState; |
| 182 |
break; |
| 183 |
case SkPathVerb::kLine: |
| 184 |
if (preserveEmptyVerbs || !SkPathPriv::AllPointsEq(args)) { |
| 185 |
AppendLine(args[1].fX, args[1].fY, ¤tSegment); |
| 186 |
if ((fillState == kEmpty_SkipFillState) && (args[0] != lastMovePt)) { |
| 187 |
fillState = kSingleLine_SkipFillState; |
| 188 |
break; |
| 189 |
} |
| 190 |
fillState = kNonSingleLine_SkipFillState; |
| 191 |
} |
| 192 |
break; |
| 193 |
case SkPathVerb::kQuad: |
| 194 |
if (preserveEmptyVerbs || !SkPathPriv::AllPointsEq(args)) { |
| 195 |
append_quad(args, ¤tSegment); |
| 196 |
fillState = kNonSingleLine_SkipFillState; |
| 197 |
} |
| 198 |
break; |
| 199 |
case SkPathVerb::kConic: |
| 200 |
if (preserveEmptyVerbs || !SkPathPriv::AllPointsEq(args)) { |
| 201 |
SkAutoConicToQuads converter; |
| 202 |
const SkPoint* quads = converter.computeQuads(args, rec->conicWeight(), tolerance); |
| 203 |
for (int i = 0; i < converter.countQuads(); ++i) { |
| 204 |
append_quad({&quads[i * 2], 3}, ¤tSegment); |
| 205 |
} |
| 206 |
fillState = kNonSingleLine_SkipFillState; |
| 207 |
} |
| 208 |
break; |
| 209 |
case SkPathVerb::kCubic: |
| 210 |
if (preserveEmptyVerbs || !SkPathPriv::AllPointsEq(args)) { |
| 211 |
append_cubic(args[1].fX, args[1].fY, args[2].fX, args[2].fY, |
| 212 |
args[3].fX, args[3].fY, ¤tSegment); |
| 213 |
fillState = kNonSingleLine_SkipFillState; |
| 214 |
} |
| 215 |
break; |
| 216 |
case SkPathVerb::kClose: |
| 217 |
ClosePath(¤tSegment); |
| 218 |
currentSegment.writeToStream(content); |
| 219 |
wroteContent = true; |
| 220 |
currentSegment.reset(); |
| 221 |
break; |
| 222 |
} |
| 223 |
} |
| 224 |
if (currentSegment.bytesWritten() > 0) { |
| 225 |
currentSegment.writeToStream(content); |
| 226 |
wroteContent = true; |
| 227 |
} |
| 228 |
return wroteContent; |
| 229 |
} |
| 230 |
|
| 231 |
void SkPDFUtils::ClosePath(SkWStream* content) { |
| 232 |
content->writeText("h\n"); |
| 233 |
} |
| 234 |
|
| 235 |
void SkPDFUtils::PaintPath(SkPaint::Style style, SkPathFillType fill, SkWStream* content) { |
| 236 |
if (style == SkPaint::kFill_Style) { |
| 237 |
content->writeText("f"); |
| 238 |
} else if (style == SkPaint::kStrokeAndFill_Style) { |
| 239 |
content->writeText("B"); |
| 240 |
} else if (style == SkPaint::kStroke_Style) { |
| 241 |
content->writeText("S"); |
| 242 |
} |
| 243 |
|
| 244 |
if (style != SkPaint::kStroke_Style) { |
| 245 |
NOT_IMPLEMENTED(fill == SkPathFillType::kInverseEvenOdd, false); |
| 246 |
NOT_IMPLEMENTED(fill == SkPathFillType::kInverseWinding, false); |
| 247 |
if (fill == SkPathFillType::kEvenOdd) { |
| 248 |
content->writeText("*"); |
| 249 |
} |
| 250 |
} |
| 251 |
content->writeText("\n"); |
| 252 |
} |
| 253 |
|
| 254 |
void SkPDFUtils::StrokePath(SkWStream* content) { |
| 255 |
SkPDFUtils::PaintPath(SkPaint::kStroke_Style, SkPathFillType::kWinding, content); |
| 256 |
} |
| 257 |
|
| 258 |
void SkPDFUtils::ApplyGraphicState(int objectIndex, SkWStream* content) { |
| 259 |
SkPDFWriteResourceName(content, SkPDFResourceType::kExtGState, objectIndex); |
| 260 |
content->writeText(" gs\n"); |
| 261 |
} |
| 262 |
|
| 263 |
void SkPDFUtils::ApplyPattern(int objectIndex, SkWStream* content) { |
| 264 |
// Select Pattern color space (CS, cs) and set pattern object as current |
| 265 |
// color (SCN, scn) |
| 266 |
content->writeText("/Pattern CS/Pattern cs"); |
| 267 |
SkPDFWriteResourceName(content, SkPDFResourceType::kPattern, objectIndex); |
| 268 |
content->writeText(" SCN"); |
| 269 |
SkPDFWriteResourceName(content, SkPDFResourceType::kPattern, objectIndex); |
| 270 |
content->writeText(" scn\n"); |
| 271 |
} |
| 272 |
|
| 273 |
// return "x/pow(10, places)", given 0<x<pow(10, places) |
| 274 |
// result points to places+2 chars. |
| 275 |
static size_t print_permil_as_decimal(int x, char* result, unsigned places) { |
| 276 |
result[0] = '.'; |
| 277 |
for (int i = places; i > 0; --i) { |
| 278 |
result[i] = '0' + x % 10; |
| 279 |
x /= 10; |
| 280 |
} |
| 281 |
int j; |
| 282 |
for (j = places; j > 1; --j) { |
| 283 |
if (result[j] != '0') { |
| 284 |
break; |
| 285 |
} |
| 286 |
} |
| 287 |
result[j + 1] = '\0'; |
| 288 |
return j + 1; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
static constexpr int int_pow(int base, unsigned exp, int acc = 1) { |
| 293 |
return exp < 1 ? acc |
| 294 |
: int_pow(base * base, |
| 295 |
exp / 2, |
| 296 |
(exp % 2) ? acc * base : acc); |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
size_t SkPDFUtils::ColorToDecimalF(float value, char (&result)[kFloatColorDecimalCount + 2]) { |
| 301 |
static constexpr int kFactor = int_pow(10, kFloatColorDecimalCount); |
| 302 |
int x = sk_float_round2int(value * kFactor); |
| 303 |
if (x >= kFactor || x <= 0) { // clamp to 0-1 |
| 304 |
result[0] = x > 0 ? '1' : '0'; |
| 305 |
result[1] = '\0'; |
| 306 |
return 1; |
| 307 |
} |
| 308 |
return print_permil_as_decimal(x, result, kFloatColorDecimalCount); |
| 309 |
} |
| 310 |
|
| 311 |
size_t SkPDFUtils::ColorToDecimal(uint8_t value, char result[5]) { |
| 312 |
if (value == 255 || value == 0) { |
| 313 |
result[0] = value ? '1' : '0'; |
| 314 |
result[1] = '\0'; |
| 315 |
return 1; |
| 316 |
} |
| 317 |
// int x = 0.5 + (1000.0 / 255.0) * value; |
| 318 |
int x = SkFixedRoundToInt((SK_Fixed1 * 1000 / 255) * value); |
| 319 |
return print_permil_as_decimal(x, result, 3); |
| 320 |
} |
| 321 |
|
| 322 |
bool SkPDFUtils::InverseTransformBBox(const SkMatrix& matrix, SkRect* bbox) { |
| 323 |
if (auto inverse = matrix.invert()) { |
| 324 |
inverse->mapRect(bbox); |
| 325 |
return true; |
| 326 |
} |
| 327 |
return false; |
| 328 |
} |
| 329 |
|
| 330 |
void SkPDFUtils::PopulateTilingPatternDict(SkPDFDict* pattern, |
| 331 |
SkRect& bbox, |
| 332 |
bool tileX, bool tileY, |
| 333 |
std::unique_ptr<SkPDFDict> resources, |
| 334 |
const SkMatrix& matrix) { |
| 335 |
const int kTiling_PatternType = 1; |
| 336 |
const int kColoredTilingPattern_PaintType = 1; |
| 337 |
const int kConstantSpacing_TilingType = 1; |
| 338 |
|
| 339 |
pattern->insertName("Type", "Pattern"); |
| 340 |
pattern->insertInt("PatternType", kTiling_PatternType); |
| 341 |
pattern->insertInt("PaintType", kColoredTilingPattern_PaintType); |
| 342 |
pattern->insertInt("TilingType", kConstantSpacing_TilingType); |
| 343 |
pattern->insertObject("BBox", SkPDFUtils::RectToArray(bbox)); |
| 344 |
// PDF tiling is a raster operation which may involve pixel snapping XStep and YStep values. |
| 345 |
// Add space between "tiles" if not tiling in the given direction. https://crbug.com/41496385 |
| 346 |
pattern->insertScalar("XStep", bbox.width() + (tileX ? 0 : 2)); |
| 347 |
pattern->insertScalar("YStep", bbox.height() + (tileY ? 0 : 2)); |
| 348 |
pattern->insertObject("Resources", std::move(resources)); |
| 349 |
if (!matrix.isIdentity()) { |
| 350 |
pattern->insertObject("Matrix", SkPDFUtils::MatrixToArray(matrix)); |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
bool SkPDFUtils::ToBitmap(const SkImage* img, SkBitmap* dst) { |
| 355 |
SkASSERT(img); |
| 356 |
SkASSERT(dst); |
| 357 |
SkBitmap bitmap; |
| 358 |
// TODO: support GPU images |
| 359 |
if(as_IB(img)->getROPixels(nullptr, &bitmap)) { |
| 360 |
SkASSERT(bitmap.dimensions() == img->dimensions()); |
| 361 |
SkASSERT(!bitmap.drawsNothing()); |
| 362 |
*dst = std::move(bitmap); |
| 363 |
return true; |
| 364 |
} |
| 365 |
return false; |
| 366 |
} |
| 367 |
|
| 368 |
void SkPDFUtils::AppendTransform(const SkMatrix& matrix, SkWStream* content) { |
| 369 |
SkScalar values[6]; |
| 370 |
if (!matrix.asAffine(values)) { |
| 371 |
SkMatrix::SetAffineIdentity(values); |
| 372 |
} |
| 373 |
for (SkScalar v : values) { |
| 374 |
SkPDFUtils::AppendScalar(v, content); |
| 375 |
content->writeText(" "); |
| 376 |
} |
| 377 |
content->writeText("cm\n"); |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
#if defined(SK_BUILD_FOR_WIN) |
| 382 |
|
| 383 |
void SkPDFUtils::GetDateTime(SkPDF::DateTime* dt) { |
| 384 |
if (dt) { |
| 385 |
SYSTEMTIME st; |
| 386 |
GetSystemTime(&st); |
| 387 |
dt->fTimeZoneMinutes = 0; |
| 388 |
dt->fYear = st.wYear; |
| 389 |
dt->fMonth = SkToU8(st.wMonth); |
| 390 |
dt->fDayOfWeek = SkToU8(st.wDayOfWeek); |
| 391 |
dt->fDay = SkToU8(st.wDay); |
| 392 |
dt->fHour = SkToU8(st.wHour); |
| 393 |
dt->fMinute = SkToU8(st.wMinute); |
| 394 |
dt->fSecond = SkToU8(st.wSecond); |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
#else // SK_BUILD_FOR_WIN |
| 399 |
|
| 400 |
void SkPDFUtils::GetDateTime(SkPDF::DateTime* dt) { |
| 401 |
if (dt) { |
| 402 |
time_t m_time; |
| 403 |
time(&m_time); |
| 404 |
struct tm tstruct; |
| 405 |
gmtime_r(&m_time, &tstruct); |
| 406 |
dt->fTimeZoneMinutes = 0; |
| 407 |
dt->fYear = tstruct.tm_year + 1900; |
| 408 |
dt->fMonth = SkToU8(tstruct.tm_mon + 1); |
| 409 |
dt->fDayOfWeek = SkToU8(tstruct.tm_wday); |
| 410 |
dt->fDay = SkToU8(tstruct.tm_mday); |
| 411 |
dt->fHour = SkToU8(tstruct.tm_hour); |
| 412 |
dt->fMinute = SkToU8(tstruct.tm_min); |
| 413 |
dt->fSecond = SkToU8(tstruct.tm_sec); |
| 414 |
} |
| 415 |
} |
| 416 |
#endif // SK_BUILD_FOR_WIN |