Commit graph

589 commits

Author SHA1 Message Date
Adrian Johnson
f7c7bcb603 tags: allow links to page numbers not yet created
Previously, forward references were required to use named destinations.

This patch is based on the patch in #336 by Guillaume Ayoub <guillaume.ayoub@kozea.fr>
that converted all links to indirect objects written at the end of the document.

I have reworked the patch so that only forward references to future page numbers are
written as indirect objects. Backward references and named destinations remain as they
are. This is to minimize the number of objects written to the PDF file.

Fixes #336
2021-07-24 21:33:09 +09:30
Uli Schlachter
2fbd53a6b3 pdf: Properly pass on stdio write errors
cairo-pdf was silently ignoring write errors in
_cairo_pdf_surface_finish(). Any write errors that happened here ended
up setting a "status" variable, but the value in this variable was then
unused.

This commit fixes this bug by passing this error on to the caller.

Additionally, this also adds a test case for this behaviour based on
writing to /dev/full. This file is non-standard and thus the test first
checks that this file exists and is writable before trying to write to
it.

This bug was found based on a report from Knut Petersen [0].

[0]: https://lists.cairographics.org/archives/cairo/2021-July/029281.html

Signed-off-by: Uli Schlachter <psychon@znc.in>
2021-07-23 17:22:16 +02:00
Adrian Johnson
1c64d2635b Remove all gstate from _cairo_surface_tag
The gstate is not required and was causing some tag operations to be
ignored.
2021-07-18 18:13:10 +09:30
Uli Schlachter
a3b69a0215 pdf font subset: Generate valid font names
A hash value is encoded in base 26 with upper case letters for font
names.

Commit ed984146 replaced "numerator = abs (hash);" with "numerator =
hash;" in this code, because hash has type uint32_t and the compiler
warned about taking the absolute value of an unsigned value.  However,
abs() is actually defined to take an int argument. Thus, there was some
implicit cast.

Since numerator has type long, i.e. is signed, it is now actually
possible to get an overflow in the implicit cast and then have a
negative number. The following code is not prepared for this and
produces non-letters when encoding the hash.

This commit fixes that problem by not using ldiv() and instead using /
and % to directly compute the needed values. This gets rid of the need
to convert to type long. Since now everything works with uint32_t, there
is no more chance for negative numbers messing things up.

Fixes: https://gitlab.freedesktop.org/cairo/cairo/-/issues/449
Signed-off-by: Uli Schlachter <psychon@znc.in>
2021-02-09 16:54:35 +01:00
Uli Schlachter
4e2e876be1 'Fix' a NULL pointer 'dereference' in cairo-pdf-surface
The expression &image_surface->base basically just casts the
cairo_image_surface_t* to cairo_surface_t*. However, technically it is a
NULL pointer dereference and UndefinedBehaviorSanitizer flags it as
such:

runtime error: member access within null pointer of type 'cairo_image_surface_t' (aka 'struct _cairo_image_surface')

This commit fixes this by adding a NULL check.

Signed-off-by: Uli Schlachter <psychon@znc.in>
2021-01-17 20:59:58 +01:00
Félix Poisot
8913b6e2fc The array introduced in bff47b43 isn't cleared on surface finish 2020-11-21 11:58:59 +00:00
George Matsumura
ed98414686 build: Fix various compiler warnings
This fixes a few compiler warnings that were encountered with gcc 9.3.0.

Signed-off-by: George Matsumura <gmmatsumura01@bvsd.org>
2020-11-07 06:45:01 -07:00
Adrian Johnson
4c8813f0ea pdf: add missing flush
Issue #342
2018-10-25 18:46:17 +10:30
Bryce Harrington
bf597b8928 Revert "Correctly decode Adobe CMYK JPEGs in PDF export"
From further testing and investigation it appears that many PDF viewers
already have a workaround to invert Adobe CMYK JPEGs, so our generated
PDFs display incorrectly with those viewers due to double-inversion.

Further investigation will be needed to find a better solution that
doesn't cause regression for some PDF viewers; perhaps PDF viewers that
lack this inversion workaround should be changed to include it.  For now
we'll drop the patch to avoid shipping the regression in 1.16.0.

