Page 1 of 1

Draw Text to fit a size

Posted: 2016-03-24T06:16:26-07:00
by Costyv95
Hello. I need to draw a text and make it fit a certain size(300x500). I searched on google and found only terminal commands to do it , but no answer regarding magick++ . I use drawableText, but i don't know what drawable to use in order to force the text to fit a certain rectangle.


Thanks.

Re: Draw Text to fit a size

Posted: 2016-03-24T08:06:46-07:00
by snibgo
I don't know the Magick++ API. At the MagickCore level, it can be done like this:

Code: Select all

  image_info=CloneImageInfo((ImageInfo *) NULL);
  image_info->size = ConstantString ("300x500");
  (void) strcpy(image_info->filename,"label:Hello");
  images=ReadImage(image_info,exception);

Re: Draw Text to fit a size

Posted: 2016-03-24T08:16:48-07:00
by Costyv95
Thank You, I've reached the label solution, but I have another question. How can I change the color of the label text? fillColor for that image didn't work for me..

Re: Draw Text to fit a size

Posted: 2016-03-24T08:18:31-07:00
by Costyv95
snibgo wrote:I don't know the Magick++ API. At the MagickCore level, it can be done like this:

Code: Select all

  image_info=CloneImageInfo((ImageInfo *) NULL);
  image_info->size = ConstantString ("300x500");
  (void) strcpy(image_info->filename,"label:Hello);
  images=ReadImage(image_info,exception);

Thank You, I've reached the label solution, but I have another question. How can I change the color of the label text? fillColor for that image didn't work for me..

Re: Draw Text to fit a size

Posted: 2016-03-24T08:51:21-07:00
by snibgo
Setting the fill colour after the image is created won't work. To fill with, say, Red:

Code: Select all

  image_info=CloneImageInfo((ImageInfo *) NULL);
  image_info->size = ConstantString ("300x500");
  SetImageOption (image_info, "fill", "Red");
  (void) strcpy(image_info->filename,"label:hello");
  images=ReadImage(image_info,exception);

Re: Draw Text to fit a size

Posted: 2016-03-24T08:53:45-07:00
by Costyv95
Thank You, I'll try to translate that to magick++ .

Re: Draw Text to fit a size

Posted: 2016-03-24T09:12:52-07:00
by snibgo
When you find the answer, please post a code snippet here, to help others.

Re: Draw Text to fit a size

Posted: 2016-03-25T01:02:21-07:00
by snibgo
Looking at Magick++\options.h, method "void fillColor(const Color &fillColor_)" looks likely.

Re: Draw Text to fit a size

Posted: 2016-03-25T05:03:19-07:00
by Costyv95
snibgo wrote:Looking at Magick++\options.h, method "void fillColor(const Color &fillColor_)" looks likely.
Options.h is a internal class which i cannot use in my code. For the moment I have this code:

Code: Select all

 
 		 Image img;
        img.fillColor("red"); // this has no effect for the label, only for drawable objects that i draw when i use method img.draw()
        img.size(Geometry(300,300));
        img.backgroundColor("transparent");
        img.font("Liberation-Sans-Regular");
        string text = "label:" + "This is a test";
        img.read(text);
 
I still think that this solution is not the best approach to make a text fit a size and still have no idea how to change the color of the label or allign it(centered for example).

Re: Draw Text to fit a size

Posted: 2016-03-25T06:52:42-07:00
by Costyv95
The code i wrote earlier works fine. It didn't work at me because I wasn't using the last version of Magick++ , but libmagick++-dev . After I downloaded the last version of ImageMagick from git and compile it, both img.fillColor(Color("red")) and img.textGravity(CenterGravity); worked.

The last code is:

Code: Select all

	 Image img;
    img.textGravity(CenterGravity);
    img.fillColor(Color("blue"));
    img.size(Geometry(300,300,0,0));
    img.backgroundColor("red");
    string text = "label:";
    text += "This is a test";
    img.read(text);
    img.write("output.png");