【发布时间】:2016-03-24 23:22:30
【问题描述】:
我正在尝试通过这样做在 Magick++ 中向图像添加文本:
方法一:
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);
方法二:
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);
方法 1 在给定重力的情况下计算最佳偏移量,但如果文本超出图像范围,则不进行任何自动换行。
方法 2 存在方法 1 的问题,而且它假定已计算出正确的偏移量,因此将文本写入正确的位置。
如何将自动换行添加到这两种方法中的任何一种,但最好是方法 1?
PS:ImageMagick 使用 caption 选项自动换行,但我找不到 caption在 Magick++ 中。
编辑:基于字体大小的丑陋边界控制。
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);
【问题讨论】:
标签: c++ image-processing imagemagick word-wrap magick++