git.iko.soy/typeable/pandoc
on master help sign in
Added `--epub-embed-font` option.

* This can be repeated for multiple fonts. * Added parameter for fonts to embed to writeEPUB. * Added ttf, otf to Mime types in Text.Pandoc.MIME.

commit8f1bfec7b939f80d619c0cff0bc6338f9807a6af authorJohn MacFarlane <fiddlosopher@gmail.com> dateMon 30 Jan 2012, 11:45 (15 years ago) parent34801ac
4 files changed+69−6
README+32 src/Text/Pandoc/MIME.hs+2 src/Text/Pandoc/Writers/EPUB.hs+23−5 src/pandoc.hs+12−1
README+32
@@ -442,6 +442,38 @@ Options affecting specific writers
442442 id="BookId">` (a randomly generated UUID). Any of these may be
443443 overridden by elements in the metadata file.
444444
445+`--epub-embed-font=`*FILE*
446+: Embed the specified font in the EPUB. This option can be repeated
447+ to embed multiple fonts. To use embedded fonts, you
448+ will need to add declarations like the following to your CSS (see
449+ ``--epub-stylesheet`):
450+
451+ @font-face {
452+ font-family: DejaVuSans;
453+ font-style: normal;
454+ font-weight: normal;
455+ src:url("DejaVuSans-Regular.ttf");
456+ }
457+ @font-face {
458+ font-family: DejaVuSans;
459+ font-style: normal;
460+ font-weight: bold;
461+ src:url("DejaVuSans-Bold.ttf");
462+ }
463+ @font-face {
464+ font-family: DejaVuSans;
465+ font-style: italic;
466+ font-weight: normal;
467+ src:url("DejaVuSans-Oblique.ttf");
468+ }
469+ @font-face {
470+ font-family: DejaVuSans;
471+ font-style: italic;
472+ font-weight: bold;
473+ src:url("DejaVuSans-BoldOblique.ttf");
474+ }
475+ body { font-family: "DejaVuSans"; }
476+
445477`--latex-engine=`*pdflatex|lualatex|xelatex*
446478: Use the specified LaTeX engine when producing PDF output.
447479 The default is `pdflatex`. If the engine is not in your PATH,
src/Text/Pandoc/MIME.hs+2
@@ -295,6 +295,7 @@ getMimeType f = M.lookup (map toLower $ drop 1 $ takeExtension f) mimeTypes
295295 ,("oth","application/vnd.oasis.opendocument.text-web")
296296 ,("otp","application/vnd.oasis.opendocument.presentation-template")
297297 ,("ots","application/vnd.oasis.opendocument.spreadsheet-template")
298+ ,("otf","application/x-font-opentype")
298299 ,("ott","application/vnd.oasis.opendocument.text-template")
299300 ,("oza","application/x-oz-application")
300301 ,("p","text/x-pascal")
@@ -428,6 +429,7 @@ getMimeType f = M.lookup (map toLower $ drop 1 $ takeExtension f) mimeTypes
428429 ,("ts","text/texmacs")
429430 ,("tsp","application/dsptype")
430431 ,("tsv","text/tab-separated-values")
432+ ,("ttf","application/x-font-truetype")
431433 ,("txt","text/plain")
432434 ,("udeb","application/x-debian-package")
433435 ,("uls","text/iuls")
src/Text/Pandoc/Writers/EPUB.hs+23−5
@@ -32,7 +32,7 @@ import Data.IORef
3232import Data.Maybe ( fromMaybe, isNothing )
3333import Data.List ( findIndices, isPrefixOf )
3434import System.Environment ( getEnv )
35−import System.FilePath ( (</>), (<.>), takeBaseName, takeExtension )
35+import System.FilePath ( (</>), (<.>), takeBaseName, takeExtension, takeFileName )
3636import qualified Data.ByteString.Lazy as B
3737import Data.ByteString.Lazy.UTF8 ( fromString )
3838import Codec.Archive.Zip
@@ -47,13 +47,15 @@ import Text.Pandoc.Writers.HTML
4747import Text.Pandoc.Writers.Markdown ( writePlain )
4848import Data.Char ( toLower )
4949import Network.URI ( unEscapeString )
50+import Text.Pandoc.MIME (getMimeType)
5051
5152-- | Produce an EPUB file from a Pandoc document.
5253writeEPUB :: Maybe String -- ^ EPUB stylesheet specified at command line
54+ -> [FilePath] -- ^ Paths to fonts to embed
5355 -> WriterOptions -- ^ Writer options
5456 -> Pandoc -- ^ Document to convert
5557 -> IO B.ByteString
56−writeEPUB mbStylesheet opts doc@(Pandoc meta _) = do
58+writeEPUB mbStylesheet fonts opts doc@(Pandoc meta _) = do
5759 epochtime <- floor `fmap` getPOSIXTime
5860 let mkEntry path content = toEntry path epochtime content
5961 let opts' = opts{ writerEmailObfuscation = NoObfuscation
@@ -101,6 +103,10 @@ writeEPUB mbStylesheet opts doc@(Pandoc meta _) = do
101103 return e{ eRelativePath = newsrc }
102104 picEntries <- mapM readPicEntry pics
103105
106+ -- handle fonts
107+ let mkFontEntry f = mkEntry (takeFileName f) `fmap` B.readFile f
108+ fontEntries <- mapM mkFontEntry fonts
109+
104110 -- body pages
105111 let isH1 (Header 1 _) = True
106112 isH1 _ = False
@@ -137,6 +143,10 @@ writeEPUB mbStylesheet opts doc@(Pandoc meta _) = do
137143 ("href", eRelativePath ent),
138144 ("media-type", fromMaybe "application/octet-stream"
139145 $ imageTypeOf $ eRelativePath ent)] $ ()
146+ let fontNode ent = unode "item" !
147+ [("id", takeBaseName $ eRelativePath ent),
148+ ("href", eRelativePath ent),
149+ ("media-type", maybe "" id $ getMimeType $ eRelativePath ent)] $ ()
140150 let plainify t = removeTrailingSpace $
141151 writePlain opts'{ writerStandalone = False } $
142152 Pandoc meta [Plain t]
@@ -156,7 +166,8 @@ writeEPUB mbStylesheet opts doc@(Pandoc meta _) = do
156166 ,("media-type","text/css")] $ ()
157167 ] ++
158168 map chapterNode (cpgEntry ++ (tpEntry : chapterEntries)) ++
159− map pictureNode (cpicEntry ++ picEntries)
169+ map pictureNode (cpicEntry ++ picEntries) ++
170+ map fontNode fontEntries
160171 , unode "spine" ! [("toc","ncx")] $
161172 case mbCoverImage of
162173 Nothing -> []
@@ -210,6 +221,13 @@ writeEPUB mbStylesheet opts doc@(Pandoc meta _) = do
210221 ,("media-type","application/oebps-package+xml")] $ ()
211222 let containerEntry = mkEntry "META-INF/container.xml" containerData
212223
224+ -- com.apple.ibooks.display-options.xml
225+ let apple = fromString $ ppTopElement $
226+ unode "display_options" $
227+ unode "platform" ! [("name","*")] $
228+ unode "option" ! [("name","specified-fonts")] $ "true"
229+ let appleEntry = mkEntry "META-INF/com.apple.ibooks.display-options.xml" apple
230+
213231 -- stylesheet
214232 stylesheet <- case mbStylesheet of
215233 Just s -> return s
@@ -218,9 +236,9 @@ writeEPUB mbStylesheet opts doc@(Pandoc meta _) = do
218236
219237 -- construct archive
220238 let archive = foldr addEntryToArchive emptyArchive
221− (mimetypeEntry : containerEntry : stylesheetEntry : tpEntry :
239+ (mimetypeEntry : containerEntry : appleEntry : stylesheetEntry : tpEntry :
222240 contentsEntry : tocEntry :
223− (picEntries ++ cpicEntry ++ cpgEntry ++ chapterEntries) )
241+ (picEntries ++ cpicEntry ++ cpgEntry ++ chapterEntries ++ fontEntries) )
224242 return $ fromArchive archive
225243
226244metadataElement :: String -> UUID -> String -> String -> [String] -> String -> Maybe a -> Element
src/pandoc.hs+12−1
@@ -113,6 +113,7 @@ data Opt = Opt
113113 , optReferenceDocx :: Maybe FilePath -- ^ Path of reference.docx
114114 , optEPUBStylesheet :: Maybe String -- ^ EPUB stylesheet
115115 , optEPUBMetadata :: String -- ^ EPUB metadata
116+ , optEPUBFonts :: [FilePath] -- ^ EPUB fonts to embed
116117 , optDumpArgs :: Bool -- ^ Output command-line arguments
117118 , optIgnoreArgs :: Bool -- ^ Ignore command-line arguments
118119 , optStrict :: Bool -- ^ Use strict markdown syntax
@@ -163,6 +164,7 @@ defaultOpts = Opt
163164 , optReferenceDocx = Nothing
164165 , optEPUBStylesheet = Nothing
165166 , optEPUBMetadata = ""
167+ , optEPUBFonts = []
166168 , optDumpArgs = False
167169 , optIgnoreArgs = False
168170 , optStrict = False
@@ -531,6 +533,13 @@ options =
531533 "FILENAME")
532534 "" -- "Path of epub metadata file"
533535
536+ , Option "" ["epub-embed-font"]
537+ (ReqArg
538+ (\arg opt -> do
539+ return opt{ optEPUBFonts = arg : optEPUBFonts opt })
540+ "FILE")
541+ "" -- "Directory of fonts to embed"
542+
534543 , Option "" ["latex-engine"]
535544 (ReqArg
536545 (\arg opt -> do
@@ -792,6 +801,7 @@ main = do
792801 , optReferenceDocx = referenceDocx
793802 , optEPUBStylesheet = epubStylesheet
794803 , optEPUBMetadata = epubMetadata
804+ , optEPUBFonts = epubFonts
795805 , optDumpArgs = dumpArgs
796806 , optIgnoreArgs = ignoreArgs
797807 , optStrict = strict
@@ -1021,7 +1031,8 @@ main = do
10211031 case lookup writerName' writers of
10221032 Nothing
10231033 | writerName' == "epub" ->
1024− writeEPUB epubStylesheet writerOptions doc2 >>= writeBinary
1034+ writeEPUB epubStylesheet epubFonts writerOptions doc2
1035+ >>= writeBinary
10251036 | writerName' == "odt" ->
10261037 writeODT referenceODT writerOptions doc2 >>= writeBinary
10271038 | writerName' == "docx" ->