Skip to content

Commit 254c5e7

Browse files
committed
Add \includegraphics support
1 parent bb94e2a commit 254c5e7

File tree

4 files changed

+42
-10
lines changed

4 files changed

+42
-10
lines changed

docs/.vitepress/theme/components/LtxPlayground.vue

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,22 @@ let generator: HtmlGenerator | null = null
5050
5151
function getGenerator() {
5252
if (typeof document === 'undefined') return null // SSR
53+
54+
let img_url;
55+
if (typeof window === 'undefined'){
56+
img_url = '';
57+
} else {
58+
img_url = window.location.origin + '/img/baboon.jpg';
59+
}
60+
5361
if (!generator) {
5462
generator = new HtmlGenerator({
5563
hyphenate: true,
5664
languagePatterns: en,
57-
styles: ['css/error.css']
65+
styles: ['css/error.css'],
66+
image_urls: {
67+
'baboon.jpg': img_url
68+
}
5869
})
5970
}
6071
return generator

docs/public/img/baboon.jpg

45.9 KB
Loading

docs/showcase.tex

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
\usepackage{calc,pict2e,picture}
77
\usepackage{textgreek,textcomp,gensymb,stix}
88

9+
\usepackage{graphicx}
10+
911
\setcounter{secnumdepth}{2}
1012

1113
\title{\LaTeX.js Showcase}
@@ -721,6 +723,19 @@ \subsection{An advice}
721723
\textsl{\textsf{beautiful} it bec\large o\Large m\LARGE e\huge s}.
722724
\end{center}
723725

726+
\section{Image Support Test}
727+
728+
This is a test of the new image feature.
729+
The LaTeX code requests \texttt{\textbackslash includegraphics\{image.png\}}. The generator is initialized with a map:
730+
\texttt{ \{ ‘image.png': ‘https://image/url' \}}.
731+
732+
The result should be the image below:
733+
734+
\begin{center}
735+
\includegraphics[width=200pt]{baboon.jpg}
736+
\end{center}
737+
738+
If you see this text but no image, something is wrong.
724739

725740
\appendix
726741

src/html-generator.ls

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,15 @@ export class HtmlGenerator extends Generator
107107

108108
img: "img"
109109

110-
image: (width, height, url) ~> ~>
110+
image: (width, height, path) ~> ~>
111+
url = @_options.image_urls?[path] or path
111112
el = create @img
112-
el.src = url
113-
el.height = height
114-
el.width = width
115-
113+
el.setAttribute "src", url
114+
if width?
115+
el.style.width = if width.value? then width.value else String width
116+
if height?
117+
el.style.height = if height.value? then height.value else String height
118+
el
116119
return el
117120

118121
picture: ~> create @inline, "picture"
@@ -137,15 +140,17 @@ export class HtmlGenerator extends Generator
137140
# - hyphenate: boolean, enable or disable automatic hyphenation
138141
# - languagePatterns: language patterns object to use for hyphenation if it is enabled (default en)
139142
# TODO: infer language from LaTeX preamble and load hypenation patterns automatically
140-
# - styles: array with additional CSS stylesheets
141-
# - precision: precision of numbers in CSS and SVG
143+
#  - styles: array with additional CSS stylesheets
144+
#  - precision: precision of numbers in CSS and SVG
145+
  #  - image_urls: a dict mapping image paths (from \includegraphics) to actual image URLs
142146
(options) ->
143147
@_options = Object.assign {
144148
documentClass: "article"
145149
styles: []
146150
hyphenate: true
147151
languagePatterns: h-en
148152
precision: 3
153+
image_urls: {}
149154
}, options
150155

151156
if @_options.hyphenate
@@ -393,8 +398,9 @@ export class HtmlGenerator extends Generator
393398
f = document.createDocumentFragment!
394399
appendChildren f, children
395400

396-
createImage: (width, height, url) ->
397-
@create @image(width, height, url)
401+
402+
createImage: (width, height, path) ->
403+
@create @image(width, height, path)
398404

399405
createPicture: (size, offset, content) ->
400406
# canvas

0 commit comments

Comments
 (0)