summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/layout/ng/inline/ng_line_height_metrics.h
blob: e5ef21b8580ddd1a844b63d4f822dab244b3fe2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NGLineHeightMetrics_h
#define NGLineHeightMetrics_h

#include "third_party/blink/renderer/platform/fonts/font_baseline.h"
#include "third_party/blink/renderer/platform/layout_unit.h"

namespace blink {

class ComputedStyle;
class FontMetrics;

// Represents line-progression metrics for line boxes and inline boxes.
// Computed for inline boxes, then the metrics of inline boxes are united to
// compute metrics for line boxes.
// https://drafts.csswg.org/css2/visudet.html#line-height
struct NGLineHeightMetrics {
  NGLineHeightMetrics()
      : ascent(LayoutUnit::Min()), descent(LayoutUnit::Min()) {}
  NGLineHeightMetrics(LayoutUnit initial_ascent, LayoutUnit initial_descent)
      : ascent(initial_ascent), descent(initial_descent) {}

  // Compute from ComputedStyle, using the font metrics of the prikmary font.
  // The leading is not included.
  NGLineHeightMetrics(const ComputedStyle&);
  NGLineHeightMetrics(const ComputedStyle&, FontBaseline);

  // Compute from FontMetrics. The leading is not included.
  NGLineHeightMetrics(const FontMetrics&, FontBaseline);

  bool IsEmpty() const { return ascent == LayoutUnit::Min(); }

  bool operator==(const NGLineHeightMetrics& other) const {
    return ascent == other.ascent && descent == other.descent;
  }
  bool operator!=(const NGLineHeightMetrics& other) const {
    return !operator==(other);
  }

  // Add the leading. Half the leading is added to ascent and descent each.
  // https://drafts.csswg.org/css2/visudet.html#leading
  void AddLeading(LayoutUnit line_height);

  // Move the metrics by the specified amount, in line progression direction.
  void Move(LayoutUnit);

  // Unite a metrics for an inline box to a metrics for a line box.
  void Unite(const NGLineHeightMetrics&);

  void operator+=(const NGLineHeightMetrics&);

  // Ascent and descent of glyphs, or synthesized for replaced elements.
  // Then united to compute 'text-top' and 'text-bottom' of line boxes.
  LayoutUnit ascent;
  LayoutUnit descent;

  LayoutUnit LineHeight() const { return ascent + descent; }

 private:
  void Initialize(const FontMetrics&, FontBaseline);
};

}  // namespace blink

#endif  // NGLineHeightMetrics_h