1bbe50131413098bffed9186660c41f047d881596bungeman@google.com/*
2bbe50131413098bffed9186660c41f047d881596bungeman@google.com * Copyright 2006 The Android Open Source Project
3bbe50131413098bffed9186660c41f047d881596bungeman@google.com *
4bbe50131413098bffed9186660c41f047d881596bungeman@google.com * Use of this source code is governed by a BSD-style license that can be
5bbe50131413098bffed9186660c41f047d881596bungeman@google.com * found in the LICENSE file.
6bbe50131413098bffed9186660c41f047d881596bungeman@google.com */
7bbe50131413098bffed9186660c41f047d881596bungeman@google.com
8bbe50131413098bffed9186660c41f047d881596bungeman@google.com#ifndef SkGlyph_DEFINED
9bbe50131413098bffed9186660c41f047d881596bungeman@google.com#define SkGlyph_DEFINED
10bbe50131413098bffed9186660c41f047d881596bungeman@google.com
11c71239b9ff7d8c19dc03cb6c9081e7dc4e0947d2herb#include "SkChecksum.h"
12bbe50131413098bffed9186660c41f047d881596bungeman@google.com#include "SkTypes.h"
13bbe50131413098bffed9186660c41f047d881596bungeman@google.com#include "SkFixed.h"
14bbe50131413098bffed9186660c41f047d881596bungeman@google.com#include "SkMask.h"
15bbe50131413098bffed9186660c41f047d881596bungeman@google.com
16bbe50131413098bffed9186660c41f047d881596bungeman@google.comclass SkPath;
17b69d0e0ac45e13f667bc11a937dcb547072bc93dherbclass SkGlyphCache;
18bbe50131413098bffed9186660c41f047d881596bungeman@google.com
19bbe50131413098bffed9186660c41f047d881596bungeman@google.com// needs to be != to any valid SkMask::Format
20bbe50131413098bffed9186660c41f047d881596bungeman@google.com#define MASK_FORMAT_UNKNOWN         (0xFF)
21bbe50131413098bffed9186660c41f047d881596bungeman@google.com#define MASK_FORMAT_JUST_ADVANCE    MASK_FORMAT_UNKNOWN
22bbe50131413098bffed9186660c41f047d881596bungeman@google.com
23bbe50131413098bffed9186660c41f047d881596bungeman@google.com#define kMaxGlyphWidth (1<<13)
24bbe50131413098bffed9186660c41f047d881596bungeman@google.com
25b68ce74bd197a9ca4becd53cbcfee825b8d08e0emtkleinSK_BEGIN_REQUIRE_DENSE
26b69d0e0ac45e13f667bc11a937dcb547072bc93dherbclass SkGlyph {
27b69d0e0ac45e13f667bc11a937dcb547072bc93dherb    enum {
28b69d0e0ac45e13f667bc11a937dcb547072bc93dherb        kSubBits = 2,
29b69d0e0ac45e13f667bc11a937dcb547072bc93dherb        kSubMask = ((1 << kSubBits) - 1),
30b69d0e0ac45e13f667bc11a937dcb547072bc93dherb        kSubShift = 24, // must be large enough for glyphs and unichars
31b69d0e0ac45e13f667bc11a937dcb547072bc93dherb        kCodeMask = ((1 << kSubShift) - 1),
32b69d0e0ac45e13f667bc11a937dcb547072bc93dherb        // relative offsets for X and Y subpixel bits
33b69d0e0ac45e13f667bc11a937dcb547072bc93dherb        kSubShiftX = kSubBits,
34b69d0e0ac45e13f667bc11a937dcb547072bc93dherb        kSubShiftY = 0
35b69d0e0ac45e13f667bc11a937dcb547072bc93dherb    };
36b69d0e0ac45e13f667bc11a937dcb547072bc93dherb
370449bcfb2fa1dd33cb3a4c0c8b17960d17edf01acaryclark    // Support horizontal and vertical skipping strike-through / underlines.
380449bcfb2fa1dd33cb3a4c0c8b17960d17edf01acaryclark    // The caller walks the linked list looking for a match. For a horizontal underline,
390449bcfb2fa1dd33cb3a4c0c8b17960d17edf01acaryclark    // the fBounds contains the top and bottom of the underline. The fInterval pair contains the
400449bcfb2fa1dd33cb3a4c0c8b17960d17edf01acaryclark    // beginning and end of of the intersection of the bounds and the glyph's path.
410449bcfb2fa1dd33cb3a4c0c8b17960d17edf01acaryclark    // If interval[0] >= interval[1], no intesection was found.
420449bcfb2fa1dd33cb3a4c0c8b17960d17edf01acaryclark    struct Intercept {
430449bcfb2fa1dd33cb3a4c0c8b17960d17edf01acaryclark        Intercept* fNext;
440449bcfb2fa1dd33cb3a4c0c8b17960d17edf01acaryclark        SkScalar   fBounds[2];    // for horz underlines, the boundaries in Y
450449bcfb2fa1dd33cb3a4c0c8b17960d17edf01acaryclark        SkScalar   fInterval[2];  // the outside intersections of the axis and the glyph
460449bcfb2fa1dd33cb3a4c0c8b17960d17edf01acaryclark    };
470449bcfb2fa1dd33cb3a4c0c8b17960d17edf01acaryclark
480449bcfb2fa1dd33cb3a4c0c8b17960d17edf01acaryclark    struct PathData {
490449bcfb2fa1dd33cb3a4c0c8b17960d17edf01acaryclark        Intercept* fIntercept;
500449bcfb2fa1dd33cb3a4c0c8b17960d17edf01acaryclark        SkPath*    fPath;
510449bcfb2fa1dd33cb3a4c0c8b17960d17edf01acaryclark    };
520449bcfb2fa1dd33cb3a4c0c8b17960d17edf01acaryclark
530449bcfb2fa1dd33cb3a4c0c8b17960d17edf01acaryclarkpublic:
54b69d0e0ac45e13f667bc11a937dcb547072bc93dherb    static const SkFixed kSubpixelRound = SK_FixedHalf >> SkGlyph::kSubBits;
55c1e97b372e21edf9c7e45cfea0eca7f1a52fe9e5herb    // A value that can never be generated by MakeID.
56c1e97b372e21edf9c7e45cfea0eca7f1a52fe9e5herb    static const uint32_t kImpossibleID = ~0;
57bbe50131413098bffed9186660c41f047d881596bungeman@google.com    void*       fImage;
580449bcfb2fa1dd33cb3a4c0c8b17960d17edf01acaryclark    PathData*   fPathData;
59bbe50131413098bffed9186660c41f047d881596bungeman@google.com    SkFixed     fAdvanceX, fAdvanceY;
60bbe50131413098bffed9186660c41f047d881596bungeman@google.com
61bbe50131413098bffed9186660c41f047d881596bungeman@google.com    uint16_t    fWidth, fHeight;
62bbe50131413098bffed9186660c41f047d881596bungeman@google.com    int16_t     fTop, fLeft;
63bbe50131413098bffed9186660c41f047d881596bungeman@google.com
64bbe50131413098bffed9186660c41f047d881596bungeman@google.com    uint8_t     fMaskFormat;
65bbe50131413098bffed9186660c41f047d881596bungeman@google.com    int8_t      fRsbDelta, fLsbDelta;  // used by auto-kerning
66b2f7fce9e034c3b6749e75a129e7836df3529706Ben Wagner    int8_t      fForceBW;
67bbe50131413098bffed9186660c41f047d881596bungeman@google.com
68b69d0e0ac45e13f667bc11a937dcb547072bc93dherb    void initWithGlyphID(uint32_t glyph_id) {
69b69d0e0ac45e13f667bc11a937dcb547072bc93dherb        this->initCommon(MakeID(glyph_id));
70b69d0e0ac45e13f667bc11a937dcb547072bc93dherb    }
71b69d0e0ac45e13f667bc11a937dcb547072bc93dherb
72b69d0e0ac45e13f667bc11a937dcb547072bc93dherb    void initGlyphIdFrom(const SkGlyph& glyph) {
73b69d0e0ac45e13f667bc11a937dcb547072bc93dherb        this->initCommon(glyph.fID);
74bbe50131413098bffed9186660c41f047d881596bungeman@google.com    }
75bbe50131413098bffed9186660c41f047d881596bungeman@google.com
76e70de9e4f0b7bf73f7cd1a20dbabcb233ffbb7f1herb    void initGlyphFromCombinedID(uint32_t combined_id) {
77e70de9e4f0b7bf73f7cd1a20dbabcb233ffbb7f1herb      this->initCommon(combined_id);
78e70de9e4f0b7bf73f7cd1a20dbabcb233ffbb7f1herb    }
79e70de9e4f0b7bf73f7cd1a20dbabcb233ffbb7f1herb
80bbe50131413098bffed9186660c41f047d881596bungeman@google.com    /**
81bbe50131413098bffed9186660c41f047d881596bungeman@google.com     *  Compute the rowbytes for the specified width and mask-format.
82bbe50131413098bffed9186660c41f047d881596bungeman@google.com     */
83bbe50131413098bffed9186660c41f047d881596bungeman@google.com    static unsigned ComputeRowBytes(unsigned width, SkMask::Format format) {
84bbe50131413098bffed9186660c41f047d881596bungeman@google.com        unsigned rb = width;
85bbe50131413098bffed9186660c41f047d881596bungeman@google.com        if (SkMask::kBW_Format == format) {
86bbe50131413098bffed9186660c41f047d881596bungeman@google.com            rb = (rb + 7) >> 3;
87d54d3fc82456f8789b3e4dff40faeb7b670bcf4breed        } else if (SkMask::kARGB32_Format == format) {
88bbe50131413098bffed9186660c41f047d881596bungeman@google.com            rb <<= 2;
89bbe50131413098bffed9186660c41f047d881596bungeman@google.com        } else if (SkMask::kLCD16_Format == format) {
90bbe50131413098bffed9186660c41f047d881596bungeman@google.com            rb = SkAlign4(rb << 1);
91bbe50131413098bffed9186660c41f047d881596bungeman@google.com        } else {
92bbe50131413098bffed9186660c41f047d881596bungeman@google.com            rb = SkAlign4(rb);
93bbe50131413098bffed9186660c41f047d881596bungeman@google.com        }
94bbe50131413098bffed9186660c41f047d881596bungeman@google.com        return rb;
95bbe50131413098bffed9186660c41f047d881596bungeman@google.com    }
96bbe50131413098bffed9186660c41f047d881596bungeman@google.com
97bbe50131413098bffed9186660c41f047d881596bungeman@google.com    unsigned rowBytes() const {
98bbe50131413098bffed9186660c41f047d881596bungeman@google.com        return ComputeRowBytes(fWidth, (SkMask::Format)fMaskFormat);
99bbe50131413098bffed9186660c41f047d881596bungeman@google.com    }
100bbe50131413098bffed9186660c41f047d881596bungeman@google.com
101bbe50131413098bffed9186660c41f047d881596bungeman@google.com    bool isJustAdvance() const {
102bbe50131413098bffed9186660c41f047d881596bungeman@google.com        return MASK_FORMAT_JUST_ADVANCE == fMaskFormat;
103bbe50131413098bffed9186660c41f047d881596bungeman@google.com    }
104bbe50131413098bffed9186660c41f047d881596bungeman@google.com
105bbe50131413098bffed9186660c41f047d881596bungeman@google.com    bool isFullMetrics() const {
106bbe50131413098bffed9186660c41f047d881596bungeman@google.com        return MASK_FORMAT_JUST_ADVANCE != fMaskFormat;
107bbe50131413098bffed9186660c41f047d881596bungeman@google.com    }
108bbe50131413098bffed9186660c41f047d881596bungeman@google.com
109bbe50131413098bffed9186660c41f047d881596bungeman@google.com    uint16_t getGlyphID() const {
110bbe50131413098bffed9186660c41f047d881596bungeman@google.com        return ID2Code(fID);
111bbe50131413098bffed9186660c41f047d881596bungeman@google.com    }
112bbe50131413098bffed9186660c41f047d881596bungeman@google.com
113bbe50131413098bffed9186660c41f047d881596bungeman@google.com    unsigned getSubX() const {
114bbe50131413098bffed9186660c41f047d881596bungeman@google.com        return ID2SubX(fID);
115bbe50131413098bffed9186660c41f047d881596bungeman@google.com    }
116bbe50131413098bffed9186660c41f047d881596bungeman@google.com
117bbe50131413098bffed9186660c41f047d881596bungeman@google.com    SkFixed getSubXFixed() const {
118bbe50131413098bffed9186660c41f047d881596bungeman@google.com        return SubToFixed(ID2SubX(fID));
119bbe50131413098bffed9186660c41f047d881596bungeman@google.com    }
120bbe50131413098bffed9186660c41f047d881596bungeman@google.com
121bbe50131413098bffed9186660c41f047d881596bungeman@google.com    SkFixed getSubYFixed() const {
122bbe50131413098bffed9186660c41f047d881596bungeman@google.com        return SubToFixed(ID2SubY(fID));
123bbe50131413098bffed9186660c41f047d881596bungeman@google.com    }
124bbe50131413098bffed9186660c41f047d881596bungeman@google.com
125bbe50131413098bffed9186660c41f047d881596bungeman@google.com    size_t computeImageSize() const;
126bbe50131413098bffed9186660c41f047d881596bungeman@google.com
127bbe50131413098bffed9186660c41f047d881596bungeman@google.com    /** Call this to set all of the metrics fields to 0 (e.g. if the scaler
128bbe50131413098bffed9186660c41f047d881596bungeman@google.com        encounters an error measuring a glyph). Note: this does not alter the
129bbe50131413098bffed9186660c41f047d881596bungeman@google.com        fImage, fPath, fID, fMaskFormat fields.
130bbe50131413098bffed9186660c41f047d881596bungeman@google.com     */
131bbe50131413098bffed9186660c41f047d881596bungeman@google.com    void zeroMetrics();
132bbe50131413098bffed9186660c41f047d881596bungeman@google.com
133b69d0e0ac45e13f667bc11a937dcb547072bc93dherb    void toMask(SkMask* mask) const;
134b69d0e0ac45e13f667bc11a937dcb547072bc93dherb
135c71239b9ff7d8c19dc03cb6c9081e7dc4e0947d2herb    class HashTraits {
136c71239b9ff7d8c19dc03cb6c9081e7dc4e0947d2herb    public:
137c71239b9ff7d8c19dc03cb6c9081e7dc4e0947d2herb        static uint32_t GetKey(const SkGlyph& glyph) {
138c71239b9ff7d8c19dc03cb6c9081e7dc4e0947d2herb            return glyph.fID;
139c71239b9ff7d8c19dc03cb6c9081e7dc4e0947d2herb        }
140c71239b9ff7d8c19dc03cb6c9081e7dc4e0947d2herb        static uint32_t Hash(uint32_t glyphId) {
141c71239b9ff7d8c19dc03cb6c9081e7dc4e0947d2herb            return SkChecksum::CheapMix(glyphId);
142c71239b9ff7d8c19dc03cb6c9081e7dc4e0947d2herb        }
143c71239b9ff7d8c19dc03cb6c9081e7dc4e0947d2herb    };
144c71239b9ff7d8c19dc03cb6c9081e7dc4e0947d2herb
145b69d0e0ac45e13f667bc11a937dcb547072bc93dherb private:
146b69d0e0ac45e13f667bc11a937dcb547072bc93dherb    // TODO(herb) remove friend statement after SkGlyphCache cleanup.
147b69d0e0ac45e13f667bc11a937dcb547072bc93dherb    friend class SkGlyphCache;
148b69d0e0ac45e13f667bc11a937dcb547072bc93dherb
149b69d0e0ac45e13f667bc11a937dcb547072bc93dherb    void initCommon(uint32_t id) {
150b69d0e0ac45e13f667bc11a937dcb547072bc93dherb        fID             = id;
15196fcdcc219d2a0d3579719b84b28bede76efba64halcanary        fImage          = nullptr;
1520449bcfb2fa1dd33cb3a4c0c8b17960d17edf01acaryclark        fPathData       = nullptr;
153b69d0e0ac45e13f667bc11a937dcb547072bc93dherb        fMaskFormat     = MASK_FORMAT_UNKNOWN;
154b69d0e0ac45e13f667bc11a937dcb547072bc93dherb        fForceBW        = 0;
155b69d0e0ac45e13f667bc11a937dcb547072bc93dherb    }
156e70de9e4f0b7bf73f7cd1a20dbabcb233ffbb7f1herb
157bbe50131413098bffed9186660c41f047d881596bungeman@google.com    static unsigned ID2Code(uint32_t id) {
158e70de9e4f0b7bf73f7cd1a20dbabcb233ffbb7f1herb        return id & kCodeMask;
159bbe50131413098bffed9186660c41f047d881596bungeman@google.com    }
160bbe50131413098bffed9186660c41f047d881596bungeman@google.com
161bbe50131413098bffed9186660c41f047d881596bungeman@google.com    static unsigned ID2SubX(uint32_t id) {
162bbe50131413098bffed9186660c41f047d881596bungeman@google.com        return id >> (kSubShift + kSubShiftX);
163bbe50131413098bffed9186660c41f047d881596bungeman@google.com    }
164bbe50131413098bffed9186660c41f047d881596bungeman@google.com
165bbe50131413098bffed9186660c41f047d881596bungeman@google.com    static unsigned ID2SubY(uint32_t id) {
166bbe50131413098bffed9186660c41f047d881596bungeman@google.com        return (id >> (kSubShift + kSubShiftY)) & kSubMask;
167bbe50131413098bffed9186660c41f047d881596bungeman@google.com    }
168bbe50131413098bffed9186660c41f047d881596bungeman@google.com
169bbe50131413098bffed9186660c41f047d881596bungeman@google.com    static unsigned FixedToSub(SkFixed n) {
170bbe50131413098bffed9186660c41f047d881596bungeman@google.com        return (n >> (16 - kSubBits)) & kSubMask;
171bbe50131413098bffed9186660c41f047d881596bungeman@google.com    }
172bbe50131413098bffed9186660c41f047d881596bungeman@google.com
173bbe50131413098bffed9186660c41f047d881596bungeman@google.com    static SkFixed SubToFixed(unsigned sub) {
174bbe50131413098bffed9186660c41f047d881596bungeman@google.com        SkASSERT(sub <= kSubMask);
175bbe50131413098bffed9186660c41f047d881596bungeman@google.com        return sub << (16 - kSubBits);
176bbe50131413098bffed9186660c41f047d881596bungeman@google.com    }
177bbe50131413098bffed9186660c41f047d881596bungeman@google.com
178bbe50131413098bffed9186660c41f047d881596bungeman@google.com    static uint32_t MakeID(unsigned code) {
179e70de9e4f0b7bf73f7cd1a20dbabcb233ffbb7f1herb        SkASSERT(code <= kCodeMask);
180c1e97b372e21edf9c7e45cfea0eca7f1a52fe9e5herb        SkASSERT(code != kImpossibleID);
1815a2a5e729c081aa8ddfaab3311b7730622689abfmtklein        return code;
182bbe50131413098bffed9186660c41f047d881596bungeman@google.com    }
183bbe50131413098bffed9186660c41f047d881596bungeman@google.com
184bbe50131413098bffed9186660c41f047d881596bungeman@google.com    static uint32_t MakeID(unsigned code, SkFixed x, SkFixed y) {
1855a2a5e729c081aa8ddfaab3311b7730622689abfmtklein        SkASSERT(code <= kCodeMask);
186bbe50131413098bffed9186660c41f047d881596bungeman@google.com        x = FixedToSub(x);
187bbe50131413098bffed9186660c41f047d881596bungeman@google.com        y = FixedToSub(y);
188c1e97b372e21edf9c7e45cfea0eca7f1a52fe9e5herb        uint32_t ID = (x << (kSubShift + kSubShiftX)) |
189c1e97b372e21edf9c7e45cfea0eca7f1a52fe9e5herb                      (y << (kSubShift + kSubShiftY)) |
190c1e97b372e21edf9c7e45cfea0eca7f1a52fe9e5herb                      code;
191c1e97b372e21edf9c7e45cfea0eca7f1a52fe9e5herb        SkASSERT(ID != kImpossibleID);
192c1e97b372e21edf9c7e45cfea0eca7f1a52fe9e5herb        return ID;
193bbe50131413098bffed9186660c41f047d881596bungeman@google.com    }
194bbe50131413098bffed9186660c41f047d881596bungeman@google.com
1950449bcfb2fa1dd33cb3a4c0c8b17960d17edf01acaryclark    // FIXME - This is needed because the Android frame work directly
196b69d0e0ac45e13f667bc11a937dcb547072bc93dherb  // accesses fID. Remove when fID accesses are cleaned up.
197b69d0e0ac45e13f667bc11a937dcb547072bc93dherb#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
198b69d0e0ac45e13f667bc11a937dcb547072bc93dherb  public:
199b69d0e0ac45e13f667bc11a937dcb547072bc93dherb#endif
200b69d0e0ac45e13f667bc11a937dcb547072bc93dherb    uint32_t    fID;
201bbe50131413098bffed9186660c41f047d881596bungeman@google.com};
202b68ce74bd197a9ca4becd53cbcfee825b8d08e0emtkleinSK_END_REQUIRE_DENSE
203bbe50131413098bffed9186660c41f047d881596bungeman@google.com
204bbe50131413098bffed9186660c41f047d881596bungeman@google.com#endif
205