Find a file
Taylor R Campbell b1ccd52183 Avoid misuse of ctype(3) functions
The ctype(3) character classification and mapping functions have a
peculiarly limited definition (C11, Sec. 7.4 `Character handling
<ctype.h>', p. 200):

	`The header <ctype.h> declares several functions useful for
	 classifying and mapping characters.  In all cases the
	 argument is an int, the value of which shall be
	 representable as an unsigned char or shall equal the value
	 of the macro EOF.  If the argument has any other value, the
	 behavior is undefined.'

In other words, in the most common case of 8-bit char and EOF = -1,
the domain of the 257 allowed arguments is:

	-1, 0, 1, 2, ..., 254, 255

The ctype(3) functions are designed for use with stdio functions like
getchar and fgetc which return int values in the same domain.

In an ABI where char is signed (e.g., x86 SysV ABI used by most
Unixish operating systems), passing an argument of type char as is
can go wrong in two ways:

1. The value of a non-EOF input octet interpreted as `char' may
   coincide, as an integer, with the value of EOF, leading to wrong
   answers for some non-EOF inputs.

   E.g., if EOF = 1, and an input octet has all bits set, i.e., 255
   as an unsigned char, then as a char the value is -1, which will be
   confused with EOF.  In the ISO-8859-1 locale, the code point 255
   is (in Unicode terminology) LATIN SMALL LETTER Y WITH DIAERESIS,
   for which isprint, isalpha, &c., are true.  But isprint, isalpha,
   &c., are false for EOF.  So if char *s points to a string with
   that character, isprint(*s) will return false when it should
   return true.

2. Passing a negative char whose value does not coincide with EOF is
   undefined behaviour.

   This isn't purely theoretical: often the functions are implemented
   by an array lookup, #define isprint(c) (ctypetab[c] & ISPRINT).
   If c is out of range (e.g., 192, ISO-8859-1 for LATIN CAPITAL
   LETTER A WITH GRAVE, which convers to (signed) char as -64), then
   you can get garbage answers by reading uninitialized memory or
   application crashes with SIGSEGV if the page preceding the table
   is unmapped.

If what you have is an arbitrary char (e.g., from a char * string
pointing at user input), then the only correct way to use the
ctype(3) functions is by converting to unsigned char first -- e.g.,
isprint((unsigned char)*s).  (If the functions were defined as macros
that convert to unsigned char first, they would then spuriously
interpret EOF as a non-EOF, so they can't do that themselves.)

It is possible, in some cases, to prove that the input always
actually lies in {0, 1, 2, ..., 127}, so the conversion to unsigned
char is not necessary.  I didn't check whether this was the case --
it's safer to just adopt the habit of always casting char to unsigned
char first before using the ctype(3) macros, which satisfies a
compiler warning on some systems designed to detect this class of
application errors at compile-time.
2023-03-29 09:04:46 +00:00
.gitlab-ci [quartz] Cleanup and make better use of cairo_quartz_image_surface_t. 2023-02-14 11:52:57 -08:00
boilerplate [quartz]Conditionally Use Main Display ColorSpace instead of kCGColorSpaceDefaultRGB. 2023-02-17 10:25:14 -08:00
doc Merge branch 'doc-fixes' into 'master' 2023-02-08 09:53:29 +00:00
meson-cc-tests Fuzzer 2022-12-28 13:22:00 +10:30
perf Avoid misuse of ctype(3) functions 2023-03-29 09:04:46 +00:00
src Merge branch 'issue-643' into 'master' 2023-03-07 19:22:04 +00:00
subprojects ci: update Windows image to latest, and glib wrap too 2022-10-27 15:39:02 +01:00
test Avoid misuse of ctype(3) functions 2023-03-29 09:04:46 +00:00
util [gobject] Bind cairo_glyph_t and cairo_text_cluster_t 2023-03-09 13:04:50 -07:00
.gitignore Ignore build instead of builddir 2021-05-29 18:13:30 +02:00
.gitlab-ci.yml Drop cairo-gl 2023-01-27 19:21:46 +00:00
AUTHORS Misc. typos 2019-01-31 17:37:15 -08:00
BUGS Use HTTPS URLs for freedesktop.org domains 2018-10-16 10:03:07 -07:00
CODING_STYLE Add emacs modeline to CODING_STYLE 2023-01-15 19:00:22 +10:30
COPYING Add a COPYING file to each aux. source directory 2008-10-31 16:14:14 +00:00
COPYING-LGPL-2.1 Update FSF address 2010-04-27 11:13:38 +02:00
COPYING-MPL-1.1 Add the MPL as a new license option, in addition to the LGPL. 2004-09-04 06:38:34 +00:00
HACKING Use HTTPS URLs for freedesktop.org domains 2018-10-16 10:03:07 -07:00
INSTALL README fixes 2023-01-15 19:00:22 +10:30
meson.build Merge branch 'drop-xml-surface' into 'master' 2023-03-02 19:22:44 +00:00
meson_options.txt Remove XML surface 2023-02-04 11:36:25 +01:00
NEWS Release Cairo 1.17.8 (snapshot) 2023-02-02 08:37:29 +01:00
README.md docs: Port the README to Markdown 2023-02-02 08:37:29 +01:00
README.win32 [README.win32] Update wording from Tor Lillqvist 2008-09-26 12:44:53 -04:00
version.py build: Turn version.py into idiomatic Python 2023-02-02 08:37:28 +01:00

Cairo: Multi-platform 2D graphics library

https://cairographics.org

What is cairo

Cairo is a 2D graphics library with support for multiple output devices. Currently supported output targets include the X Window System (via both Xlib and XCB), quartz, win32, and image buffers, as well as PDF, PostScript, and SVG file output. Experimental backends include OpenGL.

Cairo is designed to produce consistent output on all output media while taking advantage of display hardware acceleration when available (for example, through the X Render Extension).

The cairo API provides operations similar to the drawing operators of PostScript and PDF. Operations in cairo include stroking and filling cubic Bézier splines, transforming and compositing translucent images, and antialiased text rendering. All drawing operations can be transformed by any affine transformation (scale, rotation, shear, etc.).

Cairo has been designed to let you draw anything you want in a modern 2D graphical user interface. At the same time, the cairo API has been designed to be as fun and easy to learn as possible. If you're not having fun while programming with cairo, then we have failed somewhere---let us know and we'll try to fix it next time around.

Cairo is free software and is available to be redistributed and/or modified under the terms of either the GNU Lesser General Public License (LGPL) version 2.1 or the Mozilla Public License (MPL) version 1.1.

Where to get more information about cairo

The primary source of information about cairo is its website:

The latest versions of cairo can always be found at:

Documentation on using cairo and frequently-asked questions:

Mailing lists for contacting cairo users and developers:

Roadmap and unscheduled things to do, (please feel free to help out):

Dependencies

The set of libraries needed to compile cairo depends on which backends are enabled when cairo is configured. So look at the list below to determine which dependencies are needed for the backends of interest.

For the surface backends, we have both "supported" and "experimental" backends. Further, the supported backends can be divided into the "standard" backends which can be easily built on any platform, and the "platform" backends which depend on some underlying platform-specific system, (such as the X Window System or some other window system).

As an example, for a standard Linux build similar to what's shipped by your distro, (with image, png, pdf, PostScript, svg, and xlib surface backends, and the freetype font backend), the following sample commands will install necessary dependencies:

  • Debian (and similar):

    • apt-get build-dep cairo
  • Fedora (and similar):

    • dnf builddep cairo

Technically you probably don't need pixman from the distribution since if you're manually compiling Cairo you probably want an updated pixman as well. However, if you follow the default settings and install pixman to /usr/local, your Cairo build should properly use it in preference to the system pixman.

Supported, "standard" surface backends

image backend (required)

PNG support (preferred)

PDF backend

PostScript backend

SVG backend

  • none

Supported, "platform" surface backends

Xlib backend

xlib-xrender backend

Quartz backend

  • macOS >= 10.4 with Xcode >= 2.5

Windows backend

  • Microsoft Windows 2000 or newer.

XCB backend

Font backends (required)

freetype font backend

Quartz-font backend

  • MacOS X >= 10.4 with Xcode >= 2.5

Windows GDI font backend

  • Microsoft Windows 2000 or newer

Windows DirectWrite font backend

  • Microsoft Windows 7 or newer

Compiling

See the INSTALL document for build instructions.

Licensing

Cairo is released under the terms of either the GNU Lesser General Public License version 2.1, or the terms of the Mozilla Public License version 1.1.

See the COPYING document for more information.

History

Cairo was originally developed by Carl Worth cworth@cworth.org and Keith Packard keithp@keithp.com. Many thanks are due to Lyle Ramshaw without whose patient help our ignorance would be much more apparent.

Since the original development, many more people have contributed to cairo. See the AUTHORS document for as complete a list as we've been able to compile so far.