1/*
2 * Copyright 2010 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
8#ifndef GrGlyph_DEFINED
9#define GrGlyph_DEFINED
10
11#include "GrRect.h"
12#include "SkPath.h"
13#include "SkChecksum.h"
14
15class GrPlot;
16
17/*  Need this to be quad-state:
18    - complete w/ image
19    - just metrics
20    - failed to get image, but has metrics
21    - failed to get metrics
22 */
23struct GrGlyph {
24    typedef uint32_t PackedID;
25
26    GrPlot*     fPlot;
27    SkPath*     fPath;
28    PackedID    fPackedID;
29    GrIRect16   fBounds;
30    SkIPoint16  fAtlasLocation;
31
32    void init(GrGlyph::PackedID packed, const SkIRect& bounds) {
33        fPlot = NULL;
34        fPath = NULL;
35        fPackedID = packed;
36        fBounds.set(bounds);
37        fAtlasLocation.set(0, 0);
38    }
39
40    void free() {
41        if (fPath) {
42            delete fPath;
43            fPath = NULL;
44        }
45    }
46
47    int width() const { return fBounds.width(); }
48    int height() const { return fBounds.height(); }
49    bool isEmpty() const { return fBounds.isEmpty(); }
50    uint16_t glyphID() const { return UnpackID(fPackedID); }
51
52    ///////////////////////////////////////////////////////////////////////////
53
54    static inline unsigned ExtractSubPixelBitsFromFixed(SkFixed pos) {
55        // two most significant fraction bits from fixed-point
56        return (pos >> 14) & 3;
57    }
58
59    static inline PackedID Pack(uint16_t glyphID, SkFixed x, SkFixed y) {
60        x = ExtractSubPixelBitsFromFixed(x);
61        y = ExtractSubPixelBitsFromFixed(y);
62        return (x << 18) | (y << 16) | glyphID;
63    }
64
65    static inline SkFixed UnpackFixedX(PackedID packed) {
66        return ((packed >> 18) & 3) << 14;
67    }
68
69    static inline SkFixed UnpackFixedY(PackedID packed) {
70        return ((packed >> 16) & 3) << 14;
71    }
72
73    static inline uint16_t UnpackID(PackedID packed) {
74        return (uint16_t)packed;
75    }
76
77    static inline const GrGlyph::PackedID& GetKey(const GrGlyph& glyph) {
78        return glyph.fPackedID;
79    }
80
81    static inline uint32_t Hash(GrGlyph::PackedID key) {
82        return SkChecksum::Murmur3(&key, sizeof(key));
83    }
84};
85
86#endif
87