How to add word wrapping to texts in Magick++

2019-08-26 02:27发布

I am trying to add texts to images in Magick++ by doing so:

Method 1:

Magick::Image image(Magick::Geometry(800,800),Magick::Color("white"));
Magick::Color color(0,0,0,0);
image.font("Waree");
image.fontPointsize(36);
image.strokeColor(color);
image.fillColor(color);
image.annotate("HelloWorld!", NorthWestGravity);

Method 2:

Magick::Image image(Magick::Geometry(800,800),Magick::Color("white"));
Magick::Color color(0,0,0,0);
std::list<Magick::Drawable> text_draw_list;
text_draw_list.push_back(Magick::DrawableViewbox(0,0,image.columns(), image.rows()));
text_draw_list.push_back(Magick::DrawableFont("Waree", (Magick::StyleType)NormalStyle, 400, (Magick::StretchType)NormalStretch ));
text_draw_list.push_back(Magick::DrawablePointSize(36));
//Manual offsets
text_draw_list.push_back(Magick::DrawableText(0, 200, "HelloWorld!"));
text_draw_list.push_back(Magick::DrawableStrokeColor(color));
text_draw_list.push_back(Magick::DrawableFillColor(color));
image.draw(text_draw_list);

Method 1 calculates best offsets given the gravity but does not have any word wrapping if the text is out of bounds of the image.

Method 2 has Method 1's problem plus it assumes that the correct offsets have been calculated so the text is written at the correct position.

How do I add automatic word wrapping to either of the 2 methods but preferably to method 1?

P.S: ImageMagick has automatic word wrapping by using the caption option but I couldn't find caption in Magick++.

Edit: Ugly boundary control based on font size.

Magick::Image image(Magick::Geometry(800,800),Magick::Color("white"));
Magick::Color color(0,0,0,0);
image.font("Waree");
image.fontPointsize(36);
image.strokeColor(color);
image.fillColor(color);
std::string txt = "HelloWorld!";
Magick::TypeMetric typeMetrics;
double fontSize = 36;
image.fontTypeMetrics(txt, &typeMetrics);
while(fontSize > 0)
{
 if(typeMetrics.textWidth() >= image.columns() || typeMetrics.textHeight() >= image.rows())
 {
  fontSize--;
  image.fontTypeMetrics(txt, &typeMetrics);
 }
}
image.annotate(txt, NorthWestGravity);

1条回答
别忘想泡老子
2楼-- · 2019-08-26 03:31

The best thing to do is read the caption.c source, and understand how word-wrapping is implemented. This would allow your application to have full control.

Another options would be to use PANGO: protocol. This would allow your content-author to have full control of word-wrapping, format, and many other font-display features.

But for the quickest approach, as you mentioned, caption already does this. But caption is not a method, but a file protocol.

#include <Magick++.h>

using namespace Magick;

int main(int argc, const char * argv[]) {
    InitializeMagick(argv[0]);
    Image words(Geometry(250,250), Color("white"));
    words.backgroundColor(Color("lime")); // might not be needed.
    words.font("Avenir-Heavy");
    words.fillColor(Color("firebrick"));
    words.strokeColor(Color("yellow"));
    words.read("CAPTION:Hello World!"); // <---- CAPTION: protocol
    words.write("/tmp/words.jpg");
    return 0;
}

word wrapping in Magick++

查看更多
登录 后发表回答