1 # -*- coding: ISO-8859-1 -*-
4 # Copyright (C) 2006, 2007 André Wobst <wobsta@users.sourceforge.net>
6 # This file is part of PyX (http://pyx.sourceforge.net/).
8 # PyX is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # PyX is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with PyX; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 # Just a quick'n'dirty ascii art (I'll do a nice PyX plot later on):
31 # neighbor3 |element * node3
39 import struct
, binascii
, zlib
40 import bbox
, canvas
, color
, pdfwriter
, unit
45 def __init__(self
, coords_pt
, value
):
46 self
.coords_pt
= coords_pt
52 def __init__(self
, coords
, value
):
53 node_pt
.__init
__(self
, [unit
.topt(coord
) for coord
in coords
], value
)
58 def __init__(self
, nodes
, neighbors
=None):
60 self
.neighbors
= neighbors
63 def coords24bit_pt(coords_pt
, min_pt
, max_pt
):
64 return struct
.pack(">I", int((coords_pt
-min_pt
)*16777215.0/(max_pt
-min_pt
)))[1:]
67 class PDFGenericResource(pdfwriter
.PDFobject
):
69 def __init__(self
, type, name
, content
):
70 pdfwriter
.PDFobject
.__init
__(self
, type, name
)
71 self
.content
= content
73 def write(self
, file, writer
, registry
):
74 file.write(self
.content
)
77 class mesh(canvas
.canvasitem
):
79 def __init__(self
, elements
, check
=1):
80 self
.elements
= elements
83 for element
in elements
:
84 if len(element
.nodes
) != 3:
85 raise ValueError("triangular mesh expected")
87 for node
in element
.nodes
:
88 if not colorspacestring
:
89 colorspacestring
= node
.value
.colorspacestring()
90 elif node
.value
.colorspacestring() != colorspacestring
:
91 raise ValueError("color space mismatch")
92 except AttributeError:
93 raise ValueError("gray, rgb or cmyk color values expected")
94 for node
in element
.nodes
:
95 if len(node
.coords_pt
) != 2:
96 raise ValueError("two dimensional coordinates expected")
99 return bbox
.bbox_pt(min([node
.coords_pt
[0] for element
in self
.elements
for node
in element
.nodes
]),
100 min([node
.coords_pt
[1] for element
in self
.elements
for node
in element
.nodes
]),
101 max([node
.coords_pt
[0] for element
in self
.elements
for node
in element
.nodes
]),
102 max([node
.coords_pt
[1] for element
in self
.elements
for node
in element
.nodes
]))
104 def data(self
, bbox
):
105 return "".join(["\000%s%s%s" % (coords24bit_pt(node
.coords_pt
[0], bbox
.llx_pt
, bbox
.urx_pt
),
106 coords24bit_pt(node
.coords_pt
[1], bbox
.lly_pt
, bbox
.ury_pt
),
107 node
.value
.tostring8bit())
108 for element
in self
.elements
for node
in element
.nodes
])
110 def processPS(self
, file, writer
, context
, registry
, bbox
):
111 thisbbox
= self
.bbox()
113 file.write("""<< /ShadingType 4
115 /BitsPerCoordinate 24
118 /Decode [%f %f %f %f %s]
119 /DataSource currentfile /ASCIIHexDecode filter /FlateDecode filter
120 >> shfill\n""" % (self
.elements
[0].nodes
[0].value
.colorspacestring(),
121 thisbbox
.llx_pt
, thisbbox
.urx_pt
, thisbbox
.lly_pt
, thisbbox
.ury_pt
,
122 " ".join(["0 1" for value
in self
.elements
[0].nodes
[0].value
.tostring8bit()])))
123 file.write(binascii
.b2a_hex(zlib
.compress(self
.data(thisbbox
))))
126 def processPDF(self
, file, writer
, context
, registry
, bbox
):
127 thisbbox
= self
.bbox()
129 d
= self
.data(thisbbox
)
131 filter = "/Filter /FlateDecode\n"
135 name
= "shading-%s" % id(self
)
136 shading
= PDFGenericResource("shading", name
, """<< /ShadingType 4
138 /BitsPerCoordinate 24
141 /Decode [%f %f %f %f %s]
146 endstream\n""" % (self
.elements
[0].nodes
[0].value
.colorspacestring(),
147 thisbbox
.llx_pt
, thisbbox
.urx_pt
, thisbbox
.lly_pt
, thisbbox
.ury_pt
,
148 " ".join(["0 1" for value
in self
.elements
[0].nodes
[0].value
.tostring8bit()]),
150 registry
.add(shading
)
151 registry
.addresource("Shading", name
, shading
)
152 file.write("/%s sh\n" % name
)