[TD]fix rich text font size in pdf

This commit is contained in:
wandererfan
2023-07-13 10:37:41 -04:00
committed by WandererFan
parent 4688582de3
commit f83a3fbb6a
6 changed files with 74 additions and 110 deletions

View File

@@ -288,9 +288,9 @@ void MDIViewPage::printPdf()
Gui::WaitCursor wc;
std::string utf8Content = fn.toUtf8().constData();
m_scene->setExporting(true);
m_scene->setExportingPdf(true);
printPdf(utf8Content);
m_scene->setExporting(false);
m_scene->setExportingPdf(false);
}
void MDIViewPage::printPdf(std::string file)

View File

@@ -54,7 +54,7 @@ using namespace TechDrawGui;
//**************************************************************
QGIRichAnno::QGIRichAnno() :
m_isExporting(false), m_hasHover(false)
m_isExportingPdf(false), m_isExportingSvg(false), m_hasHover(false)
{
setHandlesChildEvents(false);
setAcceptHoverEvents(false);
@@ -78,18 +78,6 @@ QGIRichAnno::QGIRichAnno() :
}
//void QGIRichAnno::select(bool state)
//{
// setSelected(state);
// draw();
//}
//void QGIRichAnno::hover(bool state)
//{
// m_hasHover = state;
// draw();
//}
void QGIRichAnno::updateView(bool update)
{
// Base::Console().Message("QGIRA::updateView() - %s\n", getViewName());
@@ -149,66 +137,57 @@ void QGIRichAnno::setTextItem()
TechDraw::DrawRichAnno* annoFeat = getFeature();
QString inHtml = QString::fromUtf8(annoFeat->AnnoText.getValue());
//don't do this multiplication if exporting to SVG as other apps interpret
//font sizes differently from QGraphicsTextItem (?)
if (!getExporting()) {
//convert point font sizes to (Rez, mm) font sizes
QRegularExpression rxFontSize(QString::fromUtf8("font-size:([0-9]*)pt;"));
QRegularExpressionMatch match;
double mmPerPoint = 0.353;
double sizeConvert = Rez::getRezFactor() * mmPerPoint;
int pos = 0;
QStringList findList;
QStringList replList;
while ((pos = inHtml.indexOf(rxFontSize, pos, &match)) != -1) {
QString found = match.captured(0);
findList << found;
QString qsOldSize = match.captured(1);
QRegularExpression rxFontSize(QString::fromUtf8("font-size:([0-9]*)pt;"));
QRegularExpressionMatch match;
double mmPerPoint = 0.353; // 25.4 mm/in / 72 points/inch
double sceneUnitsPerPoint = Rez::getRezFactor() * mmPerPoint; // scene units per point: 3.53
int pos = 0;
QStringList findList;
QStringList replList;
while ((pos = inHtml.indexOf(rxFontSize, pos, &match)) != -1) {
QString found = match.captured(0);
findList << found;
QString qsOldSize = match.captured(1);
QString repl = found;
double newSize = qsOldSize.toDouble();
newSize = newSize * sizeConvert;
QString qsNewSize = QString::number(newSize, 'f', 2);
repl.replace(qsOldSize, qsNewSize);
replList << repl;
pos += match.capturedLength();
}
QString outHtml = inHtml;
int iRepl = 0;
//TODO: check list for duplicates?
for ( ; iRepl < findList.size(); iRepl++) {
outHtml = outHtml.replace(findList[iRepl], replList[iRepl]);
}
QString repl = found;
double newSize = qsOldSize.toDouble();
m_text->setTextWidth(Rez::guiX(annoFeat->MaxWidth.getValue()));
m_text->setHtml(outHtml);
// setLineSpacing(50); //this has no effect on the display?!
// m_text->update();
if (annoFeat->ShowFrame.getValue()) {
QRectF r = m_text->boundingRect().adjusted(1, 1,-1, -1);
m_rect->setPen(rectPen());
m_rect->setBrush(Qt::NoBrush);
m_rect->setRect(r);
m_rect->show();
// The font size in the QGraphicsTextItem html is interpreted differently
// in svg rendering compared to the screen or pdf? The calculation for
// screen/pdf makes sense (scene units) and mm/point makes sense for svg
// but the requirement to divide by 2 is a mystery.
if (getExportingSvg()) {
newSize = newSize / ( 2.0 * mmPerPoint);
} else {
m_rect->hide();
newSize = newSize * sceneUnitsPerPoint;
}
QString qsNewSize = QString::number(newSize, 'f', 2);
repl.replace(qsOldSize, qsNewSize);
replList << repl;
pos += match.capturedLength();
}
QString outHtml = inHtml;
int iRepl = 0;
//TODO: check list for duplicates?
for ( ; iRepl < findList.size(); iRepl++) {
outHtml = outHtml.replace(findList[iRepl], replList[iRepl]);
}
m_text->setTextWidth(Rez::guiX(annoFeat->MaxWidth.getValue()));
m_text->setHtml(outHtml);
if (getExportingSvg()) {
// lines are correctly spaced on screen or in pdf, but svg needs this
setLineSpacing(100);
}
if (annoFeat->ShowFrame.getValue()) {
QRectF r = m_text->boundingRect().adjusted(1, 1,-1, -1);
m_rect->setPen(rectPen());
m_rect->setBrush(Qt::NoBrush);
m_rect->setRect(r);
m_rect->show();
} else {
// don't force line wrap & strip formatting that doesn't export well!
double realWidth = m_text->boundingRect().width();
m_text->setTextWidth(realWidth);
QFont f = prefFont();
double ptSize = prefPointSize();
f.setPointSizeF(ptSize);
m_text->setFont(f);
QString plainText = QTextDocumentFragment::fromHtml( inHtml ).toPlainText();
m_text->setPlainText(plainText);
setLineSpacing(100); //this doesn't appear in the generated Svg, but does space the lines!
m_rect->hide();
m_rect->update();
}
m_text->centerAt(0.0, 0.0);
@@ -218,25 +197,16 @@ void QGIRichAnno::setTextItem()
void QGIRichAnno::setLineSpacing(int lineSpacing)
{
//this line spacing should be px, but seems to be %? in any event, it does
//space out the lines.
//space out the lines
QTextBlock block = m_text->document()->begin();
for (; block.isValid(); block = block.next()) {
QTextCursor tc = QTextCursor(block);
QTextBlockFormat fmt = block.blockFormat();
// fmt.setTopMargin(lineSpacing); //no effect???
fmt.setBottomMargin(lineSpacing); //spaces out the lines!
tc.setBlockFormat(fmt);
// }
}
}
//void QGIRichAnno::drawBorder()
//{
//////Leaders have no border!
//// QGIView::drawBorder(); //good for debugging
//}
TechDraw::DrawRichAnno* QGIRichAnno::getFeature()
{
TechDraw::DrawRichAnno* result =
@@ -286,17 +256,6 @@ QFont QGIRichAnno::prefFont()
return PreferencesGui::labelFontQFont();
}
double QGIRichAnno::prefPointSize()
{
// Base::Console().Message("QGIRA::prefPointSize()\n");
double fontSize = Preferences::dimFontSizeMM();
//this conversion is only approximate. the factor changes for different fonts.
// double mmToPts = 2.83; //theoretical value
double mmToPts = 2.00; //practical value. seems to be reasonable for common fonts.
return round(fontSize * mmToPts);
}
void QGIRichAnno::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event) {
Q_UNUSED(event);

View File

@@ -74,25 +74,20 @@ public:
virtual TechDraw::DrawRichAnno* getFeature(void);
QPen rectPen() const;
void setExporting(bool b) { m_isExporting = b; }
bool getExporting(void) { return m_isExporting; }
public Q_SLOTS:
/* void textDragging(void);*/
/* void textDragFinished(void);*/
/* void hover(bool state);*/
/* void select(bool state);*/
void setExportingPdf(bool b) { m_isExportingPdf = b; }
bool getExportingPdf(void) { return m_isExportingPdf; }
void setExportingSvg(bool b) { m_isExportingSvg = b; }
bool getExportingSvg(void) { return m_isExportingSvg; }
protected:
virtual void draw() override;
void setLineSpacing(int lineSpacing);
double prefPointSize(void);
QFont prefFont(void);
virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override;
bool m_isExporting;
bool m_isExportingPdf;
bool m_isExportingSvg;
QGCustomText* m_text;
bool m_hasHover;
QGCustomRect* m_rect;

View File

@@ -1051,7 +1051,7 @@ void QGSPage::redraw1View(TechDraw::DrawView* dView)
}
}
void QGSPage::setExporting(bool enable)
void QGSPage::setExportingPdf(bool enable)
{
QList<QGraphicsItem*> sceneItems = items();
std::vector<QGIViewPart*> dvps;
@@ -1063,7 +1063,7 @@ void QGSPage::setExporting(bool enable)
dvps.push_back(qgiPart);
}
if (qgiRTA) {
qgiRTA->setExporting(enable);
qgiRTA->setExportingPdf(enable);
}
}
for (auto& v : dvps) {
@@ -1071,6 +1071,19 @@ void QGSPage::setExporting(bool enable)
}
}
// RichTextAnno needs to know when it is rendering an Svg as the font size
// is handled differently in Svg compared to the screen or Pdf.
void QGSPage::setExportingSvg(bool enable)
{
QList<QGraphicsItem*> sceneItems = items();
for (auto& qgi : sceneItems) {
QGIRichAnno* qgiRTA = dynamic_cast<QGIRichAnno*>(qgi);
if (qgiRTA) {
qgiRTA->setExportingSvg(enable);
}
}
}
void QGSPage::saveSvg(QString filename)
{
// TODO: We only have m_vpPage because constructor gets passed a view provider...
@@ -1106,7 +1119,7 @@ void QGSPage::saveSvg(QString filename)
bool saveState = m_vpPage->getFrameState();
m_vpPage->setFrameState(false);
m_vpPage->setTemplateMarkers(false);
setExporting(true);
setExportingSvg(true);
// Here we temporarily hide the page template, because Qt would otherwise convert the SVG template
// texts into series of paths, making the later document edits practically unfeasible.
@@ -1119,7 +1132,6 @@ void QGSPage::saveSvg(QString filename)
}
refreshViews();
// viewport()->repaint();
double width = Rez::guiX(page->getPageWidth());
double height = Rez::guiX(page->getPageHeight());
@@ -1135,13 +1147,12 @@ void QGSPage::saveSvg(QString filename)
m_vpPage->setFrameState(saveState);
m_vpPage->setTemplateMarkers(saveState);
setExporting(false);
setExportingSvg(false);
if (templateVisible && svgTemplate) {
svgTemplate->show();
}
refreshViews();
// viewport()->repaint();
temporaryFile.close();
postProcessXml(temporaryFile, filename, pageName);

View File

@@ -132,7 +132,8 @@ public:
TechDraw::DrawPage* getDrawPage();
void setExporting(bool enable);
void setExportingPdf(bool enable);
void setExportingSvg(bool enable);
virtual void refreshViews();
/// Renders the page to SVG with filename.

View File

@@ -97,8 +97,6 @@ public:
TechDraw::DrawPage* getDrawPage();
void setExporting(bool enable);
void makeGrid(int width, int height, double step);
void showGrid(bool state) { m_showGrid = state; }
void updateViewport() { viewport()->repaint(); }