From efacc2489aa2648647d043226f724547ca3cfbf0 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Tue, 1 Sep 2026 08:28:47 -0700 Subject: [PATCH] Docx writer: fix sizing for images. Setting size to a percent now scales image to percent of page width. Previously a percent width or height would just provide a maximum bound rather than scaling. Closes #11838. --- src/Text/Pandoc/Writers/Docx/OpenXML.hs | 21 +++++++++++++++++---- src/Text/Pandoc/Writers/Docx/Types.hs | 2 +- src/Text/Pandoc/Writers/OOXML.hs | 4 ++-- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/Text/Pandoc/Writers/Docx/OpenXML.hs b/src/Text/Pandoc/Writers/Docx/OpenXML.hs index 957fbdd47..5c84bc80a 100644 --- a/src/Text/Pandoc/Writers/Docx/OpenXML.hs +++ b/src/Text/Pandoc/Writers/Docx/OpenXML.hs @@ -995,10 +995,23 @@ inlineToOpenXML' opts (Image attr@(imgident, _, _) alt (src, title)) = do (xpt,ypt) = desiredSizeInPoints opts attr (either (const def) id (imageSize opts img)) -- 12700 emu = 1 pt - pageWidthPt = case dimension Width attr of - Just (Percent a) -> pageWidth * floor (a * 127) - _ -> pageWidth * 12700 - (xemu,yemu) = fitToPage (xpt * 12700, ypt * 12700) pageWidthPt + pageWidthPt = fromIntegral pageWidth + pageWidthEmu = pageWidth * 12700 + (xpt', ypt') = + case (dimension Width attr, dimension Height attr) of + (Just (Percent a), Just (Percent b)) + -> ((a / 100.0) * pageWidthPt, (b / 100.0) * pageWidthPt) + -- note, should use pageHeightPt but we don't have this + -- information. + (Just (Percent a), _) + -> ((a / 100.0) * pageWidthPt, + (a / 100.0) * pageWidthPt * (ypt / xpt)) + (_, Just (Percent b)) + -> ((b / 100.0) * pageWidthPt * (xpt / ypt), + (b / 100.0) * pageWidthPt) + (_, _) -> (xpt, ypt) + (xemu,yemu) = fitToPage (xpt' * 12700, + ypt' * 12700) pageWidthEmu cNvPicPr = mknode "pic:cNvPicPr" [] $ mknode "a:picLocks" [("noChangeArrowheads","1") ,("noChangeAspect","1")] () diff --git a/src/Text/Pandoc/Writers/Docx/Types.hs b/src/Text/Pandoc/Writers/Docx/Types.hs index febef6603..e5af230e2 100644 --- a/src/Text/Pandoc/Writers/Docx/Types.hs +++ b/src/Text/Pandoc/Writers/Docx/Types.hs @@ -87,7 +87,7 @@ data WriterEnv = WriterEnv , envInNote :: Bool , envChangesAuthor :: Text , envChangesDate :: Text - , envPrintWidth :: Integer + , envPrintWidth :: Integer -- in points , envLang :: Maybe Text , envSectPr :: Maybe Element } diff --git a/src/Text/Pandoc/Writers/OOXML.hs b/src/Text/Pandoc/Writers/OOXML.hs index ecf7f2163..1844945bc 100644 --- a/src/Text/Pandoc/Writers/OOXML.hs +++ b/src/Text/Pandoc/Writers/OOXML.hs @@ -89,8 +89,8 @@ isElem ns prefix name element = type NameSpaces = [(Text, Text)] --- | Scales the image to fit the page --- sizes are passed in emu +-- | Scales the image to fit the page if it would overflow. +-- Sizes are passed in emu. fitToPage :: (Double, Double) -> Integer -> (Integer, Integer) fitToPage (x, y) pageWidth -- Fixes width to the page width and scales the height