SkPdfUtils.h revision c8fda9d96be0bd944d37a6e23f7adad5f247c51d
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
333aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.comstruct NotOwnedString {
343aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com    const unsigned char* fBuffer;
35c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com    // TODO(edisonn): clean up, the last two bytes are used to signal if compression is used
363aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com    size_t fBytes;
373aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com
383aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com    static void init(NotOwnedString* str) {
393aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com        str->fBuffer = NULL;
403aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com        str->fBytes = 0;
413aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com    }
423aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com
433aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com    static void init(NotOwnedString* str, const char* sz) {
443aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com        str->fBuffer = (const unsigned char*)sz;
453aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com        str->fBytes = strlen(sz);
463aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com    }
473aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com
483aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com    bool equals(const char* sz) {
493aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com        return strncmp((const char*)fBuffer, sz, fBytes) == 0 && fBytes == strlen(sz);
503aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com
513aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com    }
523aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com};
53b857a0c7de8cffb09281fa59591649fb1db6ad0aedisonn@google.com
54c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.comSkMatrix SkMatrixFromPdfMatrix(double array[6]);
55c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com
56063d7072ef45971c17045721626b3f0cd052b3b9edisonn@google.com// TODO(edisonn): hack to make code generation simpler. Alternatively we can update the
57063d7072ef45971c17045721626b3f0cd052b3b9edisonn@google.com// generate_code.py not to rely on != operator
58063d7072ef45971c17045721626b3f0cd052b3b9edisonn@google.combool operator !=(const SkString& first, const char* second);
59063d7072ef45971c17045721626b3f0cd052b3b9edisonn@google.com
60b857a0c7de8cffb09281fa59591649fb1db6ad0aedisonn@google.comSkMatrix SkMatrixFromPdfArray(SkPdfArray* pdfArray);
61b857a0c7de8cffb09281fa59591649fb1db6ad0aedisonn@google.com
623aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.comSkPdfResult doType3Char(SkPdfContext* pdfContext, SkCanvas* canvas, const SkPdfNativeObject* skobj, SkRect bBox, SkMatrix matrix, double textSize);
633aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com
643aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com////////////////////////////////////////////////////////////////////////////////////////////////////
653aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com//
663aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com// TRACE functions
673aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com//
683aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com#ifdef PDF_TRACE
693aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.comvoid SkTraceMatrix(const SkMatrix& matrix, const char* sz);
703aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.comvoid SkTraceRect(const SkRect& rect, const char* sz);
713aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com#else
723aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com#define SkTraceMatrix(a,b)
733aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com#define SkTraceRect(a,b)
743aa355527a3b91d3e12b8bee49e5637d00a736caedisonn@google.com#endif
75b857a0c7de8cffb09281fa59591649fb1db6ad0aedisonn@google.com
76c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com#ifdef PDF_TRACE_TOKENIZER
77c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com
78c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.comstatic void TRACE_COMMENT(char ch) {
79c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com    printf("%c", ch);
80c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com}
81c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com
82c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.comstatic void TRACE_TK(char ch) {
83c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com    printf("%c", ch);
84c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com}
85c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com
86c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.comstatic void TRACE_NAME(const unsigned char* start, const unsigned char* end) {
87c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com    while (start < end) {
88c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com        printf("%c", *start);
89c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com        start++;
90c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com    }
91c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com    printf("\n");
92c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com}
93c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com
94c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.comstatic void TRACE_STRING(const unsigned char* start, const unsigned char* end) {
95c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com    while (start < end) {
96c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com        printf("%c", *start);
97c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com        start++;
98c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com    }
99c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com    printf("\n");
100c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com}
101c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com
102c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.comstatic void TRACE_HEXSTRING(const unsigned char* start, const unsigned char* end) {
103c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com    while (start < end) {
104c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com        printf("%c", *start);
105c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com        start++;
106c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com    }
107c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com    printf("\n");
108c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com}
109c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com
110c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com#else
111c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com#define TRACE_COMMENT(ch)
112c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com#define TRACE_TK(ch)
113c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com#define TRACE_NAME(start,end)
114c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com#define TRACE_STRING(start,end)
115c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com#define TRACE_HEXSTRING(start,end)
116c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com#endif
117c8fda9d96be0bd944d37a6e23f7adad5f247c51dedisonn@google.com
118cf2cfa174ca878c144e17e9fc60ca8e9070d7dededisonn@google.com#endif   // SkPdfUtils_DEFINED
119