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 // TODO either plot or AtlasID will be valid, not both 35 GrBatchAtlas::AtlasID fID; 36 GrPlot* fPlot; 37 SkPath* fPath; 38 PackedID fPackedID; 39 GrMaskFormat fMaskFormat; 40 GrIRect16 fBounds; 41 SkIPoint16 fAtlasLocation; 42 bool fTooLargeForAtlas; 43 initGrGlyph44 void init(GrGlyph::PackedID packed, const SkIRect& bounds, GrMaskFormat format) { 45 fID = GrBatchAtlas::kInvalidAtlasID; 46 fPlot = NULL; 47 fPath = NULL; 48 fPackedID = packed; 49 fBounds.set(bounds); 50 fMaskFormat = format; 51 fAtlasLocation.set(0, 0); 52 fTooLargeForAtlas = GrBatchAtlas::GlyphTooLargeForAtlas(bounds.width(), bounds.height()); 53 } 54 freeGrGlyph55 void free() { 56 if (fPath) { 57 delete fPath; 58 fPath = NULL; 59 } 60 } 61 widthGrGlyph62 int width() const { return fBounds.width(); } heightGrGlyph63 int height() const { return fBounds.height(); } isEmptyGrGlyph64 bool isEmpty() const { return fBounds.isEmpty(); } glyphIDGrGlyph65 uint16_t glyphID() const { return UnpackID(fPackedID); } 66 67 /////////////////////////////////////////////////////////////////////////// 68 ExtractSubPixelBitsFromFixedGrGlyph69 static inline unsigned ExtractSubPixelBitsFromFixed(SkFixed pos) { 70 // two most significant fraction bits from fixed-point 71 return (pos >> 14) & 3; 72 } 73 PackGrGlyph74 static inline PackedID Pack(uint16_t glyphID, SkFixed x, SkFixed y, MaskStyle ms) { 75 x = ExtractSubPixelBitsFromFixed(x); 76 y = ExtractSubPixelBitsFromFixed(y); 77 int dfFlag = (ms == kDistance_MaskStyle) ? 0x1 : 0x0; 78 return (dfFlag << 20) | (x << 18) | (y << 16) | glyphID; 79 } 80 UnpackFixedXGrGlyph81 static inline SkFixed UnpackFixedX(PackedID packed) { 82 return ((packed >> 18) & 3) << 14; 83 } 84 UnpackFixedYGrGlyph85 static inline SkFixed UnpackFixedY(PackedID packed) { 86 return ((packed >> 16) & 3) << 14; 87 } 88 UnpackMaskStyleGrGlyph89 static inline MaskStyle UnpackMaskStyle(PackedID packed) { 90 return ((packed >> 20) & 1) ? kDistance_MaskStyle : kCoverage_MaskStyle; 91 } 92 UnpackIDGrGlyph93 static inline uint16_t UnpackID(PackedID packed) { 94 return (uint16_t)packed; 95 } 96 GetKeyGrGlyph97 static inline const GrGlyph::PackedID& GetKey(const GrGlyph& glyph) { 98 return glyph.fPackedID; 99 } 100 HashGrGlyph101 static inline uint32_t Hash(GrGlyph::PackedID key) { 102 return SkChecksum::Mix(key); 103 } 104 }; 105 106 #endif 107