Coverage Report

Created: 2026-12-01 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/harfbuzz/src/hb-paint.cc
Line
Count
Source
1
/*
2
 * Copyright © 2022 Matthias Clasen
3
 *
4
 *  This is part of HarfBuzz, a text shaping library.
5
 *
6
 * Permission is hereby granted, without written agreement and without
7
 * license or royalty fees, to use, copy, modify, and distribute this
8
 * software and its documentation for any purpose, provided that the
9
 * above copyright notice and the following two paragraphs appear in
10
 * all copies of this software.
11
 *
12
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16
 * DAMAGE.
17
 *
18
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23
 */
24
25
#include "hb.hh"
26
27
#ifndef HB_NO_PAINT
28
29
#include "hb-paint.hh"
30
31
/**
32
 * SECTION: hb-paint
33
 * @title: hb-paint
34
 * @short_description: Glyph painting
35
 * @include: hb.h
36
 *
37
 * Functions for painting glyphs.
38
 *
39
 * The main purpose of these functions is to paint (extract) color glyph layers
40
 * from the COLRv1 table, but the API works for drawing ordinary outlines and
41
 * images as well.
42
 *
43
 * The #hb_paint_funcs_t struct can be used with hb_font_paint_glyph().
44
 **/
45
46
static void
47
hb_paint_push_transform_nil (hb_paint_funcs_t *funcs, void *paint_data,
48
                             float xx, float yx,
49
                             float xy, float yy,
50
                             float dx, float dy,
51
0
                             void *user_data) {}
52
53
static void
54
hb_paint_pop_transform_nil (hb_paint_funcs_t *funcs, void *paint_data,
55
0
                            void *user_data) {}
56
57
static hb_bool_t
58
hb_paint_color_glyph_nil (hb_paint_funcs_t *funcs, void *paint_data,
59
                          hb_codepoint_t glyph,
60
                          hb_font_t *font,
61
0
                          void *user_data) { return false; }
62
63
static void
64
hb_paint_fill_glyph_nil (hb_paint_funcs_t *funcs, void *paint_data,
65
                         hb_codepoint_t glyph,
66
                         hb_font_t *font,
67
                         hb_bool_t is_foreground,
68
                         hb_color_t color,
69
                         void *user_data)
70
0
{
71
  /* Decompose into the equivalent general sequence. */
72
0
  funcs->push_clip_glyph (paint_data, glyph, font);
73
0
  funcs->color (paint_data, is_foreground, color);
74
0
  funcs->pop_clip (paint_data);
75
0
}
76
77
static void
78
hb_paint_push_clip_glyph_nil (hb_paint_funcs_t *funcs, void *paint_data,
79
                              hb_codepoint_t glyph,
80
                              hb_font_t *font,
81
0
                              void *user_data) {}
82
83
static void
84
hb_paint_push_clip_rectangle_nil (hb_paint_funcs_t *funcs, void *paint_data,
85
                                  float xmin, float ymin, float xmax, float ymax,
86
0
                                  void *user_data) {}
87
88
static hb_draw_funcs_t *
89
hb_paint_push_clip_path_start_nil (hb_paint_funcs_t *funcs, void *paint_data,
90
                                   void **draw_data,
91
0
                                   void *user_data) { if (draw_data) *draw_data = nullptr; return nullptr; }
92
93
static void
94
hb_paint_push_clip_path_end_nil (hb_paint_funcs_t *funcs, void *paint_data,
95
0
                                 void *user_data) {}
96
97
static void
98
hb_paint_pop_clip_nil (hb_paint_funcs_t *funcs, void *paint_data,
99
0
                       void *user_data) {}
100
101
static void
102
hb_paint_color_nil (hb_paint_funcs_t *funcs, void *paint_data,
103
                    hb_bool_t is_foreground,
104
                    hb_color_t color,
105
0
                    void *user_data) {}
106
107
static hb_bool_t
108
hb_paint_image_nil (hb_paint_funcs_t *funcs, void *paint_data,
109
                    hb_blob_t *image,
110
                    unsigned int width,
111
                    unsigned int height,
112
                    hb_tag_t format,
113
                    float slant_xy_deprecated,
114
                    hb_glyph_extents_t *extents,
115
0
                    void *user_data) { return false; }
116
117
static void
118
hb_paint_linear_gradient_nil (hb_paint_funcs_t *funcs, void *paint_data,
119
                              hb_color_line_t *color_line,
120
                              float x0, float y0,
121
                              float x1, float y1,
122
                              float x2, float y2,
123
0
                              void *user_data) {}
124
125
static void
126
hb_paint_radial_gradient_nil (hb_paint_funcs_t *funcs, void *paint_data,
127
                              hb_color_line_t *color_line,
128
                              float x0, float y0, float r0,
129
                              float x1, float y1, float r1,
130
0
                              void *user_data) {}
131
132
static void
133
hb_paint_sweep_gradient_nil (hb_paint_funcs_t *funcs, void *paint_data,
134
                             hb_color_line_t *color_line,
135
                             float x0, float y0,
136
                             float start_angle,
137
                             float end_angle,
138
0
                             void *user_data) {}
139
140
static void
141
hb_paint_push_group_nil (hb_paint_funcs_t *funcs, void *paint_data,
142
0
                         void *user_data) {}
143
144
static void
145
hb_paint_push_group_for_nil (hb_paint_funcs_t *funcs, void *paint_data,
146
                             hb_paint_composite_mode_t mode,
147
                             void *user_data)
148
0
{
149
0
  hb_paint_push_group (funcs, paint_data);
150
0
}
151
152
static void
153
hb_paint_pop_group_nil (hb_paint_funcs_t *funcs, void *paint_data,
154
                        hb_paint_composite_mode_t mode,
155
0
                        void *user_data) {}
156
157
static hb_bool_t
158
hb_paint_custom_palette_color_nil (hb_paint_funcs_t *funcs, void *paint_data,
159
                                   unsigned int color_index,
160
                                   hb_color_t *color,
161
0
                                   void *user_data) { return false; }
162
163
static hb_bool_t
164
hb_paint_set_budget_nil (hb_paint_funcs_t *funcs HB_UNUSED, void *paint_data HB_UNUSED,
165
       int64_t budget HB_UNUSED,
166
0
       void *user_data HB_UNUSED) { return false; }
167
168
static int64_t
169
hb_paint_get_budget_nil (hb_paint_funcs_t *funcs HB_UNUSED, void *paint_data HB_UNUSED,
170
0
       void *user_data HB_UNUSED) { return HB_BUDGET_DEFAULT; }
171
172
static int64_t *
173
hb_paint_get_budget_remaining_nil (hb_paint_funcs_t *funcs HB_UNUSED, void *paint_data HB_UNUSED,
174
0
           void *user_data HB_UNUSED) { return nullptr; }
175
176
static bool
177
_hb_paint_funcs_set_preamble (hb_paint_funcs_t  *funcs,
178
                             bool                func_is_null,
179
                             void              **user_data,
180
                             hb_destroy_func_t  *destroy)
