GrGlyph.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  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.
  6. */
  7. #ifndef GrGlyph_DEFINED
  8. #define GrGlyph_DEFINED
  9. #include "include/gpu/GrTypes.h"
  10. #include "src/gpu/GrDrawOpAtlas.h"
  11. #include "src/gpu/geometry/GrRect.h"
  12. #include "include/core/SkPath.h"
  13. #include "include/private/SkChecksum.h"
  14. #include "include/private/SkFixed.h"
  15. struct GrGlyph {
  16. enum MaskStyle {
  17. kCoverage_MaskStyle,
  18. kDistance_MaskStyle
  19. };
  20. static GrMaskFormat FormatFromSkGlyph(SkMask::Format format) {
  21. switch (format) {
  22. case SkMask::kBW_Format:
  23. case SkMask::kSDF_Format:
  24. // fall through to kA8 -- we store BW and SDF glyphs in our 8-bit cache
  25. case SkMask::kA8_Format:
  26. return kA8_GrMaskFormat;
  27. case SkMask::k3D_Format:
  28. return kA8_GrMaskFormat; // ignore the mul and add planes, just use the mask
  29. case SkMask::kLCD16_Format:
  30. return kA565_GrMaskFormat;
  31. case SkMask::kARGB32_Format:
  32. return kARGB_GrMaskFormat;
  33. default:
  34. SkDEBUGFAIL("unsupported SkMask::Format");
  35. return kA8_GrMaskFormat;
  36. }
  37. }
  38. static MaskStyle MaskStyleFromSkGlyph(const SkGlyph& skGlyph) {
  39. return skGlyph.maskFormat() == SkMask::kSDF_Format
  40. ? GrGlyph::MaskStyle::kDistance_MaskStyle
  41. : GrGlyph::MaskStyle::kCoverage_MaskStyle;
  42. }
  43. GrGlyph(const SkGlyph& skGlyph)
  44. : fPackedID{skGlyph.getPackedID()}
  45. , fMaskFormat{FormatFromSkGlyph(skGlyph.maskFormat())}
  46. , fMaskStyle{MaskStyleFromSkGlyph(skGlyph)}
  47. , fBounds{GrIRect16::Make(skGlyph.iRect())} {}
  48. SkRect destRect(SkPoint origin) {
  49. return SkRect::MakeXYWH(
  50. SkIntToScalar(fBounds.fLeft) + origin.x(),
  51. SkIntToScalar(fBounds.fTop) + origin.y(),
  52. SkIntToScalar(fBounds.width()),
  53. SkIntToScalar(fBounds.height()));
  54. }
  55. SkRect destRect(SkPoint origin, SkScalar textScale) {
  56. if (fMaskStyle == kCoverage_MaskStyle) {
  57. return SkRect::MakeXYWH(
  58. SkIntToScalar(fBounds.fLeft) * textScale + origin.x(),
  59. SkIntToScalar(fBounds.fTop) * textScale + origin.y(),
  60. SkIntToScalar(fBounds.width()) * textScale,
  61. SkIntToScalar(fBounds.height()) * textScale);
  62. } else {
  63. return SkRect::MakeXYWH(
  64. (SkIntToScalar(fBounds.fLeft) + SK_DistanceFieldInset) * textScale + origin.x(),
  65. (SkIntToScalar(fBounds.fTop) + SK_DistanceFieldInset) * textScale + origin.y(),
  66. (SkIntToScalar(fBounds.width()) - 2 * SK_DistanceFieldInset) * textScale,
  67. (SkIntToScalar(fBounds.height()) - 2 * SK_DistanceFieldInset) * textScale);
  68. }
  69. }
  70. int width() const { return fBounds.width(); }
  71. int height() const { return fBounds.height(); }
  72. uint32_t pageIndex() const { return GrDrawOpAtlas::GetPageIndexFromID(fID); }
  73. MaskStyle maskStyle() const { return fMaskStyle; }
  74. // GetKey and Hash for the the hash table.
  75. static const SkPackedGlyphID& GetKey(const GrGlyph& glyph) {
  76. return glyph.fPackedID;
  77. }
  78. static uint32_t Hash(SkPackedGlyphID key) {
  79. return SkChecksum::Mix(key.hash());
  80. }
  81. const SkPackedGlyphID fPackedID;
  82. const GrMaskFormat fMaskFormat;
  83. const MaskStyle fMaskStyle;
  84. const GrIRect16 fBounds;
  85. SkIPoint16 fAtlasLocation{0, 0};
  86. GrDrawOpAtlas::AtlasID fID{GrDrawOpAtlas::kInvalidAtlasID};
  87. };
  88. #endif