Exemplo n.º 1
0
        /// <summary>
        /// This constructor takes a IDM.COMPOSESETTINGS string
        /// Note that the font size specified by such a string is limited to the HTML font tag sizes 1-7.
        /// </summary>
        /// <param name="fontString"></param>
        public MshtmlFontWrapper(string fontString)
        {
            string[]      parts = fontString.Split(',');
            StringBuilder start = new StringBuilder();
            StringBuilder end   = new StringBuilder();

            if (parts.Length < 7)
            {
                Trace.Fail("Incorrectly formatted font string: ", fontString);
                ValidFont = false;
                return;
            }

            start.Append("<DIV STYLE=\"");
            end.Insert(0, "</DIV>");


            start.Append("font-size:");
            if (Int32.TryParse(parts[3], NumberStyles.Integer, CultureInfo.InvariantCulture, out FontSize))
            {
                if (FontSize > 7)
                {
                    FontSize = 7;
                }
                if (FontSize < 1)
                {
                    FontSize = 1;
                }
            }
            else
            {
                FontSize = 2;
            }

            FontPointSize = HTMLElementHelper.HtmlFontSizeToPointFontSize(FontSize);
            start.Append(FontPointSize);
            start.Append("pt;");

            if (!string.IsNullOrEmpty(parts[4]))
            {
                string[] rgb = parts[4].Split('.');

                if (rgb.Length != 3)
                {
                    ValidFont = false;
                    return;
                }

                try
                {
                    Color c = Color.FromArgb(Int32.Parse(rgb[0], CultureInfo.InvariantCulture), Int32.Parse(rgb[1], CultureInfo.InvariantCulture), Int32.Parse(rgb[2], CultureInfo.InvariantCulture));
                    FontColor = ColorHelper.ColorToString(c);
                    start.Append("color:");
                    start.Append(FontColor);
                    start.Append(";");
                }
                catch (ArgumentException e) // Invalid color
                {
                    Trace.Fail("Exception thrown while parsing font string: " + fontString + "\r\n" + e);
                    ValidFont = false;
                    return;
                }
                catch (FormatException e) // Invalid numbers in the string
                {
                    Trace.Fail("Exception thrown while parsing font string: " + fontString + "\r\n" + e);
                    ValidFont = false;
                    return;
                }
            }

            // Parse font family, which is a comma delimited list of font names
            start.Append("font-family:'");
            if (!string.IsNullOrEmpty(parts[6]))
            {
                FontFamily = parts[6];

                int fontFamilyIndex = 7;
                while (fontFamilyIndex < parts.Length && !String.IsNullOrEmpty(parts[fontFamilyIndex]))
                {
                    FontFamily = String.Format(CultureInfo.InvariantCulture, "{0},{1}", FontFamily, parts[fontFamilyIndex]);
                    fontFamilyIndex++;
                }

                // We will normalize to single quotes (if any) in order to facilitate string comparisons of font families
                FontFamily = FontFamily.Replace('\"', '\'');
            }
            else
            {
                FontFamily = "Calibri";
            }
            start.Append(FontFamily);
            start.Append("';\"");


            start.Append(">");

            if (parts[0] == "1")
            {
                start.Append("<STRONG>");
                end.Insert(0, "</STRONG>");
                Bold = true;
            }

            if (parts[1] == "1")
            {
                start.Append("<EM>");
                end.Insert(0, "</EM>");
                Italics = true;
            }

            if (parts[2] == "1")
            {
                start.Append("<U>");
                end.Insert(0, "</U>");
                Underlined = true;
            }

            start.Append("{0}");

            HtmlWrap = start.ToString() + end.ToString();

            ValidFont = true;
        }