git.iko.soy/typeable/pandoc
on master help sign in
Docx reader: image attributes
commit37931cb0c56c8ae770f696a31f7beba1d6cb0f08 authormb21 <mb21@users.noreply.github.com> dateSun 6 Dec 2015, 00:41 (11 years ago) parent2060f5f
6 files changed+38−16
src/Text/Pandoc/Readers/Docx.hs+11−4 src/Text/Pandoc/Readers/Docx/Parse.hs+21−6 tests/docx/image_no_embed.native+1−1 tests/docx/image_no_embed_writer.native+1−1 tests/docx/inline_images.native+2−2 tests/docx/inline_images_writer.native+2−2
src/Text/Pandoc/Readers/Docx.hs+11−4
@@ -298,10 +298,17 @@ runToInlines (Footnote bps) = do
298298runToInlines (Endnote bps) = do
299299 blksList <- concatReduce <$> (mapM bodyPartToBlocks bps)
300300 return $ note blksList
301−runToInlines (InlineDrawing fp bs) = do
301+runToInlines (InlineDrawing fp bs ext) = do
302302 mediaBag <- gets docxMediaBag
303303 modify $ \s -> s { docxMediaBag = insertMedia fp Nothing bs mediaBag }
304− return $ image fp "" ""
304+ return $ imageWith (extentToAttr ext) fp "" ""
305+
306+extentToAttr :: Extent -> Attr
307+extentToAttr (Just (w, h)) =
308+ ("", [], [("width", showDim w), ("height", showDim h)] )
309+ where
310+ showDim d = show (d / 914400) ++ "in"
311+extentToAttr _ = nullAttr
305312
306313parPartToInlines :: ParPart -> DocxContext Inlines
307314parPartToInlines (PlainRun r) = runToInlines r
@@ -348,10 +355,10 @@ parPartToInlines (BookMark _ anchor) =
348355 unless inHdrBool
349356 (modify $ \s -> s { docxAnchorMap = M.insert anchor newAnchor anchorMap})
350357 return $ spanWith (newAnchor, ["anchor"], []) mempty
351−parPartToInlines (Drawing fp bs) = do
358+parPartToInlines (Drawing fp bs ext) = do
352359 mediaBag <- gets docxMediaBag
353360 modify $ \s -> s { docxMediaBag = insertMedia fp Nothing bs mediaBag }
354− return $ image fp "" ""
361+ return $ imageWith (extentToAttr ext) fp "" ""
355362parPartToInlines (InternalHyperLink anchor runs) = do
356363 ils <- concatReduce <$> mapM runToInlines runs
357364 return $ link ('#' : anchor) "" ils
src/Text/Pandoc/Readers/Docx/Parse.hs+21−6
@@ -35,6 +35,7 @@ module Text.Pandoc.Readers.Docx.Parse ( Docx(..)
3535 , Body(..)
3636 , BodyPart(..)
3737 , TblLook(..)
38+ , Extent
3839 , ParPart(..)
3940 , Run(..)
4041 , RunElem(..)
@@ -62,6 +63,7 @@ import Control.Monad.Reader
6263import Control.Applicative ((<|>))
6364import qualified Data.Map as M
6465import Text.Pandoc.Compat.Except
66+import Text.Pandoc.Shared (safeRead)
6567import Text.TeXMath.Readers.OMML (readOMML)
6668import Text.Pandoc.Readers.Docx.Fonts (getUnicode, Font(..))
6769import Text.TeXMath (Exp)
@@ -196,20 +198,23 @@ data Row = Row [Cell]
196198data Cell = Cell [BodyPart]
197199 deriving Show
198200
201+-- (width, height) in EMUs
202+type Extent = Maybe (Double, Double)
203+
199204data ParPart = PlainRun Run
200205 | Insertion ChangeId Author ChangeDate [Run]
201206 | Deletion ChangeId Author ChangeDate [Run]
202207 | BookMark BookMarkId Anchor
203208 | InternalHyperLink Anchor [Run]
204209 | ExternalHyperLink URL [Run]
205− | Drawing FilePath B.ByteString
210+ | Drawing FilePath B.ByteString Extent
206211 | PlainOMath [Exp]
207212 deriving Show
208213
209214data Run = Run RunStyle [RunElem]
210215 | Footnote [BodyPart]
211216 | Endnote [BodyPart]
212− | InlineDrawing FilePath B.ByteString
217+ | InlineDrawing FilePath B.ByteString Extent
213218 deriving Show
214219
215220data RunElem = TextRun String | LnBrk | Tab | SoftHyphen | NoBreakHyphen
@@ -619,13 +624,13 @@ expandDrawingId s = do
619624elemToParPart :: NameSpaces -> Element -> D ParPart
620625elemToParPart ns element
621626 | isElem ns "w" "r" element
622− , Just _ <- findChild (elemName ns "w" "drawing") element =
627+ , Just drawingElem <- findChild (elemName ns "w" "drawing") element =
623628 let a_ns = "http://schemas.openxmlformats.org/drawingml/2006/main"
624629 drawing = findElement (QName "blip" (Just a_ns) (Just "a")) element
625630 >>= findAttr (QName "embed" (lookup "r" ns) (Just "r"))
626631 in
627632 case drawing of
628− Just s -> expandDrawingId s >>= (\(fp, bs) -> return $ Drawing fp bs)
633+ Just s -> expandDrawingId s >>= (\(fp, bs) -> return $ Drawing fp bs $ elemToExtent drawingElem)
629634 Nothing -> throwError WrongElem
630635-- The below is an attempt to deal with images in deprecated vml format.
631636elemToParPart ns element
@@ -635,7 +640,7 @@ elemToParPart ns element
635640 >>= findAttr (elemName ns "r" "id")
636641 in
637642 case drawing of
638− Just s -> expandDrawingId s >>= (\(fp, bs) -> return $ Drawing fp bs)
643+ Just s -> expandDrawingId s >>= (\(fp, bs) -> return $ Drawing fp bs Nothing)
639644 Nothing -> throwError WrongElem
640645elemToParPart ns element
641646 | isElem ns "w" "r" element =
@@ -687,6 +692,16 @@ lookupFootnote s (Notes _ fns _) = fns >>= (M.lookup s)
687692lookupEndnote :: String -> Notes -> Maybe Element
688693lookupEndnote s (Notes _ _ ens) = ens >>= (M.lookup s)
689694
695+elemToExtent :: Element -> Extent
696+elemToExtent drawingElem =
697+ case (getDim "cx", getDim "cy") of
698+ (Just w, Just h) -> Just (w, h)
699+ _ -> Nothing
700+ where
701+ wp_ns = "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
702+ getDim at = findElement (QName "extent" (Just wp_ns) (Just "wp")) drawingElem
703+ >>= findAttr (QName at Nothing Nothing) >>= safeRead
704+
690705elemToRun :: NameSpaces -> Element -> D Run
691706elemToRun ns element
692707 | isElem ns "w" "r" element
@@ -697,7 +712,7 @@ elemToRun ns element
697712 in
698713 case drawing of
699714 Just s -> expandDrawingId s >>=
700− (\(fp, bs) -> return $ InlineDrawing fp bs)
715+ (\(fp, bs) -> return $ InlineDrawing fp bs $ elemToExtent drawingElem)
701716 Nothing -> throwError WrongElem
702717elemToRun ns element
703718 | isElem ns "w" "r" element
tests/docx/image_no_embed.native+1−1
@@ -1,2 +1,2 @@
11[Para [Str "An",Space,Str "image:"]
2−,Para [Image ("",[],[]) [] ("media/image1.jpg","")]]
2+,Para [Image ("",[],[("width","6.5in"),("height","5.508333333333334in")]) [] ("media/image1.jpg","")]]
tests/docx/image_no_embed_writer.native+1−1
@@ -1,2 +1,2 @@
11[Para [Str "An",Space,Str "image:"]
2−,Para [Image ("",[],[]) [] ("media/rId25.jpg","")]]
2+,Para [Image ("",[],[("width","0.4166666666666667in"),("height","0.4166666666666667in")]) [] ("media/rId25.jpg","")]]
tests/docx/inline_images.native+2−2
@@ -1,2 +1,2 @@
1−[Para [Str "This",Space,Str "picture",Space,Image ("",[],[]) [] ("media/image1.jpg",""),Space,Str "is",Space,Str "an",Space,Str "identicon."]
2−,Para [Str "Here",Space,Str "is",Space,Link ("",[],[]) [Str "one",Space,Image ("",[],[]) [] ("media/image2.jpg",""),Space,Str "that"] ("http://www.google.com",""),Space,Str "links."]]
1+[Para [Str "This",Space,Str "picture",Space,Image ("",[],[("width","0.8888888888888888in"),("height","0.8888888888888888in")]) [] ("media/image1.jpg",""),Space,Str "is",Space,Str "an",Space,Str "identicon."]
2+,Para [Str "Here",Space,Str "is",Space,Link ("",[],[]) [Str "one",Space,Image ("",[],[("width","0.8888888888888888in"),("height","0.8888888888888888in")]) [] ("media/image2.jpg",""),Space,Str "that"] ("http://www.google.com",""),Space,Str "links."]]
tests/docx/inline_images_writer.native+2−2
@@ -1,2 +1,2 @@
1−[Para [Str "This",Space,Str "picture",Space,Image ("",[],[]) [] ("media/rId26.jpg",""),Space,Str "is",Space,Str "an",Space,Str "identicon."]
2−,Para [Str "Here",Space,Str "is",Space,Link ("",[],[]) [Str "one",Space,Image ("",[],[]) [] ("media/rId27.jpg",""),Space,Str "that"] ("http://www.google.com",""),Space,Str "links."]]
1+[Para [Str "This",Space,Str "picture",Space,Image ("",[],[("width","0.4166666666666667in"),("height","0.4166666666666667in")]) [] ("media/rId26.jpg",""),Space,Str "is",Space,Str "an",Space,Str "identicon."]
2+,Para [Str "Here",Space,Str "is",Space,Link ("",[],[]) [Str "one",Space,Image ("",[],[("width","0.4166666666666667in"),("height","0.4166666666666667in")]) [] ("media/rId26.jpg",""),Space,Str "that"] ("http://www.google.com",""),Space,Str "links."]]