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