181
0
{
182
0
  if (hb_object_is_immutable (funcs))
183
0
  {
184
0
    if (*destroy)
185
0
      (*destroy) (*user_data);
186
0
    return false;
187
0
  }
188
189
0
  if (func_is_null)
190
0
  {
191
0
    if (*destroy)
192
0
      (*destroy) (*user_data);
193
0
    *destroy = nullptr;
194
0
    *user_data = nullptr;
195
0
  }
196
197
0
  return true;
198
0
}
199
200
static bool
201
_hb_paint_funcs_set_middle (hb_paint_funcs_t  *funcs,
202
                            void              *user_data,
203
                            hb_destroy_func_t  destroy)
204
0
{
205
0
  auto destroy_guard = hb_make_scope_guard ([&]() {
206
0
    if (destroy) destroy (user_data);
207
0
  });
208
209
0
  if (user_data && !funcs->user_data)
210
0
  {
211
0
    funcs->user_data = (decltype (funcs->user_data)) hb_calloc (1, sizeof (*funcs->user_data));
212
0
    if (unlikely (!funcs->user_data))
213
0
      return false;
214
0
  }
215
0
  if (destroy && !funcs->destroy)
216
0
  {
217
0
    funcs->destroy = (decltype (funcs->destroy)) hb_calloc (1, sizeof (*funcs->destroy));
218
0
    if (unlikely (!funcs->destroy))
219
0
      return false;
220
0
  }
221
222
0
  destroy_guard.release ();
223
0
  return true;
224
0
}
225
226
#define HB_PAINT_FUNC_IMPLEMENT(name)                                           \
227
                                                                                \
228
void                                                                            \
229
hb_paint_funcs_set_##name##_func (hb_paint_funcs_t         *funcs,              \
230
                                  hb_paint_##name##_func_t  func,               \
231
                                  void                     *user_data,          \
232
0
                                  hb_destroy_func_t         destroy)            \
233
0
{                                                                               \
234
0
  if (!_hb_paint_funcs_set_preamble (funcs, !func, &user_data, &destroy))       \
235
0
      return;                                                                   \
236
0
                                                                                \
237
0
  if (funcs->destroy && funcs->destroy->name)                                   \
238
0
    funcs->destroy->name (!funcs->user_data ? nullptr : funcs->user_data->name);\
239
0
                                                                                \
240
0
  if (!_hb_paint_funcs_set_middle (funcs, user_data, destroy))                  \
241
0
      return;                                                                   \
242
0
                                                                                \
243
0
  if (func)                                                                     \
244
0
    funcs->func.name = func;                                                    \
245
0
  else                                                                          \
246
0
    funcs->func.name = hb_paint_##name##_nil;                                   \
247
0
                                                                                \
248
0
  if (funcs->user_data)                                                         \
249
0
    funcs->user_data->name = user_data;                                         \
250
0
  if (funcs->destroy)                                                           \
251
0
    funcs->destroy->name = destroy;                                             \
252
0
}
Unexecuted instantiation: hb_paint_funcs_set_push_transform_func
Unexecuted instantiation: hb_paint_funcs_set_pop_transform_func
Unexecuted instantiation: hb_paint_funcs_set_color_glyph_func
Unexecuted instantiation: hb_paint_funcs_set_push_clip_glyph_func
Unexecuted instantiation: hb_paint_funcs_set_push_clip_rectangle_func
Unexecuted instantiation: hb_paint_funcs_set_push_clip_path_start_func
Unexecuted instantiation: hb_paint_funcs_set_push_clip_path_end_func
Unexecuted instantiation: hb_paint_funcs_set_pop_clip_func
Unexecuted instantiation: hb_paint_funcs_set_color_func
Unexecuted instantiation: hb_paint_funcs_set_image_func
Unexecuted instantiation: hb_paint_funcs_set_linear_gradient_func
Unexecuted instantiation: hb_paint_funcs_set_radial_gradient_func
Unexecuted instantiation: hb_paint_funcs_set_sweep_gradient_func
Unexecuted instantiation: hb_paint_funcs_set_push_group_func
Unexecuted instantiation: hb_paint_funcs_set_push_group_for_func
Unexecuted instantiation: hb_paint_funcs_set_pop_group_func
Unexecuted instantiation: hb_paint_funcs_set_custom_palette_color_func
Unexecuted instantiation: hb_paint_funcs_set_fill_glyph_func
Unexecuted instantiation: hb_paint_funcs_set_set_budget_func
Unexecuted instantiation: hb_paint_funcs_set_get_budget_func
Unexecuted instantiation: hb_paint_funcs_set_get_budget_remaining_func
253
254
HB_PAINT_FUNCS_IMPLEMENT_CALLBACKS
255
#undef HB_PAINT_FUNC_IMPLEMENT
256
257
/**
258
 * hb_paint_funcs_create:
259
 *
260
 * Creates a new #hb_paint_funcs_t structure of paint functions.
261
 *
262
 * The initial reference count of 1 should be released with hb_paint_funcs_destroy()
263
 * when you are done using the #hb_paint_funcs_t. This function never returns
264
 * `NULL`.
265
 *
266
 * Return value: (transfer full): The paint-functions structure, or
267
 * hb_paint_funcs_get_empty() on allocation failure.
268
 *
269
 * Since: 7.0.0
270
 */
271
hb_paint_funcs_t *
272
hb_paint_funcs_create ()
273
0
{
274
0
  hb_paint_funcs_t *funcs;
275
0
  if (unlikely (!(funcs = hb_object_create<hb_paint_funcs_t> ())))
276
0
    return const_cast<hb_paint_funcs_t *> (&Null (hb_paint_funcs_t));
277
278
0
  funcs->func =  Null (hb_paint_funcs_t).func;
279
280
0
  return funcs;
281
0
}
282
283
DEFINE_NULL_INSTANCE (hb_paint_funcs_t) =
284
{
285
  HB_OBJECT_HEADER_STATIC,
286
287
  {
288
#define HB_PAINT_FUNC_IMPLEMENT(name) hb_paint_##name##_nil,
289
    HB_PAINT_FUNCS_IMPLEMENT_CALLBACKS
290
#undef HB_PAINT_FUNC_IMPLEMENT
291
  }
292
};
293
294
/**
295
 * hb_paint_funcs_get_empty:
296
 *
297
 * Fetches the singleton empty paint-functions structure.
298
 *
299
 * Return value: (transfer full): The empty paint-functions structure
300
 *
301
 * Since: 7.0.0
302
 **/
303
hb_paint_funcs_t *
304
hb_paint_funcs_get_empty ()
305
0
{
306
0
  return const_cast<hb_paint_funcs_t *> (&Null (hb_paint_funcs_t));
307
0
}
308
309
/**
310
 * hb_paint_funcs_reference: (skip)
311
 * @funcs: The paint-functions structure
312
 *
313
 * Increases the reference count on a paint-functions structure.
314
 *
315
 * This prevents @funcs from being destroyed until a matching
316
 * call to hb_paint_funcs_destroy() is made.
317
 *
318
 * Return value: The paint-functions structure
319
 *
320
 * Since: 7.0.0
321
 */
