blob: a379144a4289067c572ded56d9462ecff5d01abd [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"
bsalomon@google.com8d033a12012-04-27 15:52:53 +000012#include "SkPath.h"
jvanverthdd6d2272014-07-22 13:25:26 -070013#include "SkChecksum.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000015class GrPlot;
reed@google.comac10a2d2010-12-22 21:39:39 +000016
17/* Need this to be quad-state:
18 - complete w/ image
19 - just metrics
20 - failed to get image, but has metrics
21 - failed to get metrics
22 */
23struct GrGlyph {
24 typedef uint32_t PackedID;
25
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000026 GrPlot* fPlot;
bsalomon@google.com8d033a12012-04-27 15:52:53 +000027 SkPath* fPath;
reed@google.comac10a2d2010-12-22 21:39:39 +000028 PackedID fPackedID;
29 GrIRect16 fBounds;
robertphillipsd5373412014-06-02 10:20:14 -070030 SkIPoint16 fAtlasLocation;
reed@google.comac10a2d2010-12-22 21:39:39 +000031
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000032 void init(GrGlyph::PackedID packed, const SkIRect& bounds) {
commit-bot@chromium.org7d330eb2013-09-27 19:39:38 +000033 fPlot = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000034 fPath = NULL;
35 fPackedID = packed;
36 fBounds.set(bounds);
37 fAtlasLocation.set(0, 0);
38 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000039
reed@google.comac10a2d2010-12-22 21:39:39 +000040 void free() {
41 if (fPath) {
42 delete fPath;
43 fPath = NULL;
44 }
45 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000046
reed@google.comac10a2d2010-12-22 21:39:39 +000047 int width() const { return fBounds.width(); }
48 int height() const { return fBounds.height(); }
49 bool isEmpty() const { return fBounds.isEmpty(); }
50 uint16_t glyphID() const { return UnpackID(fPackedID); }
51
52 ///////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +000053
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000054 static inline unsigned ExtractSubPixelBitsFromFixed(SkFixed pos) {
reed@google.comac10a2d2010-12-22 21:39:39 +000055 // two most significant fraction bits from fixed-point
56 return (pos >> 14) & 3;
57 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000058
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000059 static inline PackedID Pack(uint16_t glyphID, SkFixed x, SkFixed y) {
reed@google.comac10a2d2010-12-22 21:39:39 +000060 x = ExtractSubPixelBitsFromFixed(x);
61 y = ExtractSubPixelBitsFromFixed(y);
62 return (x << 18) | (y << 16) | glyphID;
63 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000064
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000065 static inline SkFixed UnpackFixedX(PackedID packed) {
reed@google.comac10a2d2010-12-22 21:39:39 +000066 return ((packed >> 18) & 3) << 14;
67 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000068
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000069 static inline SkFixed UnpackFixedY(PackedID packed) {
reed@google.comac10a2d2010-12-22 21:39:39 +000070 return ((packed >> 16) & 3) << 14;
71 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000072
reed@google.comac10a2d2010-12-22 21:39:39 +000073 static inline uint16_t UnpackID(PackedID packed) {
74 return (uint16_t)packed;
75 }
reed@google.comac10a2d2010-12-22 21:39:39 +000076
jvanverthdd6d2272014-07-22 13:25:26 -070077 static inline const GrGlyph::PackedID& GetKey(const GrGlyph& glyph) {
78 return glyph.fPackedID;
79 }
80
81 static inline uint32_t Hash(GrGlyph::PackedID key) {
82 return SkChecksum::Murmur3(&key, sizeof(key));
83 }
84};
reed@google.comac10a2d2010-12-22 21:39:39 +000085
86#endif