예제 #1
0
        public static Vector2 ComputePreferredSize(FormattedText text)
        {
            bool empty = text.IsEmpty;

            if (empty)
            {
                text.ApplySource(" ");
            }

            var wrap      = text.WordWrap;
            var alignment = text.LineAlign;
            var width     = text.MaxWidth;
            var height    = text.MaxHeight;

            text.WordWrap  = FormattedText.WrapMode.Element;
            text.LineAlign = Alignment.Left;
            text.MaxWidth  = 0;
            text.MaxHeight = 0;

            var size = text.Size;

            text.WordWrap  = wrap;
            text.LineAlign = alignment;
            text.MaxWidth  = width;
            text.MaxHeight = height;

            if (empty)
            {
                text.ApplySource("");
            }

            return(size);
        }
예제 #2
0
        public static Vector2 ComputePreferredSize(FormattedText text, Vector2 maxSize)
        {
            bool empty = text.IsEmpty;

            if (empty)
            {
                text.ApplySource(" ");
            }

            // This is far from perfect, I don't understand the precise behaviour of FormattedText.MaxWidth/Height
            // It seems inconsistent.

            if (maxSize.X == 0 && maxSize.Y == 0)
            {
                // Handle this specific case here, since 0 is a magic "no constraint" value for formatted text width and height.
                // This shouldn't make any real difference, since the final size will be clamped in UIComponent.cs somehwere.
                return(Vector2.Zero);
            }

            maxSize.X = MathF.Clamp(maxSize.X, 0, BlueConfig.MaxTextAreaSize.X);
            maxSize.Y = MathF.Clamp(maxSize.Y, 0, BlueConfig.MaxTextAreaSize.Y);

            var width  = text.MaxWidth;
            var height = text.MaxHeight;

            text.MaxWidth  = (int)maxSize.X;
            text.MaxHeight = (int)maxSize.Y;

            var size = text.Size;

            text.MaxWidth  = width;
            text.MaxHeight = height;

            if (empty)
            {
                text.ApplySource("");
            }

            return(size);
        }
예제 #3
0
        public override void Perform(Font font, PreviewImageQuery query)
        {
            int           desiredWidth  = query.DesiredWidth;
            int           desiredHeight = query.DesiredHeight;
            const string  text          = "/acThe quick brown fox jumps over the lazy dog.";
            FormattedText formatText    = new FormattedText();

            formatText.MaxWidth  = Math.Max(1, desiredWidth - 10);
            formatText.MaxHeight = Math.Max(1, desiredHeight - 10);
            formatText.WordWrap  = FormattedText.WrapMode.Word;
            formatText.Fonts     = new[] { new ContentRef <Font>(font) };
            formatText.ApplySource(text);
            PixelData textLayer = new PixelData(desiredWidth, MathF.RoundToInt(formatText.Size.Y));

            formatText.RenderToBitmap(text, textLayer, 5, 0);

            Bitmap resultBitmap = textLayer.ToBitmap();

            // Debug Font metrics
            //const bool drawDebugFontMetrics = true;
            //if (drawDebugFontMetrics)
            //{
            //    var metrics = formatText.TextMetrics;
            //    Color fgColor = Color.White;
            //    Color baseLineColor = Color.FromArgb(255, 0, 0);
            //    Color bodyAscentColor = Color.FromArgb(0, 192, 0);
            //    Color ascentColor = Color.FromArgb(64, 64, 255);
            //    Color descentColor = Color.FromArgb(255, 0, 255);
            //    using (Graphics g = Graphics.FromImage(resultBitmap))
            //    {
            //        for (int i = 0; i < metrics.LineCount; i++)
            //        {
            //            Rect lineBounds = metrics.LineBounds[i];
            //            g.DrawRectangle(new Pen(Color.FromArgb(128, fgColor)), lineBounds.X + 5, lineBounds.Y, lineBounds.W, lineBounds.H - 1);
            //            g.DrawLine(new Pen(Color.FromArgb(192, baseLineColor)), 0, lineBounds.Y + font.BaseLine, resultBitmap.Width, lineBounds.Y + font.BaseLine);
            //            g.DrawLine(new Pen(Color.FromArgb(192, bodyAscentColor)), 0, lineBounds.Y + font.BaseLine - font.BodyAscent, resultBitmap.Width, lineBounds.Y + font.BaseLine - font.BodyAscent);
            //            g.DrawLine(new Pen(Color.FromArgb(192, ascentColor)), 0, lineBounds.Y + font.BaseLine - font.Ascent, resultBitmap.Width, lineBounds.Y + font.BaseLine - font.Ascent);
            //            g.DrawLine(new Pen(Color.FromArgb(192, descentColor)), 0, lineBounds.Y + font.BaseLine + font.Descent, resultBitmap.Width, lineBounds.Y + font.BaseLine + font.Descent);
            //        }
            //    }
            //}

            query.Result = resultBitmap.Resize(desiredWidth, desiredHeight, Alignment.Left);
        }