322
hb_paint_funcs_t *
323
hb_paint_funcs_reference (hb_paint_funcs_t *funcs)
324
0
{
325
0
  return hb_object_reference (funcs);
326
0
}
327
328
/**
329
 * hb_paint_funcs_destroy: (skip)
330
 * @funcs: The paint-functions structure
331
 *
332
 * Decreases the reference count on a paint-functions structure.
333
 *
334
 * When the reference count reaches zero, the structure
335
 * is destroyed, freeing all memory.
336
 *
337
 * Since: 7.0.0
338
 */
339
void
340
hb_paint_funcs_destroy (hb_paint_funcs_t *funcs)
341
0
{
342
0
  if (!hb_object_destroy (funcs)) return;
343
344
0
  if (funcs->destroy)
345
0
  {
346
0
#define HB_PAINT_FUNC_IMPLEMENT(name) \
347
0
    if (funcs->destroy->name) funcs->destroy->name (!funcs->user_data ? nullptr : funcs->user_data->name);
348
0
      HB_PAINT_FUNCS_IMPLEMENT_CALLBACKS
349
0
#undef HB_PAINT_FUNC_IMPLEMENT
350
0
  }
351
352
0
  hb_free (funcs->destroy);
353
0
  hb_free (funcs->user_data);
354
0
  hb_free (funcs);
355
0
}
356
357
/**
358
 * hb_paint_funcs_set_user_data: (skip)
359
 * @funcs: The paint-functions structure
360
 * @key: The user-data key
361
 * @data: A pointer to the user data
362
 * @destroy: (nullable): A callback to call when @data is not needed anymore
363
 * @replace: Whether to replace an existing data with the same key
364
 *
365
 * Attaches a user-data key/data pair to the specified paint-functions structure.
366
 *
367
 * Return value: `true` if success, `false` otherwise
368
 *
369
 * Since: 7.0.0
370
 **/
371
hb_bool_t
372
hb_paint_funcs_set_user_data (hb_paint_funcs_t *funcs,
373
           hb_user_data_key_t *key,
374
           void *              data,
375
           hb_destroy_func_t   destroy,
376
           hb_bool_t           replace)
377
0
{
378
0
  return hb_object_set_user_data (funcs, key, data, destroy, replace);
379
0
}
380
381
/**
382
 * hb_paint_funcs_get_user_data: (skip)
383
 * @funcs: The paint-functions structure
384
 * @key: The user-data key to query
385
 *
386
 * Fetches the user-data associated with the specified key,
387
 * attached to the specified paint-functions structure.
388
 *
389
 * Return value: (transfer none): A pointer to the user data
390
 *
391
 * Since: 7.0.0
392
 **/
393
void *
394
hb_paint_funcs_get_user_data (const hb_paint_funcs_t *funcs,
395
           hb_user_data_key_t       *key)
396
0
{
397
0
  return hb_object_get_user_data (funcs, key);
398
0
}
399
400
/**
401
 * hb_paint_funcs_make_immutable:
402
 * @funcs: The paint-functions structure
403
 *
404
 * Makes a paint-functions structure immutable.
405
 *
406
 * After this call, all attempts to set one of the callbacks
407
 * on @funcs will fail.
408
 *
409
 * Since: 7.0.0
410
 */
411
void
412
hb_paint_funcs_make_immutable (hb_paint_funcs_t *funcs)
413
0
{
414
0
  if (hb_object_is_immutable (funcs))
415
0
    return;
416
417
0
  hb_object_make_immutable (funcs);
418
0
}
419
420
/**
421
 * hb_paint_funcs_is_immutable:
422
 * @funcs: The paint-functions structure
423
 *
424
 * Tests whether a paint-functions structure is immutable.
425
 *
426
 * Return value: `true` if @funcs is immutable, `false` otherwise
427
 *
428
 * Since: 7.0.0
429
 */
430
hb_bool_t
431
hb_paint_funcs_is_immutable (hb_paint_funcs_t *funcs)
432
0
{
433
0
  return hb_object_is_immutable (funcs);
434
0
}
435
436
/**
437
 * hb_paint_set_budget:
438
 * @funcs: paint functions object
439
 * @paint_data: associated paint data
440
 * @budget: the new work-budget policy
441
 *
442
 * Sets the work-budget policy and recharges the live work budget. Negative
443
 * values other than #HB_BUDGET_DEFAULT are clamped to zero.
444
 *
445
 * Return value: `true` if the paint functions support work budgets
446
 *
447
 * Since: 14.5.0
448
 **/
449
hb_bool_t
450
hb_paint_set_budget (hb_paint_funcs_t *funcs, void *paint_data, int64_t budget)
451
0
{
452
0
  return funcs->set_budget (paint_data, budget);
453
0
}
454
455
/**
456
 * hb_paint_get_budget:
457
 * @funcs: paint functions object
458
 * @paint_data: associated paint data
459
 *
460
 * Fetches the configured work-budget policy.
461
 *
462
 * Return value: the configured policy, or #HB_BUDGET_DEFAULT if unsupported
463
 *
464
 * Since: 14.5.0
465
 **/
466
int64_t
467
hb_paint_get_budget (hb_paint_funcs_t *funcs, void *paint_data)
468
0
{
469
0
  return funcs->get_budget (paint_data);
470
0
}
471
472
/**
473
 * hb_paint_get_budget_remaining:
474
 * @funcs: paint functions object
475
 * @paint_data: associated paint data
476
 *
477
 * Fetches the live work budget. A negative value means the budget was
478
 * exhausted. Zero means the preceding work fit exactly, but no positive-cost
479
 * work can start.
480
 *
481
 * Return value: the live work budget; if it is not tracked, the concrete
482
 *   configured policy or #HB_BUDGET_UNLIMITED
483
 *
484
 * Since: 14.5.0
485
 **/
486
int64_t
487
hb_paint_get_budget_remaining (hb_paint_funcs_t *funcs, void *paint_data)
488
0
{
489
0
  int64_t *remaining = funcs->get_budget_remaining_ptr (paint_data);
490
0
  if (remaining)
491
0
    return *remaining;
492
493
0
  int64_t budget = funcs->get_budget (paint_data);
494
0
  return budget == HB_BUDGET_DEFAULT ? HB_BUDGET_UNLIMITED : budget;
495
0
}
496
497
498
/**
499
 * hb_color_line_get_color_stops:
500
 * @color_line: a #hb_color_line_t object
501
 * @start: the index of the first color stop to return
502
 * @count: (inout) (optional): Input = the maximum number of color stops to return;
503
 *     Output = the actual number of color stops returned (may be zero)
504
 * @color_stops: (out caller-allocates) (array length=count) (optional): Array of #hb_color_stop_t to populate.
505
 *     May be `NULL` only if @count is `NULL` or zero.
506
 *
507
 * Fetches a list of color stops from the given color line object.
508
 *
509
 * Note that due to variations being applied, the returned color stops
510
 * may be out of order. It is the callers responsibility to ensure that
511
 * color stops are sorted by their offset before they are used.
512
 *
513
 * Return value: the total number of color stops in @color_line
514
 *
515
 * Since: 7.0.0
516
 */
