1cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com/*
2cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com * Copyright 2013 Google Inc.
3cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com *
4cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com * Use of this source code is governed by a BSD-style license that can be
5cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com * found in the LICENSE file.
6cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com */
7cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com
8cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com#ifndef SkPdfUtils_DEFINED
9cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com#define SkPdfUtils_DEFINED
10b857a0c7de8cffb09281fa59591649fb1db6ad0aedisonn@google.com
113aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com#include "SkMatrix.h"
123aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com#include "SkRect.h"
1333f11b6fcdb7dfce27f953803be40fbacedc7450edisonn@google.com#include "SkPdfConfig.h"
14063d7072ef45971c17045721626b3f0cd052b3b9edisonn@google.com#include "SkString.h"
15b857a0c7de8cffb09281fa59591649fb1db6ad0aedisonn@google.com
16b857a0c7de8cffb09281fa59591649fb1db6ad0aedisonn@google.comclass SkPdfArray;
173aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.comclass SkPdfContext;
183aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.comclass SkCanvas;
193aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.comclass SkPdfNativeObject;
203aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com
213aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com// TODO(edisonn): temporary code, to report how much of the PDF we actually think we rendered.
223aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.comenum SkPdfResult {
233aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com    kOK_SkPdfResult,
243aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com    kPartial_SkPdfResult,
253aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com    kNYI_SkPdfResult,
263aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com    kIgnoreError_SkPdfResult,
273aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com    kError_SkPdfResult,
283aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com    kUnsupported_SkPdfResult,
293aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com
303aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com    kCount_SkPdfResult
313aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com};
323aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com
33d03c2c732e2e914f8429cfc8b077a7b9b853dd8eedisonn@google.com// In order to operate fast, when we parse the pdf, we try not to allocate many new strings,
34d03c2c732e2e914f8429cfc8b077a7b9b853dd8eedisonn@google.com// and if possible we refer the string in the pdf stream.
353aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.comstruct NotOwnedString {
363aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com    const unsigned char* fBuffer;
37c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com    // TODO(edisonn): clean up, the last two bytes are used to signal if compression is used
383aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com    size_t fBytes;
393aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com
403aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com    static void init(NotOwnedString* str) {
413aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com        str->fBuffer = NULL;
423aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com        str->fBytes = 0;
433aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com    }
443aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com
453aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com    static void init(NotOwnedString* str, const char* sz) {
463aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com        str->fBuffer = (const unsigned char*)sz;
473aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com        str->fBytes = strlen(sz);
483aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com    }
493aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com
503aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com    bool equals(const char* sz) {
513aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com        return strncmp((const char*)fBuffer, sz, fBytes) == 0 && fBytes == strlen(sz);
523aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com
533aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com    }
543aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com};
55b857a0c7de8cffb09281fa59591649fb1db6ad0aedisonn@google.com
56c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.comSkMatrix SkMatrixFromPdfMatrix(double array[6]);
57c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com
58063d7072ef45971c17045721626b3f0cd052b3b9edisonn@google.com// TODO(edisonn): hack to make code generation simpler. Alternatively we can update the
59063d7072ef45971c17045721626b3f0cd052b3b9edisonn@google.com// generate_code.py not to rely on != operator
60063d7072ef45971c17045721626b3f0cd052b3b9edisonn@google.combool operator !=(const SkString& first, const char* second);
61063d7072ef45971c17045721626b3f0cd052b3b9edisonn@google.com
62b857a0c7de8cffb09281fa59591649fb1db6ad0aedisonn@google.comSkMatrix SkMatrixFromPdfArray(SkPdfArray* pdfArray);
63b857a0c7de8cffb09281fa59591649fb1db6ad0aedisonn@google.com
64e50d9a1fcd9c4298079ff54f9a40c9708d30f8c6edisonn@google.comSkPdfResult doType3Char(SkPdfContext* pdfContext, SkCanvas* canvas, const SkPdfNativeObject* skobj,
65e50d9a1fcd9c4298079ff54f9a40c9708d30f8c6edisonn@google.com                        SkRect bBox, SkMatrix matrix, double textSize);
663aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com
673aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com////////////////////////////////////////////////////////////////////////////////////////////////////
683aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com//
693aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com// TRACE functions
703aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com//
713aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com#ifdef PDF_TRACE
723aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.comvoid SkTraceMatrix(const SkMatrix& matrix, const char* sz);
733aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.comvoid SkTraceRect(const SkRect& rect, const char* sz);
743aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com#else
753aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com#define SkTraceMatrix(a,b)
763aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com#define SkTraceRect(a,b)
773aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com#endif
78b857a0c7de8cffb09281fa59591649fb1db6ad0aedisonn@google.com
79c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com#ifdef PDF_TRACE_TOKENIZER
80c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com
81c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.comstatic void TRACE_COMMENT(char ch) {
82c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com    printf("%c", ch);
83c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com}
84c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com
85c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.comstatic void TRACE_TK(char ch) {
86c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com    printf("%c", ch);
87c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com}
88c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com
89c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.comstatic void TRACE_NAME(const unsigned char* start, const unsigned char* end) {
90c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com    while (start < end) {
91c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com        printf("%c", *start);
92c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com        start++;
93c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com    }
94c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com    printf("\n");
95c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com}
96c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com
97c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.comstatic void TRACE_STRING(const unsigned char* start, const unsigned char* end) {
98c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com    while (start < end) {
99c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com        printf("%c", *start);
100c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com        start++;
101c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com    }
102c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com    printf("\n");
103c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com}
104c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com
105c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.comstatic void TRACE_HEXSTRING(const unsigned char* start, const unsigned char* end) {
106c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com    while (start < end) {
107c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com        printf("%c", *start);
108c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com        start++;
109c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com    }
110c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com    printf("\n");
111c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com}
112c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com
113c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com#else
114c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com#define TRACE_COMMENT(ch)
115c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com#define TRACE_TK(ch)
116c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com#define TRACE_NAME(start,end)
117c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com#define TRACE_STRING(start,end)
118c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com#define TRACE_HEXSTRING(start,end)
119c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com#endif
120c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com
121cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com#endif   // SkPdfUtils_DEFINED
122