Skip to content

Commit 4d801b8

Browse files
committed
fix(docx): honour percentage widths for SVG images
Signed-off-by: Edwin Török <edwin@etorok.net>
1 parent 7141c62 commit 4d801b8

File tree

5 files changed

+12
-8
lines changed

5 files changed

+12
-8
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 pageWidthPoints' 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, pageWidthPoints') of
181+
(Width, Just pageWidthPoints) ->
182+
Just $ fromIntegral pageWidthPoints * a
183+
_ -> Nothing
180184
Just dim -> Just $ inPoints opts dim
181185
Nothing -> Nothing
182186

src/Text/Pandoc/Writers/Docx.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,15 +1530,15 @@ inlineToOpenXML' opts (Link _ txt (src,_)) = do
15301530
return i
15311531
return [ Elem $ mknode "w:hyperlink" [("r:id",id')] contents ]
15321532
inlineToOpenXML' opts (Image attr@(imgident, _, _) alt (src, title)) = do
1533-
pageWidth <- asks envPrintWidth
1533+
pageWidth <- asks envPrintWidth -- in Points
15341534
imgs <- gets stImages
15351535
let
15361536
stImage = M.lookup (T.unpack src) imgs
15371537
generateImgElt (ident, fp, mt, img) = 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/command/9288.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@
4444
^D
4545
2> [trace] rsvg-convert -f png -a --dpi-x 96 --dpi-y 96 --width 360.000000pt --height 360.000000pt
4646
2> [trace] rsvg-convert -f png -a --dpi-x 96 --dpi-y 96 --width 360.000000pt --height 360.000000pt
47-
2> [trace] rsvg-convert -f png -a --dpi-x 96 --dpi-y 96 --width 75.000000pt --height 75.000000pt
47+
2> [trace] rsvg-convert -f png -a --dpi-x 96 --dpi-y 96 --width 336.000000pt --height 336.000000pt
4848
2> [trace] rsvg-convert -f png -a --dpi-x 96 --dpi-y 96 --width 75.000000pt --height 75.000000pt
4949
```

0 commit comments

Comments
 (0)