517
unsigned int
518
hb_color_line_get_color_stops (hb_color_line_t *color_line,
519
                               unsigned int start,
520
                               unsigned int *count,
521
                               hb_color_stop_t *color_stops)
522
0
{
523
0
  if (count && !color_stops) *count = 0;
524
525
0
  return color_line->get_color_stops (color_line,
526
0
              color_line->data,
527
0
              start, count,
528
0
              color_stops,
529
0
              color_line->get_color_stops_user_data);
530
0
}
531
532
/**
533
 * hb_color_line_get_extend:
534
 * @color_line: a #hb_color_line_t object
535
 *
536
 * Fetches the extend mode of the color line object.
537
 *
538
 * Return value: the extend mode of @color_line
539
 *
540
 * Since: 7.0.0
541
 */
542
hb_paint_extend_t
543
hb_color_line_get_extend (hb_color_line_t *color_line)
544
0
{
545
0
  return color_line->get_extend (color_line,
546
0
         color_line->data,
547
0
         color_line->get_extend_user_data);
548
0
}
549
550
551
/**
552
 * hb_paint_push_transform:
553
 * @funcs: paint functions
554
 * @paint_data: associated data passed by the caller
555
 * @xx: xx component of the transform matrix
556
 * @yx: yx component of the transform matrix
557
 * @xy: xy component of the transform matrix
558
 * @yy: yy component of the transform matrix
559
 * @dx: dx component of the transform matrix
560
 * @dy: dy component of the transform matrix
561
 *
562
 * Perform a "push-transform" paint operation.
563
 *
564
 * Since: 7.0.0
565
 */
566
void
567
hb_paint_push_transform (hb_paint_funcs_t *funcs, void *paint_data,
568
                         float xx, float yx,
569
                         float xy, float yy,
570
                         float dx, float dy)
571
0
{
572
0
  funcs->push_transform (paint_data, xx, yx, xy, yy, dx, dy);
573
0
}
574
575
/**
576
 * hb_paint_push_font_transform:
577
 * @funcs: paint functions
578
 * @paint_data: associated data passed by the caller
579
 * @font: a font
580
 *
581
 * Push the transform reflecting the font's scale and slant
582
 * settings onto the paint functions.
583
 *
584
 * Since: 11.0.0
585
 */
586
void
587
hb_paint_push_font_transform (hb_paint_funcs_t *funcs, void *paint_data,
588
                              const hb_font_t *font)
589
0
{
590
0
  funcs->push_font_transform (paint_data, font);
591
0
}
592
593
/**
594
 * hb_paint_push_inverse_font_transform:
595
 * @funcs: paint functions
596
 * @paint_data: associated data passed by the caller
597
 * @font: a font
598
 *
599
 * Push the inverse of the transform reflecting the font's
600
 * scale and slant settings onto the paint functions.
601
 *
602
 * Since: 11.0.0
603
 */
604
void
605
hb_paint_push_inverse_font_transform (hb_paint_funcs_t *funcs, void *paint_data,
606
                                      const hb_font_t *font)
607
0
{
608
0
  funcs->push_inverse_font_transform (paint_data, font);
609
0
}
610
611
/**
612
 * hb_paint_pop_transform:
613
 * @funcs: paint functions
614
 * @paint_data: associated data passed by the caller
615
 *
616
 * Perform a "pop-transform" paint operation.
617
 *
618
 * Since: 7.0.0
619
 */
620
void
621
hb_paint_pop_transform (hb_paint_funcs_t *funcs, void *paint_data)
622
0
{
623
0
  funcs->pop_transform (paint_data);
624
0
}
625
626
/**
627
 * hb_paint_color_glyph:
628
 * @funcs: paint functions
629
 * @paint_data: associated data passed by the caller
630
 * @glyph: the glyph ID
631
 * @font: the font
632
 *
633
 * Perform a "color-glyph" paint operation.
634
 *
635
 * Since: 8.2.0
636
 */
637
hb_bool_t
638
hb_paint_color_glyph (hb_paint_funcs_t *funcs, void *paint_data,
639
                      hb_codepoint_t glyph,
640
                      hb_font_t *font)
641
0
{
642
  /* A callback is owed a font.  Hand it the empty one for none. */
643
0
  if (unlikely (!font)) font = hb_font_get_empty ();
644
0
  return funcs->color_glyph (paint_data, glyph, font);
645
0
}
646
647
/**
648
 * hb_paint_fill_glyph:
649
 * @funcs: paint functions
650
 * @paint_data: associated data passed by the caller
651
 * @glyph: the glyph ID
652
 * @font: the font
653
 * @is_foreground: whether the color is the foreground
654
 * @color: The color to use
655
 *
656
 * Perform a "fill-glyph" paint operation: fill the glyph's shape
657
 * with a solid color.
658
 *
659
 * Since: 14.3.0
660
 */
661
void
662
hb_paint_fill_glyph (hb_paint_funcs_t *funcs, void *paint_data,
663
                     hb_codepoint_t glyph,
664
                     hb_font_t *font,
665
                     hb_bool_t is_foreground,
666
                     hb_color_t color)
667
0
{
668
  /* A callback is owed a font.  Hand it the empty one for none. */
669
0
  if (unlikely (!font)) font = hb_font_get_empty ();
670
0
  funcs->fill_glyph (paint_data, glyph, font, is_foreground, color);
671
0
}
672
673
/**
674
 * hb_paint_push_clip_glyph:
675
 * @funcs: paint functions
676
 * @paint_data: associated data passed by the caller
677
 * @glyph: the glyph ID
678
 * @font: the font
679
 *
680
 * Perform a "push-clip-glyph" paint operation.
681
 *
682
 * Since: 7.0.0
683
 */
684
void
685
hb_paint_push_clip_glyph (hb_paint_funcs_t *funcs, void *paint_data,
686
                          hb_codepoint_t glyph,
687
                          hb_font_t *font)
688
0
{
689
  /* A callback is owed a font.  Hand it the empty one for none. */
690
0
  if (unlikely (!font)) font = hb_font_get_empty ();
691
0
  funcs->push_clip_glyph (paint_data, glyph, font);
692
0
}
693
694
/**
695
 * hb_paint_push_clip_rectangle:
696
 * @funcs: paint functions
697
 * @paint_data: associated data passed by the caller
698
 * @xmin: min X for the rectangle
699
 * @ymin: min Y for the rectangle
700
 * @xmax: max X for the rectangle
701
 * @ymax: max Y for the rectangle
702
 *
703
 * Perform a "push-clip-rect" paint operation.
704
 *
705
 * Since: 7.0.0
706
 */
707
void
708
hb_paint_push_clip_rectangle (hb_paint_funcs_t *funcs, void *paint_data,
709
                              float xmin, float ymin, float xmax, float ymax)
