private double BuildContext(PageRenderData context, ParagraphInfo p, double width, double defaultFontSize, int paragraphID, double top) { TextAlignment align = GetTextAlignment(p); var list = new List<TextElement>(); bool firstLine = true; foreach (TextElementBase baseInlineItem in p.Inlines) { if (baseInlineItem is EOLElement) { top = AppendLine(context, list, align, p, width, top, defaultFontSize, paragraphID, firstLine); firstLine = false; list.Clear(); } else { var inlineItem = (TextElement) baseInlineItem; list.Add(inlineItem); } } return AppendLine(context, list, align == TextAlignment.Justify ? TextAlignment.Left : align, p, width, top, defaultFontSize, paragraphID, firstLine); }
private double BuildImageContext(PageRenderData context, ParagraphInfo p, double top) { var item = (ImageElement)p.Inlines.First(); BookImage bookImage = _images.First(t => t.ID == item.ImageID); double offsetX = (context.PageSize.Width - p.MarginLeft - p.MarginRight - item.Width) / 2.0; var imageContext = CreateImage(context, offsetX, top, item, bookImage); context.Images.Add(imageContext); return top + item.Height; }
public PageBuilder(Size pageSize, IEnumerable<BookImage> images) { _images = images; _pageSize = pageSize; _height = 0.0; _paragraph = null; _page = new PageInfo { FirstTokenID = -1 }; }
private bool AppendToPage(TextTokenBlock block) { if (_page.FirstTokenID < 0) _page.FirstTokenID = block.FirstTokenID; double height = block.Height * (double) AppSettings.Default.FontSettings.FontInterval; if (_height + height <= _pageSize.Height) { _height += height; _page.LastTokenID = block.LastTokenID; _page.LastTextPart = block.GetLastPart(); if (_paragraph == null) { _paragraph = CreateParagraph(block); _page.Paragraphs.Add(_paragraph); } AddInlineToParagraph(block); return true; } return false; }
private void AddInlineToParagraph(TextTokenBlock block) { _page.Lines.Add(block); foreach (TextElementBase baseInlineItem in block.Inlines) { if (!(baseInlineItem is EOPElement)) { _paragraph.Inlines.Add(baseInlineItem); } else { _paragraph = null; break; } } }
private static double AppendLine(PageRenderData context, ICollection<TextElement> inlines, TextAlignment align, ParagraphInfo p, double width, double top, double defaultFontSize, int paragraphID, bool firstLine) { if (inlines.Count == 0) return top; double height = inlines.Max((t => t.Height)); double inlinesWidth = inlines.Sum((t => t.Width)); double leftMargin = 0.0; double wordSpacing = 0.0; double textIndent = !firstLine || align != TextAlignment.Justify && align != TextAlignment.Left ? 0.0 : p.TextIndent; width -= p.MarginRight + p.MarginLeft + textIndent; switch (align) { case TextAlignment.Center: leftMargin = (width - inlinesWidth)/2.0; break; case TextAlignment.Right: leftMargin = width - inlinesWidth; break; case TextAlignment.Justify: wordSpacing = (width - inlinesWidth)/(inlines.Count - 1); break; } double tempLeftMargin = leftMargin + (p.MarginLeft + textIndent); inlines.Aggregate(tempLeftMargin, (current, inlineItem) => BuildInlineItem(context, top, defaultFontSize, paragraphID, inlineItem, height, current, wordSpacing)); return top + height * (double) AppSettings.Default.FontSettings.FontInterval; }
private static TextAlignment GetTextAlignment(ParagraphInfo p) { if (!string.IsNullOrEmpty(p.TextAlign)) { switch (p.TextAlign) { case "center": return TextAlignment.Center; case "right": return TextAlignment.Right; case "left": return TextAlignment.Left; } } return TextAlignment.Justify; }