This reverts commit b207a932a2.

Reference: https://bugs.freedesktop.org/show_bug.cgi?id=97612
Fixes: https://gitlab.freedesktop.org/cairo/cairo/issues/156
2018-10-16 09:53:00 -07:00
Bryce Harrington
9b0355a591 pdf: Fix potential null ptr deref when creating smask groups (CID #1159559)
Patch 37a22669 improved performance by using bounding box extents.
However, the code appears to be incorrect.  If extents is non-NULL it
copies its contents to group->extents, otherwise it sets group->extents
to sensible defaults, but then goes ahead and tries to copy the
undefined contents.  This second copy is unnecessary if extents is
non-NULL and will cause a crash if it is NULL.

Drop the extra copy, guessing it's just a typo.

Coverity ID: #1159559
Signed-off-by: Bryce Harrington <bryce@bryceharrington.org>
Reviewed-By: Uli Schlachter <psychon@znc.in>
2018-06-13 15:21:50 -07:00
Adrian Johnson
1998239387 Use _cairo_malloc instead of malloc
_cairo_malloc(0) always returns NULL, but has not been used
consistently.  This patch replaces many calls to malloc() with
_cairo_malloc().

Fixes:  fdo# 101547
CVE: CVE-2017-9814 Heap buffer overflow at cairo-truetype-subset.c:1299
Reviewed-by: Bryce Harrington <bryce@osg.samsung.com>
2018-05-07 16:35:51 -07:00
Unknown
12cb59be7d Cairo trivial typos
Found using `codespell -q 3 -I cairo-whitelist.txt`
whereby whitelist contained:
```
amin
iff
lod
writen
```

Reviewed-by: Bryce Harrington <bryce@osg.samsung.com>
2018-04-02 17:46:58 -07:00
Adrian Johnson
a709197121 pdf: fix some annotation bugs
- each annotation was emitted on every page instead of just the page
  that contains the annotation

- the document structure did not correctly link to annotation objects

- fix some annotation related memory leaks
2017-11-26 13:03:18 +10:30
Adrian Johnson
d5cb45013b pdf: fix mime-unique-id jpeg attached to recording test
- Restructure the emit_surface code so that mime types are checked first.

- Add a test parameter to emit_surface to test if the surface will be emitted
  as an image or recording instead checking the surface type as the attached
  mime may override this.

- Mark surface as not clear when mime is attached to avoid optimizing away
  "clear" surfaces that have mime attached.

- Include entire surface in analysis if mime attached (also fixes bug with
  calculating the extents CONTENT_COLOR surfaces)
2017-11-09 22:01:40 +10:30
Adrian Johnson
bff47b43c4 pdf: fix mime-unique-id unbounded recording test
PDF XObjects need to specify the bounding box. Emit unbounded surfaces
when finishing as at this point the extents of all uses of the
unbounded surface are known.
2017-11-09 20:52:36 +10:30
Adrian Johnson
cf9a070352 pdf: fix mime-unique-id bounded recording test
The embedded bounded recording surface was being clipped to the
extents of its first use.
2017-11-09 20:52:36 +10:30
Adrian Johnson
3322580f04 pdf: remove old comment
These issues have been fixed.
2017-10-24 21:44:08 +10:30
Adrian Johnson
1674d2b885 pdf: set default create date 2017-10-24 21:44:08 +10:30
Adrian Johnson
378e8e2f59 pdf: set ca/CA instead of using an smask when the mask has constant alpha 2017-10-24 07:01:14 +10:30
Adrian Johnson
87dfd0c16f Allow mime image to be different size to cairo image
Previously it was assumed the mime image size is the same as the cairo
image surface size. When using the 1 bpp formats (CCITT, JBIG2),
creating a cairo image of the same size will have very large memory
requirements and in some case may exceed the pixman image size
limits. In these cases it is useful to allow the mime image to have a
different resolution to the cairo image and in the PDF/PS output scale
the mime image to be the same physical size as the cairo image.

In PDF, this is easy as all PDF images are scaled to 1x1 unit and the
CTM is used to scale the image to the required size. The PS surface
has been changed to also scale images to 1x1 and use the CTM to get
the required size.
2017-10-22 08:59:58 +10:30
Adrian Johnson
e1a02b180d Add CCITT_FAX mime type for PDF and PS surfaces
This completes the full set of PDF/PS image filters allowing image
data to be passed though without decompressing then recompresssing in
a less efficient format.

The difficulty with CCITT_FAX is it needs some decoding parameters
that are not stored inside the image data. This is achieved by using
an additional mime type CCITT_FAX_PARAMS that contains the params in
key=value format.
2017-10-22 08:42:36 +10:30
Adrian Johnson
1220e3c6b8 replace _BSD_SOURCE with _DEFAULT_SOURCE
fixes the warning:

warning: #warning "_BSD_SOURCE and _SVID_SOURCE are deprecated, use _DEFAULT_SOURCE"
2017-09-16 10:04:57 +09:30
Adrian Johnson
12b875aef3 pdf: use link attributes instead of dest name for cairo_pdf_surface_add_outline
In PDF outline targets are specified the same way as link targets so
there is no need to restrict the target to dest names.
2017-08-26 16:32:48 +09:30
Adrian Johnson
df37baf789 pdf: fix link positions
Converting the link position from cairo to pdf coordinates requires
the page height. Since the link may point to a different page, build
an array of the height of each page and use the target page height for
the conversion.

Don't default to a [0,0] position if "pos" is not specified. PDF
allows a null destination position to be specified which means don't
change the position if the page is already displayed or show top left
if switching to a different page. This is more useful default
particularly for external files where the coordinates (which must be
in PDF coordinates as we don't know the page height) of the top left
corner may not be known.
2017-08-26 16:26:12 +09:30
Adrian Johnson
2d6a0f5d16 pdf: thumbnail API 2016-10-01 22:46:49 +09:30
Adrian Johnson
26b3f83ff6 pdf: page label API 2016-10-01 22:44:22 +09:30
Adrian Johnson
5bfadd5530 pdf: metadata API 2016-10-01 22:41:36 +09:30
Adrian Johnson
dfc7b9e669 pdf: add document outline API 2016-10-01 22:28:02 +09:30
Adrian Johnson
dcbfb72647 pdf: structured text and hyperlink support 2016-10-01 22:05:42 +09:30
Adrian Johnson
25da407a5f Support tag operations in analysis and paginated surface 2016-10-01 17:30:13 +09:30
Peter TB Brett
b207a932a2 Correctly decode Adobe CMYK JPEGs in PDF export
Adobe PhotoShop generates CMYK JPEG files with inverted CMYK.  When a
JPEG file with this format is included in a PDF file, a `/Decode`
array must be included to convert to "normal" CMYK.

These JPEG files can be detected via the presence of the APP14 "Adobe"
marker.  However, PDF viewers are not required to detect and handle
this private marker, so it must be detected and handled (by adding a
`/Decode`) by the PDF generator.

Signed-Off-By: Peter TB Brett <peter.brett@livecode.com>
2016-09-09 22:35:55 +09:30
Adrian Johnson
56ad58a807 pdf: fix combined image/smask
a736fd8 introduced a regression
2016-07-19 22:42:53 +09:30
Adrian Johnson
16a8c13b6a pdf: Don't fail subsetting if unable to convert utf8 to utf16
If the unicode came from the font, don't fail if utf8_to_utf16 fails.
2016-07-17 21:19:37 +09:30
Adrian Johnson
1a380ef5f3 ps/pdf: remove debug and commented out code 2016-07-15 22:03:04 +09:30
Adrian Johnson
1ada65c34f pdf: remove unused variable 2016-07-03 10:39:08 +09:30
Adrian Johnson
1c5ec6e380 Fix PS record-neg-extents test failure 2016-06-05 20:43:36 +09:30
Adrian Johnson
14fa88fd02 pdf: fix record-replay-extend test failures 2016-06-05 20:43:36 +09:30
Adrian Johnson
a736fd8699 Fix PDF record-neg-extents test failure
Modify PDF surface to allow surface extents to have negative x, y.
When emitting recording surfaces, set the surface extents to the
recording extents.
2016-06-05 20:43:36 +09:30
Adrian Johnson
1e07ced66d pdf: change from pdf coordinates to cairo coordinates
When an unbounded recording surface is used multiple times with
different extents for each operation we need the XObject containing
the recording surface to have the same origin for each operation. This
is not possible when the recording surface is converted to PDF
coordinates because each operation has different extents resulting in
a different origin when the Y-axis is flipped (since the flip matrix
depends on the recording surface height which for unbounded surfaces
depends on the extents of the operation that paints the recording
surface).

Switching to cairo coordinates by emitting a Y-axis flip matrix as the
first object of each page allows the recording surface to be emitted
in cairo coordinates. This results in the same origin for all
operations using the recording surface XObject.
2016-06-05 20:43:36 +09:30
Adrian Johnson
b1192beac7 Don't cull very thin lines on vector surfaces
On vector surfaces, use a minimum line width when calculating extents.

Bug 77298
2015-10-17 18:39:18 +10:30
Adrian Johnson
094f0e0fa0 pdf: fix subsurface recordings 2015-10-17 18:16:33 +10:30
Bryce Harrington
2cf2d8e340 Fix spellings descibed, indicies, stange 2015-04-04 15:49:18 -07:00
Adrian Johnson
e4b78424ac pdf-operators: only wrap text strings for PS output
since the PS Document Structing Conventions impose a 255 character
line limit. PDF does not require wrapping.

pdf-operators is designed to emit the same output for PS and PDF.
Unfortunately some PDF interpreters don't like strings split with
'\\\n' and some PS interpreters don't like strings split with ')('.
So we are forced to make pdf-operators handling string wrapping
differently for PDF and PS.

Bug 85662
2014-10-31 19:26:05 +10:30
Adrian Johnson
1013d18a01 pdf: fix compiler warning 2014-10-19 12:08:55 +10:30
Sylvestre Ledru
247cfaaa25 Fix some memory leaks found by scan-build, the LLVM/Clang static analyzer
Signed-off-by: Bryce Harrington <b.harrington@samsung.com>
Reviewed-by: Uli Schlachter <psychon@znc.in>
2014-05-16 11:11:34 -07:00
Adrian Johnson
dcbe16eb40 pdf/ps: avoid outputting excess decimal places in matrices
Sometimes as a result of rounding errors in matrix transformations the
matrices in ps/pdf output look like:

    0.000000000000000061 1 1 -0.000000000000000061 0 842 cm

This patch rounds to zero matrix elements that are very small compared to
other elements in the same matrix.
2013-12-07 15:54:49 +10:30
Adrian Johnson
5e6e453671 pdf: stencil masks may be opaque 2013-09-29 22:23:31 +09:30
Adrian Johnson
d3a8d5a9c2 pdf: Support stencil masks with jpeg/jpx/jbig2 embedding 2013-09-29 22:18:06 +09:30
Adrian Johnson
07940ecd60 pdf: combine source and mask images into single image
Since PDF stores the alpha component of images in a separate stream
which can have a different resolution and encoding to the color
components we can optimize the case where the source and mask are both images.

If the source and mask are both images with the same extents and the
source is opaque, combine the images into a single PDF image (ie the
source is written the the image stream and the mask is written to the
smask stream).
2013-09-29 13:08:02 +09:30
Adrian Johnson
5c0caa6f82 pdf: support JBIG2 mime data
JBIG2 images may have shared global data that is stored in a separate
stream in PDF. The CAIRO_MIME_TYPE_JBIG2 mime type is for the JBIG2
data for each image. All images that use global data must also set
CAIRO_MIME_TYPE_JBIG2_GLOBAL_ID to a unique identifier. One of the
images must also set CAIRO_MIME_TYPE_JBIG2_GLOBAL to the global
data. The global data will be shared by all JBIG2 images with the same
CAIRO_MIME_TYPE_JBIG2_GLOBAL_ID.
2013-09-15 20:50:46 +09:30