710
0
{
711
0
  funcs->push_clip_rectangle (paint_data, xmin, ymin, xmax, ymax);
712
0
}
713
714
/**
715
 * hb_paint_push_clip_path_start:
716
 * @funcs: paint functions
717
 * @paint_data: associated data passed by the caller
718
 * @draw_data: (out) (nullable): location to receive the draw data
719
 *   the caller should pass alongside the returned draw funcs.
720
 *
721
 * Begin clipping to an arbitrary path.  Returns an
722
 * #hb_draw_funcs_t owned by the backend (the caller must not
723
 * free it) that the caller uses to emit the clip outline via
724
 * hb_draw_*() calls, using the returned @draw_data as the
725
 * draw data.  The returned draw funcs and draw data are only
726
 * valid until the matching hb_paint_push_clip_path_end() call;
727
 * no other paint calls should be made between start and end
728
 * except hb_draw_*() on the returned funcs.  Finish the path
729
 * with hb_paint_push_clip_path_end(); pop the clip later
730
 * with hb_paint_pop_clip().
731
 *
732
 * Usage:
733
 *
734
 * |[<!-- language="plain" -->
735
 * hb_draw_funcs_t *df = hb_paint_push_clip_path_start (pf, pd, &dd);
736
 * hb_draw_move_to (df, dd, NULL, ...);
737
 * hb_draw_line_to (df, dd, NULL, ...);
738
 * ...
739
 * hb_draw_close_path (df, dd, NULL);
740
 * hb_paint_push_clip_path_end (pf, pd);
741
 * /&ast; paint ops here are clipped to the emitted path &ast;/
742
 * hb_paint_pop_clip (pf, pd);
743
 * ]|
744
 *
745
 * Return value: (transfer none): draw funcs that accumulate
746
 *   the clip path, or `NULL` if the backend does not implement
747
 *   arbitrary-path clipping.
748
 *
749
 * Since: 14.2.0
750
 */
751
hb_draw_funcs_t *
752
hb_paint_push_clip_path_start (hb_paint_funcs_t  *funcs,
753
                               void              *paint_data,
754
                               void             **draw_data)
755
0
{
756
0
  void *scratch = nullptr;
757
0
  if (!draw_data) draw_data = &scratch;
758
0
  return funcs->push_clip_path_start (paint_data, draw_data);
759
0
}
760
761
/**
762
 * hb_paint_push_clip_path_end:
763
 * @funcs: paint functions
764
 * @paint_data: associated data passed by the caller
765
 *
766
 * Signal that the arbitrary-clip path started by
767
 * hb_paint_push_clip_path_start() is fully drawn.  The
768
 * accumulated path now acts as a clip on the paint context
769
 * until a matching hb_paint_pop_clip() call.
770
 *
771
 * Since: 14.2.0
772
 */
773
void
774
hb_paint_push_clip_path_end (hb_paint_funcs_t *funcs, void *paint_data)
775
0
{
776
0
  funcs->push_clip_path_end (paint_data);
777
0
}
778
779
/**
780
 * hb_paint_pop_clip:
781
 * @funcs: paint functions
782
 * @paint_data: associated data passed by the caller
783
 *
784
 * Perform a "pop-clip" paint operation.
785
 *
786
 * Since: 7.0.0
787
 */
788
void
789
hb_paint_pop_clip (hb_paint_funcs_t *funcs, void *paint_data)
790
0
{
791
0
  funcs->pop_clip (paint_data);
792
0
}
793
794
/**
795
 * hb_paint_color:
796
 * @funcs: paint functions
797
 * @paint_data: associated data passed by the caller
798
 * @is_foreground: whether the color is the foreground
799
 * @color: The color to use
800
 *
801
 * Perform a "color" paint operation.
802
 *
803
 * Since: 7.0.0
804
 */
805
void
806
hb_paint_color (hb_paint_funcs_t *funcs, void *paint_data,
807
                hb_bool_t is_foreground,
808
                hb_color_t color)
809
0
{
810
0
  funcs->color (paint_data, is_foreground, color);
811
0
}
812
813
/**
814
 * hb_paint_image:
815
 * @funcs: paint functions
816
 * @paint_data: associated data passed by the caller
817
 * @image: image data
818
 * @width: width of the raster image in pixels, or 0
819
 * @height: height of the raster image in pixels, or 0
820
 * @format: the image format as a tag
821
 * @slant: Deprecated. set to 0.0
822
 * @extents: (nullable): the extents of the glyph
823
 *
824
 * Perform a "image" paint operation.
825
 *
826
 * Since: 7.0.0
827
 */
828
void
829
hb_paint_image (hb_paint_funcs_t *funcs, void *paint_data,
830
                hb_blob_t *image,
831
                unsigned int width,
832
                unsigned int height,
833
                hb_tag_t format,
834
                HB_UNUSED float slant,
835
                hb_glyph_extents_t *extents)
836
0
{
837
  /* A callback is owed a blob.  Hand it the empty one for none. */
838
0
  if (unlikely (!image)) image = hb_blob_get_empty ();
839
0
  funcs->image (paint_data, image, width, height, format, 0.f, extents);
840
0
}
841
842
/**
843
 * hb_paint_linear_gradient:
844
 * @funcs: paint functions
845
 * @paint_data: associated data passed by the caller
846
 * @color_line: Color information for the gradient
847
 * @x0: X coordinate of the first point
848
 * @y0: Y coordinate of the first point
849
 * @x1: X coordinate of the second point
850
 * @y1: Y coordinate of the second point
851
 * @x2: X coordinate of the third point
852
 * @y2: Y coordinate of the third point
853
 *
854
 * Perform a "linear-gradient" paint operation.
855
 *
856
 * Since: 7.0.0
857
 */
858
void
859
hb_paint_linear_gradient (hb_paint_funcs_t *funcs, void *paint_data,
860
                          hb_color_line_t *color_line,
861
                          float x0, float y0,
862
                          float x1, float y1,
863
                          float x2, float y2)
864
0
{
865
0
  funcs->linear_gradient (paint_data, color_line, x0, y0, x1, y1, x2, y2);
866
0
}
867
868
/**
869
 * hb_paint_radial_gradient:
870
 * @funcs: paint functions
871
 * @paint_data: associated data passed by the caller
872
 * @color_line: Color information for the gradient
873
 * @x0: X coordinate of the first circle's center
874
 * @y0: Y coordinate of the first circle's center
875
 * @r0: radius of the first circle
876
 * @x1: X coordinate of the second circle's center
877
 * @y1: Y coordinate of the second circle's center
878
 * @r1: radius of the second circle
879
 *
880
 * Perform a "radial-gradient" paint operation.
881
 *
882
 * Since: 7.0.0
883
 */
884
void
885
hb_paint_radial_gradient (hb_paint_funcs_t *funcs, void *paint_data,
886
                          hb_color_line_t *color_line,
887
                          float x0, float y0, float r0,
888
                          float x1, float y1, float r1)
889
0
{
890
0
  funcs->radial_gradient (paint_data, color_line, x0, y0, r0, x1, y1, r1);
891
0
}
892
893
/**
894
 * hb_paint_sweep_gradient:
895
 * @funcs: paint functions
896
 * @paint_data: associated data passed by the caller
897
 * @color_line: Color information for the gradient
898
 * @x0: X coordinate of the circle's center
899
 * @y0: Y coordinate of the circle's center
900
 * @start_angle: the start angle
901
 * @end_angle: the end angle
902
 *
903
 * Perform a "sweep-gradient" paint operation.
904
 *
905
 * Since: 7.0.0
906
 */
