utils: Some cairo helper tweaks

This commit is contained in:
Matthias Clasen 2022-12-21 09:49:42 -05:00 committed by Behdad Esfahbod
parent 97224f3b63
commit b1500babaa
3 changed files with 48 additions and 45 deletions

View File

@ -45,6 +45,11 @@
#define MAX(x,y) ((x) > (y) ? (x) : (y)) #define MAX(x,y) ((x) > (y) ? (x) : (y))
#endif #endif
#ifndef FALSE
#define TRUE 1
#define FALSE 0
#endif
#define PREALLOCATED_COLOR_STOPS 16 #define PREALLOCATED_COLOR_STOPS 16
typedef struct { typedef struct {
@ -65,37 +70,35 @@ cairo_extend (hb_paint_extend_t extend)
return CAIRO_EXTEND_PAD; return CAIRO_EXTEND_PAD;
} }
void hb_bool_t
hb_face_get_color (hb_face_t *face, hb_cairo_get_font_color (hb_font_t *font,
unsigned int palette_index, unsigned int palette_index,
unsigned int color_index, unsigned int color_index,
float alpha, float alpha,
float *r, float *g, float *b, float *a) float *r, float *g, float *b, float *a)
{ {
if (color_index == 0xffff) if (color_index != 0xffff)
{
/* foreground color */
*r = *g = *b = 0;
*a = alpha;
}
else
{ {
hb_color_t color; hb_color_t color;
unsigned int clen = 1; unsigned int clen = 1;
hb_face_t *face = hb_font_get_face (font);
if (hb_ot_color_palette_get_colors (face, palette_index, color_index, &clen, &color)) if (hb_ot_color_palette_get_colors (face, palette_index, color_index, &clen, &color))
{ {
*r = hb_color_get_red (color) / 255.f; *r = hb_color_get_red (color) / 255.f;
*g = hb_color_get_green (color) / 255.f; *g = hb_color_get_green (color) / 255.f;
*b = hb_color_get_blue (color) / 255.f; *b = hb_color_get_blue (color) / 255.f;
*a = (hb_color_get_alpha (color) / 255.f) * alpha; *a = (hb_color_get_alpha (color) / 255.f) * alpha;
}
else return TRUE;
{ }
*r = *g = *b = 0;
*a = alpha;
}
} }
/* foreground color */
*r = *g = *b = 0;
*a = alpha;
return FALSE;
} }
typedef struct typedef struct
@ -240,7 +243,6 @@ hb_cairo_paint_linear_gradient (cairo_t *cr,
float x1, float y1, float x1, float y1,
float x2, float y2) float x2, float y2)
{ {
hb_face_t *face = hb_font_get_face (font);
hb_color_stop_t stops_[PREALLOCATED_COLOR_STOPS]; hb_color_stop_t stops_[PREALLOCATED_COLOR_STOPS];
hb_color_stop_t *stops = stops_; hb_color_stop_t *stops = stops_;
unsigned int len; unsigned int len;
@ -267,7 +269,7 @@ hb_cairo_paint_linear_gradient (cairo_t *cr,
for (unsigned int i = 0; i < len; i++) for (unsigned int i = 0; i < len; i++)
{ {
float r, g, b, a; float r, g, b, a;
hb_face_get_color (face, 0, stops[i].color_index, stops[i].alpha, &r, &g, &b, &a); hb_cairo_get_font_color (font, 0, stops[i].color_index, stops[i].alpha, &r, &g, &b, &a);
cairo_pattern_add_color_stop_rgba (pattern, stops[i].offset, r, g, b, a); cairo_pattern_add_color_stop_rgba (pattern, stops[i].offset, r, g, b, a);
} }
@ -287,7 +289,6 @@ hb_cairo_paint_radial_gradient (cairo_t *cr,
float x0, float y0, float r0, float x0, float y0, float r0,
float x1, float y1, float r1) float x1, float y1, float r1)
{ {
hb_face_t *face = hb_font_get_face (font);
hb_color_stop_t stops_[PREALLOCATED_COLOR_STOPS]; hb_color_stop_t stops_[PREALLOCATED_COLOR_STOPS];
hb_color_stop_t *stops = stops_; hb_color_stop_t *stops = stops_;
unsigned int len; unsigned int len;
@ -316,7 +317,7 @@ hb_cairo_paint_radial_gradient (cairo_t *cr,
for (unsigned int i = 0; i < len; i++) for (unsigned int i = 0; i < len; i++)
{ {
float r, g, b, a; float r, g, b, a;
hb_face_get_color (face, 0, stops[i].color_index, stops[i].alpha, &r, &g, &b, &a); hb_cairo_get_font_color (font, 0, stops[i].color_index, stops[i].alpha, &r, &g, &b, &a);
cairo_pattern_add_color_stop_rgba (pattern, stops[i].offset, r, g, b, a); cairo_pattern_add_color_stop_rgba (pattern, stops[i].offset, r, g, b, a);
} }
@ -481,7 +482,6 @@ add_sweep_gradient_patches (hb_font_t *font,
float end_angle, float end_angle,
cairo_pattern_t *pattern) cairo_pattern_t *pattern)
{ {
hb_face_t *face = hb_font_get_face (font);
float angles_[PREALLOCATED_COLOR_STOPS]; float angles_[PREALLOCATED_COLOR_STOPS];
float *angles = angles_; float *angles = angles_;
color_t colors_[PREALLOCATED_COLOR_STOPS]; color_t colors_[PREALLOCATED_COLOR_STOPS];
@ -495,7 +495,7 @@ add_sweep_gradient_patches (hb_font_t *font,
color_t c; color_t c;
if (start_angle > 0) if (start_angle > 0)
{ {
hb_face_get_color (face, 0, stops[0].color_index, stops[0].alpha, &c.r, &c.g, &c.b, &c.a); hb_cairo_get_font_color (font, 0, stops[0].color_index, stops[0].alpha, &c.r, &c.g, &c.b, &c.a);
add_sweep_gradient_patches1 (cx, cy, radius, add_sweep_gradient_patches1 (cx, cy, radius,
0., &c, 0., &c,
start_angle, &c, start_angle, &c,
@ -503,7 +503,7 @@ add_sweep_gradient_patches (hb_font_t *font,
} }
if (end_angle < 2 * M_PI) if (end_angle < 2 * M_PI)
{ {
hb_face_get_color (face, 0, stops[n_stops-1].color_index, stops[n_stops-1].alpha, &c.r, &c.g, &c.b, &c.a); hb_cairo_get_font_color (font, 0, stops[n_stops-1].color_index, stops[n_stops-1].alpha, &c.r, &c.g, &c.b, &c.a);
add_sweep_gradient_patches1 (cx, cy, radius, add_sweep_gradient_patches1 (cx, cy, radius,
end_angle, &c, end_angle, &c,
2 * M_PI, &c, 2 * M_PI, &c,
@ -539,7 +539,7 @@ add_sweep_gradient_patches (hb_font_t *font,
for (unsigned i = 0; i < n_stops; i++) for (unsigned i = 0; i < n_stops; i++)
{ {
angles[i] = start_angle + stops[i].offset * (end_angle - start_angle); angles[i] = start_angle + stops[i].offset * (end_angle - start_angle);
hb_face_get_color (face, 0, stops[i].color_index, stops[i].alpha, &colors[i].r, &colors[i].g, &colors[i].b, &colors[i].a); hb_cairo_get_font_color (font, 0, stops[i].color_index, stops[i].alpha, &colors[i].r, &colors[i].g, &colors[i].b, &colors[i].a);
} }
if (extend == CAIRO_EXTEND_PAD) if (extend == CAIRO_EXTEND_PAD)

View File

@ -70,11 +70,11 @@ hb_paint_composite_mode_to_cairo (hb_paint_composite_mode_t mode)
return CAIRO_OPERATOR_SOURCE; return CAIRO_OPERATOR_SOURCE;
} }
void hb_face_get_color (hb_face_t *face, hb_bool_t hb_cairo_get_font_color (hb_font_t *font,
unsigned int palette_index, unsigned int palette_index,
unsigned int color_index, unsigned int color_index,
float alpha, float alpha,
float *r, float *g, float *b, float *a); float *r, float *g, float *b, float *a);
void hb_cairo_paint_glyph_image (cairo_t *cr, void hb_cairo_paint_glyph_image (cairo_t *cr,
hb_font_t *font, hb_font_t *font,

View File

@ -118,7 +118,9 @@ push_transform (hb_paint_funcs_t *funcs,
cairo_matrix_t m; cairo_matrix_t m;
cairo_save (cr); cairo_save (cr);
cairo_matrix_init (&m, xx, yx, xy, yy, dx, dy); cairo_matrix_init (&m, (double)xx, (double)yx,
(double)xy, (double)yy,
(double)dx, (double)dy);
cairo_transform (cr, &m); cairo_transform (cr, &m);
} }
@ -144,7 +146,7 @@ push_clip_glyph (hb_paint_funcs_t *funcs,
cairo_save (cr); cairo_save (cr);
cairo_new_path (cr); cairo_new_path (cr);
hb_font_get_glyph_shape (font, glyph, get_cairo_draw_funcs (), cr); hb_font_draw_glyph (font, glyph, get_cairo_draw_funcs (), cr);
cairo_close_path (cr); cairo_close_path (cr);
cairo_clip (cr); cairo_clip (cr);
} }
@ -159,7 +161,9 @@ push_clip_rectangle (hb_paint_funcs_t *funcs,
cairo_t *cr = (cairo_t *)paint_data; cairo_t *cr = (cairo_t *)paint_data;
cairo_save (cr); cairo_save (cr);
cairo_rectangle (cr, xmin, ymin, xmax - xmin, ymax - ymin); cairo_rectangle (cr,
(double)xmin, (double)ymin,
(double)(xmax - xmin), (double)(ymax - ymin));
cairo_clip (cr); cairo_clip (cr);
} }
@ -211,11 +215,10 @@ paint_color (hb_paint_funcs_t *funcs,
void *user_data) void *user_data)
{ {
cairo_t *cr = (cairo_t *)paint_data; cairo_t *cr = (cairo_t *)paint_data;
hb_face_t *face = hb_font_get_face (font);
float r, g, b, a; float r, g, b, a;
hb_face_get_color (face, 0, color_index, alpha, &r, &g, &b, &a); hb_cairo_get_font_color (font, 0, color_index, alpha, &r, &g, &b, &a);
cairo_set_source_rgba (cr, r, g, b, a); cairo_set_source_rgba (cr, (double)r, (double)g, (double)b, (double)a);
cairo_paint (cr); cairo_paint (cr);
} }
@ -277,7 +280,7 @@ paint_sweep_gradient (hb_paint_funcs_t *funcs,
} }
static hb_paint_funcs_t * static hb_paint_funcs_t *
get_cairo_paint_funcs (void) get_cairo_paint_funcs ()
{ {
static hb_paint_funcs_t *funcs; static hb_paint_funcs_t *funcs;
@ -331,7 +334,7 @@ render_glyph (cairo_scaled_font_t *scaled_font,
hb_font_get_scale (font, &x_scale, &y_scale); hb_font_get_scale (font, &x_scale, &y_scale);
cairo_scale (cr, +1./x_scale, -1./y_scale); cairo_scale (cr, +1./x_scale, -1./y_scale);
hb_font_get_glyph_shape (font, glyph, get_cairo_draw_funcs (), cr); hb_font_draw_glyph (font, glyph, get_cairo_draw_funcs (), cr);
cairo_fill (cr); cairo_fill (cr);
return CAIRO_STATUS_SUCCESS; return CAIRO_STATUS_SUCCESS;