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 "GrBatchAtlas.h"
12 #include "GrRect.h"
13 #include "GrTypes.h"
14 
15 #include "SkChecksum.h"
16 #include "SkPath.h"
17 
18 class GrPlot;
19 
20 /*  Need this to be quad-state:
21     - complete w/ image
22     - just metrics
23     - failed to get image, but has metrics
24     - failed to get metrics
25  */
26 struct GrGlyph {
27     enum MaskStyle {
28         kCoverage_MaskStyle,
29         kDistance_MaskStyle
30     };
31 
32     typedef uint32_t PackedID;
33 
34     GrBatchAtlas::AtlasID fID;
35     SkPath*               fPath;
36     PackedID              fPackedID;
37     GrMaskFormat          fMaskFormat;
38     GrIRect16             fBounds;
39     SkIPoint16            fAtlasLocation;
40     bool                  fTooLargeForAtlas;
41 
initGrGlyph42     void init(GrGlyph::PackedID packed, const SkIRect& bounds, GrMaskFormat format) {
43         fID = GrBatchAtlas::kInvalidAtlasID;
44         fPath = nullptr;
45         fPackedID = packed;
46         fBounds.set(bounds);
47         fMaskFormat = format;
48         fAtlasLocation.set(0, 0);
49         fTooLargeForAtlas = GrBatchAtlas::GlyphTooLargeForAtlas(bounds.width(), bounds.height());
50     }
51 
freeGrGlyph52     void free() {
53         if (fPath) {
54             delete fPath;
55             fPath = nullptr;
56         }
57     }
58 
widthGrGlyph59     int width() const { return fBounds.width(); }
heightGrGlyph60     int height() const { return fBounds.height(); }
isEmptyGrGlyph61     bool isEmpty() const { return fBounds.isEmpty(); }
glyphIDGrGlyph62     uint16_t glyphID() const { return UnpackID(fPackedID); }
63 
64     ///////////////////////////////////////////////////////////////////////////
65 
ExtractSubPixelBitsFromFixedGrGlyph66     static inline unsigned ExtractSubPixelBitsFromFixed(SkFixed pos) {
67         // two most significant fraction bits from fixed-point
68         return (pos >> 14) & 3;
69     }
70 
PackGrGlyph71     static inline PackedID Pack(uint16_t glyphID, SkFixed x, SkFixed y, MaskStyle ms) {
72         x = ExtractSubPixelBitsFromFixed(x);
73         y = ExtractSubPixelBitsFromFixed(y);
74         int dfFlag = (ms == kDistance_MaskStyle) ? 0x1 : 0x0;
75         return (dfFlag << 20) | (x << 18) | (y << 16) | glyphID;
76     }
77 
UnpackFixedXGrGlyph78     static inline SkFixed UnpackFixedX(PackedID packed) {
79         return ((packed >> 18) & 3) << 14;
80     }
81 
UnpackFixedYGrGlyph82     static inline SkFixed UnpackFixedY(PackedID packed) {
83         return ((packed >> 16) & 3) << 14;
84     }
85 
UnpackMaskStyleGrGlyph86     static inline MaskStyle UnpackMaskStyle(PackedID packed) {
87         return ((packed >> 20) & 1) ? kDistance_MaskStyle : kCoverage_MaskStyle;
88     }
89 
UnpackIDGrGlyph90     static inline uint16_t UnpackID(PackedID packed) {
91         return (uint16_t)packed;
92     }
93 
GetKeyGrGlyph94     static inline const GrGlyph::PackedID& GetKey(const GrGlyph& glyph) {
95         return glyph.fPackedID;
96     }
97 
HashGrGlyph98     static inline uint32_t Hash(GrGlyph::PackedID key) {
99         return SkChecksum::Mix(key);
100     }
101 };
102 
103 #endif
104