907
void
908
hb_paint_sweep_gradient (hb_paint_funcs_t *funcs, void *paint_data,
909
                         hb_color_line_t *color_line,
910
                         float x0, float y0,
911
                         float start_angle, float end_angle)
912
0
{
913
0
  funcs->sweep_gradient (paint_data, color_line, x0, y0, start_angle, end_angle);
914
0
}
915
916
/**
917
 * hb_paint_push_group:
918
 * @funcs: paint functions
919
 * @paint_data: associated data passed by the caller
920
 *
921
 * Perform a "push-group" paint operation.
922
 *
923
 * Since: 7.0.0
924
 */
925
void
926
hb_paint_push_group (hb_paint_funcs_t *funcs, void *paint_data)
927
0
{
928
0
  funcs->push_group (paint_data);
929
0
}
930
931
/**
932
 * hb_paint_push_group_for:
933
 * @funcs: paint functions
934
 * @paint_data: associated data passed by the caller
935
 * @mode: the compositing mode that will be used when the group is popped
936
 *
937
 * Perform a "push-group" paint operation, with the compositing
938
 * mode known in advance.  By default, this calls
939
 * hb_paint_push_group().
940
 *
941
 * Since: 14.2.0
942
 */
943
void
944
hb_paint_push_group_for (hb_paint_funcs_t *funcs, void *paint_data,
945
                         hb_paint_composite_mode_t mode)
946
0
{
947
0
  funcs->push_group_for (paint_data, mode);
948
0
}
949
950
/**
951
 * hb_paint_pop_group:
952
 * @funcs: paint functions
953
 * @paint_data: associated data passed by the caller
954
 * @mode: the compositing mode to use
955
 *
956
 * Perform a "pop-group" paint operation.
957
 *
958
 * Since: 7.0.0
959
 */
960
void
961
hb_paint_pop_group (hb_paint_funcs_t *funcs, void *paint_data,
962
                    hb_paint_composite_mode_t mode)
963
0
{
964
0
  funcs->pop_group (paint_data, mode);
965
0
}
966
967
/**
968
 * hb_paint_custom_palette_color:
969
 * @funcs: paint functions.
970
 * @paint_data: associated data passed by the caller.
971
 * @color_index: color index to fetch.
972
 * @color: (out): fetched color.
973
 *
974
 * Gets the custom palette override color for @color_index.
975
 *
976
 * Return value: `true` if a custom color is provided, `false` otherwise.
977
 *
978
 * Since: 7.0.0
979
 */
980
hb_bool_t
981
hb_paint_custom_palette_color (hb_paint_funcs_t *funcs, void *paint_data,
982
                               unsigned int color_index,
983
                               hb_color_t *color)
984
0
{
985
0
  return funcs->custom_palette_color (paint_data, color_index, color);
986
0
}
987
988
989
/**
990
 * hb_paint_reduce_linear_anchors:
991
 * @x0: x coordinate of P0 (color stop 0).
992
 * @y0: y coordinate of P0 (color stop 0).
993
 * @x1: x coordinate of P1 (color stop 1).
994
 * @y1: y coordinate of P1 (color stop 1).
995
 * @x2: x coordinate of P2 (rotation reference).
996
 * @y2: y coordinate of P2 (rotation reference).
997
 * @xx0: (out): x coordinate of the resulting axis start.
998
 * @yy0: (out): y coordinate of the resulting axis start.
999
 * @xx1: (out): x coordinate of the resulting axis end.
1000
 * @yy1: (out): y coordinate of the resulting axis end.
1001
 *
1002
 * Reduces a COLRv1 linear gradient's 3-anchor spec (P0=color
1003
 * stop 0, P1=color stop 1, P2=rotation reference) to the
1004
 * 2-point axis (P0, P1') used by SVG / cairo / most software
1005
 * renderers.  P1' is the foot of P1 on the line through P0
1006
 * perpendicular to (P2 - P0); the resulting axis is the
1007
 * gradient's actual direction (perpendicular to the rotation
1008
 * line).  Degenerate (P0 == P2) passes through unchanged.  If
1009
 * P1 is on the line through P0 and P2, P1' is P0: the gradient
1010
 * is along no line.
1011
 *
1012
 * Since: 14.2.0
1013
 **/
1014
void
1015
hb_paint_reduce_linear_anchors (float x0, float y0,
1016
        float x1, float y1,
1017
        float x2, float y2,
1018
        float *xx0, float *yy0,
1019
        float *xx1, float *yy1)
1020
0
{
1021
0
  float q2x = x2 - x0, q2y = y2 - y0;
1022
0
  float s = q2x * q2x + q2y * q2y;
1023
0
  if (s < 1e-6f)
1024
0
  {
1025
0
    *xx0 = x0; *yy0 = y0;
1026
0
    *xx1 = x1; *yy1 = y1;
1027
0
    return;
1028
0
  }
1029
0
  float q1x = x1 - x0, q1y = y1 - y0;
1030
  /* P1 on the line through P0 and P2 has its foot at P0, which the
1031
   * arithmetic below only comes close to. */
1032
0
  if (q1x * q2y == q1y * q2x)
1033
0
  {
1034
0
    *xx0 = *xx1 = x0;
1035
0
    *yy0 = *yy1 = y0;
1036
0
    return;
1037
0
  }
1038
0
  float k = (q2x * q1x + q2y * q1y) / s;
1039
0
  *xx0 = x0;
1040
0
  *yy0 = y0;
1041
0
  *xx1 = x1 - k * q2x;
1042
0
  *yy1 = y1 - k * q2y;
1043
0
}
1044
1045
/**
1046
 * hb_paint_normalize_color_line:
1047
 * @stops: (array length=len) (inout): color stops.
1048
 * @len: number of stops.
1049
 * @min: (out): original minimum offset.
1050
 * @max: (out): original maximum offset.
1051
 *
1052
 * Sorts @stops by offset and rescales offsets into [0, 1] in
1053
 * place.  Writes the original (min, max) to @min / @max so the
1054
 * caller can shift the gradient geometry (axis endpoints for
1055
 * linear, centers+radii for radial, start+end angles for sweep)
1056
 * to keep the rendered gradient visually unchanged after the
1057
 * rescale.  Empty input is safe: both out-parameters set to 0.
1058
 *
1059
 * Since: 14.2.0
1060
 **/
1061
void
1062
hb_paint_normalize_color_line (hb_color_stop_t *stops,
1063
             unsigned int     len,
1064
             float           *min,
1065
             float           *max)
