Skip to content

Commit f06bb78

Browse files
committed
fix(docx): honour percentage widths for SVG images
Signed-off-by: Edwin Török <[email protected]>
1 parent de96454 commit f06bb78

File tree

5 files changed

+10
-6
lines changed

5 files changed

+10
-6
lines changed

src/Text/Pandoc/ImageSize.hs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ sizeInPoints s = (pxXf * 72 / dpiXf, pxYf * 72 / dpiYf)
166166
-- | Calculate (height, width) in points, considering the desired dimensions in the
167167
-- attribute, while falling back on the image file's dpi metadata if no dimensions
168168
-- are specified in the attribute (or only dimensions in percentages).
169-
desiredSizeInPoints :: WriterOptions -> Attr -> ImageSize -> (Double, Double)
170-
desiredSizeInPoints opts attr s =
169+
desiredSizeInPoints :: WriterOptions -> Attr -> Maybe Integer -> ImageSize -> (Double, Double)
170+
desiredSizeInPoints opts attr pageWidth' s =
171171
case (getDim Width, getDim Height) of
172172
(Just w, Just h) -> (w, h)
173173
(Just w, Nothing) -> (w, w / ratio)
@@ -176,7 +176,11 @@ desiredSizeInPoints opts attr s =
176176
where
177177
ratio = fromIntegral (pxX s) / fromIntegral (pxY s)
178178
getDim dir = case dimension dir attr of
179-
Just (Percent _) -> Nothing
179+
Just (Percent a) ->
180+
case (dir, pageWidth') of
181+
(Width, Just pageWidth) ->
182+
Just $ fromIntegral pageWidth * a
183+
_ -> Nothing
180184
Just dim -> Just $ inPoints opts dim
181185
Nothing -> Nothing
182186

src/Text/Pandoc/Writers/Docx.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1538,7 +1538,7 @@ inlineToOpenXML' opts (Image attr@(imgident, _, _) alt (src, title)) = do
15381538
docprid <- getUniqueId
15391539
nvpicprid <- getUniqueId
15401540
let
1541-
(xpt,ypt) = desiredSizeInPoints opts attr
1541+
(xpt,ypt) = desiredSizeInPoints opts attr (Just pageWidth)
15421542
(either (const def) id (imageSize opts img))
15431543
-- 12700 emu = 1 pt
15441544
pageWidthPt = case dimension Width attr of

src/Text/Pandoc/Writers/ICML.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ imageICML opts style attr (src, _) = do
613613
report $ CouldNotFetchResource src $ tshow e
614614
return def)
615615
let (ow, oh) = sizeInPoints imgS
616-
(imgWidth, imgHeight) = desiredSizeInPoints opts attr imgS
616+
(imgWidth, imgHeight) = desiredSizeInPoints opts attr Nothing imgS
617617
hw = showFl $ ow / 2
618618
hh = showFl $ oh / 2
619619
scale = showFl (imgWidth / ow) <> " 0 0 " <> showFl (imgHeight / oh)

src/Text/Pandoc/Writers/RTF.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ rtfEmbedImage opts x@(Image attr _ (src,_)) = catchError
6464
<> "\\pichgoal" <> tshow (floor (ypt * 20) :: Integer)
6565
-- twip = 1/1440in = 1/20pt
6666
where (xpx, ypx) = sizeInPixels sz
67-
(xpt, ypt) = desiredSizeInPoints opts attr sz
67+
(xpt, ypt) = desiredSizeInPoints opts attr Nothing sz
6868
let raw = "{\\pict" <> filetype <> sizeSpec <> " " <>
6969
T.concat bytes <> "}"
7070
if B.null imgdata

test/docx/golden/img.docx

54.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)