File: writing_mode_utils.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (293 lines) | stat: -rw-r--r-- 10,151 bytes parent folder | download | duplicates (6)
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Copyright 2017 The Chromium Authors
// 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_TEXT_WRITING_MODE_UTILS_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_TEXT_WRITING_MODE_UTILS_H_

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

namespace blink {

// Templates to map values between logical orientations and physical
// orientations. See https://www.w3.org/TR/css-writing-modes-3/ and
// https://www.w3.org/TR/css-logical-1/ for definitions of logical orientations.

// This file provides two types of templates:
//
// - Simple value mappers (PhysicalToLogical and LogicalToPhysical): they take
//   4 input values in physical or logical orientations, and provide accessors
//   to get values in logical or physical orientations. As the inputs may be
//   evaluated even if not used (in case that the compiler is unable to remove
//   unused evaluations, e.g. containing non-inlined function calls), for
//   performance-senstive code, the evaluation of the inputs should be simple
//   and/or be fully inlined.
//
// - Value mappers based on getter/setter methods (PhysicalToLogicalGetter,
//   LogicalToPhysicalGetter, PhysicalToLogicalSetter and
//   LogicalToPhysicalSetter): they take 4 method pointers as inputs pointing to
//   methods accessing values in physical or logical orientations, and provide
//   accessors to get or set values in logical or physical orientations. They
//   are suitable for mapping of setters, or getters implemented with non-
//   inlined functions. Evaluation of the input values are delayed when they are
//   actually needed.
//
// See WritingModeUtilsTest.cpp, LayoutBoxModelObject.h and ComputedStyle.h for
// examples.

template <typename Value>
class PhysicalToLogical {
  STACK_ALLOCATED();

 public:
  PhysicalToLogical(WritingDirectionMode writing_direction,
                    Value top,
                    Value right,
                    Value bottom,
                    Value left)
      : writing_direction_(writing_direction),
        top_(top),
        right_(right),
        bottom_(bottom),
        left_(left) {}

  Value InlineStart() const {
    if (writing_direction_.IsHorizontal())
      return writing_direction_.IsLtr() ? left_ : right_;
    return ValueFor(writing_direction_.InlineStart());
  }

  Value InlineEnd() const {
    if (writing_direction_.IsHorizontal())
      return writing_direction_.IsLtr() ? right_ : left_;
    return ValueFor(writing_direction_.InlineEnd());
  }

  Value BlockStart() const {
    if (writing_direction_.IsHorizontal())
      return top_;
    return ValueFor(writing_direction_.BlockStart());
  }

  Value BlockEnd() const {
    if (writing_direction_.IsHorizontal())
      return bottom_;
    return ValueFor(writing_direction_.BlockEnd());
  }

  Value LineOver() const {
    return writing_direction_.IsHorizontal()
               ? top_
               : ValueFor(writing_direction_.LineOver());
  }

  Value LineUnder() const {
    return writing_direction_.IsHorizontal()
               ? bottom_
               : ValueFor(writing_direction_.LineUnder());
  }

 private:
  Value ValueFor(PhysicalDirection direction) const {
    switch (direction) {
      case PhysicalDirection::kUp:
        return top_;
      case PhysicalDirection::kRight:
        return right_;
      case PhysicalDirection::kDown:
        return bottom_;
      case PhysicalDirection::kLeft:
        return left_;
    }
    return top_;
  }

  WritingDirectionMode writing_direction_;
  Value top_;
  Value right_;
  Value bottom_;
  Value left_;
};

template <typename Value>
class LogicalToPhysical {
  STACK_ALLOCATED();

 public:
  LogicalToPhysical(WritingDirectionMode writing_direction,
                    Value inline_start,
                    Value inline_end,
                    Value block_start,
                    Value block_end)
      : writing_direction_(writing_direction),
        inline_start_(inline_start),
        inline_end_(inline_end),
        block_start_(block_start),
        block_end_(block_end) {}

  Value Left() const {
    if (writing_direction_.IsHorizontal())
      return writing_direction_.IsLtr() ? inline_start_ : inline_end_;
    return writing_direction_.IsFlippedBlocks() ? block_end_ : block_start_;
  }

  Value Right() const {
    if (writing_direction_.IsHorizontal())
      return writing_direction_.IsLtr() ? inline_end_ : inline_start_;
    return writing_direction_.IsFlippedBlocks() ? block_start_ : block_end_;
  }

  Value Top() const {
    if (writing_direction_.IsHorizontal())
      return block_start_;
    if (writing_direction_.GetWritingMode() == WritingMode::kSidewaysLr) {
      return writing_direction_.IsLtr() ? inline_end_ : inline_start_;
    }
    return writing_direction_.IsLtr() ? inline_start_ : inline_end_;
  }

