/
EPubFileV2.cs
764 lines (667 loc) · 26.3 KB
/
EPubFileV2.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using EPubLibrary.Container;
using EPubLibrary.Content;
using EPubLibrary.Content.CalibreMetadata;
using EPubLibrary.Content.NavigationManagement;
using EPubLibrary.CSS_Items;
using EPubLibrary.PathUtils;
using EPubLibrary.Template;
using EPubLibrary.XHTML_Items;
using FontSettingsContracts;
using FontsSettings;
using ICSharpCode.SharpZipLib.Zip;
using TranslitRu;
using EPubLibrary.AppleEPubV2Extensions;
using EPubLibraryContracts;
using EPubLibraryContracts.Settings;
using TranslitRuContracts;
using XHTMLClassLibrary.BaseElements;
namespace EPubLibrary
{
/// <summary>
/// Used to connect to logger
/// </summary>
static public class Logger
{
// Create a logger for use in this class
public static readonly log4net.ILog Log = log4net.LogManager.GetLogger(Assembly.GetExecutingAssembly().GetType());
}
/// <summary>
/// Extends XNode class with function that estimates size of generated data
/// </summary>
internal static class XNodetemExtender
{
public static ulong EstimateSize(this XNode node)
{
var stream = new MemoryStream();
using (var writer = XmlWriter.Create(stream))
{
node.WriteTo(writer);
}
return (ulong)stream.Length;
}
}
/// <summary>
/// This class represent the actual ePub file to be generated
/// </summary>
public class EPubFileV2 : IEpubFile
{
#region readonly_private_propeties
private readonly ZipEntryFactory _zipFactory = new ZipEntryFactory();
private readonly CSSFile _mainCss = new CSSFile { ID = "mainCSS", FileName = "main.css" };
private readonly AdobeTemplate _adobeTemplate = new AdobeTemplate();
private readonly List<CSSFile> _cssFiles = new List<CSSFile>();
private readonly List<IBaseXHTMLFile> _sections = new List<IBaseXHTMLFile>();
private readonly NavigationManagerV2 _navigationManager = new NavigationManagerV2();
private readonly List<string> _aboutTexts = new List<string>();
private readonly List<string> _aboutLinks = new List<string>();
private readonly CSSFontSettingsCollection _fontSettings = new CSSFontSettingsCollection();
private readonly AppleDisplayOptionsFile _appleOptionsFile = new AppleDisplayOptionsFile();
private readonly Dictionary<string, EPUBImage> _images = new Dictionary<string, EPUBImage>();
private readonly ICalibreMetadata _calibreMetadata = new CalibreMetadataObject();
private readonly ContentFileV2 _content = new ContentFileV2();
private readonly SectionIDTracker _sectionIDTracker = new SectionIDTracker();
private readonly IEPubV2Settings _v2Settings;
private readonly IEPubCommonSettings _commonSettings;
private readonly BookInformationData _bookInformation = new BookInformationData();
#endregion
#region private_properties
private ITransliterationSettings _translitMode;
#endregion
#region public_properties
public EPubFileV2(IEPubCommonSettings commonSettings, IEPubV2Settings v2Settings)
{
_v2Settings = v2Settings;
_commonSettings = commonSettings;
SetupAppleSettings();
_content.FlatStructure = commonSettings.FlatStructure;
}
public IBookInformationData BookInformation
{
get { return _bookInformation; }
}
/// <summary>
/// Used to set creator software string
/// </summary>
public string CreatorSoftwareString {
set { _content.CreatorSoftwareString = value; }
}
/// <summary>
/// Return Calibre's metadata object
/// </summary>
public ICalibreMetadata CalibreMetadata { get { return _calibreMetadata; } }
/// <summary>
/// Controls if Lord Kiron's license need to be added to file
/// </summary>
public bool InjectLKRLicense { private get; set; }
/// <summary>
/// Return reference to the list of the CSS style files
/// </summary>
public List<CSSFile> CSSFiles { get { return _cssFiles; } }
/// <summary>
/// Return reference to the list of images
/// </summary>
public Dictionary<string,EPUBImage> Images
{
get { return _images; }
}
/// <summary>
/// Set/get Annotation object
/// </summary>
public AnnotationPageFileV2 AnnotationPage { get; set; }
/// <summary>
/// Strings added to about page
/// </summary>
public List<string> AboutTexts
{
get
{
return _aboutTexts;
}
}
/// <summary>
/// Links added to about page
/// </summary>
public List<string> AboutLinks
{
get
{
return _aboutLinks;
}
}
#endregion
#region Transliteration_common_properties
/// <summary>
/// Transliteration mode
/// </summary>
public ITransliterationSettings TranslitMode
{
get { return _translitMode; }
set { _translitMode = value; }
}
#endregion
#region public_functions
public void AddXHTMLFile(IBaseXHTMLFile file)
{
file.StyleFiles.Add(_mainCss);
if (_v2Settings.EnableAdobeTemplate)
{
file.StyleFiles.Add(_adobeTemplate);
}
_sections.Add(file);
}
/// <summary>
/// Assign ePub fonts from set of Fonts settings
/// </summary>
/// <param name="fonts">font settings to use</param>
/// <param name="resourcesPath">path to program's "resources"</param>
/// <param name="decorateFontNames">add or not random decoration to the font names</param>
public void SetEPubFonts(IEPubFontSettings fonts, string resourcesPath, bool decorateFontNames)
{
_fontSettings.ResourceMask = resourcesPath;
_fontSettings.Load(fonts, decorateFontNames ? _bookInformation.Identifiers[0].IdentifierName : string.Empty);
}
/// <summary>
/// Writes (generates) file to disk
/// </summary>
/// <param name="outFileName"></param>
public void Generate(string outFileName)
{
Logger.Log.DebugFormat("Generating file : {0}", outFileName);
if (!IsValid())
{
Logger.Log.Error("File data not valid. Can't generate. Aborting.");
throw new InvalidDataException("File data not valid. Can't generate.");
}
try
{
string folder = Path.GetDirectoryName(outFileName);
if (!string.IsNullOrEmpty(folder))
{
Directory.CreateDirectory(folder);
}
}
catch (Exception ex)
{
Logger.Log.ErrorFormat("Error creating folder {0}, exception thrown : {1}", Path.GetDirectoryName(outFileName), ex);
}
try
{
using (var fileStream = File.Create(outFileName))
{
using (var s = new ZipOutputStream(fileStream))
{
// The EPub does not like 64 bit "patched" headers due to mimetype entry
s.UseZip64 = UseZip64.Off;
AddMimeTypeEntry(s);
AddCSSData();
AddBookData(s);
AddMetaData(s);
s.Finish();
}
fileStream.Close();
}
}
catch (Exception ex)
{
Logger.Log.ErrorFormat("Error generating file, exception thrown : {0}", ex);
File.Delete(outFileName);
throw;
}
}
private void AddCSSData()
{
AddFontsToCSS(_fontSettings.Fonts);
AddCssElementsToCSS(_fontSettings.CssElements);
}
#endregion
#region private_functions
/// <summary>
/// Check if epub file data is valid (ready to be generated)
/// </summary>
/// <returns></returns>
private bool IsValid()
{
if ((_bookInformation != null) &&
!_bookInformation.IsValid())
{
return false;
}
return true;
}
/// <summary>
/// Adds content of META-INF subfolder
/// </summary>
/// <param name="stream"></param>
private void AddMetaData(ZipOutputStream stream)
{
AddMetaDataFile(stream);
AddAppleOptionsFile(stream); // generate apple options file with custom fonts option allowed
}
/// <summary>
/// Add file containing options for Apple based readers
/// </summary>
/// <param name="stream"></param>
private void AddAppleOptionsFile(ZipOutputStream stream)
{
stream.SetLevel(9);
CreateFileEntryInZip(stream, _appleOptionsFile);
_appleOptionsFile.Write(stream);
stream.CloseEntry();
}
/// <summary>
/// Adds container.xml
/// </summary>
/// <param name="stream"></param>
private void AddMetaDataFile(ZipOutputStream stream)
{
stream.SetLevel(9);
ContainerFile container;
CreateContainer(out container);
CreateFileEntryInZip(stream, container);
container.Write(stream);
stream.CloseEntry();
}
private void CreateContainer(out ContainerFile container)
{
container = new ContainerFile { FlatStructure = _commonSettings.FlatStructure, ContentFilePath = _content };
}
/// <summary>
/// Adds actual book "context"
/// </summary>
/// <param name="stream"></param>
private void AddBookData(ZipOutputStream stream)
{
AddImages(stream);
AddFontFiles(stream);
AddAdditionalFiles(stream);
AddNavigation(stream);
AddContentFile(stream);
}
private void AddNavigation(ZipOutputStream stream)
{
_navigationManager.SetupBookNavigation(_bookInformation.Identifiers[0].ID, Rus2Lat.Instance.Translate(_bookInformation.BookTitles[0].TitleName, TranslitMode));
AddTOCFile(stream);
}
/// <summary>
/// Adds embedded font files
/// </summary>
/// <param name="stream"></param>
private void AddFontFiles(ZipOutputStream stream)
{
if (_fontSettings.NumberOfEmbededFiles > 0)
{
stream.SetLevel(9);
foreach (var embededFileLocation in _fontSettings.EmbededFilesLocations)
{
var fontFileName = new FontOnStorage(embededFileLocation, ConvertFontToMediaType(_fontSettings.GetFontFormat(embededFileLocation)));
CreateFileEntryInZip(stream,fontFileName);
try
{
EmbeddedFontsCache.Instance.WriteFontToStream(embededFileLocation,stream);
}
catch (Exception ex)
{
Logger.Log.ErrorFormat("Error loading font file {0} : {1}", embededFileLocation, ex);
continue;
}
_content.AddFontFile(fontFileName);
}
}
}
private EPubCoreMediaType ConvertFontToMediaType(FontFormat fontFormat)
{
if (fontFormat == FontFormat.WOFF)
{
return EPubCoreMediaType.ApplicationFontWoff;
}
return EPubCoreMediaType.ApplicationFontMSOpen;
}
/// <summary>
/// Adds different additional generated files like cover, annotation etc.
/// </summary>
/// <param name="stream"></param>
private void AddAdditionalFiles(ZipOutputStream stream)
{
AddAdobeTemplate(stream);
AddCSSFiles(stream);
AddTitle(stream);
AddAnnotation(stream);
AddBookContent(stream);
if (_aboutTexts.Count >0 || _aboutLinks.Count > 0)
{
AddAbout(stream);
}
}
/// <summary>
/// Adds Adobe XGTP template
/// </summary>
/// <param name="stream"></param>
private void AddAdobeTemplate(ZipOutputStream stream)
{
if (!_v2Settings.EnableAdobeTemplate || string.IsNullOrEmpty(_v2Settings.AdobeTemplatePath))
{
return;
}
stream.SetLevel(9);
_adobeTemplate.TemplateFileInputPath = _v2Settings.AdobeTemplatePath;
try
{
_adobeTemplate.Load();
CreateFileEntryInZip(stream,_adobeTemplate);
_adobeTemplate.Write(stream);
_content.AddXPGTTemplate(_adobeTemplate);
}
catch (Exception)
{
Logger.Log.ErrorFormat("Exception adding template, template will not be added");
}
}
/// <summary>
/// Adds annotation page file
/// </summary>
/// <param name="stream"></param>
private void AddAnnotation(ZipOutputStream stream)
{
if (AnnotationPage != null)
{
stream.SetLevel(9);
CreateFileEntryInZip(stream, AnnotationPage);
PutPageToFile(stream, AnnotationPage);
_content.AddXHTMLTextItem(AnnotationPage);
}
}
/// <summary>
/// Adds CSS styles files
/// </summary>
/// <param name="stream"></param>
private void AddCSSFiles(ZipOutputStream stream)
{
foreach (var cssFile in _cssFiles)
{
_mainCss.Load(cssFile.FilePathOnDisk, true);
}
if (_commonSettings.EmbedStyles)
{
return;
}
AddMainCSS(stream);
}
/// <summary>
/// Add CSS file to ZIP stream
/// </summary>
/// <param name="stream"></param>
private void AddMainCSS(ZipOutputStream stream)
{
stream.SetLevel(9);
CreateFileEntryInZip(stream, _mainCss);
_mainCss.Write(stream);
_content.AddCSS(_mainCss);
}
/// <summary>
/// Create a file in the ZIP stream
/// </summary>
/// <param name="stream"></param>
/// <param name="pathObject"></param>
private void CreateFileEntryInZip(ZipOutputStream stream, IEPubPath pathObject)
{
ZipEntry file = _zipFactory.MakeFileEntry(pathObject.PathInEPUB.GetFilePathInZip(_commonSettings.FlatStructure), false);
file.CompressionMethod = CompressionMethod.Deflated; // as defined by ePub stndard
stream.PutNextEntry(file);
}
/// <summary>
/// Adds title page file
/// </summary>
/// <param name="stream"></param>
private void AddTitle(ZipOutputStream stream)
{
var titlePage = new TitlePageFileV2(_bookInformation);
//if (titlePage != null)
{
stream.SetLevel(9);
CreateFileEntryInZip(stream, titlePage);
PutPageToFile(stream, titlePage);
_content.AddXHTMLTextItem(titlePage);
}
}
private void PutPageToFile(ZipOutputStream stream, IBaseXHTMLFile xhtmlFile)
{
xhtmlFile.FlatStructure = _commonSettings.FlatStructure;
xhtmlFile.EmbedStyles = _commonSettings.EmbedStyles;
xhtmlFile.StyleFiles.Add(_mainCss);
if (_v2Settings.EnableAdobeTemplate)
{
xhtmlFile.StyleFiles.Add(_adobeTemplate);
}
xhtmlFile.Write(stream);
}
/// <summary>
/// Adds "About" page file
/// </summary>
/// <param name="stream"></param>
private void AddAbout(ZipOutputStream stream)
{
stream.SetLevel(9);
var aboutPage = new AboutPageFileV2()
{
FlatStructure = _commonSettings.FlatStructure,
EmbedStyles = _commonSettings.EmbedStyles,
AboutLinks = _aboutLinks,
AboutTexts = _aboutTexts
};
CreateFileEntryInZip(stream,aboutPage);
PutPageToFile(stream,aboutPage);
_content.AddXHTMLTextItem(aboutPage);
}
/// <summary>
/// Writes book content to the stream
/// </summary>
/// <param name="stream">stream to write to</param>
private void AddBookContent(ZipOutputStream stream)
{
int count = 1;
stream.SetLevel(9);
foreach (var section in _sections)
{
section.FlatStructure = _commonSettings.FlatStructure;
section.EmbedStyles = _commonSettings.EmbedStyles;
section.MaxSize = _v2Settings.HTMLFileMaxSize;
if (string.IsNullOrEmpty(section.FileName)) // if file name not defined yet create our own (not converter case)
{
section.FileName = string.Format(@"section{0}.xhtml", count);
}
if (section.MaxSize != 0)
{
count = SplitAndAddSubSections(stream, section, count);
}
else
{
CreateFileEntryInZip(stream, section);
section.Write(stream);
AddBookContentSection(section);
count++;
}
}
// remove navigation leaf end points with empty names
_navigationManager.Consolidate();
// to be valid we need at least one NAVPoint
if (_navigationManager.TableOfContentFile.IsNavMapEmpty() && (_sections.Count > 0))
{
_navigationManager.AddBookSubsection(_sections[0], _commonSettings.TransliterateToc ? Rus2Lat.Instance.Translate(_bookInformation.BookTitles[0].TitleName, _translitMode) : _bookInformation.BookTitles[0].TitleName);
}
}
private int SplitAndAddSubSections(ZipOutputStream stream, IBaseXHTMLFile section, int count)
{
int newCount = count;
XDocument document = section.Generate();
ulong docSize = document.EstimateSize();
if (docSize >= section.MaxSize)
{
// This case is not for converter
// after converter the files should be in right size already
int subCount = 0;
var baseXHTMLFileV2 = section as BaseXHTMLFileV2;
if (baseXHTMLFileV2 == null)
{
throw new ArgumentException("The section passed has to be BaseXHTMLFileV2");
}
foreach (var subsection in baseXHTMLFileV2.Split())
{
subsection.FlatStructure = _commonSettings.FlatStructure;
subsection.EmbedStyles = _commonSettings.EmbedStyles;
subsection.FileName = string.Format("{0}_{1}.xhtml",
Path.GetFileNameWithoutExtension(section.FileName), subCount);
CreateFileEntryInZip(stream, subsection);
subsection.Write(stream);
AddBookContentSection(subsection);
subCount++;
}
newCount++;
}
else
{
CreateFileEntryInZip(stream, section);
section.Write(stream);
AddBookContentSection(section);
newCount++;
}
return newCount;
}
private void AddBookContentSection(IBaseXHTMLFile subsection)
{
subsection.Id = _sectionIDTracker.GenerateSectionId(subsection);
_content.AddXHTMLTextItem(subsection);
_navigationManager.AddBookSubsection(subsection,
_commonSettings.TransliterateToc? Rus2Lat.Instance.Translate(subsection.PageTitle, _translitMode):subsection.PageTitle);
}
private void AddTOCFile(ZipOutputStream stream)
{
stream.SetLevel(9);
CreateFileEntryInZip(stream, _navigationManager.TableOfContentFile);
_navigationManager.TableOfContentFile.Write(stream);
_content.AddTOC();
}
private void AddContentFile(ZipOutputStream stream)
{
stream.SetLevel(9);
CreateFileEntryInZip(stream,_content);
_content.BookInformation = _bookInformation;
if (_v2Settings.AddCalibreMetadata)
{
_content.CalibreData = _calibreMetadata;
}
_content.Write(stream);
}
private void AddImages(ZipOutputStream stream)
{
if (_images.Count > 0)
{
AddImagesFiles(stream);
}
}
private void AddImagesFiles(ZipOutputStream stream)
{
stream.SetLevel(9);
foreach (var epubImage in _images)
{
var imageFile = new ImageOnStorage(epubImage.Value) {FileName = epubImage.Value.ID};
CreateFileEntryInZip(stream,imageFile);
stream.Write(epubImage.Value.ImageData, 0, epubImage.Value.ImageData.Length);
_content.AddImage(imageFile);
}
}
private void AddMimeTypeEntry(ZipOutputStream s)
{
s.SetLevel(0);
ZipEntry entry = _zipFactory.MakeFileEntry("mimetype",false);
entry.CompressionMethod = CompressionMethod.Stored;
entry.IsUnicodeText = false;
entry.ZipFileIndex = 0;
s.PutNextEntry(entry);
const string mimetype = "application/epub+zip";
var encoding = new ASCIIEncoding();
byte[] buffer = encoding.GetBytes(mimetype);
s.Write(buffer, 0, buffer.Length);
s.CloseEntry();
}
private void AddCssElementsToCSS(Dictionary<string, Dictionary<string, List<ICSSFontFamily>>> cssElements)
{
// Now add the elements
foreach (var elementName in cssElements.Keys)
{
foreach (var elementClass in cssElements[elementName].Keys)
{
var cssItem = new BaseCSSItem();
var sb = new StringBuilder();
sb.Append(elementName);
if (!string.IsNullOrEmpty(elementClass))
{
sb.AppendFormat(".{0}",elementClass);
}
cssItem.Name = sb.ToString();
// now build a list of fonts
sb.Clear();
int counter = 0;
if (elementClass != null)
{
foreach (var fontFamily in cssElements[elementName][elementClass])
{
sb.AppendFormat("\"{0}\"", fontFamily.Name);
if (counter != 0)
{
sb.Append(", ");
}
counter++;
}
}
cssItem.Parameters.Add("font-family", sb.ToString());
_mainCss.AddTarget(cssItem);
}
}
}
private void AddFontsToCSS(Dictionary<string, ICSSFontFamily> fontsFamilies)
{
// Add the fonts to CSS
foreach (var cssFontFamily in fontsFamilies)
{
foreach (var subFont in cssFontFamily.Value.Fonts)
{
var cssFont = new CssFontDefinition
{
Family = cssFontFamily.Key,
FontStyle = CssFontDefinition.FromStyle(subFont.FontStyle),
FontWidth = CssFontDefinition.FromWidth(subFont.FontWidth)
};
var sources = subFont.Sources.Select(fontSource => CssFontDefinition.ConvertToSourceString(fontSource, _commonSettings)).ToList();
cssFont.FontSrcs = sources;
_mainCss.AddFont(cssFont);
}
}
}
private void SetupAppleSettings()
{
// setup epub2 options
_appleOptionsFile.SetSettings(_v2Settings.AppleConverterEPubSettings);
}
#endregion
public IBaseXHTMLFile GetIDOfParentDocument(IHTMLItem value)
{
return _sections.FirstOrDefault(document =>
{
var baseXHTMLFileV2 = document as BaseXHTMLFileV2;
return baseXHTMLFileV2 != null && baseXHTMLFileV2.PartOfDocument(value);
});
}
public void SetCoverImageID(string id)
{
_content.CoverId = id;
}
}
}