private void LayerText(FastString Page, TextObject obj)
        {
            float top = 0;

            switch (obj.TextRenderType)
            {
            case TextRenderType.HtmlParagraph:


                HtmlTextRenderer htmlTextRenderer = obj.GetHtmlTextRenderer(Zoom, Zoom);


                if (obj.VertAlign == VertAlign.Center)
                {
                    top = (obj.Height - htmlTextRenderer.CalcHeight()) / 2;
                }
                else if (obj.VertAlign == VertAlign.Bottom)
                {
                    top = obj.Height - htmlTextRenderer.CalcHeight();
                }
                FastString sb = GetHtmlParagraph(htmlTextRenderer);

                LayerBack(Page, obj,
                          GetSpanText(obj, sb,
                                      top + obj.Padding.Top,
                                      obj.Width - obj.Padding.Horizontal,
                                      obj.ParagraphOffset));
                break;

            default:
                if (obj.VertAlign != VertAlign.Top)
                {
                    Graphics g = htmlMeasureGraphics;
                    using (Font f = new Font(obj.Font.Name, obj.Font.Size, obj.Font.Style))
                    {
                        RectangleF           textRect  = new RectangleF(obj.AbsLeft, obj.AbsTop, obj.Width, obj.Height);
                        StringFormat         format    = obj.GetStringFormat(Report.GraphicCache, 0);
                        Brush                textBrush = Report.GraphicCache.GetBrush(obj.TextColor);
                        AdvancedTextRenderer renderer  = new AdvancedTextRenderer(obj.Text, g, f, textBrush, null,
                                                                                  textRect, format, obj.HorzAlign, obj.VertAlign, obj.LineHeight, obj.Angle, obj.FontWidthRatio,
                                                                                  obj.ForceJustify, obj.Wysiwyg, obj.HasHtmlTags, false, Zoom, Zoom, obj.InlineImageCache);
                        if (renderer.Paragraphs.Count > 0)
                        {
                            if (renderer.Paragraphs[0].Lines.Count > 0)
                            {
                                float height = renderer.Paragraphs[0].Lines[0].CalcHeight();
                                if (height > obj.Height)
                                {
                                    top = -(height - obj.Height) / 2;
                                }
                                else
                                {
                                    top = renderer.Paragraphs[0].Lines[0].Top - obj.AbsTop;
                                }
                            }
                        }
                    }
                }

                LayerBack(Page, obj,
                          GetSpanText(obj, ExportUtils.HtmlString(obj.Text, obj.TextRenderType),
                                      top + obj.Padding.Top,
                                      obj.Width - obj.Padding.Horizontal,
                                      obj.ParagraphOffset));
                break;
            }
        }
        private FastString GetHtmlParagraph(HtmlTextRenderer renderer)
        {
            FastString sb = new FastString();

            foreach (HtmlTextRenderer.Paragraph paragraph in renderer.Paragraphs)
            {
                foreach (HtmlTextRenderer.Line line in paragraph.Lines)
                {
                    if (sb == null)
                    {
                        sb = new FastString();
                    }
                    sb.Append("<span style=\"");
                    sb.Append("display:block;");
                    if (line.Top + line.Height > renderer.DisplayRect.Bottom)
                    {
                        sb.Append("height:").Append(Math.Max(renderer.DisplayRect.Bottom - line.Top, 0).ToString(HtmlTextRenderer.CultureInfo)).Append("px;");
                    }
                    else
                    {
                        //sb.Append("height:").Append(line.Height.ToString(HtmlTextRenderer.CultureInfo)).Append("px;");
                        if (line.LineSpacing > 0)
                        {
                            sb.Append("margin-bottom:").Append(line.LineSpacing.ToString(HtmlTextRenderer.CultureInfo)).Append("px;");
                        }
                    }
                    sb.Append("overflow:hidden;");
                    sb.Append("line-height:").Append(line.Height.ToString(HtmlTextRenderer.CultureInfo)).Append("px;");
                    if (line.HorzAlign == HorzAlign.Justify)
                    {
                        sb.Append("text-align-last:justify;");
                    }
                    else
                    {
                        sb.Append("white-space:pre;");
                    }
                    sb.Append("\">");
                    HtmlTextRenderer.StyleDescriptor styleDesc = null;
                    foreach (HtmlTextRenderer.Word word in line.Words)
                    {
                        foreach (HtmlTextRenderer.Run run in word.Runs)
                        {
                            if (!run.Style.FullEquals(styleDesc))
                            {
                                if (styleDesc != null)
                                {
                                    styleDesc.ToHtml(sb, true);
                                }
                                styleDesc = run.Style;
                                styleDesc.ToHtml(sb, false);
                            }

                            if (run is HtmlTextRenderer.RunText)
                            {
                                HtmlTextRenderer.RunText runText = run as HtmlTextRenderer.RunText;

                                foreach (char ch in runText.Text)
                                {
                                    switch (ch)
                                    {
                                    case '"':
                                        sb.Append("&quot;");
                                        break;

                                    case '&':
                                        sb.Append("&amp;");
                                        break;

                                    case '<':
                                        sb.Append("&lt;");
                                        break;

                                    case '>':
                                        sb.Append("&gt;");
                                        break;

                                    case '\t':
                                        sb.Append("&Tab;");
                                        break;

                                    default:
                                        sb.Append(ch);
                                        break;
                                    }
                                }
                            }
                            else if (run is HtmlTextRenderer.RunImage)
                            {
                                HtmlTextRenderer.RunImage runImage = run as HtmlTextRenderer.RunImage;

                                using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                                {
                                    try
                                    {
                                        float w, h;
                                        using (Bitmap bmp = runImage.GetBitmap(out w, out h))
                                        {
                                            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                                        }
                                        ms.Flush();
                                        sb.Append("<img src=\"data:image/png;base64,").Append(Convert.ToBase64String(ms.ToArray()))
                                        .Append("\" width=\"").Append(w.ToString(HtmlTextRenderer.CultureInfo)).Append("\" height=\"").Append(h.ToString(HtmlTextRenderer.CultureInfo)).Append("\"/>");
                                    }
                                    catch (Exception /*e*/)
                                    {
                                    }
                                }
                            }
                            //run.ToHtml(sb, true);
                        }
                    }
                    if (styleDesc != null)
                    {
                        styleDesc.ToHtml(sb, true);
                    }
                    else
                    {
                        sb.Append("<br/>");
                    }
                    sb.Append("</span>");
                }
            }
            return(sb);
        }
