diff options
| author | John MacFarlane <jgm@berkeley.edu> | 2024-03-17 10:59:47 -0700 |
|---|---|---|
| committer | John MacFarlane <jgm@berkeley.edu> | 2024-03-17 10:59:47 -0700 |
| commit | 9ae7557ac0ac27ee63e8b32aec2bab53b008f72a (patch) | |
| tree | 44f86520e0eb5d08af895d38464cef9e3fe8fab6 /src | |
| parent | 2514ad25311a68d8244a77e4bfc4fd6e26b6848e (diff) | |
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.
Diffstat (limited to 'src')
| -rw-r--r-- | src/Text/Pandoc/Writers/Typst.hs | 37 |
1 files changed, 9 insertions, 28 deletions
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 |
