C# Class Bloom.Publish.EpubMaker

This class handles the process of creating an ePUB out of a bloom book. The process has two stages, corresponding to the way our UI displays a preview and then allows the user to save. We 'stage' the ePUB by generating all the files into a temporary folder. Then, if the user says to save, we actually zip them into an ePUB.
Inheritance: IDisposable
Show file Open project: BloomBooks/BloomDesktop Class Usage Examples

Private Properties

Property Type Description
AddAudioOverlay void
AddFontFace void
ConvertStyleFromPxToPercent bool
CopyFile void
CopyFileToEpub string
CopyImages void
CopyStyleSheets void
EmbedFonts void
FixChangedFileNames void
FixIllegalIds void
FixPictureSizes void
GetEpubFriendlyHtmlDomForPage Bloom.Book.HtmlDom
GetIdOfFile string
GetMediaType string
GetOrCreateCompressedAudioIfWavExists string
GetOverlayName string
HasClass bool
IsCompressedAudioForIdMissing bool
MakeCompressedAudio string
MakeCssLinksAppropriateForEpub void
MakeManifest void
MakeNavPage void
MakePageFile Bloom.Book.HtmlDom
MakeSpine void
RemoveBloomUiElements void
RemoveRegularStylesheets void
RemoveScripts void
RemoveSpuriousLinks void
RemoveUnwantedContent void
ToValidXmlId string

Public Methods

Method Description
Dispose ( ) : void
EnsureEditableDivsHaveIds ( XmlElement pageElt ) : void
EpubMaker ( BookThumbNailer thumbNailer, NavigationIsolator _isolator ) : NAudio.Wave
FinishEpub ( string destinationEpubPath ) : void

Finish publishing an ePUB that has been staged, by zipping it into the desired final file.

GetFontsUsed ( string bookPath, bool includeFallbackFonts ) : IEnumerable

First step of embedding fonts: determine what are used in the document. Eventually we may load each page into a DOM and use JavaScript to ask each bit of text what actual font and face it is using. For now we examine the stylesheets and collect the font families they mention.

RemoveTempIds ( XmlElement pageElt ) : void
SaveEpub ( string destinationEpubPath ) : void
StageEpub ( bool publishWithoutAudio = false ) : void

Generate all the files we will zip into the ePUB for the current book into the StagingFolder. It is required that the parent of the StagingFolder is a temporary folder into which we can copy the Readium stuff. This folder is deleted when the EpubMaker is disposed.

Private Methods

Method Description
AddAudioOverlay ( HtmlDom pageDom, string pageDocName ) : void

Create an audio overlay for the page if appropriate. We are looking for the page to contain spans with IDs. For each such ID X, we look for a file _storage.FolderPath/audio/X.mp{3,4}. If we find at least one such file, we create pageDocName_overlay.smil with appropriate contents to tell the reader how to read all such spans aloud.

AddFontFace ( StringBuilder sb, string name, string weight, string style, string path ) : void
ConvertStyleFromPxToPercent ( string stylename, double pageWidthMm, double multiplier, string &imgStyle ) : bool
CopyFile ( string srcPath, string dstPath ) : void

This supports testing without actually copying files.

CopyFileToEpub ( string srcPath ) : string
CopyImages ( HtmlDom pageDom ) : void
CopyStyleSheets ( HtmlDom pageDom ) : void
EmbedFonts ( ) : void

Try to embed the fonts we need.

FixChangedFileNames ( HtmlDom pageDom ) : void
FixIllegalIds ( HtmlDom pageDom ) : void
FixPictureSizes ( HtmlDom pageDom ) : void

Typically pictures are given an absolute size in px, which looks right given the current absolute size of the page it is on. For an ePUB, a percent size will work better. We calculate it based on the page sizes and margins in BasePage.less and commonMixins.less. The page size definitions are unlikely to change, but change might be needed here if there is a change to the main .marginBox rule in basePage.less. To partly accommodate origami pages, we adjust for parent divs with an explict style setting the percent width.

