blob: 27b3500f25dfba80c72470ef1072c611cfbea123 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/gpu/GrTypes.h"
12#include "src/gpu/GrDrawOpAtlas.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040013#include "src/gpu/geometry/GrRect.h"
jvanverth294c3262014-10-10 11:36:12 -070014
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/core/SkPath.h"
16#include "include/private/SkChecksum.h"
17#include "include/private/SkFixed.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) {
Herb Derby37216882019-06-13 17:24:24 -040026 switch (glyph.maskFormat()) {
Herb Derby5a3fdee2018-12-20 14:47:03 -050027 case SkMask::kBW_Format:
28 case SkMask::kSDF_Format:
29 // fall through to kA8 -- we store BW and SDF glyphs in our 8-bit cache
30 case SkMask::kA8_Format:
31 return kA8_GrMaskFormat;
32 case SkMask::k3D_Format:
33 return kA8_GrMaskFormat; // ignore the mul and add planes, just use the mask
34 case SkMask::kLCD16_Format:
35 return kA565_GrMaskFormat;
36 case SkMask::kARGB32_Format:
37 return kARGB_GrMaskFormat;
38 default:
39 SkDEBUGFAIL("unsupported SkMask::Format");
40 return kA8_GrMaskFormat;
41 }
reed@google.comac10a2d2010-12-22 21:39:39 +000042 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000043
Herb Derby5a3fdee2018-12-20 14:47:03 -050044 static GrIRect16 BoundsFromSkGlyph(const SkGlyph& glyph) {
45 return GrIRect16::MakeXYWH(glyph.fLeft,
46 glyph.fTop,
47 glyph.fWidth,
48 glyph.fHeight);
49 }
Herb Derbyb03e0242018-12-20 13:19:44 -050050
Herb Derby5a3fdee2018-12-20 14:47:03 -050051 static MaskStyle MaskStyleFromSkGlyph(const SkGlyph& skGlyph) {
Herb Derby37216882019-06-13 17:24:24 -040052 return skGlyph.maskFormat() == SkMask::kSDF_Format
Herb Derby5a3fdee2018-12-20 14:47:03 -050053 ? GrGlyph::MaskStyle::kDistance_MaskStyle
54 : GrGlyph::MaskStyle::kCoverage_MaskStyle;
55 }
56
57 GrGlyph(const SkGlyph& skGlyph)
58 : fPackedID{skGlyph.getPackedID()}
59 , fMaskFormat{FormatFromSkGlyph(skGlyph)}
60 , fMaskStyle{MaskStyleFromSkGlyph(skGlyph)}
61 , fBounds{BoundsFromSkGlyph(skGlyph)} {}
62
Herb Derby438ea542018-12-19 18:25:11 -050063
64 SkRect destRect(SkPoint origin) {
65 return SkRect::MakeXYWH(
66 SkIntToScalar(fBounds.fLeft) + origin.x(),
67 SkIntToScalar(fBounds.fTop) + origin.y(),
68 SkIntToScalar(fBounds.width()),
69 SkIntToScalar(fBounds.height()));
70 }
71
72 SkRect destRect(SkPoint origin, SkScalar textScale) {
73 if (fMaskStyle == kCoverage_MaskStyle) {
74 return SkRect::MakeXYWH(
75 SkIntToScalar(fBounds.fLeft) * textScale + origin.x(),
76 SkIntToScalar(fBounds.fTop) * textScale + origin.y(),
77 SkIntToScalar(fBounds.width()) * textScale,
78 SkIntToScalar(fBounds.height()) * textScale);
79 } else {
80 return SkRect::MakeXYWH(
81 (SkIntToScalar(fBounds.fLeft) + SK_DistanceFieldInset) * textScale + origin.x(),
82 (SkIntToScalar(fBounds.fTop) + SK_DistanceFieldInset) * textScale + origin.y(),
83 (SkIntToScalar(fBounds.width()) - 2 * SK_DistanceFieldInset) * textScale,
84 (SkIntToScalar(fBounds.height()) - 2 * SK_DistanceFieldInset) * textScale);
85 }
86 }
Herb Derbyb03e0242018-12-20 13:19:44 -050087
reed@google.comac10a2d2010-12-22 21:39:39 +000088 int width() const { return fBounds.width(); }
89 int height() const { return fBounds.height(); }
Jim Van Vertheafa64b2017-09-18 10:05:00 -040090 uint32_t pageIndex() const { return GrDrawOpAtlas::GetPageIndexFromID(fID); }
Herb Derby37e21f62018-12-19 19:56:02 -050091 MaskStyle maskStyle() const { return fMaskStyle; }
reed@google.comac10a2d2010-12-22 21:39:39 +000092
Herb Derby5a3fdee2018-12-20 14:47:03 -050093 // GetKey and Hash for the the hash table.
94 static const SkPackedGlyphID& GetKey(const GrGlyph& glyph) {
jvanverthdd6d2272014-07-22 13:25:26 -070095 return glyph.fPackedID;
96 }
97
Herb Derby5a3fdee2018-12-20 14:47:03 -050098 static uint32_t Hash(SkPackedGlyphID key) {
99 return SkChecksum::Mix(key.hash());
jvanverthdd6d2272014-07-22 13:25:26 -0700100 }
Herb Derby438ea542018-12-19 18:25:11 -0500101
102 const SkPackedGlyphID fPackedID;
103 const GrMaskFormat fMaskFormat;
104 const MaskStyle fMaskStyle;
105 const GrIRect16 fBounds;
106 SkIPoint16 fAtlasLocation{0, 0};
107 GrDrawOpAtlas::AtlasID fID{GrDrawOpAtlas::kInvalidAtlasID};
jvanverthdd6d2272014-07-22 13:25:26 -0700108};
reed@google.comac10a2d2010-12-22 21:39:39 +0000109
110#endif