blob: 91aa8429f9a9d15f470168a6b14252c299890488 [file] [log] [blame]
bungeman@google.combbe50132012-07-24 20:33:21 +00001/*
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
herbc71239b2015-07-21 15:36:25 -070011#include "SkChecksum.h"
bungeman@google.combbe50132012-07-24 20:33:21 +000012#include "SkTypes.h"
13#include "SkFixed.h"
14#include "SkMask.h"
15
16class SkPath;
herbb69d0e02015-02-25 06:47:06 -080017class SkGlyphCache;
bungeman@google.combbe50132012-07-24 20:33:21 +000018
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
mtkleinb68ce742015-11-24 05:35:58 -080025SK_BEGIN_REQUIRE_DENSE
herbb69d0e02015-02-25 06:47:06 -080026class 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
caryclark0449bcf2016-02-09 13:25:45 -080037 // 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
53public:
herbb69d0e02015-02-25 06:47:06 -080054 static const SkFixed kSubpixelRound = SK_FixedHalf >> SkGlyph::kSubBits;
herbc1e97b32015-03-05 11:51:11 -080055 // A value that can never be generated by MakeID.
56 static const uint32_t kImpossibleID = ~0;
bungeman@google.combbe50132012-07-24 20:33:21 +000057 void* fImage;
caryclark0449bcf2016-02-09 13:25:45 -080058 PathData* fPathData;
benjaminwagner6b3eacb2016-03-24 19:07:58 -070059 float fAdvanceX, fAdvanceY;
bungeman@google.combbe50132012-07-24 20:33:21 +000060
bungeman@google.combbe50132012-07-24 20:33:21 +000061 uint16_t fWidth, fHeight;
62 int16_t fTop, fLeft;
63
64 uint8_t fMaskFormat;
65 int8_t fRsbDelta, fLsbDelta; // used by auto-kerning
Ben Wagnerb2f7fce2014-08-27 19:17:41 -040066 int8_t fForceBW;
bungeman@google.combbe50132012-07-24 20:33:21 +000067
herbb69d0e02015-02-25 06:47:06 -080068 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.combbe50132012-07-24 20:33:21 +000074 }
75
herbe70de9e2015-02-27 07:22:48 -080076 void initGlyphFromCombinedID(uint32_t combined_id) {
77 this->initCommon(combined_id);
78 }
79
bungeman@google.combbe50132012-07-24 20:33:21 +000080 /**
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;
reedd54d3fc2014-11-13 14:39:58 -080087 } else if (SkMask::kARGB32_Format == format) {
bungeman@google.combbe50132012-07-24 20:33:21 +000088 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
benjaminwagner706d21f2016-03-07 17:58:03 -080097#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
benjaminwagner2732e182016-03-16 14:01:39 -070098 // Temporary accessor for Android.
benjaminwagner19de5042016-03-07 12:41:49 -080099 SkFixed advanceXFixed() const { return fAdvanceX; }
benjaminwagner2732e182016-03-16 14:01:39 -0700100 // Temporary accessor for Android.
benjaminwagner19de5042016-03-07 12:41:49 -0800101 SkFixed advanceYFixed() const { return fAdvanceY; }
benjaminwagner706d21f2016-03-07 17:58:03 -0800102#endif
benjaminwagner19de5042016-03-07 12:41:49 -0800103
bungeman@google.combbe50132012-07-24 20:33:21 +0000104 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.combbe50132012-07-24 20:33:21 +0000120 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
herbb69d0e02015-02-25 06:47:06 -0800140 void toMask(SkMask* mask) const;
141
herbc71239b2015-07-21 15:36:25 -0700142 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
herbb69d0e02015-02-25 06:47:06 -0800152 private:
153 // TODO(herb) remove friend statement after SkGlyphCache cleanup.
154 friend class SkGlyphCache;
155
156 void initCommon(uint32_t id) {
157 fID = id;
halcanary96fcdcc2015-08-27 07:41:13 -0700158 fImage = nullptr;
caryclark0449bcf2016-02-09 13:25:45 -0800159 fPathData = nullptr;
herbb69d0e02015-02-25 06:47:06 -0800160 fMaskFormat = MASK_FORMAT_UNKNOWN;
161 fForceBW = 0;
162 }
herbe70de9e2015-02-27 07:22:48 -0800163
bungeman@google.combbe50132012-07-24 20:33:21 +0000164 static unsigned ID2Code(uint32_t id) {
herbe70de9e2015-02-27 07:22:48 -0800165 return id & kCodeMask;
bungeman@google.combbe50132012-07-24 20:33:21 +0000166 }
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) {
herbe70de9e2015-02-27 07:22:48 -0800186 SkASSERT(code <= kCodeMask);
herbc1e97b32015-03-05 11:51:11 -0800187 SkASSERT(code != kImpossibleID);
mtklein5a2a5e72015-02-09 07:52:51 -0800188 return code;
bungeman@google.combbe50132012-07-24 20:33:21 +0000189 }
190
191 static uint32_t MakeID(unsigned code, SkFixed x, SkFixed y) {
mtklein5a2a5e72015-02-09 07:52:51 -0800192 SkASSERT(code <= kCodeMask);
bungeman@google.combbe50132012-07-24 20:33:21 +0000193 x = FixedToSub(x);
194 y = FixedToSub(y);
herbc1e97b32015-03-05 11:51:11 -0800195 uint32_t ID = (x << (kSubShift + kSubShiftX)) |
196 (y << (kSubShift + kSubShiftY)) |
197 code;
198 SkASSERT(ID != kImpossibleID);
199 return ID;
bungeman@google.combbe50132012-07-24 20:33:21 +0000200 }
201
caryclark0449bcf2016-02-09 13:25:45 -0800202 // FIXME - This is needed because the Android frame work directly
herbb69d0e02015-02-25 06:47:06 -0800203 // 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.combbe50132012-07-24 20:33:21 +0000208};
mtkleinb68ce742015-11-24 05:35:58 -0800209SK_END_REQUIRE_DENSE
bungeman@google.combbe50132012-07-24 20:33:21 +0000210
211#endif