Skip to content

Add png, pdf and jpeg image support to the latex backend #1297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased

### Added

- Added support for (local) images in the latex backend (@Octachron, #1297)

### Changed

- Drop support for OCaml < 4.08 (@jonludlam, #1300)
26 changes: 22 additions & 4 deletions src/latex/generator.ml
Original file line number Diff line number Diff line change
@@ -97,7 +97,7 @@ let elt_size (x : elt) =
| Code_fragment _ | Tag _ | Break _ | Ligaturable _ ->
Small
| List _ | Section _ | Verbatim _ | Raw _ | Code_block _ | Indented _
| Description _ ->
| Description _ | Image _ ->
Large
| Table _ | Layout_table _ -> Huge

@@ -167,6 +167,7 @@ let rec pp_elt ppf = function
| Indented x -> Raw.indent pp ppf x
| Ligaturable s -> Fmt.string ppf s
| Tag (s, t) -> tag s ppf t
| Image target -> Raw.includegraphics Fpath.pp ppf target

and pp ppf = function
| [] -> ()
@@ -277,13 +278,30 @@ let non_empty_code_fragment c =
let s = source (inline ~verbatim:false ~in_source:true) c in
match s with [] -> [] | _ :: _ as l -> [ Code_fragment l ]

let alt_text ~in_source (target : Target.t) alt =
let text = txt ~verbatim:false ~in_source:false [ alt ] in
let break = if in_source then [] else [ Break Paragraph ] in
match target with
| Internal _ -> text @ break
| External l -> [ External_ref (l, Some text) ] @ break

let image ~in_source (internal_url : Url.t) alt =
let dir, file = Link.get_dir_and_file internal_url.page in
match Fpath.(get_ext @@ v file) with
(* list imported from pdftex.def *)
| "" | ".pdf" | ".png" | ".jpg" | ".mps" | ".jpeg" | ".jbig2" | ".jb2"
| ".PDF" | ".PNG" | ".JPG" | ".JPEG" | ".JBIG2" | ".JB2" ->
let fpath = Fpath.v (String.concat Fpath.dir_sep (dir @ [ file ])) in
[ Image fpath ]
| _ -> alt_text ~in_source (Internal (Resolved internal_url)) alt

let rec block ~in_source (l : Block.t) =
let one (t : Block.one) =
match t.desc with
| Inline i -> inline ~verbatim:false ~in_source:false i
| Audio (_, content) | Video (_, content) | Image (_, content) ->
txt ~verbatim:false ~in_source:false [ content ]
@ if in_source then [] else [ Break Paragraph ]
| Image (Internal (Resolved x), alt) -> image ~in_source x alt
| Image (t, alt) | Audio (t, alt) | Video (t, alt) ->
alt_text ~in_source t alt
| Paragraph i ->
inline ~in_source:false ~verbatim:false i
@ if in_source then [] else [ Break Paragraph ]
2 changes: 2 additions & 0 deletions src/latex/raw.ml
Original file line number Diff line number Diff line change
@@ -126,6 +126,8 @@ let code_block pp ppf x =
Fmt.cut ppf ();
mend ppf name

let includegraphics pp = create "includegraphics" pp

let section pp = create "section" pp

let subsection pp = create "subsection" pp
2 changes: 2 additions & 0 deletions src/latex/raw.mli
Original file line number Diff line number Diff line change
@@ -66,6 +66,8 @@ val small_table : ('a, Types.alignment list option * 'a list list) tr

val input : Fpath.t Fmt.t

val includegraphics : 'a t

(** {1 Required OCaml-specific primitives}
All the macro should be implemented as "ocaml"-suffixed macro in the latex
preamble *)
1 change: 1 addition & 0 deletions src/latex/types.ml
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@ type elt =
| Layout_table of layout_table
| Table of table
| Ligaturable of string
| Image of Fpath.t

and section = { level : int; label : string option; content : t }

2 changes: 1 addition & 1 deletion test/pages/medias.t/index.mld
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@
Some image:

- Without alt text:{image!caml.gif}
- With an alt text: {{image!caml.gif}With alt text and {b emphasis}}
- With an alt text: {{image!caml.png}With alt text and {b emphasis}}
- Unresolved without alt text: {image!caqzdqzdml.gif}
- Unresolved with alt text: {{image!camezfzeffl.gif}With alt text and {b emphasis}}

8 changes: 6 additions & 2 deletions test/pages/medias.t/run.t
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ We need to odoc-compile the package mld file, listing its children
$ odoc compile index.mld --parent-id pkg1/ --output-dir _odoc

$ odoc compile-asset --parent-id pkg1/ --output-dir _odoc --name caml.gif
$ odoc compile-asset --parent-id pkg1/ --output-dir _odoc --name caml.png
$ odoc compile-asset --parent-id pkg1/ --output-dir _odoc --name Cri_du_chameau.ogg
$ odoc compile-asset --parent-id pkg1/ --output-dir _odoc --name flower.webm

@@ -28,8 +29,8 @@ Testing the working references:
$ cat html/pkg1/index.html | grep img
<a href="caml.gif" class="img-link">
<img src="caml.gif" alt="caml.gif"/>
<a href="caml.gif" class="img-link">
<img src="caml.gif" alt="With alt text and {b emphasis}"/>
<a href="caml.png" class="img-link">
<img src="caml.png" alt="With alt text and {b emphasis}"/>
<a href="https://picsum.photos/200/300" class="img-link">
<img src="https://picsum.photos/200/300" alt="reference"/>
<a href="https://picsum.photos/200/300" class="img-link">
@@ -72,6 +73,9 @@ Testing latex and manpages
caml.gif
./caqzdqzdml.gif

$ cat latex/pkg1/index.tex | grep png
\includegraphics{pkg1/caml.png}}%

$ odoc man-generate -o man _odoc/pkg1/page-index.odocl
$ cat man/pkg1/index.3o | grep gif
caml\.gif