jpeg reader on bytes
[PyX.git] / graph / key.py
blobf1ee0eb7722cb71686fe05e56455d9c855214707
1 # -*- encoding: utf-8 -*-
4 # Copyright (C) 2002-2004 Jörg Lehmann <joergl@users.sourceforge.net>
5 # Copyright (C) 2003-2004 Michael Schindler <m-schindler@users.sourceforge.net>
6 # Copyright (C) 2002-2011 André Wobst <wobsta@users.sourceforge.net>
8 # This file is part of PyX (http://pyx.sourceforge.net/).
10 # PyX is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # PyX is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with PyX; if not, write to the Free Software
22 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 from pyx import box, canvas, text, trafo, unit
28 class key:
30 defaulttextattrs = [text.vshift.mathaxis]
32 def __init__(self, dist=0.2*unit.v_cm, pos="tr", hpos=None, vpos=None,
33 hinside=1, vinside=1, hdist=0.6*unit.v_cm, vdist=0.4*unit.v_cm,
34 symbolwidth=0.5*unit.v_cm, symbolheight=0.25*unit.v_cm, symbolspace=0.2*unit.v_cm,
35 textattrs=[], columns=1, columndist=0.5*unit.v_cm,
36 border=0.3*unit.v_cm, keyattrs=None):
37 self.dist = dist
38 self.hinside = hinside
39 self.vinside = vinside
40 self.hdist = hdist
41 self.vdist = vdist
42 self.symbolwidth = symbolwidth
43 self.symbolheight = symbolheight
44 self.symbolspace = symbolspace
45 self.textattrs = textattrs
46 self.columns = columns
47 self.columndist = columndist
48 self.border = border
49 self.keyattrs = keyattrs
50 if pos is not None:
51 if vpos is not None or hpos is not None:
52 raise ValueError("either specify pos or a combination of hpos, vpos")
53 for poslist, hpos, vpos in [(["tr", "rt"], 1, 1),
54 (["tc", "ct"], 0.5, 1),
55 (["tl", "lt"], 0, 1),
56 (["mr", "rm"], 1, 0.5),
57 (["mc", "cm"], 0.5, 0.5),
58 (["ml", "lm"], 0, 0.5),
59 (["br", "rb"], 1, 0),
60 (["bc", "cb"], 0.5, 0),
61 (["bl", "lb"], 0, 0)]:
62 if pos in poslist:
63 self.hpos = hpos
64 self.vpos = vpos
65 break
66 else:
67 raise ValueError("invalid pos")
68 else:
69 if vpos is None or hpos is None:
70 raise ValueError("either specify pos or a combination of hpos, vpos")
71 self.hpos = hpos
72 self.vpos = vpos
74 def paintcolumn(self, plotitems):
75 "creates the layout of a key column"
76 c = canvas.canvas()
77 self.dist_pt = unit.topt(self.dist)
78 self.hdist_pt = unit.topt(self.hdist)
79 self.vdist_pt = unit.topt(self.vdist)
80 self.symbolwidth_pt = unit.topt(self.symbolwidth)
81 self.symbolheight_pt = unit.topt(self.symbolheight)
82 self.symbolspace_pt = unit.topt(self.symbolspace)
83 titleboxes = []
84 for plotitem in plotitems:
85 titlebox = c.texrunner.text_pt(0, 0, plotitem.title, self.defaulttextattrs + self.textattrs)
86 titlebox.plotitem = plotitem
87 titleboxes.append(titlebox)
88 dy_pt = box.tile_pt(titleboxes, self.dist_pt, 0, -1)
89 box.linealignequal_pt(titleboxes, self.symbolwidth_pt + self.symbolspace_pt, 1, 0)
90 y_pt = -0.5 * self.symbolheight_pt + titleboxes[0].center[1]
91 for titlebox in titleboxes:
92 titlebox.plotitem.key_pt(c, 0, y_pt, self.symbolwidth_pt, self.symbolheight_pt)
93 y_pt -= dy_pt
94 for titlebox in titleboxes:
95 c.insert(titlebox)
96 return c
98 def paint(self, plotitems):
99 "creates the layout of the key"
100 columndist_pt = unit.topt(self.columndist)
101 c = canvas.canvas()
102 itemspercolumn = (len(plotitems) + self.columns - 1) // self.columns
103 x_pt = 0
104 while plotitems:
105 subc = self.paintcolumn(plotitems[:itemspercolumn])
106 c.insert(subc, [trafo.translate_pt(x_pt, 0)])
107 x_pt += unit.topt(subc.bbox().width()) + columndist_pt
108 del plotitems[:itemspercolumn]
109 if self.keyattrs is not None:
110 newc = canvas.canvas()
111 newc.draw(c.bbox().enlarged(self.border).path(), self.keyattrs)
112 newc.insert(c)
113 c = newc
114 return c