/// <summary>
 /// 加载自定义视图(通常用于在线阅读)
 /// </summary>
 /// <param name="chapters">目录列表</param>
 /// <param name="style">视图样式</param>
 /// <param name="details">目录详情列表(其中包含单个章节的内容)</param>
 public void LoadCustomView(List <Chapter> chapters, ReaderStyle style, List <ChapterDetail> details = null)
 {
     if (chapters == null || chapters.Count == 0 || style == null)
     {
         throw new ArgumentNullException();
     }
     OpenStarting?.Invoke(this, EventArgs.Empty);
     if (_tempSpeechStream != null)
     {
         _tempSpeechStream.Dispose();
         _tempSpeechStream = null;
     }
     ReaderType = _readerView.ReaderType = ReaderType.Custom;
     Chapters   = chapters;
     ChapterLoaded?.Invoke(this, Chapters);
     _readerView.ViewStyle = style;
     UpdateBackground(style);
     if (details != null)
     {
         CustomChapterDetailList = details;
     }
     else
     {
         CustomChapterDetailList = new List <ChapterDetail>();
     }
     OpenCompleted?.Invoke(this, EventArgs.Empty);
 }
Beispiel #2
0
        private void UpdateTxtStyle(ReaderStyle inputStyle = null)
        {
            if (inputStyle != null)
            {
                ViewStyle = inputStyle;
            }
            var style = ViewStyle;

            foreach (var item in _displayContainer.Children)
            {
                if (item is RichTextBlock rtb)
                {
                    rtb.FontFamily = new FontFamily(style.FontFamily);
                    rtb.FontSize   = style.FontSize;
                    rtb.Foreground = new SolidColorBrush(style.Foreground);
                    rtb.LineHeight = style.LineHeight;
                    rtb.Margin     = style.Padding;
                    var title = rtb.Blocks.FirstOrDefault();
                    if (title != null)
                    {
                        title.FontSize = style.HeaderFontSize;
                        title.Margin   = style.HeaderMargin;
                    }
                    rtb.Blocks.OfType <Paragraph>().Skip(1).ToList().ForEach(p => { p.Margin = new Thickness(0, style.SegmentSpacing / 2.0, 0, style.SegmentSpacing / 2.0); p.TextIndent = style.TextIndent * style.FontSize; });
                }
                else if (item is RichTextBlockOverflow of)
                {
                    of.Margin = style.Padding;
                }
            }
        }
Beispiel #3
0
        private void UpdateEpubStyle(ReaderStyle inputStyle = null)
        {
            if (inputStyle != null)
            {
                ViewStyle = inputStyle;
            }
            var style = ViewStyle;

            foreach (var item in _displayContainer.Children)
            {
                if (item is RichTextBlock rtb)
                {
                    rtb.FontFamily = new FontFamily(style.FontFamily);
                    rtb.FontSize   = style.FontSize;
                    rtb.Foreground = new SolidColorBrush(style.Foreground);
                    rtb.LineHeight = style.LineHeight;
                    rtb.Margin     = style.Padding;
                    rtb.Blocks.OfType <Paragraph>().Where(p => p.Inlines.Count > 0 && !(p.Inlines.First() is InlineUIContainer)).ToList().ForEach(p => { p.Margin = new Thickness(0, style.SegmentSpacing / 2.0, 0, style.SegmentSpacing / 2.0); p.TextIndent = style.TextIndent * style.FontSize; });
                }
                else if (item is RichTextBlockOverflow of)
                {
                    of.Margin = style.Padding;
                }
            }
        }