示例#3
0
        private void LayerText(FastString Page, TextObject obj)
        {
            float top = 0;

            if (obj.Font.FontFamily.Name == "Wingdings" || obj.Font.FontFamily.Name == "Webdings")
            {
                obj.Text = WingdingsToUnicodeConverter.Convert(obj.Text);
            }

            switch (obj.TextRenderType)
            {
            case TextRenderType.HtmlParagraph:

                using (HtmlTextRenderer htmlTextRenderer = obj.GetHtmlTextRenderer(Zoom, Zoom))
                {
                    if (obj.VertAlign == VertAlign.Center)
                    {
                        top = (obj.Height - htmlTextRenderer.CalcHeight()) / 2;
                    }
                    else if (obj.VertAlign == VertAlign.Bottom)
                    {
                        top = obj.Height - htmlTextRenderer.CalcHeight();
                    }
                    FastString sb = GetHtmlParagraph(htmlTextRenderer);

                    LayerBack(Page, obj,
                              GetSpanText(obj, sb,
                                          top + obj.Padding.Top,
                                          obj.Width - obj.Padding.Horizontal,
                                          obj.ParagraphOffset));
                }
                break;

            default:
                if (obj.VertAlign != VertAlign.Top)
                {
                    IGraphics g = htmlMeasureGraphics;
                    using (Font f = new Font(obj.Font.FontFamily, obj.Font.Size * DrawUtils.ScreenDpiFX, obj.Font.Style))
                    {
                        RectangleF textRect = new RectangleF(obj.AbsLeft + obj.Padding.Left, obj.AbsTop + obj.Padding.Top,
                                                             obj.Width - obj.Padding.Left - obj.Padding.Right,
                                                             obj.Height - obj.Padding.Top - obj.Padding.Bottom);
                        StringFormat         format    = obj.GetStringFormat(Report.GraphicCache, 0);
                        Brush                textBrush = Report.GraphicCache.GetBrush(obj.TextColor);
                        AdvancedTextRenderer renderer  = new AdvancedTextRenderer(obj.Text, g, f, textBrush, null,
                                                                                  textRect, format, obj.HorzAlign, obj.VertAlign, obj.LineHeight, obj.Angle, obj.FontWidthRatio,
                                                                                  obj.ForceJustify, obj.Wysiwyg, obj.HasHtmlTags, false, Zoom, Zoom, obj.InlineImageCache);
                        if (renderer.Paragraphs.Count > 0)
                        {
                            if (renderer.Paragraphs[0].Lines.Count > 0)
                            {
                                float height = renderer.Paragraphs[0].Lines[0].CalcHeight();
                                if (height > obj.Height)
                                {
                                    top = -(height - obj.Height) / 2;
                                }
                                else
                                {
                                    top    = renderer.Paragraphs[0].Lines[0].Top - obj.AbsTop;
                                    height = renderer.CalcHeight();

                                    if (obj.VertAlign == VertAlign.Center)
                                    {
                                        top = (obj.Height - height - obj.Padding.Bottom + obj.Padding.Top) / 2;
                                    }
                                    else if (obj.VertAlign == VertAlign.Bottom)
                                    {
                                        // (float)(Math.Round(obj.Font.Size * 96 / 72) / 2
                                        // necessary to compensate for paragraph offset error in GetSpanText method below
                                        top = obj.Height - height - obj.Padding.Bottom - (float)(Math.Round(obj.Font.Size * 96 / 72) / 2);
                                    }
                                }
                            }
                        }
                    }
                }

                LayerBack(Page, obj,
                          GetSpanText(obj, ExportUtils.HtmlString(obj.Text, obj.TextRenderType, Px(Math.Round(obj.Font.Size * 96 / 72))),
                                      top,
                                      obj.Width - obj.Padding.Horizontal,
                                      obj.ParagraphOffset));
                break;
            }
        }