1 // Copyright 2019 Google LLC. 2 #ifndef Metrics_DEFINED 3 #define Metrics_DEFINED 4 5 #include <map> 6 #include "modules/skparagraph/include/TextStyle.h" 7 #ifdef ENABLE_TEXT_ENHANCE 8 #include "drawing.h" 9 #endif 10 11 namespace skia { 12 namespace textlayout { 13 class StyleMetrics { 14 public: StyleMetrics(const TextStyle * style)15 StyleMetrics(const TextStyle* style) : text_style(style) {} 16 17 #ifdef ENABLE_TEXT_ENHANCE StyleMetrics(const TextStyle * style,RSFontMetrics & metrics)18 StyleMetrics(const TextStyle* style, RSFontMetrics& metrics) 19 : text_style(style), font_metrics(metrics) {} 20 #else StyleMetrics(const TextStyle * style,SkFontMetrics & metrics)21 StyleMetrics(const TextStyle* style, SkFontMetrics& metrics) 22 : text_style(style), font_metrics(metrics) {} 23 #endif 24 25 const TextStyle* text_style; 26 27 // SkFontMetrics contains the following metrics: 28 // 29 // * Top distance to reserve above baseline 30 // * Ascent distance to reserve below baseline 31 // * Descent extent below baseline 32 // * Bottom extent below baseline 33 // * Leading distance to add between lines 34 // * AvgCharWidth average character width 35 // * MaxCharWidth maximum character width 36 // * XMin minimum x 37 // * XMax maximum x 38 // * XHeight height of lower-case 'x' 39 // * CapHeight height of an upper-case letter 40 // * UnderlineThickness underline thickness 41 // * UnderlinePosition underline position relative to baseline 42 // * StrikeoutThickness strikeout thickness 43 // * StrikeoutPosition strikeout position relative to baseline 44 #ifdef ENABLE_TEXT_ENHANCE 45 RSFontMetrics font_metrics; 46 #else 47 SkFontMetrics font_metrics; 48 #endif 49 }; 50 51 class LineMetrics { 52 public: LineMetrics()53 LineMetrics() { } 54 LineMetrics(size_t start,size_t end,size_t end_excluding_whitespace,size_t end_including_newline,bool hard_break)55 LineMetrics(size_t start, 56 size_t end, 57 size_t end_excluding_whitespace, 58 size_t end_including_newline, 59 bool hard_break) 60 : fStartIndex(start) 61 , fEndIndex(end) 62 , fEndExcludingWhitespaces(end_excluding_whitespace) 63 , fEndIncludingNewline(end_including_newline) 64 , fHardBreak(hard_break) {} 65 // The following fields are used in the layout process itself. 66 67 // The indexes in the text buffer the line begins and ends. 68 size_t fStartIndex = 0; 69 size_t fEndIndex = 0; 70 size_t fEndExcludingWhitespaces = 0; 71 size_t fEndIncludingNewline = 0; 72 bool fHardBreak = false; 73 74 // The following fields are tracked after or during layout to provide to 75 // the user as well as for computing bounding boxes. 76 77 // The final computed ascent and descent for the line. This can be impacted by 78 // the strut, height, scaling, as well as outlying runs that are very tall. 79 // 80 // The top edge is `baseline - ascent` and the bottom edge is `baseline + 81 // descent`. Ascent and descent are provided as positive numbers. Raw numbers 82 // for specific runs of text can be obtained in run_metrics_map. These values 83 // are the cumulative metrics for the entire line. 84 double fAscent = SK_ScalarMax; 85 double fDescent = SK_ScalarMin; 86 double fUnscaledAscent = SK_ScalarMax; 87 // Total height of the paragraph including the current line. 88 // 89 // The height of the current line is `round(ascent + descent)`. 90 double fHeight = 0.0; 91 // Width of the line. 92 double fWidth = 0.0; 93 // The left edge of the line. The right edge can be obtained with `left + 94 // width` 95 double fLeft = 0.0; 96 // The y position of the baseline for this line from the top of the paragraph. 97 double fBaseline = 0.0; 98 // Zero indexed line number 99 size_t fLineNumber = 0; 100 #ifdef ENABLE_TEXT_ENHANCE 101 // Width include spaces 102 double fWidthWithSpaces = 0.0; 103 // Height from the top 104 double fTopHeight = 0.0; 105 #endif 106 107 // Mapping between text index ranges and the FontMetrics associated with 108 // them. The first run will be keyed under start_index. The metrics here 109 // are before layout and are the base values we calculate from. 110 std::map<size_t, StyleMetrics> fLineMetrics; 111 }; 112 113 } // namespace textlayout 114 } // namespace skia 115 116 #endif // Metrics_DEFINED 117