Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a00fc84

Browse files
author
Steve Chaplin
committed
SC 03/11/2004
svn path=/trunk/matplotlib/; revision=655
1 parent bcb98e4 commit a00fc84

1 file changed

Lines changed: 18 additions & 96 deletions

File tree

lib/matplotlib/backends/backend_cairo.py

Lines changed: 18 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -18,73 +18,12 @@
1818
Requires (in order, all available from Cairo website):
1919
libpixman, cairo, libsvg, libsvg-cairo, pycairo
2020
21+
pycairo from Nov 02 2004 onwards is required
22+
2123
Naming Conventions
2224
* classes MixedUpperCase
2325
* varables lowerUpper
2426
* functions underscore_separated
25-
26-
27-
backend_cairo requires Cairo functions that are not (yet?) wrapped by pycairo
28-
29-
/* Add the following to pycairo/cairo/pycairo-context.c */
30-
static PyObject *
31-
pycairo_set_target_png(PyCairoContext *self, PyObject *args)
32-
{
33-
PyObject *file_object;
34-
cairo_format_t format;
35-
int width, height;
36-
37-
if (!PyArg_ParseTuple(args, "O!iii:Context.set_target_png",
38-
&PyFile_Type, &file_object, &format, &width,
39-
&height))
40-
return NULL;
41-
42-
cairo_set_target_png(self->ctx, PyFile_AsFile(file_object), format,
43-
width, height);
44-
if (pycairo_check_status(cairo_status(self->ctx)))
45-
return NULL;
46-
47-
Py_INCREF(Py_None);
48-
return Py_None;
49-
}
50-
51-
static PyObject *
52-
pycairo_set_target_ps(PyCairoContext *self, PyObject *args)
53-
{
54-
PyObject *file_object;
55-
double width_inches, height_inches;
56-
double x_pixels_per_inch, y_pixels_per_inch;
57-
58-
if (!PyArg_ParseTuple(args, "O!dddd:Context.set_target_ps",
59-
&PyFile_Type, &file_object,
60-
&width_inches, &height_inches,
61-
&x_pixels_per_inch, &y_pixels_per_inch))
62-
return NULL;
63-
64-
cairo_set_target_ps(self->ctx, PyFile_AsFile(file_object),
65-
width_inches, height_inches,
66-
x_pixels_per_inch, y_pixels_per_inch);
67-
if (pycairo_check_status(cairo_status(self->ctx)))
68-
return NULL;
69-
70-
Py_INCREF(Py_None);
71-
return Py_None;
72-
}
73-
74-
static PyObject *
75-
pycairo_show_page(PyCairoContext *self)
76-
{
77-
cairo_show_page(self->ctx);
78-
if (pycairo_check_status(cairo_status(self->ctx)))
79-
return NULL;
80-
Py_INCREF(Py_None);
81-
return Py_None;
82-
}
83-
84-
/* Add to static PyMethodDef pycairo_methods[] = { */
85-
{ "set_target_png", (PyCFunction)pycairo_set_target_png, METH_VARARGS },
86-
{ "set_target_ps", (PyCFunction)pycairo_set_target_ps, METH_VARARGS },
87-
{ "show_page", (PyCFunction)pycairo_show_page, METH_NOARGS },
8827
"""
8928

9029
from __future__ import division
@@ -550,6 +489,8 @@ def print_figure_fn(figure, filename, dpi=150, facecolor='w', edgecolor='w',
550489
"""
551490
if DEBUG: print 'backend_cairo.FigureCanvasCairo.%s()' % _fn_name()
552491

