blob: 0e534d694afe1fb7c1ef2b518d83938c4910a0bf [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2010 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
reed@google.comac10a2d2010-12-22 21:39:39 +00008#ifndef GrGlyph_DEFINED
9#define GrGlyph_DEFINED
10
reed@google.comac10a2d2010-12-22 21:39:39 +000011#include "GrRect.h"
jvanverth294c3262014-10-10 11:36:12 -070012#include "GrTypes.h"
13
jvanverthe817dbb2014-10-10 08:52:03 -070014#include "SkChecksum.h"
jvanverth294c3262014-10-10 11:36:12 -070015#include "SkPath.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000016
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000017class GrPlot;
reed@google.comac10a2d2010-12-22 21:39:39 +000018
19/* Need this to be quad-state:
20 - complete w/ image
21 - just metrics
22 - failed to get image, but has metrics
23 - failed to get metrics
24 */
25struct GrGlyph {
26 typedef uint32_t PackedID;
27
jvanverth294c3262014-10-10 11:36:12 -070028 GrPlot* fPlot;
29 SkPath* fPath;
30 PackedID fPackedID;
31 GrMaskFormat fMaskFormat;
32 GrIRect16 fBounds;
33 SkIPoint16 fAtlasLocation;
reed@google.comac10a2d2010-12-22 21:39:39 +000034
jvanverth294c3262014-10-10 11:36:12 -070035 void init(GrGlyph::PackedID packed, const SkIRect& bounds, GrMaskFormat format) {
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000036 fPlot = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000037 fPath = NULL;
38 fPackedID = packed;
39 fBounds.set(bounds);
jvanverth294c3262014-10-10 11:36:12 -070040 fMaskFormat = format;
reed@google.comac10a2d2010-12-22 21:39:39 +000041 fAtlasLocation.set(0, 0);
42 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000043
reed@google.comac10a2d2010-12-22 21:39:39 +000044 void free() {
45 if (fPath) {
46 delete fPath;
47 fPath = NULL;
48 }
49 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000050
reed@google.comac10a2d2010-12-22 21:39:39 +000051 int width() const { return fBounds.width(); }
52 int height() const { return fBounds.height(); }
53 bool isEmpty() const { return fBounds.isEmpty(); }
54 uint16_t glyphID() const { return UnpackID(fPackedID); }
55
56 ///////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +000057
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000058 static inline unsigned ExtractSubPixelBitsFromFixed(SkFixed pos) {
reed@google.comac10a2d2010-12-22 21:39:39 +000059 // two most significant fraction bits from fixed-point
60 return (pos >> 14) & 3;
61 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000062
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000063 static inline PackedID Pack(uint16_t glyphID, SkFixed x, SkFixed y) {
reed@google.comac10a2d2010-12-22 21:39:39 +000064 x = ExtractSubPixelBitsFromFixed(x);
65 y = ExtractSubPixelBitsFromFixed(y);
66 return (x << 18) | (y << 16) | glyphID;
67 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000068
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000069 static inline SkFixed UnpackFixedX(PackedID packed) {
reed@google.comac10a2d2010-12-22 21:39:39 +000070 return ((packed >> 18) & 3) << 14;
71 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000072
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000073 static inline SkFixed UnpackFixedY(PackedID packed) {
reed@google.comac10a2d2010-12-22 21:39:39 +000074 return ((packed >> 16) & 3) << 14;
75 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000076
reed@google.comac10a2d2010-12-22 21:39:39 +000077 static inline uint16_t UnpackID(PackedID packed) {
78 return (uint16_t)packed;
79 }
reed@google.comac10a2d2010-12-22 21:39:39 +000080
jvanverthdd6d2272014-07-22 13:25:26 -070081 static inline const GrGlyph::PackedID& GetKey(const GrGlyph& glyph) {
82 return glyph.fPackedID;
83 }
84
85 static inline uint32_t Hash(GrGlyph::PackedID key) {
86 return SkChecksum::Murmur3(&key, sizeof(key));
87 }
88};
reed@google.comac10a2d2010-12-22 21:39:39 +000089
90#endif