示例#1
0
        internal static FastString HtmlString(string text, TextRenderType textRenderType, CRLF crlf, bool excel2007, string fontSize = "13px;")
        {
            FastString Result         = new FastString(text.Length);
            int        len            = text.Length;
            int        lineBreakCount = 0;

            if (textRenderType == TextRenderType.HtmlTags)
            {
                string wingdings = "<font face=\"Wingdings\">";
                string webdings = "<font face=\"Webdings\">";
                int    ind1 = 0, ind2 = 0;
                if (text.Contains(wingdings))
                {
                    ind1 = text.IndexOf(wingdings) + wingdings.Length;
                    ind2 = text.IndexOf('<', ind1);
                    text = text.Substring(0, ind1) +
                           WingdingsToUnicodeConverter.Convert(text.Substring(ind1, ind2 - ind1)) +
                           text.Substring(ind2, text.Length - ind2);
                }
                else if (text.Contains(webdings))
                {
                    ind1 = text.IndexOf(webdings) + webdings.Length;
                    ind2 = text.IndexOf('<', ind1);
                    text = text.Substring(0, ind1) +
                           WingdingsToUnicodeConverter.Convert(text.Substring(ind1, ind2 - ind1)) +
                           text.Substring(ind2, text.Length - ind2);
                }
            }

            for (int i = 0; i < len; i++)
            {
                if (crlf != CRLF.xml && crlf != CRLF.odt && text[i] == ' ' && (text.Length == 1 ||
                                                                               (i < (len - 1) && text[i + 1] == ' ') ||
                                                                               (i > 0 && text[i - 1] == ' ') ||
                                                                               i == len - 1))
                {
                    Result.Append("&nbsp;");
                }
                else if (text[i] == '<' && textRenderType == TextRenderType.HtmlTags && crlf == CRLF.odt)
                {
                    i += text.IndexOf('>', i) - i;
                }
                else if (i < text.Length - 1 && text[i] == '\r' && text[i + 1] == '\n')
                {
                    if (crlf == CRLF.xml)
                    {
                        Result.Append("&#10;");
                    }
                    else if (crlf == CRLF.odt)
                    {
                        Result.Append("<text:line-break />");
                    }
                    else
                    {
                        if (lineBreakCount == 0)
                        {
                            Result.Append("<p style=\"margin-top:0px;margin-bottom:0px;\"></p>");
                        }
                        else
                        {
                            Result.Append($"<p style=\"margin-top:0px;height:{fontSize}margin-bottom:0px\"></p>");
                        }
                        lineBreakCount++;
                    }
                    i++;
                }
                else
                {
                    lineBreakCount = 0;
                    if (text[i] == '\t' && crlf == CRLF.odt)
                    {
                        Result.Append("<text:tab/>");
                    }
                    else if (text[i] == ' ' && crlf == CRLF.odt)
                    {
                        int spaces = 1;
                        while (i < text.Length - 1)
                        {
                            if (text[i + 1] == ' ')
                            {
                                i++;
                                spaces++;
                            }
                            else
                            {
                                break;
                            }
                        }
                        Result.Append("<text:s text:c=\"" + spaces + "\"/>");
                    }
                    else if (text[i] == '\\')
                    {
                        Result.Append("&#92;");
                    }
                    else if (text[i] == '~' && !excel2007)
                    {
                        Result.Append("&tilde;");
                    }
                    else if (text[i] == '€' && !excel2007)
                    {
                        Result.Append("&euro;");
                    }
                    else if (text[i] == '‹' && !excel2007)
                    {
                        Result.Append("&lsaquo;");
                    }
                    else if (text[i] == '›' && !excel2007)
                    {
                        Result.Append("&rsaquo;");
                    }
                    else if (text[i] == 'ˆ' && !excel2007)
                    {
                        Result.Append("&circ;");
                    }
                    else if (text[i] == '&' && textRenderType == TextRenderType.Default)
                    {
                        Result.Append("&amp;");
                    }
                    else if (text[i] == '"' && textRenderType == TextRenderType.Default)
                    {
                        Result.Append("&quot;");
                    }
                    else if (text[i] == '<' && textRenderType == TextRenderType.Default)
                    {
                        Result.Append("&lt;");
                    }
                    else if (text[i] == '>' && textRenderType == TextRenderType.Default)
                    {
                        Result.Append("&gt;");
                    }
                    else if (text[i] == '\t' && excel2007)
                    {
                        continue;
                    }
                    else
                    {
                        Result.Append(text[i]);
                    }
                }
            }
            return(Result);
        }
示例#2
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;
            }
        }