git.iko.soy/typeable/pandoc
on master help sign in
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.

commitcb7b0a69859cbf838519c5ad5f35d40ffd4f4246 authorJohn MacFarlane <jgm@berkeley.edu> dateThu 25 May 2017, 22:48 (9 years ago) parent708973a
5 files changed+69−42
src/Text/Pandoc/ImageSize.hs+18−2 src/Text/Pandoc/Writers/HTML.hs+3−4 test/command/3450.md+12 test/command/svg.md+24−24 test/writer.icml+12−12
src/Text/Pandoc/ImageSize.hs+18−2
@@ -43,6 +43,7 @@ module Text.Pandoc.ImageSize ( ImageType(..)
4343 , inInch
4444 , inPixel
4545 , inPoints
46+ , inEm
4647 , numUnit
4748 , showInInch
4849 , showInPixel
@@ -80,12 +81,14 @@ data Dimension = Pixel Integer
8081 | Centimeter Double
8182 | Inch Double
8283 | Percent Double
84+ | Em Double
8385
8486instance Show Dimension where
8587 show (Pixel a) = show a ++ "px"
8688 show (Centimeter a) = showFl a ++ "cm"
8789 show (Inch a) = showFl a ++ "in"
8890 show (Percent a) = show a ++ "%"
91+ show (Em a) = showFl a ++ "em"
8992
9093data ImageSize = ImageSize{
9194 pxX :: Integer
@@ -97,7 +100,13 @@ instance Default ImageSize where
97100 def = ImageSize 300 200 72 72
98101
99102showFl :: (RealFloat a) => a -> String
100−showFl a = showFFloat (Just 5) a ""
103+showFl a = removeExtra0s $ showFFloat (Just 5) a ""
104+
105+removeExtra0s :: String -> String
106+removeExtra0s s =
107+ case dropWhile (=='0') $ reverse s of
108+ '.':xs -> reverse xs
109+ xs -> reverse xs
101110
102111imageType :: ByteString -> Maybe ImageType
103112imageType img = case B.take 4 img of
@@ -167,6 +176,9 @@ desiredSizeInPoints opts attr s =
167176inPoints :: WriterOptions -> Dimension -> Double
168177inPoints opts dim = 72 * inInch opts dim
169178
179+inEm :: WriterOptions -> Dimension -> Double
180+inEm opts dim = (64/11) * inInch opts dim
181+
170182inInch :: WriterOptions -> Dimension -> Double
171183inInch opts dim =
172184 case dim of
@@ -174,6 +186,7 @@ inInch opts dim =
174186 (Centimeter a) -> a * 0.3937007874
175187 (Inch a) -> a
176188 (Percent _) -> 0
189+ (Em a) -> a * (11/64)
177190
178191inPixel :: WriterOptions -> Dimension -> Integer
179192inPixel opts dim =
@@ -181,7 +194,8 @@ inPixel opts dim =
181194 (Pixel a) -> a
182195 (Centimeter a) -> floor $ dpi * a * 0.3937007874 :: Integer
183196 (Inch a) -> floor $ dpi * a :: Integer
184− _ -> 0
197+ (Percent _) -> 0
198+ (Em a) -> floor $ dpi * a * (11/64) :: Integer
185199 where
186200 dpi = fromIntegral $ writerDpi opts
187201
@@ -213,6 +227,7 @@ scaleDimension factor dim =
213227 Centimeter x -> Centimeter (factor * x)
214228 Inch x -> Inch (factor * x)
215229 Percent x -> Percent (factor * x)
230+ Em x -> Em (factor * x)
216231
217232-- | Read a Dimension from an Attr attribute.
218233-- `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
236251 toDim a "" = Just $ Pixel (floor a::Integer)
237252 toDim a "pt" = Just $ Inch (a / 72)
238253 toDim a "pc" = Just $ Inch (a / 6)
254+ toDim a "em" = Just $ Em a
239255 toDim _ _ = Nothing
240256
241257epsSize :: ByteString -> Maybe ImageSize
src/Text/Pandoc/Writers/HTML.hs+3−4
@@ -533,10 +533,9 @@ dimensionsToAttrList :: WriterOptions -> Attr -> [(String, String)]
533533dimensionsToAttrList opts attr = (go Width) ++ (go Height)
534534 where
535535 go dir = case (dimension dir attr) of
536− (Just (Percent a)) -> [("style", show dir ++ ":" ++ show (Percent a))]
537− (Just dim) -> [(show dir, showInPixel opts dim)]
538− _ -> []
539−
536+ (Just (Pixel a)) -> [(show dir, show a)]
537+ (Just x) -> [("style", show dir ++ ":" ++ show x)]
538+ Nothing -> []
540539
541540imageExts :: [String]
542541imageExts = [ "art", "bmp", "cdr", "cdt", "cpt", "cr2", "crw", "djvu", "erf",
test/command/3450.mdnew file+12
@@ -0,0 +1,12 @@
1+```
2+% pandoc -fmarkdown-implicit_figures
3+![image](lalune.jpg){height=2em}
4+^D
5+<p><img src="lalune.jpg" alt="image" style="height:2em" /></p>
6+```
7+```
8+% pandoc -fmarkdown-implicit_figures -t latex
9+![image](lalune.jpg){height=2em}
10+^D
11+\includegraphics[height=2em]{lalune.jpg}
12+```
test/command/svg.md+24−24
@@ -5,20 +5,20 @@
55[warning] Could not determine image size for 'command/corrupt.svg': could not determine image type
66<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
77 <CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
8− <Rectangle Self="uec" StrokeWeight="0" ItemTransform="1.00000 0 0 1.00000 150.00000 -100.00000">
8+ <Rectangle Self="uec" StrokeWeight="0" ItemTransform="1 0 0 1 150 -100">
99 <Properties>
1010 <PathGeometry>
1111 <GeometryPathType PathOpen="false">
1212 <PathPointArray>
13− <PathPointType Anchor="-150.00000 -100.00000" LeftDirection="-150.00000 -100.00000" RightDirection="-150.00000 -100.00000" />
14− <PathPointType Anchor="-150.00000 100.00000" LeftDirection="-150.00000 100.00000" RightDirection="-150.00000 100.00000" />
15− <PathPointType Anchor="150.00000 100.00000" LeftDirection="150.00000 100.00000" RightDirection="150.00000 100.00000" />
16− <PathPointType Anchor="150.00000 -100.00000" LeftDirection="150.00000 -100.00000" RightDirection="150.00000 -100.00000" />
13+ <PathPointType Anchor="-150 -100" LeftDirection="-150 -100" RightDirection="-150 -100" />
14+ <PathPointType Anchor="-150 100" LeftDirection="-150 100" RightDirection="-150 100" />
15+ <PathPointType Anchor="150 100" LeftDirection="150 100" RightDirection="150 100" />
16+ <PathPointType Anchor="150 -100" LeftDirection="150 -100" RightDirection="150 -100" />
1717 </PathPointArray>
1818 </GeometryPathType>
1919 </PathGeometry>
2020 </Properties>
21− <Image Self="ue6" ItemTransform="1.00000 0 0 1.00000 -150.00000 -100.00000">
21+ <Image Self="ue6" ItemTransform="1 0 0 1 -150 -100">
2222 <Properties>
2323 <Profile type="string">
2424 $ID/Embedded
@@ -38,20 +38,20 @@
3838[warning] Could not determine image size for 'command/SVG_logo.svg': could not determine SVG size
3939<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
4040 <CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
41− <Rectangle Self="uec" StrokeWeight="0" ItemTransform="1.00000 0 0 1.00000 150.00000 -100.00000">
41+ <Rectangle Self="uec" StrokeWeight="0" ItemTransform="1 0 0 1 150 -100">
4242 <Properties>
4343 <PathGeometry>
4444 <GeometryPathType PathOpen="false">
4545 <PathPointArray>
46− <PathPointType Anchor="-150.00000 -100.00000" LeftDirection="-150.00000 -100.00000" RightDirection="-150.00000 -100.00000" />
47− <PathPointType Anchor="-150.00000 100.00000" LeftDirection="-150.00000 100.00000" RightDirection="-150.00000 100.00000" />
48− <PathPointType Anchor="150.00000 100.00000" LeftDirection="150.00000 100.00000" RightDirection="150.00000 100.00000" />
49− <PathPointType Anchor="150.00000 -100.00000" LeftDirection="150.00000 -100.00000" RightDirection="150.00000 -100.00000" />
46+ <PathPointType Anchor="-150 -100" LeftDirection="-150 -100" RightDirection="-150 -100" />
47+ <PathPointType Anchor="-150 100" LeftDirection="-150 100" RightDirection="-150 100" />
48+ <PathPointType Anchor="150 100" LeftDirection="150 100" RightDirection="150 100" />
49+ <PathPointType Anchor="150 -100" LeftDirection="150 -100" RightDirection="150 -100" />
5050 </PathPointArray>
5151 </GeometryPathType>
5252 </PathGeometry>
5353 </Properties>
54− <Image Self="ue6" ItemTransform="1.00000 0 0 1.00000 -150.00000 -100.00000">
54+ <Image Self="ue6" ItemTransform="1 0 0 1 -150 -100">
5555 <Properties>
5656 <Profile type="string">
5757 $ID/Embedded
@@ -71,20 +71,20 @@
7171[warning] Could not determine image size for 'command/SVG_logo-without-xml-declaration.svg': could not determine SVG size
7272<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
7373 <CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
74− <Rectangle Self="uec" StrokeWeight="0" ItemTransform="1.00000 0 0 1.00000 150.00000 -100.00000">
74+ <Rectangle Self="uec" StrokeWeight="0" ItemTransform="1 0 0 1 150 -100">
7575 <Properties>
7676 <PathGeometry>
7777 <GeometryPathType PathOpen="false">
7878 <PathPointArray>
79− <PathPointType Anchor="-150.00000 -100.00000" LeftDirection="-150.00000 -100.00000" RightDirection="-150.00000 -100.00000" />
80− <PathPointType Anchor="-150.00000 100.00000" LeftDirection="-150.00000 100.00000" RightDirection="-150.00000 100.00000" />
81− <PathPointType Anchor="150.00000 100.00000" LeftDirection="150.00000 100.00000" RightDirection="150.00000 100.00000" />
82− <PathPointType Anchor="150.00000 -100.00000" LeftDirection="150.00000 -100.00000" RightDirection="150.00000 -100.00000" />
79+ <PathPointType Anchor="-150 -100" LeftDirection="-150 -100" RightDirection="-150 -100" />
80+ <PathPointType Anchor="-150 100" LeftDirection="-150 100" RightDirection="-150 100" />
81+ <PathPointType Anchor="150 100" LeftDirection="150 100" RightDirection="150 100" />
82+ <PathPointType Anchor="150 -100" LeftDirection="150 -100" RightDirection="150 -100" />
8383 </PathPointArray>
8484 </GeometryPathType>
8585 </PathGeometry>
8686 </Properties>
87− <Image Self="ue6" ItemTransform="1.00000 0 0 1.00000 -150.00000 -100.00000">
87+ <Image Self="ue6" ItemTransform="1 0 0 1 -150 -100">
8888 <Properties>
8989 <Profile type="string">
9090 $ID/Embedded
@@ -104,20 +104,20 @@
104104^D
105105<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Paragraph">
106106 <CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
107− <Rectangle Self="uec" StrokeWeight="0" ItemTransform="1.00000 0 0 1.00000 54.75000 -65.25000">
107+ <Rectangle Self="uec" StrokeWeight="0" ItemTransform="1 0 0 1 54.75 -65.25">
108108 <Properties>
109109 <PathGeometry>
110110 <GeometryPathType PathOpen="false">
111111 <PathPointArray>
112− <PathPointType Anchor="-54.75000 -65.25000" LeftDirection="-54.75000 -65.25000" RightDirection="-54.75000 -65.25000" />
113− <PathPointType Anchor="-54.75000 65.25000" LeftDirection="-54.75000 65.25000" RightDirection="-54.75000 65.25000" />
114− <PathPointType Anchor="54.75000 65.25000" LeftDirection="54.75000 65.25000" RightDirection="54.75000 65.25000" />
115− <PathPointType Anchor="54.75000 -65.25000" LeftDirection="54.75000 -65.25000" RightDirection="54.75000 -65.25000" />
112+ <PathPointType Anchor="-54.75 -65.25" LeftDirection="-54.75 -65.25" RightDirection="-54.75 -65.25" />
113+ <PathPointType Anchor="-54.75 65.25" LeftDirection="-54.75 65.25" RightDirection="-54.75 65.25" />
114+ <PathPointType Anchor="54.75 65.25" LeftDirection="54.75 65.25" RightDirection="54.75 65.25" />
115+ <PathPointType Anchor="54.75 -65.25" LeftDirection="54.75 -65.25" RightDirection="54.75 -65.25" />
116116 </PathPointArray>
117117 </GeometryPathType>
118118 </PathGeometry>
119119 </Properties>
120− <Image Self="ue6" ItemTransform="1.00000 0 0 1.00000 -54.75000 -65.25000">
120+ <Image Self="ue6" ItemTransform="1 0 0 1 -54.75 -65.25">
121121 <Properties>
122122 <Profile type="string">
123123 $ID/Embedded
test/writer.icml+12−12
@@ -2833,20 +2833,20 @@ These should not be escaped: \$ \\ \&gt; \[ \{</Content>
28332833<Br />
28342834<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/Figure">
28352835 <CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
2836− <Rectangle Self="uec" StrokeWeight="0" ItemTransform="1.00000 0 0 1.00000 75.00000 -75.00000">
2836+ <Rectangle Self="uec" StrokeWeight="0" ItemTransform="1 0 0 1 75 -75">
28372837 <Properties>
28382838 <PathGeometry>
28392839 <GeometryPathType PathOpen="false">
28402840 <PathPointArray>
2841− <PathPointType Anchor="-75.00000 -75.00000" LeftDirection="-75.00000 -75.00000" RightDirection="-75.00000 -75.00000" />
2842− <PathPointType Anchor="-75.00000 75.00000" LeftDirection="-75.00000 75.00000" RightDirection="-75.00000 75.00000" />
2843− <PathPointType Anchor="75.00000 75.00000" LeftDirection="75.00000 75.00000" RightDirection="75.00000 75.00000" />
2844− <PathPointType Anchor="75.00000 -75.00000" LeftDirection="75.00000 -75.00000" RightDirection="75.00000 -75.00000" />
2841+ <PathPointType Anchor="-75 -75" LeftDirection="-75 -75" RightDirection="-75 -75" />
2842+ <PathPointType Anchor="-75 75" LeftDirection="-75 75" RightDirection="-75 75" />
2843+ <PathPointType Anchor="75 75" LeftDirection="75 75" RightDirection="75 75" />
2844+ <PathPointType Anchor="75 -75" LeftDirection="75 -75" RightDirection="75 -75" />
28452845 </PathPointArray>
28462846 </GeometryPathType>
28472847 </PathGeometry>
28482848 </Properties>
2849− <Image Self="ue6" ItemTransform="1.00000 0 0 1.00000 -75.00000 -75.00000">
2849+ <Image Self="ue6" ItemTransform="1 0 0 1 -75 -75">
28502850 <Properties>
28512851 <Profile type="string">
28522852 $ID/Embedded
@@ -2869,20 +2869,20 @@ These should not be escaped: \$ \\ \&gt; \[ \{</Content>
28692869 <Content>Here is a movie </Content>
28702870 </CharacterStyleRange>
28712871 <CharacterStyleRange AppliedCharacterStyle="$ID/NormalCharacterStyle">
2872− <Rectangle Self="uec" StrokeWeight="0" ItemTransform="1.00000 0 0 1.00000 10.00000 -11.00000">
2872+ <Rectangle Self="uec" StrokeWeight="0" ItemTransform="1 0 0 1 10 -11">
28732873 <Properties>
28742874 <PathGeometry>
28752875 <GeometryPathType PathOpen="false">
28762876 <PathPointArray>
2877− <PathPointType Anchor="-10.00000 -11.00000" LeftDirection="-10.00000 -11.00000" RightDirection="-10.00000 -11.00000" />
2878− <PathPointType Anchor="-10.00000 11.00000" LeftDirection="-10.00000 11.00000" RightDirection="-10.00000 11.00000" />
2879− <PathPointType Anchor="10.00000 11.00000" LeftDirection="10.00000 11.00000" RightDirection="10.00000 11.00000" />
2880− <PathPointType Anchor="10.00000 -11.00000" LeftDirection="10.00000 -11.00000" RightDirection="10.00000 -11.00000" />
2877+ <PathPointType Anchor="-10 -11" LeftDirection="-10 -11" RightDirection="-10 -11" />
2878+ <PathPointType Anchor="-10 11" LeftDirection="-10 11" RightDirection="-10 11" />
2879+ <PathPointType Anchor="10 11" LeftDirection="10 11" RightDirection="10 11" />
2880+ <PathPointType Anchor="10 -11" LeftDirection="10 -11" RightDirection="10 -11" />
28812881 </PathPointArray>
28822882 </GeometryPathType>
28832883 </PathGeometry>
28842884 </Properties>
2885− <Image Self="ue6" ItemTransform="1.00000 0 0 1.00000 -10.00000 -11.00000">
2885+ <Image Self="ue6" ItemTransform="1 0 0 1 -10 -11">
28862886 <Properties>
28872887 <Profile type="string">
28882888 $ID/Embedded