7#if QT_CONFIG(clipboard)
14#include <QFontDatabase>
15#include <QFontMetrics>
16#include <QGuiApplication>
17#include <QLoggingCategory>
20#include <QRegularExpression>
30using Context = QPainter;
32#ifdef Q_STATIC_LOGGING_CATEGORY
33Q_STATIC_LOGGING_CATEGORY(log,
"qlitehtml", QtCriticalMsg)
38static QFont toQFont(litehtml::uint_ptr hFont)
40 return *
reinterpret_cast<Font *>(hFont);
45 return reinterpret_cast<Context *>(hdc);
50 return {position.x, position.y, position.width, position.height};
53static bool isVisible(
const litehtml::element::ptr &element)
56 return element->css().get_display() != litehtml::display_none
57 && element->css().get_visibility() == litehtml::visibility_visible;
62 litehtml::elements_list result;
63 litehtml::element::ptr current = element;
65 result.push_back(current);
66 current = current->parent();
68 std::reverse(
std::begin(result),
std::end(result));
74getCommonParent(
const litehtml::elements_list &a,
const litehtml::elements_list &b)
76 litehtml::element::ptr parent;
77 auto ait = a.cbegin();
78 auto bit = b.cbegin();
79 while (ait != a.cend() && bit != b.cend()) {
81 return {parent, *ait, *bit};
87 (ait != a.cend() ? *ait : litehtml::element::ptr()),
88 (bit != b.cend() ? *bit : litehtml::element::ptr())};
94 if (a.element == b.element) {
99 const litehtml::elements_list aPath = path(a.element);
100 const litehtml::elements_list bPath = path(b.element);
101 litehtml::element::ptr commonParent;
102 litehtml::element::ptr aBranch;
103 litehtml::element::ptr bBranch;
104 std::tie(commonParent, aBranch, bBranch) = getCommonParent(aPath, bPath);
106 qWarning() <<
"internal error: litehtml elements do not have common parent";
109 if (commonParent == a.element)
111 if (commonParent == b.element)
114 for (
const litehtml::element::ptr &child : commonParent->children()) {
115 if (child == aBranch)
117 if (child == bBranch)
120 qWarning() <<
"internal error: failed to find out order of litehtml elements";
127 const litehtml::element::ptr &stop)
131 litehtml::element::ptr current = element;
132 while (current != stop && !current->children().empty())
133 current = current->children().front();
141 const litehtml::element::ptr &stop)
145 litehtml::element::ptr current = element;
146 if (!current->is_root()) {
148 const litehtml::element::ptr parent = current->parent();
149 const litehtml::elements_list &children = parent->children();
150 auto childIt =
std::find_if(children.cbegin(),
152 [¤t](
const litehtml::element::ptr &e) {
155 if (childIt == children.cend()) {
156 qWarning() <<
"internal error: filed to find litehtml child element in parent";
160 if (childIt == children.cend())
161 return nextLeaf(parent, stop);
164 return firstLeaf(current, stop);
172 if (!element->children().empty())
173 return {element, -1, -1};
174 const QFont &font = toQFont(element->css().get_font());
175 const QFontMetrics fm(font);
177 for (
int i = 0; i < text.size(); ++i) {
178 const int width = fm.size(0, text.left(i + 1)).width();
179 if ((width + previous) / 2 >= pos.x())
180 return {element, i, previous};
183 return {element,
int(text.size()), previous};
191 const QPoint &viewportPos,
192 const std::function<
bool(
const litehtml::element::ptr &)> &action,
199 const QRect placement = toQRect(element->get_placement());
203 if (!placement.size().isEmpty() && element->tag() != litehtml::_html_
204 && element->tag() != litehtml::_body_ && !placement.contains(pos)) {
209 const litehtml::elements_list &children = element->children();
210 for (
auto it = children.cbegin(); it != children.cend(); ++it) {
211 if (deepest_child_at_point(*it, pos, viewportPos, action, level + 1))
214 if (placement.contains(pos))
215 return action(element);
221 const QPoint &viewportPos,
225 deepest_child_at_point(element,
228 [mode, &result, &pos](
const litehtml::element::ptr &element) {
229 const QRect placement = toQRect(element->get_placement());
231 element->get_text(text);
233 result = mode == Selection::Mode::Free
234 ? selectionDetails(element,
235 QString::fromStdString(text),
236 pos - placement.topLeft())
237 : Selection::Element({element, -1, -1});
250#if QT_VERSION >= QT_VERSION_CHECK(6
, 0
, 0
)
251 return QFont::Weight(cssWeight);
253 if (cssWeight <= 400)
254 return QFont::Weight(cssWeight * 50 / 400);
255 if (cssWeight >= 700)
256 return QFont::Weight(75 + (cssWeight - 700) * 25 / 300);
257 return QFont::Weight(50 + (cssWeight - 400) * 25 / 300);
264 case litehtml::font_style_normal:
265 return QFont::StyleNormal;
266 case litehtml::font_style_italic:
267 return QFont::StyleItalic;
270 qWarning(log) <<
"Unknown litehtml font style:" << style;
271 return QFont::StyleNormal;
276 return {color.red, color.green, color.blue, color.alpha};
282 case litehtml::border_style_dotted:
284 case litehtml::border_style_dashed:
286 case litehtml::border_style_solid:
287 return Qt::SolidLine;
289 qWarning(log) <<
"Unsupported border style:" << style;
291 return Qt::SolidLine;
296 return {toQColor(border.color), qreal(border.width), borderPenStyle(border.style)};
302 return {Qt::PointingHandCursor};
303 if (c ==
"all-scroll")
304 return {Qt::SizeAllCursor};
306 return {Qt::ArrowCursor};
308 return {Qt::UpArrowCursor};
309 if (c ==
"context-menu")
310 return {Qt::ArrowCursor};
311 if (c ==
"col-resize")
312 return {Qt::SplitHCursor};
314 return {Qt::DragCopyCursor};
315 if (c ==
"crosshair")
316 return {Qt::CrossCursor};
318 return {Qt::ArrowCursor};
320 return {Qt::SizeHorCursor};
321 if (c ==
"ew-resize")
322 return {Qt::SizeHorCursor};
324 return {Qt::OpenHandCursor};
326 return {Qt::ClosedHandCursor};
328 return {Qt::WhatsThisCursor};
330 return {Qt::SizeAllCursor};
332 return {Qt::SizeVerCursor};
333 if (c ==
"ne-resize")
334 return {Qt::SizeBDiagCursor};
335 if (c ==
"nesw-resize")
336 return {Qt::SizeBDiagCursor};
337 if (c ==
"ns-resize")
338 return {Qt::SizeVerCursor};
339 if (c ==
"nw-resize")
340 return {Qt::SizeFDiagCursor};
341 if (c ==
"nwse-resize")
342 return {Qt::SizeFDiagCursor};
344 return {Qt::ForbiddenCursor};
346 return {Qt::BlankCursor};
347 if (c ==
"not-allowed")
348 return {Qt::ForbiddenCursor};
350 return {Qt::PointingHandCursor};
352 return {Qt::BusyCursor};
353 if (c ==
"row-resize")
354 return {Qt::SplitVCursor};
356 return {Qt::SizeVerCursor};
357 if (c ==
"se-resize")
358 return {Qt::SizeFDiagCursor};
359 if (c ==
"sw-resize")
360 return {Qt::SizeBDiagCursor};
362 return {Qt::IBeamCursor};
364 return {Qt::ArrowCursor};
366 return {Qt::SizeHorCursor};
368 return {Qt::BusyCursor};
370 return {Qt::ArrowCursor};
371 qWarning(log) << QString(
"unknown cursor property \"%1\"").arg(c).toUtf8().constData();
372 return {Qt::ArrowCursor};
377 return !selection.isEmpty();
384 std::string elemText;
385 element.element->get_text(elemText);
386 const QString textStr = QString::fromStdString(elemText);
387 if (!textStr.isEmpty()) {
388 QRect rect = toQRect(element.element->get_placement()).adjusted(-1, -1, 1, 1);
391 }
else if (end.element) {
392 if (element.element == end.element) {
394 text += textStr.mid(element.index, end.index - element.index);
395 const int left = rect.left();
396 rect.setLeft(left + element
.x);
397 rect.setRight(left + end.x);
399 text += textStr.mid(element.index);
400 rect.setLeft(rect.left() + element
.x);
403 text += textStr.left(element.index);
404 rect.setRight(rect.left() + element
.x);
406 selection.append(rect);
410 if (startElem.element && endElem.element) {
415 std::tie(start, end) = getStartAndEnd(startElem, endElem);
422 addElement(start, end);
423 if (start.element != end.element) {
424 litehtml::element::ptr current = start.element;
426 current = nextLeaf(current, end.element);
427 if (current == end.element)
430 addElement({current, -1, -1});
431 }
while (current != end.element);
437#if QT_CONFIG(clipboard)
438 QClipboard *cb = QGuiApplication::clipboard();
439 if (cb->supportsSelection())
440 cb->setText(text, QClipboard::Selection);
447 for (
const QRect &r : selection)
448 rect = rect.united(r);
452DocumentContainer::DocumentContainer()
453 : d(
new DocumentContainerPrivate)
456DocumentContainer::~DocumentContainer() =
default;
461 litehtml::font_style italic,
462 unsigned int decoration,
463 litehtml::font_metrics *fm)
465 const QStringList splitNames = QString::fromUtf8(faceName).split(
',', Qt::SkipEmptyParts);
466 QStringList familyNames;
467 std::transform(splitNames.cbegin(),
469 std::back_inserter(familyNames),
470 [
this](
const QString &s) {
472 QString name = s.trimmed();
473 if (name.startsWith(
'\"'))
475 if (name.endsWith(
'\"'))
477 const QString lowerName = name.toLower();
478 if (lowerName ==
"serif")
480 if (lowerName ==
"sans-serif")
481 return sansSerifFont();
482 if (lowerName ==
"monospace")
483 return monospaceFont();
486 auto font =
new QFont();
487 font->setFamilies(familyNames);
488 font->setPixelSize(size);
489 font->setWeight(cssWeightToQtWeight(weight));
490 font->setStyle(toQFontStyle(italic));
491 font->setStyleStrategy(m_antialias ? QFont::PreferAntialias : QFont::NoAntialias);
492 if (decoration == litehtml::font_decoration_underline)
493 font->setUnderline(
true);
494 if (decoration == litehtml::font_decoration_overline)
495 font->setOverline(
true);
496 if (decoration == litehtml::font_decoration_linethrough)
497 font->setStrikeOut(
true);
499 const QFontMetrics metrics(*font);
500 fm->height = metrics.height();
501 fm->ascent = metrics.ascent();
502 fm->descent = metrics.descent();
503 fm->x_height = metrics.xHeight();
504 fm->draw_spaces =
true;
506 return reinterpret_cast<litehtml::uint_ptr>(font);
511 auto font =
reinterpret_cast<Font *>(hFont);
517 const QFontMetrics fm(toQFont(hFont));
518 return fm.horizontalAdvance(QString::fromUtf8(text));
523 litehtml::uint_ptr hFont,
524 litehtml::web_color color,
525 const litehtml::position &pos)
527 auto painter = toQPainter(hdc);
528 painter->setFont(toQFont(hFont));
529 painter->setPen(toQColor(color));
530 painter->drawText(toQRect(pos), 0, QString::fromUtf8(text));
536 return m_paintDevice->physicalDpiY() * pt * 11 / m_paintDevice->logicalDpiY() / 12;
541 return m_defaultFont.pointSize();
546 return m_defaultFontFamilyName.constData();
550 const litehtml::list_marker &marker)
552 auto painter = toQPainter(hdc);
553 if (marker.image.empty()) {
554 if (marker.marker_type == litehtml::list_style_type_square) {
555 painter->setPen(Qt::NoPen);
556 painter->setBrush(toQColor(marker.color));
557 painter->drawRect(toQRect(marker.pos));
558 }
else if (marker.marker_type == litehtml::list_style_type_disc) {
559 painter->setPen(Qt::NoPen);
560 painter->setBrush(toQColor(marker.color));
561 painter->drawEllipse(toQRect(marker.pos));
562 }
else if (marker.marker_type == litehtml::list_style_type_circle) {
563 painter->setPen(toQColor(marker.color));
564 painter->setBrush(Qt::NoBrush);
565 painter->drawEllipse(toQRect(marker.pos));
569 painter->setPen(Qt::NoPen);
570 painter->setBrush(toQColor(marker.color));
571 painter->drawEllipse(toQRect(marker.pos));
572 qWarning(log) <<
"list marker of type" << marker.marker_type <<
"not supported";
575 const QPixmap pixmap = getPixmap(QString::fromStdString(marker.image),
576 QString::fromStdString(marker.baseurl));
577 painter->drawPixmap(toQRect(marker.pos), pixmap);
583 const auto qtSrc = QString::fromUtf8(src);
584 const auto qtBaseUrl = QString::fromUtf8(baseurl);
585 Q_UNUSED(redraw_on_ready)
586 qDebug(log) <<
"load_image:" << QString(
"src = \"%1\";").arg(qtSrc).toUtf8().constData()
587 << QString(
"base = \"%1\"").arg(qtBaseUrl).toUtf8().constData();
588 const QUrl url = resolveUrl(qtSrc, qtBaseUrl);
589 if (m_pixmaps.contains(url))
593 pixmap.loadFromData(m_dataCallback(url));
594 m_pixmaps.insert(url, pixmap);
601 const auto qtSrc = QString::fromUtf8(src);
602 const auto qtBaseUrl = QString::fromUtf8(baseurl);
605 qDebug(log) <<
"get_image_size:" << QString(
"src = \"%1\";").arg(qtSrc).toUtf8().constData()
606 << QString(
"base = \"%1\"").arg(qtBaseUrl).toUtf8().constData();
607 const QPixmap pm = getPixmap(qtSrc, qtBaseUrl);
608 sz.width = pm.width();
609 sz.height = pm.height();
615 painter->setClipRect(clip, Qt::IntersectClip);
616 for (
const QRect &r : m_selection.selection) {
617 const QRect clientRect = r.translated(-m_scrollPosition);
618 const QPalette palette = m_paletteCallback();
619 painter->fillRect(clientRect, palette.brush(QPalette::Highlight));
626 litehtml::element::ptr current = e;
627 while (current && std::strlen(current->get_tagName()) == 0)
628 current = current->parent();
629 return current ? QString::fromUtf8(current->get_tagName()) : QString();
634 m_index.elementToIndex.clear();
635 m_index.indexToElement.clear();
636 m_index.text.clear();
640 litehtml::element::ptr current = firstLeaf(m_document->root(),
nullptr);
641 while (current != m_document->root()) {
642 m_index.elementToIndex.insert({current, index});
644 inBody = tagName(current).toLower() ==
"body";
645 if (inBody && isVisible(current)) {
647 current->get_text(text);
649 m_index.indexToElement.push_back({index, current});
650 const QString str = QString::fromStdString(text);
655 current = nextLeaf(current, m_document->root());
661 const QString oldText = m_selection.text;
662 m_selection.update();
663 if (!m_clipboardCallback)
666 const QString newText = m_selection.text;
667 if (oldText.isEmpty() && !newText.isEmpty())
668 m_clipboardCallback(
true);
669 else if (!oldText.isEmpty() && newText.isEmpty())
670 m_clipboardCallback(
false);
675 const QString oldText = m_selection.text;
677 if (!m_clipboardCallback)
680 if (!oldText.isEmpty())
681 m_clipboardCallback(
false);
685 const std::vector<litehtml::background_paint> &bgs)
687 auto painter = toQPainter(hdc);
689 for (
const litehtml::background_paint &bg : bgs) {
694 painter->setClipRect(toQRect(bg.clip_box));
695 const QRegion horizontalMiddle(QRect(bg.border_box.x,
696 bg.border_box.y + bg.border_radius.top_left_y,
698 bg.border_box.height - bg.border_radius.top_left_y
699 - bg.border_radius.bottom_left_y));
700 const QRegion horizontalTop(
701 QRect(bg.border_box.x + bg.border_radius.top_left_x,
703 bg.border_box.width - bg.border_radius.top_left_x - bg.border_radius.top_right_x,
704 bg.border_radius.top_left_y));
705 const QRegion horizontalBottom(QRect(bg.border_box.x + bg.border_radius.bottom_left_x,
706 bg.border_box.bottom() - bg.border_radius.bottom_left_y,
707 bg.border_box.width - bg.border_radius.bottom_left_x
708 - bg.border_radius.bottom_right_x,
709 bg.border_radius.bottom_left_y));
710 const QRegion topLeft(QRect(bg.border_box.left(),
712 2 * bg.border_radius.top_left_x,
713 2 * bg.border_radius.top_left_y),
715 const QRegion topRight(QRect(bg.border_box.right() - 2 * bg.border_radius.top_right_x,
717 2 * bg.border_radius.top_right_x,
718 2 * bg.border_radius.top_right_y),
720 const QRegion bottomLeft(QRect(bg.border_box.left(),
721 bg.border_box.bottom() - 2 * bg.border_radius.bottom_left_y,
722 2 * bg.border_radius.bottom_left_x,
723 2 * bg.border_radius.bottom_left_y),
725 const QRegion bottomRight(QRect(bg.border_box.right() - 2 * bg.border_radius.bottom_right_x,
726 bg.border_box.bottom() - 2 * bg.border_radius.bottom_right_y,
727 2 * bg.border_radius.bottom_right_x,
728 2 * bg.border_radius.bottom_right_y),
730 const QRegion clipRegion = horizontalMiddle.united(horizontalTop)
731 .united(horizontalBottom)
735 .united(bottomRight);
736 painter->setClipRegion(clipRegion, Qt::IntersectClip);
737 painter->setPen(Qt::NoPen);
738 painter->setBrush(toQColor(bg.color));
739 painter->drawRect(bg.border_box.x,
742 bg.border_box.height);
743 drawSelection(painter, toQRect(bg.border_box));
744 if (!bg.image.empty()) {
745 const QPixmap pixmap = getPixmap(QString::fromStdString(bg.image),
746 QString::fromStdString(bg.baseurl));
747 if (bg.repeat == litehtml::background_repeat_no_repeat) {
748 painter->drawPixmap(QRect(bg.position_x,
751 bg.image_size.height),
753 }
else if (bg.repeat == litehtml::background_repeat_repeat_x) {
754 if (bg.image_size.width > 0) {
755 int x = bg.border_box.left();
756 while (x <= bg.border_box.right()) {
757 painter->drawPixmap(QRect(x,
760 bg.image_size.height),
762 x += bg.image_size.width;
766 qWarning(log) <<
"unsupported background repeat" << bg.repeat;
774 const litehtml::borders &borders,
775 const litehtml::position &draw_pos,
780 auto painter = toQPainter(hdc);
781 if (borders.top.style != litehtml::border_style_none
782 && borders.top.style != litehtml::border_style_hidden) {
783 painter->setPen(borderPen(borders.top));
784 painter->drawLine(draw_pos.left() + borders.radius.top_left_x,
786 draw_pos.right() - borders.radius.top_right_x,
788 painter->drawArc(draw_pos.left(),
790 2 * borders.radius.top_left_x,
791 2 * borders.radius.top_left_y,
794 painter->drawArc(draw_pos.right() - 2 * borders.radius.top_right_x,
796 2 * borders.radius.top_right_x,
797 2 * borders.radius.top_right_y,
801 if (borders.bottom.style != litehtml::border_style_none
802 && borders.bottom.style != litehtml::border_style_hidden) {
803 painter->setPen(borderPen(borders.bottom));
804 painter->drawLine(draw_pos.left() + borders.radius.bottom_left_x,
806 draw_pos.right() - borders.radius.bottom_right_x,
808 painter->drawArc(draw_pos.left(),
809 draw_pos.bottom() - 2 * borders.radius.bottom_left_y,
810 2 * borders.radius.bottom_left_x,
811 2 * borders.radius.bottom_left_y,
814 painter->drawArc(draw_pos.right() - 2 * borders.radius.bottom_right_x,
815 draw_pos.bottom() - 2 * borders.radius.bottom_right_y,
816 2 * borders.radius.bottom_right_x,
817 2 * borders.radius.bottom_right_y,
821 if (borders.left.style != litehtml::border_style_none
822 && borders.left.style != litehtml::border_style_hidden) {
823 painter->setPen(borderPen(borders.left));
824 painter->drawLine(draw_pos.left(),
825 draw_pos.top() + borders.radius.top_left_y,
827 draw_pos.bottom() - borders.radius.bottom_left_y);
829 if (borders.right.style != litehtml::border_style_none
830 && borders.right.style != litehtml::border_style_hidden) {
831 painter->setPen(borderPen(borders.right));
832 painter->drawLine(draw_pos.right(),
833 draw_pos.top() + borders.radius.top_right_y,
835 draw_pos.bottom() - borders.radius.bottom_right_y);
841 m_caption = QString::fromUtf8(caption);
846 m_baseUrl = QString::fromUtf8(base_url);
850 const litehtml::element::ptr &el)
853 qDebug(log) <<
"link";
862 m_linkCallback(resolveUrl(QString::fromUtf8(url), m_baseUrl));
867 m_cursorCallback(toQCursor(QString::fromUtf8(cursor)));
873 qDebug(log) <<
"transform_text";
879 const std::string &url,
880 std::string &baseurl)
882 const QUrl actualUrl = resolveUrl(QString::fromStdString(url), QString::fromStdString(baseurl));
883 const QString urlString = actualUrl.toString(QUrl::None);
884 const int lastSlash = urlString.lastIndexOf(
'/');
885 baseurl = urlString.left(lastSlash).toStdString();
886 text = QString::fromUtf8(m_dataCallback(actualUrl)).toStdString();
890 const litehtml::border_radiuses &bdr_radius)
893 qDebug(log) <<
"set_clip";
901 qDebug(log) <<
"del_clip";
906 client = {m_clientRect.x(), m_clientRect.y(), m_clientRect.width(), m_clientRect.height()};
910 const char *tag_name,
911 const litehtml::string_map &attributes,
912 const std::shared_ptr<litehtml::document> &doc)
915 qDebug(log) <<
"create_element" << QString::fromUtf8(tag_name);
923 media.type = litehtml::media_type_screen;
925 qDebug(log) <<
"get_media_features";
931 qDebug(log) <<
"get_language";
936void DocumentContainer::setPaintDevice(QPaintDevice *paintDevice)
938 d->m_paintDevice = paintDevice;
941void DocumentContainer::setScrollPosition(
const QPoint &pos)
943 d->m_scrollPosition = pos;
946void DocumentContainer::setDocument(
const QByteArray &data, DocumentContainerContext *context)
948 d->m_pixmaps.clear();
950 d->m_document = litehtml::document::createFromString(data.constData(),
952 context->d->masterCss.toUtf8().constData());
956bool DocumentContainer::hasDocument()
const
958 return d->m_document.get();
961void DocumentContainer::setBaseUrl(
const QString &url)
963 d->set_base_url(url.toUtf8().constData());
966void DocumentContainer::render(
int width,
int height)
968 d->m_clientRect = {0, 0, width, height};
971 d->m_document->render(width);
972 d->updateSelection();
975void DocumentContainer::draw(QPainter *painter,
const QRect &clip)
977 d->drawSelection(painter, clip);
978 const QPoint pos = -d->m_scrollPosition;
979 const litehtml::position clipRect = {clip.x(), clip.y(), clip.width(), clip.height()};
980 d->m_document->draw(
reinterpret_cast<litehtml::uint_ptr>(painter), pos.x(), pos.y(), &clipRect);
983int DocumentContainer::documentWidth()
const
985 return d->m_document->width();
988int DocumentContainer::documentHeight()
const
990 return d->m_document->height();
993int DocumentContainer::anchorY(
const QString &anchorName)
const
995 litehtml::element::ptr element = d->m_document->root()->select_one(
996 QString(
"#%1").arg(anchorName).toStdString());
998 element = d->m_document->root()->select_one(QString(
"[name=%1]").arg(anchorName).toStdString());
1003 if (element->get_placement().y > 0)
1004 return element->get_placement().y;
1005 element = element->parent();
1010QVector<QRect> DocumentContainer::mousePressEvent(
const QPoint &documentPos,
1011 const QPoint &viewportPos,
1012 Qt::MouseButton button)
1014 if (!d->m_document || button != Qt::LeftButton)
1016 QVector<QRect> redrawRects;
1018 if (d->m_selection.isValid())
1019 redrawRects.append(d->m_selection.boundingRect());
1020 d->clearSelection();
1021 d->m_selection.selectionStartDocumentPos = documentPos;
1022 d->m_selection.startElem = selection_element_at_point(d->m_document->root(),
1025 d->m_selection.mode);
1027 litehtml::position::vector redrawBoxes;
1028 if (d->m_document->on_lbutton_down(
1029 documentPos.x(), documentPos.y(), viewportPos.x(), viewportPos.y(), redrawBoxes)) {
1030 for (
const litehtml::position &box : redrawBoxes)
1031 redrawRects.append(toQRect(box));
1036QVector<QRect> DocumentContainer::mouseMoveEvent(
const QPoint &documentPos,
1037 const QPoint &viewportPos)
1041 QVector<QRect> redrawRects;
1043 if (d->m_selection.isSelecting
1044 || (!d->m_selection.selectionStartDocumentPos.isNull()
1045 && (d->m_selection.selectionStartDocumentPos - documentPos).manhattanLength() >= kDragDistance
1046 && d->m_selection.startElem.element)) {
1047 const Selection::Element element = selection_element_at_point(d->m_document->root(),
1050 d->m_selection.mode);
1051 if (element.element) {
1053 d->m_selection.boundingRect() );
1054 d->m_selection.endElem = element;
1055 d->updateSelection();
1056 redrawRects.append(d->m_selection.boundingRect());
1058 d->m_selection.isSelecting =
true;
1060 litehtml::position::vector redrawBoxes;
1061 if (d->m_document->on_mouse_over(
1062 documentPos.x(), documentPos.y(), viewportPos.x(), viewportPos.y(), redrawBoxes)) {
1063 for (
const litehtml::position &box : redrawBoxes)
1064 redrawRects.append(toQRect(box));
1069QVector<QRect> DocumentContainer::mouseReleaseEvent(
const QPoint &documentPos,
1070 const QPoint &viewportPos,
1071 Qt::MouseButton button)
1073 if (!d->m_document || button != Qt::LeftButton)
1075 QVector<QRect> redrawRects;
1077 d->m_selection.isSelecting =
false;
1078 d->m_selection.selectionStartDocumentPos = {};
1079 if (d->m_selection.isValid())
1080 d->m_blockLinks =
true;
1082 d->clearSelection();
1083 litehtml::position::vector redrawBoxes;
1084 if (d->m_document->on_lbutton_up(
1085 documentPos.x(), documentPos.y(), viewportPos.x(), viewportPos.y(), redrawBoxes)) {
1086 for (
const litehtml::position &box : redrawBoxes)
1087 redrawRects.append(toQRect(box));
1089 d->m_blockLinks =
false;
1093QVector<QRect> DocumentContainer::mouseDoubleClickEvent(
const QPoint &documentPos,
1094 const QPoint &viewportPos,
1095 Qt::MouseButton button)
1097 if (!d->m_document || button != Qt::LeftButton)
1099 QVector<QRect> redrawRects;
1100 d->clearSelection();
1101 d->m_selection.mode = Selection::Mode::Word;
1102 const Selection::Element element = selection_element_at_point(d->m_document->root(),
1105 d->m_selection.mode);
1106 if (element.element) {
1107 d->m_selection.startElem = element;
1108 d->m_selection.endElem = d->m_selection.startElem;
1109 d->m_selection.isSelecting =
true;
1110 d->updateSelection();
1111 if (d->m_selection.isValid())
1112 redrawRects.append(d->m_selection.boundingRect());
1114 if (d->m_selection.isValid())
1115 redrawRects.append(d->m_selection.boundingRect());
1116 d->clearSelection();
1121QVector<QRect> DocumentContainer::leaveEvent()
1125 litehtml::position::vector redrawBoxes;
1126 if (d->m_document->on_mouse_leave(redrawBoxes)) {
1127 QVector<QRect> redrawRects;
1128 for (
const litehtml::position &box : redrawBoxes)
1129 redrawRects.append(toQRect(box));
1135QUrl DocumentContainer::linkAt(
const QPoint &documentPos,
const QPoint &viewportPos)
1139 const char *href =
nullptr;
1140 deepest_child_at_point(d->m_document->root(),
1143 [&href](
const litehtml::element::ptr &e) {
1144 const litehtml::element::ptr parent = e->parent();
1145 if (parent && parent->tag() == litehtml::_a_) {
1146 href = parent->get_attr(
"href");
1153 return d->resolveUrl(QString::fromUtf8(href), d->m_baseUrl);
1157QString DocumentContainer::caption()
const
1159 return d->m_caption;
1162QString DocumentContainer::selectedText()
const
1164 return d->m_selection.text;
1167void DocumentContainer::findText(
const QString &text,
1168 QTextDocument::FindFlags flags,
1172 QVector<QRect> *oldSelection,
1173 QVector<QRect> *newSelection)
1178 oldSelection->clear();
1180 newSelection->clear();
1183 const bool backward = flags & QTextDocument::FindBackward;
1184 int startIndex = backward ? -1 : 0;
1185 if (d->m_selection.startElem.element && d->m_selection.endElem.element) {
1188 Selection::Element start;
1189 Selection::Element end;
1190 std::tie(start, end) = getStartAndEnd(d->m_selection.startElem, d->m_selection.endElem);
1191 Selection::Element searchStart;
1192 if (incremental || backward) {
1193 if (start.index < 0)
1194 searchStart = {firstLeaf(start.element,
nullptr), 0, -1};
1196 searchStart = start;
1199 searchStart = {nextLeaf(end.element,
nullptr), 0, -1};
1203 const auto findInIndex = d->m_index.elementToIndex.find(searchStart.element);
1204 if (findInIndex == std::end(d->m_index.elementToIndex)) {
1205 qWarning() <<
"internal error: cannot find litehmtl element in index";
1208 startIndex = findInIndex->second + searchStart.index;
1213 const auto fillXPos = [](
const Selection::Element &e) {
1215 e.element->get_text(ttext);
1216 const QString text = QString::fromStdString(ttext);
1217 const auto fontPtr = e.element->css().get_font();
1220 const QFont &font = toQFont(fontPtr);
1221 const QFontMetrics fm(font);
1222 return Selection::Element{e.element, e.index, fm.size(0, text.left(e.index)).width()};
1225 QString term = QRegularExpression::escape(text);
1226 if (flags & QTextDocument::FindWholeWords)
1227 term = QString(
"\\b%1\\b").arg(term);
1228 const QRegularExpression::PatternOptions patternOptions
1229 = (flags & QTextDocument::FindCaseSensitively) ? QRegularExpression::NoPatternOption
1230 : QRegularExpression::CaseInsensitiveOption;
1231 const QRegularExpression expression(term, patternOptions);
1233 int foundIndex = backward ? d->m_index.text.lastIndexOf(expression, startIndex)
1234 : d->m_index.text.indexOf(expression, startIndex);
1235 if (foundIndex < 0) {
1236 foundIndex = backward ? d->m_index.text.lastIndexOf(expression)
1237 : d->m_index.text.indexOf(expression);
1238 if (wrapped && foundIndex >= 0)
1241 if (foundIndex >= 0) {
1242 const Index::Entry startEntry = d->m_index.findElement(foundIndex);
1243 const Index::Entry endEntry = d->m_index.findElement(foundIndex + text.size());
1244 if (!startEntry.second || !endEntry.second) {
1245 qWarning() <<
"internal error: search ended up with nullptr elements";
1249 *oldSelection = d->m_selection.selection;
1250 d->clearSelection();
1251 d->m_selection.startElem = fillXPos({startEntry.second, foundIndex - startEntry.first, -1});
1252 d->m_selection.endElem = fillXPos(
1253 {endEntry.second,
int(foundIndex + text.size() - endEntry.first), -1});
1254 d->updateSelection();
1256 *newSelection = d->m_selection.selection;
1264void DocumentContainer::setDefaultFont(
const QFont &font)
1266 d->m_defaultFont = font;
1267 d->m_defaultFontFamilyName = d->m_defaultFont.family().toUtf8();
1270 if (d->m_document && d->m_document->root()) {
1271 d->m_document->root()->refresh_styles();
1272 d->m_document->root()->compute_styles();
1276QFont DocumentContainer::defaultFont()
const
1278 return d->m_defaultFont;
1281void DocumentContainer::setAntialias(
bool on)
1283 d->m_antialias = on;
1286bool DocumentContainer::antialias()
const
1288 return d->m_antialias;
1291void DocumentContainer::setDataCallback(
const DocumentContainer::DataCallback &callback)
1293 d->m_dataCallback = callback;
1296void DocumentContainer::setCursorCallback(
const DocumentContainer::CursorCallback &callback)
1298 d->m_cursorCallback = callback;
1301void DocumentContainer::setLinkCallback(
const DocumentContainer::LinkCallback &callback)
1303 d->m_linkCallback = callback;
1306void DocumentContainer::setPaletteCallback(
const DocumentContainer::PaletteCallback &callback)
1308 d->m_paletteCallback = callback;
1311void DocumentContainer::setClipboardCallback(
const DocumentContainer::ClipboardCallback &callback)
1313 d->m_clipboardCallback = callback;
1320 if (element->get_placement().y >= y)
1322 for (
const litehtml::element::ptr &child : element->children()) {
1323 litehtml::element::ptr result = elementForY(y, child);
1335 return elementForY(y, document->root());
1338int DocumentContainer::withFixedElementPosition(
int y,
const std::function<
void()> &action)
1340 const litehtml::element::ptr element = elementForY(y, d->m_document);
1343 return element->get_placement().y;
1349 const QUrl url = resolveUrl(imageUrl, baseUrl);
1350 if (!m_pixmaps.contains(url)) {
1351 qWarning(log) <<
"draw_background: pixmap not loaded for" << url;
1354 return m_pixmaps.value(url);
1360 return {
"Times New Roman"};
1383 const QUrl qurl = QUrl::fromEncoded(url.toUtf8());
1384 if (qurl.scheme().isEmpty()) {
1385 if (url.startsWith(
'#'))
1387 const QUrl pageBaseUrl = QUrl(baseUrl.isEmpty() ? m_baseUrl : baseUrl);
1388 if (url.startsWith(
"//"))
1389 return QUrl(pageBaseUrl.scheme() +
":" + url);
1390 QUrl serverUrl = QUrl(pageBaseUrl);
1391 serverUrl.setPath(
"");
1392 const QString actualBaseUrl = url.startsWith(
'/')
1393 ? serverUrl.toString(QUrl::FullyEncoded)
1394 : pageBaseUrl.toString(QUrl::FullyEncoded);
1395 QUrl resolvedUrl(actualBaseUrl +
'/' + url);
1396 resolvedUrl.setPath(resolvedUrl.path(QUrl::FullyEncoded | QUrl::NormalizePathSegments), QUrl::TolerantMode);
1404 const auto upper = std::upper_bound(std::begin(indexToElement),
1405 std::end(indexToElement),
1407 [](
const Entry &a,
const Entry &b) {
1408 return a.first < b.first;
1410 if (upper == std::begin(indexToElement))
1412 return *(upper - 1);
1415DocumentContainerContext::DocumentContainerContext()
1416 : d(
new DocumentContainerContextPrivate)
1419DocumentContainerContext::~DocumentContainerContext() =
default;
1421void DocumentContainerContext::setMasterStyleSheet(
const QString &css)
void get_image_size(const char *src, const char *baseurl, litehtml::size &sz) override
int pt_to_px(int pt) const override
void get_client_rect(litehtml::position &client) const override
const char * get_default_font_name() const override
void on_anchor_click(const char *url, const litehtml::element::ptr &el) override
void get_media_features(litehtml::media_features &media) const override
void draw_borders(litehtml::uint_ptr hdc, const litehtml::borders &borders, const litehtml::position &draw_pos, bool root) override
void link(const std::shared_ptr< litehtml::document > &doc, const litehtml::element::ptr &el) override
litehtml::uint_ptr create_font(const char *faceName, int size, int weight, litehtml::font_style italic, unsigned int decoration, litehtml::font_metrics *fm) override
void draw_list_marker(litehtml::uint_ptr hdc, const litehtml::list_marker &marker) override
QString serifFont() const
void set_caption(const char *caption) override
void get_language(std::string &language, std::string &culture) const override
void delete_font(litehtml::uint_ptr hFont) override
void import_css(std::string &text, const std::string &url, std::string &baseurl) override
void load_image(const char *src, const char *baseurl, bool redraw_on_ready) override
void transform_text(std::string &text, litehtml::text_transform tt) override
void draw_background(litehtml::uint_ptr hdc, const std::vector< litehtml::background_paint > &bgs) override
int get_default_font_size() const override
void set_clip(const litehtml::position &pos, const litehtml::border_radiuses &bdr_radius) override
QString monospaceFont() const
int text_width(const char *text, litehtml::uint_ptr hFont) override
void set_cursor(const char *cursor) override
std::shared_ptr< litehtml::element > create_element(const char *tag_name, const litehtml::string_map &attributes, const std::shared_ptr< litehtml::document > &doc) override
void drawSelection(QPainter *painter, const QRect &clip) const
DocumentContainer::DataCallback m_dataCallback
QUrl resolveUrl(const QString &url, const QString &baseUrl) const
QString sansSerifFont() const
void set_base_url(const char *base_url) override
QPixmap getPixmap(const QString &imageUrl, const QString &baseUrl)
void draw_text(litehtml::uint_ptr hdc, const char *text, litehtml::uint_ptr hFont, litehtml::web_color color, const litehtml::position &pos) override
QRect boundingRect() const
static std::tuple< litehtml::element::ptr, litehtml::element::ptr, litehtml::element::ptr > getCommonParent(const litehtml::elements_list &a, const litehtml::elements_list &b)
static Qt::PenStyle borderPenStyle(litehtml::border_style style)
static QCursor toQCursor(const QString &c)
static Selection::Element selection_element_at_point(const litehtml::element::ptr &element, const QPoint &pos, const QPoint &viewportPos, Selection::Mode mode)
static Q_LOGGING_CATEGORY(log, "qlitehtml", QtCriticalMsg) static QFont toQFont(litehtml
static litehtml::element::ptr firstLeaf(const litehtml::element::ptr &element, const litehtml::element::ptr &stop)
static QFont::Style toQFontStyle(litehtml::font_style style)
static QRect toQRect(litehtml::position position)
static bool deepest_child_at_point(const litehtml::element::ptr &element, const QPoint &pos, const QPoint &viewportPos, const std::function< bool(const litehtml::element::ptr &)> &action, int level=0)
static QPen borderPen(const litehtml::border &border)
static std::pair< Selection::Element, Selection::Element > getStartAndEnd(const Selection::Element &a, const Selection::Element &b)
static litehtml::elements_list path(const litehtml::element::ptr &element)
static QPainter * toQPainter(litehtml::uint_ptr hdc)
static Selection::Element selectionDetails(const litehtml::element::ptr &element, const QString &text, const QPoint &pos)
static litehtml::element::ptr nextLeaf(const litehtml::element::ptr &element, const litehtml::element::ptr &stop)
static QColor toQColor(const litehtml::web_color &color)
static bool isVisible(const litehtml::element::ptr &element)
static litehtml::element::ptr elementForY(int y, const litehtml::element::ptr &element)
static QFont::Weight cssWeightToQtWeight(int cssWeight)
static QString tagName(const litehtml::element::ptr &e)
Entry findElement(int index) const