Skip to content

Commit efacc24

Browse files
committed
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.
1 parent 1572906 commit efacc24

3 files changed

Lines changed: 20 additions & 7 deletions

File tree

src/Text/Pandoc/Writers/Docx/OpenXML.hs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -995,10 +995,23 @@ inlineToOpenXML' opts (Image attr@(imgident, _, _) alt (src, title)) = do
995995
(xpt,ypt) = desiredSizeInPoints opts attr
996996
(either (const def) id (imageSize opts img))
997997
-- 12700 emu = 1 pt
998-
pageWidthPt = case dimension Width attr of
999-
Just (Percent a) -> pageWidth * floor (a * 127)
1000-
_ -> pageWidth * 12700
1001-
(xemu,yemu) = fitToPage (xpt * 12700, ypt * 12700) pageWidthPt
998+
pageWidthPt = fromIntegral pageWidth
999+
pageWidthEmu = pageWidth * 12700
1000+
(xpt', ypt') =
1001+
case (dimension Width attr, dimension Height attr) of
1002+
(Just (Percent a), Just (Percent b))
1003+
-> ((a / 100.0) * pageWidthPt, (b / 100.0) * pageWidthPt)
1004+
-- note, should use pageHeightPt but we don't have this
1005+
-- information.
1006+
(Just (Percent a), _)
1007+
-> ((a / 100.0) * pageWidthPt,
1008+
(a / 100.0) * pageWidthPt * (ypt / xpt))
1009+
(_, Just (Percent b))
1010+
-> ((b / 100.0) * pageWidthPt * (xpt / ypt),
1011+
(b / 100.0) * pageWidthPt)
1012+
(_, _) -> (xpt, ypt)
1013+
(xemu,yemu) = fitToPage (xpt' * 12700,
1014+
ypt' * 12700) pageWidthEmu
10021015
cNvPicPr = mknode "pic:cNvPicPr" [] $
10031016
mknode "a:picLocks" [("noChangeArrowheads","1")
10041017
,("noChangeAspect","1")] ()

src/Text/Pandoc/Writers/Docx/Types.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ data WriterEnv = WriterEnv
8787
, envInNote :: Bool
8888
, envChangesAuthor :: Text
8989
, envChangesDate :: Text
90-
, envPrintWidth :: Integer
90+
, envPrintWidth :: Integer -- in points
9191
, envLang :: Maybe Text
9292
, envSectPr :: Maybe Element
9393
}

src/Text/Pandoc/Writers/OOXML.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ isElem ns prefix name element =
8989

9090
type NameSpaces = [(Text, Text)]
9191

92-
-- | Scales the image to fit the page
93-
-- sizes are passed in emu
92+
-- | Scales the image to fit the page if it would overflow.
93+
-- Sizes are passed in emu.
9494
fitToPage :: (Double, Double) -> Integer -> (Integer, Integer)
9595
fitToPage (x, y) pageWidth
9696
-- Fixes width to the page width and scales the height

0 commit comments

Comments
 (0)