492+
# TODO: add support for filename being a fileObject - like agg
493+
553494
root, ext = os.path.splitext(filename)
554495
ext = ext[1:]
555496
if ext == '':
@@ -575,7 +516,7 @@ def print_figure_fn(figure, filename, dpi=150, facecolor='w', edgecolor='w',
575516
verbose.report_error("%s: %s" % (exc.filename, exc.strerror))
576517
else:
577518
if ext == 'png': _save_png (figure, fileObject)
578-
else: _save_ps (figure, fileObject)
519+
else: _save_ps (figure, fileObject, orientation)
579520

580521
elif ext in ('eps', 'svg'): # backend_svg, later - use Cairo to write svg?
581522
if ext == 'eps':
@@ -593,8 +534,6 @@ def print_figure_fn(figure, filename, dpi=150, facecolor='w', edgecolor='w',
593534
figure.dpi.set(origDPI)
594535
figure.set_facecolor(origfacecolor)
595536
figure.set_edgecolor(origedgecolor)
596-
print 'filename:', filename, 'finished'
597-
598537

599538

600539
def _save_png (figure, fileObject):
@@ -608,36 +547,23 @@ def _save_png (figure, fileObject):
608547
width, height, figure.dpi)
609548
figure.draw(renderer)
610549
ctx.show_page()
611-
fileObject.close()
612550

613551

614-
def _save_ps (figure, fileObject):
552+
def _save_ps (figure, fileObject, orientation):
615553
# Cairo produces PostScript Level 3
616554
# 'ggv' can't read cairo ps files, but 'gv' can
617-
dpi = 72.0 # ignore the passed dpi setting for PS
618-
page_w_in, page_h_in = defaultPaperSize = 8.5, 11
619-
page_w, page_h = page_w_in * dpi, page_h_in * dpi
620-
621-
figure.dpi.set (dpi)
622-
l,b, width, height = figure.bbox.get_bounds()
623-
tx = (page_w - width) / 2.0
624-
ty = (page_h - height) / 2.0
625-
555+
556+
ppi = 300.0
557+
#figure.dpi.set(72) # this upsets the figure sizing
558+
559+
w_in, h_in = figure.get_size_inches()
560+
width, height = figure.get_width_height()
561+
626562
ctx = cairo.Context()
627-
ctx.set_target_ps (fileObject, page_w_in, page_h_in, dpi, dpi)
563+
ctx.set_target_ps (fileObject, w_in, h_in, ppi, ppi)
628564

629565
orientation = 'portrait' # landscape not supported yet
630-
if orientation == 'portrait': # default orientation
631-
# lines look a little jagged - just 'gv', cairo problem, or problem
632-
# with the way cairo is used?
633-
634-
# center the figure on the page
635-
matrix = cairo.Matrix (tx=tx, ty=ty)
636-
ctx.set_matrix (matrix)
637-
# TODO? scale figure to maximize yet leave space for margin/border round page?
638-
# the figure already has a 'margin', so could use full page width?
639-
640-
else: # landscape
566+
if orientation == 'landscape':
641567
# cairo/src/cairo_ps_surface.c
642568
# '%%Orientation: Portrait' is always written to the file header
643569
# '%%Orientation: Landscape' would possibly cause problems
@@ -651,22 +577,18 @@ def _save_ps (figure, fileObject):
651577
renderer = RendererCairo (ctx.target_surface, ctx.matrix, width, height, figure.dpi)
652578
figure.draw(renderer)
653579

654-
show_fig_border = False # for testing figure orientation and scaling
580+
show_fig_border = True # for testing figure orientation and scaling
655581
if show_fig_border:
656-
print 'Page w,h:', page_w, page_h
657-
print 'Fig w,h:', width, height
658-
print 'dx,dy :', dx, dy
582+
ctx.new_path()
659583
ctx.rectangle(0, 0, width, height)
660-
ctx.set_line_width(1)
584+
ctx.set_line_width(4.0)
661585
ctx.set_rgb_color(1,0,0)
662586
ctx.stroke()
663587
ctx.move_to(30,30)
664588
ctx.select_font('sans-serif')
665589
ctx.scale_font(20)
666590
ctx.show_text('Origin corner')
667-
668591
ctx.show_page()
669-
fileObject.close()
670592

671593

672594
class FigureCanvasCairo(FigureCanvasBase):

0 commit comments

Comments
 (0)