blob: 45f333d5ff0ef5bdc061cf64a396a02b53e328f7 [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
Brian Salomon903da792016-12-16 14:24:46 -050011#include "GrDrawOpAtlas.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000012#include "GrRect.h"
jvanverth294c3262014-10-10 11:36:12 -070013#include "GrTypes.h"
14
jvanverthe817dbb2014-10-10 08:52:03 -070015#include "SkChecksum.h"
benjaminwagner6c71e0a2016-04-07 08:49:31 -070016#include "SkFixed.h"
jvanverth294c3262014-10-10 11:36:12 -070017#include "SkPath.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000018
reed@google.comac10a2d2010-12-22 21:39:39 +000019struct GrGlyph {
jvanvertha7634612015-03-19 06:08:31 -070020 enum MaskStyle {
21 kCoverage_MaskStyle,
22 kDistance_MaskStyle
23 };
mtklein852f15d2016-03-17 10:51:27 -070024
Herb Derby5a3fdee2018-12-20 14:47:03 -050025 static GrMaskFormat FormatFromSkGlyph(const SkGlyph& glyph) {
26 SkMask::Format format = static_cast<SkMask::Format>(glyph.fMaskFormat);
27 switch (format) {
28 case SkMask::kBW_Format:
29 case SkMask::kSDF_Format:
30 // fall through to kA8 -- we store BW and SDF glyphs in our 8-bit cache
31 case SkMask::kA8_Format:
32 return kA8_GrMaskFormat;
33 case SkMask::k3D_Format:
34 return kA8_GrMaskFormat; // ignore the mul and add planes, just use the mask
35 case SkMask::kLCD16_Format:
36 return kA565_GrMaskFormat;
37 case SkMask::kARGB32_Format:
38 return kARGB_GrMaskFormat;
39 default:
40 SkDEBUGFAIL("unsupported SkMask::Format");
41 return kA8_GrMaskFormat;
42 }
reed@google.comac10a2d2010-12-22 21:39:39 +000043 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000044
Herb Derby5a3fdee2018-12-20 14:47:03 -050045 static GrIRect16 BoundsFromSkGlyph(const SkGlyph& glyph) {
46 return GrIRect16::MakeXYWH(glyph.fLeft,
47 glyph.fTop,
48 glyph.fWidth,
49 glyph.fHeight);
50 }
Herb Derbyb03e0242018-12-20 13:19:44 -050051
Herb Derby5a3fdee2018-12-20 14:47:03 -050052 static MaskStyle MaskStyleFromSkGlyph(const SkGlyph& skGlyph) {
53 return (SkMask::Format)skGlyph.fMaskFormat == SkMask::kSDF_Format
54 ? GrGlyph::MaskStyle::kDistance_MaskStyle
55 : GrGlyph::MaskStyle::kCoverage_MaskStyle;
56 }
57
58 GrGlyph(const SkGlyph& skGlyph)
59 : fPackedID{skGlyph.getPackedID()}
60 , fMaskFormat{FormatFromSkGlyph(skGlyph)}
61 , fMaskStyle{MaskStyleFromSkGlyph(skGlyph)}
62 , fBounds{BoundsFromSkGlyph(skGlyph)} {}
63
Herb Derby438ea542018-12-19 18:25:11 -050064
65 SkRect destRect(SkPoint origin) {
66 return SkRect::MakeXYWH(
67 SkIntToScalar(fBounds.fLeft) + origin.x(),
68 SkIntToScalar(fBounds.fTop) + origin.y(),
69 SkIntToScalar(fBounds.width()),
70 SkIntToScalar(fBounds.height()));
71 }
72
73 SkRect destRect(SkPoint origin, SkScalar textScale) {
74 if (fMaskStyle == kCoverage_MaskStyle) {
75 return SkRect::MakeXYWH(
76 SkIntToScalar(fBounds.fLeft) * textScale + origin.x(),
77 SkIntToScalar(fBounds.fTop) * textScale + origin.y(),
78 SkIntToScalar(fBounds.width()) * textScale,
79 SkIntToScalar(fBounds.height()) * textScale);
80 } else {
81 return SkRect::MakeXYWH(
82 (SkIntToScalar(fBounds.fLeft) + SK_DistanceFieldInset) * textScale + origin.x(),
83 (SkIntToScalar(fBounds.fTop) + SK_DistanceFieldInset) * textScale + origin.y(),
84 (SkIntToScalar(fBounds.width()) - 2 * SK_DistanceFieldInset) * textScale,
85 (SkIntToScalar(fBounds.height()) - 2 * SK_DistanceFieldInset) * textScale);
86 }
87 }
Herb Derbyb03e0242018-12-20 13:19:44 -050088
reed@google.comac10a2d2010-12-22 21:39:39 +000089 int width() const { return fBounds.width(); }
90 int height() const { return fBounds.height(); }
Jim Van Vertheafa64b2017-09-18 10:05:00 -040091 uint32_t pageIndex() const { return GrDrawOpAtlas::GetPageIndexFromID(fID); }
Herb Derby37e21f62018-12-19 19:56:02 -050092 MaskStyle maskStyle() const { return fMaskStyle; }
reed@google.comac10a2d2010-12-22 21:39:39 +000093
Herb Derby5a3fdee2018-12-20 14:47:03 -050094 // GetKey and Hash for the the hash table.
95 static const SkPackedGlyphID& GetKey(const GrGlyph& glyph) {
jvanverthdd6d2272014-07-22 13:25:26 -070096 return glyph.fPackedID;
97 }
98
Herb Derby5a3fdee2018-12-20 14:47:03 -050099 static uint32_t Hash(SkPackedGlyphID key) {
100 return SkChecksum::Mix(key.hash());
jvanverthdd6d2272014-07-22 13:25:26 -0700101 }
Herb Derby438ea542018-12-19 18:25:11 -0500102
103 const SkPackedGlyphID fPackedID;
104 const GrMaskFormat fMaskFormat;
105 const MaskStyle fMaskStyle;
106 const GrIRect16 fBounds;
107 SkIPoint16 fAtlasLocation{0, 0};
108 GrDrawOpAtlas::AtlasID fID{GrDrawOpAtlas::kInvalidAtlasID};
jvanverthdd6d2272014-07-22 13:25:26 -0700109};
reed@google.comac10a2d2010-12-22 21:39:39 +0000110
111#endif