SkSGText.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright 2018 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 SkSGText_DEFINED
  8. #define SkSGText_DEFINED
  9. #include "modules/sksg/include/SkSGGeometryNode.h"
  10. #include "include/core/SkFont.h"
  11. #include "include/core/SkPoint.h"
  12. #include "include/core/SkString.h"
  13. #include "include/core/SkTextBlob.h"
  14. #include "include/utils/SkTextUtils.h"
  15. class SkCanvas;
  16. class SkPaint;
  17. class SkTypeface;
  18. namespace sksg {
  19. /**
  20. * Concrete Geometry node, wrapping a (shaped) SkTextBlob.
  21. */
  22. class Text final : public GeometryNode {
  23. public:
  24. static sk_sp<Text> Make(sk_sp<SkTypeface> tf, const SkString& text);
  25. ~Text() override;
  26. SG_ATTRIBUTE(Typeface, sk_sp<SkTypeface> , fTypeface)
  27. SG_ATTRIBUTE(Text , SkString , fText )
  28. SG_ATTRIBUTE(Position, SkPoint , fPosition)
  29. SG_ATTRIBUTE(Size , SkScalar , fSize )
  30. SG_ATTRIBUTE(ScaleX , SkScalar , fScaleX )
  31. SG_ATTRIBUTE(SkewX , SkScalar , fSkewX )
  32. SG_ATTRIBUTE(Align , SkTextUtils::Align, fAlign )
  33. SG_ATTRIBUTE(Edging , SkFont::Edging , fEdging )
  34. SG_ATTRIBUTE(Hinting , SkFontHinting , fHinting )
  35. // TODO: add shaping functionality.
  36. protected:
  37. void onClip(SkCanvas*, bool antiAlias) const override;
  38. void onDraw(SkCanvas*, const SkPaint&) const override;
  39. bool onContains(const SkPoint&) const override;
  40. SkRect onRevalidate(InvalidationController*, const SkMatrix&) override;
  41. SkPath onAsPath() const override;
  42. private:
  43. Text(sk_sp<SkTypeface>, const SkString&);
  44. SkPoint alignedPosition(SkScalar advance) const;
  45. sk_sp<SkTypeface> fTypeface;
  46. SkString fText;
  47. SkPoint fPosition = SkPoint::Make(0, 0);
  48. SkScalar fSize = 12;
  49. SkScalar fScaleX = 1;
  50. SkScalar fSkewX = 0;
  51. SkTextUtils::Align fAlign = SkTextUtils::kLeft_Align;
  52. SkFont::Edging fEdging = SkFont::Edging::kAntiAlias;
  53. SkFontHinting fHinting = SkFontHinting::kNormal;
  54. sk_sp<SkTextBlob> fBlob; // cached text blob
  55. using INHERITED = GeometryNode;
  56. };
  57. /**
  58. * Concrete Geometry node, wrapping an external SkTextBlob.
  59. */
  60. class TextBlob final : public GeometryNode {
  61. public:
  62. static sk_sp<TextBlob> Make(sk_sp<SkTextBlob> = nullptr);
  63. ~TextBlob() override;
  64. SG_ATTRIBUTE(Blob , sk_sp<SkTextBlob>, fBlob )
  65. SG_ATTRIBUTE(Position, SkPoint , fPosition)
  66. protected:
  67. void onClip(SkCanvas*, bool antiAlias) const override;
  68. void onDraw(SkCanvas*, const SkPaint&) const override;
  69. bool onContains(const SkPoint&) const override;
  70. SkRect onRevalidate(InvalidationController*, const SkMatrix&) override;
  71. SkPath onAsPath() const override;
  72. private:
  73. explicit TextBlob(sk_sp<SkTextBlob>);
  74. sk_sp<SkTextBlob> fBlob;
  75. SkPoint fPosition = SkPoint::Make(0, 0);
  76. using INHERITED = GeometryNode;
  77. };
  78. } // namespace sksg
  79. #endif // SkSGText_DEFINED