GetEpubFriendlyHtmlDomForPage ( XmlElement page ) : HtmlDom
GetIdOfFile ( string item ) : string

Since file names often start with numbers, which ePUB validation won't allow for element IDs, stick an 'f' in front. Generally clean up file name to make a valid ID as similar as possible.

GetMediaType ( string item ) : string
GetOrCreateCompressedAudioIfWavExists ( string id ) : string
GetOverlayName ( string pageDocName ) : string
HasClass ( XmlElement elt, string className ) : bool
IsCompressedAudioForIdMissing ( string id ) : bool
MakeCompressedAudio ( string wavPath ) : string

Make a compressed audio file for the specified .wav file. (Or return null if it can't be done becaus we don't have a LAME package installed.)

MakeCssLinksAppropriateForEpub ( HtmlDom dom ) : void
MakeManifest ( string coverPageImageFile ) : void
MakeNavPage ( ) : void
MakePageFile ( XmlElement pageElement, int pageIndex, int firstContentPageIndex ) : HtmlDom
MakeSpine ( System.Xml.Linq.XNamespace opf, System.Xml.Linq.XElement rootElt, string manifestPath ) : void
RemoveBloomUiElements ( HtmlDom pageDom ) : void

Remove anything that has class bloom-ui

RemoveRegularStylesheets ( HtmlDom pageDom ) : void
RemoveScripts ( HtmlDom pageDom ) : void

We don't need to make scriptable books, and if our html contains scripts (which probably won't work on most readers) we have to add various attributes. Also our scripts are external refs, which would have to be fixed.

RemoveSpuriousLinks ( HtmlDom pageDom ) : void

Clean up any dangling pointers and similar spurious data.

RemoveUnwantedContent ( HtmlDom pageDom ) : void

Remove stuff that we don't want displayed. Some e-readers don't obey display:none. Also, not shipping it saves space.

ToValidXmlId ( string item ) : string

Given a filename, attempt to make a valid XML ID that is as similar as possible. - if it's OK don't change it - if it contains spaces remove them - if it starts with an invalid character add an initial 'f' - change other invalid characters to underlines We do this because ePUB technically uses XHTML and therefore follows XML rules. I doubt most readers care but validators do and we would like our ebooks to validate.

Method Details

Dispose() public method

public Dispose ( ) : void
return void

EnsureEditableDivsHaveIds() public method

public EnsureEditableDivsHaveIds ( XmlElement pageElt ) : void
pageElt System.Xml.XmlElement
return void

EpubMaker() public method

public EpubMaker ( BookThumbNailer thumbNailer, NavigationIsolator _isolator ) : NAudio.Wave
thumbNailer BookThumbNailer
_isolator NavigationIsolator
return NAudio.Wave

FinishEpub() public method

Finish publishing an ePUB that has been staged, by zipping it into the desired final file.
public FinishEpub ( string destinationEpubPath ) : void
destinationEpubPath string
return void

GetFontsUsed() public static method

First step of embedding fonts: determine what are used in the document. Eventually we may load each page into a DOM and use JavaScript to ask each bit of text what actual font and face it is using. For now we examine the stylesheets and collect the font families they mention.
public static GetFontsUsed ( string bookPath, bool includeFallbackFonts ) : IEnumerable
bookPath string
includeFallbackFonts bool
return IEnumerable

RemoveTempIds() public method

public RemoveTempIds ( XmlElement pageElt ) : void
pageElt System.Xml.XmlElement
return void

SaveEpub() public method

public SaveEpub ( string destinationEpubPath ) : void
destinationEpubPath string
return void

StageEpub() public method

Generate all the files we will zip into the ePUB for the current book into the StagingFolder. It is required that the parent of the StagingFolder is a temporary folder into which we can copy the Readium stuff. This folder is deleted when the EpubMaker is disposed.
public StageEpub ( bool publishWithoutAudio = false ) : void
publishWithoutAudio bool
return void