edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 "SkCanvas.h" |
| 9 | #include "SkDevice.h" |
| 10 | #include "SkForceLinking.h" |
| 11 | #include "SkGraphics.h" |
| 12 | #include "SkImageDecoder.h" |
| 13 | #include "SkImageEncoder.h" |
| 14 | #include "SkOSFile.h" |
| 15 | #include "SkPicture.h" |
| 16 | #include "SkStream.h" |
| 17 | #include "SkTypeface.h" |
| 18 | #include "SkTArray.h" |
| 19 | #include "picture_utils.h" |
| 20 | |
| 21 | #include <iostream> |
| 22 | #include <cstdio> |
| 23 | #include <stack> |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 24 | #include <set> |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 25 | |
| 26 | __SK_FORCE_IMAGE_DECODER_LINKING; |
| 27 | |
| 28 | // TODO(edisonn): tool, show what objects were read at least, show the ones not even read |
| 29 | // keep for each object pos in file |
| 30 | // plug in for VS? syntax coloring, show selected object ... from the text, or from rendered x,y |
| 31 | |
| 32 | // TODO(edisonn): security - validate all the user input, all pdf! |
| 33 | |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 34 | // TODO(edisonn): put drawtext in #ifdefs, so comparations will ignore minor changes in text positioning and font |
| 35 | // this way, we look more at other features and layout in diffs |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 36 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 37 | // TODO(edisonn): move trace dump in the get functions, and mapper ones too so it ghappens automatically |
| 38 | /* |
| 39 | #ifdef PDF_TRACE |
| 40 | std::string str; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 41 | pdfContext->fGraphicsState.fResources->native()->ToString(str); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 42 | printf("Print Tf Resources: %s\n", str.c_str()); |
| 43 | #endif |
| 44 | */ |
| 45 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 46 | #include "SkPdfHeaders_autogen.h" |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 47 | #include "SkPdfMapper_autogen.h" |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 48 | #include "SkPdfParser.h" |
| 49 | |
| 50 | #include "SkPdfBasics.h" |
| 51 | #include "SkPdfUtils.h" |
| 52 | |
| 53 | #include "SkPdfFont.h" |
| 54 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 55 | /* |
| 56 | * TODO(edisonn): |
| 57 | * - all font types and all ppdf font features |
| 58 | * - word spacing |
| 59 | * - load font for baidu.pdf |
| 60 | * - load font for youtube.pdf |
| 61 | * - parser for pdf from the definition already available in pdfspec_autogen.py |
| 62 | * - all docs from ~/work |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 63 | * - encapsulate native in the pdf api so the skpdf does not know anything about native ... in progress |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 64 | * - load gs/ especially smask and already known prop (skp) ... in progress |
| 65 | * - wrapper on classes for customizations? e.g. |
| 66 | * SkPdfPageObjectVanila - has only the basic loaders/getters |
| 67 | * SkPdfPageObject : public SkPdfPageObjectVanila, extends, and I can add customizations here |
| 68 | * need to find a nice object model for all this with constructors and factories |
| 69 | * - deal with inheritable automatically ? |
| 70 | * - deal with specific type in spec directly, add all dictionary types to known types |
| 71 | */ |
| 72 | |
| 73 | using namespace std; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 74 | |
| 75 | // Utilities |
| 76 | static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color = SK_ColorWHITE) { |
| 77 | bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 78 | |
| 79 | bitmap->allocPixels(); |
| 80 | bitmap->eraseColor(color); |
| 81 | } |
| 82 | |
| 83 | // TODO(edisonn): synonyms? DeviceRGB and RGB ... |
| 84 | int GetColorSpaceComponents(const std::string& colorSpace) { |
| 85 | if (colorSpace == "DeviceCMYK") { |
| 86 | return 4; |
| 87 | } else if (colorSpace == "DeviceGray" || |
| 88 | colorSpace == "CalGray" || |
| 89 | colorSpace == "Indexed") { |
| 90 | return 1; |
| 91 | } else if (colorSpace == "DeviceRGB" || |
| 92 | colorSpace == "CalRGB" || |
| 93 | colorSpace == "Lab") { |
| 94 | return 3; |
| 95 | } else { |
| 96 | return 0; |
| 97 | } |
| 98 | } |
| 99 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 100 | SkMatrix SkMatrixFromPdfMatrix(double array[6]) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 101 | SkMatrix matrix; |
| 102 | matrix.setAll(SkDoubleToScalar(array[0]), |
| 103 | SkDoubleToScalar(array[2]), |
| 104 | SkDoubleToScalar(array[4]), |
| 105 | SkDoubleToScalar(array[1]), |
| 106 | SkDoubleToScalar(array[3]), |
| 107 | SkDoubleToScalar(array[5]), |
| 108 | SkDoubleToScalar(0), |
| 109 | SkDoubleToScalar(0), |
| 110 | SkDoubleToScalar(1)); |
| 111 | |
| 112 | return matrix; |
| 113 | } |
| 114 | |
| 115 | SkMatrix SkMatrixFromPdfArray(SkPdfArray* pdfArray) { |
| 116 | double array[6]; |
| 117 | |
| 118 | // TODO(edisonn): security issue, ret if size() != 6 |
| 119 | for (int i = 0; i < 6; i++) { |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 120 | const SkPdfObject* elem = pdfArray->operator [](i); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 121 | if (elem == NULL || !elem->isNumber()) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 122 | return SkMatrix::I(); // TODO(edisonn): report issue |
| 123 | } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 124 | array[i] = elem->numberValue(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | return SkMatrixFromPdfMatrix(array); |
| 128 | } |
| 129 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 130 | SkBitmap* gDumpBitmap = NULL; |
| 131 | SkCanvas* gDumpCanvas = NULL; |
| 132 | char gLastKeyword[100] = ""; |
| 133 | int gLastOpKeyword = -1; |
| 134 | char allOpWithVisualEffects[100] = ",S,s,f,F,f*,B,B*,b,b*,n,Tj,TJ,\',\",d0,d1,sh,EI,Do,EX,"; |
| 135 | int gReadOp = 0; |
| 136 | |
| 137 | |
| 138 | |
| 139 | bool hasVisualEffect(const char* pdfOp) { |
| 140 | return true; |
| 141 | if (*pdfOp == '\0') return false; |
| 142 | |
| 143 | char markedPdfOp[100] = ","; |
| 144 | strcat(markedPdfOp, pdfOp); |
| 145 | strcat(markedPdfOp, ","); |
| 146 | |
| 147 | return (strstr(allOpWithVisualEffects, markedPdfOp) != NULL); |
| 148 | } |
| 149 | |
| 150 | // TODO(edisonn): Pass PdfContext and SkCanvasd only with the define for instrumentation. |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 151 | static bool readToken(SkPdfNativeTokenizer* fTokenizer, PdfToken* token) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 152 | bool ret = fTokenizer->readToken(token); |
| 153 | |
| 154 | gReadOp++; |
| 155 | |
| 156 | #ifdef PDF_TRACE_DIFF_IN_PNG |
| 157 | // TODO(edisonn): compare with old bitmap, and save only new bits are available, and save |
| 158 | // the numbar and name of last operation, so the file name will reflect op that changed. |
| 159 | if (hasVisualEffect(gLastKeyword)) { // TODO(edisonn): and has dirty bits. |
| 160 | gDumpCanvas->flush(); |
| 161 | |
| 162 | SkBitmap bitmap; |
| 163 | setup_bitmap(&bitmap, gDumpBitmap->width(), gDumpBitmap->height()); |
| 164 | |
| 165 | memcpy(bitmap.getPixels(), gDumpBitmap->getPixels(), gDumpBitmap->getSize()); |
| 166 | |
| 167 | SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (bitmap))); |
| 168 | SkCanvas canvas(device); |
| 169 | |
| 170 | // draw context stuff here |
| 171 | SkPaint blueBorder; |
| 172 | blueBorder.setColor(SK_ColorBLUE); |
| 173 | blueBorder.setStyle(SkPaint::kStroke_Style); |
| 174 | blueBorder.setTextSize(SkDoubleToScalar(20)); |
| 175 | |
| 176 | SkString str; |
| 177 | |
| 178 | const SkClipStack* clipStack = gDumpCanvas->getClipStack(); |
| 179 | if (clipStack) { |
| 180 | SkClipStack::Iter iter(*clipStack, SkClipStack::Iter::kBottom_IterStart); |
| 181 | const SkClipStack::Element* elem; |
| 182 | double y = 0; |
| 183 | int total = 0; |
| 184 | while (elem = iter.next()) { |
| 185 | total++; |
| 186 | y += 30; |
| 187 | |
| 188 | switch (elem->getType()) { |
| 189 | case SkClipStack::Element::kRect_Type: |
| 190 | canvas.drawRect(elem->getRect(), blueBorder); |
| 191 | canvas.drawText("Rect Clip", strlen("Rect Clip"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder); |
| 192 | break; |
| 193 | case SkClipStack::Element::kPath_Type: |
| 194 | canvas.drawPath(elem->getPath(), blueBorder); |
| 195 | canvas.drawText("Path Clip", strlen("Path Clip"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder); |
| 196 | break; |
| 197 | case SkClipStack::Element::kEmpty_Type: |
| 198 | canvas.drawText("Empty Clip!!!", strlen("Empty Clip!!!"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder); |
| 199 | break; |
| 200 | default: |
| 201 | canvas.drawText("Unkown Clip!!!", strlen("Unkown Clip!!!"), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder); |
| 202 | break; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | y += 30; |
| 207 | str.printf("Number of clips in stack: %i", total); |
| 208 | canvas.drawText(str.c_str(), str.size(), SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorder); |
| 209 | } |
| 210 | |
| 211 | const SkRegion& clipRegion = gDumpCanvas->getTotalClip(); |
| 212 | SkPath clipPath; |
| 213 | if (clipRegion.getBoundaryPath(&clipPath)) { |
| 214 | SkPaint redBorder; |
| 215 | redBorder.setColor(SK_ColorRED); |
| 216 | redBorder.setStyle(SkPaint::kStroke_Style); |
| 217 | canvas.drawPath(clipPath, redBorder); |
| 218 | } |
| 219 | |
| 220 | canvas.flush(); |
| 221 | |
| 222 | SkString out; |
| 223 | |
| 224 | // TODO(edisonn): get the image, and overlay on top of it, the clip , grafic state, teh stack, |
| 225 | // ... and other properties, to be able to debug th code easily |
| 226 | |
| 227 | out.appendf("/usr/local/google/home/edisonn/log_view2/step-%i-%s.png", gLastOpKeyword, gLastKeyword); |
| 228 | SkImageEncoder::EncodeFile(out.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100); |
| 229 | } |
| 230 | |
| 231 | if (token->fType == kKeyword_TokenType) { |
| 232 | strcpy(gLastKeyword, token->fKeyword); |
| 233 | gLastOpKeyword = gReadOp; |
| 234 | } else { |
| 235 | strcpy(gLastKeyword, ""); |
| 236 | } |
| 237 | #endif |
| 238 | |
| 239 | return ret; |
| 240 | } |
| 241 | |
| 242 | |
| 243 | |
| 244 | typedef PdfResult (*PdfOperatorRenderer)(PdfContext*, SkCanvas*, PdfTokenLooper**); |
| 245 | |
| 246 | map<std::string, PdfOperatorRenderer> gPdfOps; |
| 247 | |
| 248 | map<std::string, int> gRenderStats[kCount_PdfResult]; |
| 249 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 250 | const char* gRenderStatsNames[kCount_PdfResult] = { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 251 | "Success", |
| 252 | "Partially implemented", |
| 253 | "Not yet implemented", |
| 254 | "Ignore Error", |
| 255 | "Error", |
| 256 | "Unsupported/Unknown" |
| 257 | }; |
| 258 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 259 | PdfResult DrawText(PdfContext* pdfContext, |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 260 | const SkPdfObject* _str, |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 261 | SkCanvas* canvas) |
| 262 | { |
| 263 | |
| 264 | SkPdfFont* skfont = pdfContext->fGraphicsState.fSkFont; |
| 265 | if (skfont == NULL) { |
| 266 | skfont = SkPdfFont::Default(); |
| 267 | } |
| 268 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 269 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 270 | if (_str == NULL || !_str->isAnyString()) { |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 271 | // TODO(edisonn): report warning |
| 272 | return kIgnoreError_PdfResult; |
| 273 | } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 274 | const SkPdfString* str = (const SkPdfString*)_str; |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 275 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 276 | SkUnencodedText binary(str); |
| 277 | |
| 278 | SkDecodedText decoded; |
| 279 | |
| 280 | if (skfont->encoding() == NULL) { |
| 281 | // TODO(edisonn): report warning |
| 282 | return kNYI_PdfResult; |
| 283 | } |
| 284 | |
| 285 | skfont->encoding()->decodeText(binary, &decoded); |
| 286 | |
| 287 | SkPaint paint; |
| 288 | // TODO(edisonn): when should fCurFont->GetFontSize() used? When cur is fCurFontSize == 0? |
| 289 | // Or maybe just not call setTextSize at all? |
| 290 | if (pdfContext->fGraphicsState.fCurFontSize != 0) { |
| 291 | paint.setTextSize(SkDoubleToScalar(pdfContext->fGraphicsState.fCurFontSize)); |
| 292 | } |
| 293 | |
| 294 | // if (fCurFont && fCurFont->GetFontScale() != 0) { |
| 295 | // paint.setTextScaleX(SkFloatToScalar(fCurFont->GetFontScale() / 100.0)); |
| 296 | // } |
| 297 | |
| 298 | pdfContext->fGraphicsState.applyGraphicsState(&paint, false); |
| 299 | |
| 300 | canvas->save(); |
| 301 | |
| 302 | #if 1 |
| 303 | SkMatrix matrix = pdfContext->fGraphicsState.fMatrixTm; |
| 304 | |
| 305 | SkPoint point1; |
| 306 | pdfContext->fGraphicsState.fMatrixTm.mapXY(SkIntToScalar(0), SkIntToScalar(0), &point1); |
| 307 | |
| 308 | SkMatrix mirror; |
| 309 | mirror.setTranslate(0, -point1.y()); |
| 310 | // TODO(edisonn): fix rotated text, and skewed too |
| 311 | mirror.postScale(SK_Scalar1, -SK_Scalar1); |
| 312 | // TODO(edisonn): post rotate, skew |
| 313 | mirror.postTranslate(0, point1.y()); |
| 314 | |
| 315 | matrix.postConcat(mirror); |
| 316 | |
| 317 | canvas->setMatrix(matrix); |
| 318 | |
| 319 | SkTraceMatrix(matrix, "mirrored"); |
| 320 | #endif |
| 321 | |
edisonn@google.com | 6e49c34 | 2013-06-27 20:03:43 +0000 | [diff] [blame] | 322 | skfont->drawText(decoded, &paint, pdfContext, canvas); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 323 | canvas->restore(); |
| 324 | |
| 325 | return kPartial_PdfResult; |
| 326 | } |
| 327 | |
| 328 | // TODO(edisonn): create header files with declarations! |
| 329 | PdfResult PdfOp_q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper); |
| 330 | PdfResult PdfOp_Q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper); |
| 331 | PdfResult PdfOp_Tw(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper); |
| 332 | PdfResult PdfOp_Tc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper); |
| 333 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 334 | // TODO(edisonn): perf!!! |
| 335 | |
| 336 | static SkColorTable* getGrayColortable() { |
| 337 | static SkColorTable* grayColortable = NULL; |
| 338 | if (grayColortable == NULL) { |
| 339 | SkPMColor* colors = new SkPMColor[256]; |
| 340 | for (int i = 0 ; i < 256; i++) { |
| 341 | colors[i] = SkPreMultiplyARGB(255, i, i, i); |
| 342 | } |
| 343 | grayColortable = new SkColorTable(colors, 256); |
| 344 | } |
| 345 | return grayColortable; |
| 346 | } |
| 347 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 348 | SkBitmap transferImageStreamToBitmap(unsigned char* uncompressedStream, size_t uncompressedStreamLength, |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 349 | int width, int height, int bytesPerLine, |
| 350 | int bpc, const std::string& colorSpace, |
| 351 | bool transparencyMask) { |
| 352 | SkBitmap bitmap; |
| 353 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 354 | //int components = GetColorSpaceComponents(colorSpace); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 355 | //#define MAX_COMPONENTS 10 |
| 356 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 357 | // TODO(edisonn): assume start of lines are aligned at 32 bits? |
| 358 | // Is there a faster way to load the uncompressed stream into a bitmap? |
| 359 | |
| 360 | // minimal support for now |
| 361 | if ((colorSpace == "DeviceRGB" || colorSpace == "RGB") && bpc == 8) { |
| 362 | SkColor* uncompressedStreamArgb = (SkColor*)malloc(width * height * sizeof(SkColor)); |
| 363 | |
| 364 | for (int h = 0 ; h < height; h++) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 365 | long i = width * (h); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 366 | for (int w = 0 ; w < width; w++) { |
| 367 | uncompressedStreamArgb[i] = SkColorSetRGB(uncompressedStream[3 * w], |
| 368 | uncompressedStream[3 * w + 1], |
| 369 | uncompressedStream[3 * w + 2]); |
| 370 | i++; |
| 371 | } |
| 372 | uncompressedStream += bytesPerLine; |
| 373 | } |
| 374 | |
| 375 | bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 376 | bitmap.setPixels(uncompressedStreamArgb); |
| 377 | } |
| 378 | else if ((colorSpace == "DeviceGray" || colorSpace == "Gray") && bpc == 8) { |
| 379 | unsigned char* uncompressedStreamA8 = (unsigned char*)malloc(width * height); |
| 380 | |
| 381 | for (int h = 0 ; h < height; h++) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 382 | long i = width * (h); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 383 | for (int w = 0 ; w < width; w++) { |
| 384 | uncompressedStreamA8[i] = transparencyMask ? 255 - uncompressedStream[w] : |
| 385 | uncompressedStream[w]; |
| 386 | i++; |
| 387 | } |
| 388 | uncompressedStream += bytesPerLine; |
| 389 | } |
| 390 | |
| 391 | bitmap.setConfig(transparencyMask ? SkBitmap::kA8_Config : SkBitmap::kIndex8_Config, |
| 392 | width, height); |
| 393 | bitmap.setPixels(uncompressedStreamA8, transparencyMask ? NULL : getGrayColortable()); |
| 394 | } |
| 395 | |
| 396 | // TODO(edisonn): Report Warning, NYI, or error |
| 397 | return bitmap; |
| 398 | } |
| 399 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 400 | bool transferImageStreamToARGB(unsigned char* uncompressedStream, size_t uncompressedStreamLength, |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 401 | int width, int bytesPerLine, |
| 402 | int bpc, const std::string& colorSpace, |
| 403 | SkColor** uncompressedStreamArgb, |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 404 | size_t* uncompressedStreamLengthInBytesArgb) { |
| 405 | //int components = GetColorSpaceComponents(colorSpace); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 406 | //#define MAX_COMPONENTS 10 |
| 407 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 408 | // TODO(edisonn): assume start of lines are aligned at 32 bits? |
| 409 | int height = uncompressedStreamLength / bytesPerLine; |
| 410 | |
| 411 | // minimal support for now |
| 412 | if ((colorSpace == "DeviceRGB" || colorSpace == "RGB") && bpc == 8) { |
| 413 | *uncompressedStreamLengthInBytesArgb = width * height * 4; |
| 414 | *uncompressedStreamArgb = (SkColor*)malloc(*uncompressedStreamLengthInBytesArgb); |
| 415 | |
| 416 | for (int h = 0 ; h < height; h++) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 417 | long i = width * (h); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 418 | for (int w = 0 ; w < width; w++) { |
| 419 | (*uncompressedStreamArgb)[i] = SkColorSetRGB(uncompressedStream[3 * w], |
| 420 | uncompressedStream[3 * w + 1], |
| 421 | uncompressedStream[3 * w + 2]); |
| 422 | i++; |
| 423 | } |
| 424 | uncompressedStream += bytesPerLine; |
| 425 | } |
| 426 | return true; |
| 427 | } |
| 428 | |
| 429 | if ((colorSpace == "DeviceGray" || colorSpace == "Gray") && bpc == 8) { |
| 430 | *uncompressedStreamLengthInBytesArgb = width * height * 4; |
| 431 | *uncompressedStreamArgb = (SkColor*)malloc(*uncompressedStreamLengthInBytesArgb); |
| 432 | |
| 433 | for (int h = 0 ; h < height; h++) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 434 | long i = width * (h); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 435 | for (int w = 0 ; w < width; w++) { |
| 436 | (*uncompressedStreamArgb)[i] = SkColorSetRGB(uncompressedStream[w], |
| 437 | uncompressedStream[w], |
| 438 | uncompressedStream[w]); |
| 439 | i++; |
| 440 | } |
| 441 | uncompressedStream += bytesPerLine; |
| 442 | } |
| 443 | return true; |
| 444 | } |
| 445 | |
| 446 | return false; |
| 447 | } |
| 448 | |
| 449 | // utils |
| 450 | |
| 451 | // TODO(edisonn): add cache, or put the bitmap property directly on the PdfObject |
| 452 | // TODO(edisonn): deal with colorSpaces, we could add them to SkBitmap::Config |
| 453 | // TODO(edisonn): preserve A1 format that skia knows, + fast convert from 111, 222, 444 to closest |
| 454 | // skia format, through a table |
| 455 | |
| 456 | // this functions returns the image, it does not look at the smask. |
| 457 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 458 | SkBitmap getImageFromObject(PdfContext* pdfContext, SkPdfImageDictionary* image, bool transparencyMask) { |
| 459 | if (image == NULL || !image->hasStream()) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 460 | // TODO(edisonn): report warning to be used in testing. |
| 461 | return SkBitmap(); |
| 462 | } |
| 463 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 464 | long bpc = image->BitsPerComponent(pdfContext->fPdfDoc); |
| 465 | long width = image->Width(pdfContext->fPdfDoc); |
| 466 | long height = image->Height(pdfContext->fPdfDoc); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 467 | std::string colorSpace = "DeviceRGB"; |
| 468 | |
| 469 | // TODO(edisonn): color space can be an array too! |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 470 | if (image->isColorSpaceAName(pdfContext->fPdfDoc)) { |
| 471 | colorSpace = image->getColorSpaceAsName(pdfContext->fPdfDoc); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | /* |
| 475 | bool imageMask = image->imageMask(); |
| 476 | |
| 477 | if (imageMask) { |
| 478 | if (bpc != 0 && bpc != 1) { |
| 479 | // TODO(edisonn): report warning to be used in testing. |
| 480 | return SkBitmap(); |
| 481 | } |
| 482 | bpc = 1; |
| 483 | } |
| 484 | */ |
| 485 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 486 | unsigned char* uncompressedStream = NULL; |
| 487 | size_t uncompressedStreamLength = 0; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 488 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 489 | SkPdfStream* stream = (SkPdfStream*)image; |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 490 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 491 | if (!stream || !stream->GetFilteredStreamRef(&uncompressedStream, &uncompressedStreamLength, pdfContext->fPdfDoc->allocator()) || |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 492 | uncompressedStream == NULL || uncompressedStreamLength == 0) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 493 | // TODO(edisonn): report warning to be used in testing. |
| 494 | return SkBitmap(); |
| 495 | } |
| 496 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 497 | SkPdfStreamCommonDictionary* streamDict = (SkPdfStreamCommonDictionary*)stream; |
| 498 | |
| 499 | if (streamDict->has_Filter() && ((streamDict->isFilterAName(NULL) && |
| 500 | streamDict->getFilterAsName(NULL) == "DCTDecode") || |
| 501 | (streamDict->isFilterAArray(NULL) && |
| 502 | streamDict->getFilterAsArray(NULL)->size() > 0 && |
| 503 | streamDict->getFilterAsArray(NULL)->objAtAIndex(0)->isName() && |
| 504 | streamDict->getFilterAsArray(NULL)->objAtAIndex(0)->nameValue2() == "DCTDecode"))) { |
| 505 | SkBitmap bitmap; |
| 506 | SkImageDecoder::DecodeMemory(uncompressedStream, uncompressedStreamLength, &bitmap); |
| 507 | return bitmap; |
| 508 | } |
| 509 | |
| 510 | |
| 511 | |
| 512 | // TODO (edisonn): Fast Jpeg(DCTDecode) draw, or fast PNG(FlateDecode) draw ... |
| 513 | // PdfObject* value = resolveReferenceObject(pdfContext->fPdfDoc, |
| 514 | // obj.GetDictionary().GetKey(PdfName("Filter"))); |
| 515 | // if (value && value->IsArray() && value->GetArray().GetSize() == 1) { |
| 516 | // value = resolveReferenceObject(pdfContext->fPdfDoc, |
| 517 | // &value->GetArray()[0]); |
| 518 | // } |
| 519 | // if (value && value->IsName() && value->GetName().GetName() == "DCTDecode") { |
| 520 | // SkStream stream = SkStream:: |
| 521 | // SkImageDecoder::Factory() |
| 522 | // } |
| 523 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 524 | int bytesPerLine = uncompressedStreamLength / height; |
| 525 | #ifdef PDF_TRACE |
| 526 | if (uncompressedStreamLength % height != 0) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 527 | printf("Warning uncompressedStreamLength modulo height != 0 !!!\n"); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 528 | } |
| 529 | #endif |
| 530 | |
| 531 | SkBitmap bitmap = transferImageStreamToBitmap( |
| 532 | (unsigned char*)uncompressedStream, uncompressedStreamLength, |
| 533 | width, height, bytesPerLine, |
| 534 | bpc, colorSpace, |
| 535 | transparencyMask); |
| 536 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 537 | return bitmap; |
| 538 | } |
| 539 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 540 | SkBitmap getSmaskFromObject(PdfContext* pdfContext, SkPdfImageDictionary* obj) { |
| 541 | SkPdfImageDictionary* sMask = obj->SMask(pdfContext->fPdfDoc); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 542 | |
| 543 | if (sMask) { |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 544 | return getImageFromObject(pdfContext, sMask, true); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | // TODO(edisonn): implement GS SMask. Default to empty right now. |
| 548 | return pdfContext->fGraphicsState.fSMask; |
| 549 | } |
| 550 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 551 | PdfResult doXObject_Image(PdfContext* pdfContext, SkCanvas* canvas, SkPdfImageDictionary* skpdfimage) { |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 552 | if (skpdfimage == NULL) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 553 | return kIgnoreError_PdfResult; |
| 554 | } |
| 555 | |
| 556 | SkBitmap image = getImageFromObject(pdfContext, skpdfimage, false); |
| 557 | SkBitmap sMask = getSmaskFromObject(pdfContext, skpdfimage); |
| 558 | |
| 559 | canvas->save(); |
| 560 | canvas->setMatrix(pdfContext->fGraphicsState.fMatrix); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 561 | |
| 562 | #if 1 |
| 563 | SkScalar z = SkIntToScalar(0); |
| 564 | SkScalar one = SkIntToScalar(1); |
| 565 | |
| 566 | SkPoint from[4] = {SkPoint::Make(z, z), SkPoint::Make(one, z), SkPoint::Make(one, one), SkPoint::Make(z, one)}; |
| 567 | SkPoint to[4] = {SkPoint::Make(z, one), SkPoint::Make(one, one), SkPoint::Make(one, z), SkPoint::Make(z, z)}; |
| 568 | SkMatrix flip; |
| 569 | SkAssertResult(flip.setPolyToPoly(from, to, 4)); |
| 570 | SkMatrix solveImageFlip = pdfContext->fGraphicsState.fMatrix; |
| 571 | solveImageFlip.preConcat(flip); |
| 572 | canvas->setMatrix(solveImageFlip); |
| 573 | #endif |
| 574 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 575 | SkRect dst = SkRect::MakeXYWH(SkDoubleToScalar(0.0), SkDoubleToScalar(0.0), SkDoubleToScalar(1.0), SkDoubleToScalar(1.0)); |
| 576 | |
| 577 | if (sMask.empty()) { |
| 578 | canvas->drawBitmapRect(image, dst, NULL); |
| 579 | } else { |
| 580 | canvas->saveLayer(&dst, NULL); |
| 581 | canvas->drawBitmapRect(image, dst, NULL); |
| 582 | SkPaint xfer; |
| 583 | pdfContext->fGraphicsState.applyGraphicsState(&xfer, false); |
| 584 | xfer.setXfermodeMode(SkXfermode::kSrcOut_Mode); // SkXfermode::kSdtOut_Mode |
| 585 | canvas->drawBitmapRect(sMask, dst, &xfer); |
| 586 | canvas->restore(); |
| 587 | } |
| 588 | |
| 589 | canvas->restore(); |
| 590 | |
| 591 | return kPartial_PdfResult; |
| 592 | } |
| 593 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 594 | |
| 595 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 596 | |
| 597 | PdfResult doXObject_Form(PdfContext* pdfContext, SkCanvas* canvas, SkPdfType1FormDictionary* skobj) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 598 | if (!skobj || !skobj->hasStream()) { |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 599 | return kIgnoreError_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | PdfOp_q(pdfContext, canvas, NULL); |
| 603 | canvas->save(); |
| 604 | |
| 605 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 606 | if (skobj->Resources(pdfContext->fPdfDoc)) { |
| 607 | pdfContext->fGraphicsState.fResources = skobj->Resources(pdfContext->fPdfDoc); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | SkTraceMatrix(pdfContext->fGraphicsState.fMatrix, "Current matrix"); |
| 611 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 612 | if (skobj->has_Matrix()) { |
| 613 | pdfContext->fGraphicsState.fMatrix.preConcat(skobj->Matrix(pdfContext->fPdfDoc)); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 614 | pdfContext->fGraphicsState.fMatrixTm = pdfContext->fGraphicsState.fMatrix; |
| 615 | pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fMatrix; |
| 616 | // TODO(edisonn) reset matrixTm and matricTlm also? |
| 617 | } |
| 618 | |
| 619 | SkTraceMatrix(pdfContext->fGraphicsState.fMatrix, "Total matrix"); |
| 620 | |
| 621 | canvas->setMatrix(pdfContext->fGraphicsState.fMatrix); |
| 622 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 623 | if (skobj->has_BBox()) { |
| 624 | canvas->clipRect(skobj->BBox(pdfContext->fPdfDoc), SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings. |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go. |
| 628 | // For this PdfContentsTokenizer needs to be extended. |
| 629 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 630 | SkPdfStream* stream = (SkPdfStream*)skobj; |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 631 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 632 | SkPdfNativeTokenizer* tokenizer = pdfContext->fPdfDoc->tokenizerOfStream(stream); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 633 | if (tokenizer != NULL) { |
| 634 | PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas); |
| 635 | looper.loop(); |
| 636 | delete tokenizer; |
| 637 | } |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 638 | |
| 639 | // TODO(edisonn): should we restore the variable stack at the same state? |
| 640 | // There could be operands left, that could be consumed by a parent tokenizer when we pop. |
| 641 | canvas->restore(); |
| 642 | PdfOp_Q(pdfContext, canvas, NULL); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 643 | return kPartial_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 644 | } |
| 645 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 646 | PdfResult doXObject_PS(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* obj) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 647 | return kNYI_PdfResult; |
| 648 | } |
| 649 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 650 | PdfResult doType3Char(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* skobj, SkRect bBox, SkMatrix matrix, double textSize) { |
| 651 | if (!skobj || !skobj->hasStream()) { |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 652 | return kIgnoreError_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | PdfOp_q(pdfContext, canvas, NULL); |
| 656 | canvas->save(); |
| 657 | |
| 658 | pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix); |
| 659 | pdfContext->fGraphicsState.fMatrixTm.preScale(SkDoubleToScalar(textSize), SkDoubleToScalar(textSize)); |
| 660 | |
| 661 | pdfContext->fGraphicsState.fMatrix = pdfContext->fGraphicsState.fMatrixTm; |
| 662 | pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fMatrix; |
| 663 | |
| 664 | SkTraceMatrix(pdfContext->fGraphicsState.fMatrix, "Total matrix"); |
| 665 | |
| 666 | canvas->setMatrix(pdfContext->fGraphicsState.fMatrix); |
| 667 | |
| 668 | SkRect rm = bBox; |
| 669 | pdfContext->fGraphicsState.fMatrix.mapRect(&rm); |
| 670 | |
| 671 | SkTraceRect(rm, "bbox mapped"); |
| 672 | |
| 673 | canvas->clipRect(bBox, SkRegion::kIntersect_Op, true); // TODO(edisonn): AA from settings. |
| 674 | |
| 675 | // TODO(edisonn): iterate smart on the stream even if it is compressed, tokenize it as we go. |
| 676 | // For this PdfContentsTokenizer needs to be extended. |
| 677 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 678 | SkPdfStream* stream = (SkPdfStream*)skobj; |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 679 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 680 | SkPdfNativeTokenizer* tokenizer = pdfContext->fPdfDoc->tokenizerOfStream(stream); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 681 | if (tokenizer != NULL) { |
| 682 | PdfMainLooper looper(NULL, tokenizer, pdfContext, canvas); |
| 683 | looper.loop(); |
| 684 | delete tokenizer; |
| 685 | } |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 686 | |
| 687 | // TODO(edisonn): should we restore the variable stack at the same state? |
| 688 | // There could be operands left, that could be consumed by a parent tokenizer when we pop. |
| 689 | canvas->restore(); |
| 690 | PdfOp_Q(pdfContext, canvas, NULL); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 691 | |
| 692 | return kPartial_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 696 | // TODO(edisonn): make sure the pointer is unique |
| 697 | std::set<const SkPdfObject*> gInRendering; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 698 | |
| 699 | class CheckRecursiveRendering { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 700 | const SkPdfObject* fUniqueData; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 701 | public: |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 702 | CheckRecursiveRendering(const SkPdfObject* obj) : fUniqueData(obj) { |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 703 | gInRendering.insert(obj); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | ~CheckRecursiveRendering() { |
| 707 | //SkASSERT(fObj.fInRendering); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 708 | gInRendering.erase(fUniqueData); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 709 | } |
| 710 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 711 | static bool IsInRendering(const SkPdfObject* obj) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 712 | return gInRendering.find(obj) != gInRendering.end(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 713 | } |
| 714 | }; |
| 715 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 716 | PdfResult doXObject(PdfContext* pdfContext, SkCanvas* canvas, const SkPdfObject* obj) { |
| 717 | if (CheckRecursiveRendering::IsInRendering(obj)) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 718 | // Oops, corrupt PDF! |
| 719 | return kIgnoreError_PdfResult; |
| 720 | } |
| 721 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 722 | CheckRecursiveRendering checkRecursion(obj); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 723 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 724 | switch (pdfContext->fPdfDoc->mapper()->mapXObjectDictionary(obj)) |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 725 | { |
| 726 | case kImageDictionary_SkPdfObjectType: |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 727 | return doXObject_Image(pdfContext, canvas, (SkPdfImageDictionary*)obj); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 728 | case kType1FormDictionary_SkPdfObjectType: |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 729 | return doXObject_Form(pdfContext, canvas, (SkPdfType1FormDictionary*)obj); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 730 | //case kObjectDictionaryXObjectPS_SkPdfObjectType: |
| 731 | //return doXObject_PS(skxobj.asPS()); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 732 | default: |
| 733 | return kIgnoreError_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 734 | } |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | PdfResult PdfOp_q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 738 | pdfContext->fStateStack.push(pdfContext->fGraphicsState); |
| 739 | canvas->save(); |
| 740 | return kOK_PdfResult; |
| 741 | } |
| 742 | |
| 743 | PdfResult PdfOp_Q(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 744 | pdfContext->fGraphicsState = pdfContext->fStateStack.top(); |
| 745 | pdfContext->fStateStack.pop(); |
| 746 | canvas->restore(); |
| 747 | return kOK_PdfResult; |
| 748 | } |
| 749 | |
| 750 | PdfResult PdfOp_cm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 751 | double array[6]; |
| 752 | for (int i = 0 ; i < 6 ; i++) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 753 | array[5 - i] = pdfContext->fObjectStack.top()->numberValue(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 754 | pdfContext->fObjectStack.pop(); |
| 755 | } |
| 756 | |
| 757 | // a b |
| 758 | // c d |
| 759 | // e f |
| 760 | |
| 761 | // 0 1 |
| 762 | // 2 3 |
| 763 | // 4 5 |
| 764 | |
| 765 | // sx ky |
| 766 | // kx sy |
| 767 | // tx ty |
| 768 | SkMatrix matrix = SkMatrixFromPdfMatrix(array); |
| 769 | |
| 770 | pdfContext->fGraphicsState.fMatrix.preConcat(matrix); |
| 771 | |
| 772 | #ifdef PDF_TRACE |
| 773 | printf("cm "); |
| 774 | for (int i = 0 ; i < 6 ; i++) { |
| 775 | printf("%f ", array[i]); |
| 776 | } |
| 777 | printf("\n"); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 778 | SkTraceMatrix(pdfContext->fGraphicsState.fMatrix, "cm"); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 779 | #endif |
| 780 | |
| 781 | return kOK_PdfResult; |
| 782 | } |
| 783 | |
| 784 | //leading TL Set the text leading, Tl |
| 785 | //, to leading, which is a number expressed in unscaled text |
| 786 | //space units. Text leading is used only by the T*, ', and " operators. Initial value: 0. |
| 787 | PdfResult PdfOp_TL(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 788 | double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 789 | |
| 790 | pdfContext->fGraphicsState.fTextLeading = ty; |
| 791 | |
| 792 | return kOK_PdfResult; |
| 793 | } |
| 794 | |
| 795 | PdfResult PdfOp_Td(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 796 | double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 797 | double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 798 | |
| 799 | double array[6] = {1, 0, 0, 1, tx, ty}; |
| 800 | SkMatrix matrix = SkMatrixFromPdfMatrix(array); |
| 801 | |
| 802 | pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix); |
| 803 | pdfContext->fGraphicsState.fMatrixTlm.preConcat(matrix); |
| 804 | |
| 805 | return kPartial_PdfResult; |
| 806 | } |
| 807 | |
| 808 | PdfResult PdfOp_TD(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 809 | double ty = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 810 | double tx = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 811 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 812 | // TODO(edisonn): Create factory methods or constructors so native is hidden |
| 813 | SkPdfReal* _ty = pdfContext->fPdfDoc->createReal(-ty); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 814 | pdfContext->fObjectStack.push(_ty); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 815 | |
| 816 | PdfOp_TL(pdfContext, canvas, looper); |
| 817 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 818 | SkPdfReal* vtx = pdfContext->fPdfDoc->createReal(tx); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 819 | pdfContext->fObjectStack.push(vtx); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 820 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 821 | SkPdfReal* vty = pdfContext->fPdfDoc->createReal(ty); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 822 | pdfContext->fObjectStack.push(vty); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 823 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 824 | PdfResult ret = PdfOp_Td(pdfContext, canvas, looper); |
| 825 | |
| 826 | // TODO(edisonn): delete all the objects after rendering was complete, in this way pdf is rendered faster |
| 827 | // and the cleanup can happen while the user looks at the image |
| 828 | delete _ty; |
| 829 | delete vtx; |
| 830 | delete vty; |
| 831 | |
| 832 | return ret; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | PdfResult PdfOp_Tm(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 836 | double f = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 837 | double e = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 838 | double d = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 839 | double c = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 840 | double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 841 | double a = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 842 | |
| 843 | double array[6]; |
| 844 | array[0] = a; |
| 845 | array[1] = b; |
| 846 | array[2] = c; |
| 847 | array[3] = d; |
| 848 | array[4] = e; |
| 849 | array[5] = f; |
| 850 | |
| 851 | SkMatrix matrix = SkMatrixFromPdfMatrix(array); |
| 852 | matrix.postConcat(pdfContext->fGraphicsState.fMatrix); |
| 853 | |
| 854 | // TODO(edisonn): Text positioning. |
| 855 | pdfContext->fGraphicsState.fMatrixTm = matrix; |
| 856 | pdfContext->fGraphicsState.fMatrixTlm = matrix;; |
| 857 | |
| 858 | return kPartial_PdfResult; |
| 859 | } |
| 860 | |
| 861 | //— T* Move to the start of the next line. This operator has the same effect as the code |
| 862 | //0 Tl Td |
| 863 | //where Tl is the current leading parameter in the text state |
| 864 | PdfResult PdfOp_T_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 865 | SkPdfReal* zero = pdfContext->fPdfDoc->createReal(0.0); |
| 866 | SkPdfReal* tl = pdfContext->fPdfDoc->createReal(pdfContext->fGraphicsState.fTextLeading); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 867 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 868 | pdfContext->fObjectStack.push(zero); |
| 869 | pdfContext->fObjectStack.push(tl); |
| 870 | |
| 871 | PdfResult ret = PdfOp_Td(pdfContext, canvas, looper); |
| 872 | |
| 873 | delete zero; // TODO(edisonn): do not alocate and delete constants! |
| 874 | delete tl; |
| 875 | |
| 876 | return ret; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | PdfResult PdfOp_m(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 880 | if (pdfContext->fGraphicsState.fPathClosed) { |
| 881 | pdfContext->fGraphicsState.fPath.reset(); |
| 882 | pdfContext->fGraphicsState.fPathClosed = false; |
| 883 | } |
| 884 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 885 | pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 886 | pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 887 | |
| 888 | pdfContext->fGraphicsState.fPath.moveTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX), |
| 889 | SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY)); |
| 890 | |
| 891 | return kOK_PdfResult; |
| 892 | } |
| 893 | |
| 894 | PdfResult PdfOp_l(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 895 | if (pdfContext->fGraphicsState.fPathClosed) { |
| 896 | pdfContext->fGraphicsState.fPath.reset(); |
| 897 | pdfContext->fGraphicsState.fPathClosed = false; |
| 898 | } |
| 899 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 900 | pdfContext->fGraphicsState.fCurPosY = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 901 | pdfContext->fGraphicsState.fCurPosX = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 902 | |
| 903 | pdfContext->fGraphicsState.fPath.lineTo(SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosX), |
| 904 | SkDoubleToScalar(pdfContext->fGraphicsState.fCurPosY)); |
| 905 | |
| 906 | return kOK_PdfResult; |
| 907 | } |
| 908 | |
| 909 | PdfResult PdfOp_c(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 910 | if (pdfContext->fGraphicsState.fPathClosed) { |
| 911 | pdfContext->fGraphicsState.fPath.reset(); |
| 912 | pdfContext->fGraphicsState.fPathClosed = false; |
| 913 | } |
| 914 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 915 | double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 916 | double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 917 | double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 918 | double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 919 | double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 920 | double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 921 | |
| 922 | pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1), |
| 923 | SkDoubleToScalar(x2), SkDoubleToScalar(y2), |
| 924 | SkDoubleToScalar(x3), SkDoubleToScalar(y3)); |
| 925 | |
| 926 | pdfContext->fGraphicsState.fCurPosX = x3; |
| 927 | pdfContext->fGraphicsState.fCurPosY = y3; |
| 928 | |
| 929 | return kOK_PdfResult; |
| 930 | } |
| 931 | |
| 932 | PdfResult PdfOp_v(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 933 | if (pdfContext->fGraphicsState.fPathClosed) { |
| 934 | pdfContext->fGraphicsState.fPath.reset(); |
| 935 | pdfContext->fGraphicsState.fPathClosed = false; |
| 936 | } |
| 937 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 938 | double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 939 | double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 940 | double y2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 941 | double x2 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 942 | double y1 = pdfContext->fGraphicsState.fCurPosY; |
| 943 | double x1 = pdfContext->fGraphicsState.fCurPosX; |
| 944 | |
| 945 | pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1), |
| 946 | SkDoubleToScalar(x2), SkDoubleToScalar(y2), |
| 947 | SkDoubleToScalar(x3), SkDoubleToScalar(y3)); |
| 948 | |
| 949 | pdfContext->fGraphicsState.fCurPosX = x3; |
| 950 | pdfContext->fGraphicsState.fCurPosY = y3; |
| 951 | |
| 952 | return kOK_PdfResult; |
| 953 | } |
| 954 | |
| 955 | PdfResult PdfOp_y(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 956 | if (pdfContext->fGraphicsState.fPathClosed) { |
| 957 | pdfContext->fGraphicsState.fPath.reset(); |
| 958 | pdfContext->fGraphicsState.fPathClosed = false; |
| 959 | } |
| 960 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 961 | double y3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 962 | double x3 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 963 | double y2 = pdfContext->fGraphicsState.fCurPosY; |
| 964 | double x2 = pdfContext->fGraphicsState.fCurPosX; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 965 | double y1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 966 | double x1 = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 967 | |
| 968 | pdfContext->fGraphicsState.fPath.cubicTo(SkDoubleToScalar(x1), SkDoubleToScalar(y1), |
| 969 | SkDoubleToScalar(x2), SkDoubleToScalar(y2), |
| 970 | SkDoubleToScalar(x3), SkDoubleToScalar(y3)); |
| 971 | |
| 972 | pdfContext->fGraphicsState.fCurPosX = x3; |
| 973 | pdfContext->fGraphicsState.fCurPosY = y3; |
| 974 | |
| 975 | return kOK_PdfResult; |
| 976 | } |
| 977 | |
| 978 | PdfResult PdfOp_re(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 979 | if (pdfContext->fGraphicsState.fPathClosed) { |
| 980 | pdfContext->fGraphicsState.fPath.reset(); |
| 981 | pdfContext->fGraphicsState.fPathClosed = false; |
| 982 | } |
| 983 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 984 | double height = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 985 | double width = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 986 | double y = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 987 | double x = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 988 | |
| 989 | pdfContext->fGraphicsState.fPath.addRect(SkDoubleToScalar(x), SkDoubleToScalar(y), |
| 990 | SkDoubleToScalar(x + width), SkDoubleToScalar(y + height)); |
| 991 | |
| 992 | pdfContext->fGraphicsState.fCurPosX = x; |
| 993 | pdfContext->fGraphicsState.fCurPosY = y + height; |
| 994 | |
| 995 | return kOK_PdfResult; |
| 996 | } |
| 997 | |
| 998 | PdfResult PdfOp_h(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 999 | pdfContext->fGraphicsState.fPath.close(); |
| 1000 | return kOK_PdfResult; |
| 1001 | } |
| 1002 | |
| 1003 | PdfResult PdfOp_fillAndStroke(PdfContext* pdfContext, SkCanvas* canvas, bool fill, bool stroke, bool close, bool evenOdd) { |
| 1004 | SkPath path = pdfContext->fGraphicsState.fPath; |
| 1005 | |
| 1006 | if (close) { |
| 1007 | path.close(); |
| 1008 | } |
| 1009 | |
| 1010 | canvas->setMatrix(pdfContext->fGraphicsState.fMatrix); |
| 1011 | |
| 1012 | SkPaint paint; |
| 1013 | |
| 1014 | SkPoint line[2]; |
| 1015 | if (fill && !stroke && path.isLine(line)) { |
| 1016 | paint.setStyle(SkPaint::kStroke_Style); |
| 1017 | |
| 1018 | pdfContext->fGraphicsState.applyGraphicsState(&paint, false); |
| 1019 | paint.setStrokeWidth(SkDoubleToScalar(0)); |
| 1020 | |
| 1021 | canvas->drawPath(path, paint); |
| 1022 | } else { |
| 1023 | if (fill) { |
| 1024 | paint.setStyle(SkPaint::kFill_Style); |
| 1025 | if (evenOdd) { |
| 1026 | path.setFillType(SkPath::kEvenOdd_FillType); |
| 1027 | } |
| 1028 | |
| 1029 | pdfContext->fGraphicsState.applyGraphicsState(&paint, false); |
| 1030 | |
| 1031 | canvas->drawPath(path, paint); |
| 1032 | } |
| 1033 | |
| 1034 | if (stroke) { |
| 1035 | paint.setStyle(SkPaint::kStroke_Style); |
| 1036 | |
| 1037 | pdfContext->fGraphicsState.applyGraphicsState(&paint, true); |
| 1038 | |
| 1039 | path.setFillType(SkPath::kWinding_FillType); // reset it, just in case it messes up the stroke |
| 1040 | canvas->drawPath(path, paint); |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | pdfContext->fGraphicsState.fPath.reset(); |
| 1045 | // todo zoom ... other stuff ? |
| 1046 | |
| 1047 | if (pdfContext->fGraphicsState.fHasClipPathToApply) { |
| 1048 | #ifndef PDF_DEBUG_NO_CLIPING |
| 1049 | canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true); |
| 1050 | #endif |
| 1051 | } |
| 1052 | |
| 1053 | //pdfContext->fGraphicsState.fClipPath.reset(); |
| 1054 | pdfContext->fGraphicsState.fHasClipPathToApply = false; |
| 1055 | |
| 1056 | return kPartial_PdfResult; |
| 1057 | |
| 1058 | } |
| 1059 | |
| 1060 | PdfResult PdfOp_S(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1061 | return PdfOp_fillAndStroke(pdfContext, canvas, false, true, false, false); |
| 1062 | } |
| 1063 | |
| 1064 | PdfResult PdfOp_s(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1065 | return PdfOp_fillAndStroke(pdfContext, canvas, false, true, true, false); |
| 1066 | } |
| 1067 | |
| 1068 | PdfResult PdfOp_F(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1069 | return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false); |
| 1070 | } |
| 1071 | |
| 1072 | PdfResult PdfOp_f(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1073 | return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, false); |
| 1074 | } |
| 1075 | |
| 1076 | PdfResult PdfOp_f_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1077 | return PdfOp_fillAndStroke(pdfContext, canvas, true, false, false, true); |
| 1078 | } |
| 1079 | |
| 1080 | PdfResult PdfOp_B(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1081 | return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, false); |
| 1082 | } |
| 1083 | |
| 1084 | PdfResult PdfOp_B_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1085 | return PdfOp_fillAndStroke(pdfContext, canvas, true, true, false, true); |
| 1086 | } |
| 1087 | |
| 1088 | PdfResult PdfOp_b(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1089 | return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, false); |
| 1090 | } |
| 1091 | |
| 1092 | PdfResult PdfOp_b_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1093 | return PdfOp_fillAndStroke(pdfContext, canvas, true, true, true, true); |
| 1094 | } |
| 1095 | |
| 1096 | PdfResult PdfOp_n(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1097 | canvas->setMatrix(pdfContext->fGraphicsState.fMatrix); |
| 1098 | if (pdfContext->fGraphicsState.fHasClipPathToApply) { |
| 1099 | #ifndef PDF_DEBUG_NO_CLIPING |
| 1100 | canvas->clipPath(pdfContext->fGraphicsState.fClipPath, SkRegion::kIntersect_Op, true); |
| 1101 | #endif |
| 1102 | } |
| 1103 | |
| 1104 | //pdfContext->fGraphicsState.fClipPath.reset(); |
| 1105 | pdfContext->fGraphicsState.fHasClipPathToApply = false; |
| 1106 | |
| 1107 | pdfContext->fGraphicsState.fPathClosed = true; |
| 1108 | |
| 1109 | return kOK_PdfResult; |
| 1110 | } |
| 1111 | |
| 1112 | PdfResult PdfOp_BT(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1113 | pdfContext->fGraphicsState.fTextBlock = true; |
| 1114 | pdfContext->fGraphicsState.fMatrixTm = pdfContext->fGraphicsState.fMatrix; |
| 1115 | pdfContext->fGraphicsState.fMatrixTlm = pdfContext->fGraphicsState.fMatrix; |
| 1116 | |
| 1117 | return kPartial_PdfResult; |
| 1118 | } |
| 1119 | |
| 1120 | PdfResult PdfOp_ET(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1121 | if (!pdfContext->fGraphicsState.fTextBlock) { |
| 1122 | return kIgnoreError_PdfResult; |
| 1123 | } |
| 1124 | // TODO(edisonn): anything else to be done once we are done with draw text? Like restore stack? |
| 1125 | return kPartial_PdfResult; |
| 1126 | } |
| 1127 | |
| 1128 | //font size Tf Set the text font, Tf |
| 1129 | //, to font and the text font size, Tfs, to size. font is the name of a |
| 1130 | //font resource in the Fontsubdictionary of the current resource dictionary; size is |
| 1131 | //a number representing a scale factor. There is no initial value for either font or |
| 1132 | //size; they must be specified explicitly using Tf before any text is shown. |
| 1133 | PdfResult PdfOp_Tf(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1134 | pdfContext->fGraphicsState.fCurFontSize = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1135 | const char* fontName = pdfContext->fObjectStack.top()->nameValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1136 | |
| 1137 | #ifdef PDF_TRACE |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1138 | printf("font name: %s\n", fontName); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1139 | #endif |
| 1140 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1141 | if (pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)) { |
| 1142 | SkPdfObject* objFont = pdfContext->fGraphicsState.fResources->Font(pdfContext->fPdfDoc)->get(fontName); |
| 1143 | objFont = pdfContext->fPdfDoc->resolveReference(objFont); |
| 1144 | if (kNone_SkPdfObjectType == pdfContext->fPdfDoc->mapper()->mapFontDictionary(objFont)) { |
| 1145 | // TODO(edisonn): try to recover and draw it any way? |
| 1146 | return kIgnoreError_PdfResult; |
| 1147 | } |
| 1148 | SkPdfFontDictionary* fd = (SkPdfFontDictionary*)objFont; |
| 1149 | |
| 1150 | SkPdfFont* skfont = SkPdfFont::fontFromPdfDictionary(pdfContext->fPdfDoc, fd); |
| 1151 | |
| 1152 | if (skfont) { |
| 1153 | pdfContext->fGraphicsState.fSkFont = skfont; |
| 1154 | } |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1155 | } |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1156 | return kIgnoreError_PdfResult; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | PdfResult PdfOp_Tj(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1160 | if (!pdfContext->fGraphicsState.fTextBlock) { |
| 1161 | // TODO(edisonn): try to recover and draw it any way? |
| 1162 | return kIgnoreError_PdfResult; |
| 1163 | } |
| 1164 | |
| 1165 | PdfResult ret = DrawText(pdfContext, |
| 1166 | pdfContext->fObjectStack.top(), |
| 1167 | canvas); |
| 1168 | pdfContext->fObjectStack.pop(); |
| 1169 | |
| 1170 | return ret; |
| 1171 | } |
| 1172 | |
| 1173 | PdfResult PdfOp_quote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1174 | if (!pdfContext->fGraphicsState.fTextBlock) { |
| 1175 | // TODO(edisonn): try to recover and draw it any way? |
| 1176 | return kIgnoreError_PdfResult; |
| 1177 | } |
| 1178 | |
| 1179 | PdfOp_T_star(pdfContext, canvas, looper); |
| 1180 | // Do not pop, and push, just transfer the param to Tj |
| 1181 | return PdfOp_Tj(pdfContext, canvas, looper); |
| 1182 | } |
| 1183 | |
| 1184 | PdfResult PdfOp_doublequote(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1185 | if (!pdfContext->fGraphicsState.fTextBlock) { |
| 1186 | // TODO(edisonn): try to recover and draw it any way? |
| 1187 | return kIgnoreError_PdfResult; |
| 1188 | } |
| 1189 | |
| 1190 | SkPdfObject* str = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop(); |
| 1191 | SkPdfObject* ac = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop(); |
| 1192 | SkPdfObject* aw = pdfContext->fObjectStack.top(); pdfContext->fObjectStack.pop(); |
| 1193 | |
| 1194 | pdfContext->fObjectStack.push(aw); |
| 1195 | PdfOp_Tw(pdfContext, canvas, looper); |
| 1196 | |
| 1197 | pdfContext->fObjectStack.push(ac); |
| 1198 | PdfOp_Tc(pdfContext, canvas, looper); |
| 1199 | |
| 1200 | pdfContext->fObjectStack.push(str); |
| 1201 | PdfOp_quote(pdfContext, canvas, looper); |
| 1202 | |
| 1203 | return kPartial_PdfResult; |
| 1204 | } |
| 1205 | |
| 1206 | PdfResult PdfOp_TJ(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1207 | if (!pdfContext->fGraphicsState.fTextBlock) { |
| 1208 | // TODO(edisonn): try to recover and draw it any way? |
| 1209 | return kIgnoreError_PdfResult; |
| 1210 | } |
| 1211 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1212 | SkPdfArray* array = (SkPdfArray*)pdfContext->fObjectStack.top(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1213 | pdfContext->fObjectStack.pop(); |
| 1214 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1215 | if (!array->isArray()) { |
| 1216 | return kIgnoreError_PdfResult; |
| 1217 | } |
| 1218 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1219 | for( int i=0; i<static_cast<int>(array->size()); i++ ) |
| 1220 | { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1221 | if( (*array)[i]->isAnyString()) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1222 | SkPdfObject* obj = (*array)[i]; |
| 1223 | DrawText(pdfContext, |
| 1224 | obj, |
| 1225 | canvas); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1226 | } else if ((*array)[i]->isNumber()) { |
| 1227 | double dx = (*array)[i]->numberValue(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1228 | SkMatrix matrix; |
| 1229 | matrix.setAll(SkDoubleToScalar(1), |
| 1230 | SkDoubleToScalar(0), |
| 1231 | // TODO(edisonn): use writing mode, vertical/horizontal. |
| 1232 | SkDoubleToScalar(-dx), // amount is substracted!!! |
| 1233 | SkDoubleToScalar(0), |
| 1234 | SkDoubleToScalar(1), |
| 1235 | SkDoubleToScalar(0), |
| 1236 | SkDoubleToScalar(0), |
| 1237 | SkDoubleToScalar(0), |
| 1238 | SkDoubleToScalar(1)); |
| 1239 | |
| 1240 | pdfContext->fGraphicsState.fMatrixTm.preConcat(matrix); |
| 1241 | } |
| 1242 | } |
| 1243 | return kPartial_PdfResult; // TODO(edisonn): Implement fully DrawText before returing OK. |
| 1244 | } |
| 1245 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 1246 | PdfResult PdfOp_CS_cs(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1247 | colorOperator->fColorSpace = pdfContext->fObjectStack.top()->nameValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1248 | return kOK_PdfResult; |
| 1249 | } |
| 1250 | |
| 1251 | PdfResult PdfOp_CS(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1252 | return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking); |
| 1253 | } |
| 1254 | |
| 1255 | PdfResult PdfOp_cs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1256 | return PdfOp_CS_cs(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking); |
| 1257 | } |
| 1258 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 1259 | PdfResult PdfOp_SC_sc(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1260 | double c[4]; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1261 | // int64_t v[4]; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1262 | |
| 1263 | int n = GetColorSpaceComponents(colorOperator->fColorSpace); |
| 1264 | |
| 1265 | bool doubles = true; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1266 | if (strcmp(colorOperator->fColorSpace, "Indexed") == 0) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1267 | doubles = false; |
| 1268 | } |
| 1269 | |
| 1270 | #ifdef PDF_TRACE |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1271 | printf("color space = %s, N = %i\n", colorOperator->fColorSpace, n); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1272 | #endif |
| 1273 | |
| 1274 | for (int i = n - 1; i >= 0 ; i--) { |
| 1275 | if (doubles) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1276 | c[i] = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1277 | // } else { |
| 1278 | // v[i] = pdfContext->fObjectStack.top()->intValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | // TODO(edisonn): Now, set that color. Only DeviceRGB supported. |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1283 | // TODO(edisonn): do possible field values to enum at parsing time! |
| 1284 | // TODO(edisonn): support also abreviations /DeviceRGB == /RGB |
| 1285 | if (strcmp(colorOperator->fColorSpace, "DeviceRGB") == 0 || strcmp(colorOperator->fColorSpace, "RGB") == 0) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1286 | colorOperator->setRGBColor(SkColorSetRGB(255*c[0], 255*c[1], 255*c[2])); |
| 1287 | } |
| 1288 | return kPartial_PdfResult; |
| 1289 | } |
| 1290 | |
| 1291 | PdfResult PdfOp_SC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1292 | return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking); |
| 1293 | } |
| 1294 | |
| 1295 | PdfResult PdfOp_sc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1296 | return PdfOp_SC_sc(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking); |
| 1297 | } |
| 1298 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 1299 | PdfResult PdfOp_SCN_scn(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1300 | //SkPdfString* name; |
| 1301 | if (pdfContext->fObjectStack.top()->isName()) { |
| 1302 | // TODO(edisonn): get name, pass it |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1303 | pdfContext->fObjectStack.pop(); |
| 1304 | } |
| 1305 | |
| 1306 | // TODO(edisonn): SCN supports more color spaces than SCN. Read and implement spec. |
| 1307 | PdfOp_SC_sc(pdfContext, canvas, colorOperator); |
| 1308 | |
| 1309 | return kPartial_PdfResult; |
| 1310 | } |
| 1311 | |
| 1312 | PdfResult PdfOp_SCN(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1313 | return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking); |
| 1314 | } |
| 1315 | |
| 1316 | PdfResult PdfOp_scn(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1317 | return PdfOp_SCN_scn(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking); |
| 1318 | } |
| 1319 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 1320 | PdfResult PdfOp_G_g(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1321 | /*double gray = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1322 | return kNYI_PdfResult; |
| 1323 | } |
| 1324 | |
| 1325 | PdfResult PdfOp_G(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1326 | return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking); |
| 1327 | } |
| 1328 | |
| 1329 | PdfResult PdfOp_g(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1330 | return PdfOp_G_g(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking); |
| 1331 | } |
| 1332 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 1333 | PdfResult PdfOp_RG_rg(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1334 | double b = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1335 | double g = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1336 | double r = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1337 | |
| 1338 | colorOperator->fColorSpace = "DeviceRGB"; |
| 1339 | colorOperator->setRGBColor(SkColorSetRGB(255*r, 255*g, 255*b)); |
| 1340 | return kOK_PdfResult; |
| 1341 | } |
| 1342 | |
| 1343 | PdfResult PdfOp_RG(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1344 | return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking); |
| 1345 | } |
| 1346 | |
| 1347 | PdfResult PdfOp_rg(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1348 | return PdfOp_RG_rg(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking); |
| 1349 | } |
| 1350 | |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 1351 | PdfResult PdfOp_K_k(PdfContext* pdfContext, SkCanvas* canvas, SkPdfColorOperator* colorOperator) { |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1352 | // TODO(edisonn): spec has some rules about overprint, implement them. |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1353 | /*double k = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1354 | /*double y = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1355 | /*double m = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
| 1356 | /*double c = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1357 | |
| 1358 | colorOperator->fColorSpace = "DeviceCMYK"; |
| 1359 | // TODO(edisonn): Set color. |
| 1360 | return kNYI_PdfResult; |
| 1361 | } |
| 1362 | |
| 1363 | PdfResult PdfOp_K(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1364 | return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fStroking); |
| 1365 | } |
| 1366 | |
| 1367 | PdfResult PdfOp_k(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1368 | return PdfOp_K_k(pdfContext, canvas, &pdfContext->fGraphicsState.fNonStroking); |
| 1369 | } |
| 1370 | |
| 1371 | PdfResult PdfOp_W(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1372 | pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath; |
| 1373 | pdfContext->fGraphicsState.fHasClipPathToApply = true; |
| 1374 | |
| 1375 | return kOK_PdfResult; |
| 1376 | } |
| 1377 | |
| 1378 | PdfResult PdfOp_W_star(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1379 | pdfContext->fGraphicsState.fClipPath = pdfContext->fGraphicsState.fPath; |
| 1380 | |
| 1381 | #ifdef PDF_TRACE |
| 1382 | if (pdfContext->fGraphicsState.fClipPath.isRect(NULL)) { |
| 1383 | printf("CLIP IS RECT\n"); |
| 1384 | } |
| 1385 | #endif |
| 1386 | |
| 1387 | // TODO(edisonn): there seem to be a bug with clipPath of a rect with even odd. |
| 1388 | pdfContext->fGraphicsState.fClipPath.setFillType(SkPath::kEvenOdd_FillType); |
| 1389 | pdfContext->fGraphicsState.fHasClipPathToApply = true; |
| 1390 | |
| 1391 | return kPartial_PdfResult; |
| 1392 | } |
| 1393 | |
| 1394 | PdfResult PdfOp_BX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1395 | *looper = new PdfCompatibilitySectionLooper(); |
| 1396 | return kOK_PdfResult; |
| 1397 | } |
| 1398 | |
| 1399 | PdfResult PdfOp_EX(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1400 | #ifdef ASSERT_BAD_PDF_OPS |
| 1401 | SkASSERT(false); // EX must be consumed by PdfCompatibilitySectionLooper, but let's |
| 1402 | // have the assert when testing good pdfs. |
| 1403 | #endif |
| 1404 | return kIgnoreError_PdfResult; |
| 1405 | } |
| 1406 | |
| 1407 | PdfResult PdfOp_BI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1408 | *looper = new PdfInlineImageLooper(); |
| 1409 | return kOK_PdfResult; |
| 1410 | } |
| 1411 | |
| 1412 | PdfResult PdfOp_ID(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1413 | #ifdef ASSERT_BAD_PDF_OPS |
| 1414 | SkASSERT(false); // must be processed in inline image looper, but let's |
| 1415 | // have the assert when testing good pdfs. |
| 1416 | #endif |
| 1417 | return kIgnoreError_PdfResult; |
| 1418 | } |
| 1419 | |
| 1420 | PdfResult PdfOp_EI(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1421 | #ifdef ASSERT_BAD_PDF_OPS |
| 1422 | SkASSERT(false); // must be processed in inline image looper, but let's |
| 1423 | // have the assert when testing good pdfs. |
| 1424 | #endif |
| 1425 | return kIgnoreError_PdfResult; |
| 1426 | } |
| 1427 | |
| 1428 | //lineWidth w Set the line width in the graphics state (see “Line Width” on page 152). |
| 1429 | PdfResult PdfOp_w(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1430 | double lineWidth = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1431 | pdfContext->fGraphicsState.fLineWidth = lineWidth; |
| 1432 | |
| 1433 | return kOK_PdfResult; |
| 1434 | } |
| 1435 | |
| 1436 | //lineCap J Set the line cap style in the graphics state (see “Line Cap Style” on page 153). |
| 1437 | PdfResult PdfOp_J(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1438 | pdfContext->fObjectStack.pop(); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1439 | //double lineCap = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1440 | |
| 1441 | return kNYI_PdfResult; |
| 1442 | } |
| 1443 | |
| 1444 | //lineJoin j Set the line join style in the graphics state (see “Line Join Style” on page 153). |
| 1445 | PdfResult PdfOp_j(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1446 | pdfContext->fObjectStack.pop(); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1447 | //double lineJoin = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1448 | |
| 1449 | return kNYI_PdfResult; |
| 1450 | } |
| 1451 | |
| 1452 | //miterLimit M Set the miter limit in the graphics state (see “Miter Limit” on page 153). |
| 1453 | PdfResult PdfOp_M(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1454 | pdfContext->fObjectStack.pop(); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1455 | //double miterLimit = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1456 | |
| 1457 | return kNYI_PdfResult; |
| 1458 | } |
| 1459 | |
| 1460 | //dashArray dashPhase d Set the line dash pattern in the graphics state (see “Line Dash Pattern” on |
| 1461 | //page 155). |
| 1462 | PdfResult PdfOp_d(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1463 | pdfContext->fObjectStack.pop(); |
| 1464 | pdfContext->fObjectStack.pop(); |
| 1465 | |
| 1466 | return kNYI_PdfResult; |
| 1467 | } |
| 1468 | |
| 1469 | //intent ri (PDF 1.1) Set the color rendering intent in the graphics state (see “Rendering Intents” on page 197). |
| 1470 | PdfResult PdfOp_ri(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1471 | pdfContext->fObjectStack.pop(); |
| 1472 | |
| 1473 | return kNYI_PdfResult; |
| 1474 | } |
| 1475 | |
| 1476 | //flatness i Set the flatness tolerance in the graphics state (see Section 6.5.1, “Flatness |
| 1477 | //Tolerance”). flatness is a number in the range 0 to 100; a value of 0 speci- |
| 1478 | //fies the output device’s default flatness tolerance. |
| 1479 | PdfResult PdfOp_i(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1480 | pdfContext->fObjectStack.pop(); |
| 1481 | |
| 1482 | return kNYI_PdfResult; |
| 1483 | } |
| 1484 | |
| 1485 | //dictName gs (PDF 1.2) Set the specified parameters in the graphics state. dictName is |
| 1486 | //the name of a graphics state parameter dictionary in the ExtGState subdictionary of the current resource dictionary (see the next section). |
| 1487 | PdfResult PdfOp_gs(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1488 | const char* name = pdfContext->fObjectStack.top()->nameValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1489 | |
| 1490 | #ifdef PDF_TRACE |
| 1491 | std::string str; |
| 1492 | #endif |
| 1493 | |
| 1494 | //Next, get the ExtGState Dictionary from the Resource Dictionary: |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1495 | SkPdfDictionary* extGStateDictionary = pdfContext->fGraphicsState.fResources->ExtGState(pdfContext->fPdfDoc); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1496 | |
| 1497 | if (extGStateDictionary == NULL) { |
| 1498 | #ifdef PDF_TRACE |
| 1499 | printf("ExtGState is NULL!\n"); |
| 1500 | #endif |
| 1501 | return kIgnoreError_PdfResult; |
| 1502 | } |
| 1503 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1504 | SkPdfObject* value = pdfContext->fPdfDoc->resolveReference(extGStateDictionary->get(name)); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1505 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1506 | if (kNone_SkPdfObjectType == pdfContext->fPdfDoc->mapper()->mapGraphicsStateDictionary(value)) { |
| 1507 | return kIgnoreError_PdfResult; |
| 1508 | } |
| 1509 | SkPdfGraphicsStateDictionary* gs = (SkPdfGraphicsStateDictionary*)value; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1510 | |
| 1511 | // TODO(edisonn): now load all those properties in graphic state. |
| 1512 | if (gs == NULL) { |
| 1513 | return kIgnoreError_PdfResult; |
| 1514 | } |
| 1515 | |
| 1516 | if (gs->has_CA()) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1517 | pdfContext->fGraphicsState.fStroking.fOpacity = gs->CA(pdfContext->fPdfDoc); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | if (gs->has_ca()) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1521 | pdfContext->fGraphicsState.fNonStroking.fOpacity = gs->ca(pdfContext->fPdfDoc); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
| 1524 | if (gs->has_LW()) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1525 | pdfContext->fGraphicsState.fLineWidth = gs->LW(pdfContext->fPdfDoc); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1528 | return kNYI_PdfResult; |
| 1529 | } |
| 1530 | |
| 1531 | //charSpace Tc Set the character spacing, Tc |
| 1532 | //, to charSpace, which is a number expressed in unscaled text space units. Character spacing is used by the Tj, TJ, and ' operators. |
| 1533 | //Initial value: 0. |
| 1534 | PdfResult PdfOp_Tc(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1535 | double charSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1536 | pdfContext->fGraphicsState.fCharSpace = charSpace; |
| 1537 | |
| 1538 | return kOK_PdfResult; |
| 1539 | } |
| 1540 | |
| 1541 | //wordSpace Tw Set the word spacing, T |
| 1542 | //w |
| 1543 | //, to wordSpace, which is a number expressed in unscaled |
| 1544 | //text space units. Word spacing is used by the Tj, TJ, and ' operators. Initial |
| 1545 | //value: 0. |
| 1546 | PdfResult PdfOp_Tw(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1547 | double wordSpace = pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1548 | pdfContext->fGraphicsState.fWordSpace = wordSpace; |
| 1549 | |
| 1550 | return kOK_PdfResult; |
| 1551 | } |
| 1552 | |
| 1553 | //scale Tz Set the horizontal scaling, Th |
| 1554 | //, to (scale ˜ 100). scale is a number specifying the |
| 1555 | //percentage of the normal width. Initial value: 100 (normal width). |
| 1556 | PdfResult PdfOp_Tz(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1557 | /*double scale = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1558 | |
| 1559 | return kNYI_PdfResult; |
| 1560 | } |
| 1561 | |
| 1562 | //render Tr Set the text rendering mode, T |
| 1563 | //mode, to render, which is an integer. Initial value: 0. |
| 1564 | PdfResult PdfOp_Tr(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1565 | /*double render = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1566 | |
| 1567 | return kNYI_PdfResult; |
| 1568 | } |
| 1569 | //rise Ts Set the text rise, Trise, to rise, which is a number expressed in unscaled text space |
| 1570 | //units. Initial value: 0. |
| 1571 | PdfResult PdfOp_Ts(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1572 | /*double rise = */pdfContext->fObjectStack.top()->numberValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1573 | |
| 1574 | return kNYI_PdfResult; |
| 1575 | } |
| 1576 | |
| 1577 | //wx wy d0 |
| 1578 | PdfResult PdfOp_d0(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1579 | pdfContext->fObjectStack.pop(); |
| 1580 | pdfContext->fObjectStack.pop(); |
| 1581 | |
| 1582 | return kNYI_PdfResult; |
| 1583 | } |
| 1584 | |
| 1585 | //wx wy llx lly urx ury d1 |
| 1586 | PdfResult PdfOp_d1(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1587 | pdfContext->fObjectStack.pop(); |
| 1588 | pdfContext->fObjectStack.pop(); |
| 1589 | pdfContext->fObjectStack.pop(); |
| 1590 | pdfContext->fObjectStack.pop(); |
| 1591 | pdfContext->fObjectStack.pop(); |
| 1592 | pdfContext->fObjectStack.pop(); |
| 1593 | |
| 1594 | return kNYI_PdfResult; |
| 1595 | } |
| 1596 | |
| 1597 | //name sh |
| 1598 | PdfResult PdfOp_sh(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1599 | pdfContext->fObjectStack.pop(); |
| 1600 | |
| 1601 | return kNYI_PdfResult; |
| 1602 | } |
| 1603 | |
| 1604 | //name Do |
| 1605 | PdfResult PdfOp_Do(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1606 | const char* name = pdfContext->fObjectStack.top()->nameValue(); pdfContext->fObjectStack.pop(); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1607 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1608 | SkPdfDictionary* xObject = pdfContext->fGraphicsState.fResources->XObject(pdfContext->fPdfDoc); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1609 | |
| 1610 | if (xObject == NULL) { |
| 1611 | #ifdef PDF_TRACE |
| 1612 | printf("XObject is NULL!\n"); |
| 1613 | #endif |
| 1614 | return kIgnoreError_PdfResult; |
| 1615 | } |
| 1616 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1617 | SkPdfObject* value = xObject->get(name); |
| 1618 | value = pdfContext->fPdfDoc->resolveReference(value); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1619 | |
| 1620 | #ifdef PDF_TRACE |
| 1621 | // value->ToString(str); |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1622 | // printf("Do object value: %s\n", str); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1623 | #endif |
| 1624 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1625 | return doXObject(pdfContext, canvas, value); |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | //tag MP Designate a marked-content point. tag is a name object indicating the role or |
| 1629 | //significance of the point. |
| 1630 | PdfResult PdfOp_MP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1631 | pdfContext->fObjectStack.pop(); |
| 1632 | |
| 1633 | return kNYI_PdfResult; |
| 1634 | } |
| 1635 | |
| 1636 | //tag properties DP Designate a marked-content point with an associated property list. tag is a |
| 1637 | //name object indicating the role or significance of the point; properties is |
| 1638 | //either an inline dictionary containing the property list or a name object |
| 1639 | //associated with it in the Properties subdictionary of the current resource |
| 1640 | //dictionary (see Section 9.5.1, “Property Lists”). |
| 1641 | PdfResult PdfOp_DP(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1642 | pdfContext->fObjectStack.pop(); |
| 1643 | pdfContext->fObjectStack.pop(); |
| 1644 | |
| 1645 | return kNYI_PdfResult; |
| 1646 | } |
| 1647 | |
| 1648 | //tag BMC Begin a marked-content sequence terminated by a balancing EMC operator. |
| 1649 | //tag is a name object indicating the role or significance of the sequence. |
| 1650 | PdfResult PdfOp_BMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1651 | pdfContext->fObjectStack.pop(); |
| 1652 | |
| 1653 | return kNYI_PdfResult; |
| 1654 | } |
| 1655 | |
| 1656 | //tag properties BDC Begin a marked-content sequence with an associated property list, terminated |
| 1657 | //by a balancing EMCoperator. tag is a name object indicating the role or significance of the sequence; propertiesis either an inline dictionary containing the |
| 1658 | //property list or a name object associated with it in the Properties subdictionary of the current resource dictionary (see Section 9.5.1, “Property Lists”). |
| 1659 | PdfResult PdfOp_BDC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1660 | pdfContext->fObjectStack.pop(); |
| 1661 | pdfContext->fObjectStack.pop(); |
| 1662 | |
| 1663 | return kNYI_PdfResult; |
| 1664 | } |
| 1665 | |
| 1666 | //— EMC End a marked-content sequence begun by a BMC or BDC operator. |
| 1667 | PdfResult PdfOp_EMC(PdfContext* pdfContext, SkCanvas* canvas, PdfTokenLooper** looper) { |
| 1668 | return kNYI_PdfResult; |
| 1669 | } |
| 1670 | |
| 1671 | void initPdfOperatorRenderes() { |
| 1672 | static bool gInitialized = false; |
| 1673 | if (gInitialized) { |
| 1674 | return; |
| 1675 | } |
| 1676 | |
| 1677 | gPdfOps["q"] = PdfOp_q; |
| 1678 | gPdfOps["Q"] = PdfOp_Q; |
| 1679 | gPdfOps["cm"] = PdfOp_cm; |
| 1680 | |
| 1681 | gPdfOps["TD"] = PdfOp_TD; |
| 1682 | gPdfOps["Td"] = PdfOp_Td; |
| 1683 | gPdfOps["Tm"] = PdfOp_Tm; |
| 1684 | gPdfOps["T*"] = PdfOp_T_star; |
| 1685 | |
| 1686 | gPdfOps["m"] = PdfOp_m; |
| 1687 | gPdfOps["l"] = PdfOp_l; |
| 1688 | gPdfOps["c"] = PdfOp_c; |
| 1689 | gPdfOps["v"] = PdfOp_v; |
| 1690 | gPdfOps["y"] = PdfOp_y; |
| 1691 | gPdfOps["h"] = PdfOp_h; |
| 1692 | gPdfOps["re"] = PdfOp_re; |
| 1693 | |
| 1694 | gPdfOps["S"] = PdfOp_S; |
| 1695 | gPdfOps["s"] = PdfOp_s; |
| 1696 | gPdfOps["f"] = PdfOp_f; |
| 1697 | gPdfOps["F"] = PdfOp_F; |
| 1698 | gPdfOps["f*"] = PdfOp_f_star; |
| 1699 | gPdfOps["B"] = PdfOp_B; |
| 1700 | gPdfOps["B*"] = PdfOp_B_star; |
| 1701 | gPdfOps["b"] = PdfOp_b; |
| 1702 | gPdfOps["b*"] = PdfOp_b_star; |
| 1703 | gPdfOps["n"] = PdfOp_n; |
| 1704 | |
| 1705 | gPdfOps["BT"] = PdfOp_BT; |
| 1706 | gPdfOps["ET"] = PdfOp_ET; |
| 1707 | |
| 1708 | gPdfOps["Tj"] = PdfOp_Tj; |
| 1709 | gPdfOps["'"] = PdfOp_quote; |
| 1710 | gPdfOps["\""] = PdfOp_doublequote; |
| 1711 | gPdfOps["TJ"] = PdfOp_TJ; |
| 1712 | |
| 1713 | gPdfOps["CS"] = PdfOp_CS; |
| 1714 | gPdfOps["cs"] = PdfOp_cs; |
| 1715 | gPdfOps["SC"] = PdfOp_SC; |
| 1716 | gPdfOps["SCN"] = PdfOp_SCN; |
| 1717 | gPdfOps["sc"] = PdfOp_sc; |
| 1718 | gPdfOps["scn"] = PdfOp_scn; |
| 1719 | gPdfOps["G"] = PdfOp_G; |
| 1720 | gPdfOps["g"] = PdfOp_g; |
| 1721 | gPdfOps["RG"] = PdfOp_RG; |
| 1722 | gPdfOps["rg"] = PdfOp_rg; |
| 1723 | gPdfOps["K"] = PdfOp_K; |
| 1724 | gPdfOps["k"] = PdfOp_k; |
| 1725 | |
| 1726 | gPdfOps["W"] = PdfOp_W; |
| 1727 | gPdfOps["W*"] = PdfOp_W_star; |
| 1728 | |
| 1729 | gPdfOps["BX"] = PdfOp_BX; |
| 1730 | gPdfOps["EX"] = PdfOp_EX; |
| 1731 | |
| 1732 | gPdfOps["BI"] = PdfOp_BI; |
| 1733 | gPdfOps["ID"] = PdfOp_ID; |
| 1734 | gPdfOps["EI"] = PdfOp_EI; |
| 1735 | |
| 1736 | gPdfOps["w"] = PdfOp_w; |
| 1737 | gPdfOps["J"] = PdfOp_J; |
| 1738 | gPdfOps["j"] = PdfOp_j; |
| 1739 | gPdfOps["M"] = PdfOp_M; |
| 1740 | gPdfOps["d"] = PdfOp_d; |
| 1741 | gPdfOps["ri"] = PdfOp_ri; |
| 1742 | gPdfOps["i"] = PdfOp_i; |
| 1743 | gPdfOps["gs"] = PdfOp_gs; |
| 1744 | |
| 1745 | gPdfOps["Tc"] = PdfOp_Tc; |
| 1746 | gPdfOps["Tw"] = PdfOp_Tw; |
| 1747 | gPdfOps["Tz"] = PdfOp_Tz; |
| 1748 | gPdfOps["TL"] = PdfOp_TL; |
| 1749 | gPdfOps["Tf"] = PdfOp_Tf; |
| 1750 | gPdfOps["Tr"] = PdfOp_Tr; |
| 1751 | gPdfOps["Ts"] = PdfOp_Ts; |
| 1752 | |
| 1753 | gPdfOps["d0"] = PdfOp_d0; |
| 1754 | gPdfOps["d1"] = PdfOp_d1; |
| 1755 | |
| 1756 | gPdfOps["sh"] = PdfOp_sh; |
| 1757 | |
| 1758 | gPdfOps["Do"] = PdfOp_Do; |
| 1759 | |
| 1760 | gPdfOps["MP"] = PdfOp_MP; |
| 1761 | gPdfOps["DP"] = PdfOp_DP; |
| 1762 | gPdfOps["BMC"] = PdfOp_BMC; |
| 1763 | gPdfOps["BDC"] = PdfOp_BDC; |
| 1764 | gPdfOps["EMC"] = PdfOp_EMC; |
| 1765 | |
| 1766 | gInitialized = true; |
| 1767 | } |
| 1768 | |
| 1769 | class InitPdfOps { |
| 1770 | public: |
| 1771 | InitPdfOps() { |
| 1772 | initPdfOperatorRenderes(); |
| 1773 | } |
| 1774 | }; |
| 1775 | |
| 1776 | InitPdfOps gInitPdfOps; |
| 1777 | |
| 1778 | void reportPdfRenderStats() { |
| 1779 | std::map<std::string, int>::iterator iter; |
| 1780 | |
| 1781 | for (int i = 0 ; i < kCount_PdfResult; i++) { |
| 1782 | for (iter = gRenderStats[i].begin(); iter != gRenderStats[i].end(); ++iter) { |
| 1783 | printf("%s: %s -> count %i\n", gRenderStatsNames[i], iter->first.c_str(), iter->second); |
| 1784 | } |
| 1785 | } |
| 1786 | } |
| 1787 | |
| 1788 | PdfResult PdfMainLooper::consumeToken(PdfToken& token) { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1789 | char keyword[256]; |
| 1790 | |
| 1791 | if (token.fType == kKeyword_TokenType && token.fKeywordLength < 256) |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1792 | { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1793 | strncpy(keyword, token.fKeyword, token.fKeywordLength); |
| 1794 | keyword[token.fKeywordLength] = '\0'; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1795 | // TODO(edisonn): log trace flag (verbose, error, info, warning, ...) |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1796 | PdfOperatorRenderer pdfOperatorRenderer = gPdfOps[keyword]; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1797 | if (pdfOperatorRenderer) { |
| 1798 | // caller, main work is done by pdfOperatorRenderer(...) |
| 1799 | PdfTokenLooper* childLooper = NULL; |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1800 | gRenderStats[pdfOperatorRenderer(fPdfContext, fCanvas, &childLooper)][keyword]++; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1801 | |
| 1802 | if (childLooper) { |
| 1803 | childLooper->setUp(this); |
| 1804 | childLooper->loop(); |
| 1805 | delete childLooper; |
| 1806 | } |
| 1807 | } else { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1808 | gRenderStats[kUnsupported_PdfResult][keyword]++; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1809 | } |
| 1810 | } |
| 1811 | else if (token.fType == kObject_TokenType) |
| 1812 | { |
| 1813 | fPdfContext->fObjectStack.push( token.fObject ); |
| 1814 | } |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1815 | else { |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1816 | // TODO(edisonn): deine or use assert not reached |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1817 | return kIgnoreError_PdfResult; |
| 1818 | } |
| 1819 | return kOK_PdfResult; |
| 1820 | } |
| 1821 | |
| 1822 | void PdfMainLooper::loop() { |
| 1823 | PdfToken token; |
| 1824 | while (readToken(fTokenizer, &token)) { |
| 1825 | consumeToken(token); |
| 1826 | } |
| 1827 | } |
| 1828 | |
| 1829 | PdfResult PdfInlineImageLooper::consumeToken(PdfToken& token) { |
| 1830 | //pdfContext.fInlineImage.fKeyValuePairs[key] = value; |
| 1831 | return kNYI_PdfResult; |
| 1832 | } |
| 1833 | |
| 1834 | void PdfInlineImageLooper::loop() { |
| 1835 | PdfToken token; |
| 1836 | while (readToken(fTokenizer, &token)) { |
| 1837 | if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "BX") == 0) { |
| 1838 | PdfTokenLooper* looper = new PdfCompatibilitySectionLooper(); |
| 1839 | looper->setUp(this); |
| 1840 | looper->loop(); |
| 1841 | } else { |
| 1842 | if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "EI") == 0) { |
| 1843 | done(); |
| 1844 | return; |
| 1845 | } |
| 1846 | |
| 1847 | consumeToken(token); |
| 1848 | } |
| 1849 | } |
| 1850 | // TODO(edisonn): report error/warning, EOF without EI. |
| 1851 | } |
| 1852 | |
| 1853 | PdfResult PdfInlineImageLooper::done() { |
| 1854 | |
| 1855 | // TODO(edisonn): long to short names |
| 1856 | // TODO(edisonn): set properties in a map |
| 1857 | // TODO(edisonn): extract bitmap stream, check if PoDoFo has public utilities to uncompress |
| 1858 | // the stream. |
| 1859 | |
| 1860 | SkBitmap bitmap; |
| 1861 | setup_bitmap(&bitmap, 50, 50, SK_ColorRED); |
| 1862 | |
| 1863 | // TODO(edisonn): matrix use. |
| 1864 | // Draw dummy red square, to show the prezence of the inline image. |
| 1865 | fCanvas->drawBitmap(bitmap, |
| 1866 | SkDoubleToScalar(0), |
| 1867 | SkDoubleToScalar(0), |
| 1868 | NULL); |
| 1869 | return kNYI_PdfResult; |
| 1870 | } |
| 1871 | |
| 1872 | PdfResult PdfCompatibilitySectionLooper::consumeToken(PdfToken& token) { |
| 1873 | return fParent->consumeToken(token); |
| 1874 | } |
| 1875 | |
| 1876 | void PdfCompatibilitySectionLooper::loop() { |
| 1877 | // TODO(edisonn): save stacks position, or create a new stack? |
| 1878 | // TODO(edisonn): what happens if we pop out more variables then when we started? |
| 1879 | // restore them? fail? We could create a new operands stack for every new BX/EX section, |
| 1880 | // pop-ing too much will not affect outside the section. |
| 1881 | PdfToken token; |
| 1882 | while (readToken(fTokenizer, &token)) { |
| 1883 | if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "BX") == 0) { |
| 1884 | PdfTokenLooper* looper = new PdfCompatibilitySectionLooper(); |
| 1885 | looper->setUp(this); |
| 1886 | looper->loop(); |
| 1887 | delete looper; |
| 1888 | } else { |
| 1889 | if (token.fType == kKeyword_TokenType && strcmp(token.fKeyword, "EX") == 0) break; |
| 1890 | fParent->consumeToken(token); |
| 1891 | } |
| 1892 | } |
| 1893 | // TODO(edisonn): restore stack. |
| 1894 | } |
| 1895 | |
| 1896 | // TODO(edisonn): fix PoDoFo load ~/crashing/Shading.pdf |
| 1897 | // TODO(edisonn): Add API for Forms viewing and editing |
| 1898 | // e.g. SkBitmap getPage(int page); |
| 1899 | // int formsCount(); |
| 1900 | // SkForm getForm(int formID); // SkForm(SkRect, .. other data) |
| 1901 | // TODO (edisonn): Add intend when loading pdf, for example: for viewing, parsing all content, ... |
| 1902 | // if we load the first page, and we zoom to fit to screen horizontally, then load only those |
| 1903 | // resources needed, so the preview is fast. |
| 1904 | // TODO (edisonn): hide parser/tokenizer behind and interface and a query language, and resolve |
| 1905 | // references automatically. |
| 1906 | |
| 1907 | bool SkPdfViewer::load(const SkString inputFileName, SkPicture* out) { |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 1908 | std::cout << "Init: " << inputFileName.c_str() << std::endl; |
| 1909 | |
edisonn@google.com | 571c70b | 2013-07-10 17:09:50 +0000 | [diff] [blame] | 1910 | SkNativeParsedPDF* doc = new SkNativeParsedPDF(inputFileName.c_str()); |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 1911 | if (!doc->pages()) |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1912 | { |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 1913 | std::cout << "ERROR: Empty Document" << inputFileName.c_str() << std::endl; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1914 | return false; |
edisonn@google.com | 3aac1f9 | 2013-07-02 22:42:53 +0000 | [diff] [blame] | 1915 | } else { |
| 1916 | |
| 1917 | for (int pn = 0; pn < doc->pages(); ++pn) { |
| 1918 | // TODO(edisonn): implement inheritance properties as per PDF spec |
| 1919 | //SkRect rect = page->MediaBox(); |
| 1920 | SkRect rect = doc->MediaBox(pn); |
| 1921 | |
| 1922 | #ifdef PDF_TRACE |
| 1923 | printf("Page Width: %f, Page Height: %f\n", SkScalarToDouble(rect.width()), SkScalarToDouble(rect.height())); |
| 1924 | #endif |
| 1925 | |
| 1926 | // TODO(edisonn): page->GetCropBox(), page->GetTrimBox() ... how to use? |
| 1927 | |
| 1928 | SkBitmap bitmap; |
| 1929 | #ifdef PDF_DEBUG_3X |
| 1930 | setup_bitmap(&bitmap, 3 * (int)SkScalarToDouble(rect.width()), 3 * (int)SkScalarToDouble(rect.height())); |
| 1931 | #else |
| 1932 | setup_bitmap(&bitmap, (int)SkScalarToDouble(rect.width()), (int)SkScalarToDouble(rect.height())); |
| 1933 | #endif |
| 1934 | SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (bitmap))); |
| 1935 | SkCanvas canvas(device); |
| 1936 | |
| 1937 | gDumpBitmap = &bitmap; |
| 1938 | |
| 1939 | gDumpCanvas = &canvas; |
| 1940 | doc->drawPage(pn, &canvas); |
| 1941 | |
| 1942 | SkString out; |
| 1943 | if (doc->pages() > 1) { |
| 1944 | out.appendf("%s-%i.png", inputFileName.c_str(), pn); |
| 1945 | } else { |
| 1946 | out = inputFileName; |
| 1947 | // .pdf -> .png |
| 1948 | out[out.size() - 2] = 'n'; |
| 1949 | out[out.size() - 1] = 'g'; |
| 1950 | } |
| 1951 | SkImageEncoder::EncodeFile(out.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100); |
| 1952 | } |
| 1953 | return true; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
| 1956 | return true; |
| 1957 | } |