1 /* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
2 /* cairo - a vector graphics library with display and print output
4 * Copyright © 2002 University of Southern California
5 * Copyright © 2005 Red Hat, Inc.
6 * Copyright © 2007 Adrian Johnson
8 * This library is free software; you can redistribute it and/or
9 * modify it either under the terms of the GNU Lesser General Public
10 * License version 2.1 as published by the Free Software Foundation
11 * (the "LGPL") or, at your option, under the terms of the Mozilla
12 * Public License Version 1.1 (the "MPL"). If you do not alter this
13 * notice, a recipient may use your version of this file under either
14 * the MPL or the LGPL.
16 * You should have received a copy of the LGPL along with this library
17 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
19 * You should have received a copy of the MPL along with this library
20 * in the file COPYING-MPL-1.1
22 * The contents of this file are subject to the Mozilla Public License
23 * Version 1.1 (the "License"); you may not use this file except in
24 * compliance with the License. You may obtain a copy of the License at
25 * http://www.mozilla.org/MPL/
27 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
28 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
29 * the specific language governing rights and limitations.
31 * The Original Code is the cairo graphics library.
33 * The Initial Developer of the Original Code is University of Southern
37 * Carl D. Worth <cworth@cworth.org>
38 * Adrian Johnson <ajohnson@redneon.com>
42 #include "cairo-error-private.h"
44 COMPILE_TIME_ASSERT ((int)CAIRO_STATUS_LAST_STATUS < (int)CAIRO_INT_STATUS_UNSUPPORTED);
45 COMPILE_TIME_ASSERT (CAIRO_INT_STATUS_LAST_STATUS <= 127);
48 * SECTION:cairo-status
49 * @Title: Error handling
50 * @Short_Description: Decoding cairo's status
51 * @See_Also: cairo_status(), cairo_surface_status(), cairo_pattern_status(),
52 * cairo_font_face_status(), cairo_scaled_font_status(),
53 * cairo_region_status()
55 * Cairo uses a single status type to represent all kinds of errors. A status
56 * value of %CAIRO_STATUS_SUCCESS represents no error and has an integer value
57 * of zero. All other status values represent an error.
59 * Cairo's error handling is designed to be easy to use and safe. All major
60 * cairo objects <firstterm>retain</firstterm> an error status internally which
61 * can be queried anytime by the users using cairo*_status() calls. In
62 * the mean time, it is safe to call all cairo functions normally even if the
63 * underlying object is in an error status. This means that no error handling
64 * code is required before or after each individual cairo function call.
70 * cairo_status_to_string:
71 * @status: a cairo status
73 * Provides a human-readable description of a #cairo_status_t.
75 * Returns: a string representation of the status
80 cairo_status_to_string (cairo_status_t status)
83 case CAIRO_STATUS_SUCCESS:
84 return "no error has occurred";
85 case CAIRO_STATUS_NO_MEMORY:
86 return "out of memory";
87 case CAIRO_STATUS_INVALID_RESTORE:
88 return "cairo_restore() without matching cairo_save()";
89 case CAIRO_STATUS_INVALID_POP_GROUP:
90 return "no saved group to pop, i.e. cairo_pop_group() without matching cairo_push_group()";
91 case CAIRO_STATUS_NO_CURRENT_POINT:
92 return "no current point defined";
93 case CAIRO_STATUS_INVALID_MATRIX:
94 return "invalid matrix (not invertible)";
95 case CAIRO_STATUS_INVALID_STATUS:
96 return "invalid value for an input cairo_status_t";
97 case CAIRO_STATUS_NULL_POINTER:
98 return "NULL pointer";
99 case CAIRO_STATUS_INVALID_STRING:
100 return "input string not valid UTF-8";
101 case CAIRO_STATUS_INVALID_PATH_DATA:
102 return "input path data not valid";
103 case CAIRO_STATUS_READ_ERROR:
104 return "error while reading from input stream";
105 case CAIRO_STATUS_WRITE_ERROR:
106 return "error while writing to output stream";
107 case CAIRO_STATUS_SURFACE_FINISHED:
108 return "the target surface has been finished";
109 case CAIRO_STATUS_SURFACE_TYPE_MISMATCH:
110 return "the surface type is not appropriate for the operation";
111 case CAIRO_STATUS_PATTERN_TYPE_MISMATCH:
112 return "the pattern type is not appropriate for the operation";
113 case CAIRO_STATUS_INVALID_CONTENT:
114 return "invalid value for an input cairo_content_t";
115 case CAIRO_STATUS_INVALID_FORMAT:
116 return "invalid value for an input cairo_format_t";
117 case CAIRO_STATUS_INVALID_VISUAL:
118 return "invalid value for an input Visual*";
119 case CAIRO_STATUS_FILE_NOT_FOUND:
120 return "file not found";
121 case CAIRO_STATUS_INVALID_DASH:
122 return "invalid value for a dash setting";
123 case CAIRO_STATUS_INVALID_DSC_COMMENT:
124 return "invalid value for a DSC comment";
125 case CAIRO_STATUS_INVALID_INDEX:
126 return "invalid index passed to getter";
127 case CAIRO_STATUS_CLIP_NOT_REPRESENTABLE:
128 return "clip region not representable in desired format";
129 case CAIRO_STATUS_TEMP_FILE_ERROR:
130 return "error creating or writing to a temporary file";
131 case CAIRO_STATUS_INVALID_STRIDE:
132 return "invalid value for stride";
133 case CAIRO_STATUS_FONT_TYPE_MISMATCH:
134 return "the font type is not appropriate for the operation";
135 case CAIRO_STATUS_USER_FONT_IMMUTABLE:
136 return "the user-font is immutable";
137 case CAIRO_STATUS_USER_FONT_ERROR:
138 return "error occurred in a user-font callback function";
139 case CAIRO_STATUS_NEGATIVE_COUNT:
140 return "negative number used where it is not allowed";
141 case CAIRO_STATUS_INVALID_CLUSTERS:
142 return "input clusters do not represent the accompanying text and glyph arrays";
143 case CAIRO_STATUS_INVALID_SLANT:
144 return "invalid value for an input cairo_font_slant_t";
145 case CAIRO_STATUS_INVALID_WEIGHT:
146 return "invalid value for an input cairo_font_weight_t";
147 case CAIRO_STATUS_INVALID_SIZE:
148 return "invalid value (typically too big) for the size of the input (surface, pattern, etc.)";
149 case CAIRO_STATUS_USER_FONT_NOT_IMPLEMENTED:
150 return "user-font method not implemented";
151 case CAIRO_STATUS_DEVICE_TYPE_MISMATCH:
152 return "the device type is not appropriate for the operation";
153 case CAIRO_STATUS_DEVICE_ERROR:
154 return "an operation to the device caused an unspecified error";
155 case CAIRO_STATUS_INVALID_MESH_CONSTRUCTION:
156 return "invalid operation during mesh pattern construction";
157 case CAIRO_STATUS_DEVICE_FINISHED:
158 return "the target device has been finished";
160 case CAIRO_STATUS_LAST_STATUS:
161 return "<unknown error status>";
167 * cairo_glyph_allocate:
168 * @num_glyphs: number of glyphs to allocate
170 * Allocates an array of #cairo_glyph_t's.
171 * This function is only useful in implementations of
172 * #cairo_user_scaled_font_text_to_glyphs_func_t where the user
173 * needs to allocate an array of glyphs that cairo will free.
174 * For all other uses, user can use their own allocation method
177 * This function returns %NULL if @num_glyphs is not positive,
178 * or if out of memory. That means, the %NULL return value
179 * signals out-of-memory only if @num_glyphs was positive.
181 * Returns: the newly allocated array of glyphs that should be
182 * freed using cairo_glyph_free()
187 cairo_glyph_allocate (int num_glyphs)
192 return _cairo_malloc_ab (num_glyphs, sizeof (cairo_glyph_t));
194 slim_hidden_def (cairo_glyph_allocate);
198 * @glyphs: array of glyphs to free, or %NULL
200 * Frees an array of #cairo_glyph_t's allocated using cairo_glyph_allocate().
201 * This function is only useful to free glyph array returned
202 * by cairo_scaled_font_text_to_glyphs() where cairo returns
203 * an array of glyphs that the user will free.
204 * For all other uses, user can use their own allocation method
210 cairo_glyph_free (cairo_glyph_t *glyphs)
214 slim_hidden_def (cairo_glyph_free);
217 * cairo_text_cluster_allocate:
218 * @num_clusters: number of text_clusters to allocate
220 * Allocates an array of #cairo_text_cluster_t's.
221 * This function is only useful in implementations of
222 * #cairo_user_scaled_font_text_to_glyphs_func_t where the user
223 * needs to allocate an array of text clusters that cairo will free.
224 * For all other uses, user can use their own allocation method
227 * This function returns %NULL if @num_clusters is not positive,
228 * or if out of memory. That means, the %NULL return value
229 * signals out-of-memory only if @num_clusters was positive.
231 * Returns: the newly allocated array of text clusters that should be
232 * freed using cairo_text_cluster_free()
236 cairo_text_cluster_t *
237 cairo_text_cluster_allocate (int num_clusters)
239 if (num_clusters <= 0)
242 return _cairo_malloc_ab (num_clusters, sizeof (cairo_text_cluster_t));
244 slim_hidden_def (cairo_text_cluster_allocate);
247 * cairo_text_cluster_free:
248 * @clusters: array of text clusters to free, or %NULL
250 * Frees an array of #cairo_text_cluster's allocated using cairo_text_cluster_allocate().
251 * This function is only useful to free text cluster array returned
252 * by cairo_scaled_font_text_to_glyphs() where cairo returns
253 * an array of text clusters that the user will free.
254 * For all other uses, user can use their own allocation method
260 cairo_text_cluster_free (cairo_text_cluster_t *clusters)
264 slim_hidden_def (cairo_text_cluster_free);
270 * _cairo_validate_text_clusters:
272 * @utf8_len: length of @utf8 in bytes
273 * @glyphs: array of glyphs
274 * @num_glyphs: number of glyphs
275 * @clusters: array of cluster mapping information
276 * @num_clusters: number of clusters in the mapping
277 * @cluster_flags: cluster flags
279 * Check that clusters cover the entire glyphs and utf8 arrays,
280 * and that cluster boundaries are UTF-8 boundaries.
282 * Return value: %CAIRO_STATUS_SUCCESS upon success, or
283 * %CAIRO_STATUS_INVALID_CLUSTERS on error.
284 * The error is either invalid UTF-8 input,
285 * or bad cluster mapping.
288 _cairo_validate_text_clusters (const char *utf8,
290 const cairo_glyph_t *glyphs,
292 const cairo_text_cluster_t *clusters,
294 cairo_text_cluster_flags_t cluster_flags)
296 cairo_status_t status;
297 unsigned int n_bytes = 0;
298 unsigned int n_glyphs = 0;
301 for (i = 0; i < num_clusters; i++) {
302 int cluster_bytes = clusters[i].num_bytes;
303 int cluster_glyphs = clusters[i].num_glyphs;
305 if (cluster_bytes < 0 || cluster_glyphs < 0)
308 /* A cluster should cover at least one character or glyph.
309 * I can't see any use for a 0,0 cluster.
310 * I can't see an immediate use for a zero-text cluster
311 * right now either, but they don't harm.
312 * Zero-glyph clusters on the other hand are useful for
313 * things like U+200C ZERO WIDTH NON-JOINER */
314 if (cluster_bytes == 0 && cluster_glyphs == 0)
317 /* Since n_bytes and n_glyphs are unsigned, but the rest of
318 * values involved are signed, we can detect overflow easily */
319 if (n_bytes+cluster_bytes > (unsigned int)utf8_len || n_glyphs+cluster_glyphs > (unsigned int)num_glyphs)
322 /* Make sure we've got valid UTF-8 for the cluster */
323 status = _cairo_utf8_to_ucs4 (utf8+n_bytes, cluster_bytes, NULL, NULL);
324 if (unlikely (status))
325 return _cairo_error (CAIRO_STATUS_INVALID_CLUSTERS);
327 n_bytes += cluster_bytes ;
328 n_glyphs += cluster_glyphs;
331 if (n_bytes != (unsigned int) utf8_len || n_glyphs != (unsigned int) num_glyphs) {
333 return _cairo_error (CAIRO_STATUS_INVALID_CLUSTERS);
336 return CAIRO_STATUS_SUCCESS;
340 * _cairo_operator_bounded_by_mask:
341 * @op: a #cairo_operator_t
343 * A bounded operator is one where mask pixel
344 * of zero results in no effect on the destination image.
346 * Unbounded operators often require special handling; if you, for
347 * example, draw trapezoids with an unbounded operator, the effect
348 * extends past the bounding box of the trapezoids.
350 * Return value: %TRUE if the operator is bounded by the mask operand
353 _cairo_operator_bounded_by_mask (cairo_operator_t op)
356 case CAIRO_OPERATOR_CLEAR:
357 case CAIRO_OPERATOR_SOURCE:
358 case CAIRO_OPERATOR_OVER:
359 case CAIRO_OPERATOR_ATOP:
360 case CAIRO_OPERATOR_DEST:
361 case CAIRO_OPERATOR_DEST_OVER:
362 case CAIRO_OPERATOR_DEST_OUT:
363 case CAIRO_OPERATOR_XOR:
364 case CAIRO_OPERATOR_ADD:
365 case CAIRO_OPERATOR_SATURATE:
366 case CAIRO_OPERATOR_MULTIPLY:
367 case CAIRO_OPERATOR_SCREEN:
368 case CAIRO_OPERATOR_OVERLAY:
369 case CAIRO_OPERATOR_DARKEN:
370 case CAIRO_OPERATOR_LIGHTEN:
371 case CAIRO_OPERATOR_COLOR_DODGE:
372 case CAIRO_OPERATOR_COLOR_BURN:
373 case CAIRO_OPERATOR_HARD_LIGHT:
374 case CAIRO_OPERATOR_SOFT_LIGHT:
375 case CAIRO_OPERATOR_DIFFERENCE:
376 case CAIRO_OPERATOR_EXCLUSION:
377 case CAIRO_OPERATOR_HSL_HUE:
378 case CAIRO_OPERATOR_HSL_SATURATION:
379 case CAIRO_OPERATOR_HSL_COLOR:
380 case CAIRO_OPERATOR_HSL_LUMINOSITY:
382 case CAIRO_OPERATOR_OUT:
383 case CAIRO_OPERATOR_IN:
384 case CAIRO_OPERATOR_DEST_IN:
385 case CAIRO_OPERATOR_DEST_ATOP:
394 * _cairo_operator_bounded_by_source:
395 * @op: a #cairo_operator_t
397 * A bounded operator is one where source pixels of zero
398 * (in all four components, r, g, b and a) effect no change
399 * in the resulting destination image.
401 * Unbounded operators often require special handling; if you, for
402 * example, copy a surface with the SOURCE operator, the effect
403 * extends past the bounding box of the source surface.
405 * Return value: %TRUE if the operator is bounded by the source operand
408 _cairo_operator_bounded_by_source (cairo_operator_t op)
411 case CAIRO_OPERATOR_OVER:
412 case CAIRO_OPERATOR_ATOP:
413 case CAIRO_OPERATOR_DEST:
414 case CAIRO_OPERATOR_DEST_OVER:
415 case CAIRO_OPERATOR_DEST_OUT:
416 case CAIRO_OPERATOR_XOR:
417 case CAIRO_OPERATOR_ADD:
418 case CAIRO_OPERATOR_SATURATE:
419 case CAIRO_OPERATOR_MULTIPLY:
420 case CAIRO_OPERATOR_SCREEN:
421 case CAIRO_OPERATOR_OVERLAY:
422 case CAIRO_OPERATOR_DARKEN:
423 case CAIRO_OPERATOR_LIGHTEN:
424 case CAIRO_OPERATOR_COLOR_DODGE:
425 case CAIRO_OPERATOR_COLOR_BURN:
426 case CAIRO_OPERATOR_HARD_LIGHT:
427 case CAIRO_OPERATOR_SOFT_LIGHT:
428 case CAIRO_OPERATOR_DIFFERENCE:
429 case CAIRO_OPERATOR_EXCLUSION:
430 case CAIRO_OPERATOR_HSL_HUE:
431 case CAIRO_OPERATOR_HSL_SATURATION:
432 case CAIRO_OPERATOR_HSL_COLOR:
433 case CAIRO_OPERATOR_HSL_LUMINOSITY:
435 case CAIRO_OPERATOR_CLEAR:
436 case CAIRO_OPERATOR_SOURCE:
437 case CAIRO_OPERATOR_OUT:
438 case CAIRO_OPERATOR_IN:
439 case CAIRO_OPERATOR_DEST_IN:
440 case CAIRO_OPERATOR_DEST_ATOP:
449 _cairo_operator_bounded_by_either (cairo_operator_t op)
454 case CAIRO_OPERATOR_OVER:
455 case CAIRO_OPERATOR_ATOP:
456 case CAIRO_OPERATOR_DEST:
457 case CAIRO_OPERATOR_DEST_OVER:
458 case CAIRO_OPERATOR_DEST_OUT:
459 case CAIRO_OPERATOR_XOR:
460 case CAIRO_OPERATOR_ADD:
461 case CAIRO_OPERATOR_SATURATE:
462 case CAIRO_OPERATOR_MULTIPLY:
463 case CAIRO_OPERATOR_SCREEN:
464 case CAIRO_OPERATOR_OVERLAY:
465 case CAIRO_OPERATOR_DARKEN:
466 case CAIRO_OPERATOR_LIGHTEN:
467 case CAIRO_OPERATOR_COLOR_DODGE:
468 case CAIRO_OPERATOR_COLOR_BURN:
469 case CAIRO_OPERATOR_HARD_LIGHT:
470 case CAIRO_OPERATOR_SOFT_LIGHT:
471 case CAIRO_OPERATOR_DIFFERENCE:
472 case CAIRO_OPERATOR_EXCLUSION:
473 case CAIRO_OPERATOR_HSL_HUE:
474 case CAIRO_OPERATOR_HSL_SATURATION:
475 case CAIRO_OPERATOR_HSL_COLOR:
476 case CAIRO_OPERATOR_HSL_LUMINOSITY:
477 return CAIRO_OPERATOR_BOUND_BY_MASK | CAIRO_OPERATOR_BOUND_BY_SOURCE;
478 case CAIRO_OPERATOR_CLEAR:
479 case CAIRO_OPERATOR_SOURCE:
480 return CAIRO_OPERATOR_BOUND_BY_MASK;
481 case CAIRO_OPERATOR_OUT:
482 case CAIRO_OPERATOR_IN:
483 case CAIRO_OPERATOR_DEST_IN:
484 case CAIRO_OPERATOR_DEST_ATOP:
490 #if DISABLE_SOME_FLOATING_POINT
491 /* This function is identical to the C99 function lround(), except that it
492 * performs arithmetic rounding (floor(d + .5) instead of away-from-zero rounding) and
493 * has a valid input range of (INT_MIN, INT_MAX] instead of
494 * [INT_MIN, INT_MAX]. It is much faster on both x86 and FPU-less systems
495 * than other commonly used methods for rounding (lround, round, rint, lrint
496 * or float (d + 0.5)).
498 * The reason why this function is much faster on x86 than other
499 * methods is due to the fact that it avoids the fldcw instruction.
500 * This instruction incurs a large performance penalty on modern Intel
501 * processors due to how it prevents efficient instruction pipelining.
503 * The reason why this function is much faster on FPU-less systems is for
504 * an entirely different reason. All common rounding methods involve multiple
505 * floating-point operations. Each one of these operations has to be
506 * emulated in software, which adds up to be a large performance penalty.
507 * This function doesn't perform any floating-point calculations, and thus
508 * avoids this penalty.
511 _cairo_lround (double d)
513 uint32_t top, shift_amount, output;
522 /* If the integer word order doesn't match the float word order, we swap
523 * the words of the input double. This is needed because we will be
524 * treating the whole double as a 64-bit unsigned integer. Notice that we
525 * use WORDS_BIGENDIAN to detect the integer word order, which isn't
526 * exactly correct because WORDS_BIGENDIAN refers to byte order, not word
527 * order. Thus, we are making the assumption that the byte order is the
528 * same as the integer word order which, on the modern machines that we
531 #if ( defined(FLOAT_WORDS_BIGENDIAN) && !defined(WORDS_BIGENDIAN)) || \
532 (!defined(FLOAT_WORDS_BIGENDIAN) && defined(WORDS_BIGENDIAN))
534 uint32_t temp = u.ui32[0];
535 u.ui32[0] = u.ui32[1];
540 #ifdef WORDS_BIGENDIAN
541 #define MSW (0) /* Most Significant Word */
542 #define LSW (1) /* Least Significant Word */
548 /* By shifting the most significant word of the input double to the
549 * right 20 places, we get the very "top" of the double where the exponent
552 top = u.ui32[MSW] >> 20;
554 /* Here, we calculate how much we have to shift the mantissa to normalize
555 * it to an integer value. We extract the exponent "top" by masking out the
556 * sign bit, then we calculate the shift amount by subtracting the exponent
557 * from the bias. Notice that the correct bias for 64-bit doubles is
558 * actually 1075, but we use 1053 instead for two reasons:
560 * 1) To perform rounding later on, we will first need the target
561 * value in a 31.1 fixed-point format. Thus, the bias needs to be one
562 * less: (1075 - 1: 1074).
564 * 2) To avoid shifting the mantissa as a full 64-bit integer (which is
565 * costly on certain architectures), we break the shift into two parts.
566 * First, the upper and lower parts of the mantissa are shifted
567 * individually by a constant amount that all valid inputs will require
568 * at the very least. This amount is chosen to be 21, because this will
569 * allow the two parts of the mantissa to later be combined into a
570 * single 32-bit representation, on which the remainder of the shift
571 * will be performed. Thus, we decrease the bias by an additional 21:
574 shift_amount = 1053 - (top & 0x7FF);
576 /* We are done with the exponent portion in "top", so here we shift it off
581 /* Before we perform any operations on the mantissa, we need to OR in
582 * the implicit 1 at the top (see the IEEE-754 spec). We needn't mask
583 * off the sign bit nor the exponent bits because these higher bits won't
584 * make a bit of difference in the rest of our calculations.
586 u.ui32[MSW] |= 0x100000;
588 /* If the input double is negative, we have to decrease the mantissa
589 * by a hair. This is an important part of performing arithmetic rounding,
590 * as negative numbers must round towards positive infinity in the
591 * halfwase case of -x.5. Since "top" contains only the sign bit at this
592 * point, we can just decrease the mantissa by the value of "top".
596 /* By decrementing "top", we create a bitmask with a value of either
597 * 0x0 (if the input was negative) or 0xFFFFFFFF (if the input was positive
598 * and thus the unsigned subtraction underflowed) that we'll use later.
602 /* Here, we shift the mantissa by the constant value as described above.
603 * We can emulate a 64-bit shift right by 21 through shifting the top 32
604 * bits left 11 places and ORing in the bottom 32 bits shifted 21 places
605 * to the right. Both parts of the mantissa are now packed into a single
606 * 32-bit integer. Although we severely truncate the lower part in the
607 * process, we still have enough significant bits to perform the conversion
608 * without error (for all valid inputs).
610 output = (u.ui32[MSW] << 11) | (u.ui32[LSW] >> 21);
612 /* Next, we perform the shift that converts the X.Y fixed-point number
613 * currently found in "output" to the desired 31.1 fixed-point format
614 * needed for the following rounding step. It is important to consider
615 * all possible values for "shift_amount" at this point:
617 * - {shift_amount < 0} Since shift_amount is an unsigned integer, it
618 * really can't have a value less than zero. But, if the shift_amount
619 * calculation above caused underflow (which would happen with
620 * input > INT_MAX or input <= INT_MIN) then shift_amount will now be
621 * a very large number, and so this shift will result in complete
622 * garbage. But that's OK, as the input was out of our range, so our
623 * output is undefined.
625 * - {shift_amount > 31} If the magnitude of the input was very small
626 * (i.e. |input| << 1.0), shift_amount will have a value greater than
627 * 31. Thus, this shift will also result in garbage. After performing
628 * the shift, we will zero-out "output" if this is the case.
630 * - {0 <= shift_amount < 32} In this case, the shift will properly convert
631 * the mantissa into a 31.1 fixed-point number.
633 output >>= shift_amount;
635 /* This is where we perform rounding with the 31.1 fixed-point number.
636 * Since what we're after is arithmetic rounding, we simply add the single
637 * fractional bit into the integer part of "output", and just keep the
640 output = (output >> 1) + (output & 1);
642 /* Here, we zero-out the result if the magnitude if the input was very small
643 * (as explained in the section above). Notice that all input out of the
644 * valid range is also caught by this condition, which means we produce 0
645 * for all invalid input, which is a nice side effect.
647 * The most straightforward way to do this would be:
649 * if (shift_amount > 31)
652 * But we can use a little trick to avoid the potential branch. The
653 * expression (shift_amount > 31) will be either 1 or 0, which when
654 * decremented will be either 0x0 or 0xFFFFFFFF (unsigned underflow),
655 * which can be used to conditionally mask away all the bits in "output"
656 * (in the 0x0 case), effectively zeroing it out. Certain, compilers would
657 * have done this for us automatically.
659 output &= ((shift_amount > 31) - 1);
661 /* If the input double was a negative number, then we have to negate our
662 * output. The most straightforward way to do this would be:
667 * as "top" at this point is either 0x0 (if the input was negative) or
668 * 0xFFFFFFFF (if the input was positive). But, we can use a trick to
669 * avoid the branch. Observe that the following snippet of code has the
670 * same effect as the reference snippet above:
673 * output = 0 - output;
675 * output = output - 0;
677 * Armed with the bitmask found in "top", we can condense the two statements
678 * into the following:
680 * output = (output & top) - (output & ~top);
682 * where, in the case that the input double was negative, "top" will be 0,
683 * and the statement will be equivalent to:
685 * output = (0) - (output);
687 * and if the input double was positive, "top" will be 0xFFFFFFFF, and the
688 * statement will be equivalent to:
690 * output = (output) - (0);
692 * Which, as pointed out earlier, is equivalent to the original reference
695 output = (output & top) - (output & ~top);
703 /* Convert a 32-bit IEEE single precision floating point number to a
704 * 'half' representation (s10.5)
707 _cairo_half_from_float (float f)
716 s = (u.ui >> 16) & 0x00008000;
717 e = ((u.ui >> 23) & 0x000000ff) - (127 - 15);
718 m = u.ui & 0x007fffff;
725 m = (m | 0x00800000) >> (1 - e);
727 /* round to nearest, round 0.5 up. */
730 return s | (m >> 13);
731 } else if (e == 0xff - (127 - 15)) {
738 return s | 0x7c00 | m | (m == 0);
741 /* round to nearest, round 0.5 up. */
742 if (m & 0x00001000) {
745 if (m & 0x00800000) {
752 /* overflow -> infinity */
756 return s | (e << 10) | (m >> 13);
763 #define WIN32_LEAN_AND_MEAN
764 /* We require Windows 2000 features such as ETO_PDY */
765 #if !defined(WINVER) || (WINVER < 0x0500)
766 # define WINVER 0x0500
768 #if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0500)
769 # define _WIN32_WINNT 0x0500
776 /* tmpfile() replacement for Windows.
778 * On Windows tmpfile() creates the file in the root directory. This
779 * may fail due to unsufficient privileges. However, this isn't a
780 * problem on Windows CE so we don't use it there.
783 _cairo_win32_tmpfile (void)
786 WCHAR path_name[MAX_PATH + 1];
787 WCHAR file_name[MAX_PATH + 1];
792 path_len = GetTempPathW (MAX_PATH, path_name);
793 if (path_len <= 0 || path_len >= MAX_PATH)
796 if (GetTempFileNameW (path_name, L"ps_", 0, file_name) == 0)
799 handle = CreateFileW (file_name,
800 GENERIC_READ | GENERIC_WRITE,
804 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE,
806 if (handle == INVALID_HANDLE_VALUE) {
807 DeleteFileW (file_name);
811 fd = _open_osfhandle((intptr_t) handle, 0);
813 CloseHandle (handle);
817 fp = fdopen(fd, "w+b");
825 #endif /* !_WIN32_WCE */
829 typedef struct _cairo_intern_string {
830 cairo_hash_entry_t hash_entry;
833 } cairo_intern_string_t;
835 static cairo_hash_table_t *_cairo_intern_string_ht;
838 _intern_string_hash (const char *str, int len)
840 const signed char *p = (const signed char *) str;
843 for (p += 1; --len; p++)
844 h = (h << 5) - h + *p;
850 _intern_string_equal (const void *_a, const void *_b)
852 const cairo_intern_string_t *a = _a;
853 const cairo_intern_string_t *b = _b;
855 if (a->len != b->len)
858 return memcmp (a->string, b->string, a->len) == 0;
862 _cairo_intern_string (const char **str_inout, int len)
864 char *str = (char *) *str_inout;
865 cairo_intern_string_t tmpl, *istring;
866 cairo_status_t status = CAIRO_STATUS_SUCCESS;
868 if (CAIRO_INJECT_FAULT ())
869 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
873 tmpl.hash_entry.hash = _intern_string_hash (str, len);
875 tmpl.string = (char *) str;
877 CAIRO_MUTEX_LOCK (_cairo_intern_string_mutex);
878 if (_cairo_intern_string_ht == NULL) {
879 _cairo_intern_string_ht = _cairo_hash_table_create (_intern_string_equal);
880 if (unlikely (_cairo_intern_string_ht == NULL)) {
881 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
886 istring = _cairo_hash_table_lookup (_cairo_intern_string_ht,
888 if (istring == NULL) {
889 istring = malloc (sizeof (cairo_intern_string_t) + len + 1);
890 if (likely (istring != NULL)) {
891 istring->hash_entry.hash = tmpl.hash_entry.hash;
892 istring->len = tmpl.len;
893 istring->string = (char *) (istring + 1);
894 memcpy (istring->string, str, len);
895 istring->string[len] = '\0';
897 status = _cairo_hash_table_insert (_cairo_intern_string_ht,
898 &istring->hash_entry);
899 if (unlikely (status)) {
904 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
909 *str_inout = istring->string;
912 CAIRO_MUTEX_UNLOCK (_cairo_intern_string_mutex);
917 _intern_string_pluck (void *entry, void *closure)
919 _cairo_hash_table_remove (closure, entry);
924 _cairo_intern_string_reset_static_data (void)
926 CAIRO_MUTEX_LOCK (_cairo_intern_string_mutex);
927 if (_cairo_intern_string_ht != NULL) {
928 _cairo_hash_table_foreach (_cairo_intern_string_ht,
929 _intern_string_pluck,
930 _cairo_intern_string_ht);
931 _cairo_hash_table_destroy(_cairo_intern_string_ht);
932 _cairo_intern_string_ht = NULL;
934 CAIRO_MUTEX_UNLOCK (_cairo_intern_string_mutex);