edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 1 | #ifndef __DEFINED__SkPdfBasics |
| 2 | #define __DEFINED__SkPdfBasics |
| 3 | |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 4 | #include "SkCanvas.h" |
| 5 | #include "SkPaint.h" |
| 6 | |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 7 | #include <iostream> |
| 8 | #include <cstdio> |
| 9 | #include <stack> |
| 10 | |
edisonn@google.com | eee4b65 | 2013-06-27 13:22:42 +0000 | [diff] [blame] | 11 | #define PDF_TRACE |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 12 | //#define PDF_TRACE_DIFF_IN_PNG |
| 13 | //#define PDF_DEBUG_NO_CLIPING |
| 14 | //#define PDF_DEBUG_NO_PAGE_CLIPING |
| 15 | //#define PDF_DEBUG_3X |
| 16 | |
| 17 | class SkPdfFont; |
| 18 | class SkPdfDoc; |
edisonn@google.com | 131d4ee | 2013-06-26 17:48:12 +0000 | [diff] [blame] | 19 | class SkPdfObject; |
| 20 | class SkPdfResourceDictionary; |
edisonn@google.com | b857a0c | 2013-06-25 20:45:40 +0000 | [diff] [blame] | 21 | |
| 22 | // TODO(edisonn): better class design. |
| 23 | struct PdfColorOperator { |
| 24 | std::string fColorSpace; // TODO(edisonn): use SkString |
| 25 | SkColor fColor; |
| 26 | double fOpacity; // ca or CA |
| 27 | // TODO(edisonn): add here other color space options. |
| 28 | |
| 29 | void setRGBColor(SkColor color) { |
| 30 | // TODO(edisonn): ASSERT DeviceRGB is the color space. |
| 31 | fColor = color; |
| 32 | } |
| 33 | // TODO(edisonn): double check the default values for all fields. |
| 34 | PdfColorOperator() : fColor(SK_ColorBLACK), fOpacity(1) {} |
| 35 | |
| 36 | void applyGraphicsState(SkPaint* paint) { |
| 37 | paint->setColor(SkColorSetA(fColor, fOpacity * 255)); |
| 38 | } |
| 39 | }; |
| 40 | |
| 41 | // TODO(edisonn): better class design. |
| 42 | struct PdfGraphicsState { |
| 43 | SkMatrix fMatrix; |
| 44 | SkMatrix fMatrixTm; |
| 45 | SkMatrix fMatrixTlm; |
| 46 | |
| 47 | double fCurPosX; |
| 48 | double fCurPosY; |
| 49 | |
| 50 | double fCurFontSize; |
| 51 | bool fTextBlock; |
| 52 | SkPdfFont* fSkFont; |
| 53 | SkPath fPath; |
| 54 | bool fPathClosed; |
| 55 | |
| 56 | // Clip that is applied after the drawing is done!!! |
| 57 | bool fHasClipPathToApply; |
| 58 | SkPath fClipPath; |
| 59 | |
| 60 | PdfColorOperator fStroking; |
| 61 | PdfColorOperator fNonStroking; |
| 62 | |
| 63 | double fLineWidth; |
| 64 | double fTextLeading; |
| 65 | double fWordSpace; |
| 66 | double fCharSpace; |
| 67 | |
| 68 | SkPdfResourceDictionary* fResources; |
| 69 | |
| 70 | SkBitmap fSMask; |
| 71 | |
| 72 | PdfGraphicsState() { |
| 73 | fCurPosX = 0.0; |
| 74 | fCurPosY = 0.0; |
| 75 | fCurFontSize = 0.0; |
| 76 | fTextBlock = false; |
| 77 | fMatrix = SkMatrix::I(); |
| 78 | fMatrixTm = SkMatrix::I(); |
| 79 | fMatrixTlm = SkMatrix::I(); |
| 80 | fPathClosed = true; |
| 81 | fLineWidth = 0; |
| 82 | fTextLeading = 0; |
| 83 | fWordSpace = 0; |
| 84 | fCharSpace = 0; |
| 85 | fHasClipPathToApply = false; |
| 86 | fResources = NULL; |
| 87 | fSkFont = NULL; |
| 88 | } |
| 89 | |
| 90 | void applyGraphicsState(SkPaint* paint, bool stroking) { |
| 91 | if (stroking) { |
| 92 | fStroking.applyGraphicsState(paint); |
| 93 | } else { |
| 94 | fNonStroking.applyGraphicsState(paint); |
| 95 | } |
| 96 | |
| 97 | // TODO(edisonn): get this from pdfContext->options, |
| 98 | // or pdfContext->addPaintOptions(&paint); |
| 99 | paint->setAntiAlias(true); |
| 100 | |
| 101 | // TODO(edisonn): dashing, miter, ... |
| 102 | paint->setStrokeWidth(SkDoubleToScalar(fLineWidth)); |
| 103 | } |
| 104 | }; |
| 105 | |
| 106 | // TODO(edisonn): better class design. |
| 107 | struct PdfInlineImage { |
| 108 | std::map<std::string, std::string> fKeyValuePairs; |
| 109 | std::string fImageData; |
| 110 | }; |
| 111 | |
| 112 | // TODO(edisonn): better class design. |
| 113 | struct PdfContext { |
| 114 | std::stack<SkPdfObject*> fObjectStack; |
| 115 | std::stack<PdfGraphicsState> fStateStack; |
| 116 | PdfGraphicsState fGraphicsState; |
| 117 | SkPdfDoc* fPdfDoc; |
| 118 | SkMatrix fOriginalMatrix; |
| 119 | |
| 120 | PdfInlineImage fInlineImage; |
| 121 | |
| 122 | PdfContext(SkPdfDoc* doc) : fPdfDoc(doc) {} |
| 123 | |
| 124 | }; |
| 125 | |
| 126 | // TODO(edisonn): temporary code, to report how much of the PDF we actually think we rendered. |
| 127 | enum PdfResult { |
| 128 | kOK_PdfResult, |
| 129 | kPartial_PdfResult, |
| 130 | kNYI_PdfResult, |
| 131 | kIgnoreError_PdfResult, |
| 132 | kError_PdfResult, |
| 133 | kUnsupported_PdfResult, |
| 134 | |
| 135 | kCount_PdfResult |
| 136 | }; |
| 137 | |
| 138 | #endif // __DEFINED__SkPdfBasics |