reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 8 | #ifndef GrGlyph_DEFINED |
| 9 | #define GrGlyph_DEFINED |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/gpu/GrTypes.h" |
| 12 | #include "src/gpu/GrDrawOpAtlas.h" |
Michael Ludwig | 663afe5 | 2019-06-03 16:46:19 -0400 | [diff] [blame] | 13 | #include "src/gpu/geometry/GrRect.h" |
jvanverth | 294c326 | 2014-10-10 11:36:12 -0700 | [diff] [blame] | 14 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "include/core/SkPath.h" |
| 16 | #include "include/private/SkChecksum.h" |
| 17 | #include "include/private/SkFixed.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 18 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 19 | struct GrGlyph { |
jvanverth | a763461 | 2015-03-19 06:08:31 -0700 | [diff] [blame] | 20 | enum MaskStyle { |
| 21 | kCoverage_MaskStyle, |
| 22 | kDistance_MaskStyle |
| 23 | }; |
mtklein | 852f15d | 2016-03-17 10:51:27 -0700 | [diff] [blame] | 24 | |
Herb Derby | 5a3fdee | 2018-12-20 14:47:03 -0500 | [diff] [blame] | 25 | static GrMaskFormat FormatFromSkGlyph(const SkGlyph& glyph) { |
Herb Derby | 3721688 | 2019-06-13 17:24:24 -0400 | [diff] [blame^] | 26 | switch (glyph.maskFormat()) { |
Herb Derby | 5a3fdee | 2018-12-20 14:47:03 -0500 | [diff] [blame] | 27 | 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 42 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 43 | |
Herb Derby | 5a3fdee | 2018-12-20 14:47:03 -0500 | [diff] [blame] | 44 | static GrIRect16 BoundsFromSkGlyph(const SkGlyph& glyph) { |
| 45 | return GrIRect16::MakeXYWH(glyph.fLeft, |
| 46 | glyph.fTop, |
| 47 | glyph.fWidth, |
| 48 | glyph.fHeight); |
| 49 | } |
Herb Derby | b03e024 | 2018-12-20 13:19:44 -0500 | [diff] [blame] | 50 | |
Herb Derby | 5a3fdee | 2018-12-20 14:47:03 -0500 | [diff] [blame] | 51 | static MaskStyle MaskStyleFromSkGlyph(const SkGlyph& skGlyph) { |
Herb Derby | 3721688 | 2019-06-13 17:24:24 -0400 | [diff] [blame^] | 52 | return skGlyph.maskFormat() == SkMask::kSDF_Format |
Herb Derby | 5a3fdee | 2018-12-20 14:47:03 -0500 | [diff] [blame] | 53 | ? 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 Derby | 438ea54 | 2018-12-19 18:25:11 -0500 | [diff] [blame] | 63 | |
| 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 Derby | b03e024 | 2018-12-20 13:19:44 -0500 | [diff] [blame] | 87 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 88 | int width() const { return fBounds.width(); } |
| 89 | int height() const { return fBounds.height(); } |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 90 | uint32_t pageIndex() const { return GrDrawOpAtlas::GetPageIndexFromID(fID); } |
Herb Derby | 37e21f6 | 2018-12-19 19:56:02 -0500 | [diff] [blame] | 91 | MaskStyle maskStyle() const { return fMaskStyle; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 92 | |
Herb Derby | 5a3fdee | 2018-12-20 14:47:03 -0500 | [diff] [blame] | 93 | // GetKey and Hash for the the hash table. |
| 94 | static const SkPackedGlyphID& GetKey(const GrGlyph& glyph) { |
jvanverth | dd6d227 | 2014-07-22 13:25:26 -0700 | [diff] [blame] | 95 | return glyph.fPackedID; |
| 96 | } |
| 97 | |
Herb Derby | 5a3fdee | 2018-12-20 14:47:03 -0500 | [diff] [blame] | 98 | static uint32_t Hash(SkPackedGlyphID key) { |
| 99 | return SkChecksum::Mix(key.hash()); |
jvanverth | dd6d227 | 2014-07-22 13:25:26 -0700 | [diff] [blame] | 100 | } |
Herb Derby | 438ea54 | 2018-12-19 18:25:11 -0500 | [diff] [blame] | 101 | |
| 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}; |
jvanverth | dd6d227 | 2014-07-22 13:25:26 -0700 | [diff] [blame] | 108 | }; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 109 | |
| 110 | #endif |