blob: 92f974bc2cf456f8ed367c5f8d0c676baa8a2f26 [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
37 public:
38 static const SkFixed kSubpixelRound = SK_FixedHalf >> SkGlyph::kSubBits;
herbc1e97b32015-03-05 11:51:11 -080039 // A value that can never be generated by MakeID.
40 static const uint32_t kImpossibleID = ~0;
bungeman@google.combbe50132012-07-24 20:33:21 +000041 void* fImage;
42 SkPath* fPath;
43 SkFixed fAdvanceX, fAdvanceY;
44
bungeman@google.combbe50132012-07-24 20:33:21 +000045 uint16_t fWidth, fHeight;
46 int16_t fTop, fLeft;
47
48 uint8_t fMaskFormat;
49 int8_t fRsbDelta, fLsbDelta; // used by auto-kerning
Ben Wagnerb2f7fce2014-08-27 19:17:41 -040050 int8_t fForceBW;
bungeman@google.combbe50132012-07-24 20:33:21 +000051
herbb69d0e02015-02-25 06:47:06 -080052 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.combbe50132012-07-24 20:33:21 +000058 }
59
herbe70de9e2015-02-27 07:22:48 -080060 void initGlyphFromCombinedID(uint32_t combined_id) {
61 this->initCommon(combined_id);
62 }
63
bungeman@google.combbe50132012-07-24 20:33:21 +000064 /**
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;
reedd54d3fc2014-11-13 14:39:58 -080071 } else if (SkMask::kARGB32_Format == format) {
bungeman@google.combbe50132012-07-24 20:33:21 +000072 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.combbe50132012-07-24 20:33:21 +000097 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
herbb69d0e02015-02-25 06:47:06 -0800117 void toMask(SkMask* mask) const;
118
herbc71239b2015-07-21 15:36:25 -0700119 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
herbb69d0e02015-02-25 06:47:06 -0800129 private:
130 // TODO(herb) remove friend statement after SkGlyphCache cleanup.
131 friend class SkGlyphCache;
132
133 void initCommon(uint32_t id) {
134 fID = id;
halcanary96fcdcc2015-08-27 07:41:13 -0700135 fImage = nullptr;
136 fPath = nullptr;
herbb69d0e02015-02-25 06:47:06 -0800137 fMaskFormat = MASK_FORMAT_UNKNOWN;
138 fForceBW = 0;
139 }
herbe70de9e2015-02-27 07:22:48 -0800140
bungeman@google.combbe50132012-07-24 20:33:21 +0000141 static unsigned ID2Code(uint32_t id) {
herbe70de9e2015-02-27 07:22:48 -0800142 return id & kCodeMask;
bungeman@google.combbe50132012-07-24 20:33:21 +0000143 }
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) {
herbe70de9e2015-02-27 07:22:48 -0800163 SkASSERT(code <= kCodeMask);
herbc1e97b32015-03-05 11:51:11 -0800164 SkASSERT(code != kImpossibleID);
mtklein5a2a5e72015-02-09 07:52:51 -0800165 return code;
bungeman@google.combbe50132012-07-24 20:33:21 +0000166 }
167
168 static uint32_t MakeID(unsigned code, SkFixed x, SkFixed y) {
mtklein5a2a5e72015-02-09 07:52:51 -0800169 SkASSERT(code <= kCodeMask);
bungeman@google.combbe50132012-07-24 20:33:21 +0000170 x = FixedToSub(x);
171 y = FixedToSub(y);
herbc1e97b32015-03-05 11:51:11 -0800172 uint32_t ID = (x << (kSubShift + kSubShiftX)) |
173 (y << (kSubShift + kSubShiftY)) |
174 code;
175 SkASSERT(ID != kImpossibleID);
176 return ID;
bungeman@google.combbe50132012-07-24 20:33:21 +0000177 }
178
herbb69d0e02015-02-25 06:47:06 -0800179 // 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.combbe50132012-07-24 20:33:21 +0000185};
mtkleinb68ce742015-11-24 05:35:58 -0800186SK_END_REQUIRE_DENSE
bungeman@google.combbe50132012-07-24 20:33:21 +0000187
188#endif