1 #ifndef OT_LAYOUT_GPOS_CURSIVEPOSFORMAT1_HH
2 #define OT_LAYOUT_GPOS_CURSIVEPOSFORMAT1_HH
3
4 #include "Anchor.hh"
5
6 namespace OT {
7 namespace Layout {
8 namespace GPOS_impl {
9
10 struct EntryExitRecord
11 {
12 friend struct CursivePosFormat1;
13
sanitizeOT::Layout::GPOS_impl::EntryExitRecord14 bool sanitize (hb_sanitize_context_t *c, const void *base) const
15 {
16 TRACE_SANITIZE (this);
17 return_trace (entryAnchor.sanitize (c, base) && exitAnchor.sanitize (c, base));
18 }
19
collect_variation_indicesOT::Layout::GPOS_impl::EntryExitRecord20 void collect_variation_indices (hb_collect_variation_indices_context_t *c,
21 const void *src_base) const
22 {
23 (src_base+entryAnchor).collect_variation_indices (c);
24 (src_base+exitAnchor).collect_variation_indices (c);
25 }
26
subsetOT::Layout::GPOS_impl::EntryExitRecord27 bool subset (hb_subset_context_t *c,
28 const void *src_base) const
29 {
30 TRACE_SERIALIZE (this);
31 auto *out = c->serializer->embed (this);
32 if (unlikely (!out)) return_trace (false);
33
34 bool ret = false;
35 ret |= out->entryAnchor.serialize_subset (c, entryAnchor, src_base);
36 ret |= out->exitAnchor.serialize_subset (c, exitAnchor, src_base);
37 return_trace (ret);
38 }
39
40 protected:
41 Offset16To<Anchor>
42 entryAnchor; /* Offset to EntryAnchor table--from
43 * beginning of CursivePos
44 * subtable--may be NULL */
45 Offset16To<Anchor>
46 exitAnchor; /* Offset to ExitAnchor table--from
47 * beginning of CursivePos
48 * subtable--may be NULL */
49 public:
50 DEFINE_SIZE_STATIC (4);
51 };
52
53 static void
reverse_cursive_minor_offset(hb_glyph_position_t * pos,unsigned int i,hb_direction_t direction,unsigned int new_parent)54 reverse_cursive_minor_offset (hb_glyph_position_t *pos, unsigned int i, hb_direction_t direction, unsigned int new_parent) {
55 int chain = pos[i].attach_chain(), type = pos[i].attach_type();
56 if (likely (!chain || 0 == (type & ATTACH_TYPE_CURSIVE)))
57 return;
58
59 pos[i].attach_chain() = 0;
60
61 unsigned int j = (int) i + chain;
62
63 /* Stop if we see new parent in the chain. */
64 if (j == new_parent)
65 return;
66
67 reverse_cursive_minor_offset (pos, j, direction, new_parent);
68
69 if (HB_DIRECTION_IS_HORIZONTAL (direction))
70 pos[j].y_offset = -pos[i].y_offset;
71 else
72 pos[j].x_offset = -pos[i].x_offset;
73
74 pos[j].attach_chain() = -chain;
75 pos[j].attach_type() = type;
76 }
77
78
79 struct CursivePosFormat1
80 {
81 protected:
82 HBUINT16 format; /* Format identifier--format = 1 */
83 Offset16To<Coverage>
84 coverage; /* Offset to Coverage table--from
85 * beginning of subtable */
86 Array16Of<EntryExitRecord>
87 entryExitRecord; /* Array of EntryExit records--in
88 * Coverage Index order */
89 public:
90 DEFINE_SIZE_ARRAY (6, entryExitRecord);
91
sanitizeOT::Layout::GPOS_impl::CursivePosFormat192 bool sanitize (hb_sanitize_context_t *c) const
93 {
94 TRACE_SANITIZE (this);
95 if (unlikely (!coverage.sanitize (c, this)))
96 return_trace (false);
97
98 if (c->lazy_some_gpos)
99 return_trace (entryExitRecord.sanitize_shallow (c));
100 else
101 return_trace (entryExitRecord.sanitize (c, this));
102 }
103
intersectsOT::Layout::GPOS_impl::CursivePosFormat1104 bool intersects (const hb_set_t *glyphs) const
105 { return (this+coverage).intersects (glyphs); }
106
closure_lookupsOT::Layout::GPOS_impl::CursivePosFormat1107 void closure_lookups (hb_closure_lookups_context_t *c) const {}
108
collect_variation_indicesOT::Layout::GPOS_impl::CursivePosFormat1109 void collect_variation_indices (hb_collect_variation_indices_context_t *c) const
110 {
111 + hb_zip (this+coverage, entryExitRecord)
112 | hb_filter (c->glyph_set, hb_first)
113 | hb_map (hb_second)
114 | hb_apply ([&] (const EntryExitRecord& record) { record.collect_variation_indices (c, this); })
115 ;
116 }
117
collect_glyphsOT::Layout::GPOS_impl::CursivePosFormat1118 void collect_glyphs (hb_collect_glyphs_context_t *c) const
119 { if (unlikely (!(this+coverage).collect_coverage (c->input))) return; }
120
get_coverageOT::Layout::GPOS_impl::CursivePosFormat1121 const Coverage &get_coverage () const { return this+coverage; }
122
applyOT::Layout::GPOS_impl::CursivePosFormat1123 bool apply (hb_ot_apply_context_t *c) const
124 {
125 TRACE_APPLY (this);
126 hb_buffer_t *buffer = c->buffer;
127
128 const EntryExitRecord &this_record = entryExitRecord[(this+coverage).get_coverage (buffer->cur().codepoint)];
129 if (!this_record.entryAnchor ||
130 unlikely (!this_record.entryAnchor.sanitize (&c->sanitizer, this))) return_trace (false);
131
132 hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input;
133 skippy_iter.reset_fast (buffer->idx);
134 unsigned unsafe_from;
135 if (unlikely (!skippy_iter.prev (&unsafe_from)))
136 {
137 buffer->unsafe_to_concat_from_outbuffer (unsafe_from, buffer->idx + 1);
138 return_trace (false);
139 }
140
141 const EntryExitRecord &prev_record = entryExitRecord[(this+coverage).get_coverage (buffer->info[skippy_iter.idx].codepoint)];
142 if (!prev_record.exitAnchor ||
143 unlikely (!prev_record.exitAnchor.sanitize (&c->sanitizer, this)))
144 {
145 buffer->unsafe_to_concat_from_outbuffer (skippy_iter.idx, buffer->idx + 1);
146 return_trace (false);
147 }
148
149 unsigned int i = skippy_iter.idx;
150 unsigned int j = buffer->idx;
151
152 if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
153 {
154 c->buffer->message (c->font,
155 "cursive attaching glyph at %u to glyph at %u",
156 i, j);
157 }
158
159 buffer->unsafe_to_break (i, j + 1);
160 float entry_x, entry_y, exit_x, exit_y;
161 (this+prev_record.exitAnchor).get_anchor (c, buffer->info[i].codepoint, &exit_x, &exit_y);
162 (this+this_record.entryAnchor).get_anchor (c, buffer->info[j].codepoint, &entry_x, &entry_y);
163
164 hb_glyph_position_t *pos = buffer->pos;
165
166 hb_position_t d;
167 /* Main-direction adjustment */
168 switch (c->direction) {
169 case HB_DIRECTION_LTR:
170 pos[i].x_advance = roundf (exit_x) + pos[i].x_offset;
171
172 d = roundf (entry_x) + pos[j].x_offset;
173 pos[j].x_advance -= d;
174 pos[j].x_offset -= d;
175 break;
176 case HB_DIRECTION_RTL:
177 d = roundf (exit_x) + pos[i].x_offset;
178 pos[i].x_advance -= d;
179 pos[i].x_offset -= d;
180
181 pos[j].x_advance = roundf (entry_x) + pos[j].x_offset;
182 break;
183 case HB_DIRECTION_TTB:
184 pos[i].y_advance = roundf (exit_y) + pos[i].y_offset;
185
186 d = roundf (entry_y) + pos[j].y_offset;
187 pos[j].y_advance -= d;
188 pos[j].y_offset -= d;
189 break;
190 case HB_DIRECTION_BTT:
191 d = roundf (exit_y) + pos[i].y_offset;
192 pos[i].y_advance -= d;
193 pos[i].y_offset -= d;
194
195 pos[j].y_advance = roundf (entry_y);
196 break;
197 case HB_DIRECTION_INVALID:
198 default:
199 break;
200 }
201
202 /* Cross-direction adjustment */
203
204 /* We attach child to parent (think graph theory and rooted trees whereas
205 * the root stays on baseline and each node aligns itself against its
206 * parent.
207 *
208 * Optimize things for the case of RightToLeft, as that's most common in
209 * Arabic. */
210 unsigned int child = i;
211 unsigned int parent = j;
212 hb_position_t x_offset = roundf (entry_x - exit_x);
213 hb_position_t y_offset = roundf (entry_y - exit_y);
214 if (!(c->lookup_props & LookupFlag::RightToLeft))
215 {
216 unsigned int k = child;
217 child = parent;
218 parent = k;
219 x_offset = -x_offset;
220 y_offset = -y_offset;
221 }
222
223 /* If child was already connected to someone else, walk through its old
224 * chain and reverse the link direction, such that the whole tree of its
225 * previous connection now attaches to new parent. Watch out for case
226 * where new parent is on the path from old chain...
227 */
228 reverse_cursive_minor_offset (pos, child, c->direction, parent);
229
230 pos[child].attach_type() = ATTACH_TYPE_CURSIVE;
231 pos[child].attach_chain() = (int) parent - (int) child;
232 buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT;
233 if (likely (HB_DIRECTION_IS_HORIZONTAL (c->direction)))
234 pos[child].y_offset = y_offset;
235 else
236 pos[child].x_offset = x_offset;
237
238 /* If parent was attached to child, separate them.
239 * https://github.com/harfbuzz/harfbuzz/issues/2469
240 */
241 if (unlikely (pos[parent].attach_chain() == -pos[child].attach_chain()))
242 {
243 pos[parent].attach_chain() = 0;
244 if (likely (HB_DIRECTION_IS_HORIZONTAL (c->direction)))
245 pos[parent].y_offset = 0;
246 else
247 pos[parent].x_offset = 0;
248 }
249
250 if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
251 {
252 c->buffer->message (c->font,
253 "cursive attached glyph at %u to glyph at %u",
254 i, j);
255 }
256
257 buffer->idx++;
258 return_trace (true);
259 }
260
261 template <typename Iterator,
262 hb_requires (hb_is_iterator (Iterator))>
serializeOT::Layout::GPOS_impl::CursivePosFormat1263 void serialize (hb_subset_context_t *c,
264 Iterator it,
265 const void *src_base)
266 {
267 if (unlikely (!c->serializer->extend_min ((*this)))) return;
268 this->format = 1;
269 this->entryExitRecord.len = it.len ();
270
271 for (const EntryExitRecord& entry_record : + it
272 | hb_map (hb_second))
273 entry_record.subset (c, src_base);
274
275 auto glyphs =
276 + it
277 | hb_map_retains_sorting (hb_first)
278 ;
279
280 coverage.serialize_serialize (c->serializer, glyphs);
281 }
282
subsetOT::Layout::GPOS_impl::CursivePosFormat1283 bool subset (hb_subset_context_t *c) const
284 {
285 TRACE_SUBSET (this);
286 const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
287 const hb_map_t &glyph_map = *c->plan->glyph_map;
288
289 auto *out = c->serializer->start_embed (*this);
290
291 auto it =
292 + hb_zip (this+coverage, entryExitRecord)
293 | hb_filter (glyphset, hb_first)
294 | hb_map_retains_sorting ([&] (hb_pair_t<hb_codepoint_t, const EntryExitRecord&> p) -> hb_pair_t<hb_codepoint_t, const EntryExitRecord&>
295 { return hb_pair (glyph_map[p.first], p.second);})
296 ;
297
298 bool ret = bool (it);
299 out->serialize (c, it, this);
300 return_trace (ret);
301 }
302 };
303
304
305 }
306 }
307 }
308
309 #endif /* OT_LAYOUT_GPOS_CURSIVEPOSFORMAT1_HH */
310