]> git.scottworley.com Git - paperdoorknob/commitdiff
Learning TeX: Render icon images with TeX command
authorScott Worley <scottworley@scottworley.com>
Sun, 31 Dec 2023 22:37:57 +0000 (14:37 -0800)
committerScott Worley <scottworley@scottworley.com>
Mon, 1 Jan 2024 06:14:24 +0000 (22:14 -0800)
glowfic.py
paperdoorknob.py

index 4c108b8310dc3441e373c08c11854f4ff8405c0d..b7e7ba740b33f82fbebd43ea9c1c4ffa36bc1973 100644 (file)
@@ -111,14 +111,6 @@ def makeChunk(chunk_dom: Tag, image_store: ImageStore) -> Chunk:
                  content)
 
 
-def renderIcon(icon_path: str | None) -> bytes | None:
-    if icon_path is None:
-        return None
-    return (
-        br'\includegraphics[width=\glowiconsize,height=\glowiconsize,keepaspectratio]{%s}' %
-        icon_path.encode('UTF-8'))
-
-
 class Layout(ABC):
 
     @abstractmethod
@@ -141,8 +133,9 @@ class BelowIconLayout(Layout):
         self._texifier = texifier
 
     def renderChunk(self, chunk: Chunk) -> bytes:
-        icon = renderIcon(chunk.icon)
-        meta = [icon] if icon else []
+        meta = []
+        if chunk.icon:
+            meta += [br'\glowicon{%s}' % chunk.icon.encode('UTF-8')]
         meta += [self._texifier.texify(x)
                  for x in [chunk.character, chunk.screen_name, chunk.author]
                  if x is not None]
@@ -179,7 +172,6 @@ class BesideIconLayout(Layout):
         self._texifier = texifier
 
     def renderChunk(self, chunk: Chunk) -> bytes:
-        icon = renderIcon(chunk.icon)
         meta = [
             chunk.character,
             chunk.screen_name,
@@ -202,6 +194,6 @@ class BesideIconLayout(Layout):
 \strut
 
 ''' % (
-            icon if icon else b'',
+            br'\glowicon{%s}' % chunk.icon.encode('UTF-8') if chunk.icon else b'',
             br'\\*'.join(self._texifier.texify(x) for x in meta if x is not None),
             self._texifier.texify(chunk.content))
index 81831360e462a6a9af1b366f3384e566e0adb412..0fb3fe18c1f5c8c34e9cf8ace6052b15a030995e 100644 (file)
@@ -37,6 +37,9 @@ def process(spec: Spec) -> None:
     spec.texout.write(br'''\begin{document}
 \newcommand{\href}[2]{#2\footnote{\detokenize{#1}}}
 \def\glowiconsize{%fmm}
+\newcommand{\glowicon}[1]{\includegraphics[
+  width=\glowiconsize,height=\glowiconsize,keepaspectratio
+]{#1}}
 ''' % spec.icon_size)
     url = flatURL(spec.url)
     spec.log('Fetching HTML...\r')