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 | |
caryclark | 0449bcf | 2016-02-09 13:25:45 -0800 | [diff] [blame] | 37 | // Support horizontal and vertical skipping strike-through / underlines. |
| 38 | // The caller walks the linked list looking for a match. For a horizontal underline, |
| 39 | // the fBounds contains the top and bottom of the underline. The fInterval pair contains the |
| 40 | // beginning and end of of the intersection of the bounds and the glyph's path. |
| 41 | // If interval[0] >= interval[1], no intesection was found. |
| 42 | struct Intercept { |
| 43 | Intercept* fNext; |
| 44 | SkScalar fBounds[2]; // for horz underlines, the boundaries in Y |
| 45 | SkScalar fInterval[2]; // the outside intersections of the axis and the glyph |
| 46 | }; |
| 47 | |
| 48 | struct PathData { |
| 49 | Intercept* fIntercept; |
| 50 | SkPath* fPath; |
| 51 | }; |
| 52 | |
| 53 | public: |
herb | b69d0e0 | 2015-02-25 06:47:06 -0800 | [diff] [blame] | 54 | static const SkFixed kSubpixelRound = SK_FixedHalf >> SkGlyph::kSubBits; |
herb | c1e97b3 | 2015-03-05 11:51:11 -0800 | [diff] [blame] | 55 | // A value that can never be generated by MakeID. |
| 56 | static const uint32_t kImpossibleID = ~0; |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 57 | void* fImage; |
caryclark | 0449bcf | 2016-02-09 13:25:45 -0800 | [diff] [blame] | 58 | PathData* fPathData; |
benjaminwagner | 6b3eacb | 2016-03-24 19:07:58 -0700 | [diff] [blame^] | 59 | float fAdvanceX, fAdvanceY; |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 60 | |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 61 | uint16_t fWidth, fHeight; |
| 62 | int16_t fTop, fLeft; |
| 63 | |
| 64 | uint8_t fMaskFormat; |
| 65 | int8_t fRsbDelta, fLsbDelta; // used by auto-kerning |
Ben Wagner | b2f7fce | 2014-08-27 19:17:41 -0400 | [diff] [blame] | 66 | int8_t fForceBW; |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 67 | |
herb | b69d0e0 | 2015-02-25 06:47:06 -0800 | [diff] [blame] | 68 | void initWithGlyphID(uint32_t glyph_id) { |
| 69 | this->initCommon(MakeID(glyph_id)); |
| 70 | } |
| 71 | |
| 72 | void initGlyphIdFrom(const SkGlyph& glyph) { |
| 73 | this->initCommon(glyph.fID); |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 74 | } |
| 75 | |
herb | e70de9e | 2015-02-27 07:22:48 -0800 | [diff] [blame] | 76 | void initGlyphFromCombinedID(uint32_t combined_id) { |
| 77 | this->initCommon(combined_id); |
| 78 | } |
| 79 | |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 80 | /** |
| 81 | * Compute the rowbytes for the specified width and mask-format. |
| 82 | */ |
| 83 | static unsigned ComputeRowBytes(unsigned width, SkMask::Format format) { |
| 84 | unsigned rb = width; |
| 85 | if (SkMask::kBW_Format == format) { |
| 86 | rb = (rb + 7) >> 3; |
reed | d54d3fc | 2014-11-13 14:39:58 -0800 | [diff] [blame] | 87 | } else if (SkMask::kARGB32_Format == format) { |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 88 | rb <<= 2; |
| 89 | } else if (SkMask::kLCD16_Format == format) { |
| 90 | rb = SkAlign4(rb << 1); |
| 91 | } else { |
| 92 | rb = SkAlign4(rb); |
| 93 | } |
| 94 | return rb; |
| 95 | } |
| 96 | |
benjaminwagner | 706d21f | 2016-03-07 17:58:03 -0800 | [diff] [blame] | 97 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
benjaminwagner | 2732e18 | 2016-03-16 14:01:39 -0700 | [diff] [blame] | 98 | // Temporary accessor for Android. |
benjaminwagner | 19de504 | 2016-03-07 12:41:49 -0800 | [diff] [blame] | 99 | SkFixed advanceXFixed() const { return fAdvanceX; } |
benjaminwagner | 2732e18 | 2016-03-16 14:01:39 -0700 | [diff] [blame] | 100 | // Temporary accessor for Android. |
benjaminwagner | 19de504 | 2016-03-07 12:41:49 -0800 | [diff] [blame] | 101 | SkFixed advanceYFixed() const { return fAdvanceY; } |
benjaminwagner | 706d21f | 2016-03-07 17:58:03 -0800 | [diff] [blame] | 102 | #endif |
benjaminwagner | 19de504 | 2016-03-07 12:41:49 -0800 | [diff] [blame] | 103 | |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 104 | unsigned rowBytes() const { |
| 105 | return ComputeRowBytes(fWidth, (SkMask::Format)fMaskFormat); |
| 106 | } |
| 107 | |
| 108 | bool isJustAdvance() const { |
| 109 | return MASK_FORMAT_JUST_ADVANCE == fMaskFormat; |
| 110 | } |
| 111 | |
| 112 | bool isFullMetrics() const { |
| 113 | return MASK_FORMAT_JUST_ADVANCE != fMaskFormat; |
| 114 | } |
| 115 | |
| 116 | uint16_t getGlyphID() const { |
| 117 | return ID2Code(fID); |
| 118 | } |
| 119 | |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 120 | unsigned getSubX() const { |
| 121 | return ID2SubX(fID); |
| 122 | } |
| 123 | |
| 124 | SkFixed getSubXFixed() const { |
| 125 | return SubToFixed(ID2SubX(fID)); |
| 126 | } |
| 127 | |
| 128 | SkFixed getSubYFixed() const { |
| 129 | return SubToFixed(ID2SubY(fID)); |
| 130 | } |
| 131 | |
| 132 | size_t computeImageSize() const; |
| 133 | |
| 134 | /** Call this to set all of the metrics fields to 0 (e.g. if the scaler |
| 135 | encounters an error measuring a glyph). Note: this does not alter the |
| 136 | fImage, fPath, fID, fMaskFormat fields. |
| 137 | */ |
| 138 | void zeroMetrics(); |
| 139 | |
herb | b69d0e0 | 2015-02-25 06:47:06 -0800 | [diff] [blame] | 140 | void toMask(SkMask* mask) const; |
| 141 | |
herb | c71239b | 2015-07-21 15:36:25 -0700 | [diff] [blame] | 142 | class HashTraits { |
| 143 | public: |
| 144 | static uint32_t GetKey(const SkGlyph& glyph) { |
| 145 | return glyph.fID; |
| 146 | } |
| 147 | static uint32_t Hash(uint32_t glyphId) { |
| 148 | return SkChecksum::CheapMix(glyphId); |
| 149 | } |
| 150 | }; |
| 151 | |
herb | b69d0e0 | 2015-02-25 06:47:06 -0800 | [diff] [blame] | 152 | private: |
| 153 | // TODO(herb) remove friend statement after SkGlyphCache cleanup. |
| 154 | friend class SkGlyphCache; |
| 155 | |
| 156 | void initCommon(uint32_t id) { |
| 157 | fID = id; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 158 | fImage = nullptr; |
caryclark | 0449bcf | 2016-02-09 13:25:45 -0800 | [diff] [blame] | 159 | fPathData = nullptr; |
herb | b69d0e0 | 2015-02-25 06:47:06 -0800 | [diff] [blame] | 160 | fMaskFormat = MASK_FORMAT_UNKNOWN; |
| 161 | fForceBW = 0; |
| 162 | } |
herb | e70de9e | 2015-02-27 07:22:48 -0800 | [diff] [blame] | 163 | |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 164 | static unsigned ID2Code(uint32_t id) { |
herb | e70de9e | 2015-02-27 07:22:48 -0800 | [diff] [blame] | 165 | return id & kCodeMask; |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | static unsigned ID2SubX(uint32_t id) { |
| 169 | return id >> (kSubShift + kSubShiftX); |
| 170 | } |
| 171 | |
| 172 | static unsigned ID2SubY(uint32_t id) { |
| 173 | return (id >> (kSubShift + kSubShiftY)) & kSubMask; |
| 174 | } |
| 175 | |
| 176 | static unsigned FixedToSub(SkFixed n) { |
| 177 | return (n >> (16 - kSubBits)) & kSubMask; |
| 178 | } |
| 179 | |
| 180 | static SkFixed SubToFixed(unsigned sub) { |
| 181 | SkASSERT(sub <= kSubMask); |
| 182 | return sub << (16 - kSubBits); |
| 183 | } |
| 184 | |
| 185 | static uint32_t MakeID(unsigned code) { |
herb | e70de9e | 2015-02-27 07:22:48 -0800 | [diff] [blame] | 186 | SkASSERT(code <= kCodeMask); |
herb | c1e97b3 | 2015-03-05 11:51:11 -0800 | [diff] [blame] | 187 | SkASSERT(code != kImpossibleID); |
mtklein | 5a2a5e7 | 2015-02-09 07:52:51 -0800 | [diff] [blame] | 188 | return code; |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static uint32_t MakeID(unsigned code, SkFixed x, SkFixed y) { |
mtklein | 5a2a5e7 | 2015-02-09 07:52:51 -0800 | [diff] [blame] | 192 | SkASSERT(code <= kCodeMask); |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 193 | x = FixedToSub(x); |
| 194 | y = FixedToSub(y); |
herb | c1e97b3 | 2015-03-05 11:51:11 -0800 | [diff] [blame] | 195 | uint32_t ID = (x << (kSubShift + kSubShiftX)) | |
| 196 | (y << (kSubShift + kSubShiftY)) | |
| 197 | code; |
| 198 | SkASSERT(ID != kImpossibleID); |
| 199 | return ID; |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 200 | } |
| 201 | |
caryclark | 0449bcf | 2016-02-09 13:25:45 -0800 | [diff] [blame] | 202 | // FIXME - This is needed because the Android frame work directly |
herb | b69d0e0 | 2015-02-25 06:47:06 -0800 | [diff] [blame] | 203 | // accesses fID. Remove when fID accesses are cleaned up. |
| 204 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 205 | public: |
| 206 | #endif |
| 207 | uint32_t fID; |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 208 | }; |
mtklein | b68ce74 | 2015-11-24 05:35:58 -0800 | [diff] [blame] | 209 | SK_END_REQUIRE_DENSE |
bungeman@google.com | bbe5013 | 2012-07-24 20:33:21 +0000 | [diff] [blame] | 210 | |
| 211 | #endif |