Quantcast
Channel: magick Discussions Rss Feed
Viewing all articles
Browse latest Browse all 3693

New Post: Best fit label

$
0
0
So, i have the following code, but the bounding box loop in CalculateFontPointSize iterates generally 7-8 times(may be less may be more) to find best font size so its too slow for big images. I am looking for an alternative with better performance.
                Rectangle rect2 = new Rectangle(6163, 3049, 4321, 2433);
                imgLabel = new MagickImage(Color.Transparent, rect2.Width, rect2.Height);
                
                imgLabel.Font = "Arial";
                imgLabel.BackgroundColor = white;
                imgLabel.FillColor = black;
                imgLabel.TextGravity = Gravity.Center;
                CalculateFontPointSize(imgLabel, text2, 100);

                imgLabel.Read("label:" + text2);

                imgOrig.Composite(imgLabel, new MagickGeometry(rect2));


        privatevoid CalculateFontPointSize(MagickImage image, string text, double initialFontPointSize)
        {
            double calculatedWidth = 0;
            double calculatedHeight = 0;
            var metrics = image.FontTypeMetrics(text);
            double sizeInc = 500;
            double fontPointsize = initialFontPointSize, prevFontPointsize = 0,
                lastFitFontPointsize = initialFontPointSize;
            while (true)
            {
                prevFontPointsize = fontPointsize;
                fontPointsize += sizeInc;
                image.FontPointsize = fontPointsize;
                metrics = image.FontTypeMetrics(text);
                calculatedWidth = metrics.TextWidth;
                calculatedHeight = metrics.TextHeight;

                // Height calculation by FontTypeMetrics is not accurate so just use width.if (calculatedWidth > image.Width)
                {
                    fontPointsize = prevFontPointsize;
                    if (sizeInc > 100)
                        sizeInc -= 100;
                    elseif (sizeInc > 10)
                        sizeInc -= 10;
                    elseif (sizeInc > 1)
                        sizeInc--;
                    else// sizeInc = 1
                    {
                        image.FontPointsize = lastFitFontPointsize;
                        break;
                    }
                }
                if (calculatedWidth <= image.Width && calculatedHeight <= image.Height)
                {
                    lastFitFontPointsize = fontPointsize;
                }
            }

            int threshold = 10;
            if (calculatedWidth >= image.Width - threshold && calculatedWidth <= image.Width && 
                calculatedHeight >= image.Height - threshold && calculatedHeight <= image.Height)
                return;
            using (MagickImage tempImg = new MagickImage(image))
            {
                sizeInc = 200;
                
                fontPointsize = image.FontPointsize;
                prevFontPointsize = 0;

                tempImg.BackgroundColor = Color.White;
                tempImg.FillColor = Color.Black;
                MagickGeometry boundingBox = new MagickGeometry(0, 0);
                while (true)
                {
                    prevFontPointsize = fontPointsize;
                    fontPointsize += sizeInc;
                    tempImg.FontPointsize = fontPointsize;

                    tempImg.Read("label:" + text);
                    boundingBox = tempImg.BoundingBox;

                    if (boundingBox.Width > image.Width - threshold || boundingBox.Height > image.Height - threshold)
                    {
                        tempImg.Draw(new DrawableFillColor(Color.White),
                            new DrawableRectangle(new Rectangle(0, 0, tempImg.Width, tempImg.Height)));

                        fontPointsize = prevFontPointsize;
                        if (sizeInc > 100)
                            sizeInc -= 100;
                        elseif (sizeInc > 50)
                            sizeInc -= 50;
                        else// sizeInc = 50
                        {
                            image.FontPointsize = lastFitFontPointsize;
                            break;
                        }
                    }
                    else
                    {
                        lastFitFontPointsize = fontPointsize;
                    }
                }
            }
        }

Viewing all articles
Browse latest Browse all 3693

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>