| OLD | NEW |
| (Empty) |
| 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 "SkPdfTokenLooper.h" | |
| 9 #include "SkPdfNativeTokenizer.h" | |
| 10 #include "SkBitmap.h" | |
| 11 | |
| 12 #ifdef PDF_TRACE_DIFF_IN_PNG | |
| 13 #include "SkBitmapDevice.h" | |
| 14 #include "SkCanvas.h" | |
| 15 #include "SkClipStack.h" | |
| 16 #include "SkColor.h" | |
| 17 #include "SkImageEncoder.h" | |
| 18 #include "SkPaint.h" | |
| 19 #include "SkPath.h" | |
| 20 #include "SkRegion.h" | |
| 21 #include "SkScalar.h" | |
| 22 #include "SkString.h" | |
| 23 #endif // PDF_TRACE_DIFF_IN_PNG | |
| 24 | |
| 25 // FIXME (scroggo): Put behind build flags. | |
| 26 extern "C" SkBitmap* gDumpBitmap; | |
| 27 extern "C" SkCanvas* gDumpCanvas; | |
| 28 SkBitmap* gDumpBitmap = NULL; | |
| 29 SkCanvas* gDumpCanvas = NULL; | |
| 30 int gReadOp; | |
| 31 int gLastOpKeyword; | |
| 32 char gLastKeyword[100] = ""; | |
| 33 | |
| 34 #ifdef PDF_TRACE_DIFF_IN_PNG | |
| 35 // FIXME (scroggo): allOpWithVisualEffects can be local to hasVisualEffect. | |
| 36 char allOpWithVisualEffects[100] = ",S,s,f,F,f*,B,B*,b,b*,n,Tj,TJ,\',\",d0,d1,sh
,EI,Do,EX,"; | |
| 37 // FIXME (scroggo): has_visual_effect | |
| 38 static bool hasVisualEffect(const char* pdfOp) { | |
| 39 return true; | |
| 40 if (*pdfOp == '\0') return false; | |
| 41 | |
| 42 char markedPdfOp[100] = ","; | |
| 43 strcat(markedPdfOp, pdfOp); | |
| 44 strcat(markedPdfOp, ","); | |
| 45 | |
| 46 return (strstr(allOpWithVisualEffects, markedPdfOp) != NULL); | |
| 47 } | |
| 48 | |
| 49 static void setup_bitmap(SkBitmap* bitmap, int width, int height) { | |
| 50 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height); | |
| 51 | |
| 52 bitmap->allocPixels(); | |
| 53 bitmap->eraseColor(SK_ColorWHITE); | |
| 54 } | |
| 55 | |
| 56 #endif // PDF_TRACE_DIFF_IN_PNG | |
| 57 | |
| 58 // FIXME (scroggo): fTokenizer -> tokenizer. | |
| 59 bool readToken(SkPdfNativeTokenizer* fTokenizer, PdfToken* token) { | |
| 60 bool ret = fTokenizer->readToken(token); | |
| 61 | |
| 62 gReadOp++; | |
| 63 gLastOpKeyword++; | |
| 64 #ifdef PDF_TRACE_DIFF_IN_PNG | |
| 65 // TODO(edisonn): this code is used to make a step by step history of all th
e draw operations | |
| 66 // so we could find the step where something is wrong. | |
| 67 if (gLastKeyword[0] && hasVisualEffect(gLastKeyword)) { | |
| 68 gDumpCanvas->flush(); | |
| 69 | |
| 70 // FIXME (scroggo): Could use SkSurface/SkImage? | |
| 71 SkBitmap bitmap; | |
| 72 setup_bitmap(&bitmap, gDumpBitmap->width(), gDumpBitmap->height()); | |
| 73 | |
| 74 memcpy(bitmap.getPixels(), gDumpBitmap->getPixels(), gDumpBitmap->getSiz
e()); | |
| 75 | |
| 76 SkAutoTUnref<SkBaseDevice> device(SkNEW_ARGS(SkBitmapDevice, (bitmap))); | |
| 77 SkCanvas canvas(device); | |
| 78 | |
| 79 // draw context stuff here | |
| 80 SkPaint blueBorder; | |
| 81 blueBorder.setColor(SK_ColorBLUE); | |
| 82 blueBorder.setStyle(SkPaint::kStroke_Style); | |
| 83 blueBorder.setTextSize(SkDoubleToScalar(20)); | |
| 84 | |
| 85 SkString str; | |
| 86 | |
| 87 const SkClipStack* clipStack = gDumpCanvas->getClipStack(); | |
| 88 if (clipStack) { | |
| 89 SkClipStack::Iter iter(*clipStack, SkClipStack::Iter::kBottom_IterSt
art); | |
| 90 const SkClipStack::Element* elem; | |
| 91 double y = 0; | |
| 92 int total = 0; | |
| 93 while ((elem = iter.next()) != NULL) { | |
| 94 total++; | |
| 95 y += 30; | |
| 96 | |
| 97 switch (elem->getType()) { | |
| 98 case SkClipStack::Element::kRect_Type: | |
| 99 canvas.drawRect(elem->getRect(), blueBorder); | |
| 100 canvas.drawText("Rect Clip", strlen("Rect Clip"), | |
| 101 SkDoubleToScalar(10), SkDoubleToScalar(y
), blueBorder); | |
| 102 break; | |
| 103 case SkClipStack::Element::kPath_Type: | |
| 104 canvas.drawPath(elem->getPath(), blueBorder); | |
| 105 canvas.drawText("Path Clip", strlen("Path Clip"), | |
| 106 SkDoubleToScalar(10), SkDoubleToScalar(y
), blueBorder); | |
| 107 break; | |
| 108 case SkClipStack::Element::kEmpty_Type: | |
| 109 canvas.drawText("Empty Clip!!!", strlen("Empty Clip!!!")
, | |
| 110 SkDoubleToScalar(10), SkDoubleToScalar(y
), blueBorder); | |
| 111 break; | |
| 112 default: | |
| 113 canvas.drawText("Unkown Clip!!!", strlen("Unkown Clip!!!
"), | |
| 114 SkDoubleToScalar(10), SkDoubleToScalar(y
), blueBorder); | |
| 115 break; | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 y += 30; | |
| 120 str.printf("Number of clips in stack: %i", total); | |
| 121 canvas.drawText(str.c_str(), str.size(), | |
| 122 SkDoubleToScalar(10), SkDoubleToScalar(y), blueBorde
r); | |
| 123 } | |
| 124 | |
| 125 const SkRegion& clipRegion = gDumpCanvas->getTotalClip(); | |
| 126 SkPath clipPath; | |
| 127 if (clipRegion.getBoundaryPath(&clipPath)) { | |
| 128 SkPaint redBorder; | |
| 129 redBorder.setColor(SK_ColorRED); | |
| 130 redBorder.setStyle(SkPaint::kStroke_Style); | |
| 131 canvas.drawPath(clipPath, redBorder); | |
| 132 } | |
| 133 | |
| 134 canvas.flush(); | |
| 135 | |
| 136 SkString out; | |
| 137 | |
| 138 // TODO(edisonn): overlay on top of image inf about the clip , grafic st
ate, the stack | |
| 139 | |
| 140 out.appendf("/tmp/log_step_by_step/step-%i-%s.png", | |
| 141 gLastOpKeyword, gLastKeyword); | |
| 142 SkImageEncoder::EncodeFile(out.c_str(), bitmap, SkImageEncoder::kPNG_Typ
e, 100); | |
| 143 } | |
| 144 | |
| 145 if (ret && token->fType == kKeyword_TokenType && | |
| 146 token->fKeyword && token->fKeywordLength > 0 && token->fKeywordLengt
h < 100) { | |
| 147 strncpy(gLastKeyword, token->fKeyword, token->fKeywordLength); | |
| 148 gLastKeyword[token->fKeywordLength] = '\0'; | |
| 149 } else { | |
| 150 gLastKeyword[0] = '\0'; | |
| 151 } | |
| 152 #endif | |
| 153 | |
| 154 return ret; | |
| 155 } | |
| 156 | |
| OLD | NEW |