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