  Value Bottom() const {
    if (writing_direction_.IsHorizontal())
      return block_end_;
    if (writing_direction_.GetWritingMode() == WritingMode::kSidewaysLr) {
      return writing_direction_.IsLtr() ? inline_start_ : inline_end_;
    }
    return writing_direction_.IsLtr() ? inline_end_ : inline_start_;
  }

 private:
  WritingDirectionMode writing_direction_;
  Value inline_start_;  // a.k.a. start
  Value inline_end_;    // a.k.a. end
  Value block_start_;   // a.k.a. before
  Value block_end_;     // a.k.a. after
};

template <typename Value>
class LogicalToLogical {
  STACK_ALLOCATED();

 public:
  LogicalToLogical(WritingDirectionMode from_writing_direction,
                   WritingDirectionMode to_writing_direction,
                   Value inline_start,
                   Value inline_end,
                   Value block_start,
                   Value block_end)
      : LogicalToLogical(to_writing_direction,
                         LogicalToPhysical<Value>(from_writing_direction,
                                                  inline_start,
                                                  inline_end,
                                                  block_start,
                                                  block_end)) {}

  Value InlineStart() const { return logical_.InlineStart(); }
  Value BlockStart() const { return logical_.BlockStart(); }
  Value InlineEnd() const { return logical_.InlineEnd(); }
  Value BlockEnd() const { return logical_.BlockEnd(); }

 private:
  LogicalToLogical(WritingDirectionMode to_writing_direction,
                   LogicalToPhysical<Value> physical)
      : logical_(to_writing_direction,
                 physical.Top(),
                 physical.Right(),
                 physical.Bottom(),
                 physical.Left()) {}

  PhysicalToLogical<Value> logical_;
};

template <typename Value, typename Object>
class LogicalToPhysicalGetter {
  STACK_ALLOCATED();

 public:
  using Getter = Value (Object::*)() const;
  LogicalToPhysicalGetter(WritingDirectionMode writing_direction,
                          const Object& object,
                          Getter inline_start_getter,
                          Getter inline_end_getter,
                          Getter block_start_getter,
                          Getter block_end_getter)
      : object_(object),
        converter_(writing_direction,
                   inline_start_getter,
                   inline_end_getter,
                   block_start_getter,
                   block_end_getter) {}

  Value Left() const { return (object_.*converter_.Left())(); }
  Value Right() const { return (object_.*converter_.Right())(); }
  Value Top() const { return (object_.*converter_.Top())(); }
  Value Bottom() const { return (object_.*converter_.Bottom())(); }

 private:
  const Object& object_;
  LogicalToPhysical<Getter> converter_;
};

template <typename Value, typename Object>
class PhysicalToLogicalGetter {
  STACK_ALLOCATED();

 public:
  using Getter = Value (Object::*)() const;
  PhysicalToLogicalGetter(WritingDirectionMode writing_direction,
                          const Object& object,
                          Getter top_getter,
                          Getter right_getter,
                          Getter bottom_getter,
                          Getter left_getter)
      : object_(object),
        converter_(writing_direction,
                   top_getter,
                   right_getter,
                   bottom_getter,
                   left_getter) {}

  Value InlineStart() const { return (object_.*converter_.InlineStart())(); }
  Value InlineEnd() const { return (object_.*converter_.InlineEnd())(); }
  Value BlockStart() const { return (object_.*converter_.BlockStart())(); }
  Value BlockEnd() const { return (object_.*converter_.BlockEnd())(); }

 private:
  const Object& object_;
  PhysicalToLogical<Getter> converter_;
};

template <typename Value, typename Object>
class LogicalToPhysicalSetter {
  STACK_ALLOCATED();

 public:
  using Setter = void (Object::*)(Value);
  LogicalToPhysicalSetter(WritingDirectionMode writing_direction,
                          Object& object,
                          Setter top_setter,
                          Setter right_setter,
                          Setter bottom_setter,
                          Setter left_setter)
      : object_(object),
        converter_(writing_direction,
                   top_setter,
                   right_setter,
                   bottom_setter,
                   left_setter) {}

  void SetInlineStart(Value v) { (object_.*converter_.InlineStart())(v); }
  void SetInlineEnd(Value v) { (object_.*converter_.InlineEnd())(v); }
  void SetBlockStart(Value v) { (object_.*converter_.BlockStart())(v); }
  void SetBlockEnd(Value v) { (object_.*converter_.BlockEnd())(v); }

 private:
  Object& object_;
  // This converter converts physical setters to logical setters which accept
  // logical values and call the physical setters to set physical values.
  PhysicalToLogical<Setter> converter_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_TEXT_WRITING_MODE_UTILS_H_