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

Alan W. Irwin irwin at beluga.phys.uvic.ca
Mon Sep 10 11:00:49 PDT 2007

To give you some quick background, a set of devices (pscairo, pdfcairo,
pngcairo, xcairo, and svgcairo) that are based on libcairo/libpango has just
been developed for the PLplot scientific plotting library. This device
driver code was developed by another PLplot developer (Hazen Babcock). He is
fairly new to libcairo and I am completely new to libcairo so please excuse
any mistakes when I discuss the tight bounding-box/crop unused margins issue

Currently pscairo produces outstanding looking results both for graphics and
text so we are quite pleased with it.  However, there is one remaining issue
which is the loose bounding box in the generated PostScript file. The
postscript backend to libcairo simply sets that bounding box to the
width_in_points and height_in_points specified by the call to
cairo_ps_surface_create_for_stream. What we would like to do is modify this
device code so that PLplot users have the option of using an automatically
calculated tight bounding box corresponding to the maximum rectangular
extent of the graphics plus text in the plot. (Tight bounding boxes for the
devices that support bounding boxes or the corresponding removal of unused
margins for those devices that do not support bounding boxes are useful when
including plots in other documents.)

Skimming through the current cairo API documentation it appears there are a
number of different extent calls that would allow us to easily calculate the
maximum extent of the graphics plus text corresponding to a tight bounding
box.  So we could, for example, right after the postscript file is closed
have our device code run an ImageMagick command to crop the unused margins
from our pscairo PostScript result and similarly for the PDF, PNG, and SVG
results from our pdfcairo, pngcairo, and svgcairo devices. (N.B. crop here
is used in the ImageMagick sense where the actual size of the final image
and the bounding box for those formats that have a bounding box is reduced
by the cropping.) But that potential solution would not work with xcairo
results which would therefore have an inconsistent size with the others. 
Perhaps more seriously that potential solution would not work on PLplot
user's platforms without ImageMagick. Therefore, I hope there is a general
way to do this (crop [in the ImageMagick sense] unused margins from the
final result) internal to libcairo that works for some/all libcairo back ends.
If so, can somebody point me in the right direction?

Alan W. Irwin

Astronomical research affiliation with Department of Physics and Astronomy,
University of Victoria (astrowww.phys.uvic.ca).

Programming affiliations with the FreeEOS equation-of-state implementation
for stellar interiors (freeeos.sf.net); PLplot scientific plotting software
package (plplot.org); the libLASi project (unifont.org/lasi); the Loads of
Linux Links project (loll.sf.net); and the Linux Brochure Project

Linux-powered Science

More information about the cairo mailing list