37 #include <OGRE/OgreMaterialManager.h>
39 #include <OGRE/OgreTexture.h>
40 #include <OGRE/OgreHardwarePixelBuffer.h>
41 #include <QFontDatabase>
43 #include <QStaticText>
44 #include <QTextDocument>
45 #include <boost/algorithm/string.hpp>
46 #include <boost/format.hpp>
47 #include <jsk_topic_tools/log_utils.h>
53 texture_width_(0), texture_height_(0),
57 bg_color_(0, 0, 0, 0),
58 fg_color_(255, 255, 255, 255.0),
59 require_update_texture_(false)
63 ros::message_traits::datatype<jsk_rviz_plugins::OverlayText>(),
64 "jsk_rviz_plugins::OverlayText topic to subscribe to.",
65 this, SLOT( updateTopic() ));
67 "Overtake Position Properties",
false,
68 "overtake position properties specified by message such as left, top and font",
69 this, SLOT(updateOvertakePositionProperties()));
71 "Overtake FG Color Properties",
false,
72 "overtake color properties specified by message such as foreground color and alpha",
73 this, SLOT(updateOvertakeFGColorProperties()));
75 "Overtake BG Color Properties",
false,
76 "overtake color properties specified by message such as background color and alpha",
77 this, SLOT(updateOvertakeBGColorProperties()));
79 "Align Bottom",
false,
80 "align text with the bottom of the overlay region",
81 this, SLOT(updateAlignBottom()));
83 "Invert Shadow",
false,
84 "make shadow lighter than original text",
85 this, SLOT(updateInvertShadow()));
89 this, SLOT(updateTop()));
90 top_property_->setMin(0);
94 this, SLOT(updateLeft()));
95 left_property_->setMin(0);
99 this, SLOT(updateWidth()));
100 width_property_->setMin(0);
104 this, SLOT(updateHeight()));
105 height_property_->setMin(0);
109 this, SLOT(updateTextSize()));
110 text_size_property_->setMin(0);
114 this, SLOT(updateLineWidth()));
115 line_width_property_->setMin(0);
117 "Foreground Color", QColor(25, 255, 240),
119 this, SLOT(updateFGColor()));
121 "Foreground Alpha", 0.8,
"Foreground Alpha",
122 this, SLOT(updateFGAlpha()));
123 fg_alpha_property_->setMin(0.0);
124 fg_alpha_property_->setMax(1.0);
126 "Background Color", QColor(0, 0, 0),
128 this, SLOT(updateBGColor()));
130 "Background Alpha", 0.8,
"Background Alpha",
131 this, SLOT(updateBGAlpha()));
132 bg_alpha_property_->setMin(0.0);
133 bg_alpha_property_->setMax(1.0);
135 QFontDatabase database;
136 font_families_ = database.families();
138 "font",
"DejaVu Sans Mono",
141 for (
size_t i = 0; i < font_families_.size(); i++) {
142 font_property_->addOption(font_families_[i], (
int)i);
193 if (topic_name.length() > 0 && topic_name !=
"/") {
243 QPainter painter( &Hud );
244 painter.setRenderHint(QPainter::Antialiasing,
true);
246 uint16_t
w =
overlay_->getTextureWidth();
247 uint16_t h =
overlay_->getTextureHeight();
252 QFont
font(
font_.length() > 0 ?
font_.c_str():
"Liberation Sans");
255 painter.setFont(
font);
257 if (
text_.length() > 0) {
261 shadow_color = Qt::white;
263 shadow_color = Qt::black;
264 shadow_color.setAlpha(
fg_color_.alpha());
266 std::string color_wrapped_text
267 = (boost::format(
"<span style=\"color: rgba(%2%, %3%, %4%, %5%)\">%1%</span>")
272 std::regex color_tag_re(
"color:.+?;");
273 std::string null_char(
"");
274 std::string formatted_text_ = std::regex_replace(
text_, color_tag_re, null_char);
275 std::string color_wrapped_shadow
276 = (boost::format(
"<span style=\"color: rgba(%2%, %3%, %4%, %5%)\">%1%</span>")
277 % formatted_text_ % shadow_color.red() % shadow_color.green() % shadow_color.blue() % shadow_color.alpha()).
str();
279 QStaticText static_text(
280 boost::algorithm::replace_all_copy(color_wrapped_text,
"\n",
"<br >").c_str());
281 static_text.setTextWidth(
w);
283 painter.setPen(QPen(shadow_color, std::max(
line_width_,1), Qt::SolidLine));
284 QStaticText static_shadow(
285 boost::algorithm::replace_all_copy(color_wrapped_shadow,
"\n",
"<br >").c_str());
286 static_shadow.setTextWidth(
w);
289 painter.drawStaticText(1, 1, static_shadow);
290 painter.drawStaticText(0, 0, static_text);
292 QStaticText only_wrapped_text(color_wrapped_text.c_str());
293 QFontMetrics fm(painter.fontMetrics());
294 QRect text_rect = fm.boundingRect(0, 0,
w, h,
295 Qt::TextWordWrap | Qt::AlignLeft | Qt::AlignTop,
296 only_wrapped_text.text().remove(QRegExp(
"<[^>]*>")));
297 painter.drawStaticText(1, h - text_rect.height()+1, static_shadow);
298 painter.drawStaticText(0, h - text_rect.height(), static_text);
308 (
const jsk_rviz_plugins::OverlayText::ConstPtr& msg)
314 static int count = 0;
316 ss <<
"OverlayTextDisplayObject" <<
count++;
321 if (
msg->action == jsk_rviz_plugins::OverlayText::DELETE) {
324 else if (
msg->action == jsk_rviz_plugins::OverlayText::ADD) {
341 msg->bg_color.b * 255.0,
342 msg->bg_color.a * 255.0);
345 msg->fg_color.g * 255.0,
346 msg->fg_color.b * 255.0,
347 msg->fg_color.a * 255.0);
534 ROS_FATAL(
"Unexpected error at selecting font index %d.", font_index);
552 return (top_ < y && top_ + texture_height_ >
y &&
553 left_ < x && left_ + texture_width_ >
x);