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

Vladimir Vukicevic vladimir at pobox.com
Mon Sep 10 11:26:29 PDT 2007


Hi Alan,

Alan W. Irwin wrote:
> 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
> below.
> 
> 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?

Since you just need to figure out the bounding box for the entire 
rendering, it seems like you could do that on the PLplot side -- that 
is, you know the extents of your rendering (plus a little extra 
measurement for text that might stick out of any of the four sides). 
Cairo can certainly help you do those measurements (e.g. via 
fill_extents and text metrics), but eventually your application code has 
to decide how big of a final surface to create.  How do you currently 
decide how big of a surface to create?

     - Vlad


More information about the cairo mailing list