예제 #1
0
        private static void ParseText(HtmlNode htmlNode, ref List <Elements.BaseElement> elements, double fontSizeMultiplier, FontStyle fontStyle, FontWeight fontWeight, TextDecorationCollection textDecorations, Brush foreground)
        {
            string decodedText = DecodeText(htmlNode.InnerText);

            decodedText = decodedText.Trim();

            if (String.IsNullOrEmpty(decodedText) && elements.Count > 0 && elements[elements.Count - 1] is Elements.Break) // we don't add empty space after break
            {
                return;
            }

            // create text
            Elements.BaseElement element = new Elements.Text()
            {
                FontSizeMultiplier = fontSizeMultiplier,
                FontStyle          = fontStyle.ToTextFontStyle(),
                FontWeight         = fontWeight.ToTextFontWeight(),
                Decoration         = textDecorations.ToTextDecoration(),
                Foreground         = foreground,
                Value = decodedText
            };

            // extract id
            ExtractId(htmlNode, ref element);

            // extract anchor
            if (htmlNode.Name == "a")
            {
                ExtractHref(htmlNode, ref element);
            }

            // add
            elements.Add(element);
        }
예제 #2
0
        private Run CreateRun(Elements.Text textElement, string text)
        {
            Run run = new Run();

            run.FontSize   = _ePubViewer.FontSize * textElement.FontSizeMultiplier;
            run.FontStyle  = textElement.FontStyle.ToFontStyle();
            run.FontWeight = textElement.FontWeight.ToFontWeight();
            if (textElement.Foreground != null)
            {
                // force Foreground - for links
                run.Foreground = textElement.Foreground;
            }
            run.TextDecorations = textElement.Decoration.ToTextDecorationCollection();
            run.Text            = text;

            return(run);
        }
예제 #3
0
        private void RenderText(ItemElementsContainer iec, int currentLocation, ref Page page, ref double top, Elements.BaseElement element)
        {
            Elements.Text textElement = (Elements.Text)element;

            string value = "  " + textElement.Value;

            TextBlock tb = ExtractTextBlock(ref page, top);

            double prevTextBlockActualHeight = tb.ActualHeight;

            tb.Inlines.Add(CreateRun(textElement, value));
            double textBlockActualHeight = tb.ActualHeight - prevTextBlockActualHeight;

            // check if fits (in most cases it will pass, because of short paragraphs)
            if (top + textBlockActualHeight > page.Content.Height)
            {
                // remove inline
                tb.Inlines.RemoveAt(tb.Inlines.Count - 1);

                int      idx   = 1;
                string[] parts = value.Split(' ');

                while (true)
                {
                    string text = StringJoin(' ', parts, idx);
                    prevTextBlockActualHeight = tb.ActualHeight;
                    tb.Inlines.Add(CreateRun(textElement, text));
                    textBlockActualHeight = tb.ActualHeight - prevTextBlockActualHeight;
                    if (top + textBlockActualHeight > page.Content.Height)
                    {
                        // remove inline
                        tb.Inlines.RemoveAt(tb.Inlines.Count - 1);

                        // push prev text
                        // it could be empty if nothing fits
                        string prevText = null;
                        if (idx > 1)
                        {
                            prevText = StringJoin(' ', parts, idx - 1);

                            tb.Inlines.Add(CreateRun(textElement, prevText));
                        }

                        PushPage(iec, ref page, currentLocation, ref top);

                        // render rest of text
                        if (idx > 1)
                        {
                            textElement.Value = value.Substring(prevText.Length);
                        }
                        else
                        {
                            textElement.Value = value;
                        }

                        RenderText(iec, currentLocation, ref page, ref top, element);

                        break;
                    }
                    else
                    {
                        // remove inline
                        tb.Inlines.RemoveAt(tb.Inlines.Count - 1);
                    }

                    idx++;
                }
            }
            else
            {
                // create anchor rect if href isn't null
                if (!String.IsNullOrEmpty(textElement.Href))
                {
                    AnchorRect anchorRect = new AnchorRect();
                    anchorRect.Href = textElement.Href;
                    anchorRect.Rect = new Rect(
                        _ePubViewer.ContentMargin.Left,
                        (textBlockActualHeight == 0) ? top - _ePubViewer.LineHeight : top,
                        _ePubViewer.ContentSize.Width,
                        (textBlockActualHeight == 0) ? _ePubViewer.LineHeight : textBlockActualHeight);

                    page.AnchorRects.Add(anchorRect);
                }

                top += textBlockActualHeight;
            }
        }