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";
159 case CAIRO_STATUS_JBIG2_GLOBAL_MISSING
:
160 return "CAIRO_MIME_TYPE_JBIG2_GLOBAL_ID used but no CAIRO_MIME_TYPE_JBIG2_GLOBAL data provided";
162 case CAIRO_STATUS_LAST_STATUS
:
163 return "<unknown error status>";
169 * cairo_glyph_allocate:
170 * @num_glyphs: number of glyphs to allocate
172 * Allocates an array of #cairo_glyph_t's.
173 * This function is only useful in implementations of
174 * #cairo_user_scaled_font_text_to_glyphs_func_t where the user
175 * needs to allocate an array of glyphs that cairo will free.
176 * For all other uses, user can use their own allocation method
179 * This function returns %NULL if @num_glyphs is not positive,
180 * or if out of memory. That means, the %NULL return value
181 * signals out-of-memory only if @num_glyphs was positive.
183 * Returns: the newly allocated array of glyphs that should be
184 * freed using cairo_glyph_free()
189 cairo_glyph_allocate (int num_glyphs
)
194 return _cairo_malloc_ab (num_glyphs
, sizeof (cairo_glyph_t
));
196 slim_hidden_def (cairo_glyph_allocate
);
200 * @glyphs: array of glyphs to free, or %NULL
202 * Frees an array of #cairo_glyph_t's allocated using cairo_glyph_allocate().
203 * This function is only useful to free glyph array returned
204 * by cairo_scaled_font_text_to_glyphs() where cairo returns
205 * an array of glyphs that the user will free.
206 * For all other uses, user can use their own allocation method
212 cairo_glyph_free (cairo_glyph_t
*glyphs
)
216 slim_hidden_def (cairo_glyph_free
);
219 * cairo_text_cluster_allocate:
220 * @num_clusters: number of text_clusters to allocate
222 * Allocates an array of #cairo_text_cluster_t's.
223 * This function is only useful in implementations of
224 * #cairo_user_scaled_font_text_to_glyphs_func_t where the user
225 * needs to allocate an array of text clusters that cairo will free.
226 * For all other uses, user can use their own allocation method
229 * This function returns %NULL if @num_clusters is not positive,
230 * or if out of memory. That means, the %NULL return value
231 * signals out-of-memory only if @num_clusters was positive.
233 * Returns: the newly allocated array of text clusters that should be
234 * freed using cairo_text_cluster_free()
238 cairo_text_cluster_t
*
239 cairo_text_cluster_allocate (int num_clusters
)
241 if (num_clusters
<= 0)
244 return _cairo_malloc_ab (num_clusters
, sizeof (cairo_text_cluster_t
));
246 slim_hidden_def (cairo_text_cluster_allocate
);
249 * cairo_text_cluster_free:
250 * @clusters: array of text clusters to free, or %NULL
252 * Frees an array of #cairo_text_cluster's allocated using cairo_text_cluster_allocate().
253 * This function is only useful to free text cluster array returned
254 * by cairo_scaled_font_text_to_glyphs() where cairo returns
255 * an array of text clusters that the user will free.
256 * For all other uses, user can use their own allocation method
262 cairo_text_cluster_free (cairo_text_cluster_t
*clusters
)
266 slim_hidden_def (cairo_text_cluster_free
);
272 * _cairo_validate_text_clusters:
274 * @utf8_len: length of @utf8 in bytes
275 * @glyphs: array of glyphs
276 * @num_glyphs: number of glyphs
277 * @clusters: array of cluster mapping information
278 * @num_clusters: number of clusters in the mapping
279 * @cluster_flags: cluster flags
281 * Check that clusters cover the entire glyphs and utf8 arrays,
282 * and that cluster boundaries are UTF-8 boundaries.
284 * Return value: %CAIRO_STATUS_SUCCESS upon success, or
285 * %CAIRO_STATUS_INVALID_CLUSTERS on error.
286 * The error is either invalid UTF-8 input,
287 * or bad cluster mapping.
290 _cairo_validate_text_clusters (const char *utf8
,
292 const cairo_glyph_t
*glyphs
,
294 const cairo_text_cluster_t
*clusters
,
296 cairo_text_cluster_flags_t cluster_flags
)
298 cairo_status_t status
;
299 unsigned int n_bytes
= 0;
300 unsigned int n_glyphs
= 0;
303 for (i
= 0; i
< num_clusters
; i
++) {
304 int cluster_bytes
= clusters
[i
].num_bytes
;
305 int cluster_glyphs
= clusters
[i
].num_glyphs
;
307 if (cluster_bytes
< 0 || cluster_glyphs
< 0)
310 /* A cluster should cover at least one character or glyph.
311 * I can't see any use for a 0,0 cluster.
312 * I can't see an immediate use for a zero-text cluster
313 * right now either, but they don't harm.
314 * Zero-glyph clusters on the other hand are useful for
315 * things like U+200C ZERO WIDTH NON-JOINER */
316 if (cluster_bytes
== 0 && cluster_glyphs
== 0)
319 /* Since n_bytes and n_glyphs are unsigned, but the rest of
320 * values involved are signed, we can detect overflow easily */
321 if (n_bytes
+cluster_bytes
> (unsigned int)utf8_len
|| n_glyphs
+cluster_glyphs
> (unsigned int)num_glyphs
)
324 /* Make sure we've got valid UTF-8 for the cluster */
325 status
= _cairo_utf8_to_ucs4 (utf8
+n_bytes
, cluster_bytes
, NULL
, NULL
);
326 if (unlikely (status
))
327 return _cairo_error (CAIRO_STATUS_INVALID_CLUSTERS
);
329 n_bytes
+= cluster_bytes
;
330 n_glyphs
+= cluster_glyphs
;
333 if (n_bytes
!= (unsigned int) utf8_len
|| n_glyphs
!= (unsigned int) num_glyphs
) {
335 return _cairo_error (CAIRO_STATUS_INVALID_CLUSTERS
);
338 return CAIRO_STATUS_SUCCESS
;
342 * _cairo_operator_bounded_by_mask:
343 * @op: a #cairo_operator_t
345 * A bounded operator is one where mask pixel
346 * of zero results in no effect on the destination image.
348 * Unbounded operators often require special handling; if you, for
349 * example, draw trapezoids with an unbounded operator, the effect
350 * extends past the bounding box of the trapezoids.
352 * Return value: %TRUE if the operator is bounded by the mask operand
355 _cairo_operator_bounded_by_mask (cairo_operator_t op
)
358 case CAIRO_OPERATOR_CLEAR
:
359 case CAIRO_OPERATOR_SOURCE
:
360 case CAIRO_OPERATOR_OVER
:
361 case CAIRO_OPERATOR_ATOP
:
362 case CAIRO_OPERATOR_DEST
:
363 case CAIRO_OPERATOR_DEST_OVER
:
364 case CAIRO_OPERATOR_DEST_OUT
:
365 case CAIRO_OPERATOR_XOR
:
366 case CAIRO_OPERATOR_ADD
:
367 case CAIRO_OPERATOR_SATURATE
:
368 case CAIRO_OPERATOR_MULTIPLY
:
369 case CAIRO_OPERATOR_SCREEN
:
370 case CAIRO_OPERATOR_OVERLAY
:
371 case CAIRO_OPERATOR_DARKEN
:
372 case CAIRO_OPERATOR_LIGHTEN
:
373 case CAIRO_OPERATOR_COLOR_DODGE
:
374 case CAIRO_OPERATOR_COLOR_BURN
:
375 case CAIRO_OPERATOR_HARD_LIGHT
:
376 case CAIRO_OPERATOR_SOFT_LIGHT
:
377 case CAIRO_OPERATOR_DIFFERENCE
:
378 case CAIRO_OPERATOR_EXCLUSION
:
379 case CAIRO_OPERATOR_HSL_HUE
:
380 case CAIRO_OPERATOR_HSL_SATURATION
:
381 case CAIRO_OPERATOR_HSL_COLOR
:
382 case CAIRO_OPERATOR_HSL_LUMINOSITY
:
384 case CAIRO_OPERATOR_OUT
:
385 case CAIRO_OPERATOR_IN
:
386 case CAIRO_OPERATOR_DEST_IN
:
387 case CAIRO_OPERATOR_DEST_ATOP
:
396 * _cairo_operator_bounded_by_source:
397 * @op: a #cairo_operator_t
399 * A bounded operator is one where source pixels of zero
400 * (in all four components, r, g, b and a) effect no change
401 * in the resulting destination image.
403 * Unbounded operators often require special handling; if you, for
404 * example, copy a surface with the SOURCE operator, the effect
405 * extends past the bounding box of the source surface.
407 * Return value: %TRUE if the operator is bounded by the source operand
410 _cairo_operator_bounded_by_source (cairo_operator_t op
)
413 case CAIRO_OPERATOR_OVER
:
414 case CAIRO_OPERATOR_ATOP
:
415 case CAIRO_OPERATOR_DEST
:
416 case CAIRO_OPERATOR_DEST_OVER
:
417 case CAIRO_OPERATOR_DEST_OUT
:
418 case CAIRO_OPERATOR_XOR
:
419 case CAIRO_OPERATOR_ADD
:
420 case CAIRO_OPERATOR_SATURATE
:
421 case CAIRO_OPERATOR_MULTIPLY
:
422 case CAIRO_OPERATOR_SCREEN
:
423 case CAIRO_OPERATOR_OVERLAY
:
424 case CAIRO_OPERATOR_DARKEN
:
425 case CAIRO_OPERATOR_LIGHTEN
:
426 case CAIRO_OPERATOR_COLOR_DODGE
:
427 case CAIRO_OPERATOR_COLOR_BURN
:
428 case CAIRO_OPERATOR_HARD_LIGHT
:
429 case CAIRO_OPERATOR_SOFT_LIGHT
:
430 case CAIRO_OPERATOR_DIFFERENCE
:
431 case CAIRO_OPERATOR_EXCLUSION
:
432 case CAIRO_OPERATOR_HSL_HUE
:
433 case CAIRO_OPERATOR_HSL_SATURATION
:
434 case CAIRO_OPERATOR_HSL_COLOR
:
435 case CAIRO_OPERATOR_HSL_LUMINOSITY
:
437 case CAIRO_OPERATOR_CLEAR
:
438 case CAIRO_OPERATOR_SOURCE
:
439 case CAIRO_OPERATOR_OUT
:
440 case CAIRO_OPERATOR_IN
:
441 case CAIRO_OPERATOR_DEST_IN
:
442 case CAIRO_OPERATOR_DEST_ATOP
:
451 _cairo_operator_bounded_by_either (cairo_operator_t op
)
456 case CAIRO_OPERATOR_OVER
:
457 case CAIRO_OPERATOR_ATOP
:
458 case CAIRO_OPERATOR_DEST
:
459 case CAIRO_OPERATOR_DEST_OVER
:
460 case CAIRO_OPERATOR_DEST_OUT
:
461 case CAIRO_OPERATOR_XOR
:
462 case CAIRO_OPERATOR_ADD
:
463 case CAIRO_OPERATOR_SATURATE
:
464 case CAIRO_OPERATOR_MULTIPLY
:
465 case CAIRO_OPERATOR_SCREEN
:
466 case CAIRO_OPERATOR_OVERLAY
:
467 case CAIRO_OPERATOR_DARKEN
:
468 case CAIRO_OPERATOR_LIGHTEN
:
469 case CAIRO_OPERATOR_COLOR_DODGE
:
470 case CAIRO_OPERATOR_COLOR_BURN
:
471 case CAIRO_OPERATOR_HARD_LIGHT
:
472 case CAIRO_OPERATOR_SOFT_LIGHT
:
473 case CAIRO_OPERATOR_DIFFERENCE
:
474 case CAIRO_OPERATOR_EXCLUSION
:
475 case CAIRO_OPERATOR_HSL_HUE
:
476 case CAIRO_OPERATOR_HSL_SATURATION
:
477 case CAIRO_OPERATOR_HSL_COLOR
:
478 case CAIRO_OPERATOR_HSL_LUMINOSITY
:
479 return CAIRO_OPERATOR_BOUND_BY_MASK
| CAIRO_OPERATOR_BOUND_BY_SOURCE
;
480 case CAIRO_OPERATOR_CLEAR
:
481 case CAIRO_OPERATOR_SOURCE
:
482 return CAIRO_OPERATOR_BOUND_BY_MASK
;
483 case CAIRO_OPERATOR_OUT
:
484 case CAIRO_OPERATOR_IN
:
485 case CAIRO_OPERATOR_DEST_IN
:
486 case CAIRO_OPERATOR_DEST_ATOP
:
492 #if DISABLE_SOME_FLOATING_POINT
493 /* This function is identical to the C99 function lround(), except that it
494 * performs arithmetic rounding (floor(d + .5) instead of away-from-zero rounding) and
495 * has a valid input range of (INT_MIN, INT_MAX] instead of
496 * [INT_MIN, INT_MAX]. It is much faster on both x86 and FPU-less systems
497 * than other commonly used methods for rounding (lround, round, rint, lrint
498 * or float (d + 0.5)).
500 * The reason why this function is much faster on x86 than other
501 * methods is due to the fact that it avoids the fldcw instruction.
502 * This instruction incurs a large performance penalty on modern Intel
503 * processors due to how it prevents efficient instruction pipelining.
505 * The reason why this function is much faster on FPU-less systems is for
506 * an entirely different reason. All common rounding methods involve multiple
507 * floating-point operations. Each one of these operations has to be
508 * emulated in software, which adds up to be a large performance penalty.
509 * This function doesn't perform any floating-point calculations, and thus
510 * avoids this penalty.
513 _cairo_lround (double d
)
515 uint32_t top
, shift_amount
, output
;
524 /* If the integer word order doesn't match the float word order, we swap
525 * the words of the input double. This is needed because we will be
526 * treating the whole double as a 64-bit unsigned integer. Notice that we
527 * use WORDS_BIGENDIAN to detect the integer word order, which isn't
528 * exactly correct because WORDS_BIGENDIAN refers to byte order, not word
529 * order. Thus, we are making the assumption that the byte order is the
530 * same as the integer word order which, on the modern machines that we
533 #if ( defined(FLOAT_WORDS_BIGENDIAN) && !defined(WORDS_BIGENDIAN)) || \
534 (!defined(FLOAT_WORDS_BIGENDIAN) && defined(WORDS_BIGENDIAN))
536 uint32_t temp
= u
.ui32
[0];
537 u
.ui32
[0] = u
.ui32
[1];
542 #ifdef WORDS_BIGENDIAN
543 #define MSW (0) /* Most Significant Word */
544 #define LSW (1) /* Least Significant Word */
550 /* By shifting the most significant word of the input double to the
551 * right 20 places, we get the very "top" of the double where the exponent
554 top
= u
.ui32
[MSW
] >> 20;
556 /* Here, we calculate how much we have to shift the mantissa to normalize
557 * it to an integer value. We extract the exponent "top" by masking out the
558 * sign bit, then we calculate the shift amount by subtracting the exponent
559 * from the bias. Notice that the correct bias for 64-bit doubles is
560 * actually 1075, but we use 1053 instead for two reasons:
562 * 1) To perform rounding later on, we will first need the target
563 * value in a 31.1 fixed-point format. Thus, the bias needs to be one
564 * less: (1075 - 1: 1074).
566 * 2) To avoid shifting the mantissa as a full 64-bit integer (which is
567 * costly on certain architectures), we break the shift into two parts.
568 * First, the upper and lower parts of the mantissa are shifted
569 * individually by a constant amount that all valid inputs will require
570 * at the very least. This amount is chosen to be 21, because this will
571 * allow the two parts of the mantissa to later be combined into a
572 * single 32-bit representation, on which the remainder of the shift
573 * will be performed. Thus, we decrease the bias by an additional 21:
576 shift_amount
= 1053 - (top
& 0x7FF);
578 /* We are done with the exponent portion in "top", so here we shift it off
583 /* Before we perform any operations on the mantissa, we need to OR in
584 * the implicit 1 at the top (see the IEEE-754 spec). We needn't mask
585 * off the sign bit nor the exponent bits because these higher bits won't
586 * make a bit of difference in the rest of our calculations.
588 u
.ui32
[MSW
] |= 0x100000;
590 /* If the input double is negative, we have to decrease the mantissa
591 * by a hair. This is an important part of performing arithmetic rounding,
592 * as negative numbers must round towards positive infinity in the
593 * halfwase case of -x.5. Since "top" contains only the sign bit at this
594 * point, we can just decrease the mantissa by the value of "top".
598 /* By decrementing "top", we create a bitmask with a value of either
599 * 0x0 (if the input was negative) or 0xFFFFFFFF (if the input was positive
600 * and thus the unsigned subtraction underflowed) that we'll use later.
604 /* Here, we shift the mantissa by the constant value as described above.
605 * We can emulate a 64-bit shift right by 21 through shifting the top 32
606 * bits left 11 places and ORing in the bottom 32 bits shifted 21 places
607 * to the right. Both parts of the mantissa are now packed into a single
608 * 32-bit integer. Although we severely truncate the lower part in the
609 * process, we still have enough significant bits to perform the conversion
610 * without error (for all valid inputs).
612 output
= (u
.ui32
[MSW
] << 11) | (u
.ui32
[LSW
] >> 21);
614 /* Next, we perform the shift that converts the X.Y fixed-point number
615 * currently found in "output" to the desired 31.1 fixed-point format
616 * needed for the following rounding step. It is important to consider
617 * all possible values for "shift_amount" at this point:
619 * - {shift_amount < 0} Since shift_amount is an unsigned integer, it
620 * really can't have a value less than zero. But, if the shift_amount
621 * calculation above caused underflow (which would happen with
622 * input > INT_MAX or input <= INT_MIN) then shift_amount will now be
623 * a very large number, and so this shift will result in complete
624 * garbage. But that's OK, as the input was out of our range, so our
625 * output is undefined.
627 * - {shift_amount > 31} If the magnitude of the input was very small
628 * (i.e. |input| << 1.0), shift_amount will have a value greater than
629 * 31. Thus, this shift will also result in garbage. After performing
630 * the shift, we will zero-out "output" if this is the case.
632 * - {0 <= shift_amount < 32} In this case, the shift will properly convert
633 * the mantissa into a 31.1 fixed-point number.
635 output
>>= shift_amount
;
637 /* This is where we perform rounding with the 31.1 fixed-point number.
638 * Since what we're after is arithmetic rounding, we simply add the single
639 * fractional bit into the integer part of "output", and just keep the
642 output
= (output
>> 1) + (output
& 1);
644 /* Here, we zero-out the result if the magnitude if the input was very small
645 * (as explained in the section above). Notice that all input out of the
646 * valid range is also caught by this condition, which means we produce 0
647 * for all invalid input, which is a nice side effect.
649 * The most straightforward way to do this would be:
651 * if (shift_amount > 31)
654 * But we can use a little trick to avoid the potential branch. The
655 * expression (shift_amount > 31) will be either 1 or 0, which when
656 * decremented will be either 0x0 or 0xFFFFFFFF (unsigned underflow),
657 * which can be used to conditionally mask away all the bits in "output"
658 * (in the 0x0 case), effectively zeroing it out. Certain, compilers would
659 * have done this for us automatically.
661 output
&= ((shift_amount
> 31) - 1);
663 /* If the input double was a negative number, then we have to negate our
664 * output. The most straightforward way to do this would be:
669 * as "top" at this point is either 0x0 (if the input was negative) or
670 * 0xFFFFFFFF (if the input was positive). But, we can use a trick to
671 * avoid the branch. Observe that the following snippet of code has the
672 * same effect as the reference snippet above:
675 * output = 0 - output;
677 * output = output - 0;
679 * Armed with the bitmask found in "top", we can condense the two statements
680 * into the following:
682 * output = (output & top) - (output & ~top);
684 * where, in the case that the input double was negative, "top" will be 0,
685 * and the statement will be equivalent to:
687 * output = (0) - (output);
689 * and if the input double was positive, "top" will be 0xFFFFFFFF, and the
690 * statement will be equivalent to:
692 * output = (output) - (0);
694 * Which, as pointed out earlier, is equivalent to the original reference
697 output
= (output
& top
) - (output
& ~top
);
705 /* Convert a 32-bit IEEE single precision floating point number to a
706 * 'half' representation (s10.5)
709 _cairo_half_from_float (float f
)
718 s
= (u
.ui
>> 16) & 0x00008000;
719 e
= ((u
.ui
>> 23) & 0x000000ff) - (127 - 15);
720 m
= u
.ui
& 0x007fffff;
727 m
= (m
| 0x00800000) >> (1 - e
);
729 /* round to nearest, round 0.5 up. */
732 return s
| (m
>> 13);
733 } else if (e
== 0xff - (127 - 15)) {
740 return s
| 0x7c00 | m
| (m
== 0);
743 /* round to nearest, round 0.5 up. */
744 if (m
& 0x00001000) {
747 if (m
& 0x00800000) {
754 /* overflow -> infinity */
758 return s
| (e
<< 10) | (m
>> 13);
766 cairo_get_locale_decimal_point (void)
768 struct lconv
*locale_data
= localeconv ();
769 return locale_data
->decimal_point
;
773 /* Android's Bionic libc doesn't provide decimal_point */
775 cairo_get_locale_decimal_point (void)
783 #define WIN32_LEAN_AND_MEAN
784 /* We require Windows 2000 features such as ETO_PDY */
785 #if !defined(WINVER) || (WINVER < 0x0500)
786 # define WINVER 0x0500
788 #if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0500)
789 # define _WIN32_WINNT 0x0500
796 /* tmpfile() replacement for Windows.
798 * On Windows tmpfile() creates the file in the root directory. This
799 * may fail due to unsufficient privileges. However, this isn't a
800 * problem on Windows CE so we don't use it there.
803 _cairo_win32_tmpfile (void)
806 WCHAR path_name
[MAX_PATH
+ 1];
807 WCHAR file_name
[MAX_PATH
+ 1];
812 path_len
= GetTempPathW (MAX_PATH
, path_name
);
813 if (path_len
<= 0 || path_len
>= MAX_PATH
)
816 if (GetTempFileNameW (path_name
, L
"ps_", 0, file_name
) == 0)
819 handle
= CreateFileW (file_name
,
820 GENERIC_READ
| GENERIC_WRITE
,
824 FILE_ATTRIBUTE_NORMAL
| FILE_FLAG_DELETE_ON_CLOSE
,
826 if (handle
== INVALID_HANDLE_VALUE
) {
827 DeleteFileW (file_name
);
831 fd
= _open_osfhandle((intptr_t) handle
, 0);
833 CloseHandle (handle
);
837 fp
= fdopen(fd
, "w+b");
845 #endif /* !_WIN32_WCE */
849 typedef struct _cairo_intern_string
{
850 cairo_hash_entry_t hash_entry
;
853 } cairo_intern_string_t
;
855 static cairo_hash_table_t
*_cairo_intern_string_ht
;
858 _intern_string_hash (const char *str
, int len
)
860 const signed char *p
= (const signed char *) str
;
863 for (p
+= 1; --len
; p
++)
864 h
= (h
<< 5) - h
+ *p
;
870 _intern_string_equal (const void *_a
, const void *_b
)
872 const cairo_intern_string_t
*a
= _a
;
873 const cairo_intern_string_t
*b
= _b
;
875 if (a
->len
!= b
->len
)
878 return memcmp (a
->string
, b
->string
, a
->len
) == 0;
882 _cairo_intern_string (const char **str_inout
, int len
)
884 char *str
= (char *) *str_inout
;
885 cairo_intern_string_t tmpl
, *istring
;
886 cairo_status_t status
= CAIRO_STATUS_SUCCESS
;
888 if (CAIRO_INJECT_FAULT ())
889 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
893 tmpl
.hash_entry
.hash
= _intern_string_hash (str
, len
);
895 tmpl
.string
= (char *) str
;
897 CAIRO_MUTEX_LOCK (_cairo_intern_string_mutex
);
898 if (_cairo_intern_string_ht
== NULL
) {
899 _cairo_intern_string_ht
= _cairo_hash_table_create (_intern_string_equal
);
900 if (unlikely (_cairo_intern_string_ht
== NULL
)) {
901 status
= _cairo_error (CAIRO_STATUS_NO_MEMORY
);
906 istring
= _cairo_hash_table_lookup (_cairo_intern_string_ht
,
908 if (istring
== NULL
) {
909 istring
= malloc (sizeof (cairo_intern_string_t
) + len
+ 1);
910 if (likely (istring
!= NULL
)) {
911 istring
->hash_entry
.hash
= tmpl
.hash_entry
.hash
;
912 istring
->len
= tmpl
.len
;
913 istring
->string
= (char *) (istring
+ 1);
914 memcpy (istring
->string
, str
, len
);
915 istring
->string
[len
] = '\0';
917 status
= _cairo_hash_table_insert (_cairo_intern_string_ht
,
918 &istring
->hash_entry
);
919 if (unlikely (status
)) {
924 status
= _cairo_error (CAIRO_STATUS_NO_MEMORY
);
929 *str_inout
= istring
->string
;
932 CAIRO_MUTEX_UNLOCK (_cairo_intern_string_mutex
);
937 _intern_string_pluck (void *entry
, void *closure
)
939 _cairo_hash_table_remove (closure
, entry
);
944 _cairo_intern_string_reset_static_data (void)
946 CAIRO_MUTEX_LOCK (_cairo_intern_string_mutex
);
947 if (_cairo_intern_string_ht
!= NULL
) {
948 _cairo_hash_table_foreach (_cairo_intern_string_ht
,
949 _intern_string_pluck
,
950 _cairo_intern_string_ht
);
951 _cairo_hash_table_destroy(_cairo_intern_string_ht
);
952 _cairo_intern_string_ht
= NULL
;
954 CAIRO_MUTEX_UNLOCK (_cairo_intern_string_mutex
);