WTextItem WPdfImage::measureText(const WString& text, double maxWidth, bool wordWrap) { if (trueTypeFont_ && !trueTypeFonts_->busy()) return trueTypeFonts_->measureText(painter()->font(), text, maxWidth, wordWrap); else { HPDF_REAL width = 0; if (!wordWrap) maxWidth = 1E9; else maxWidth += EPSILON; if (trueTypeFonts_->busy()) setChanged(PainterChangeFlag::Font); std::string s = trueTypeFont_ ? text.toUTF8() : text.narrow(); int bytes = HPDF_Page_MeasureText(page_, s.c_str(), maxWidth, wordWrap, &width); if (trueTypeFont_) return WTextItem(WString::fromUTF8(s.substr(0, bytes)), width); else return WTextItem(text.value().substr(0, bytes), width); } }
void WMenuItem::setText(const WString& text) { if (!text_) { text_ = new WLabel(anchor()); text_->setTextFormat(Wt::PlainText); } text_->setText(text); if (!customPathComponent_) { std::string result; #ifdef WT_TARGET_JAVA WString t = text; #else const WString& t = text; #endif if (t.literal()) result = t.narrow(); else result = t.key(); for (unsigned i = 0; i < result.length(); ++i) { if (std::isspace((unsigned char)result[i])) result[i] = '-'; else if (std::isalnum((unsigned char)result[i])) result[i] = std::tolower((unsigned char)result[i]); else result[i] = '_'; } setPathComponent(result); customPathComponent_ = false; } }
void WPdfImage::drawText(const WRectF& rect, WFlags<AlignmentFlag> flags, TextFlag textFlag, const WString& text, const WPointF *clipPoint) { // FIXME: textFlag if (clipPoint && painter() && !painter()->clipPath().isEmpty()) { if (!painter()->clipPathTransform().map(painter()->clipPath()) .isPointInPath(painter()->worldTransform().map(*clipPoint))) return; } if (trueTypeFont_ && !trueTypeFonts_->busy()) trueTypeFonts_->drawText(painter()->font(), rect, flags, text); else { HPDF_REAL left, top, right, bottom; HPDF_TextAlignment alignment = HPDF_TALIGN_LEFT; AlignmentFlag horizontalAlign = flags & AlignHorizontalMask; AlignmentFlag verticalAlign = flags & AlignVerticalMask; switch (horizontalAlign) { default: // should never happen case AlignmentFlag::Left: left = rect.left(); right = left + 10000; alignment = HPDF_TALIGN_LEFT; break; case AlignmentFlag::Right: right = rect.right(); left = right - 10000; alignment = HPDF_TALIGN_RIGHT; break; case AlignmentFlag::Center: { float center = rect.center().x(); left = center - 5000; right = center + 5000; alignment = HPDF_TALIGN_CENTER; break; } } switch (verticalAlign) { default: // fall-through ; should never happen case AlignmentFlag::Top: top = rect.top(); break; case AlignmentFlag::Middle: // FIXME: use font metrics to center middle of ascent ! top = rect.center().y() - 0.60 * fontSize_; break; case AlignmentFlag::Bottom: top = rect.bottom() - fontSize_; break; } bottom = top + fontSize_; if (trueTypeFonts_->busy()) setChanged(PainterChangeFlag::Font); HPDF_Page_GSave(page_); // Undo the global inversion HPDF_Page_Concat(page_, 1, 0, 0, -1, 0, bottom); HPDF_Page_BeginText(page_); // Need to fill text using pen color const WColor& penColor = painter()->pen().color(); HPDF_Page_SetRGBFill(page_, penColor.red() / 255., penColor.green() / 255., penColor.blue() / 255.); std::string s = trueTypeFont_ ? text.toUTF8() : text.narrow(); HPDF_Page_TextRect(page_, left, fontSize_, right, 0, s.c_str(), alignment, nullptr); HPDF_Page_EndText(page_); HPDF_Page_GRestore(page_); } }
void WPdfImage::drawText(const WRectF& rect, WFlags<AlignmentFlag> flags, TextFlag textFlag, const WString& text) { // FIXME: textFlag if (trueTypeFont_ && !trueTypeFonts_->busy()) trueTypeFonts_->drawText(painter()->font(), rect, flags, text); else { HPDF_REAL left, top, right, bottom; HPDF_TextAlignment alignment; AlignmentFlag horizontalAlign = flags & AlignHorizontalMask; AlignmentFlag verticalAlign = flags & AlignVerticalMask; switch (horizontalAlign) { case AlignLeft: left = rect.left(); right = left + 1000; alignment = HPDF_TALIGN_LEFT; break; case AlignRight: right = rect.right(); left = right - 1000; alignment = HPDF_TALIGN_RIGHT; break; case AlignCenter: { float center = rect.center().x(); left = center - 500; right = center + 500; alignment = HPDF_TALIGN_CENTER; break; } default: break; } switch (verticalAlign) { case AlignTop: top = rect.top(); break; case AlignMiddle: // FIXME: use font metrics to center middle of ascent ! top = rect.center().y() - 0.60 * fontSize_; break; case AlignBottom: top = rect.bottom() - fontSize_; break; default: break; } bottom = top + fontSize_; if (trueTypeFonts_->busy()) setChanged(Font); HPDF_Page_GSave(page_); // Undo the global inversion HPDF_Page_Concat(page_, 1, 0, 0, -1, 0, bottom); HPDF_Page_BeginText(page_); // Need to fill text using pen color const WColor& penColor = painter()->pen().color(); HPDF_Page_SetRGBFill(page_, penColor.red() / 255., penColor.green() / 255., penColor.blue() / 255.); std::string s = trueTypeFont_ ? text.toUTF8() : text.narrow(); HPDF_Page_TextRect(page_, left, fontSize_, right, 0, s.c_str(), alignment, 0); HPDF_Page_EndText(page_); HPDF_Page_GRestore(page_); } }