SHARE
TWEET

Untitled

a guest Jan 17th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Idea:  put all of the primitive-shaping functions in a single place in our API.
  3. This will make clear that the NONE of these functions do what you expect them
  4. to do.
  5. */
  6.  
  7. // Copyright 2019 Google LLC.
  8. // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
  9. #ifndef SkSimpleShaper_DEFINED
  10. #define SkSimpleShaper_DEFINED
  11.  
  12. #include "SkFontTypes.h"
  13.  
  14. class SkTypeface;
  15. class SkFont;
  16. class SkPaint;
  17. class SkTextBlob;
  18. template <typename T> class sk_sp;
  19.  
  20. namespace SkSimpleShaper {
  21.  
  22. /** Creates SkTextBlob with a single run.
  23.  
  24.     font contains attributes used to define the run text.
  25.  
  26.     When encoding is SkTextEncoding::kUTF8, SkTextEncoding::kUTF16, or
  27.     SkTextEncoding::kUTF32, this function uses the default
  28.     character-to-glyph mapping from the SkTypeface in font.  It does not
  29.     perform typeface fallback for characters not found in the SkTypeface.
  30.     It does not perform kerning or other complex shaping; glyphs are
  31.     positioned based on their default advances.
  32.  
  33.     @param text        character code points or glyphs drawn
  34.     @param byteLength  byte length of text array
  35.     @param font        text size, typeface, text scale, and so on, used to draw
  36.     @param encoding    text encoding used in the text array
  37.     @return            SkTextBlob constructed from one run
  38. */
  39. sk_sp<SkTextBlob> MakeBlob(const void* text, size_t byteLength, const SkFont& font,
  40.                            SkTextEncoding encoding);
  41.  
  42. /** Creates SkTextBlob with a single run. string meaning depends on SkTextEncoding;
  43.     by default, string is encoded as UTF-8.
  44.  
  45.     font contains attributes used to define the run text.
  46.  
  47.     This function uses the default character-to-glyph mapping from the
  48.     SkTypeface in font.  It does not perform typeface fallback for characters
  49.     not found in the SkTypeface.  It does not perform kerning or other complex
  50.     shaping; glyphs are positioned based on their default advances.
  51.  
  52.     @param string   character code points or glyphs drawn
  53.     @param font     text size, typeface, text scale, and so on, used to draw
  54.     @return         SkTextBlob constructed from one run
  55. */
  56. static inline sk_sp<SkTextBlob> MakeBlobFromString(const char* string, const SkFont& font) {
  57.     return SkSimpleShaper::MakeBlob(string, strlen(string), font, kUTF8_SkTextEncoding);
  58. }
  59.  
  60. /** Converts text into glyph indices.
  61.     Returns the number of glyph indices represented by text.  SkTextEncoding
  62.     specifies how text represents characters or glyphs.  glyphs may be nullptr,
  63.     to compute the glyph count.
  64.  
  65.     Does not check text for valid character codes or valid glyph indices.
  66.  
  67.     If byteLength equals zero, returns zero.  If byteLength includes a partial
  68.     character, the partial character is ignored.
  69.  
  70.     If encoding is kUTF8_SkTextEncoding and text contains an invalid UTF-8
  71.     sequence, zero is returned.
  72.  
  73.     When encoding is SkTextEncoding::kUTF8, SkTextEncoding::kUTF16, or
  74.     SkTextEncoding::kUTF32; then each Unicode codepoint is mapped to a single
  75.     glyph.  This function uses the default character-to-glyph mapping from the
  76.     SkTypeface and maps characters not found in the SkTypeface to zero.
  77.  
  78.     If maxGlyphCount is not sufficient to store all the glyphs, no glyphs are
  79.     copied.  The total glyph count is returned for subsequent buffer
  80.     reallocation.
  81.  
  82.     @param typeface      FIXME, may be nullptr for the default typeface
  83.     @param text          character storage encoded with SkTextEncoding
  84.     @param byteLength    length of character storage in bytes
  85.     @param encoding      one of: kUTF8_SkTextEncoding, kUTF16_SkTextEncoding,
  86.                          kUTF32_SkTextEncoding, kGlyphID_SkTextEncoding
  87.     @param glyphs        storage for glyph indices; may be nullptr
  88.     @param maxGlyphCount storage capacity
  89.     @return              number of glyphs represented by text of length byteLength
  90. */
  91. int TextToGlyphs(const SkTypeface* typeface, const void* text, size_t byteLength,
  92.                  SkTextEncoding encoding, SkGlyphID glyphs[], int maxGlyphCount);
  93.  
  94. /** Returns number of glyphs represented by text.
  95.  
  96.     If encoding is kUTF8_SkTextEncoding, kUTF16_SkTextEncoding, or
  97.     kUTF32_SkTextEncoding; then each Unicode codepoint is mapped to a
  98.     single glyph.
  99.  
  100.     @param typeface      FIXME, may be nullptr for the default typeface
  101.     @param text          character storage encoded with SkTextEncoding
  102.     @param byteLength    length of character storage in bytes
  103.     @param encoding      one of: kUTF8_SkTextEncoding, kUTF16_SkTextEncoding,
  104.                          kUTF32_SkTextEncoding, kGlyphID_SkTextEncoding
  105.     @return              number of glyphs represented by text of length byteLength
  106. */
  107. static inline int CountText(const SkTypeface* typeface, const void* text, size_t byteLength,
  108.                             SkTextEncoding encoding) {
  109.     return SkSimpleShaper::TextToGlyphs(typeface, text, byteLength, encoding, nullptr, 0);
  110. }
  111.  
  112. /** Returns true if all text corresponds to a non-zero glyph index.
  113.     Returns false if any characters in text are not supported in
  114.     SkTypeface.
  115.  
  116.     If SkTextEncoding is kGlyphID_SkTextEncoding,
  117.     returns true if all glyph indices in text are non-zero;
  118.     does not check to see if text contains valid glyph indices for SkTypeface.
  119.  
  120.     Returns true if byteLength is zero.
  121.  
  122.     @param typeface    FIXME, may be nullptr for the default typeface
  123.     @param text        array of characters or glyphs
  124.     @param byteLength  number of bytes in text array
  125.     @param encoding    text encoding
  126.     @return            true if all text corresponds to a non-zero glyph index
  127.  */
  128. bool ContainsText(const SkTypeface* typeface, const void* text, size_t byteLength,
  129.                   SkTextEncoding encoding);
  130.  
  131. /** Returns the bytes of text that fit within maxWidth.
  132.     The text fragment fits if its advance width is less than or equal to maxWidth.
  133.     Measures only while the advance is less than or equal to maxWidth.
  134.     Returns the advance or the text fragment in measuredWidth if it not nullptr.
  135.     Uses encoding to decode text, SkTypeface to get the font metrics,
  136.     and text size to scale the metrics.
  137.     Does not scale the advance or bounds by fake bold.
  138.  
  139.     @param font           FIXME
  140.     @param text           character codes or glyph indices to be measured
  141.     @param length         number of bytes of text to measure
  142.     @param encoding       text encoding
  143.     @param maxWidth       advance limit; text is measured while advance is less than maxWidth
  144.     @param measuredWidth  returns the width of the text less than or equal to maxWidth
  145.     @return               bytes of text that fit, always less than or equal to length
  146. */
  147. size_t BreakText(const SkFont& font, const void* text, size_t length, SkTextEncoding encoding,
  148.                  SkScalar maxWidth, SkScalar* measuredWidth = nullptr);
  149.  
  150. /** Returns the advance width of text.
  151.     The advance is the normal distance to move before drawing additional text.
  152.     Returns the bounding box of text if bounds is not nullptr. paint
  153.     stroke width or SkPathEffect may modify the advance with.
  154.  
  155.     @param font        FIXME
  156.     @param text        character storage encoded with SkTextEncoding
  157.     @param byteLength  length of character storage in bytes
  158.     @param encoding    one of: kUTF8_SkTextEncoding, kUTF16_SkTextEncoding,
  159.                        kUTF32_SkTextEncoding, kGlyphID_SkTextEncoding
  160.     @param bounds      returns bounding box relative to (0, 0) if not nullptr
  161.     @param paint       optional; may be nullptr
  162.     @return            number of glyphs represented by text of length byteLength
  163. */
  164. SkScalar MeasureText(const SkFont& font, const void* text, size_t byteLength,
  165.                      SkTextEncoding encoding, SkRect* bounds, const SkPaint* paint);
  166.  
  167. /** Returns the advance width of text.
  168.     The advance is the normal distance to move before drawing additional text.
  169.     Returns the bounding box of text if bounds is not nullptr.
  170.  
  171.     @param font           FIXME
  172.     @param text        character storage encoded with SkTextEncoding
  173.     @param byteLength  length of character storage in bytes
  174.     @param encoding    one of: kUTF8_SkTextEncoding, kUTF16_SkTextEncoding,
  175.                        kUTF32_SkTextEncoding, kGlyphID_SkTextEncoding
  176.     @param bounds      returns bounding box relative to (0, 0) if not nullptr
  177.     @return            number of glyphs represented by text of length byteLength
  178. */
  179. static inline SkScalar MeasureText(const SkFont& font, const void* text, size_t byteLength,
  180.                                    SkTextEncoding encoding, SkRect* bounds = nullptr) {
  181.     return SkSimpleShaper::MeasureText(font, text, byteLength, encoding, bounds, nullptr);
  182. }
  183.  
  184. /** Retrieves the advance and bounds for each glyph in glyphs.
  185.     Both widths and bounds may be nullptr.
  186.     If widths is not nullptr, widths must be an array of count entries.
  187.     if bounds is not nullptr, bounds must be an array of count entries.
  188.  
  189.     @param font        FIXME
  190.     @param glyphs      array of glyph indices to be measured
  191.     @param count       number of glyphs
  192.     @param widths      returns text advances for each glyph; may be nullptr
  193.     @param bounds      returns bounds for each glyph relative to (0, 0); may be nullptr
  194.     @param paint       optional, specifies stroking, SkPathEffect and SkMaskFilter
  195.  */
  196. void GetWidthsBounds(const SkFont& font, const uint16_t glyphs[], int count, SkScalar widths[],
  197.                      SkRect bounds[], const SkPaint* paint);
  198.  
  199. /** Retrieves the advance and bounds for each glyph in glyphs.
  200.     Both widths and bounds may be nullptr.
  201.     If widths is not nullptr, widths must be an array of count entries.
  202.     if bounds is not nullptr, bounds must be an array of count entries.
  203.  
  204.     @param font        FIXME
  205.     @param glyphs      array of glyph indices to be measured
  206.     @param count       number of glyphs
  207.     @param widths      returns text advances for each glyph
  208.  */
  209. static inline void GetWidths(const SkFont& font, const uint16_t glyphs[], int count,
  210.                              SkScalar widths[]) {
  211.     SkSimpleShaper::GetWidthsBounds(font, glyphs, count, widths, nullptr, nullptr);
  212. }
  213.  
  214. /** Retrieves the bounds for each glyph in glyphs.
  215.     bounds must be an array of count entries.
  216.     If paint is not nullptr, its stroking, SkPathEffect, and SkMaskFilter fields are respected.
  217.  
  218.     @param font        FIXME
  219.     @param glyphs      array of glyph indices to be measured
  220.     @param count       number of glyphs
  221.     @param bounds      returns bounds for each glyph relative to (0, 0); may be nullptr
  222.     @param paint       optional, specifies stroking, SkPathEffect, and SkMaskFilter
  223.  */
  224. static inline void GetBounds(const SkFont& font, const uint16_t glyphs[], int count,
  225.                              SkRect bounds[], const SkPaint* paint) {
  226.     SkSimpleShaper::GetWidthsBounds(font, glyphs, count, nullptr, bounds, paint);
  227. }
  228.  
  229. /** Retrieves the positions for each glyph, beginning at the specified origin. The caller
  230.     must allocated at least count number of elements in the pos[] array.
  231.  
  232.     @param font     FIXME
  233.     @param glyphs   array of glyph indices to be positioned
  234.     @param count    number of glyphs
  235.     @param pos      returns glyphs positions
  236.     @param origin   location of the first glyph. Defaults to {0, 0}.
  237.  */
  238. void GetPos(const SkFont& font, const uint16_t glyphs[], int count, SkPoint pos[],
  239.             SkPoint origin = {0, 0});
  240.  
  241. /** Retrieves the x-positions for each glyph, beginning at the specified origin. The caller
  242.     must allocated at least count number of elements in the xpos[] array.
  243.  
  244.     @param font     FIXME
  245.     @param glyphs   array of glyph indices to be positioned
  246.     @param count    number of glyphs
  247.     @param xpos     returns glyphs x-positions
  248.     @param origin   x-position of the first glyph. Defaults to 0.
  249.  */
  250. void GetXPos(const SkFont& font, const uint16_t glyphs[], int count, SkScalar xpos[],
  251.              SkScalar origin = 0);
  252.  
  253. }  // namespace SkSimpleShaper
  254. #endif  // SkSimpleShaper_DEFINED
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top