Exemplo n.º 1
0
        private void DrawDebugBox(Graphics graphics, TextBoxInfo info)
        {
            graphics.DrawRectangle(
                new Pen(Color.Blue, 1),
                this.padding.Width, this.padding.Height,
                this.RealEstateSize().Width, this.RealEstateSize().Height
            );

            graphics.DrawRectangle(
                new Pen(Color.Red, 1),
                (this.size.Width - info.trueBox.Width) / 2, (this.size.Height - info.trueBox.Height) / 2,
                info.trueBox.Width, info.trueBox.Height
            );
        }
Exemplo n.º 2
0
 private PointF TextPosition(TextBoxInfo info)
 {
     return new PointF(
         - info.trueBox.Left + (this.size.Width - info.trueBox.Width) / 2,
         - info.trueBox.Top + (this.size.Height - info.trueBox.Height) / 2
     );
 }
Exemplo n.º 3
0
        /*
         * Find an optimal font size for text to fit in the given box.
         */
        public static TextBoxInfo FitTextSize(
            Graphics graphic, String textToFit, SizeF boxSize, String fontName, 
            FontStyle fontStyle, StringFormat format, 
            float guessSize = 16, float step=0.05f)
        {
            var fontTry = GetFont(fontName, guessSize, fontStyle);

            /* Initial measure */
            SizeF measuredSize = MeasureString(graphic, textToFit, fontTry, format);

            float currentWidth = measuredSize.Width;
            float currentHeight = measuredSize.Height;
            float desiredWidth = boxSize.Width;
            float desiredHeight = boxSize.Height;

            /* Estimate size */
            float estimatedFontSize = Math.Min(
                currentWidth * (desiredWidth / currentWidth),
                currentHeight * (desiredHeight / currentHeight)
            );

            /* Refine size */
            float refinedSize = estimatedFontSize;
            while (refinedSize > 1)
            {
                fontTry = GetFont(fontName, refinedSize, fontStyle);
                measuredSize = MeasureString(graphic, textToFit, fontTry, format);

                if (measuredSize.Width <= desiredWidth && measuredSize.Height <= desiredHeight)
                    break;
                refinedSize -= step;
            }

            var textBoxInfo = new TextBoxInfo(
                textToFit,
                GetFont(fontName, refinedSize, fontStyle),
                refinedSize,
                MeasureRealStringBoundaries(graphic, textToFit, fontTry, format)
            );

            return textBoxInfo;
        }