Beispiel #4
0
 public void EpubInit(EpubBook book, ReaderStyle style)
 {
     Book                = book;
     ViewStyle           = style;
     helper              = new HtmlHelper(book.Resources.Images.ToList(), style);
     helper.LinkTapped  += (_s, _e) => { LinkTapped?.Invoke(_s, _e); };
     helper.ImageTapped += (_s, _e) => { ImageTapped?.Invoke(_s, _e); };
 }
        /// <summary>
        /// 打开书籍文件
        /// </summary>
        /// <param name="bookFile">书籍文件</param>
        /// <param name="style">阅读器样式</param>
        /// <param name="chapters">外部导入的目录(通常是前一次生成的目录,以避免重复生成目录),为<c>null</c>或空列表将重新生成目录</param>
        /// <returns></returns>
        public async Task OpenAsync(StorageFile bookFile, ReaderStyle style, List <Chapter> chapters = null)
        {
            if (bookFile == null)
            {
                throw new ArgumentNullException();
            }

            string extension = Path.GetExtension(bookFile.Path).ToLower();

            if (extension != ".epub" && extension != ".txt")
            {
                throw new NotSupportedException("File type not support (Currently only support txt and epub file)");
            }
            OpenStarting?.Invoke(this, EventArgs.Empty);
            if (_tempSpeechStream != null)
            {
                _tempSpeechStream.Dispose();
                _tempSpeechStream = null;
            }
            bool hasExternalChapters = chapters != null && chapters.Count > 0;

            if (hasExternalChapters)
            {
                Chapters = chapters;
                ChapterLoaded?.Invoke(this, Chapters);
            }
            _readerView.ViewStyle = style;
            UpdateBackground(style);
            if (extension.ToLower() == ".txt")
            {
                ReaderType = _readerView.ReaderType = ReaderType.Txt;
                _readerView.SetVirtualMode(true);
                if (!hasExternalChapters)
                {
                    Chapters = await GetTxtChapters(bookFile);

                    ChapterLoaded?.Invoke(this, Chapters);
                }
                _txtContent = await GetTxtContent(bookFile);
            }
            else
            {
                _readerView.SetVirtualMode(false);
                ReaderType   = _readerView.ReaderType = ReaderType.Epub;
                _epubContent = await EpubReader.Read(bookFile, Encoding.Default);

                if (!hasExternalChapters)
                {
                    Chapters = GetEpubChapters(_epubContent);
                    ChapterLoaded?.Invoke(this, Chapters);
                }
                _readerView.EpubInit(_epubContent, style);
            }

            OpenCompleted?.Invoke(this, EventArgs.Empty);
        }
Beispiel #6
0
 public override void UpdateStyle(ReaderStyle style = null)
 {
     if (ReaderType == ReaderType.Epub)
     {
         UpdateEpubStyle(style);
     }
     else
     {
         UpdateTxtStyle(style);
     }
 }
Beispiel #7
0
        /// <summary>
        /// 更新阅读器样式(视图与样式需匹配,比如阅读Txt时需传入TxtViewStyle)
        /// </summary>
        /// <param name="style">阅读器样式</param>
        public void UpdateStyle(ReaderStyle style)
        {
            if ((ReaderType == Enums.ReaderType.Txt && !(style is TxtViewStyle)) ||
                (ReaderType == Enums.ReaderType.Epub && !(style is EpubViewStyle)))
            {
                throw new ArgumentException("The format of the view and style must match");
            }
            var view = _mainPresenter.Content as ReaderViewBase;

            if (view != null)
            {
                view.UpdateStyle(style);
            }
        }
        public override void UpdateStyle(ReaderStyle inputStyle = null)
        {
            if (inputStyle != null)
            {
                ViewStyle = inputStyle;
            }
            var style = ViewStyle as TxtViewStyle;

            if (style.IsAcrylicBackground)
            {
                var opacity        = Convert.ToInt32(style.Background.A) / 255.0;
                var tempBackground = style.Background;
                tempBackground.A = 255;
                var acrylic = new AcrylicBrush()
                {
                    TintColor        = tempBackground,
                    TintOpacity      = opacity,
                    FallbackColor    = style.Background,
                    BackgroundSource = AcrylicBackgroundSource.HostBackdrop
                };
                Background = acrylic;
            }
            else
            {
                Background = new SolidColorBrush(style.Background);
            }
            foreach (var item in _displayContainer.Children)
            {
                if (item is RichTextBlock rtb)
                {
                    rtb.FontFamily = new FontFamily(style.FontFamily);
                    rtb.FontSize   = style.FontSize;
                    rtb.Foreground = new SolidColorBrush(style.Foreground);
                    rtb.LineHeight = style.LineHeight;
                    rtb.Margin     = style.Padding;
                    var title = rtb.Blocks.FirstOrDefault();
                    if (title != null)
                    {
                        title.FontSize = style.HeaderFontSize;
                        title.Margin   = style.HeaderMargin;
                    }
                }
                else if (item is RichTextBlockOverflow of)
                {
                    of.Margin = style.Padding;
                }
            }
        }
 /// <summary>
 /// 更新阅读器背景
 /// </summary>
 /// <param name="style">阅读器样式</param>
 public void UpdateBackground(ReaderStyle style)
 {
     if (style.IsAcrylicBackground)
     {
         var opacity        = Convert.ToInt32(style.Background.A) / 255.0;
         var tempBackground = style.Background;
         tempBackground.A = 255;
         var acrylic = new AcrylicBrush()
         {
             TintColor        = tempBackground,
             TintOpacity      = opacity,
             FallbackColor    = style.Background,
             BackgroundSource = AcrylicBackgroundSource.HostBackdrop
         };
         _rootGrid.Background   = acrylic;
         _readerView.Background = acrylic;
     }
     else
     {
         _rootGrid.Background = _readerView.Background = new SolidColorBrush(style.Background);
     }
 }
