예제 #1
0
        public static SKSize MeasureText(string text, SKTypeface font, int fontSize, SKSize proposedSize)
        {
            var text_bounds = SKRect.Empty;

            using var paint = SkiaTextExtensions.CreateTextPaint(font, fontSize, SKColors.Black);

            paint.MeasureText(text, ref text_bounds);

            // If we fit in the proposed size then just use that
            if (text_bounds.Width <= proposedSize.Width && text_bounds.Height <= proposedSize.Height)
            {
                return(new SKSize(text_bounds.Width, proposedSize.Height));
            }

            // Figure out how many lines we have room for
            var line_count = proposedSize.Height / text_bounds.Height;

            // If we only have room for one line there's not a lot we can do
            if (line_count <= 1)
            {
                return(new SKSize(text_bounds.Width, proposedSize.Height));
            }

            var words          = BreakDownIntoWords(text);
            var max_word_width = Math.Max(words.Select(w => MeasureText(w, font, fontSize)).Max(), proposedSize.Width);

            return(new SKSize(max_word_width, proposedSize.Height));
        }
예제 #2
0
        public static SKPoint[] MeasureCharacters(string text, SKTypeface font, int fontSize, float xOffset = 0, float yOffset = 0)
        {
            using var paint  = SkiaTextExtensions.CreateTextPaint(font, fontSize, SKColors.Black);
            using var shaper = new SKShaper(font);

            var result = shaper.Shape(text, xOffset, yOffset, paint);

            return(result.Points);
        }
예제 #3
0
        public static float MeasureText(string text, SKTypeface font, int fontSize)
        {
            using var paint = SkiaTextExtensions.CreateTextPaint(font, fontSize, SKColors.Black);

            return(paint.MeasureText(text));
        }