예제 #1
0
        private void ShowFlowDocument(HtmlToFlowDocument.Dom.FlowDocument flowDocument, Dictionary <string, string> fontDictionary)
        {
            if (null != flowDocument)
            {
                var renderer = new HtmlToFlowDocument.Rendering.WpfRenderer()
                {
                    InvertColors = Controller.IsBookInDarkMode, AttachDomAsTags = true, FontDictionary = fontDictionary
                };
                renderer.TemplateBindingViewportWidth = new Binding(nameof(ViewPortProperties.Width))
                {
                    Source = _viewPortProperties
                };
                renderer.TemplateBindingViewportHeight = new Binding(nameof(ViewPortProperties.Height))
                {
                    Source = _viewPortProperties
                };

                var flowDocumentE = renderer.Render(flowDocument);

                flowDocumentE.IsColumnWidthFlexible = false;

                // var binding1 = new Binding("ActualWidth") { Source = _guiViewer };
                // flowDocumentE.SetBinding(FlowDocument.ColumnWidthProperty, binding1); // Make sure the ColumnWidth property is same as the actual width of the flow document

                // Note: binding ActualHeight to PageHeight seems not a good idea,
                // since the Zoom have to be taken into account. Otherwise zooming to increase font size will no longer work

                _guiViewer.Document = flowDocumentE;

                flowDocumentE.ColumnWidth = double.MaxValue;
                flowDocumentE.ColumnGap   = 0;                // ColumnWidth=infinity and columngap=0 are neccessary to span the text to a maximum width
                flowDocumentE.PagePadding = new Thickness(8); // set page padding to zero instead of auto


                CalculateViewPortProperties();
            }
            else
            {
                _guiViewer.Document = null;
            }
        }
예제 #2
0
        private void ShowFlowDocument(HtmlToFlowDocument.Dom.FlowDocument flowDocument, Dictionary <string, string> fontDictionary)
        {
            if (null != flowDocument)
            {
                var renderer = new HtmlToFlowDocument.Rendering.WpfRenderer()
                {
                    InvertColors = Controller.IsBookInDarkMode, AttachDomAsTags = true, FontDictionary = fontDictionary
                };
                var flowDocumentE = renderer.Render(flowDocument);

                flowDocumentE.IsColumnWidthFlexible = false;

                var binding = new Binding("ActualWidth")
                {
                    Source = _guiViewer
                };
                flowDocumentE.SetBinding(FlowDocument.ColumnWidthProperty, binding); // Make sure the ColumnWidth property is same as the actual width of the flow document
                _guiViewer.Document = flowDocumentE;
            }
            else
            {
                _guiViewer.Document = null;
            }
        }
예제 #3
0
        private void EhLoaded(object sender, RoutedEventArgs e)
        {
            _guiMenuItem_IsInAudioMode.IsChecked = Controller.IsInAudioMode;

            if (SpeechWorker_Windows10.IsSupportedByThisComputer())
            {
                _speech = new SpeechWorker_Windows10()
                {
                    IsInDarkMode = Controller.IsBookInDarkMode, Dispatcher = this.Dispatcher
                }
            }
            ;
            else
            {
                _speech = new SpeechWorker()
                {
                    IsInDarkMode = Controller.IsBookInDarkMode
                }
            };

            _speech.ApplySettings(Controller.Settings.SpeechSettings);
            _speech.SpeechCompleted += EhSpeechCompleted;

            Microsoft.Win32.SystemEvents.PowerModeChanged += EhPowerModeChanged; // Attention: unsubscribe to this event if the application is closing

            _guiDictionary.Controller.LoadDictionariesUsingSettings(Controller.Settings.DictionarySettings);

            HtmlToFlowDocument.Dom.FlowDocument document       = null;
            Dictionary <string, string>         fontDictionary = null;
            bool wasNewBookOpened;

            if (null != Gui.StartupSettings.StartupArguments && Gui.StartupSettings.StartupArguments.Length > 0)
            {
                (document, fontDictionary) = Controller.OpenEbook(Gui.StartupSettings.StartupArguments[0]);
                wasNewBookOpened           = 0 != string.Compare(Gui.StartupSettings.StartupArguments[0], Controller.Settings.BookSettings.BookFileName, true);
            }
            else
            {
                (document, fontDictionary) = Controller.ReopenEbook();
                wasNewBookOpened           = false;
            }


            _guiViewer.Zoom = Controller.Settings.BookSettings.Zoom;
            ShowFlowDocument(document, fontDictionary);
            SlobViewer.Gui.GuiActions.UpdateUnloadSubmenus(_guiDictionary.Controller, _guiUnloadMenuItem);

            bool navigated = false;

            if (Controller.Settings.BookSettings.Bookmark != null && !wasNewBookOpened)
            {
                var flowDocument = (FlowDocument)_guiViewer.Document;
                var textElement  = HtmlToFlowDocument.Rendering.WpfHelper.GetTextElementFromBookmark(flowDocument, Controller.Settings.BookSettings.Bookmark);
                if (null != textElement)
                {
                    textElement.BringIntoView();
                    if (textElement is TextElement te)
                    {
                        _guiViewer.Selection.Select(te.ContentStart, te.ContentEnd);
                    }
                    _guiViewer.Focus();
                    navigated = true;
                }
            }

            if (!navigated)
            {
                if (_guiViewer.CanGoToPage(Controller.Settings.BookSettings.PageNumber))
                {
                    _guiViewer.GoToPage(Controller.Settings.BookSettings.PageNumber);
                }
            }
        }
