如何添加文字包装到Magick ++中的文本 [英] How to add word wrapping to texts in Magick++

查看:845
本文介绍了如何添加文字包装到Magick ++中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要通过这样做,向Magick ++中的图片添加文字:



方法1:

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

  Magick :: Image image(Magick :: Geometry(800,800),Magick :: Color(white)); 
Magick ::颜色(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));
//手动偏移
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);

方法1计算给定重力的最佳偏移,但如果文本不在



方法2具有方法1的问题,并假定已计算正确的偏移量,以便将文本写入正确的位置。



如何将自动换行功能添加到这两种方法中的任一种,但最好使用方法1?



PS:ImageMagick通过使用字幕 选项自动换行,但找不到 Magick ++。



编辑:基于字体大小的边框控制。

  Magick :: Image image(Magick :: Geometry(800,800),Magick :: Color(white)); 
Magick ::颜色(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);


解决方案

最好的办法是阅读


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

解决方案

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

这篇关于如何添加文字包装到Magick ++中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