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