예제 #4
0
        public (HtmlToFlowDocument.Dom.FlowDocument Document, Dictionary <string, string> FontDictionary) OpenEbook(string fileName)
        {
            var epubBook = EpubReader.ReadBook(fileName);

            _bookContent = epubBook.Content;

            Dictionary <string, EpubTextContentFile> htmlFiles = _bookContent.Html;
            Dictionary <string, EpubTextContentFile> cssFiles  = _bookContent.Css;
            var readingOrder = epubBook.ReadingOrder;

            // ----------------- handle fonts ------------------------------
            var fontDictionary = new Dictionary <string, string>(); // Key is the font name, value is the absolute path to the font file
            var fontPath       = Path.Combine(_instanceStorageService.InstanceStoragePath, "Fonts");

            Directory.CreateDirectory(fontPath);

            foreach (var entry in _bookContent.Fonts)
            {
                var fontName     = entry.Key;
                var bytes        = entry.Value;
                var fontFileName = Path.GetFileName(entry.Value.FileName);
                fontFileName = Path.Combine(fontPath, fontFileName);
                using (var stream = new FileStream(fontFileName, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    var byteArray = bytes.Content;
                    stream.Write(byteArray, 0, byteArray.Length);
                }
                fontDictionary.Add(fontName, fontFileName);
            }

            // -------------------------------------------------------------

            string GetStyleSheet(string name, string htmlFileNameReferencedFrom)
            {
                EpubTextContentFile cssFile;
                // calculate absolute name with reference to htmlFileNameReferencedFrom
                var absoluteName = HtmlToFlowDocument.CssStylesheets.GetAbsoluteFileNameForFileRelativeToHtmlFile(name, htmlFileNameReferencedFrom);

                if (cssFiles.TryGetValue(absoluteName, out cssFile))
                {
                    return(cssFile.Content);
                }

                // if this could not resolve the name, then try to go to parent directories
                while (htmlFileNameReferencedFrom.Contains("/"))
                {
                    var idx = htmlFileNameReferencedFrom.LastIndexOf("/");
                    htmlFileNameReferencedFrom = htmlFileNameReferencedFrom.Substring(0, idx - 1);
                    absoluteName = HtmlToFlowDocument.CssStylesheets.GetAbsoluteFileNameForFileRelativeToHtmlFile(name, htmlFileNameReferencedFrom);
                    if (cssFiles.TryGetValue(absoluteName, out cssFile))
                    {
                        return(cssFile.Content);
                    }
                }

                // if this was not successful, then try it with the name alone
                if (cssFiles.TryGetValue(name, out cssFile))
                {
                    return(cssFile.Content);
                }

                return(null);
                // throw new ArgumentException($"CssFile {name} was not found!", nameof(name));
            }

            // Entire HTML content of the book
            var converter = new HtmlToFlowDocument.Converter()
            {
                AttachSourceAsTags = true
            };
            var flowDocument = new HtmlToFlowDocument.Dom.FlowDocument();

            foreach (EpubTextContentFile htmlFile in readingOrder)
            {
                string htmlContent = htmlFile.Content;
                var    textElement = converter.ConvertXHtml(htmlContent, false, GetStyleSheet, htmlFile.FileName); // create sections
                flowDocument.AppendChild(textElement);                                                             // and add them to the flow document
            }
            Settings.BookSettings.BookFileName = fileName;
            return(flowDocument, fontDictionary);
        }