SkFontPriv.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 SkFontPriv_DEFINED
  8. #define SkFontPriv_DEFINED
  9. #include "include/core/SkFont.h"
  10. #include "include/core/SkMatrix.h"
  11. #include "include/core/SkTypeface.h"
  12. class SkReadBuffer;
  13. class SkWriteBuffer;
  14. class SkFontPriv {
  15. public:
  16. /* This is the size we use when we ask for a glyph's path. We then
  17. * post-transform it as we draw to match the request.
  18. * This is done to try to re-use cache entries for the path.
  19. *
  20. * This value is somewhat arbitrary. In theory, it could be 1, since
  21. * we store paths as floats. However, we get the path from the font
  22. * scaler, and it may represent its paths as fixed-point (or 26.6),
  23. * so we shouldn't ask for something too big (might overflow 16.16)
  24. * or too small (underflow 26.6).
  25. *
  26. * This value could track kMaxSizeForGlyphCache, assuming the above
  27. * constraints, but since we ask for unhinted paths, the two values
  28. * need not match per-se.
  29. */
  30. static constexpr int kCanonicalTextSizeForPaths = 64;
  31. /**
  32. * Return a matrix that applies the paint's text values: size, scale, skew
  33. */
  34. static SkMatrix MakeTextMatrix(SkScalar size, SkScalar scaleX, SkScalar skewX) {
  35. SkMatrix m = SkMatrix::MakeScale(size * scaleX, size);
  36. if (skewX) {
  37. m.postSkew(skewX, 0);
  38. }
  39. return m;
  40. }
  41. static SkMatrix MakeTextMatrix(const SkFont& font) {
  42. return MakeTextMatrix(font.getSize(), font.getScaleX(), font.getSkewX());
  43. }
  44. static void ScaleFontMetrics(SkFontMetrics*, SkScalar);
  45. /**
  46. Returns the union of bounds of all glyphs.
  47. Returned dimensions are computed by font manager from font data,
  48. ignoring SkPaint::Hinting. Includes font metrics, but not fake bold or SkPathEffect.
  49. If text size is large, text scale is one, and text skew is zero,
  50. returns the bounds as:
  51. { SkFontMetrics::fXMin, SkFontMetrics::fTop, SkFontMetrics::fXMax, SkFontMetrics::fBottom }.
  52. @return union of bounds of all glyphs
  53. */
  54. static SkRect GetFontBounds(const SkFont&);
  55. static bool IsFinite(const SkFont& font) {
  56. return SkScalarIsFinite(font.getSize()) &&
  57. SkScalarIsFinite(font.getScaleX()) &&
  58. SkScalarIsFinite(font.getSkewX());
  59. }
  60. // Returns the number of elements (characters or glyphs) in the array.
  61. static int CountTextElements(const void* text, size_t byteLength, SkTextEncoding);
  62. static void GlyphsToUnichars(const SkFont&, const uint16_t glyphs[], int count, SkUnichar[]);
  63. static void Flatten(const SkFont&, SkWriteBuffer& buffer);
  64. static bool Unflatten(SkFont*, SkReadBuffer& buffer);
  65. };
  66. class SkAutoToGlyphs {
  67. public:
  68. SkAutoToGlyphs(const SkFont& font, const void* text, size_t length, SkTextEncoding encoding) {
  69. if (encoding == SkTextEncoding::kGlyphID || length == 0) {
  70. fGlyphs = reinterpret_cast<const uint16_t*>(text);
  71. fCount = length >> 1;
  72. } else {
  73. fCount = font.countText(text, length, encoding);
  74. fStorage.reset(fCount);
  75. font.textToGlyphs(text, length, encoding, fStorage.get(), fCount);
  76. fGlyphs = fStorage.get();
  77. }
  78. }
  79. int count() const { return fCount; }
  80. const uint16_t* glyphs() const { return fGlyphs; }
  81. private:
  82. SkAutoSTArray<32, uint16_t> fStorage;
  83. const uint16_t* fGlyphs;
  84. int fCount;
  85. };
  86. #endif