From 9ae7557ac0ac27ee63e8b32aec2bab53b008f72a Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Sun, 17 Mar 2024 10:59:47 -0700 Subject: Typst writer: omit width/height in images unless explicitly specified. Previously we computed width/heigth for images that didn't have size information, because otherwise typst would expand the image to fit page width. This typst behavior has changed in 0.11. This change fixes a bug in which images would sometimes overflow page margins, depending on their intrinsic size. Closes #9236. --- src/Text/Pandoc/Writers/Typst.hs | 37 +++++++++---------------------------- 1 file changed, 9 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/Text/Pandoc/Writers/Typst.hs b/src/Text/Pandoc/Writers/Typst.hs index 007e0881e..ce2f2d8f0 100644 --- a/src/Text/Pandoc/Writers/Typst.hs +++ b/src/Text/Pandoc/Writers/Typst.hs @@ -17,8 +17,7 @@ module Text.Pandoc.Writers.Typst ( writeTypst ) where import Text.Pandoc.Definition -import Text.Pandoc.Class ( PandocMonad, fetchItem ) -import Text.Pandoc.ImageSize (imageSize, sizeInPoints) +import Text.Pandoc.Class ( PandocMonad) import Text.Pandoc.Options ( WriterOptions(..), WrapOption(..), isEnabled ) import Data.Text (Text) import Data.List (intercalate) @@ -32,7 +31,6 @@ 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) import Data.Char (isAlphaNum) @@ -307,32 +305,15 @@ inlineToTypst inline = then mempty else nowrap $ brackets contents) <> endCode Image (_,_,kvs) _inlines (src,_tit) -> do - opts <- gets stOptions - let mbHeight = lookup "height" kvs - let mdWidth = lookup "width" kvs let src' = T.pack $ unEscapeString $ T.unpack src -- #9389 - let coreImage = "image" <> parens (doubleQuoted src') - -- 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) - <> endCode - Nothing -> return $ "#" <> coreImage <> endCode - (Just w, _) -> return $ "#box" <> - parens ("width: " <> literal w <> ", " <> coreImage) - <> endCode - (_, Just h) -> return $ "#box" <> - parens ("height: " <> literal h <> ", " <> coreImage) - <> endCode + let toDimAttr k = + case lookup k kvs of + Just v -> ", " <> literal k <> ": " <> literal v + Nothing -> mempty + let dimAttrs = mconcat $ map toDimAttr ["height", "width"] + pure $ "#box" <> -- see #9104; need box or image is treated as block-level + parens ("image" <> + parens (doubleQuoted src' <> dimAttrs)) Note blocks -> do contents <- blocksToTypst blocks return $ "#footnote" <> brackets (chomp contents) <> endCode -- cgit v1.2.3