1/*
2 * Copyright 2015 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#ifndef SkPDFCanon_DEFINED
8#define SkPDFCanon_DEFINED
9
10#include "SkBitmapKey.h"
11#include "SkPDFGradientShader.h"
12#include "SkPDFGraphicState.h"
13#include "SkPDFShader.h"
14#include "SkTDArray.h"
15#include "SkTHash.h"
16#include "SkTypeface.h"
17
18class SkPDFFont;
19struct SkAdvancedTypefaceMetrics;
20
21/**
22 *  The SkPDFCanon canonicalizes objects across PDF pages
23 *  (SkPDFDevices) and across draw calls.
24 */
25class SkPDFCanon {
26public:
27    ~SkPDFCanon();
28    SkPDFCanon();
29    SkPDFCanon(const SkPDFCanon&) = delete;
30    SkPDFCanon& operator=(const SkPDFCanon&) = delete;
31
32    SkTHashMap<SkPDFImageShaderKey, sk_sp<SkPDFObject>> fImageShaderMap;
33
34    SkPDFGradientShader::HashMap fGradientPatternMap;
35
36    SkTHashMap<SkBitmapKey, sk_sp<SkPDFObject>> fPDFBitmapMap;
37
38    SkTHashMap<uint32_t, std::unique_ptr<SkAdvancedTypefaceMetrics>> fTypefaceMetrics;
39    SkTHashMap<uint32_t, sk_sp<SkPDFDict>> fFontDescriptors;
40    SkTHashMap<uint64_t, sk_sp<SkPDFFont>> fFontMap;
41
42    SkTHashMap<SkPDFStrokeGraphicState, sk_sp<SkPDFDict>> fStrokeGSMap;
43    SkTHashMap<SkPDFFillGraphicState, sk_sp<SkPDFDict>> fFillGSMap;
44
45    sk_sp<SkPDFStream> fInvertFunction;
46    sk_sp<SkPDFDict> fNoSmaskGraphicState;
47    sk_sp<SkPDFArray> fRangeObject;
48
49    SK_BEGIN_REQUIRE_DENSE
50    struct BitmapGlyphKey {
51        SkFontID fFontID;      // uint32_t
52        SkScalar fTextSize;    // float32
53        SkScalar fTextScaleX;  // float32
54        SkScalar fTextSkewX;   // float32
55        SkGlyphID fGlyphID;    // uint16_t
56        uint16_t fPadding;
57    };
58    SK_END_REQUIRE_DENSE
59    struct BitmapGlyph {
60        sk_sp<SkImage> fImage;
61        SkIPoint fOffset;
62    };
63    SkTHashMap<BitmapGlyphKey, BitmapGlyph> fBitmapGlyphImages;
64};
65
66inline bool operator==(const SkPDFCanon::BitmapGlyphKey& u, const SkPDFCanon::BitmapGlyphKey& v) {
67    return memcmp(&u, &v, sizeof(SkPDFCanon::BitmapGlyphKey)) == 0;
68}
69#endif  // SkPDFCanon_DEFINED
70