aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2017-05-25 22:48:27 +0200
committerJohn MacFarlane <jgm@berkeley.edu>2017-05-25 22:48:27 +0200
commitcb7b0a69859cbf838519c5ad5f35d40ffd4f4246 (patch)
tree6b4449b94e0f218598b430ce1cf6e13a35e67b0f
parent708973a33a0ce425bb21a5ffa06fbdab465d3fb8 (diff)
downloadpandoc-cb7b0a69859cbf838519c5ad5f35d40ffd4f4246.tar.gz
Allow em for image height/width in HTML, LaTeX.
- Export `inEm` from ImageSize [API change]. - Change `showFl` and `show` instance for `Dimension` so extra decimal places are omitted. - Added `Em` as a constructor of `Dimension` [API change]. - Allow `em`, `cm`, `in` to pass through without conversion in HTML, LaTeX. Closes #3450.
-rw-r--r--src/Text/Pandoc/ImageSize.hs20
-rw-r--r--src/Text/Pandoc/Writers/HTML.hs7
-rw-r--r--test/command/3450.md12
-rw-r--r--test/command/svg.md48
-rw-r--r--test/writer.icml24
5 files changed, 69 insertions, 42 deletions
diff --git a/src/Text/Pandoc/ImageSize.hs b/src/Text/Pandoc/ImageSize.hs
index 4d914a10c..eec8658c5 100644
--- a/src/Text/Pandoc/ImageSize.hs
+++ b/src/Text/Pandoc/ImageSize.hs
@@ -43,6 +43,7 @@ module Text.Pandoc.ImageSize ( ImageType(..)
, inInch
, inPixel
, inPoints
+ , inEm
, numUnit
, showInInch
, showInPixel
@@ -80,12 +81,14 @@ data Dimension = Pixel Integer
| Centimeter Double
| Inch Double
| Percent Double
+ | Em Double
instance Show Dimension where
show (Pixel a) = show a ++ "px"
show (Centimeter a) = showFl a ++ "cm"
show (Inch a) = showFl a ++ "in"
show (Percent a) = show a ++ "%"
+ show (Em a) = showFl a ++ "em"
data ImageSize = ImageSize{
pxX :: Integer
@@ -97,7 +100,13 @@ instance Default ImageSize where
def = ImageSize 300 200 72 72
showFl :: (RealFloat a) => a -> String
-showFl a = showFFloat (Just 5) a ""
+showFl a = removeExtra0s $ showFFloat (Just 5) a ""
+
+removeExtra0s :: String -> String
+removeExtra0s s =
+ case dropWhile (=='0') $ reverse s of
+ '.':xs -> reverse xs
+ xs -> reverse xs
imageType :: ByteString -> Maybe ImageType
imageType img = case B.take 4 img of
@@ -167,6 +176,9 @@ desiredSizeInPoints opts attr s =
inPoints :: WriterOptions -> Dimension -> Double
inPoints opts dim = 72 * inInch opts dim
+inEm :: WriterOptions -> Dimension -> Double
+inEm opts dim = (64/11) * inInch opts dim
+
inInch :: WriterOptions -> Dimension -> Double
inInch opts dim =
case dim of
@@ -174,6 +186,7 @@ inInch opts dim =
(Centimeter a) -> a * 0.3937007874
(Inch a) -> a
(Percent _) -> 0
+ (Em a) -> a * (11/64)
inPixel :: WriterOptions -> Dimension -> Integer
inPixel opts dim =
@@ -181,7 +194,8 @@ inPixel opts dim =
(Pixel a) -> a
(Centimeter a) -> floor $ dpi * a * 0.3937007874 :: Integer
(Inch a) -> floor $ dpi * a :: Integer
- _ -> 0
+ (Percent _) -> 0
+ (Em a) -> floor $ dpi * a * (11/64) :: Integer
where
dpi = fromIntegral $ writerDpi opts
@@ -213,6 +227,7 @@ scaleDimension factor dim =
Centimeter x -> Centimeter (factor * x)
Inch x -> Inch (factor * x)
Percent x -> Percent (factor * x)
+ Em x -> Em (factor * x)
-- | Read a Dimension from an Attr attribute.
-- `dimension Width attr` might return `Just (Pixel 3)` or for example `Just (Centimeter 2.0)`, etc.
@@ -236,6 +251,7 @@ lengthToDim s = numUnit s >>= uncurry toDim
toDim a "" = Just $ Pixel (floor a::Integer)
toDim a "pt" = Just $ Inch (a / 72)
toDim a "pc" = Just $ Inch (a / 6)
+ toDim a "em" = Just $ Em a
toDim _ _ = Nothing
epsSize :: ByteString -> Maybe ImageSize
diff --git a/src/Text/Pandoc/Writers/HTML.hs b/src/Text/Pandoc/Writers/HTML.hs
index 63e839684..2a72f6f3d 100644
--- a/src/Text/Pandoc/Writers/HTML.hs
+++ b/src/Text/Pandoc/Writers/HTML.hs
@@ -533,10 +533,9 @@ dimensionsToAttrList :: WriterOptions -> Attr -> [(String, String)]
dimensionsToAttrList opts attr = (go Width) ++ (go Height)
where
go dir = case (dimension dir attr) of
- (Just (Percent a)) -> [("style", show dir ++ ":" ++ show (Percent a))]
- (Just dim) -> [(show dir, showInPixel opts dim)]
- _ -> []
-
+ (Just (Pixel a)) -> [(show dir, show a)]
+ (Just x) -> [("style", show dir ++ ":" ++ show x)]
+ Nothing -> []
imageExts :: [String]
imageExts = [ "art", "bmp", "cdr", "cdt", "cpt", "cr2", "crw", "djvu", "erf",
diff --git a/test/command/3450.md b/test/command/3450.md
new file mode 100644
index 000000000..8759aa0c1
--- /dev/null
+++ b/test/command/3450.md
@@ -0,0 +1,12 @@
+```
+% pandoc -fmarkdown-implicit_figures
+![image](lalune.jpg){height=2em}
+^D
+<p><img src="lalune.jpg" alt="image" style="height:2em" /></p>
+```
+```
+% pandoc -fmarkdown-implicit_figures -t latex
+![image](lalune.jpg){height=2em}
+^D
+\includegraphics[height=2em]{lalune.jpg}
+```
diff --git a/test/command/svg.md b/test/command/svg.md
index 36ca2fdb7..b48745f9a 100644
--- a/test/command/svg.md
+++ b/test/command/svg.md
@@ -5,20 +5,20 @@
[warning] Could not determine image size for 'command/corrupt.svg': could not determine image type
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
<CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
- <Rectangle Self="uec" StrokeWeight="0" ItemTransform="1.00000 0 0 1.00000 150.00000 -100.00000">
+ <Rectangle Self="uec" StrokeWeight="0" ItemTransform="1 0 0 1 150 -100">
<Properties>
<PathGeometry>
<GeometryPathType PathOpen="false">
<PathPointArray>
- <PathPointType Anchor="-150.00000 -100.00000" LeftDirection="-150.00000 -100.00000" RightDirection="-150.00000 -100.00000" />
- <PathPointType Anchor="-150.00000 100.00000" LeftDirection="-150.00000 100.00000" RightDirection="-150.00000 100.00000" />
- <PathPointType Anchor="150.00000 100.00000" LeftDirection="150.00000 100.00000" RightDirection="150.00000 100.00000" />
- <PathPointType Anchor="150.00000 -100.00000" LeftDirection="150.00000 -100.00000" RightDirection="150.00000 -100.00000" />
+ <PathPointType Anchor="-150 -100" LeftDirection="-150 -100" RightDirection="-150 -100" />
+ <PathPointType Anchor="-150 100" LeftDirection="-150 100" RightDirection="-150 100" />
+ <PathPointType Anchor="150 100" LeftDirection="150 100" RightDirection="150 100" />
+ <PathPointType Anchor="150 -100" LeftDirection="150 -100" RightDirection="150 -100" />
</PathPointArray>
</GeometryPathType>
</PathGeometry>
</Properties>
- <Image Self="ue6" ItemTransform="1.00000 0 0 1.00000 -150.00000 -100.00000">
+ <Image Self="ue6" ItemTransform="1 0 0 1 -150 -100">
<Properties>
<Profile type="string">
$ID/Embedded
@@ -38,20 +38,20 @@
[warning] Could not determine image size for 'command/SVG_logo.svg': could not determine SVG size
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
<CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
- <Rectangle Self="uec" StrokeWeight="0" ItemTransform="1.00000 0 0 1.00000 150.00000 -100.00000">
+ <Rectangle Self="uec" StrokeWeight="0" ItemTransform="1 0 0 1 150 -100">
<Properties>
<PathGeometry>
<GeometryPathType PathOpen="false">
<PathPointArray>
- <PathPointType Anchor="-150.00000 -100.00000" LeftDirection="-150.00000 -100.00000" RightDirection="-150.00000 -100.00000" />
- <PathPointType Anchor="-150.00000 100.00000" LeftDirection="-150.00000 100.00000" RightDirection="-150.00000 100.00000" />
- <PathPointType Anchor="150.00000 100.00000" LeftDirection="150.00000 100.00000" RightDirection="150.00000 100.00000" />
- <PathPointType Anchor="150.00000 -100.00000" LeftDirection="150.00000 -100.00000" RightDirection="150.00000 -100.00000" />
+ <PathPointType Anchor="-150 -100" LeftDirection="-150 -100" RightDirection="-150 -100" />
+ <PathPointType Anchor="-150 100" LeftDirection="-150 100" RightDirection="-150 100" />
+ <PathPointType Anchor="150 100" LeftDirection="150 100" RightDirection="150 100" />
+ <PathPointType Anchor="150 -100" LeftDirection="150 -100" RightDirection="150 -100" />
</PathPointArray>
</GeometryPathType>
</PathGeometry>
</Properties>
- <Image Self="ue6" ItemTransform="1.00000 0 0 1.00000 -150.00000 -100.00000">
+ <Image Self="ue6" ItemTransform="1 0 0 1 -150 -100">
<Properties>
<Profile type="string">
$ID/Embedded
@@ -71,20 +71,20 @@
[warning] Could not determine image size for 'command/SVG_logo-without-xml-declaration.svg': could not determine SVG size
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
<CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
- <Rectangle Self="uec" StrokeWeight="0" ItemTransform="1.00000 0 0 1.00000 150.00000 -100.00000">
+ <Rectangle Self="uec" StrokeWeight="0" ItemTransform="1 0 0 1 150 -100">
<Properties>
<PathGeometry>
<GeometryPathType PathOpen="false">
<PathPointArray>
- <PathPointType Anchor="-150.00000 -100.00000" LeftDirection="-150.00000 -100.00000" RightDirection="-150.00000 -100.00000" />
- <PathPointType Anchor="-150.00000 100.00000" LeftDirection="-150.00000 100.00000" RightDirection="-150.00000 100.00000" />
- <PathPointType Anchor="150.00000 100.00000" LeftDirection="150.00000 100.00000" RightDirection="150.00000 100.00000" />
- <PathPointType Anchor="150.00000 -100.00000" LeftDirection="150.00000 -100.00000" RightDirection="150.00000 -100.00000" />
+ <PathPointType Anchor="-150 -100" LeftDirection="-150 -100" RightDirection="-150 -100" />
+ <PathPointType Anchor="-150 100" LeftDirection="-150 100" RightDirection="-150 100" />
+ <PathPointType Anchor="150 100" LeftDirection="150 100" RightDirection="150 100" />
+ <PathPointType Anchor="150 -100" LeftDirection="150 -100" RightDirection="150 -100" />
</PathPointArray>
</GeometryPathType>
</PathGeometry>
</Properties>
- <Image Self="ue6" ItemTransform="1.00000 0 0 1.00000 -150.00000 -100.00000">
+ <Image Self="ue6" ItemTransform="1 0 0 1 -150 -100">
<Properties>
<Profile type="string">
$ID/Embedded
@@ -104,20 +104,20 @@
^D
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
<CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
- <Rectangle Self="uec" StrokeWeight="0" ItemTransform="1.00000 0 0 1.00000 54.75000 -65.25000">
+ <Rectangle Self="uec" StrokeWeight="0" ItemTransform="1 0 0 1 54.75 -65.25">
<Properties>
<PathGeometry>
<GeometryPathType PathOpen="false">
<PathPointArray>
- <PathPointType Anchor="-54.75000 -65.25000" LeftDirection="-54.75000 -65.25000" RightDirection="-54.75000 -65.25000" />
- <PathPointType Anchor="-54.75000 65.25000" LeftDirection="-54.75000 65.25000" RightDirection="-54.75000 65.25000" />
- <PathPointType Anchor="54.75000 65.25000" LeftDirection="54.75000 65.25000" RightDirection="54.75000 65.25000" />
- <PathPointType Anchor="54.75000 -65.25000" LeftDirection="54.75000 -65.25000" RightDirection="54.75000 -65.25000" />
+ <PathPointType Anchor="-54.75 -65.25" LeftDirection="-54.75 -65.25" RightDirection="-54.75 -65.25" />
+ <PathPointType Anchor="-54.75 65.25" LeftDirection="-54.75 65.25" RightDirection="-54.75 65.25" />
+ <PathPointType Anchor="54.75 65.25" LeftDirection="54.75 65.25" RightDirection="54.75 65.25" />
+ <PathPointType Anchor="54.75 -65.25" LeftDirection="54.75 -65.25" RightDirection="54.75 -65.25" />
</PathPointArray>
</GeometryPathType>
</PathGeometry>
</Properties>
- <Image Self="ue6" ItemTransform="1.00000 0 0 1.00000 -54.75000 -65.25000">
+ <Image Self="ue6" ItemTransform="1 0 0 1 -54.75 -65.25">
<Properties>
<Profile type="string">
$ID/Embedded
diff --git a/test/writer.icml b/test/writer.icml
index c39915120..6e070e264 100644
--- a/test/writer.icml
+++ b/test/writer.icml
@@ -2833,20 +2833,20 @@ These should not be escaped: \$ \\ \&gt; \[ \{</Content>
<Br />
<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Figure">
<CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
- <Rectangle Self="uec" StrokeWeight="0" ItemTransform="1.00000 0 0 1.00000 75.00000 -75.00000">
+ <Rectangle Self="uec" StrokeWeight="0" ItemTransform="1 0 0 1 75 -75">
<Properties>
<PathGeometry>
<GeometryPathType PathOpen="false">
<PathPointArray>
- <PathPointType Anchor="-75.00000 -75.00000" LeftDirection="-75.00000 -75.00000" RightDirection="-75.00000 -75.00000" />
- <PathPointType Anchor="-75.00000 75.00000" LeftDirection="-75.00000 75.00000" RightDirection="-75.00000 75.00000" />
- <PathPointType Anchor="75.00000 75.00000" LeftDirection="75.00000 75.00000" RightDirection="75.00000 75.00000" />
- <PathPointType Anchor="75.00000 -75.00000" LeftDirection="75.00000 -75.00000" RightDirection="75.00000 -75.00000" />
+ <PathPointType Anchor="-75 -75" LeftDirection="-75 -75" RightDirection="-75 -75" />
+ <PathPointType Anchor="-75 75" LeftDirection="-75 75" RightDirection="-75 75" />
+ <PathPointType Anchor="75 75" LeftDirection="75 75" RightDirection="75 75" />
+ <PathPointType Anchor="75 -75" LeftDirection="75 -75" RightDirection="75 -75" />
</PathPointArray>
</GeometryPathType>
</PathGeometry>
</Properties>
- <Image Self="ue6" ItemTransform="1.00000 0 0 1.00000 -75.00000 -75.00000">
+ <Image Self="ue6" ItemTransform="1 0 0 1 -75 -75">
<Properties>
<Profile type="string">
$ID/Embedded
@@ -2869,20 +2869,20 @@ These should not be escaped: \$ \\ \&gt; \[ \{</Content>
<Content>Here is a movie </Content>
</CharacterStyleRange>
<CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
- <Rectangle Self="uec" StrokeWeight="0" ItemTransform="1.00000 0 0 1.00000 10.00000 -11.00000">
+ <Rectangle Self="uec" StrokeWeight="0" ItemTransform="1 0 0 1 10 -11">
<Properties>
<PathGeometry>
<GeometryPathType PathOpen="false">
<PathPointArray>
- <PathPointType Anchor="-10.00000 -11.00000" LeftDirection="-10.00000 -11.00000" RightDirection="-10.00000 -11.00000" />
- <PathPointType Anchor="-10.00000 11.00000" LeftDirection="-10.00000 11.00000" RightDirection="-10.00000 11.00000" />
- <PathPointType Anchor="10.00000 11.00000" LeftDirection="10.00000 11.00000" RightDirection="10.00000 11.00000" />
- <PathPointType Anchor="10.00000 -11.00000" LeftDirection="10.00000 -11.00000" RightDirection="10.00000 -11.00000" />
+ <PathPointType Anchor="-10 -11" LeftDirection="-10 -11" RightDirection="-10 -11" />
+ <PathPointType Anchor="-10 11" LeftDirection="-10 11" RightDirection="-10 11" />
+ <PathPointType Anchor="10 11" LeftDirection="10 11" RightDirection="10 11" />
+ <PathPointType Anchor="10 -11" LeftDirection="10 -11" RightDirection="10 -11" />
</PathPointArray>
</GeometryPathType>
</PathGeometry>
</Properties>
- <Image Self="ue6" ItemTransform="1.00000 0 0 1.00000 -10.00000 -11.00000">
+ <Image Self="ue6" ItemTransform="1 0 0 1 -10 -11">
<Properties>
<Profile type="string">
$ID/Embedded