How to add word wrapping to texts in Magick++

巧了我就是萌 提交于 2020-01-07 09:02:21

问题


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:


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;
}



来源:https://stackoverflow.com/questions/34352420/how-to-add-word-wrapping-to-texts-in-magick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!