Files
klammertext/sks/image/image_grid.py
T
ack af29bbf7ac Unicode sequences, @typeface, and the font/typeface distinction
A run of Unicode code points now needs one pair of carets, not one pair
each: ^9053 53EF 9053^ renders the characters adjacently, and a single
character inside such a run stands for itself.  An invalid code point is
an error naming the position that wrote it, rather than an apology
rendered into the finished document; noncharacters are refused alongside
surrogates; and a code point that lands on a Klammertext special is safe
through the re-reads that assemble a document.  The character tables now
produce the precomposed form, so a diacritic written as ^a" and one typed
directly are the same text to a search or a copy-paste.

"kdesc -u" describes how to write a Unicode character, with tables of
glyphs beside the codes that produce them.  It is new and not yet
complete: it predates the sequence form and the typeface klammer below.

A typeface and a style are no longer both called "font".  A typeface is a
family resolved by name; a style code is the one letter naming a type
style.  So @document takes :serif_typeface and its siblings, a table takes
:style, captions take :caption_style and :caption_size, the uniform scale
is :text_scale, and the command is "kdesc --typeface".  This renames
arguments a document may already use; an old name is a located error
rather than a silent omission.

@typeface sets a passage in another typeface for the length of that
passage:

    @typeface Noto Sans Egyptian Hieroglyphs | ^132AA 133CF^ :scale 1.5 @

Writing the klammer is the whole declaration -- the document resolves the
typeface, embeds it in what it produces, and carries its license along.
Two typefaces for scripts no text font covers now ship with Klammertext:
Noto Sans Egyptian Hieroglyphs and Noto Sans Cuneiform.  Every typeface in
the distribution now carries its license file.

(from dev 61e598698891)
2026-08-25 23:30:55 +02:00

126 lines
4.7 KiB
Python

import os
import re
import kutil
import klammer_base
import image
import latex_util
import html
import html_util
from html_util import E
import image_cache
#import kutil
class Kargs:
def __init__(self, target, K, **kwargs):
self.__dict__ = kwargs
self.center = False
self.caption_style = "i"
self.caption_side = "bottom"
self.caption_side_center = False
self.Image_search_path = K.Image_search_path
self.number = K.cell_number
# A cell is placed by the grid, not by itself: no positioning
# container, and nothing to inset it from. Both members of the
# hpos_args option set have to be set here -- image.py reads them
# unconditionally, so a missing one is an AttributeError at render
# time rather than a message.
self.hpos = "none"
self.offset = "0pt"
self.K_target = target
self.K_input_dir = K.K_input_dir
self.K_output_dir = K.K_output_dir
self.K_output_basename = K.K_output_basename
self.Image_output_dir = K.Image_output_dir
self.K_toc_only = False
self.id = ""
self.as_string = False
self.rel = None
# Defaults for @image parameters the grid does not use per cell.
self.border = False
self.vmargin = False
self.abswidth = 0.0
self.caption_size = .9
def img(K, target, basename, caption, width):
result = image.Image(
Kargs(target, K, basename=basename, caption=caption), width=width)
return result
class Image_grid(klammer_base.Klammer_base):
def __init__(self, K):
super().__init__(K)
self.K = K
image_pat = re.compile("([^\s]+)\s*(.*?)", re.S)
self.images = []
self.basenames = []
self.captions = []
for row in self.image_specs:
self.images.append(
[e.groups() for e in [image_pat.fullmatch(s.strip()) for s in row]])
self.basenames.append([e[0] for e in self.images[-1]])
self.captions.append([e[1] for e in self.images[-1]])
self.cache = image_cache.Image_cache(
self.K_input_dir,
self.Image_search_path,
verbose=False)
self.margin = 0.01
self.grid = []
for basenames, captions in zip(self.basenames, self.captions):
cell_widths = self.widths(self.K_target, basenames)
row = []
for basename, caption, cell_width in zip(basenames, captions, cell_widths):
row.append([basename, caption, cell_width])
self.grid.append(row)
def widths(self, target, basenames):
dims = []
for basename in basenames:
dims.append(self.cache.get("html", basename))
heights = [e[2] for e in dims]
total_height = sum(heights)
height_scales = [h/total_height for h in heights]
widths = [e[1] for e in dims]
widths = [e[0] / e[1] for e in zip(widths, height_scales)]
total_width = sum(widths)
margin = self.margin * total_width
total_width += margin * (len(widths) - 1)
result = [w/total_width for w in widths]
result = [f"{w:0.6f}w" for w in result]
return result
def html(self):
result = ""
for row in self.grid:
row_cells = ""
for basename, caption, cell_width in row:
cell_image = img(self.K, "html", basename, caption, cell_width).html()
row_cells += str(cell_image).strip() + "\n"
result += str(E("div").body(row_cells).cls("image_grid_row").attr("data-row-size", len(row)))
result = E("div").body(result).cls("image_grid")
if self.number or self.caption:
result = html_util.add_caption(result, "Figure", self.number, self.caption)
return str(result)
def tex(self):
result = ""
for row in self.grid:
row_cells = ""
for basename, caption, cell_width in row:
cell_image = img(self.K, "tex", basename, caption, cell_width).tex()
row_cells += str(cell_image).strip() + f"\\hspace*{{{self.margin}\\textwidth}}%\n"
#result += "\\begin{minipage}[t]{\\textwidth}" \
# + f"{{{row_cells}}}\n\\end{{minipage}}
result += latex_util.minipage(row_cells, vertical="t") + f"\\\\[{self.margin}\\textwidth]\n"
if self.number or self.caption:
result = latex_util.add_caption(
result.strip(), "Figure", self.number, self.caption, "\\textwidth")
else:
#result = f"\\begin{{minipage}}{{\\textwidth}}\n\\centering {result}\n\\end{{minipage}}\n"
result = latex_util.minipage("\\centering " + result, vertical="t") + "\n"
return result