1 /* cairo - a vector graphics library with display and print output
3 * Copyright © 2005 Red Hat Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it either under the terms of the GNU Lesser General Public
7 * License version 2.1 as published by the Free Software Foundation
8 * (the "LGPL") or, at your option, under the terms of the Mozilla
9 * Public License Version 1.1 (the "MPL"). If you do not alter this
10 * notice, a recipient may use your version of this file under either
11 * the MPL or the LGPL.
13 * You should have received a copy of the LGPL along with this library
14 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
16 * You should have received a copy of the MPL along with this library
17 * in the file COPYING-MPL-1.1
19 * The contents of this file are subject to the Mozilla Public License
20 * Version 1.1 (the "License"); you may not use this file except in
21 * compliance with the License. You may obtain a copy of the License at
22 * http://www.mozilla.org/MPL/
24 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
25 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
26 * the specific language governing rights and limitations.
28 * The Original Code is the cairo graphics library.
30 * The Initial Developer of the Original Code is University of Southern
34 * Owen Taylor <otaylor@redhat.com>
38 #include "cairo-error-private.h"
41 * SECTION:cairo-font-options
42 * @Title: cairo_font_options_t
43 * @Short_Description: How a font should be rendered
44 * @See_Also: #cairo_scaled_font_t
46 * The font options specify how fonts should be rendered. Most of the
47 * time the font options implied by a surface are just right and do not
48 * need any changes, but for pixel-based targets tweaking font options
49 * may result in superior output on a particular display.
52 static const cairo_font_options_t _cairo_font_options_nil = {
53 CAIRO_ANTIALIAS_DEFAULT,
54 CAIRO_SUBPIXEL_ORDER_DEFAULT,
55 CAIRO_LCD_FILTER_DEFAULT,
56 CAIRO_HINT_STYLE_DEFAULT,
57 CAIRO_HINT_METRICS_DEFAULT,
58 CAIRO_ROUND_GLYPH_POS_DEFAULT
62 * _cairo_font_options_init_default:
63 * @options: a #cairo_font_options_t
65 * Initializes all fields of the font options object to default values.
68 _cairo_font_options_init_default (cairo_font_options_t *options)
70 options->antialias = CAIRO_ANTIALIAS_DEFAULT;
71 options->subpixel_order = CAIRO_SUBPIXEL_ORDER_DEFAULT;
72 options->lcd_filter = CAIRO_LCD_FILTER_DEFAULT;
73 options->hint_style = CAIRO_HINT_STYLE_DEFAULT;
74 options->hint_metrics = CAIRO_HINT_METRICS_DEFAULT;
75 options->round_glyph_positions = CAIRO_ROUND_GLYPH_POS_DEFAULT;
76 options->color = CAIRO_FONT_COLOR_DEFAULT;
80 _cairo_font_options_init_copy (cairo_font_options_t *options,
81 const cairo_font_options_t *other)
83 options->antialias = other->antialias;
84 options->subpixel_order = other->subpixel_order;
85 options->lcd_filter = other->lcd_filter;
86 options->hint_style = other->hint_style;
87 options->hint_metrics = other->hint_metrics;
88 options->round_glyph_positions = other->round_glyph_positions;
89 options->color = other->color;
93 * cairo_font_options_create:
95 * Allocates a new font options object with all options initialized
98 * Return value: a newly allocated #cairo_font_options_t. Free with
99 * cairo_font_options_destroy(). This function always returns a
100 * valid pointer; if memory cannot be allocated, then a special
101 * error object is returned where all operations on the object do nothing.
102 * You can check for this with cairo_font_options_status().
106 cairo_font_options_t *
107 cairo_font_options_create (void)
109 cairo_font_options_t *options;
111 options = _cairo_malloc (sizeof (cairo_font_options_t));
113 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
114 return (cairo_font_options_t *) &_cairo_font_options_nil;
117 _cairo_font_options_init_default (options);
123 * cairo_font_options_copy:
124 * @original: a #cairo_font_options_t
126 * Allocates a new font options object copying the option values from
129 * Return value: a newly allocated #cairo_font_options_t. Free with
130 * cairo_font_options_destroy(). This function always returns a
131 * valid pointer; if memory cannot be allocated, then a special
132 * error object is returned where all operations on the object do nothing.
133 * You can check for this with cairo_font_options_status().
137 cairo_font_options_t *
138 cairo_font_options_copy (const cairo_font_options_t *original)
140 cairo_font_options_t *options;
142 if (cairo_font_options_status ((cairo_font_options_t *) original))
143 return (cairo_font_options_t *) &_cairo_font_options_nil;
145 options = _cairo_malloc (sizeof (cairo_font_options_t));
147 _cairo_error_throw (CAIRO_STATUS_NO_MEMORY);
148 return (cairo_font_options_t *) &_cairo_font_options_nil;
151 _cairo_font_options_init_copy (options, original);
157 * cairo_font_options_destroy:
158 * @options: a #cairo_font_options_t
160 * Destroys a #cairo_font_options_t object created with
161 * cairo_font_options_create() or cairo_font_options_copy().
166 cairo_font_options_destroy (cairo_font_options_t *options)
168 if (cairo_font_options_status (options))
175 * cairo_font_options_status:
176 * @options: a #cairo_font_options_t
178 * Checks whether an error has previously occurred for this
179 * font options object
181 * Return value: %CAIRO_STATUS_SUCCESS or %CAIRO_STATUS_NO_MEMORY
186 cairo_font_options_status (cairo_font_options_t *options)
189 return CAIRO_STATUS_NULL_POINTER;
190 else if (options == (cairo_font_options_t *) &_cairo_font_options_nil)
191 return CAIRO_STATUS_NO_MEMORY;
193 return CAIRO_STATUS_SUCCESS;
195 slim_hidden_def (cairo_font_options_status);
198 * cairo_font_options_merge:
199 * @options: a #cairo_font_options_t
200 * @other: another #cairo_font_options_t
202 * Merges non-default options from @other into @options, replacing
203 * existing values. This operation can be thought of as somewhat
204 * similar to compositing @other onto @options with the operation
205 * of %CAIRO_OPERATOR_OVER.
210 cairo_font_options_merge (cairo_font_options_t *options,
211 const cairo_font_options_t *other)
213 if (cairo_font_options_status (options))
216 if (cairo_font_options_status ((cairo_font_options_t *) other))
219 if (other->antialias != CAIRO_ANTIALIAS_DEFAULT)
220 options->antialias = other->antialias;
221 if (other->subpixel_order != CAIRO_SUBPIXEL_ORDER_DEFAULT)
222 options->subpixel_order = other->subpixel_order;
223 if (other->lcd_filter != CAIRO_LCD_FILTER_DEFAULT)
224 options->lcd_filter = other->lcd_filter;
225 if (other->hint_style != CAIRO_HINT_STYLE_DEFAULT)
226 options->hint_style = other->hint_style;
227 if (other->hint_metrics != CAIRO_HINT_METRICS_DEFAULT)
228 options->hint_metrics = other->hint_metrics;
229 if (other->round_glyph_positions != CAIRO_ROUND_GLYPH_POS_DEFAULT)
230 options->round_glyph_positions = other->round_glyph_positions;
231 if (other->color != CAIRO_FONT_COLOR_DEFAULT)
232 options->color = other->color;
234 slim_hidden_def (cairo_font_options_merge);
237 * cairo_font_options_equal:
238 * @options: a #cairo_font_options_t
239 * @other: another #cairo_font_options_t
241 * Compares two font options objects for equality.
243 * Return value: %TRUE if all fields of the two font options objects match.
244 * Note that this function will return %FALSE if either object is in
250 cairo_font_options_equal (const cairo_font_options_t *options,
251 const cairo_font_options_t *other)
253 if (cairo_font_options_status ((cairo_font_options_t *) options))
255 if (cairo_font_options_status ((cairo_font_options_t *) other))
258 if (options == other)
261 return (options->antialias == other->antialias &&
262 options->subpixel_order == other->subpixel_order &&
263 options->lcd_filter == other->lcd_filter &&
264 options->hint_style == other->hint_style &&
265 options->hint_metrics == other->hint_metrics &&
266 options->round_glyph_positions == other->round_glyph_positions &&
267 options->color == other->color);
269 slim_hidden_def (cairo_font_options_equal);
272 * cairo_font_options_hash:
273 * @options: a #cairo_font_options_t
275 * Compute a hash for the font options object; this value will
276 * be useful when storing an object containing a #cairo_font_options_t
279 * Return value: the hash value for the font options object.
280 * The return value can be cast to a 32-bit type if a
281 * 32-bit hash value is needed.
286 cairo_font_options_hash (const cairo_font_options_t *options)
288 if (cairo_font_options_status ((cairo_font_options_t *) options))
289 options = &_cairo_font_options_nil; /* force default values */
291 return ((options->antialias) |
292 (options->subpixel_order << 4) |
293 (options->lcd_filter << 8) |
294 (options->hint_style << 12) |
295 (options->hint_metrics << 16) |
296 (options->color << 20));
298 slim_hidden_def (cairo_font_options_hash);
301 * cairo_font_options_set_antialias:
302 * @options: a #cairo_font_options_t
303 * @antialias: the new antialiasing mode
305 * Sets the antialiasing mode for the font options object. This
306 * specifies the type of antialiasing to do when rendering text.
311 cairo_font_options_set_antialias (cairo_font_options_t *options,
312 cairo_antialias_t antialias)
314 if (cairo_font_options_status (options))
317 options->antialias = antialias;
319 slim_hidden_def (cairo_font_options_set_antialias);
322 * cairo_font_options_get_antialias:
323 * @options: a #cairo_font_options_t
325 * Gets the antialiasing mode for the font options object.
327 * Return value: the antialiasing mode
332 cairo_font_options_get_antialias (const cairo_font_options_t *options)
334 if (cairo_font_options_status ((cairo_font_options_t *) options))
335 return CAIRO_ANTIALIAS_DEFAULT;
337 return options->antialias;
341 * cairo_font_options_set_subpixel_order:
342 * @options: a #cairo_font_options_t
343 * @subpixel_order: the new subpixel order
345 * Sets the subpixel order for the font options object. The subpixel
346 * order specifies the order of color elements within each pixel on
347 * the display device when rendering with an antialiasing mode of
348 * %CAIRO_ANTIALIAS_SUBPIXEL. See the documentation for
349 * #cairo_subpixel_order_t for full details.
354 cairo_font_options_set_subpixel_order (cairo_font_options_t *options,
355 cairo_subpixel_order_t subpixel_order)
357 if (cairo_font_options_status (options))
360 options->subpixel_order = subpixel_order;
362 slim_hidden_def (cairo_font_options_set_subpixel_order);
365 * cairo_font_options_get_subpixel_order:
366 * @options: a #cairo_font_options_t
368 * Gets the subpixel order for the font options object.
369 * See the documentation for #cairo_subpixel_order_t for full details.
371 * Return value: the subpixel order for the font options object
375 cairo_subpixel_order_t
376 cairo_font_options_get_subpixel_order (const cairo_font_options_t *options)
378 if (cairo_font_options_status ((cairo_font_options_t *) options))
379 return CAIRO_SUBPIXEL_ORDER_DEFAULT;
381 return options->subpixel_order;
385 * _cairo_font_options_set_lcd_filter:
386 * @options: a #cairo_font_options_t
387 * @lcd_filter: the new LCD filter
389 * Sets the LCD filter for the font options object. The LCD filter
390 * specifies how pixels are filtered when rendered with an antialiasing
391 * mode of %CAIRO_ANTIALIAS_SUBPIXEL. See the documentation for
392 * #cairo_lcd_filter_t for full details.
395 _cairo_font_options_set_lcd_filter (cairo_font_options_t *options,
396 cairo_lcd_filter_t lcd_filter)
398 if (cairo_font_options_status (options))
401 options->lcd_filter = lcd_filter;
405 * _cairo_font_options_get_lcd_filter:
406 * @options: a #cairo_font_options_t
408 * Gets the LCD filter for the font options object.
409 * See the documentation for #cairo_lcd_filter_t for full details.
411 * Return value: the LCD filter for the font options object
414 _cairo_font_options_get_lcd_filter (const cairo_font_options_t *options)
416 if (cairo_font_options_status ((cairo_font_options_t *) options))
417 return CAIRO_LCD_FILTER_DEFAULT;
419 return options->lcd_filter;
423 * _cairo_font_options_set_round_glyph_positions:
424 * @options: a #cairo_font_options_t
425 * @round: the new rounding value
427 * Sets the rounding options for the font options object. If rounding is set, a
428 * glyph's position will be rounded to integer values.
431 _cairo_font_options_set_round_glyph_positions (cairo_font_options_t *options,
432 cairo_round_glyph_positions_t round)
434 if (cairo_font_options_status (options))
437 options->round_glyph_positions = round;
441 * _cairo_font_options_get_round_glyph_positions:
442 * @options: a #cairo_font_options_t
444 * Gets the glyph position rounding option for the font options object.
446 * Return value: The round glyph posistions flag for the font options object.
448 cairo_round_glyph_positions_t
449 _cairo_font_options_get_round_glyph_positions (const cairo_font_options_t *options)
451 if (cairo_font_options_status ((cairo_font_options_t *) options))
452 return CAIRO_ROUND_GLYPH_POS_DEFAULT;
454 return options->round_glyph_positions;
458 * cairo_font_options_set_hint_style:
459 * @options: a #cairo_font_options_t
460 * @hint_style: the new hint style
462 * Sets the hint style for font outlines for the font options object.
463 * This controls whether to fit font outlines to the pixel grid,
464 * and if so, whether to optimize for fidelity or contrast.
465 * See the documentation for #cairo_hint_style_t for full details.
470 cairo_font_options_set_hint_style (cairo_font_options_t *options,
471 cairo_hint_style_t hint_style)
473 if (cairo_font_options_status (options))
476 options->hint_style = hint_style;
478 slim_hidden_def (cairo_font_options_set_hint_style);
481 * cairo_font_options_get_hint_style:
482 * @options: a #cairo_font_options_t
484 * Gets the hint style for font outlines for the font options object.
485 * See the documentation for #cairo_hint_style_t for full details.
487 * Return value: the hint style for the font options object
492 cairo_font_options_get_hint_style (const cairo_font_options_t *options)
494 if (cairo_font_options_status ((cairo_font_options_t *) options))
495 return CAIRO_HINT_STYLE_DEFAULT;
497 return options->hint_style;
501 * cairo_font_options_set_hint_metrics:
502 * @options: a #cairo_font_options_t
503 * @hint_metrics: the new metrics hinting mode
505 * Sets the metrics hinting mode for the font options object. This
506 * controls whether metrics are quantized to integer values in
508 * See the documentation for #cairo_hint_metrics_t for full details.
513 cairo_font_options_set_hint_metrics (cairo_font_options_t *options,
514 cairo_hint_metrics_t hint_metrics)
516 if (cairo_font_options_status (options))
519 options->hint_metrics = hint_metrics;
521 slim_hidden_def (cairo_font_options_set_hint_metrics);
524 * cairo_font_options_get_hint_metrics:
525 * @options: a #cairo_font_options_t
527 * Gets the metrics hinting mode for the font options object.
528 * See the documentation for #cairo_hint_metrics_t for full details.
530 * Return value: the metrics hinting mode for the font options object
535 cairo_font_options_get_hint_metrics (const cairo_font_options_t *options)
537 if (cairo_font_options_status ((cairo_font_options_t *) options))
538 return CAIRO_HINT_METRICS_DEFAULT;
540 return options->hint_metrics;
544 cairo_font_options_set_font_color (cairo_font_options_t *options,
545 cairo_font_color_t font_color)
547 if (cairo_font_options_status (options))
550 options->color = font_color;
552 slim_hidden_def (cairo_font_options_set_font_color);
555 cairo_font_options_get_font_color (const cairo_font_options_t *options)
557 if (cairo_font_options_status ((cairo_font_options_t *) options))
558 return CAIRO_FONT_COLOR_DEFAULT;
560 return options->color;