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 | |
Brian Salomon | 903da79 | 2016-12-16 14:24:46 -0500 | [diff] [blame] | 11 | #include "GrDrawOpAtlas.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 12 | #include "GrRect.h" |
jvanverth | 294c326 | 2014-10-10 11:36:12 -0700 | [diff] [blame] | 13 | #include "GrTypes.h" |
| 14 | |
jvanverth | e817dbb | 2014-10-10 08:52:03 -0700 | [diff] [blame] | 15 | #include "SkChecksum.h" |
benjaminwagner | 6c71e0a | 2016-04-07 08:49:31 -0700 | [diff] [blame] | 16 | #include "SkFixed.h" |
jvanverth | 294c326 | 2014-10-10 11:36:12 -0700 | [diff] [blame] | 17 | #include "SkPath.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) { |
| 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 43 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 44 | |
Herb Derby | 5a3fdee | 2018-12-20 14:47:03 -0500 | [diff] [blame] | 45 | static GrIRect16 BoundsFromSkGlyph(const SkGlyph& glyph) { |
| 46 | return GrIRect16::MakeXYWH(glyph.fLeft, |
| 47 | glyph.fTop, |
| 48 | glyph.fWidth, |
| 49 | glyph.fHeight); |
| 50 | } |
Herb Derby | b03e024 | 2018-12-20 13:19:44 -0500 | [diff] [blame] | 51 | |
Herb Derby | 5a3fdee | 2018-12-20 14:47:03 -0500 | [diff] [blame] | 52 | 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 Derby | 438ea54 | 2018-12-19 18:25:11 -0500 | [diff] [blame] | 64 | |
| 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 Derby | b03e024 | 2018-12-20 13:19:44 -0500 | [diff] [blame] | 88 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 89 | int width() const { return fBounds.width(); } |
| 90 | int height() const { return fBounds.height(); } |
Jim Van Verth | eafa64b | 2017-09-18 10:05:00 -0400 | [diff] [blame] | 91 | uint32_t pageIndex() const { return GrDrawOpAtlas::GetPageIndexFromID(fID); } |
Herb Derby | 37e21f6 | 2018-12-19 19:56:02 -0500 | [diff] [blame] | 92 | MaskStyle maskStyle() const { return fMaskStyle; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 93 | |
Herb Derby | 5a3fdee | 2018-12-20 14:47:03 -0500 | [diff] [blame] | 94 | // GetKey and Hash for the the hash table. |
| 95 | static const SkPackedGlyphID& GetKey(const GrGlyph& glyph) { |
jvanverth | dd6d227 | 2014-07-22 13:25:26 -0700 | [diff] [blame] | 96 | return glyph.fPackedID; |
| 97 | } |
| 98 | |
Herb Derby | 5a3fdee | 2018-12-20 14:47:03 -0500 | [diff] [blame] | 99 | static uint32_t Hash(SkPackedGlyphID key) { |
| 100 | return SkChecksum::Mix(key.hash()); |
jvanverth | dd6d227 | 2014-07-22 13:25:26 -0700 | [diff] [blame] | 101 | } |
Herb Derby | 438ea54 | 2018-12-19 18:25:11 -0500 | [diff] [blame] | 102 | |
| 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}; |
jvanverth | dd6d227 | 2014-07-22 13:25:26 -0700 | [diff] [blame] | 109 | }; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 110 | |
| 111 | #endif |