Exemplo n.º 1
0
        /// <summary>
        /// See base docs.
        /// </summary>
        /// <param name="drawing"></param>
        /// <param name="font"></param>
        /// <param name="text"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        protected override void MeasureText(VrsDrawing.IDrawing drawing, VrsDrawing.IFont font, string text, out float width, out float height)
        {
            width  = 0F;
            height = 0F;

            if (drawing is Drawing context && font is FontWrapper fontWrapper)
            {
                var size = context.NativeDrawingContext.MeasureString(
                    text,
                    fontWrapper.NativeFont,
                    new PointF(0, 0),
                    StringFormat.GenericTypographic
                    );

                width  = size.Width;
                height = size.Height;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// See interface docs.
        /// </summary>
        /// <param name="drawing"></param>
        /// <param name="fontFamily"></param>
        /// <param name="fontStyle"></param>
        /// <param name="largestPointSize"></param>
        /// <param name="smallestPointSize"></param>
        /// <param name="constrainToWidth"></param>
        /// <param name="constrainToHeight"></param>
        /// <param name="text"></param>
        /// <param name="useCache"></param>
        /// <returns></returns>
        public VrsDrawing.FontAndText GetFontForRectangle(VrsDrawing.IDrawing drawing, VrsDrawing.IFontFamily fontFamily, VrsDrawing.FontStyle fontStyle, float largestPointSize, float smallestPointSize, float constrainToWidth, float constrainToHeight, string text, bool useCache)
        {
            var textBuffer = new StringBuilder(text);

            var  pointSize = largestPointSize;
            bool trySmaller;

            VrsDrawing.IFont font;
            do
            {
                font = CreateFont(fontFamily, pointSize, fontStyle, useCache);
                MeasureText(drawing, font, textBuffer.ToString(), out var width, out var height);

                trySmaller = width > constrainToWidth || height > constrainToHeight;
                if (trySmaller)
                {
                    if (pointSize > smallestPointSize)
                    {
                        pointSize = Math.Max(smallestPointSize, pointSize - 0.25F);
                    }
                    else
                    {
                        if (textBuffer.Length == 0 || ImageLibraryDrawTextWillTruncate)
                        {
                            trySmaller = false;
                        }
                        else
                        {
                            textBuffer.Remove(textBuffer.Length - 1, 1);
                        }
                    }
                }
            } while(trySmaller);

            return(new VrsDrawing.FontAndText(font, text.ToString()));
        }
Exemplo n.º 3
0
 /// <summary>
 /// Measures a string given a font and a drawing context.
 /// </summary>
 /// <param name="drawing"></param>
 /// <param name="font"></param>
 /// <param name="text"></param>
 /// <param name="width"></param>
 /// <param name="height"></param>
 protected abstract void MeasureText(VrsDrawing.IDrawing drawing, VrsDrawing.IFont font, string text, out float width, out float height);