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 "GrDrawOpAtlas.h" 12 #include "GrRect.h" 13 #include "GrTypes.h" 14 15 #include "SkChecksum.h" 16 #include "SkFixed.h" 17 #include "SkPath.h" 18 19 class GrPlot; 20 21 /* Need this to be quad-state: 22 - complete w/ image 23 - just metrics 24 - failed to get image, but has metrics 25 - failed to get metrics 26 */ 27 struct GrGlyph { 28 enum MaskStyle { 29 kCoverage_MaskStyle, 30 kDistance_MaskStyle 31 }; 32 33 typedef uint32_t PackedID; 34 35 GrDrawOpAtlas::AtlasID fID; 36 SkPath* fPath; 37 PackedID fPackedID; 38 GrMaskFormat fMaskFormat; 39 GrIRect16 fBounds; 40 SkIPoint16 fAtlasLocation; 41 bool fTooLargeForAtlas; 42 initGrGlyph43 void init(GrGlyph::PackedID packed, const SkIRect& bounds, GrMaskFormat format) { 44 fID = GrDrawOpAtlas::kInvalidAtlasID; 45 fPath = nullptr; 46 fPackedID = packed; 47 fBounds.set(bounds); 48 fMaskFormat = format; 49 fAtlasLocation.set(0, 0); 50 fTooLargeForAtlas = GrDrawOpAtlas::GlyphTooLargeForAtlas(bounds.width(), bounds.height()); 51 } 52 resetGrGlyph53 void reset() { 54 if (fPath) { 55 delete fPath; 56 fPath = nullptr; 57 } 58 } 59 widthGrGlyph60 int width() const { return fBounds.width(); } heightGrGlyph61 int height() const { return fBounds.height(); } isEmptyGrGlyph62 bool isEmpty() const { return fBounds.isEmpty(); } glyphIDGrGlyph63 uint16_t glyphID() const { return UnpackID(fPackedID); } 64 65 /////////////////////////////////////////////////////////////////////////// 66 ExtractSubPixelBitsFromFixedGrGlyph67 static inline unsigned ExtractSubPixelBitsFromFixed(SkFixed pos) { 68 // two most significant fraction bits from fixed-point 69 return (pos >> 14) & 3; 70 } 71 PackGrGlyph72 static inline PackedID Pack(uint16_t glyphID, SkFixed x, SkFixed y, MaskStyle ms) { 73 x = ExtractSubPixelBitsFromFixed(x); 74 y = ExtractSubPixelBitsFromFixed(y); 75 int dfFlag = (ms == kDistance_MaskStyle) ? 0x1 : 0x0; 76 return (dfFlag << 20) | (x << 18) | (y << 16) | glyphID; 77 } 78 UnpackFixedXGrGlyph79 static inline SkFixed UnpackFixedX(PackedID packed) { 80 return ((packed >> 18) & 3) << 14; 81 } 82 UnpackFixedYGrGlyph83 static inline SkFixed UnpackFixedY(PackedID packed) { 84 return ((packed >> 16) & 3) << 14; 85 } 86 UnpackMaskStyleGrGlyph87 static inline MaskStyle UnpackMaskStyle(PackedID packed) { 88 return ((packed >> 20) & 1) ? kDistance_MaskStyle : kCoverage_MaskStyle; 89 } 90 UnpackIDGrGlyph91 static inline uint16_t UnpackID(PackedID packed) { 92 return (uint16_t)packed; 93 } 94 GetKeyGrGlyph95 static inline const GrGlyph::PackedID& GetKey(const GrGlyph& glyph) { 96 return glyph.fPackedID; 97 } 98 HashGrGlyph99 static inline uint32_t Hash(GrGlyph::PackedID key) { 100 return SkChecksum::Mix(key); 101 } 102 }; 103 104 #endif 105