summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/fonts/shaping/shape_result_spacing.h
blob: bd17332b7d049c927a022e41d75130d682666f1c (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2016 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 THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_SHAPING_SHAPE_RESULT_SPACING_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_SHAPING_SHAPE_RESULT_SPACING_H_

#include "third_party/blink/renderer/platform/platform_export.h"
#include "third_party/blink/renderer/platform/text/character.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"

namespace blink {

class FontDescription;

// A context object to apply letter-spacing, word-spacing, and justification to
// ShapeResult.
template <typename TextContainerType>
class PLATFORM_EXPORT ShapeResultSpacing final {
  STACK_ALLOCATED();

 public:
  explicit ShapeResultSpacing(const TextContainerType& text,
                              bool allow_word_spacing_anywhere = false)
      : text_(text),
        letter_spacing_(0),
        word_spacing_(0),
        expansion_(0),
        expansion_per_opportunity_(0),
        expansion_opportunity_count_(0),
        text_justify_(TextJustify::kAuto),
        has_spacing_(false),
        normalize_space_(false),
        allow_tabs_(false),
        is_after_expansion_(false),
        allow_word_spacing_anywhere_(allow_word_spacing_anywhere) {}

  const TextContainerType& Text() const { return text_; }
  float LetterSpacing() const { return has_spacing_ ? letter_spacing_ : .0f; }
  float WordSpacing() const { return has_spacing_ ? word_spacing_ : .0f; }
  bool HasSpacing() const { return has_spacing_; }
  bool HasExpansion() const { return expansion_opportunity_count_; }
  unsigned ExpansionOppotunityCount() const {
    return expansion_opportunity_count_;
  }

  // Set letter-spacing and word-spacing.
  bool SetSpacing(const FontDescription&);

  // Set the expansion for the justification.
  void SetExpansion(float expansion,
                    TextDirection,
                    TextJustify,
                    bool allows_leading_expansion = false,
                    bool allows_trailing_expansion = false);

  // Set letter-spacing, word-spacing, and
  // justification. Available only for TextRun.
  void SetSpacingAndExpansion(const FontDescription&);

  // Compute the sum of all spacings for the specified |index|.
  // The |index| is for the |TextContainerType| given in the constructor.
  // For justification, this function must be called incrementally since it
  // keeps states and counts consumed justification opportunities.
  struct ComputeSpacingParameters {
    unsigned index;
    float original_advance = 0.0;
  };
  float ComputeSpacing(unsigned index, float& offset) {
    return ComputeSpacing(ComputeSpacingParameters{.index = index}, offset);
  }
  float ComputeSpacing(const ComputeSpacingParameters& parameters,
                       float& offset);

 private:
  bool IsAfterExpansion() const { return is_after_expansion_; }

  void ComputeExpansion(bool allows_leading_expansion,
                        bool allows_trailing_expansion,
                        TextDirection,
                        TextJustify);

  float NextExpansion();

  const TextContainerType& text_;
  float letter_spacing_;
  float word_spacing_;
  float expansion_;
  float expansion_per_opportunity_;
  unsigned expansion_opportunity_count_;
  TextJustify text_justify_;
  bool has_spacing_;
  bool normalize_space_;
  bool allow_tabs_;
  bool is_after_expansion_;
  bool allow_word_spacing_anywhere_;
};

// Forward declare so no implicit instantiations happen before the
// first explicit instantiation (which would be a C++ violation).
template <>
void ShapeResultSpacing<TextRun>::SetSpacingAndExpansion(
    const FontDescription&);
}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_SHAPING_SHAPE_RESULT_SPACING_H_