Files
klammertext/sks/image/image.py
T

108 lines
4.2 KiB
Python
Raw Normal View History

import sys
import os
_klammertext_home = os.environ.get('KLAMMERTEXT_HOME', os.path.expanduser('~/projects/klammertext/K'))
sys.path.append(os.path.join(_klammertext_home, 'sks/kutil'))
sys.path.append(os.path.join(_klammertext_home, 'sks/target'))
import re
import os
import shutil
import kutil
import html_util
import latex_util
import klammer_base
import image_cache
import pprint
from html_util import E
import sys
class Image(klammer_base.Klammer_base):
id = 0
def __init__(self, K, as_string=True, width=None):
super().__init__(K)
# /dev/shm is a fast RAM-backed tmpfs on Linux; on macOS it does not
# exist, so fall back to the platform temp directory.
cache_dir = "/dev/shm"
if not os.path.isdir(cache_dir):
import tempfile
cache_dir = tempfile.gettempdir()
self.cache = image_cache.Image_cache(cache_dir, self.Image_search_path, verbose=False,
quality=self.Image_quality)
self.basename = klammer_base.unescape_ktesc(self.basename)
self.source, self.pwidth, self.pheight = self.cache.get(self.K_target, self.basename)
self.as_string = as_string
if width:
self.width = width
self.rel_image = None
self.rel_fraction = 1.0
if self.rel:
self.rel_image, self.rel_pwidth, self.rel_pheight = self.cache.get(self.K_target, self.rel)
self.rel_fraction = self.pwidth / self.rel_pwidth
def html(self):
img_dir = f"{self.K_output_dir}/{self.K_output_basename}/{self.Image_output_dir}"
if not os.path.exists(img_dir):
os.makedirs(img_dir)
if not os.path.exists(f"{img_dir}/{os.path.basename(self.source)}"):
shutil.copy2(self.source, img_dir)
scale_x, scale_y = 1, 1 # Old
result = E("img")
if self.id:
result.attr("id", self.id)
filename = f"{self.Image_output_dir}/{os.path.basename(self.source)}"
result.attr("src", filename).attr("alt", os.path.basename(filename))
length, value, ltype = kutil.parse_width("html", self.width, self.rel_fraction)
if ltype == "w":
percent = float(value) * 100
result.sty(f"width: {percent}%")
elif ltype == "px":
# The html width ATTRIBUTE takes pixels only; every other unit
# is a style (an attribute of "2em" was silently ignored).
result.attr("width", length)
else:
result.sty(f"width: {length}")
if self.border:
result.cls("image_border")
result = html_util.add_caption(
result, "Figure", self.number, self.caption, self.caption_style,
self.hpos, self.caption_side, False, self.caption_size,
offset=self.offset)
result = E("div").body(result).cls("image_margin")
if self.as_string:
result = str(result) + "\n"
return result
def tex(self):
# Forward slashes: this path is document text (\includegraphics, an
# html src), where a Windows backslash is a TeX command or an escape.
source = os.path.abspath(self.source).replace(os.sep, "/")
width = kutil.parse_width("tex", self.width, self.rel_fraction)[0]
width = re.sub("px", "pt", width)
#result = f'\\includegraphics[width={width}]{{{source}}}'
if self.number or self.caption:
result = f'\\includegraphics[width=\\textwidth]{{{source}}}'
result = latex_util.add_caption(
result, "Figure", self.number, self.caption, width,
self.hpos, self.caption_side, offset=self.offset)
else:
result = f'\\includegraphics[width={width}]{{{source}}}'
result = latex_util.caption_wrapper(result, self.hpos, offset=self.offset)
name = f"Reference-Figure-{Image.id}"
result = f"\\hypertarget{{{name}}}{{}}\\label{{Label-{name}}}\n{result}"
Image.id += 1
# \includegraphics (bare or inside a caption wrapper's minipage) is
# box material: it must sit in vertical mode or it is typeset beside
# any text it follows.
return latex_util.block(result)