SkGlyph.h revision b4c29ac173e6f8844327338687248b98bc94132d
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 15class 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 23struct 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 uint8_t fMaskFormat; 33 int8_t fRsbDelta, fLsbDelta; // used by auto-kerning 34 int8_t fForceBW; 35 36 void init(uint32_t id) { 37 fID = id; 38 fImage = NULL; 39 fPath = NULL; 40 fMaskFormat = MASK_FORMAT_UNKNOWN; 41 fForceBW = 0; 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 rb <<= 2; 53 } else if (SkMask::kLCD16_Format == format) { 54 rb = SkAlign4(rb << 1); 55 } else { 56 rb = SkAlign4(rb); 57 } 58 return rb; 59 } 60 61 unsigned rowBytes() const { 62 return ComputeRowBytes(fWidth, (SkMask::Format)fMaskFormat); 63 } 64 65 bool isJustAdvance() const { 66 return MASK_FORMAT_JUST_ADVANCE == fMaskFormat; 67 } 68 69 bool isFullMetrics() const { 70 return MASK_FORMAT_JUST_ADVANCE != fMaskFormat; 71 } 72 73 uint16_t getGlyphID() const { 74 return ID2Code(fID); 75 } 76 77 unsigned getSubX() const { 78 return ID2SubX(fID); 79 } 80 81 SkFixed getSubXFixed() const { 82 return SubToFixed(ID2SubX(fID)); 83 } 84 85 SkFixed getSubYFixed() const { 86 return SubToFixed(ID2SubY(fID)); 87 } 88 89 size_t computeImageSize() const; 90 91 /** Call this to set all of the metrics fields to 0 (e.g. if the scaler 92 encounters an error measuring a glyph). Note: this does not alter the 93 fImage, fPath, fID, fMaskFormat fields. 94 */ 95 void zeroMetrics(); 96 97 enum { 98 kSubBits = 2, 99 kSubMask = ((1 << kSubBits) - 1), 100 kSubShift = 24, // must be large enough for glyphs and unichars 101 kCodeMask = ((1 << kSubShift) - 1), 102 // relative offsets for X and Y subpixel bits 103 kSubShiftX = kSubBits, 104 kSubShiftY = 0 105 }; 106 107 // The code is increased by one in MakeID. Adjust back. This allows the zero 108 // id to be the invalid id. 109 static unsigned ID2Code(uint32_t id) { 110 return (id & kCodeMask) - 1; 111 } 112 113 static unsigned ID2SubX(uint32_t id) { 114 return id >> (kSubShift + kSubShiftX); 115 } 116 117 static unsigned ID2SubY(uint32_t id) { 118 return (id >> (kSubShift + kSubShiftY)) & kSubMask; 119 } 120 121 static unsigned FixedToSub(SkFixed n) { 122 return (n >> (16 - kSubBits)) & kSubMask; 123 } 124 125 static SkFixed SubToFixed(unsigned sub) { 126 SkASSERT(sub <= kSubMask); 127 return sub << (16 - kSubBits); 128 } 129 130 // This and the MakeID below must not return an id of zero. Zero is used as 131 // the invalid id. 132 static uint32_t MakeID(unsigned code) { 133 SkASSERT(code + 1 <= kCodeMask); 134 return code + 1; 135 } 136 137 // See comment for MakeID above. 138 static uint32_t MakeID(unsigned code, SkFixed x, SkFixed y) { 139 SkASSERT(code + 1 <= kCodeMask); 140 x = FixedToSub(x); 141 y = FixedToSub(y); 142 return (x << (kSubShift + kSubShiftX)) | 143 (y << (kSubShift + kSubShiftY)) | 144 (code + 1); 145 } 146 147 void toMask(SkMask* mask) const; 148}; 149 150#endif 151