1066
0
{
1067
0
  if (unlikely (!len))
1068
0
  {
1069
0
    *min = *max = 0.f;
1070
0
    return;
1071
0
  }
1072
1073
0
  hb_color_stops_sort (stops, len);
1074
1075
0
  float mn = stops[0].offset, mx = stops[0].offset;
1076
0
  for (unsigned i = 1; i < len; i++)
1077
0
  {
1078
0
    mn = hb_min (mn, stops[i].offset);
1079
0
    mx = hb_max (mx, stops[i].offset);
1080
0
  }
1081
0
  if (mn != mx)
1082
0
    for (unsigned i = 0; i < len; i++)
1083
0
      stops[i].offset = (stops[i].offset - mn) / (mx - mn);
1084
1085
0
  *min = mn;
1086
0
  *max = mx;
1087
0
}
1088
1089
/* The color @t of the way from @c0 to @c1, the two interpolated with
1090
 * alpha multiplied into them, as COLR interpolates.  Where they differ
1091
 * in alpha that is not hb_color_lerp(): the more transparent a color
1092
 * is, the less of it there is to give. */
1093
static hb_color_t
1094
hb_color_lerp_premultiplied (hb_color_t c0, hb_color_t c1, float t)
1095
0
{
1096
0
  float a0 = hb_color_get_alpha (c0), a1 = hb_color_get_alpha (c1);
1097
0
  if (a0 == a1)
1098
0
    return hb_color_lerp (c0, c1, t);
1099
1100
0
  float w0 = (1.f - t) * a0, w1 = t * a1;
1101
0
  float a = w0 + w1;
1102
0
  if (!(a > 0.f))
1103
    /* Transparent: the color that the other one fades out in. */
1104
0
    return (a0 > a1 ? c0 : c1) & ~(hb_color_t) 0xFF;
1105
1106
0
  auto channel = [&] (unsigned shift) -> unsigned {
1107
0
    float v = (w0 * ((c0 >> shift) & 0xFF) + w1 * ((c1 >> shift) & 0xFF)) / a + 0.5f;
1108
0
    if (!(v > 0.f)) return 0;
1109
0
    if (v > 255.f) return 255;
1110
0
    return (unsigned) v;
1111
0
  };
1112
0
  return HB_COLOR (channel (24), channel (16), channel (8),
1113
0
       a < 255.f ? (unsigned) (a + 0.5f) : 255);
1114
0
}
1115
1116
/**
1117
 * hb_paint_sweep_gradient_tiles:
1118
 * @stops: (array length=n_stops) (inout): color stops (sorted, offsets in [0,1]).
1119
 * @n_stops: number of stops.
1120
 * @extend: extend mode.
1121
 * @start_angle: sweep start angle, in radians.
1122
 * @end_angle: sweep end angle, in radians.
1123
 * @emit_patch: (scope call): callback invoked once per tile.
1124
 * @user_data: data passed to @emit_patch.
1125
 *
1126
 * Iterates the full 0..2π sweep produced by a color-stop list,
1127
 * invoking @emit_patch once per (start, end) angular segment.
1128
 * Segments are clipped to 0..2π, wherever the angles lie.
1129
 * Handles #HB_PAINT_EXTEND_PAD, #HB_PAINT_EXTEND_REPEAT, and
1130
 * #HB_PAINT_EXTEND_REFLECT.  Stops must be pre-sorted by
1131
 * offset; use hb_paint_normalize_color_line() first if they
1132
 * aren't.
1133
 *
1134
 * Since: 14.2.0
1135
 **/
1136
void
1137
hb_paint_sweep_gradient_tiles (hb_color_stop_t                     *stops,
1138
             unsigned int                         n_stops,
1139
             hb_paint_extend_t                    extend,
1140
             float                                start_angle,
1141
             float                                end_angle,
1142
             hb_paint_sweep_gradient_tile_func_t  emit_patch,
1143
             void                                *user_data)
