Ejemplo n.º 1
0
        /// <summary>
        /// для простоты - каждая строка текста имеет свой формат
        /// необязательные модификаторы вида [b][i][u][#RRGGBB] идут _в начале_ строки
        /// [b][i][u][#ff00e6]This is a bold/italic/underlined magenta line of text
        /// </summary>
        public static SizeF MeasureFormattedText(this Graphics g, string text, FontStorage fonts)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(new SizeF(0, 0));
            }
            var lines = text.Split(lineSeparator, StringSplitOptions.RemoveEmptyEntries);

            var totalHeight = 0f;
            var totalWidth  = 0f;

            foreach (var line in lines)
            {
                FontStyle fontStyle;
                Color?    fontColor;

                var pureLine = GetLineModifiers(line, out fontStyle, out fontColor);
                var font     = fonts.GetFont(fontStyle);
                var dH       = font.GetHeight(g);
                totalHeight += dH;

                if (string.IsNullOrEmpty(pureLine))
                {
                    continue;
                }
                var sz = g.MeasureString(pureLine, font);
                if (sz.Width > totalWidth)
                {
                    totalWidth = sz.Width;
                }
            }

            return(new SizeF(totalWidth, totalHeight));
        }
Ejemplo n.º 2
0
        public static void DrawFormattedText(this Graphics g,
                                             float x, float y,
                                             BrushesStorage brushes,
                                             Color colorMain,
                                             string text, FontStorage fonts)
        {
            if (string.IsNullOrEmpty(text))
            {
                return;
            }
            var lines       = text.Split(lineSeparator, StringSplitOptions.RemoveEmptyEntries);
            var totalHeight = 0f;

            foreach (var line in lines)
            {
                FontStyle fontStyle;
                Color?    fontColor;

                var pureLine = GetLineModifiers(line, out fontStyle, out fontColor);
                var font     = fonts.GetFont(fontStyle);

                g.DrawString(pureLine, font, brushes.GetBrush(fontColor ?? colorMain), x, y + totalHeight);

                var dH = font.GetHeight(g);
                totalHeight += dH;
            }
        }