| 1 |
import sys |
| 2 |
import os |
| 3 |
import re |
| 4 |
import shutil |
| 5 |
import logging |
| 6 |
try: |
| 7 |
from urllib import url2pathname |
| 8 |
except ImportError: |
| 9 |
from urllib.request import url2pathname |
| 10 |
from dbtexmf.core.error import signal_error |
| 11 |
from .commander import CommandRunner |
| 12 |
|
| 13 |
class ObjectFilter: |
| 14 |
""" |
| 15 |
Its purpose is to select some objects from a list according to specified |
| 16 |
criterions. It assumes that '*' applied to a criterion means 'any'. |
| 17 |
""" |
| 18 |
def __init__(self): |
| 19 |
pass |
| 20 |
|
| 21 |
def _re_multi_or_star(self, searched): |
| 22 |
if not(searched): |
| 23 |
searched = r"\w*" |
| 24 |
else: |
| 25 |
s = searched.split() |
| 26 |
#searched = "|".join(["(?<=[/ ])%s" % p for p in s]) |
| 27 |
searched = "|".join(["%s" % p for p in s]) |
| 28 |
searched += r"|\*" |
| 29 |
return "("+searched+")" |
| 30 |
|
| 31 |
def select(self, object_list, **filter_criterions): |
| 32 |
for criterion, value in filter_criterions.items(): |
| 33 |
filter_criterions[criterion] = self._re_multi_or_star(value) |
| 34 |
|
| 35 |
founds = [] |
| 36 |
for obj in object_list: |
| 37 |
object_criterions = obj.criterions() |
| 38 |
for criterion, re_expr in filter_criterions.items(): |
| 39 |
data = object_criterions.get(criterion, "") |
| 40 |
m = re.search(re_expr, data) |
| 41 |
#print "Lookup2:", criterion, re_expr, data, not(m is None) |
| 42 |
if not(m): break |
| 43 |
|
| 44 |
if m: founds.append(obj) |
| 45 |
#print "Lookup2: found %d" % len(founds) |
| 46 |
return founds |
| 47 |
|
| 48 |
|
| 49 |
class PoolManager: |
| 50 |
def __init__(self): |
| 51 |
self._used_pool = None |
| 52 |
self._pending_pools = [] |
| 53 |
|
| 54 |
def set_pool(self, pool): |
| 55 |
self._used_pool = pool |
| 56 |
for p in self._pending_pools: |
| 57 |
pool.preprend(p) |
| 58 |
self._pending_pools = [] |
| 59 |
|
| 60 |
def prepend_pool(self, pool): |
| 61 |
if self._used_pool: |
| 62 |
self._used_pool.prepend(pool) |
| 63 |
else: |
| 64 |
self._pending_pools.append(pool) |
| 65 |
|
| 66 |
class ImageSetup: |
| 67 |
""" |
| 68 |
Central imagedata setup, filled by default object configurations and |
| 69 |
by the XML configuration |
| 70 |
""" |
| 71 |
def __init__(self): |
| 72 |
self.converter_pool = PoolManager() |
| 73 |
self.format_pool = PoolManager() |
| 74 |
|
| 75 |
_image_setup = ImageSetup() |
| 76 |
|
| 77 |
def image_setup(): |
| 78 |
global _image_setup |
| 79 |
return _image_setup |
| 80 |
|
| 81 |
|
| 82 |
# |
| 83 |
# Objects to convert an image format to another. Actually use the underlying |
| 84 |
# tools. |
| 85 |
# |
| 86 |
class ImageConverter: |
| 87 |
_log = logging.getLogger("dblatex") |
| 88 |
|
| 89 |
def __init__(self, imgsrc, imgdst="", docformat="", backend=""): |
| 90 |
self.imgsrc = imgsrc |
| 91 |
self.imgdst = imgdst or "*" |
| 92 |
self.docformat = docformat or "*" |
| 93 |
self.backend = backend or "*" |
| 94 |
self.command = CommandRunner(log=self._log) |
| 95 |
|
| 96 |
def criterions(self): |
| 97 |
return { "imgsrc": self.imgsrc, |
| 98 |
"imgdst": self.imgdst, |
| 99 |
"docformat": self.docformat, |
| 100 |
"backend": self.backend } |
| 101 |
|
| 102 |
def add_command(self, *args, **kwargs): |
| 103 |
self.command.add_command(*args, **kwargs) |
| 104 |
|
| 105 |
def convert(self, input, output, format, doexec=1): |
| 106 |
rc = self.command.run(kw={"input": input, "output": output, |
| 107 |
"dst": format}) |
| 108 |
if rc != 0: signal_error(self, "") |
| 109 |
|
| 110 |
class ImageConverterPool: |
| 111 |
def __init__(self): |
| 112 |
self.converters = [] |
| 113 |
self._filter = ObjectFilter() |
| 114 |
|
| 115 |
def add_converter(self, converter): |
| 116 |
self.converters.append(converter) |
| 117 |
|
| 118 |
def extend(self, other): |
| 119 |
self.converters.extend(other.converters) |
| 120 |
|
| 121 |
def prepend(self, other): |
| 122 |
self.converters = other.converters + self.converters |
| 123 |
|
| 124 |
def get_converters(self, imgsrc="", imgdst="", docformat="", backend=""): |
| 125 |
founds = self._filter.select(self.converters, |
| 126 |
imgsrc=imgsrc, |
| 127 |
imgdst=imgdst, |
| 128 |
docformat=docformat, |
| 129 |
backend=backend) |
| 130 |
return founds |
| 131 |
|
| 132 |
|
| 133 |
class ImageConverters(ImageConverterPool): |
| 134 |
def __init__(self): |
| 135 |
ImageConverterPool.__init__(self) |
| 136 |
# Default setup |
| 137 |
self.add_converter(GifConverter("gif")) |
| 138 |
self.add_converter(EpsConverter("eps", "pdf")) |
| 139 |
self.add_converter(EpsConverter("eps", "png")) |
| 140 |
self.add_converter(FigConverter("fig", "pdf")) |
| 141 |
self.add_converter(FigConverter("fig", "png")) |
| 142 |
self.add_converter(SvgConverter("svg")) |
| 143 |
|
| 144 |
# Register as main pool |
| 145 |
image_setup().converter_pool.set_pool(self) |
| 146 |
|
| 147 |
|
| 148 |
class GifConverter(ImageConverter): |
| 149 |
def __init__(self, imgsrc, imgdst="", docformat="", backend=""): |
| 150 |
ImageConverter.__init__(self, imgsrc="gif bmp", imgdst="*") |
| 151 |
self.add_command(["convert", "%(input)s", "%(output)s"]) |
| 152 |
|
| 153 |
class EpsConverter(ImageConverter): |
| 154 |
def __init__(self, imgsrc, imgdst="", docformat="", backend=""): |
| 155 |
ImageConverter.__init__(self, imgsrc="eps", imgdst=imgdst) |
| 156 |
if imgdst == "pdf": |
| 157 |
self.add_command(["epstopdf", "--outfile=%(output)s", "%(input)s"], |
| 158 |
shell=True) |
| 159 |
elif imgdst == "png": |
| 160 |
self.add_command(["convert", "%(input)s", "%(output)s"]) |
| 161 |
|
| 162 |
class FigConverter(ImageConverter): |
| 163 |
def __init__(self, imgsrc, imgdst="", docformat="", backend=""): |
| 164 |
ImageConverter.__init__(self, imgsrc="fig", imgdst=imgdst) |
| 165 |
self.add_command(["fig2dev", "-L", "eps", "%(input)s"], |
| 166 |
stdout="%(output)s") |
| 167 |
if imgdst != "eps": |
| 168 |
self.conv_next = EpsConverter("eps", imgdst=imgdst) |
| 169 |
else: |
| 170 |
self.conv_next = None |
| 171 |
|
| 172 |
def convert(self, input, output, format): |
| 173 |
if self.conv_next: |
| 174 |
epsfile = "tmp_fig.eps" |
| 175 |
else: |
| 176 |
epsfile = output |
| 177 |
ImageConverter.convert(self, input, epsfile, "eps") |
| 178 |
if self.conv_next: |
| 179 |
self.conv_next.convert(epsfile, output, format) |
| 180 |
|
| 181 |
class SvgConverter(ImageConverter): |
| 182 |
def __init__(self, imgsrc, imgdst="", docformat="", backend=""): |
| 183 |
ImageConverter.__init__(self, imgsrc="svg", imgdst=imgdst) |
| •184 |
self.add_command(["inkscape", "-D", "--export-filename=%(output)s", |
| 185 |
"%(input)s"]) |
| 186 |
|
| 187 |
|
| 188 |
class FormatRule: |
| 189 |
def __init__(self, imgsrc="", imgdst="", docformat="", backend=""): |
| 190 |
self.imgsrc = imgsrc or "*" |
| 191 |
self.imgdst = imgdst or "*" |
| 192 |
self.docformat = docformat or "*" |
| 193 |
self.backend = backend or "*" |
| 194 |
|
| 195 |
def criterions(self): |
| 196 |
return { "imgsrc": self.imgsrc, |
| 197 |
"imgdst": self.imgdst, |
| 198 |
"docformat": self.docformat, |
| 199 |
"backend": self.backend } |
| 200 |
|
| 201 |
class ImageFormatPool: |
| 202 |
def __init__(self): |
| 203 |
self.rules = [] |
| 204 |
self._filter = ObjectFilter() |
| 205 |
|
| 206 |
def add_rule(self, rule): |
| 207 |
self.rules.append(rule) |
| 208 |
|
| 209 |
def prepend(self, other): |
| 210 |
self.rules = other.rules + self.rules |
| 211 |
|
| 212 |
def output_format(self, imgsrc="", docformat="", backend=""): |
| 213 |
founds = self._filter.select(self.rules, |
| 214 |
imgsrc=imgsrc, |
| 215 |
docformat=docformat, |
| 216 |
backend=backend) |
| 217 |
if founds: |
| 218 |
return founds[0].imgdst |
| 219 |
else: |
| 220 |
return "" |
| 221 |
|
| 222 |
class ImageFormatRuleset(ImageFormatPool): |
| 223 |
def __init__(self): |
| 224 |
ImageFormatPool.__init__(self) |
| 225 |
# There can be a mismatch between PDF-1.4 images and PDF-1.3 |
| 226 |
# document produced by XeTeX |
| 227 |
self.add_rule(FormatRule(docformat="pdf", backend="xetex", |
| 228 |
imgdst="png")) |
| 229 |
self.add_rule(FormatRule(docformat="pdf", imgdst="pdf")) |
| 230 |
self.add_rule(FormatRule(docformat="dvi", imgdst="eps")) |
| 231 |
self.add_rule(FormatRule(docformat="ps", imgdst="eps")) |
| 232 |
|
| 233 |
# Register as main pool |
| 234 |
image_setup().format_pool.set_pool(self) |
| 235 |
|
| 236 |
# |
| 237 |
# The Imagedata class handles all the image transformation |
| 238 |
# process, from the discovery of the actual image involved to |
| 239 |
# the conversion process. |
| 240 |
# |
| 241 |
class Imagedata: |
| 242 |
def __init__(self): |
| 243 |
self.paths = [] |
| 244 |
self.input_format = "png" |
| 245 |
self.output_format = "pdf" |
| 246 |
self.docformat = "pdf" |
| 247 |
self.backend = "" |
| 248 |
self.rules = ImageFormatRuleset() |
| 249 |
self.converters = ImageConverters() |
| 250 |
self.converted = {} |
| 251 |
self.log = logging.getLogger("dblatex") |
| 252 |
self.output_encoding = "" |
| 253 |
|
| 254 |
def set_encoding(self, output_encoding): |
| 255 |
self.output_encoding = output_encoding |
| 256 |
|
| 257 |
def set_format(self, docformat, backend): |
| 258 |
self.docformat = docformat |
| 259 |
self.backend = backend |
| 260 |
self.output_format = self.rules.output_format(docformat=docformat, |
| 261 |
backend=backend) |
| 262 |
|
| 263 |
def convert(self, fig): |
| 264 |
fig = fig.decode("utf-8") |
| 265 |
|
| 266 |
# Translate the URL to an actual local path |
| 267 |
fig = url2pathname(fig) |
| 268 |
|
| 269 |
# Always use '/' in path: work even on windows and is required by tex |
| 270 |
if os.path.sep != '/': fig = fig.replace(os.path.sep, '/') |
| 271 |
|
| 272 |
# First, scan the available formats |
| 273 |
(realfig, ext) = self.scanformat(fig) |
| 274 |
|
| 275 |
# No real file found, give up |
| 276 |
if not(realfig): |
| 277 |
self.log.warning("Image '%s' not found" % fig) |
| 278 |
return fig.encode("ascii") |
| 279 |
|
| 280 |
# Check if this image has been already converted |
| 281 |
if realfig in self.converted: |
| 282 |
self.log.info("Image '%s' already converted as %s" % \ |
| 283 |
(fig, self.converted[realfig])) |
| 284 |
return self.converted[realfig] |
| 285 |
|
| 286 |
# No format found, take the default one |
| 287 |
if not(ext): |
| 288 |
ext = self.input_format |
| 289 |
|
| 290 |
# Natively supported format? |
| 291 |
if (ext == self.output_format): |
| 292 |
return self._safe_file(fig, realfig, ext) |
| 293 |
|
| 294 |
# Try to convert |
| 295 |
count = len(self.converted) |
| 296 |
newfig = "fig%d.%s" % (count, self.output_format) |
| 297 |
|
| 298 |
conv = self.converters.get_converters(imgsrc=ext, |
| 299 |
imgdst=self.output_format, |
| 300 |
backend=self.backend) |
| 301 |
if not(conv): |
| 302 |
self.log.debug("Cannot convert '%s' to %s" % (fig, |
| 303 |
self.output_format)) |
| 304 |
# Unknown conversion to do, or nothing to do |
| 305 |
return self._safe_file(fig, realfig, ext) |
| 306 |
else: |
| 307 |
# Take the first converter that does the trick |
| 308 |
conv = conv[0] |
| 309 |
|
| 310 |
# Convert the image and put it in the cache |
| 311 |
conv.log = self.log |
| 312 |
conv.convert(realfig, newfig, self.output_format) |
| 313 |
self.converted[realfig] = newfig |
| 314 |
return self._path_encode(newfig) |
| 315 |
|
| 316 |
def _safe_file(self, fig, realfig, ext): |
| 317 |
""" |
| 318 |
Copy the file in the working directory if its path contains characters |
| 319 |
unsupported by latex, like spaces. |
| 320 |
""" |
| 321 |
# Encode to expected output format. If encoding is OK and |
| 322 |
# supported by tex, just return the encoded path |
| 323 |
newfig = self._path_encode(fig) |
| 324 |
if newfig and newfig.find(b" ") == -1: |
| 325 |
return newfig |
| 326 |
|
| 327 |
# Added to the converted list |
| 328 |
count = len(self.converted) |
| 329 |
newfig = "figcopy%d.%s" % (count, ext) |
| 330 |
self.converted[realfig] = newfig |
| 331 |
|
| 332 |
# Do the copy |
| 333 |
shutil.copyfile(realfig, newfig) |
| 334 |
return self._path_encode(newfig) |
| 335 |
|
| 336 |
def _path_encode(self, fig): |
| 337 |
# Actually, only ASCII characters are sure to match filesystem encoding |
| 338 |
# so let's be conservative |
| 339 |
if self.output_encoding == "utf-8": |
| 340 |
return fig.encode("utf-8") |
| 341 |
try: |
| 342 |
newfig = fig.encode("ascii") |
| 343 |
except: |
| 344 |
newfig = b"" |
| 345 |
return newfig |
| 346 |
|
| 347 |
def scanformat(self, fig): |
| 348 |
(root, ext) = os.path.splitext(fig) |
| 349 |
|
| 350 |
if (ext): |
| 351 |
realfig = self.find(fig) |
| 352 |
return (realfig, ext[1:]) |
| 353 |
|
| 354 |
# Lookup for the best suited available figure |
| 355 |
if (self.output_format == "pdf"): |
| 356 |
formats = ("png", "pdf", "jpg", "eps", "gif", "fig", "svg") |
| 357 |
else: |
| 358 |
formats = ("eps", "fig", "pdf", "png", "svg") |
| 359 |
|
| 360 |
for format in formats: |
| 361 |
realfig = self.find("%s.%s" % (fig, format)) |
| 362 |
if realfig: |
| 363 |
self.log.info("Found %s for '%s'" % (format, fig)) |
| 364 |
break |
| 365 |
|
| 366 |
# Maybe a figure with no extension |
| 367 |
if not(realfig): |
| 368 |
realfig = self.find(fig) |
| 369 |
format = "" |
| 370 |
|
| 371 |
return (realfig, format) |
| 372 |
|
| 373 |
def find(self, fig): |
| 374 |
# First, the obvious absolute path case |
| 375 |
if os.path.isabs(fig): |
| 376 |
if os.path.isfile(fig): |
| 377 |
return fig |
| 378 |
else: |
| 379 |
return None |
| 380 |
|
| 381 |
# Then, look for the file in known paths |
| 382 |
for path in self.paths: |
| 383 |
realfig = os.path.join(path, fig) |
| 384 |
if os.path.isfile(realfig): |
| 385 |
return realfig |
| 386 |
|
| 387 |
return None |
| 388 |
|