summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/fonts/shaping/shape_result_bloberizer.h
blob: 55c455f91e472408500c6da11fcc11d86be6716f (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// 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 THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_SHAPING_SHAPE_RESULT_BLOBERIZER_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_SHAPING_SHAPE_RESULT_BLOBERIZER_H_

#include "third_party/blink/renderer/platform/fonts/canvas_rotation_in_vertical.h"
#include "third_party/blink/renderer/platform/fonts/glyph.h"
#include "third_party/blink/renderer/platform/fonts/shaping/shape_result_buffer.h"
#include "third_party/blink/renderer/platform/fonts/simple_font_data.h"
#include "third_party/blink/renderer/platform/geometry/float_point.h"
#include "third_party/blink/renderer/platform/platform_export.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"
#include "third_party/skia/include/core/SkTextBlob.h"

namespace blink {

class FontDescription;
struct TextRunPaintInfo;

class PLATFORM_EXPORT ShapeResultBloberizer {
  STACK_ALLOCATED();

 public:
  enum class Type { kNormal, kTextIntercepts, kEmitText };

  struct FillGlyphsNG;
  struct FillTextEmphasisGlyphsNG;

  struct FillGlyphs;
  struct FillTextEmphasisGlyphs;

  ShapeResultBloberizer(const FontDescription&,
                        float device_scale_factor,
                        Type);
  ShapeResultBloberizer(const ShapeResultBloberizer&) = delete;
  ShapeResultBloberizer& operator=(const ShapeResultBloberizer&) = delete;

  struct BlobInfo {
    BlobInfo(sk_sp<SkTextBlob> b, CanvasRotationInVertical r)
        : blob(std::move(b)), rotation(r) {}
    sk_sp<SkTextBlob> blob;
    CanvasRotationInVertical rotation;
  };
  using BlobBuffer = Vector<BlobInfo, 16>;

  const BlobBuffer& Blobs();
  float Advance() const { return advance_; }

 private:
  friend class ShapeResultBloberizerTestInfo;

  void Add(Glyph glyph,
           const SimpleFontData* font_data,
           CanvasRotationInVertical canvas_rotation,
           float h_offset,
           unsigned character_index) {
    // cannot mix x-only/xy offsets
    DCHECK(!HasPendingVerticalOffsets());

    if (UNLIKELY(font_data != pending_font_data_) ||
        UNLIKELY(canvas_rotation != pending_canvas_rotation_)) {
      CommitPendingRun();
      pending_font_data_ = font_data;
      pending_canvas_rotation_ = canvas_rotation;
      DCHECK(!IsCanvasRotationInVerticalUpright(canvas_rotation))
          << static_cast<int>(canvas_rotation);
    }

    pending_glyphs_.push_back(glyph);
    pending_offsets_.push_back(h_offset);
    if (UNLIKELY(!current_text_.IsNull())) {
      DVLOG(5) << "  Appending glyph " << glyph << " with start index "
               << character_index;
      current_character_indexes_.push_back(character_index);
    }
  }

  void Add(Glyph glyph,
           const SimpleFontData* font_data,
           CanvasRotationInVertical canvas_rotation,
           const FloatPoint& offset,
           unsigned character_index) {
    // cannot mix x-only/xy offsets
    DCHECK(pending_glyphs_.IsEmpty() || HasPendingVerticalOffsets());

    if (UNLIKELY(font_data != pending_font_data_) ||
        UNLIKELY(canvas_rotation != pending_canvas_rotation_)) {
      CommitPendingRun();
      pending_font_data_ = font_data;
      pending_canvas_rotation_ = canvas_rotation;
      const auto& metrics = font_data->GetFontMetrics();
      pending_vertical_baseline_x_offset_ =
          !IsCanvasRotationInVerticalUpright(canvas_rotation)
              ? 0
              : metrics.FloatAscent() - metrics.FloatAscent(kCentralBaseline);
    }

    pending_glyphs_.push_back(glyph);
    pending_offsets_.push_back(offset.X() +
                               pending_vertical_baseline_x_offset_);
    pending_offsets_.push_back(offset.Y());
    if (UNLIKELY(!current_text_.IsNull())) {
      DVLOG(5) << "  Appending glyph " << glyph << " with start index "
               << character_index;
      current_character_indexes_.push_back(character_index);
    }
  }

  // Whether the FillFastHorizontalGlyphs or AddFastHorizontalGlyphToBloberizer
  // can be used. Only applies for full runs with no vertical offsets, no text
  // intercepts, and not emitting text.
  bool CanUseFastPath(unsigned from,
                      unsigned to,
                      unsigned length,
                      bool has_vertical_offsets);
  bool CanUseFastPath(unsigned from, unsigned to, const ShapeResultView*);
  float FillFastHorizontalGlyphs(const ShapeResultBuffer&, TextDirection);
  float FillFastHorizontalGlyphs(const ShapeResult*, float advance = 0);
  static void AddFastHorizontalGlyphToBloberizer(void* context,
                                                 unsigned,
                                                 Glyph,
                                                 FloatSize glyph_offset,
                                                 float advance,
                                                 bool is_horizontal,
                                                 CanvasRotationInVertical,
                                                 const SimpleFontData*);

  float FillGlyphsForResult(const ShapeResult*,
                            const StringView&,
                            unsigned from,
                            unsigned to,
                            float initial_advance,
                            unsigned run_offset);
  static void AddGlyphToBloberizer(void* context,
                                   unsigned character_index,
                                   Glyph,
                                   FloatSize glyph_offset,
                                   float advance,
                                   bool is_horizontal,
                                   CanvasRotationInVertical,
                                   const SimpleFontData*);

  void AddEmphasisMark(const GlyphData& emphasis_data,
                       CanvasRotationInVertical canvas_rotation,
                       FloatPoint glyph_center,
                       float mid_glyph_offset);
  static void AddEmphasisMarkToBloberizer(
      void* context,
      unsigned character_index,
      float advance_so_far,
      unsigned graphemes_in_cluster,
      float cluster_advance,
      CanvasRotationInVertical canvas_rotation);

  bool IsSkipInkException(const StringView& text, unsigned character_index);

  void SetText(const StringView& text,
               unsigned from,
               unsigned to,
               base::span<const unsigned> cluster_starts);
  void CommitText();
  void CommitPendingRun();
  void CommitPendingBlob();

  bool HasPendingVerticalOffsets() const;

  const FontDescription& font_description_;
  const float device_scale_factor_;
  const Type type_;

  // Current text blob state.
  SkTextBlobBuilder builder_;
  CanvasRotationInVertical builder_rotation_ =
      CanvasRotationInVertical::kRegular;
  size_t builder_run_count_ = 0;

  // Current run state.
  const SimpleFontData* pending_font_data_ = nullptr;
  CanvasRotationInVertical pending_canvas_rotation_ =
      CanvasRotationInVertical::kRegular;
  Vector<Glyph, 1024> pending_glyphs_;
  Vector<float, 1024> pending_offsets_;

  // Reserve a small amount of space for the common case when printing.
  // Allowing this class to grow larger than ~7k impacts user perf.
  Vector<uint8_t, 64> pending_utf8_;
  Vector<uint32_t, 64> pending_utf8_character_indexes_;
  Vector<unsigned, 64> current_character_indexes_;
  Vector<unsigned, 64> cluster_ends_;
  unsigned cluster_ends_offset_ = 0;
  StringView current_text_;

  float pending_vertical_baseline_x_offset_ = 0;

  // Constructed blobs.
  BlobBuffer blobs_;
  float advance_ = 0;
};

struct PLATFORM_EXPORT ShapeResultBloberizer::FillGlyphsNG
    : public ShapeResultBloberizer {
  FillGlyphsNG(const FontDescription&,
               float device_scale_factor,
               const StringView&,
               unsigned from,
               unsigned to,
               const ShapeResultView*,
               Type);
};
struct PLATFORM_EXPORT ShapeResultBloberizer::FillTextEmphasisGlyphsNG
    : public ShapeResultBloberizer {
  FillTextEmphasisGlyphsNG(const FontDescription&,
                           float device_scale_factor,
                           const StringView&,
                           unsigned from,
                           unsigned to,
                           const ShapeResultView*,
                           const GlyphData& emphasis_data);
};

struct PLATFORM_EXPORT ShapeResultBloberizer::FillGlyphs
    : public ShapeResultBloberizer {
  FillGlyphs(const FontDescription&,
             float device_scale_factor,
             const TextRunPaintInfo&,
             const ShapeResultBuffer&,
             Type);
};
struct PLATFORM_EXPORT ShapeResultBloberizer::FillTextEmphasisGlyphs
    : public ShapeResultBloberizer {
  FillTextEmphasisGlyphs(const FontDescription&,
                         float device_scale_factor,
                         const TextRunPaintInfo&,
                         const ShapeResultBuffer&,
                         const GlyphData& emphasis_data);
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_FONTS_SHAPING_SHAPE_RESULT_BLOBERIZER_H_