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 "SkPDFUtils.h" 9 10 #include "SkData.h" 11 #include "SkFixed.h" 12 #include "SkGeometry.h" 13 #include "SkImage_Base.h" 14 #include "SkPDFResourceDict.h" 15 #include "SkPDFTypes.h" 16 #include "SkStream.h" 17 #include "SkString.h" 18 19 #include <cmath> 20 21 const char* SkPDFUtils::BlendModeName(SkBlendMode mode) { 22 // PDF32000.book section 11.3.5 "Blend Mode" 23 switch (mode) { 24 case SkBlendMode::kSrcOver: return "Normal"; 25 case SkBlendMode::kXor: return "Normal"; // (unsupported mode) 26 case SkBlendMode::kPlus: return "Normal"; // (unsupported mode) 27 case SkBlendMode::kScreen: return "Screen"; 28 case SkBlendMode::kOverlay: return "Overlay"; 29 case SkBlendMode::kDarken: return "Darken"; 30 case SkBlendMode::kLighten: return "Lighten"; 31 case SkBlendMode::kColorDodge: return "ColorDodge"; 32 case SkBlendMode::kColorBurn: return "ColorBurn"; 33 case SkBlendMode::kHardLight: return "HardLight"; 34 case SkBlendMode::kSoftLight: return "SoftLight"; 35 case SkBlendMode::kDifference: return "Difference"; 36 case SkBlendMode::kExclusion: return "Exclusion"; 37 case SkBlendMode::kMultiply: return "Multiply"; 38 case SkBlendMode::kHue: return "Hue"; 39 case SkBlendMode::kSaturation: return "Saturation"; 40 case SkBlendMode::kColor: return "Color"; 41 case SkBlendMode::kLuminosity: return "Luminosity"; 42 // Other blendmodes are handled in SkPDFDevice::setUpContentEntry. 43 default: return nullptr; 44 } 45 } 46 47 std::unique_ptr<SkPDFArray> SkPDFUtils::RectToArray(const SkRect& r) { 48 return SkPDFMakeArray(r.left(), r.top(), r.right(), r.bottom()); 49 } 50 51 std::unique_ptr<SkPDFArray> SkPDFUtils::MatrixToArray(const SkMatrix& matrix) { 52 SkScalar a[6]; 53 if (!matrix.asAffine(a)) { 54 SkMatrix::SetAffineIdentity(a); 55 } 56 return SkPDFMakeArray(a[0], a[1], a[2], a[3], a[4], a[5]); 57 } 58 59 void SkPDFUtils::MoveTo(SkScalar x, SkScalar y, SkWStream* content) { 60 SkPDFUtils::AppendScalar(x, content); 61 content->writeText(" "); 62 SkPDFUtils::AppendScalar(y, content); 63 content->writeText(" m\n"); 64 } 65 66 void SkPDFUtils::AppendLine(SkScalar x, SkScalar y, SkWStream* content) { 67 SkPDFUtils::AppendScalar(x, content); 68 content->writeText(" "); 69 SkPDFUtils::AppendScalar(y, content); 70 content->writeText(" l\n"); 71 } 72 73 static void append_cubic(SkScalar ctl1X, SkScalar ctl1Y, 74 SkScalar ctl2X, SkScalar ctl2Y, 75 SkScalar dstX, SkScalar dstY, SkWStream* content) { 76 SkString cmd("y\n"); 77 SkPDFUtils::AppendScalar(ctl1X, content); 78 content->writeText(" "); 79 SkPDFUtils::AppendScalar(ctl1Y, content); 80 content->writeText(" "); 81 if (ctl2X != dstX || ctl2Y != dstY) { 82 cmd.set("c\n"); 83 SkPDFUtils::AppendScalar(ctl2X, content); 84 content->writeText(" "); 85 SkPDFUtils::AppendScalar(ctl2Y, content); 86 content->writeText(" "); 87 } 88 SkPDFUtils::AppendScalar(dstX, content); 89 content->writeText(" "); 90 SkPDFUtils::AppendScalar(dstY, content); 91 content->writeText(" "); 92 content->writeText(cmd.c_str()); 93 } 94 95 static void append_quad(const SkPoint quad[], SkWStream* content) { 96 SkPoint cubic[4]; 97 SkConvertQuadToCubic(quad, cubic); 98 append_cubic(cubic[1].fX, cubic[1].fY, cubic[2].fX, cubic[2].fY, 99 cubic[3].fX, cubic[3].fY, content); 100 } 101 102 void SkPDFUtils::AppendRectangle(const SkRect& rect, SkWStream* content) { 103 // Skia has 0,0 at top left, pdf at bottom left. Do the right thing. 104 SkScalar bottom = SkMinScalar(rect.fBottom, rect.fTop); 105 106 SkPDFUtils::AppendScalar(rect.fLeft, content); 107 content->writeText(" "); 108 SkPDFUtils::AppendScalar(bottom, content); 109 content->writeText(" "); 110 SkPDFUtils::AppendScalar(rect.width(), content); 111 content->writeText(" "); 112 SkPDFUtils::AppendScalar(rect.height(), content); 113 content->writeText(" re\n"); 114 } 115 116 void SkPDFUtils::EmitPath(const SkPath& path, SkPaint::Style paintStyle, 117 bool doConsumeDegerates, SkWStream* content, 118 SkScalar tolerance) { 119 if (path.isEmpty() && SkPaint::kFill_Style == paintStyle) { 120 SkPDFUtils::AppendRectangle({0, 0, 0, 0}, content); 121 return; 122 } 123 // Filling a path with no area results in a drawing in PDF renderers but 124 // Chrome expects to be able to draw some such entities with no visible 125 // result, so we detect those cases and discard the drawing for them. 126 // Specifically: moveTo(X), lineTo(Y) and moveTo(X), lineTo(X), lineTo(Y). 127 128 SkRect rect; 129 bool isClosed; // Both closure and direction need to be checked. 130 SkPath::Direction direction; 131 if (path.isRect(&rect, &isClosed, &direction) && 132 isClosed && 133 (SkPath::kCW_Direction == direction || 134 SkPath::kEvenOdd_FillType == path.getFillType())) 135 { 136 SkPDFUtils::AppendRectangle(rect, content); 137 return; 138 } 139 140 enum SkipFillState { 141 kEmpty_SkipFillState, 142 kSingleLine_SkipFillState, 143 kNonSingleLine_SkipFillState, 144 }; 145 SkipFillState fillState = kEmpty_SkipFillState; 146 //if (paintStyle != SkPaint::kFill_Style) { 147 // fillState = kNonSingleLine_SkipFillState; 148 //} 149 SkPoint lastMovePt = SkPoint::Make(0,0); 150 SkDynamicMemoryWStream currentSegment; 151 SkPoint args[4]; 152 SkPath::Iter iter(path, false); 153 for (SkPath::Verb verb = iter.next(args, doConsumeDegerates); 154 verb != SkPath::kDone_Verb; 155 verb = iter.next(args, doConsumeDegerates)) { 156 // args gets all the points, even the implicit first point. 157 switch (verb) { 158 case SkPath::kMove_Verb: 159 MoveTo(args[0].fX, args[0].fY, ¤tSegment); 160 lastMovePt = args[0]; 161 fillState = kEmpty_SkipFillState; 162 break; 163 case SkPath::kLine_Verb: 164 AppendLine(args[1].fX, args[1].fY, ¤tSegment); 165 if ((fillState == kEmpty_SkipFillState) && (args[0] != lastMovePt)) { 166 fillState = kSingleLine_SkipFillState; 167 break; 168 } 169 fillState = kNonSingleLine_SkipFillState; 170 break; 171 case SkPath::kQuad_Verb: 172 append_quad(args, ¤tSegment); 173 fillState = kNonSingleLine_SkipFillState; 174 break; 175 case SkPath::kConic_Verb: { 176 SkAutoConicToQuads converter; 177 const SkPoint* quads = converter.computeQuads(args, iter.conicWeight(), tolerance); 178 for (int i = 0; i < converter.countQuads(); ++i) { 179 append_quad(&quads[i * 2], ¤tSegment); 180 } 181 fillState = kNonSingleLine_SkipFillState; 182 } break; 183 case SkPath::kCubic_Verb: 184 append_cubic(args[1].fX, args[1].fY, args[2].fX, args[2].fY, 185 args[3].fX, args[3].fY, ¤tSegment); 186 fillState = kNonSingleLine_SkipFillState; 187 break; 188 case SkPath::kClose_Verb: 189 ClosePath(¤tSegment); 190 currentSegment.writeToStream(content); 191 currentSegment.reset(); 192 break; 193 default: 194 SkASSERT(false); 195 break; 196 } 197 } 198 if (currentSegment.bytesWritten() > 0) { 199 currentSegment.writeToStream(content); 200 } 201 } 202 203 void SkPDFUtils::ClosePath(SkWStream* content) { 204 content->writeText("h\n"); 205 } 206 207 void SkPDFUtils::PaintPath(SkPaint::Style style, SkPath::FillType fill, 208 SkWStream* content) { 209 if (style == SkPaint::kFill_Style) { 210 content->writeText("f"); 211 } else if (style == SkPaint::kStrokeAndFill_Style) { 212 content->writeText("B"); 213 } else if (style == SkPaint::kStroke_Style) { 214 content->writeText("S"); 215 } 216 217 if (style != SkPaint::kStroke_Style) { 218 NOT_IMPLEMENTED(fill == SkPath::kInverseEvenOdd_FillType, false); 219 NOT_IMPLEMENTED(fill == SkPath::kInverseWinding_FillType, false); 220 if (fill == SkPath::kEvenOdd_FillType) { 221 content->writeText("*"); 222 } 223 } 224 content->writeText("\n"); 225 } 226 227 void SkPDFUtils::StrokePath(SkWStream* content) { 228 SkPDFUtils::PaintPath( 229 SkPaint::kStroke_Style, SkPath::kWinding_FillType, content); 230 } 231 232 void SkPDFUtils::ApplyGraphicState(int objectIndex, SkWStream* content) { 233 SkPDFWriteResourceName(content, SkPDFResourceType::kExtGState, objectIndex); 234 content->writeText(" gs\n"); 235 } 236 237 void SkPDFUtils::ApplyPattern(int objectIndex, SkWStream* content) { 238 // Select Pattern color space (CS, cs) and set pattern object as current 239 // color (SCN, scn) 240 content->writeText("/Pattern CS/Pattern cs"); 241 SkPDFWriteResourceName(content, SkPDFResourceType::kPattern, objectIndex); 242 content->writeText(" SCN"); 243 SkPDFWriteResourceName(content, SkPDFResourceType::kPattern, objectIndex); 244 content->writeText(" scn\n"); 245 } 246 247 // return "x/pow(10, places)", given 0<x<pow(10, places) 248 // result points to places+2 chars. 249 static size_t print_permil_as_decimal(int x, char* result, unsigned places) { 250 result[0] = '.'; 251 for (int i = places; i > 0; --i) { 252 result[i] = '0' + x % 10; 253 x /= 10; 254 } 255 int j; 256 for (j = places; j > 1; --j) { 257 if (result[j] != '0') { 258 break; 259 } 260 } 261 result[j + 1] = '\0'; 262 return j + 1; 263 } 264 265 266 static constexpr int int_pow(int base, unsigned exp, int acc = 1) { 267 return exp < 1 ? acc 268 : int_pow(base * base, 269 exp / 2, 270 (exp % 2) ? acc * base : acc); 271 } 272 273 274 size_t SkPDFUtils::ColorToDecimalF(float value, char result[kFloatColorDecimalCount + 2]) { 275 static constexpr int kFactor = int_pow(10, kFloatColorDecimalCount); 276 int x = sk_float_round2int(value * kFactor); 277 if (x >= kFactor || x <= 0) { // clamp to 0-1 278 result[0] = x > 0 ? '1' : '0'; 279 result[1] = '\0'; 280 return 1; 281 } 282 return print_permil_as_decimal(x, result, kFloatColorDecimalCount); 283 } 284 285 size_t SkPDFUtils::ColorToDecimal(uint8_t value, char result[5]) { 286 if (value == 255 || value == 0) { 287 result[0] = value ? '1' : '0'; 288 result[1] = '\0'; 289 return 1; 290 } 291 // int x = 0.5 + (1000.0 / 255.0) * value; 292 int x = SkFixedRoundToInt((SK_Fixed1 * 1000 / 255) * value); 293 return print_permil_as_decimal(x, result, 3); 294 } 295 296 bool SkPDFUtils::InverseTransformBBox(const SkMatrix& matrix, SkRect* bbox) { 297 SkMatrix inverse; 298 if (!matrix.invert(&inverse)) { 299 return false; 300 } 301 inverse.mapRect(bbox); 302 return true; 303 } 304 305 void SkPDFUtils::PopulateTilingPatternDict(SkPDFDict* pattern, 306 SkRect& bbox, 307 std::unique_ptr<SkPDFDict> resources, 308 const SkMatrix& matrix) { 309 const int kTiling_PatternType = 1; 310 const int kColoredTilingPattern_PaintType = 1; 311 const int kConstantSpacing_TilingType = 1; 312 313 pattern->insertName("Type", "Pattern"); 314 pattern->insertInt("PatternType", kTiling_PatternType); 315 pattern->insertInt("PaintType", kColoredTilingPattern_PaintType); 316 pattern->insertInt("TilingType", kConstantSpacing_TilingType); 317 pattern->insertObject("BBox", SkPDFUtils::RectToArray(bbox)); 318 pattern->insertScalar("XStep", bbox.width()); 319 pattern->insertScalar("YStep", bbox.height()); 320 pattern->insertObject("Resources", std::move(resources)); 321 if (!matrix.isIdentity()) { 322 pattern->insertObject("Matrix", SkPDFUtils::MatrixToArray(matrix)); 323 } 324 } 325 326 bool SkPDFUtils::ToBitmap(const SkImage* img, SkBitmap* dst) { 327 SkASSERT(img); 328 SkASSERT(dst); 329 SkBitmap bitmap; 330 if(as_IB(img)->getROPixels(&bitmap)) { 331 SkASSERT(bitmap.dimensions() == img->dimensions()); 332 SkASSERT(!bitmap.drawsNothing()); 333 *dst = std::move(bitmap); 334 return true; 335 } 336 return false; 337 } 338 339 #ifdef SK_PDF_BASE85_BINARY 340 void SkPDFUtils::Base85Encode(std::unique_ptr<SkStreamAsset> stream, SkDynamicMemoryWStream* dst) { 341 SkASSERT(dst); 342 SkASSERT(stream); 343 dst->writeText("\n"); 344 int column = 0; 345 while (true) { 346 uint8_t src[4] = {0, 0, 0, 0}; 347 size_t count = stream->read(src, 4); 348 SkASSERT(count < 5); 349 if (0 == count) { 350 dst->writeText("~>\n"); 351 return; 352 } 353 uint32_t v = ((uint32_t)src[0] << 24) | ((uint32_t)src[1] << 16) | 354 ((uint32_t)src[2] << 8) | src[3]; 355 if (v == 0 && count == 4) { 356 dst->writeText("z"); 357 column += 1; 358 } else { 359 char buffer[5]; 360 for (int n = 4; n > 0; --n) { 361 buffer[n] = (v % 85) + '!'; 362 v /= 85; 363 } 364 buffer[0] = v + '!'; 365 dst->write(buffer, count + 1); 366 column += count + 1; 367 } 368 if (column > 74) { 369 dst->writeText("\n"); 370 column = 0; 371 } 372 } 373 } 374 #endif // SK_PDF_BASE85_BINARY 375