Skip to content

Commit

Permalink
Typst writer: add #box around image to make it inline. (#9149)
Browse files Browse the repository at this point in the history
An `#image` by itself in typst is a block-level element.
To force images to be inline (as they are in pandoc), we need
to add a box with an explicit width. When a width is not given
in image attributes, we compute one from the image itself, when
possible.

Closes #9104, supersedes #9105.
  • Loading branch information
jgm authored Oct 20, 2023
1 parent 5e66811 commit a67223c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
33 changes: 28 additions & 5 deletions src/Text/Pandoc/Writers/Typst.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ module Text.Pandoc.Writers.Typst (
writeTypst
) where
import Text.Pandoc.Definition
import Text.Pandoc.Class.PandocMonad ( PandocMonad )
import Text.Pandoc.Class ( PandocMonad, fetchItem )
import Text.Pandoc.ImageSize (imageSize, sizeInPoints)
import Text.Pandoc.Options ( WriterOptions(..), WrapOption(..), isEnabled )
import Data.Text (Text)
import Data.List (intercalate, intersperse)
Expand All @@ -30,6 +31,7 @@ import Text.Pandoc.Writers.Math (convertMath)
import qualified Text.TeXMath as TM
import Text.DocLayout
import Text.DocTemplates (renderTemplate)
import Control.Monad.Except (catchError)
import Text.Pandoc.Extensions (Extension(..))
import Text.Collate.Lang (Lang(..), parseLang)

Expand Down Expand Up @@ -280,10 +282,31 @@ inlineToTypst inline =
then mempty
else nowrap $ brackets contents
Image (_,_,kvs) _inlines (src,_tit) -> do
let width' = maybe mempty ((", width: " <>) . literal) $ lookup "width" kvs
let height' = maybe mempty ((", height: " <>) . literal) $
lookup "height" kvs
return $ "#image(" <> doubleQuoted src <> width' <> height' <> ")"
opts <- gets stOptions
let mbHeight = lookup "height" kvs
let mdWidth = lookup "width" kvs
let coreImage = "image" <>
parens (doubleQuoted src <>
maybe mempty (\w -> ", width: " <> literal w) mdWidth <>
maybe mempty (\h -> ", height: " <> literal h) mbHeight)
-- see #9104; we need a box or the image is treated as block-level:
case (mdWidth, mbHeight) of
(Nothing, Nothing) -> do
realWidth <- catchError
(do (bs, _mt) <- fetchItem src
case imageSize opts bs of
Right x -> pure $ Just $ T.pack $
show (fst (sizeInPoints x)) <> "pt"
Left _ -> pure Nothing)
(\_ -> pure Nothing)
case realWidth of
Just w -> return $ "#box" <>
parens ("width: " <> literal w <> ", " <> coreImage)
Nothing -> return $ "#" <> coreImage
(Just w, _) -> return $ "#box" <>
parens ("width: " <> literal w <> ", " <> coreImage)
(_, Just h) -> return $ "#box" <>
parens ("height: " <> literal h <> ", " <> coreImage)
Note blocks -> do
contents <- blocksToTypst blocks
return $ "#footnote" <> brackets (chomp contents)
Expand Down
4 changes: 2 additions & 2 deletions test/writer.typst
Original file line number Diff line number Diff line change
Expand Up @@ -779,13 +779,13 @@ or here: <http://example.com/>
<images>
From "Voyage dans la Lune" by Georges Melies \(1902):

#figure([#image("lalune.jpg")],
#figure([#box(width: 150.0pt, image("lalune.jpg"))],
caption: [
lalune
]
)

Here is a movie #image("movie.jpg") icon.
Here is a movie #box(width: 20.0pt, image("movie.jpg")) icon.

#horizontalrule

Expand Down

0 comments on commit a67223c

Please sign in to comment.