Beispiel #10
0
 public virtual void UpdateStyle(ReaderStyle style = null)
 {
 }
Beispiel #11
0
        /// <summary>
        /// 打开书籍文件
        /// </summary>
        /// <param name="bookFile">书籍文件</param>
        /// <param name="style">阅读器样式</param>
        /// <returns></returns>
        public async Task OpenAsync(StorageFile bookFile, ReaderStyle style)
        {
            if (bookFile == null)
            {
                throw new ArgumentNullException();
            }

            string extension = Path.GetExtension(bookFile.Path).ToLower();

            if (extension != ".epub" && extension != ".txt")
            {
                throw new NotSupportedException("File type not support (Currently only support txt and epub file)");
            }
            OpenStarting?.Invoke(this, EventArgs.Empty);
            if (extension == ".txt")
            {
                if (!(style is TxtViewStyle))
                {
                    throw new ArgumentException("Open txt file need TxtViewStyle argument");
                }
                ReaderType = Enums.ReaderType.Txt;
                Chapters   = await GetTxtChapters(bookFile);

                ChapterLoaded?.Invoke(this, Chapters);
                if (_mainPresenter.Content == null || !(_mainPresenter.Content is TxtView))
                {
                    if (_txtView == null)
                    {
                        _txtView = new TxtView();
                        _txtView.PrevPageSelected     += OnPrevPageSelected;
                        _txtView.NextPageSelected     += OnNextPageSelected;
                        _txtView.LoadingStatusChanged += OnLoad;
                        _txtView.ProgressChanged      += OnProgressChanged;
                        _txtView.TouchHolding         += OnTouchHolding;
                        _txtView.TouchTapped          += (_s, _e) => { TouchTapped?.Invoke(_s, _e); };
                        _txtView.Loaded += (_s, _e) =>
                        {
                            _txtView.SingleColumnMaxWidth = SingleColumnMaxWidth;
                            _txtView.ReaderFlyout         = ReaderFlyout;
                            ViewLoaded?.Invoke(this, EventArgs.Empty);
                        };
                    }
                    _mainPresenter.Content = _txtView;
                }
                _txtContent = await GetTxtContent(bookFile);

                _txtView.ViewStyle = style as TxtViewStyle;
            }
            else
            {
                if (!(style is EpubViewStyle))
                {
                    throw new ArgumentException("Open epub file need EpubViewStyle argument");
                }
                ReaderType   = Enums.ReaderType.Epub;
                _epubContent = await EpubReader.Read(bookFile, Encoding.Default);

                Chapters = GetEpubChapters(_epubContent);
                ChapterLoaded?.Invoke(this, Chapters);
                if (_mainPresenter.Content == null || !(_mainPresenter.Content is EpubView))
                {
                    if (_epubView == null)
                    {
                        _epubView = new EpubView();
                        _epubView.PrevPageSelected     += OnPrevPageSelected;
                        _epubView.NextPageSelected     += OnNextPageSelected;
                        _epubView.LoadingStatusChanged += OnLoad;
                        _epubView.ProgressChanged      += OnProgressChanged;
                        _epubView.TouchHolding         += OnTouchHolding;
                        _epubView.TouchTapped          += (_s, _e) => { TouchTapped?.Invoke(_s, _e); };
                        _epubView.Loaded += (_s, _e) =>
                        {
                            _epubView.SingleColumnMaxWidth = SingleColumnMaxWidth;
                            _epubView.ReaderFlyout         = ReaderFlyout;
                            ViewLoaded?.Invoke(this, EventArgs.Empty);
                        };
                        _epubView.LinkTapped  += (_s, _e) => { LinkTapped?.Invoke(this, _e); };
                        _epubView.ImageTapped += (_s, _e) => { ImageTapped?.Invoke(this, _e); };
                    }

                    _mainPresenter.Content = _epubView;
                }
                _epubView.Init(_epubContent, style as EpubViewStyle);
            }
            OpenCompleted?.Invoke(this, EventArgs.Empty);
        }
 /// <summary>
 /// 更新阅读器样式
 /// </summary>
 /// <param name="style">阅读器样式</param>
 public void UpdateStyle(ReaderStyle style)
 {
     UpdateBackground(style);
     _readerView.UpdateStyle(style);
 }