Bug 1785744 [wpt PR 35504] - Recalc style for elements where :toggle() pseudo-class...
[gecko.git] / gfx / cairo / disable-subpixel-antialiasing.patch
blob7a0eb3a06c7f0d3fb827c301a6544aaf952c0ce8
1 # HG changeset patch
2 # User Robert O'Callahan <robert@ocallahan.org>
3 # Date 1294019288 -46800
4 # Node ID 8857392e37aea7475ed6d8ee4b45023e1233bcec
5 # Parent c53f60831c43cca397dfed8adf8d350aeec7d3ca
6 Bug 363861. Part 2: Introduce cairo_surface_get/set_subpixel_antialiasing. r=jrmuizel,sr=vlad,a=blocking
8 diff --git a/gfx/cairo/cairo/src/cairo-quartz-surface.c b/gfx/cairo/cairo/src/cairo-quartz-surface.c
9 --- a/gfx/cairo/cairo/src/cairo-quartz-surface.c
10 +++ b/gfx/cairo/cairo/src/cairo-quartz-surface.c
11 @@ -2473,16 +2473,17 @@ _cairo_quartz_surface_show_glyphs (void
12 cairo_int_status_t rv = CAIRO_STATUS_SUCCESS;
13 cairo_quartz_drawing_state_t state;
14 float xprev, yprev;
15 int i;
16 CGFontRef cgfref = NULL;
18 cairo_bool_t isClipping = FALSE;
19 cairo_bool_t didForceFontSmoothing = FALSE;
20 + cairo_antialias_t effective_antialiasing;
22 if (IS_EMPTY(surface))
23 return CAIRO_STATUS_SUCCESS;
25 if (num_glyphs <= 0)
26 return CAIRO_STATUS_SUCCESS;
28 if (cairo_scaled_font_get_type (scaled_font) != CAIRO_FONT_TYPE_QUARTZ)
29 @@ -2514,16 +2515,22 @@ _cairo_quartz_surface_show_glyphs (void
30 goto BAIL;
33 /* this doesn't addref */
34 cgfref = _cairo_quartz_scaled_font_get_cg_font_ref (scaled_font);
35 CGContextSetFont (state.context, cgfref);
36 CGContextSetFontSize (state.context, 1.0);
38 + effective_antialiasing = scaled_font->options.antialias;
39 + if (effective_antialiasing == CAIRO_ANTIALIAS_SUBPIXEL &&
40 + !surface->base.permit_subpixel_antialiasing) {
41 + effective_antialiasing = CAIRO_ANTIALIAS_GRAY;
42 + }
44 switch (scaled_font->options.antialias) {
45 case CAIRO_ANTIALIAS_SUBPIXEL:
46 CGContextSetShouldAntialias (state.context, TRUE);
47 CGContextSetShouldSmoothFonts (state.context, TRUE);
48 if (CGContextSetAllowsFontSmoothingPtr &&
49 !CGContextGetAllowsFontSmoothingPtr (state.context))
51 didForceFontSmoothing = TRUE;
52 diff --git a/gfx/cairo/cairo/src/cairo-surface-private.h b/gfx/cairo/cairo/src/cairo-surface-private.h
53 --- a/gfx/cairo/cairo/src/cairo-surface-private.h
54 +++ b/gfx/cairo/cairo/src/cairo-surface-private.h
55 @@ -58,16 +58,17 @@ struct _cairo_surface {
57 cairo_reference_count_t ref_count;
58 cairo_status_t status;
59 unsigned int unique_id;
61 unsigned finished : 1;
62 unsigned is_clear : 1;
63 unsigned has_font_options : 1;
64 + unsigned permit_subpixel_antialiasing : 1;
66 cairo_user_data_array_t user_data;
67 cairo_user_data_array_t mime_data;
69 cairo_matrix_t device_transform;
70 cairo_matrix_t device_transform_inverse;
72 /* The actual resolution of the device, in dots per inch. */
73 diff --git a/gfx/cairo/cairo/src/cairo-surface.c b/gfx/cairo/cairo/src/cairo-surface.c
74 --- a/gfx/cairo/cairo/src/cairo-surface.c
75 +++ b/gfx/cairo/cairo/src/cairo-surface.c
76 @@ -49,17 +49,18 @@ const cairo_surface_t name = { \
77 NULL, /* backend */ \
78 CAIRO_SURFACE_TYPE_IMAGE, /* type */ \
79 CAIRO_CONTENT_COLOR, /* content */ \
80 CAIRO_REFERENCE_COUNT_INVALID, /* ref_count */ \
81 status, /* status */ \
82 0, /* unique id */ \
83 FALSE, /* finished */ \
84 TRUE, /* is_clear */ \
85 - FALSE, /* has_font_options */ \
86 + FALSE, /* has_font_options */ \
87 + FALSE, /* permit_subpixel_antialiasing */ \
88 { 0, 0, 0, NULL, }, /* user_data */ \
89 { 0, 0, 0, NULL, }, /* mime_data */ \
90 { 1.0, 0.0, 0.0, 1.0, 0.0, 0.0 }, /* device_transform */ \
91 { 1.0, 0.0, 0.0, 1.0, 0.0, 0.0 }, /* device_transform_inverse */ \
92 0.0, /* x_resolution */ \
93 0.0, /* y_resolution */ \
94 0.0, /* x_fallback_resolution */ \
95 0.0, /* y_fallback_resolution */ \
96 @@ -342,46 +343,48 @@ _cairo_surface_init (cairo_surface_t *
97 surface->content = content;
98 surface->type = backend->type;
100 CAIRO_REFERENCE_COUNT_INIT (&surface->ref_count, 1);
101 surface->status = CAIRO_STATUS_SUCCESS;
102 surface->unique_id = _cairo_surface_allocate_unique_id ();
103 surface->finished = FALSE;
104 surface->is_clear = FALSE;
105 + surface->has_font_options = FALSE;
106 + surface->permit_subpixel_antialiasing = TRUE;
108 _cairo_user_data_array_init (&surface->user_data);
109 _cairo_user_data_array_init (&surface->mime_data);
111 cairo_matrix_init_identity (&surface->device_transform);
112 cairo_matrix_init_identity (&surface->device_transform_inverse);
114 surface->x_resolution = CAIRO_SURFACE_RESOLUTION_DEFAULT;
115 surface->y_resolution = CAIRO_SURFACE_RESOLUTION_DEFAULT;
117 surface->x_fallback_resolution = CAIRO_SURFACE_FALLBACK_RESOLUTION_DEFAULT;
118 surface->y_fallback_resolution = CAIRO_SURFACE_FALLBACK_RESOLUTION_DEFAULT;
120 _cairo_array_init (&surface->snapshots, sizeof (cairo_surface_t *));
121 surface->snapshot_of = NULL;
123 - surface->has_font_options = FALSE;
126 static void
127 _cairo_surface_copy_similar_properties (cairo_surface_t *surface,
128 cairo_surface_t *other)
130 if (other->has_font_options || other->backend != surface->backend) {
131 cairo_font_options_t options;
133 cairo_surface_get_font_options (other, &options);
134 _cairo_surface_set_font_options (surface, &options);
137 + surface->permit_subpixel_antialiasing = other->permit_subpixel_antialiasing;
139 cairo_surface_set_fallback_resolution (surface,
140 other->x_fallback_resolution,
141 other->y_fallback_resolution);
144 cairo_surface_t *
145 _cairo_surface_create_similar_scratch (cairo_surface_t *other,
146 cairo_content_t content,
147 @@ -2482,16 +2485,67 @@ cairo_surface_has_show_text_glyphs (cair
149 if (surface->backend->has_show_text_glyphs)
150 return surface->backend->has_show_text_glyphs (surface);
151 else
152 return surface->backend->show_text_glyphs != NULL;
154 slim_hidden_def (cairo_surface_has_show_text_glyphs);
156 +/**
157 + * cairo_surface_set_subpixel_antialiasing:
158 + * @surface: a #cairo_surface_t
160 + * Sets whether the surface permits subpixel antialiasing. By default,
161 + * surfaces permit subpixel antialiasing.
163 + * Enabling subpixel antialiasing for CONTENT_COLOR_ALPHA surfaces generally
164 + * requires that the pixels in the areas under a subpixel antialiasing
165 + * operation already be opaque.
167 + * Since: 1.12
168 + **/
169 +void
170 +cairo_surface_set_subpixel_antialiasing (cairo_surface_t *surface,
171 + cairo_subpixel_antialiasing_t enabled)
173 + if (surface->status)
174 + return;
176 + if (surface->finished) {
177 + _cairo_surface_set_error (surface, CAIRO_STATUS_SURFACE_FINISHED);
178 + return;
181 + surface->permit_subpixel_antialiasing =
182 + enabled == CAIRO_SUBPIXEL_ANTIALIASING_ENABLED;
184 +slim_hidden_def (cairo_surface_set_subpixel_antialiasing);
186 +/**
187 + * cairo_surface_get_subpixel_antialiasing:
188 + * @surface: a #cairo_surface_t
190 + * Gets whether the surface supports subpixel antialiasing. By default,
191 + * CAIRO_CONTENT_COLOR surfaces support subpixel antialiasing but other
192 + * surfaces do not.
194 + * Since: 1.12
195 + **/
196 +cairo_subpixel_antialiasing_t
197 +cairo_surface_get_subpixel_antialiasing (cairo_surface_t *surface)
199 + if (surface->status)
200 + return CAIRO_SUBPIXEL_ANTIALIASING_DISABLED;
202 + return surface->permit_subpixel_antialiasing ?
203 + CAIRO_SUBPIXEL_ANTIALIASING_ENABLED : CAIRO_SUBPIXEL_ANTIALIASING_DISABLED;
205 +slim_hidden_def (cairo_surface_get_subpixel_antialiasing);
207 /* Note: the backends may modify the contents of the glyph array as long as
208 * they do not return %CAIRO_INT_STATUS_UNSUPPORTED. This makes it possible to
209 * avoid copying the array again and again, and edit it in-place.
210 * Backends are in fact free to use the array as a generic buffer as they
211 * see fit.
213 * For show_glyphs backend method, and NOT for show_text_glyphs method,
214 * when they do return UNSUPPORTED, they may adjust remaining_glyphs to notify
215 diff --git a/gfx/cairo/cairo/src/cairo-win32-font.c b/gfx/cairo/cairo/src/cairo-win32-font.c
216 --- a/gfx/cairo/cairo/src/cairo-win32-font.c
217 +++ b/gfx/cairo/cairo/src/cairo-win32-font.c
218 @@ -1380,16 +1380,17 @@ _cairo_win32_scaled_font_show_glyphs (vo
219 cairo_win32_surface_t *surface = (cairo_win32_surface_t *)generic_surface;
220 cairo_status_t status;
222 if (width == 0 || height == 0)
223 return CAIRO_STATUS_SUCCESS;
225 if (_cairo_surface_is_win32 (generic_surface) &&
226 surface->format == CAIRO_FORMAT_RGB24 &&
227 + (generic_surface->permit_subpixel_antialiasing || scaled_font->quality != CLEARTYPE_QUALITY) &&
228 op == CAIRO_OPERATOR_OVER &&
229 _cairo_pattern_is_opaque_solid (pattern)) {
231 cairo_solid_pattern_t *solid_pattern = (cairo_solid_pattern_t *)pattern;
233 /* When compositing OVER on a GDI-understood surface, with a
234 * solid opaque color, we can just call ExtTextOut directly.
236 @@ -1411,16 +1412,18 @@ _cairo_win32_scaled_font_show_glyphs (vo
237 * surface by drawing the the glyphs onto a DIB, black-on-white then
238 * inverting. GDI outputs gamma-corrected images so inverted black-on-white
239 * is very different from white-on-black. We favor the more common
240 * case where the final output is dark-on-light.
242 cairo_win32_surface_t *tmp_surface;
243 cairo_surface_t *mask_surface;
244 cairo_surface_pattern_t mask;
245 + cairo_bool_t use_subpixel_antialiasing =
246 + scaled_font->quality == CLEARTYPE_QUALITY && generic_surface->permit_subpixel_antialiasing;
247 RECT r;
249 tmp_surface = (cairo_win32_surface_t *)cairo_win32_surface_create_with_dib (CAIRO_FORMAT_ARGB32, width, height);
250 if (tmp_surface->base.status)
251 return tmp_surface->base.status;
253 r.left = 0;
254 r.top = 0;
255 @@ -1432,17 +1435,17 @@ _cairo_win32_scaled_font_show_glyphs (vo
256 scaled_font, RGB (0, 0, 0),
257 dest_x, dest_y,
258 glyphs, num_glyphs);
259 if (status) {
260 cairo_surface_destroy (&tmp_surface->base);
261 return status;
264 - if (scaled_font->quality == CLEARTYPE_QUALITY) {
265 + if (use_subpixel_antialiasing) {
266 /* For ClearType, we need a 4-channel mask. If we are compositing on
267 * a surface with alpha, we need to compute the alpha channel of
268 * the mask (we just copy the green channel). But for a destination
269 * surface without alpha the alpha channel of the mask is ignored
272 if (surface->format != CAIRO_FORMAT_RGB24)
273 _compute_argb32_mask_alpha (tmp_surface);
274 @@ -1460,17 +1463,17 @@ _cairo_win32_scaled_font_show_glyphs (vo
276 /* For op == OVER, no-cleartype, a possible optimization here is to
277 * draw onto an intermediate ARGB32 surface and alpha-blend that with the
278 * destination
280 _cairo_pattern_init_for_surface (&mask, mask_surface);
281 cairo_surface_destroy (mask_surface);
283 - if (scaled_font->quality == CLEARTYPE_QUALITY)
284 + if (use_subpixel_antialiasing)
285 mask.base.has_component_alpha = TRUE;
287 status = _cairo_surface_composite (op, pattern,
288 &mask.base,
289 &surface->base,
290 source_x, source_y,
291 0, 0,
292 dest_x, dest_y,
293 diff --git a/gfx/cairo/cairo/src/cairo-xlib-surface.c b/gfx/cairo/cairo/src/cairo-xlib-surface.c
294 --- a/gfx/cairo/cairo/src/cairo-xlib-surface.c
295 +++ b/gfx/cairo/cairo/src/cairo-xlib-surface.c
296 @@ -3570,16 +3570,17 @@ typedef struct _cairo_xlib_font_glyphset
297 GlyphSet glyphset;
298 cairo_format_t format;
299 XRenderPictFormat *xrender_format;
300 cairo_xlib_font_glyphset_free_glyphs_t *pending_free_glyphs;
301 } cairo_xlib_font_glyphset_info_t;
303 typedef struct _cairo_xlib_surface_font_private {
304 cairo_scaled_font_t *scaled_font;
305 + cairo_scaled_font_t *grayscale_font;
306 cairo_xlib_hook_t close_display_hook;
307 cairo_xlib_display_t *display;
308 cairo_xlib_font_glyphset_info_t glyphset_info[NUM_GLYPHSETS];
309 } cairo_xlib_surface_font_private_t;
311 /* callback from CloseDisplay */
312 static void
313 _cairo_xlib_surface_remove_scaled_font (cairo_xlib_display_t *display,
314 @@ -3599,16 +3600,20 @@ _cairo_xlib_surface_remove_scaled_font (
316 _cairo_scaled_font_reset_cache (scaled_font);
317 CAIRO_MUTEX_UNLOCK (scaled_font->mutex);
319 if (font_private != NULL) {
320 Display *dpy;
321 int i;
323 + if (font_private->grayscale_font) {
324 + cairo_scaled_font_destroy (font_private->grayscale_font);
327 dpy = _cairo_xlib_display_get_dpy (display);
328 for (i = 0; i < NUM_GLYPHSETS; i++) {
329 cairo_xlib_font_glyphset_info_t *glyphset_info;
331 glyphset_info = &font_private->glyphset_info[i];
332 if (glyphset_info->glyphset)
333 XRenderFreeGlyphSet (dpy, glyphset_info->glyphset);
335 @@ -3629,16 +3634,17 @@ _cairo_xlib_surface_font_init (Display
336 cairo_status_t status;
337 int i;
339 font_private = malloc (sizeof (cairo_xlib_surface_font_private_t));
340 if (unlikely (font_private == NULL))
341 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
343 font_private->scaled_font = scaled_font;
344 + font_private->grayscale_font = NULL;
345 status = _cairo_xlib_display_get (dpy, &font_private->display);
346 if (unlikely (status)) {
347 free (font_private);
348 return status;
351 /* initialize and hook into the CloseDisplay callback */
352 font_private->close_display_hook.func =
353 @@ -3671,16 +3677,20 @@ _cairo_xlib_surface_scaled_font_fini (ca
355 cairo_xlib_surface_font_private_t *font_private;
357 font_private = scaled_font->surface_private;
358 if (font_private != NULL) {
359 cairo_xlib_display_t *display;
360 int i;
362 + if (font_private->grayscale_font) {
363 + cairo_scaled_font_destroy (font_private->grayscale_font);
366 display = font_private->display;
367 _cairo_xlib_remove_close_display_hook (display,
368 &font_private->close_display_hook);
370 for (i = 0; i < NUM_GLYPHSETS; i++) {
371 cairo_xlib_font_glyphset_info_t *glyphset_info;
373 glyphset_info = &font_private->glyphset_info[i];
374 @@ -4417,16 +4427,62 @@ _cairo_xlib_surface_owns_font (cairo_xli
375 (font_private != NULL && font_private->display != dst->display))
377 return FALSE;
380 return TRUE;
383 +/* Gets a grayscale version of scaled_font. The grayscale version is cached
384 + * in our surface_private data.
385 + */
386 +static cairo_scaled_font_t *
387 +_cairo_xlib_get_grayscale_font (cairo_xlib_surface_t *dst,
388 + cairo_scaled_font_t *scaled_font)
390 + cairo_xlib_surface_font_private_t *font_private = scaled_font->surface_private;
391 + cairo_bool_t needs_font;
393 + if (font_private == NULL) {
394 + cairo_status_t status = _cairo_xlib_surface_font_init (dst->dpy, scaled_font);
395 + if (unlikely (status))
396 + return _cairo_scaled_font_create_in_error (status);
397 + font_private = scaled_font->surface_private;
400 + CAIRO_MUTEX_LOCK (scaled_font->mutex);
401 + needs_font = !font_private->grayscale_font;
402 + CAIRO_MUTEX_UNLOCK (scaled_font->mutex);
404 + if (needs_font) {
405 + cairo_font_options_t options;
406 + cairo_scaled_font_t *new_font;
408 + options = scaled_font->options;
409 + options.antialias = CAIRO_ANTIALIAS_GRAY;
410 + new_font = cairo_scaled_font_create (scaled_font->font_face,
411 + &scaled_font->font_matrix,
412 + &scaled_font->ctm, &options);
414 + CAIRO_MUTEX_LOCK (scaled_font->mutex);
415 + if (!font_private->grayscale_font) {
416 + font_private->grayscale_font = new_font;
417 + new_font = NULL;
419 + CAIRO_MUTEX_UNLOCK (scaled_font->mutex);
421 + if (new_font) {
422 + cairo_scaled_font_destroy (new_font);
426 + return font_private->grayscale_font;
429 static cairo_int_status_t
430 _cairo_xlib_surface_show_glyphs (void *abstract_dst,
431 cairo_operator_t op,
432 const cairo_pattern_t *src_pattern,
433 cairo_glyph_t *glyphs,
434 int num_glyphs,
435 cairo_scaled_font_t *scaled_font,
436 cairo_clip_t *clip,
437 @@ -4475,16 +4531,21 @@ _cairo_xlib_surface_show_glyphs (void
439 operation = _categorize_composite_operation (dst, op, src_pattern, TRUE);
440 if (operation == DO_UNSUPPORTED)
441 return UNSUPPORTED ("unsupported op");
443 if (! _cairo_xlib_surface_owns_font (dst, scaled_font))
444 return UNSUPPORTED ("unowned font");
446 + if (!dst->base.permit_subpixel_antialiasing &&
447 + scaled_font->options.antialias == CAIRO_ANTIALIAS_SUBPIXEL) {
448 + scaled_font = _cairo_xlib_get_grayscale_font (dst, scaled_font);
451 X_DEBUG ((dst->dpy, "show_glyphs (dst=%x)", (unsigned int) dst->drawable));
453 if (clip_region != NULL &&
454 cairo_region_num_rectangles (clip_region) == 1)
456 cairo_rectangle_int_t glyph_extents;
457 const cairo_rectangle_int_t *clip_extents;
459 diff --git a/gfx/cairo/cairo/src/cairo.h b/gfx/cairo/cairo/src/cairo.h
460 --- a/gfx/cairo/cairo/src/cairo.h
461 +++ b/gfx/cairo/cairo/src/cairo.h
462 @@ -2101,16 +2101,35 @@ cairo_public void
463 cairo_surface_copy_page (cairo_surface_t *surface);
465 cairo_public void
466 cairo_surface_show_page (cairo_surface_t *surface);
468 cairo_public cairo_bool_t
469 cairo_surface_has_show_text_glyphs (cairo_surface_t *surface);
471 +/**
472 + * _cairo_subpixel_antialiasing_t:
473 + * @CAIRO_SUBPIXEL_ANTIALIASING_ENABLED: subpixel antialiasing is enabled
474 + * for this surface.
475 + * @CAIRO_SUBPIXEL_ANTIALIASING_DISABLED: subpixel antialiasing is disabled
476 + * for this surface.
477 + */
478 +typedef enum _cairo_subpixel_antialiasing_t {
479 + CAIRO_SUBPIXEL_ANTIALIASING_ENABLED,
480 + CAIRO_SUBPIXEL_ANTIALIASING_DISABLED
481 +} cairo_subpixel_antialiasing_t;
483 +cairo_public void
484 +cairo_surface_set_subpixel_antialiasing (cairo_surface_t *surface,
485 + cairo_subpixel_antialiasing_t enabled);
487 +cairo_public cairo_subpixel_antialiasing_t
488 +cairo_surface_get_subpixel_antialiasing (cairo_surface_t *surface);
490 /* Image-surface functions */
493 * cairo_format_t:
494 * @CAIRO_FORMAT_ARGB32: each pixel is a 32-bit quantity, with
495 * alpha in the upper 8 bits, then red, then green, then blue.
496 * The 32-bit quantities are stored native-endian. Pre-multiplied
497 * alpha is used. (That is, 50% transparent red is 0x80800000,
498 diff --git a/gfx/cairo/cairo/src/cairoint.h b/gfx/cairo/cairo/src/cairoint.h
499 --- a/gfx/cairo/cairo/src/cairoint.h
500 +++ b/gfx/cairo/cairo/src/cairoint.h
501 @@ -2750,16 +2750,18 @@ slim_hidden_proto (cairo_surface_destroy
502 slim_hidden_proto (cairo_surface_finish);
503 slim_hidden_proto (cairo_surface_flush);
504 slim_hidden_proto (cairo_surface_get_content);
505 slim_hidden_proto (cairo_surface_get_device_offset);
506 slim_hidden_proto (cairo_surface_get_font_options);
507 slim_hidden_proto (cairo_surface_get_mime_data);
508 slim_hidden_proto (cairo_surface_get_type);
509 slim_hidden_proto (cairo_surface_has_show_text_glyphs);
510 +slim_hidden_proto (cairo_surface_set_subpixel_antialiasing);
511 +slim_hidden_proto (cairo_surface_get_subpixel_antialiasing);
512 slim_hidden_proto (cairo_surface_mark_dirty_rectangle);
513 slim_hidden_proto_no_warn (cairo_surface_reference);
514 slim_hidden_proto (cairo_surface_set_device_offset);
515 slim_hidden_proto (cairo_surface_set_fallback_resolution);
516 slim_hidden_proto (cairo_surface_set_mime_data);
517 slim_hidden_proto (cairo_surface_show_page);
518 slim_hidden_proto (cairo_surface_status);
519 slim_hidden_proto (cairo_text_cluster_allocate);