Caption Max Font Size?

PerlMagick is an object-oriented Perl interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning PerlMagick.
Post Reply
johnfl68
Posts: 30
Joined: 2012-07-09T18:31:26-07:00
Authentication code: 13

Caption Max Font Size?

Post by johnfl68 » 2016-04-02T12:00:02-07:00

Just seeing if anyone has any other ideas on this.

I have a bunch of script that take API data and place a paragraph centered in a box using IM caption in Perl.

The issue is the paragraphs can be anywhere from around 20 characters to around 120 characters.

So typically I use a 36 point font for a 280x250 size box.

I do a character count on the text and if it is over 65 I remove the pointsize so that the text is resized to fit the box.

Code: Select all

	my $daily_7_summary_length = length($daily_7_summary);
	
	my $daily_7_summary_image = Image::Magick->new;
	     $daily_7_summary_image->Set(
           background => $summary_background,
           fill       => $fontcolor1,
           gravity    => 'center',
           font       => "$font2",
		     size       => '280x250'
		   );
	
    if ( $daily_7_summary_length < 68 ) {
        $daily_7_summary_image->Set(
		     pointsize  => 36
        );
	} 
	
	$daily_7_summary_image->Read("caption:$daily_7_summary");
Is there anyway to set a "maximum pointsize" instead of "pointsize", so that the text will always be a certain pointsize and only be made smaller if the text overflows the box? Otherwise if I don't set a pointsize, and the paragraph is small, it shows up in a huge font. Most of the time the paragraphs are smaller, so 36 points is fine. I just want a uniform look to all of the boxes, and only resize when smaller.

I am just looking to see if maybe there is a better way that I haven't seen or figured out yet, that would make the code even cleaner?
Or maybe a future feature request for a maxpointsize option?

Thanks!

John

User avatar
fmw42
Posts: 22080
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Caption Max Font Size?

Post by fmw42 » 2016-04-02T12:14:37-07:00

If you set -size WxH and leave the pointsize out, then IM will choose the pointsize to make the text fit the box WxH. That is all that I know. I know of no max pointsize argument.

You could processes the caption once for a given W and pointsize to determine the height (-format "%h" info:), then if the height is too large, lower the pointsize.

snibgo
Posts: 9378
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Caption Max Font Size?

Post by snibgo » 2016-04-02T13:42:52-07:00

Another way of doing this is to create a "caption:" image at the required size. Examine the property "caption:pointsize". If this is greater than 36 (or other chosen limit), re-create the image at that pointsize.
snibgo's IM pages: im.snibgo.com

User avatar
fmw42
Posts: 22080
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Caption Max Font Size?

Post by fmw42 » 2016-04-02T14:56:42-07:00

snibgo wrote:Another way of doing this is to create a "caption:" image at the required size. Examine the property "caption:pointsize". If this is greater than 36 (or other chosen limit), re-create the image at that pointsize.
Where is the property caption:pointsize documented?

Do you mean:

Code: Select all

convert -debug annotate -size 100x100 caption:"testing" null:
2016-04-02T14:53:42-07:00 0:00.140 0.120u 6.9.3 Annotate convert[231]: annotate.c/GetTypeMetrics/843/Annotate
Metrics: text: testing; width: 95; height: 37; ascent: 29; descent: -7; max advance: 64; bounds: 1.03125,-6 15.6562,17; origin: 95,0; pixels per em: 32,32; underline position: -4.5625; underline thickness: 2.34375
2016-04-02T14:53:42-07:00 0:00.140 0.120u 6.9.3 Annotate convert[231]: annotate.c/RenderFreetype/1421/Annotate
Font /Library/Fonts/Arial.ttf; font-encoding none; text-encoding none; pointsize 32.25

snibgo
Posts: 9378
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Caption Max Font Size?

Post by snibgo » 2016-04-02T16:12:24-07:00

Yes, "-debug annotate" gives it, but I was thinking of one of these:

Code: Select all

convert -size 100x100 caption:"testing" -format "%[caption:pointsize]" info:
32.25

convert -size 100x100 caption:"testing" -verbose info:

  Properties:
    caption: testing
    caption:pointsize: 32.25
I don't know if "caption:pointsize" is documented anywhere.
snibgo's IM pages: im.snibgo.com

Post Reply