Hi,
Another proposal:
I'm using the following code when I need a precise Bounding Box -
letting ghostscript do the job:
def GetBB(c, filename="-", resolution=100,
gscommand="grep -v -E '^%%(HiRes)?BoundingBox' | gs", gsoptions="",
textalphabits=4, graphicsalphabits=4,
ciecolor=False, **kwargs):
gscommand += " -dEPSCrop -dNOPAUSE -dQUIET -dBATCH -r%i -q -sDEVICE=bbox
-sOutputFile=%s" % (resolution, filename)
if gsoptions:
gscommand += " %s" % gsoptions
if textalphabits is not None:
gscommand += " -dTextAlphaBits=%i" % textalphabits
if graphicsalphabits is not None:
gscommand += " -dGraphicsAlphaBits=%i" % graphicsalphabits
if ciecolor:
gscommand += " -dUseCIEColor"
gscommand += " -"
pipe_in, pipe_out, pipe_err = os.popen3(gscommand)
c.writeEPSfile(pipe_in, **kwargs)
pipe_in.close()
for line in pipe_err.readlines():
m=re.match(r"^%%HiResBoundingBox: ([-0-9\.]*) ([-0-9\.]*) ([0-9\.]*)
([0-9\.]*)$",line)
if m != None:
b = map(lambda x: unit.length(float(x),unit="pt"), m.groups())
return bbox.bbox(b[0],b[1],b[2],b[3])
raise IOError("Can't determine BoundingBox")
It's more or less a copy of "canvas.pipeGS", however:
- as ghostscript writes the bbox onto the standard error-stream, I need
"os.popen3" to get the result from within python.
- I use a trivial regexp to get the boundingboxes-Data
- I use "grep" to guarantee that pyx is not passing its
BoundingBox-Date to ghostscript.
With that code, I can pass an arbitrary canvas to "GetBB", and it will
return the precise boundingBox - including linewidth.
However, the huge problem is: my ghostscript gives wrong results for
negative coordinates - maybe it's just a question of the parameters?
Axel
On Sun, Oct 18, 2009 at 01:56:12PM +0200, Joerg Lehmann wrote:
> Hi,
>
> On 17.10.09, Alan G Isaac wrote:
> > On 10/17/2009 9:26 AM, Michael SCHINDLER wrote:
> > > The problem is that
> > > the true bounding box of a picture does depend on both the paths and
> > > the respective linewidth. It is simple to calculate bounding boxes for
> > > lines; for curves it is more difficult. PyX calculates these two.
> > > Including also the linewidth and stroke effects coming from the
> > > different linejoin parameters is really difficult. We decided not to
> > > do this, as it would lead to a reimplementation of ghostscript.
> >
> > Did you consider using Bless's bbox code (comes with ps2eps, GPL license)?
>
> I guess, you mean this code:
>
> http://www.tex.ac.uk/tex-archive/support/ps2eps/src/C/bbox.c
>
> Unfortunately, it requires having a rasterized version of the graphics
> output.
>
> > It would be really nice to get a better bounding box.
>
> From a perfectionists point of view, I would agree. Practically,
> however, it only very seldomnly happened that the bounding box produced
> by PyX was not good enough.
>
> Best,
>
> Jörg
------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
PyX-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pyx-user