bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 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 | |
herb | c71239b | 2015-07-21 15:36:25 -0700 | [diff] [blame] | 11 | #include "SkChecksum.h" |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 12 | #include "SkTypes.h" |
| 13 | #include "SkFixed.h" |
| 14 | #include "SkMask.h" |
| 15 | |
| 16 | class SkPath; |
herb | b69d0e0 | 2015-02-25 06:47:06 -0800 | [diff] [blame] | 17 | class SkGlyphCache; |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 18 | |
| 19 | // needs to be != to any valid SkMask::Format |
| 20 | #define MASK_FORMAT_UNKNOWN (0xFF) |
| 21 | #define MASK_FORMAT_JUST_ADVANCE MASK_FORMAT_UNKNOWN |
| 22 | |
| 23 | #define kMaxGlyphWidth (1<<13) |
| 24 | |
mtklein | b68ce74 | 2015-11-24 05:35:58 -0800 | [diff] [blame^] | 25 | SK_BEGIN_REQUIRE_DENSE |
herb | b69d0e0 | 2015-02-25 06:47:06 -0800 | [diff] [blame] | 26 | class SkGlyph { |
| 27 | enum { |
| 28 | kSubBits = 2, |
| 29 | kSubMask = ((1 << kSubBits) - 1), |
| 30 | kSubShift = 24, // must be large enough for glyphs and unichars |
| 31 | kCodeMask = ((1 << kSubShift) - 1), |
| 32 | // relative offsets for X and Y subpixel bits |
| 33 | kSubShiftX = kSubBits, |
| 34 | kSubShiftY = 0 |
| 35 | }; |
| 36 | |
| 37 | public: |
| 38 | static const SkFixed kSubpixelRound = SK_FixedHalf >> SkGlyph::kSubBits; |
herb | c1e97b3 | 2015-03-05 11:51:11 -0800 | [diff] [blame] | 39 | // A value that can never be generated by MakeID. |
| 40 | static const uint32_t kImpossibleID = ~0; |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 41 | void* fImage; |
| 42 | SkPath* fPath; |
| 43 | SkFixed fAdvanceX, fAdvanceY; |
| 44 | |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 45 | uint16_t fWidth, fHeight; |
| 46 | int16_t fTop, fLeft; |
| 47 | |
| 48 | uint8_t fMaskFormat; |
| 49 | int8_t fRsbDelta, fLsbDelta; // used by auto-kerning |
Ben Wagner | b2f7fce | 2014-08-27 19:17:41 -0400 | [diff] [blame] | 50 | int8_t fForceBW; |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 51 | |
herb | b69d0e0 | 2015-02-25 06:47:06 -0800 | [diff] [blame] | 52 | void initWithGlyphID(uint32_t glyph_id) { |
| 53 | this->initCommon(MakeID(glyph_id)); |
| 54 | } |
| 55 | |
| 56 | void initGlyphIdFrom(const SkGlyph& glyph) { |
| 57 | this->initCommon(glyph.fID); |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 58 | } |
| 59 | |
herb | e70de9e | 2015-02-27 07:22:48 -0800 | [diff] [blame] | 60 | void initGlyphFromCombinedID(uint32_t combined_id) { |
| 61 | this->initCommon(combined_id); |
| 62 | } |
| 63 | |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 64 | /** |
| 65 | * Compute the rowbytes for the specified width and mask-format. |
| 66 | */ |
| 67 | static unsigned ComputeRowBytes(unsigned width, SkMask::Format format) { |
| 68 | unsigned rb = width; |
| 69 | if (SkMask::kBW_Format == format) { |
| 70 | rb = (rb + 7) >> 3; |
reed | d54d3fc | 2014-11-13 14:39:58 -0800 | [diff] [blame] | 71 | } else if (SkMask::kARGB32_Format == format) { |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 72 | rb <<= 2; |
| 73 | } else if (SkMask::kLCD16_Format == format) { |
| 74 | rb = SkAlign4(rb << 1); |
| 75 | } else { |
| 76 | rb = SkAlign4(rb); |
| 77 | } |
| 78 | return rb; |
| 79 | } |
| 80 | |
| 81 | unsigned rowBytes() const { |
| 82 | return ComputeRowBytes(fWidth, (SkMask::Format)fMaskFormat); |
| 83 | } |
| 84 | |
| 85 | bool isJustAdvance() const { |
| 86 | return MASK_FORMAT_JUST_ADVANCE == fMaskFormat; |
| 87 | } |
| 88 | |
| 89 | bool isFullMetrics() const { |
| 90 | return MASK_FORMAT_JUST_ADVANCE != fMaskFormat; |
| 91 | } |
| 92 | |
| 93 | uint16_t getGlyphID() const { |
| 94 | return ID2Code(fID); |
| 95 | } |
| 96 | |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 97 | unsigned getSubX() const { |
| 98 | return ID2SubX(fID); |
| 99 | } |
| 100 | |
| 101 | SkFixed getSubXFixed() const { |
| 102 | return SubToFixed(ID2SubX(fID)); |
| 103 | } |
| 104 | |
| 105 | SkFixed getSubYFixed() const { |
| 106 | return SubToFixed(ID2SubY(fID)); |
| 107 | } |
| 108 | |
| 109 | size_t computeImageSize() const; |
| 110 | |
| 111 | /** Call this to set all of the metrics fields to 0 (e.g. if the scaler |
| 112 | encounters an error measuring a glyph). Note: this does not alter the |
| 113 | fImage, fPath, fID, fMaskFormat fields. |
| 114 | */ |
| 115 | void zeroMetrics(); |
| 116 | |
herb | b69d0e0 | 2015-02-25 06:47:06 -0800 | [diff] [blame] | 117 | void toMask(SkMask* mask) const; |
| 118 | |
herb | c71239b | 2015-07-21 15:36:25 -0700 | [diff] [blame] | 119 | class HashTraits { |
| 120 | public: |
| 121 | static uint32_t GetKey(const SkGlyph& glyph) { |
| 122 | return glyph.fID; |
| 123 | } |
| 124 | static uint32_t Hash(uint32_t glyphId) { |
| 125 | return SkChecksum::CheapMix(glyphId); |
| 126 | } |
| 127 | }; |
| 128 | |
herb | b69d0e0 | 2015-02-25 06:47:06 -0800 | [diff] [blame] | 129 | private: |
| 130 | // TODO(herb) remove friend statement after SkGlyphCache cleanup. |
| 131 | friend class SkGlyphCache; |
| 132 | |
| 133 | void initCommon(uint32_t id) { |
| 134 | fID = id; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 135 | fImage = nullptr; |
| 136 | fPath = nullptr; |
herb | b69d0e0 | 2015-02-25 06:47:06 -0800 | [diff] [blame] | 137 | fMaskFormat = MASK_FORMAT_UNKNOWN; |
| 138 | fForceBW = 0; |
| 139 | } |
herb | e70de9e | 2015-02-27 07:22:48 -0800 | [diff] [blame] | 140 | |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 141 | static unsigned ID2Code(uint32_t id) { |
herb | e70de9e | 2015-02-27 07:22:48 -0800 | [diff] [blame] | 142 | return id & kCodeMask; |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static unsigned ID2SubX(uint32_t id) { |
| 146 | return id >> (kSubShift + kSubShiftX); |
| 147 | } |
| 148 | |
| 149 | static unsigned ID2SubY(uint32_t id) { |
| 150 | return (id >> (kSubShift + kSubShiftY)) & kSubMask; |
| 151 | } |
| 152 | |
| 153 | static unsigned FixedToSub(SkFixed n) { |
| 154 | return (n >> (16 - kSubBits)) & kSubMask; |
| 155 | } |
| 156 | |
| 157 | static SkFixed SubToFixed(unsigned sub) { |
| 158 | SkASSERT(sub <= kSubMask); |
| 159 | return sub << (16 - kSubBits); |
| 160 | } |
| 161 | |
| 162 | static uint32_t MakeID(unsigned code) { |
herb | e70de9e | 2015-02-27 07:22:48 -0800 | [diff] [blame] | 163 | SkASSERT(code <= kCodeMask); |
herb | c1e97b3 | 2015-03-05 11:51:11 -0800 | [diff] [blame] | 164 | SkASSERT(code != kImpossibleID); |
mtklein | 5a2a5e7 | 2015-02-09 07:52:51 -0800 | [diff] [blame] | 165 | return code; |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | static uint32_t MakeID(unsigned code, SkFixed x, SkFixed y) { |
mtklein | 5a2a5e7 | 2015-02-09 07:52:51 -0800 | [diff] [blame] | 169 | SkASSERT(code <= kCodeMask); |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 170 | x = FixedToSub(x); |
| 171 | y = FixedToSub(y); |
herb | c1e97b3 | 2015-03-05 11:51:11 -0800 | [diff] [blame] | 172 | uint32_t ID = (x << (kSubShift + kSubShiftX)) | |
| 173 | (y << (kSubShift + kSubShiftY)) | |
| 174 | code; |
| 175 | SkASSERT(ID != kImpossibleID); |
| 176 | return ID; |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 177 | } |
| 178 | |
herb | b69d0e0 | 2015-02-25 06:47:06 -0800 | [diff] [blame] | 179 | // FIXME - This is needed because the Android frame work directly |
| 180 | // accesses fID. Remove when fID accesses are cleaned up. |
| 181 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 182 | public: |
| 183 | #endif |
| 184 | uint32_t fID; |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 185 | }; |
mtklein | b68ce74 | 2015-11-24 05:35:58 -0800 | [diff] [blame^] | 186 | SK_END_REQUIRE_DENSE |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 187 | |
| 188 | #endif |