1 /* 2 * Copyright 2006 The Android Open Source Project 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 SkGlyph_DEFINED 9 #define SkGlyph_DEFINED 10 11 #include "SkTypes.h" 12 #include "SkFixed.h" 13 #include "SkMask.h" 14 15 class SkPath; 16 17 // needs to be != to any valid SkMask::Format 18 #define MASK_FORMAT_UNKNOWN (0xFF) 19 #define MASK_FORMAT_JUST_ADVANCE MASK_FORMAT_UNKNOWN 20 21 #define kMaxGlyphWidth (1<<13) 22 23 struct SkGlyph { 24 void* fImage; 25 SkPath* fPath; 26 SkFixed fAdvanceX, fAdvanceY; 27 28 uint32_t fID; 29 uint16_t fWidth, fHeight; 30 int16_t fTop, fLeft; 31 32 void* fDistanceField; 33 uint8_t fMaskFormat; 34 int8_t fRsbDelta, fLsbDelta; // used by auto-kerning 35 36 void init(uint32_t id) { 37 fID = id; 38 fImage = NULL; 39 fPath = NULL; 40 fDistanceField = NULL; 41 fMaskFormat = MASK_FORMAT_UNKNOWN; 42 } 43 44 /** 45 * Compute the rowbytes for the specified width and mask-format. 46 */ 47 static unsigned ComputeRowBytes(unsigned width, SkMask::Format format) { 48 unsigned rb = width; 49 if (SkMask::kBW_Format == format) { 50 rb = (rb + 7) >> 3; 51 } else if (SkMask::kARGB32_Format == format || 52 SkMask::kLCD32_Format == format) 53 { 54 rb <<= 2; 55 } else if (SkMask::kLCD16_Format == format) { 56 rb = SkAlign4(rb << 1); 57 } else { 58 rb = SkAlign4(rb); 59 } 60 return rb; 61 } 62 63 unsigned rowBytes() const { 64 return ComputeRowBytes(fWidth, (SkMask::Format)fMaskFormat); 65 } 66 67 bool isJustAdvance() const { 68 return MASK_FORMAT_JUST_ADVANCE == fMaskFormat; 69 } 70 71 bool isFullMetrics() const { 72 return MASK_FORMAT_JUST_ADVANCE != fMaskFormat; 73 } 74 75 uint16_t getGlyphID() const { 76 return ID2Code(fID); 77 } 78 79 unsigned getGlyphID(unsigned baseGlyphCount) const { 80 unsigned code = ID2Code(fID); 81 SkASSERT(code >= baseGlyphCount); 82 return code - baseGlyphCount; 83 } 84 85 unsigned getSubX() const { 86 return ID2SubX(fID); 87 } 88 89 SkFixed getSubXFixed() const { 90 return SubToFixed(ID2SubX(fID)); 91 } 92 93 SkFixed getSubYFixed() const { 94 return SubToFixed(ID2SubY(fID)); 95 } 96 97 size_t computeImageSize() const; 98 99 /** Call this to set all of the metrics fields to 0 (e.g. if the scaler 100 encounters an error measuring a glyph). Note: this does not alter the 101 fImage, fPath, fID, fMaskFormat fields. 102 */ 103 void zeroMetrics(); 104 105 enum { 106 kSubBits = 2, 107 kSubMask = ((1 << kSubBits) - 1), 108 kSubShift = 24, // must be large enough for glyphs and unichars 109 kCodeMask = ((1 << kSubShift) - 1), 110 // relative offsets for X and Y subpixel bits 111 kSubShiftX = kSubBits, 112 kSubShiftY = 0 113 }; 114 115 static unsigned ID2Code(uint32_t id) { 116 return id & kCodeMask; 117 } 118 119 static unsigned ID2SubX(uint32_t id) { 120 return id >> (kSubShift + kSubShiftX); 121 } 122 123 static unsigned ID2SubY(uint32_t id) { 124 return (id >> (kSubShift + kSubShiftY)) & kSubMask; 125 } 126 127 static unsigned FixedToSub(SkFixed n) { 128 return (n >> (16 - kSubBits)) & kSubMask; 129 } 130 131 static SkFixed SubToFixed(unsigned sub) { 132 SkASSERT(sub <= kSubMask); 133 return sub << (16 - kSubBits); 134 } 135 136 static uint32_t MakeID(unsigned code) { 137 return code; 138 } 139 140 static uint32_t MakeID(unsigned code, SkFixed x, SkFixed y) { 141 SkASSERT(code <= kCodeMask); 142 x = FixedToSub(x); 143 y = FixedToSub(y); 144 return (x << (kSubShift + kSubShiftX)) | 145 (y << (kSubShift + kSubShiftY)) | 146 code; 147 } 148 149 void toMask(SkMask* mask) const; 150 }; 151 152 #endif 153