Path: blob/master/thirdparty/harfbuzz/src/hb-cairo-utils.cc
9903 views
/*1* Copyright © 2022 Red Hat, Inc2* Copyright © 2021, 2022 Black Foundry3*4* This is part of HarfBuzz, a text shaping library.5*6* Permission is hereby granted, without written agreement and without7* license or royalty fees, to use, copy, modify, and distribute this8* software and its documentation for any purpose, provided that the9* above copyright notice and the following two paragraphs appear in10* all copies of this software.11*12* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR13* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES14* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN15* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH16* DAMAGE.17*18* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,19* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND20* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS21* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO22* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.23*24* Google Author(s): Matthias Clasen25*/2627#include "hb.hh"2829#ifdef HAVE_CAIRO3031#include "hb-cairo-utils.hh"3233/* Some routines in this file were ported from BlackRenderer by Black Foundry.34* Used by permission to relicense to HarfBuzz license.35*36* https://github.com/BlackFoundryCom/black-renderer37*/3839#define PREALLOCATED_COLOR_STOPS 164041typedef struct {42float r, g, b, a;43} hb_cairo_color_t;4445static inline cairo_extend_t46hb_cairo_extend (hb_paint_extend_t extend)47{48switch (extend)49{50case HB_PAINT_EXTEND_PAD: return CAIRO_EXTEND_PAD;51case HB_PAINT_EXTEND_REPEAT: return CAIRO_EXTEND_REPEAT;52case HB_PAINT_EXTEND_REFLECT: return CAIRO_EXTEND_REFLECT;53default: break;54}5556return CAIRO_EXTEND_PAD;57}5859#ifdef CAIRO_HAS_PNG_FUNCTIONS60typedef struct61{62hb_blob_t *blob;63unsigned int offset;64} hb_cairo_read_blob_data_t;6566static cairo_status_t67hb_cairo_read_blob (void *closure,68unsigned char *data,69unsigned int length)70{71hb_cairo_read_blob_data_t *r = (hb_cairo_read_blob_data_t *) closure;72const char *d;73unsigned int size;7475d = hb_blob_get_data (r->blob, &size);7677if (r->offset + length > size)78return CAIRO_STATUS_READ_ERROR;7980hb_memcpy (data, d + r->offset, length);81r->offset += length;8283return CAIRO_STATUS_SUCCESS;84}85#endif8687static const cairo_user_data_key_t *_hb_cairo_surface_blob_user_data_key = {0};8889static void90_hb_cairo_destroy_blob (void *p)91{92hb_blob_destroy ((hb_blob_t *) p);93}9495hb_bool_t96_hb_cairo_paint_glyph_image (hb_cairo_context_t *c,97hb_blob_t *blob,98unsigned width,99unsigned height,100hb_tag_t format,101HB_UNUSED float slant_deprecated,102hb_glyph_extents_t *extents)103{104cairo_t *cr = c->cr;105106if (!extents) /* SVG currently. */107return false;108109cairo_surface_t *surface = nullptr;110111#ifdef CAIRO_HAS_PNG_FUNCTIONS112if (format == HB_PAINT_IMAGE_FORMAT_PNG)113{114hb_cairo_read_blob_data_t r;115r.blob = blob;116r.offset = 0;117surface = cairo_image_surface_create_from_png_stream (hb_cairo_read_blob, &r);118119/* For PNG, width,height can be unreliable, as is the case for NotoColorEmoji :(.120* Just pull them out of the surface. */121width = cairo_image_surface_get_width (surface);122height = cairo_image_surface_get_width (surface);123}124else125#endif126if (format == HB_PAINT_IMAGE_FORMAT_BGRA)127{128/* Byte-endian conversion. */129unsigned data_size = hb_blob_get_length (blob);130if (data_size < width * height * 4)131return false;132133unsigned char *data;134#ifdef __BYTE_ORDER135if (__BYTE_ORDER == __BIG_ENDIAN)136{137data = (unsigned char *) hb_blob_get_data_writable (blob, nullptr);138if (!data)139return false;140141unsigned count = width * height * 4;142for (unsigned i = 0; i < count; i += 4)143{144unsigned char b;145b = data[i];146data[i] = data[i+3];147data[i+3] = b;148b = data[i+1];149data[i+1] = data[i+2];150data[i+2] = b;151}152}153else154#endif155data = (unsigned char *) hb_blob_get_data (blob, nullptr);156157surface = cairo_image_surface_create_for_data (data,158CAIRO_FORMAT_ARGB32,159width, height,160width * 4);161162cairo_surface_set_user_data (surface,163_hb_cairo_surface_blob_user_data_key,164hb_blob_reference (blob),165_hb_cairo_destroy_blob);166}167168if (!surface)169return false;170171cairo_save (cr);172/* this clip is here to work around recording surface limitations */173cairo_rectangle (cr,174extents->x_bearing,175extents->y_bearing,176extents->width,177extents->height);178cairo_clip (cr);179180cairo_pattern_t *pattern = cairo_pattern_create_for_surface (surface);181cairo_pattern_set_extend (pattern, CAIRO_EXTEND_PAD);182183cairo_matrix_t matrix = {(double) width, 0, 0, (double) height, 0, 0};184cairo_pattern_set_matrix (pattern, &matrix);185186cairo_translate (cr, extents->x_bearing, extents->y_bearing);187cairo_scale (cr, extents->width, extents->height);188cairo_set_source (cr, pattern);189190cairo_paint (cr);191192cairo_pattern_destroy (pattern);193cairo_surface_destroy (surface);194195cairo_restore (cr);196197return true;198}199200static void201_hb_cairo_reduce_anchors (float x0, float y0,202float x1, float y1,203float x2, float y2,204float *xx0, float *yy0,205float *xx1, float *yy1)206{207float q1x, q1y, q2x, q2y;208float s;209float k;210211q2x = x2 - x0;212q2y = y2 - y0;213q1x = x1 - x0;214q1y = y1 - y0;215216s = q2x * q2x + q2y * q2y;217if (s < 0.000001f)218{219*xx0 = x0; *yy0 = y0;220*xx1 = x1; *yy1 = y1;221return;222}223224k = (q2x * q1x + q2y * q1y) / s;225*xx0 = x0;226*yy0 = y0;227*xx1 = x1 - k * q2x;228*yy1 = y1 - k * q2y;229}230231static int232_hb_cairo_cmp_color_stop (const void *p1,233const void *p2)234{235const hb_color_stop_t *c1 = (const hb_color_stop_t *) p1;236const hb_color_stop_t *c2 = (const hb_color_stop_t *) p2;237238if (c1->offset < c2->offset)239return -1;240else if (c1->offset > c2->offset)241return 1;242else243return 0;244}245246static void247_hb_cairo_normalize_color_line (hb_color_stop_t *stops,248unsigned int len,249float *omin,250float *omax)251{252float min, max;253254hb_qsort (stops, len, sizeof (hb_color_stop_t), _hb_cairo_cmp_color_stop);255256min = max = stops[0].offset;257for (unsigned int i = 0; i < len; i++)258{259min = hb_min (min, stops[i].offset);260max = hb_max (max, stops[i].offset);261}262263if (min != max)264{265for (unsigned int i = 0; i < len; i++)266stops[i].offset = (stops[i].offset - min) / (max - min);267}268269*omin = min;270*omax = max;271}272273static bool274_hb_cairo_get_color_stops (hb_cairo_context_t *c,275hb_color_line_t *color_line,276unsigned *count,277hb_color_stop_t **stops)278{279unsigned len = hb_color_line_get_color_stops (color_line, 0, nullptr, nullptr);280if (len > *count)281{282*stops = (hb_color_stop_t *) hb_malloc (len * sizeof (hb_color_stop_t));283if (unlikely (!stops))284return false;285}286hb_color_line_get_color_stops (color_line, 0, &len, *stops);287for (unsigned i = 0; i < len; i++)288if ((*stops)[i].is_foreground)289{290#ifdef HAVE_CAIRO_USER_SCALED_FONT_GET_FOREGROUND_SOURCE291double r, g, b, a;292cairo_pattern_t *foreground = cairo_user_scaled_font_get_foreground_source (c->scaled_font);293if (cairo_pattern_get_rgba (foreground, &r, &g, &b, &a) == CAIRO_STATUS_SUCCESS)294(*stops)[i].color = HB_COLOR (round (b * 255.), round (g * 255.), round (r * 255.),295round (a * hb_color_get_alpha ((*stops)[i].color)));296else297#endif298(*stops)[i].color = HB_COLOR (0, 0, 0, hb_color_get_alpha ((*stops)[i].color));299}300301*count = len;302return true;303}304305void306_hb_cairo_paint_linear_gradient (hb_cairo_context_t *c,307hb_color_line_t *color_line,308float x0, float y0,309float x1, float y1,310float x2, float y2)311{312cairo_t *cr = c->cr;313314unsigned int len = PREALLOCATED_COLOR_STOPS;315hb_color_stop_t stops_[PREALLOCATED_COLOR_STOPS];316hb_color_stop_t *stops = stops_;317float xx0, yy0, xx1, yy1;318float xxx0, yyy0, xxx1, yyy1;319float min, max;320cairo_pattern_t *pattern;321322if (unlikely (!_hb_cairo_get_color_stops (c, color_line, &len, &stops)))323return;324_hb_cairo_normalize_color_line (stops, len, &min, &max);325326_hb_cairo_reduce_anchors (x0, y0, x1, y1, x2, y2, &xx0, &yy0, &xx1, &yy1);327328xxx0 = xx0 + min * (xx1 - xx0);329yyy0 = yy0 + min * (yy1 - yy0);330xxx1 = xx0 + max * (xx1 - xx0);331yyy1 = yy0 + max * (yy1 - yy0);332333pattern = cairo_pattern_create_linear ((double) xxx0, (double) yyy0, (double) xxx1, (double) yyy1);334cairo_pattern_set_extend (pattern, hb_cairo_extend (hb_color_line_get_extend (color_line)));335for (unsigned int i = 0; i < len; i++)336{337double r, g, b, a;338r = hb_color_get_red (stops[i].color) / 255.;339g = hb_color_get_green (stops[i].color) / 255.;340b = hb_color_get_blue (stops[i].color) / 255.;341a = hb_color_get_alpha (stops[i].color) / 255.;342cairo_pattern_add_color_stop_rgba (pattern, (double) stops[i].offset, r, g, b, a);343}344345cairo_set_source (cr, pattern);346cairo_paint (cr);347348cairo_pattern_destroy (pattern);349350if (stops != stops_)351hb_free (stops);352}353354void355_hb_cairo_paint_radial_gradient (hb_cairo_context_t *c,356hb_color_line_t *color_line,357float x0, float y0, float r0,358float x1, float y1, float r1)359{360cairo_t *cr = c->cr;361362unsigned int len = PREALLOCATED_COLOR_STOPS;363hb_color_stop_t stops_[PREALLOCATED_COLOR_STOPS];364hb_color_stop_t *stops = stops_;365float min, max;366float xx0, yy0, xx1, yy1;367float rr0, rr1;368cairo_pattern_t *pattern;369370if (unlikely (!_hb_cairo_get_color_stops (c, color_line, &len, &stops)))371return;372_hb_cairo_normalize_color_line (stops, len, &min, &max);373374xx0 = x0 + min * (x1 - x0);375yy0 = y0 + min * (y1 - y0);376xx1 = x0 + max * (x1 - x0);377yy1 = y0 + max * (y1 - y0);378rr0 = r0 + min * (r1 - r0);379rr1 = r0 + max * (r1 - r0);380381pattern = cairo_pattern_create_radial ((double) xx0, (double) yy0, (double) rr0, (double) xx1, (double) yy1, (double) rr1);382cairo_pattern_set_extend (pattern, hb_cairo_extend (hb_color_line_get_extend (color_line)));383384for (unsigned int i = 0; i < len; i++)385{386double r, g, b, a;387r = hb_color_get_red (stops[i].color) / 255.;388g = hb_color_get_green (stops[i].color) / 255.;389b = hb_color_get_blue (stops[i].color) / 255.;390a = hb_color_get_alpha (stops[i].color) / 255.;391cairo_pattern_add_color_stop_rgba (pattern, (double) stops[i].offset, r, g, b, a);392}393394cairo_set_source (cr, pattern);395cairo_paint (cr);396397cairo_pattern_destroy (pattern);398399if (stops != stops_)400hb_free (stops);401}402403typedef struct {404float x, y;405} hb_cairo_point_t;406407static inline float408_hb_cairo_interpolate (float f0, float f1, float f)409{410return f0 + f * (f1 - f0);411}412413static inline void414_hb_cairo_premultiply (hb_cairo_color_t *c)415{416c->r *= c->a;417c->g *= c->a;418c->b *= c->a;419}420421static inline void422_hb_cairo_unpremultiply (hb_cairo_color_t *c)423{424if (c->a != 0.f)425{426c->r /= c->a;427c->g /= c->a;428c->b /= c->a;429}430}431432static void433_hb_cairo_interpolate_colors (hb_cairo_color_t *c0, hb_cairo_color_t *c1, float k, hb_cairo_color_t *c)434{435// According to the COLR specification, gradients436// should be interpolated in premultiplied form437_hb_cairo_premultiply (c0);438_hb_cairo_premultiply (c1);439c->r = c0->r + k * (c1->r - c0->r);440c->g = c0->g + k * (c1->g - c0->g);441c->b = c0->b + k * (c1->b - c0->b);442c->a = c0->a + k * (c1->a - c0->a);443_hb_cairo_unpremultiply (c);444}445446static inline float447_hb_cairo_dot (hb_cairo_point_t p, hb_cairo_point_t q)448{449return p.x * q.x + p.y * q.y;450}451452static inline hb_cairo_point_t453_hb_cairo_normalize (hb_cairo_point_t p)454{455float len = sqrtf (_hb_cairo_dot (p, p));456457return hb_cairo_point_t { p.x / len, p.y / len };458}459460static inline hb_cairo_point_t461_hb_cairo_sum (hb_cairo_point_t p, hb_cairo_point_t q)462{463return hb_cairo_point_t { p.x + q.x, p.y + q.y };464}465466static inline hb_cairo_point_t467_hb_cairo_difference (hb_cairo_point_t p, hb_cairo_point_t q)468{469return hb_cairo_point_t { p.x - q.x, p.y - q.y };470}471472static inline hb_cairo_point_t473_hb_cairo_scale (hb_cairo_point_t p, float f)474{475return hb_cairo_point_t { p.x * f, p.y * f };476}477478typedef struct {479hb_cairo_point_t center, p0, c0, c1, p1;480hb_cairo_color_t color0, color1;481} hb_cairo_patch_t;482483static void484_hb_cairo_add_patch (cairo_pattern_t *pattern, hb_cairo_point_t *center, hb_cairo_patch_t *p)485{486cairo_mesh_pattern_begin_patch (pattern);487cairo_mesh_pattern_move_to (pattern, (double) center->x, (double) center->y);488cairo_mesh_pattern_line_to (pattern, (double) p->p0.x, (double) p->p0.y);489cairo_mesh_pattern_curve_to (pattern,490(double) p->c0.x, (double) p->c0.y,491(double) p->c1.x, (double) p->c1.y,492(double) p->p1.x, (double) p->p1.y);493cairo_mesh_pattern_line_to (pattern, (double) center->x, (double) center->y);494cairo_mesh_pattern_set_corner_color_rgba (pattern, 0,495(double) p->color0.r,496(double) p->color0.g,497(double) p->color0.b,498(double) p->color0.a);499cairo_mesh_pattern_set_corner_color_rgba (pattern, 1,500(double) p->color0.r,501(double) p->color0.g,502(double) p->color0.b,503(double) p->color0.a);504cairo_mesh_pattern_set_corner_color_rgba (pattern, 2,505(double) p->color1.r,506(double) p->color1.g,507(double) p->color1.b,508(double) p->color1.a);509cairo_mesh_pattern_set_corner_color_rgba (pattern, 3,510(double) p->color1.r,511(double) p->color1.g,512(double) p->color1.b,513(double) p->color1.a);514cairo_mesh_pattern_end_patch (pattern);515}516517#define MAX_ANGLE (HB_PI / 8.f)518519static void520_hb_cairo_add_sweep_gradient_patches1 (float cx, float cy, float radius,521float a0, hb_cairo_color_t *c0,522float a1, hb_cairo_color_t *c1,523cairo_pattern_t *pattern)524{525hb_cairo_point_t center = hb_cairo_point_t { cx, cy };526int num_splits;527hb_cairo_point_t p0;528hb_cairo_color_t color0, color1;529530num_splits = ceilf (fabsf (a1 - a0) / MAX_ANGLE);531p0 = hb_cairo_point_t { cosf (a0), sinf (a0) };532color0 = *c0;533534for (int a = 0; a < num_splits; a++)535{536float k = (a + 1.) / num_splits;537float angle1;538hb_cairo_point_t p1;539hb_cairo_point_t A, U;540hb_cairo_point_t C0, C1;541hb_cairo_patch_t patch;542543angle1 = _hb_cairo_interpolate (a0, a1, k);544_hb_cairo_interpolate_colors (c0, c1, k, &color1);545546patch.color0 = color0;547patch.color1 = color1;548549p1 = hb_cairo_point_t { cosf (angle1), sinf (angle1) };550patch.p0 = _hb_cairo_sum (center, _hb_cairo_scale (p0, radius));551patch.p1 = _hb_cairo_sum (center, _hb_cairo_scale (p1, radius));552553A = _hb_cairo_normalize (_hb_cairo_sum (p0, p1));554U = hb_cairo_point_t { -A.y, A.x };555C0 = _hb_cairo_sum (A, _hb_cairo_scale (U, _hb_cairo_dot (_hb_cairo_difference (p0, A), p0) / _hb_cairo_dot (U, p0)));556C1 = _hb_cairo_sum (A, _hb_cairo_scale (U, _hb_cairo_dot (_hb_cairo_difference (p1, A), p1) / _hb_cairo_dot (U, p1)));557558patch.c0 = _hb_cairo_sum (center, _hb_cairo_scale (_hb_cairo_sum (C0, _hb_cairo_scale (_hb_cairo_difference (C0, p0), 0.33333f)), radius));559patch.c1 = _hb_cairo_sum (center, _hb_cairo_scale (_hb_cairo_sum (C1, _hb_cairo_scale (_hb_cairo_difference (C1, p1), 0.33333f)), radius));560561_hb_cairo_add_patch (pattern, ¢er, &patch);562563p0 = p1;564color0 = color1;565}566}567568static void569_hb_cairo_add_sweep_gradient_patches (hb_color_stop_t *stops,570unsigned int n_stops,571cairo_extend_t extend,572float cx, float cy,573float radius,574float start_angle,575float end_angle,576cairo_pattern_t *pattern)577{578float angles_[PREALLOCATED_COLOR_STOPS];579float *angles = angles_;580hb_cairo_color_t colors_[PREALLOCATED_COLOR_STOPS];581hb_cairo_color_t *colors = colors_;582hb_cairo_color_t color0, color1;583584if (start_angle == end_angle)585{586if (extend == CAIRO_EXTEND_PAD)587{588hb_cairo_color_t c;589if (start_angle > 0)590{591c.r = hb_color_get_red (stops[0].color) / 255.;592c.g = hb_color_get_green (stops[0].color) / 255.;593c.b = hb_color_get_blue (stops[0].color) / 255.;594c.a = hb_color_get_alpha (stops[0].color) / 255.;595_hb_cairo_add_sweep_gradient_patches1 (cx, cy, radius,5960., &c,597start_angle, &c,598pattern);599}600if (end_angle < HB_2_PI)601{602c.r = hb_color_get_red (stops[n_stops - 1].color) / 255.;603c.g = hb_color_get_green (stops[n_stops - 1].color) / 255.;604c.b = hb_color_get_blue (stops[n_stops - 1].color) / 255.;605c.a = hb_color_get_alpha (stops[n_stops - 1].color) / 255.;606_hb_cairo_add_sweep_gradient_patches1 (cx, cy, radius,607end_angle, &c,608HB_2_PI, &c,609pattern);610}611}612return;613}614615assert (start_angle != end_angle);616617/* handle directions */618if (end_angle < start_angle)619{620hb_swap (start_angle, end_angle);621622for (unsigned i = 0; i < n_stops - 1 - i; i++)623hb_swap (stops[i], stops[n_stops - 1 - i]);624for (unsigned i = 0; i < n_stops; i++)625stops[i].offset = 1 - stops[i].offset;626}627628if (n_stops > PREALLOCATED_COLOR_STOPS)629{630angles = (float *) hb_malloc (sizeof (float) * n_stops);631colors = (hb_cairo_color_t *) hb_malloc (sizeof (hb_cairo_color_t) * n_stops);632if (unlikely (!angles || !colors))633{634hb_free (angles);635hb_free (colors);636return;637}638}639640for (unsigned i = 0; i < n_stops; i++)641{642angles[i] = start_angle + stops[i].offset * (end_angle - start_angle);643colors[i].r = hb_color_get_red (stops[i].color) / 255.;644colors[i].g = hb_color_get_green (stops[i].color) / 255.;645colors[i].b = hb_color_get_blue (stops[i].color) / 255.;646colors[i].a = hb_color_get_alpha (stops[i].color) / 255.;647}648649if (extend == CAIRO_EXTEND_PAD)650{651unsigned pos;652653color0 = colors[0];654for (pos = 0; pos < n_stops; pos++)655{656if (angles[pos] >= 0)657{658if (pos > 0)659{660float k = (0 - angles[pos - 1]) / (angles[pos] - angles[pos - 1]);661_hb_cairo_interpolate_colors (&colors[pos-1], &colors[pos], k, &color0);662}663break;664}665}666if (pos == n_stops)667{668/* everything is below 0 */669color0 = colors[n_stops-1];670_hb_cairo_add_sweep_gradient_patches1 (cx, cy, radius,6710., &color0,672HB_2_PI, &color0,673pattern);674goto done;675}676677_hb_cairo_add_sweep_gradient_patches1 (cx, cy, radius,6780., &color0,679angles[pos], &colors[pos],680pattern);681682for (pos++; pos < n_stops; pos++)683{684if (angles[pos] <= HB_2_PI)685{686_hb_cairo_add_sweep_gradient_patches1 (cx, cy, radius,687angles[pos - 1], &colors[pos-1],688angles[pos], &colors[pos],689pattern);690}691else692{693float k = (HB_2_PI - angles[pos - 1]) / (angles[pos] - angles[pos - 1]);694_hb_cairo_interpolate_colors (&colors[pos - 1], &colors[pos], k, &color1);695_hb_cairo_add_sweep_gradient_patches1 (cx, cy, radius,696angles[pos - 1], &colors[pos - 1],697HB_2_PI, &color1,698pattern);699break;700}701}702703if (pos == n_stops)704{705/* everything is below 2*M_PI */706color0 = colors[n_stops - 1];707_hb_cairo_add_sweep_gradient_patches1 (cx, cy, radius,708angles[n_stops - 1], &color0,709HB_2_PI, &color0,710pattern);711goto done;712}713}714else715{716int k;717float span;718719span = angles[n_stops - 1] - angles[0];720if (!span)721goto done;722723k = 0;724if (angles[0] >= 0)725{726float ss = angles[0];727while (ss > 0)728{729if (span > 0)730{731ss -= span;732k--;733}734else735{736ss += span;737k++;738}739}740}741else if (angles[0] < 0)742{743float ee = angles[n_stops - 1];744while (ee < 0)745{746if (span > 0)747{748ee += span;749k++;750}751else752{753ee -= span;754k--;755}756}757}758759//assert (angles[0] + k * span <= 0 && 0 < angles[n_stops - 1] + k * span);760span = fabsf (span);761762for (signed l = k; l < 1000; l++)763{764for (unsigned i = 1; i < n_stops; i++)765{766float a0, a1;767hb_cairo_color_t *c0, *c1;768769if ((l % 2 != 0) && (extend == CAIRO_EXTEND_REFLECT))770{771a0 = angles[0] + angles[n_stops - 1] - angles[n_stops - 1 - (i-1)] + l * span;772a1 = angles[0] + angles[n_stops - 1] - angles[n_stops - 1 - i] + l * span;773c0 = &colors[n_stops - 1 - (i - 1)];774c1 = &colors[n_stops - 1 - i];775}776else777{778a0 = angles[i-1] + l * span;779a1 = angles[i] + l * span;780c0 = &colors[i-1];781c1 = &colors[i];782}783784if (a1 < 0)785continue;786if (a0 < 0)787{788hb_cairo_color_t color;789float f = (0 - a0)/(a1 - a0);790_hb_cairo_interpolate_colors (c0, c1, f, &color);791_hb_cairo_add_sweep_gradient_patches1 (cx, cy, radius,7920, &color,793a1, c1,794pattern);795}796else if (a1 >= HB_2_PI)797{798hb_cairo_color_t color;799float f = (HB_2_PI - a0)/(a1 - a0);800_hb_cairo_interpolate_colors (c0, c1, f, &color);801_hb_cairo_add_sweep_gradient_patches1 (cx, cy, radius,802a0, c0,803HB_2_PI, &color,804pattern);805goto done;806}807else808{809_hb_cairo_add_sweep_gradient_patches1 (cx, cy, radius,810a0, c0,811a1, c1,812pattern);813}814}815}816}817818done:819820if (angles != angles_)821hb_free (angles);822if (colors != colors_)823hb_free (colors);824}825826void827_hb_cairo_paint_sweep_gradient (hb_cairo_context_t *c,828hb_color_line_t *color_line,829float cx, float cy,830float start_angle,831float end_angle)832{833cairo_t *cr = c->cr;834835unsigned int len = PREALLOCATED_COLOR_STOPS;836hb_color_stop_t stops_[PREALLOCATED_COLOR_STOPS];837hb_color_stop_t *stops = stops_;838cairo_extend_t extend;839double x1, y1, x2, y2;840float max_x, max_y, radius;841cairo_pattern_t *pattern;842843if (unlikely (!_hb_cairo_get_color_stops (c, color_line, &len, &stops)))844return;845846hb_qsort (stops, len, sizeof (hb_color_stop_t), _hb_cairo_cmp_color_stop);847848cairo_clip_extents (cr, &x1, &y1, &x2, &y2);849max_x = (float) hb_max ((x1 - (double) cx) * (x1 - (double) cx), (x2 - (double) cx) * (x2 - (double) cx));850max_y = (float) hb_max ((y1 - (double) cy) * (y1 - (double) cy), (y2 - (double) cy) * (y2 - (double) cy));851radius = sqrtf (max_x + max_y);852853extend = hb_cairo_extend (hb_color_line_get_extend (color_line));854pattern = cairo_pattern_create_mesh ();855856_hb_cairo_add_sweep_gradient_patches (stops, len, extend, cx, cy,857radius, start_angle, end_angle, pattern);858859cairo_set_source (cr, pattern);860cairo_paint (cr);861862cairo_pattern_destroy (pattern);863864if (stops != stops_)865hb_free (stops);866}867868#endif869870871