~ubuntu-branches/ubuntu/trusty/dblatex/trusty

« back to all changes in this revision

Viewing changes to lib/dbtexmf/core/imagedata.py

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Hoenen
  • Date: 2006-11-18 09:52:18 UTC
  • mfrom: (3.1.2 feisty)
  • Revision ID: james.westby@ubuntu.com-20061118095218-zsibrzxr9w6e2lor
Tags: 0.2-2
Do not depend on script mktexlsr belonging to a non-essential package
in postrm purge. (Urgency set to high due to serious severity.)
Closes: #398776

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import sys
 
2
import os
 
3
import re
 
4
 
 
5
#
 
6
# Objects to convert an image format to another. Actually use the underlying
 
7
# tools.
 
8
#
 
9
class ImageConverter:
 
10
    def __init__(self):
 
11
        self.debug = 1
 
12
        self.fake = 0
 
13
 
 
14
    def system(self, cmd, doexec=1):
 
15
        if not(cmd):
 
16
            return ""
 
17
        if self.debug:
 
18
            print cmd
 
19
        if doexec:
 
20
            if not(self.fake):
 
21
                os.system(cmd)
 
22
        else:
 
23
            return cmd
 
24
 
 
25
    def convert(self, input, output, format, doexec=1):
 
26
        pass
 
27
 
 
28
 
 
29
class GifConverter(ImageConverter):
 
30
    def convert(self, input, output, format, doexec=1):
 
31
        cmd = "convert \"%s\" %s" % (input, output)
 
32
        return self.system(cmd, doexec)
 
33
 
 
34
class EpsConverter(ImageConverter):
 
35
    def convert(self, input, output, format, doexec=1):
 
36
        if format == "pdf":
 
37
            cmd = "epstopdf --outfile=%s \"%s\"" % (output, input)
 
38
        elif format == "png":
 
39
            cmd = "convert \"%s\" %s" % (input, output)
 
40
        else:
 
41
            cmd = ""
 
42
        return self.system(cmd, doexec)
 
43
 
 
44
class FigConverter(ImageConverter):
 
45
    def convert(self, input, output, format, doexec=1):
 
46
        if (format != "eps"):
 
47
            conv = EpsConverter()
 
48
            conv.fake = self.fake
 
49
            conv.debug = self.debug
 
50
            epsfile = "tmp_fig.eps"
 
51
            post = " && "
 
52
            post += conv.convert(epsfile, output, format, doexec=0)
 
53
        else:
 
54
            post = ""
 
55
            epsfile = output
 
56
 
 
57
        cmd = "fig2dev -L eps \"%s\" > %s" % (input, epsfile)
 
58
        cmd += post
 
59
        self.system(cmd)
 
60
 
 
61
 
 
62
#
 
63
# The Imagedata class handles all the image transformation
 
64
# process, from the discovery of the actual image involved to
 
65
# the conversion process.
 
66
#
 
67
class Imagedata:
 
68
    def __init__(self):
 
69
        self.paths = []
 
70
        self.input_format = "png"
 
71
        self.output_format = "pdf"
 
72
        self.converted = {}
 
73
 
 
74
    def convert(self, fig):
 
75
        # First, scan the available formats
 
76
        (realfig, ext) = self.scanformat(fig)
 
77
 
 
78
        # No real file found, give up
 
79
        if not(realfig):
 
80
            print "Image '%s' not found" % fig
 
81
            return fig
 
82
 
 
83
        # Check if this image has been already converted
 
84
        if self.converted.has_key(realfig):
 
85
            print "Image '%s' already converted as %s" % \
 
86
                  (fig, self.converted[realfig])
 
87
            return self.converted[realfig]
 
88
 
 
89
        # No format found, take the default one
 
90
        if not(ext):
 
91
            ext = self.input_format
 
92
 
 
93
        # Natively supported format?
 
94
        if (ext == self.output_format):
 
95
            return fig
 
96
 
 
97
        # Try to convert
 
98
        count = len(self.converted)
 
99
        newfig = "fig%d.%s" % (count, self.output_format)
 
100
 
 
101
        if (ext == "fig" and self.output_format in ("eps", "pdf", "png")):
 
102
            conv = FigConverter()
 
103
        elif (ext == "eps"):
 
104
            conv = EpsConverter()
 
105
        elif (ext in ("gif", "bmp")):
 
106
            conv = GifConverter()
 
107
        else:
 
108
            # Unknown conversion to do, or nothing to do
 
109
            return fig
 
110
 
 
111
        # Convert the image and put it in the cache
 
112
        conv.convert(realfig, newfig, self.output_format)
 
113
        self.converted[realfig] = newfig
 
114
        return newfig
 
115
 
 
116
    def scanformat(self, fig):
 
117
        (root, ext) = os.path.splitext(fig)
 
118
 
 
119
        if (ext):
 
120
            realfig = self.find(fig)
 
121
            return (realfig, ext[1:])
 
122
        
 
123
        # Lookup for the best suited available figure
 
124
        if (self.output_format == "pdf"):
 
125
            formats = ("png", "pdf", "jpg", "eps", "gif", "fig")
 
126
        else:
 
127
            formats = ("eps", "fig", "pdf", "png")
 
128
 
 
129
        for format in formats:
 
130
            realfig = self.find("%s.%s" % (fig, format))
 
131
            if realfig:
 
132
                print "Found %s for '%s'" % (format, fig)
 
133
                break
 
134
 
 
135
        # Maybe a figure with no extension
 
136
        if not(realfig):
 
137
            realfig = self.find(fig)
 
138
            format = ""
 
139
 
 
140
        return (realfig, format)
 
141
        
 
142
    def find(self, fig):
 
143
        # First, the obvious absolute path case
 
144
        if os.path.isabs(fig):
 
145
            if os.path.isfile(fig):
 
146
                return fig
 
147
            else:
 
148
                return None
 
149
 
 
150
        # Then, look for the file in known paths
 
151
        for path in self.paths:
 
152
            realfig = os.path.join(path, fig)
 
153
            if os.path.isfile(realfig):
 
154
                return realfig
 
155
 
 
156
        return None
 
157
       
 
158
    def system(self, cmd):
 
159
        print cmd
 
160
        rc = os.system(cmd)
 
161
        # TODO: raise error when system call failed
 
162