Skip to content

Commit a80fa1c

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

File tree

5 files changed

+13
-8
lines changed

5 files changed

+13
-8
lines changed

src/Text/Pandoc/ImageSize.hs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ sizeInPoints s = (pxXf * 72 / dpiXf, pxYf * 72 / dpiYf)
174174
-- | Calculate (height, width) in points, considering the desired dimensions in the
175175
-- attribute, while falling back on the image file's dpi metadata if no dimensions
176176
-- are specified in the attribute (or only dimensions in percentages).
177-
desiredSizeInPoints :: WriterOptions -> Attr -> ImageSize -> (Double, Double)
178-
desiredSizeInPoints opts attr s =
177+
desiredSizeInPoints :: WriterOptions -> Attr -> Maybe Integer -> ImageSize -> (Double, Double)
178+
desiredSizeInPoints opts attr pageWidthPoints' s =
179179
case (getDim Width, getDim Height) of
180180
(Just w, Just h) -> (w, h)
181181
(Just w, Nothing) -> (w, w / ratio)
@@ -184,7 +184,11 @@ desiredSizeInPoints opts attr s =
184184
where
185185
ratio = fromIntegral (pxX s) / fromIntegral (pxY s)
186186
getDim dir = case dimension dir attr of
187-
Just (Percent _) -> Nothing
187+
Just (Percent a) ->
188+
case (dir, pageWidthPoints') of
189+
(Width, Just pageWidthPoints) ->
190+
Just $ fromIntegral pageWidthPoints * a
191+
_ -> Nothing
188192
Just dim -> Just $ inPoints opts dim
189193
Nothing -> Nothing
190194

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -928,15 +928,15 @@ inlineToOpenXML' opts (Link _ txt (src,_)) = do
928928
return i
929929
return [ Elem $ mknode "w:hyperlink" [("r:id",id')] contents ]
930930
inlineToOpenXML' opts (Image attr@(imgident, _, _) alt (src, title)) = do
931-
pageWidth <- asks envPrintWidth
931+
pageWidth <- asks envPrintWidth -- in Points
932932
imgs <- gets stImages
933933
let
934934
stImage = M.lookup (T.unpack src) imgs
935935
generateImgElt (ident, fp, mt, img) = do
936936
docprid <- getUniqueId
937937
nvpicprid <- getUniqueId
938938
let
939-
(xpt,ypt) = desiredSizeInPoints opts attr
939+
(xpt,ypt) = desiredSizeInPoints opts attr (Just pageWidth)
940940
(either (const def) id (imageSize opts img))
941941
-- 12700 emu = 1 pt
942942
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
@@ -615,7 +615,7 @@ imageICML opts style attr (src, _) = do
615615
report $ CouldNotFetchResource src $ tshow e
616616
return def)
617617
let (ow, oh) = sizeInPoints imgS
618-
(imgWidth, imgHeight) = desiredSizeInPoints opts attr imgS
618+
(imgWidth, imgHeight) = desiredSizeInPoints opts attr Nothing imgS
619619
hw = showFl $ ow / 2
620620
hh = showFl $ oh / 2
621621
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/command/9288.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@
4545
2> [trace] rsvg-convert -f png -a --dpi-x 96 --dpi-y 96 --width 360.000000pt --height 360.000000pt
4646
2> [trace] Found fallback "media/rId20.svg_4572000_4572000.png"
4747
2> [trace] Found fallback "media/rId20.svg_4572000_4572000.png"
48+
2> [trace] rsvg-convert -f png -a --dpi-x 96 --dpi-y 96 --width 336.000000pt --height 336.000000pt
49+
2> [trace] Found fallback "media/rId20.svg_4267200_4267200.png"
4850
2> [trace] rsvg-convert -f png -a --dpi-x 96 --dpi-y 96 --width 75.000000pt --height 75.000000pt
4951
2> [trace] Found fallback "media/rId20.svg_952500_952500.png"
50-
2> [trace] Found fallback "media/rId20.svg_952500_952500.png"
5152
5253
```

0 commit comments

Comments
 (0)