[cairo] How do you get tight bounding boxes and/or unused margins cropped for final libcairo results?

Adrian Johnson ajohnson at redneon.com
Tue Sep 11 05:33:11 PDT 2007

Alan W. Irwin wrote:
> There are two distinct but closely related issues here.
> (1) Cropping of unused margins.
> How difficult would it be for each backend to implement a libcairo API that
> would allow outputting just a rectangular subset of the image?  This would
> allow users to trim off unused margins of a fully formed image just before
> it was output using the calculated global extent of the image without
> having
> to implement any replay tricks.

It would not be difficult however I don't think extending the API to
handle this particular case is a good idea. The better solution is like
Behdad said to make the meta surface public. Then once the meta surface
has been created and drawn on it would take 4 lines of code to get the
bounding box, create an output surface, translate, then replay.

> (2) Bounding box
> Of course, once such a desired rectangular subset was passed to the
> PostScript backend, the bounding box would have to be adjusted as well
> to be
> consistent with what you say about the PostScript standard above.  I
> understand both the PDF and SVG formats also support some sort of bounding
> box (a quick google search seems to indicate it should be called CropBox in
> the PDF case). Can your work on creating tight bounding boxes be
> generalized
> to those backends?

Extending the tight bounding box work to the PDF backend is easy. PDF
has a number of page boundary boxes: MediaBox, CropBox, BleedBox,
TrimBox, and ArtBox. MediaBox is the page size. According to the PDF
Reference when printing a finished page CropBox and TrimBox should be
the same as MediaBox. When placing the contents of the page in another
application, ArtBox determines the boundary of the content.

So it appears that ArtBox should be set to the tight bounding box and
the other boxes should be the same as MediaBox.

More information about the cairo mailing list