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

Behdad Esfahbod behdad at behdad.org
Mon Sep 10 12:42:20 PDT 2007


On Mon, 2007-09-10 at 14:00 -0400, 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?

Hi, and welcome to cairo!

Currently cairo surfaces need to know the size at surface creation time.
A common approach to fixing this chicken-egg problem is that
applications first create a 1x1 (or 0x0) dummy surface of the same type
as the final one, render to it and compute extents (using
cairo_stroke_extents(), cairo_fill_extents(),
pango_layout_get_extents(), etc), and then create the final surface at
the right size and render again.

I can't readily think of any generic solution to this.  For PS/PDF/SVG
we have cairo_[ps/pdf/svg]_surface_set_size() but the docs say they
should only be called before any drawing operations have been performed
on the current page.  We can look into fixing this such that they work
even after drawing to the page.  It shouldn't be *that* hard to do.  But
this approach doesn't work for raster backends, needless to say.

In the future we may make the cairo meta-surface public.  At that point
you can render to the meta-surface, measure, create final surface and
replay the meta surface to the final surface.



Regards,

behdad

> Alan
> __________________________
> 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
> (lbproject.sf.net).

-- 
behdad
http://behdad.org/

"Those who would give up Essential Liberty to purchase a little
 Temporary Safety, deserve neither Liberty nor Safety."
        -- Benjamin Franklin, 1759





More information about the cairo mailing list