1144
0
{
1145
0
  if (!n_stops) return;
1146
1147
  /* Variation deltas can put the angles any distance outside the
1148
   * circle.  Emit only the part of each tile that is within 0..2π;
1149
   * callers do work proportional to the width of the tile. */
1150
0
  auto emit = [&] (float a0, hb_color_t c0, float a1, hb_color_t c1)
1151
0
  {
1152
0
    float t0 = hb_max (a0, 0.f);
1153
0
    float t1 = hb_min (a1, HB_2_PI);
1154
0
    if (t0 > t1) return;
1155
0
    hb_color_t d0 = c0, d1 = c1;
1156
0
    if (c0 != c1)
1157
0
    {
1158
0
      if (t0 != a0) d0 = hb_color_lerp_premultiplied (c0, c1, (t0 - a0) / (a1 - a0));
1159
0
      if (t1 != a1) d1 = hb_color_lerp_premultiplied (c0, c1, (t1 - a0) / (a1 - a0));
1160
0
    }
1161
0
    emit_patch (t0, d0, t1, d1, user_data);
1162
0
  };
1163
1164
0
  if (start_angle == end_angle)
1165
0
  {
1166
0
    if (extend == HB_PAINT_EXTEND_PAD)
1167
0
    {
1168
0
      if (start_angle > 0.f)
1169
0
  emit (0.f, stops[0].color, start_angle, stops[0].color);
1170
0
      if (end_angle < HB_2_PI)
1171
0
  emit (end_angle, stops[n_stops - 1].color, HB_2_PI, stops[n_stops - 1].color);
1172
0
    }
1173
0
    return;
1174
0
  }
1175
1176
0
  if (end_angle < start_angle)
1177
0
  {
1178
0
    float tmp = start_angle; start_angle = end_angle; end_angle = tmp;
1179
0
    for (unsigned i = 0; i < n_stops - 1 - i; i++)
1180
0
    {
1181
0
      hb_color_stop_t t = stops[i];
1182
0
      stops[i] = stops[n_stops - 1 - i];
1183
0
      stops[n_stops - 1 - i] = t;
1184
0
    }
1185
0
    for (unsigned i = 0; i < n_stops; i++)
1186
0
      stops[i].offset = 1.f - stops[i].offset;
1187
0
  }
1188
1189
  /* Map stop offsets to angles. */
1190
0
  float angles_buf[16];
1191
0
  hb_color_t colors_buf[16];
1192
0
  float *angles = angles_buf;
1193
0
  hb_color_t *colors = colors_buf;
1194
0
  bool dynamic = false;
1195
1196
0
  if (n_stops > 16)
1197
0
  {
1198
0
    angles = (float *) hb_malloc (sizeof (float) * n_stops);
1199
0
    colors = (hb_color_t *) hb_malloc (sizeof (hb_color_t) * n_stops);
1200
0
    if (!angles || !colors)
1201
0
    {
1202
0
      hb_free (angles);
1203
0
      hb_free (colors);
1204
0
      return;
1205
0
    }
1206
0
    dynamic = true;
1207
0
  }
1208
1209
0
  for (unsigned i = 0; i < n_stops; i++)
1210
0
  {
1211
0
    angles[i] = start_angle + stops[i].offset * (end_angle - start_angle);
1212
0
    colors[i] = stops[i].color;
1213
0
  }
1214
1215
0
  if (extend == HB_PAINT_EXTEND_PAD)
1216
0
  {
1217
0
    unsigned pos;
1218
0
    hb_color_t color0 = colors[0];
1219
0
    for (pos = 0; pos < n_stops; pos++)
1220
0
    {
1221
0
      if (angles[pos] >= 0)
1222
0
      {
1223
0
  if (pos > 0)
1224
0
  {
1225
0
    float f = (0.f - angles[pos - 1]) / (angles[pos] - angles[pos - 1]);
1226
0
    color0 = hb_color_lerp_premultiplied (colors[pos - 1], colors[pos], f);
1227
0
  }
1228
0
  break;
1229
0
      }
1230
0
    }
1231
0
    if (pos == n_stops)
1232
0
    {
1233
0
      color0 = colors[n_stops - 1];
1234
0
      emit (0.f, color0, HB_2_PI, color0);
1235
0
      goto done;
1236
0
    }
1237
0
    emit (0.f, color0, angles[pos], colors[pos]);
1238
0
    for (pos++; pos < n_stops; pos++)
1239
0
    {
1240
0
      if (angles[pos] <= HB_2_PI)
1241
0
  emit (angles[pos - 1], colors[pos - 1], angles[pos], colors[pos]);
1242
0
      else
1243
0
      {
1244
0
  float f = (HB_2_PI - angles[pos - 1]) / (angles[pos] - angles[pos - 1]);
1245
0
  hb_color_t color1 = hb_color_lerp_premultiplied (colors[pos - 1], colors[pos], f);
1246
0
  emit (angles[pos - 1], colors[pos - 1], HB_2_PI, color1);
1247
0
  break;
1248
0
      }
1249
0
    }
1250
0
    if (pos == n_stops)
1251
0
    {
1252
0
      color0 = colors[n_stops - 1];
1253
0
      emit (angles[n_stops - 1], color0, HB_2_PI, color0);
1254
0
      goto done;
1255
0
    }
1256
0
  }
1257
0
  else
1258
0
  {
1259
0
    float span = angles[n_stops - 1] - angles[0];
1260
0
    if (!(fabsf (span) >= 1e-6f)) /* Reversed to catch NaN. */
1261
0
      goto done;
1262
1263
0
    span = fabsf (span);
1264
1265
0
    if (span < HB_2_PI / HB_PAINT_MAX_SWEEP_TILES)
1266
0
    {
1267
      /* The pattern repeats faster than the tile resolution; tiling
1268
       * it would cost unbounded work for sub-visual detail.  Fill
1269
       * the whole circle with the period-average color instead,
1270
       * averaged as it is interpolated: with alpha multiplied in. */
1271
0
      float r = 0.f, g = 0.f, b = 0.f, a = 0.f;
1272
0
      for (unsigned i = 1; i < n_stops; i++)
1273
0
      {
1274
0
  float w = (angles[i] - angles[i - 1]) / span * 0.5f;
1275
0
  float a0 = hb_color_get_alpha (colors[i - 1]), a1 = hb_color_get_alpha (colors[i]);
1276
0
  r += w * (hb_color_get_red (colors[i - 1]) * a0 + hb_color_get_red (colors[i]) * a1);
1277
0
  g += w * (hb_color_get_green (colors[i - 1]) * a0 + hb_color_get_green (colors[i]) * a1);
1278
0
  b += w * (hb_color_get_blue (colors[i - 1]) * a0 + hb_color_get_blue (colors[i]) * a1);
1279
0
  a += w * (a0 + a1);
1280
0
      }
1281
0
      if (a > 0.f)
1282
0
      {
1283
0
  r /= a;
1284
0
  g /= a;
1285
0
  b /= a;
1286
0
      }
1287
0
      hb_color_t avg = HB_COLOR (hb_clamp_to<uint8_t> ((double) b + 0.5),
1288
0
         hb_clamp_to<uint8_t> ((double) g + 0.5),
1289
0
         hb_clamp_to<uint8_t> ((double) r + 0.5),
1290
0
         hb_clamp_to<uint8_t> ((double) a + 0.5));
1291
0
      emit (0.f, avg, HB_2_PI, avg);
1292
0
      goto done;
1293
0
    }
1294
1295
    /* First repeat that can reach angle 0, computed by division;
1296
     * counting there one span at a time can take effectively forever,
1297
     * or fail to terminate outright once the span drops below the
1298
     * float ulp of the angles.  One repeat of slack absorbs rounding
1299
     * differences; earlier repeats are fully clipped anyway. */
1300
0
    double dk = floor (-(double) hb_max (angles[0], angles[n_stops - 1]) / (double) span) - 1.;
1301
0
    if (!(dk >= -1.e18 && dk <= 1.e18)) /* Non-finite angles. */
1302
0
      goto done;
1303
1304
0
    unsigned iterations = 0, tiles = 0;
1305
0
    for (int64_t l = (int64_t) dk; ; l++)
1306
0
    {
1307
0
      for (unsigned i = 1; i < n_stops; i++)
1308
0
      {
1309
  /* Bounds clipped-away segments too; those stay negligible for
1310
   * sorted stops but are unbounded if the sort contract is
1311
   * violated. */
1312
0
  if (unlikely (iterations++ >= 256 * HB_PAINT_MAX_SWEEP_TILES))
1313
0
    goto done;
1314
1315
0
  float a0_l, a1_l;
1316
0
  hb_color_t col0, col1;
1317
0
  if ((l % 2 != 0) && (extend == HB_PAINT_EXTEND_REFLECT))
1318
0
  {
1319
0
    a0_l = angles[0] + angles[n_stops - 1] - angles[n_stops - i] + l * span;
1320
0
    a1_l = angles[0] + angles[n_stops - 1] - angles[n_stops - 1 - i] + l * span;
1321
0
    col0 = colors[n_stops - i];
1322
0
    col1 = colors[n_stops - 1 - i];
1323
0
  }
1324
0
  else
1325
0
  {
1326
0
    a0_l = angles[i - 1] + l * span;
1327
0
    a1_l = angles[i] + l * span;
1328
0
    col0 = colors[i - 1];
1329
0
    col1 = colors[i];
1330
0
  }
1331
1332
0
  if (a1_l < 0.f) continue;
1333
0
  if (unlikely (tiles++ >= HB_PAINT_MAX_SWEEP_TILES))
1334
0
    goto done;
1335
0
  if (a0_l < 0.f)
1336
0
  {
1337
0
    float f = (0.f - a0_l) / (a1_l - a0_l);
1338
0
    hb_color_t c = hb_color_lerp_premultiplied (col0, col1, f);
1339
0
    emit (0.f, c, a1_l, col1);
1340
0
  }
1341
0
  else if (a1_l >= HB_2_PI)
1342
0
  {
1343
0
    float f = (HB_2_PI - a0_l) / (a1_l - a0_l);
1344
0
    hb_color_t c = hb_color_lerp_premultiplied (col0, col1, f);
1345
0
    emit (a0_l, col0, HB_2_PI, c);
1346
0
    goto done;
1347
0
  }
1348
0
  else
1349
0
    emit (a0_l, col0, a1_l, col1);
1350
0
      }
1351
0
    }
1352
0
  }
1353
1354
0
done:
1355
0
  if (dynamic)
1356
0
  {
1357
0
    hb_free (angles);
1358
0
    hb_free (colors);
1359
0
  }
1360
0
}
1361
1362
#endif