Actually, by stealing some code from PageEdit (which marks the id of any injected dark mode code) and borrowing liberally from its GetCleanHTML routine) we could just grab the current contents of the PreviewWindow processed html, then use gumbo to clean it of Dark mode crap we injected (but keep any mathml stuff), so we could effectively create a printable PreviewWindow.
In Utility.cpp we would have to modify our DARK_STYLE to set an id attribute to make it easy to remove. Based on PageEdit's version this would look something like:
Code:
static const QString DARK_STYLE =
"<style id=\"Sigil_Injected\">:root { background-color: %1; color: %2; } ::-webkit-scrollbar { display: none; }</style>"
"<link rel=\"stylesheet\" type=\"text/css\" href=\"%3\" />";
And we could create a routine to remove that injected code and the following css dark mode links by doing something like:
Code:
static const QStringList DARKCSSLINKS = QStringList() << "qrc:///dark/mac_dark_scrollbar.css"
<< "qrc:///dark/win_dark_scrollbar.css"
<< "qrc:///dark/lin_dark_scrollbar.css";
QString PreviewWindow::GetHtmlWithNoDarkMode()
{
QString text = m_Preview->GetHtml();
// now remove any leftovers and make sure it is well formed
GumboInterface gi = GumboInterface(text, "any_version");
QList<GumboNode*> nodes;
QList<GumboTag> tags;
// remove any added AddDarkCSS (style node has id="Sigil_Injected")
tags = QList<GumboTag>() << GUMBO_TAG_STYLE;
nodes = gi.get_all_nodes_with_tags(tags);
foreach(GumboNode * node, nodes) {
GumboAttribute* attr = gumbo_get_attribute(&node->v.element.attributes, "id");
if (attr && QString::fromUtf8(attr->value) == "Sigil_Injected") {
// qDebug() << "removing Sigil_Injected dark style";
gumbo_remove_from_parent(node);
gumbo_destroy_node(node);
break;
}
}
// then the associated scrollbar stylesheet link
tags = QList<GumboTag>() << GUMBO_TAG_LINK;
nodes = gi.get_all_nodes_with_tags(tags);
foreach(GumboNode * node, nodes) {
GumboAttribute* attr = gumbo_get_attribute(&node->v.element.attributes, "href");
if (attr) {
QString attrval = QString::fromUtf8(attr->value);
if (DARKCSSLINKS.contains(attrval) ) {
// qDebug() << "removing dark css links";
gumbo_remove_from_parent(node);
gumbo_destroy_node(node);
break;
}
}
}
text = gi.getxhtml();
return text;
}
Then we could create a second hidden ViewEditors/ViewPreview (call it maybe "PrintablePreview") and use its CustomSetDocument to load it with our just cleaned html and to set its base location so that links work. The equivalent of:
m_PrintablePreview->CustomSetDocument(m_FilePath, text);
Something close to that should work as it does with PageEdit.