Skip to content

Commit

Permalink
TechDraw: SVG export refactoring - Keep correct font sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
pavltom authored and WandererFan committed Jun 11, 2019
1 parent f4ca09f commit 28b2df0
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/Mod/TechDraw/Gui/QGIHighlight.cpp
Expand Up @@ -84,7 +84,7 @@ void QGIHighlight::makeHighlight()
void QGIHighlight::makeReference()
{
prepareGeometryChange();
m_refFont.setPointSizeF(QGIView::calculateFontPointSizeF(this, m_refSize));
m_refFont.setPixelSize(QGIView::calculateFontPixelSize(m_refSize));
m_reference->setFont(m_refFont);
m_reference->setPlainText(QString::fromUtf8(m_refText));
double fudge = Rez::guiX(1.0);
Expand Down
3 changes: 2 additions & 1 deletion src/Mod/TechDraw/Gui/QGISVGTemplate.h
Expand Up @@ -54,14 +54,15 @@ class TechDrawGuiExport QGISVGTemplate : public QGITemplate
void draw();
virtual void updateView(bool update = false);

TechDraw::DrawSVGTemplate *getSVGTemplate();

protected:
void openFile(const QFile &file);
void load (const QString & fileName);
void createClickHandles(void);

protected:
bool firstTime;
TechDraw::DrawSVGTemplate * getSVGTemplate();
QGraphicsSvgItem *m_svgItem;
QSvgRenderer *m_svgRender;
virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value);
Expand Down
4 changes: 2 additions & 2 deletions src/Mod/TechDraw/Gui/QGISectionLine.cpp
Expand Up @@ -195,7 +195,7 @@ void QGISectionLine::makeSymbolsTrad()
extLineStart = m_start + offset;
extLineEnd = m_end + offset;
prepareGeometryChange();
m_symFont.setPointSizeF(QGIView::calculateFontPointSizeF(this, m_symSize));
m_symFont.setPixelSize(QGIView::calculateFontPixelSize(m_symSize));
m_symbol1->setFont(m_symFont);
m_symbol1->setPlainText(QString::fromUtf8(m_symbol));
if (m_arrowDir.y < 0.0) { //pointing down
Expand Down Expand Up @@ -238,7 +238,7 @@ void QGISectionLine::makeSymbolsISO()
symPosEnd = m_end - offset;

prepareGeometryChange();
m_symFont.setPointSizeF(QGIView::calculateFontPointSizeF(this, m_symSize));
m_symFont.setPixelSize(QGIView::calculateFontPixelSize(m_symSize));
m_symbol1->setFont(m_symFont);
m_symbol1->setPlainText(QString::fromUtf8(m_symbol));
m_symbol1->centerAt(symPosStart);
Expand Down
33 changes: 7 additions & 26 deletions src/Mod/TechDraw/Gui/QGIView.cpp
Expand Up @@ -96,7 +96,7 @@ QGIView::QGIView()
m_pen.setColor(m_colCurrent);

//Border/Label styling
m_font.setPointSizeF(calculateFontPointSizeF(getPrefFontSize()));
m_font.setPixelSize(calculateFontPixelSize(getPrefFontSize()));

m_decorPen.setStyle(Qt::DashLine);
m_decorPen.setWidth(0); // 0 => 1px "cosmetic pen"
Expand Down Expand Up @@ -428,7 +428,7 @@ void QGIView::drawCaption()
QRectF displayArea = customChildrenBoundingRect();
m_caption->setDefaultTextColor(m_colCurrent);
m_font.setFamily(getPrefFont());
m_font.setPointSizeF(calculateFontPointSizeF(getPrefFontSize()));
m_font.setPixelSize(calculateFontPixelSize(getPrefFontSize()));
m_caption->setFont(m_font);
QString captionStr = QString::fromUtf8(getViewObject()->Caption.getValue());
m_caption->setPlainText(captionStr);
Expand Down Expand Up @@ -469,7 +469,7 @@ void QGIView::drawBorder()

m_label->setDefaultTextColor(m_colCurrent);
m_font.setFamily(getPrefFont());
m_font.setPointSizeF(calculateFontPointSizeF(getPrefFontSize()));
m_font.setPixelSize(calculateFontPixelSize(getPrefFontSize()));

m_label->setFont(m_font);
QString labelStr = QString::fromUtf8(getViewObject()->Label.getValue());
Expand Down Expand Up @@ -686,30 +686,11 @@ double QGIView::getDimFontSize()
return hGrp->GetFloat("FontSize", DefaultFontSizeInMM);
}

double QGIView::calculateFontPointSizeF(const QGraphicsItem *item, double sizeInMillimetres)
int QGIView::calculateFontPixelSize(double sizeInMillimetres)
{
const QWidget *widget = MDIViewPage::getFromScene(item->scene());
if (widget == nullptr && !QApplication::topLevelWidgets().isEmpty()) {
// Fallback to some top level window if we are not assigned to a scene/widget yet
widget = QApplication::topLevelWidgets().first();
}

double logicalDPI = 96.0; // This is the most common value in PC world
if (widget != nullptr) {
logicalDPI = widget->logicalDpiY();
}

// Now calculate the correct font size to be used by a QGraphicsTextItem:
// 1) Start with font size in mm.
// 2) Convert it to "pixels" via Rex::GuiX().
// 3) Convert "pixels" to inches dividing them by DPI.
// 4) Convert inches to points - there are exactly 72 points to one inch.
return 72.0*Rez::guiX(sizeInMillimetres)/logicalDPI;
}

double QGIView::calculateFontPointSizeF(double sizeInMillimetres) const
{
return calculateFontPointSizeF(this, sizeInMillimetres);
// Calculate font size in pixels by using resolution conversion
// and round to nearest integer
return (int) (Rez::guiX(sizeInMillimetres) + 0.5);
}

const double QGIView::DefaultFontSizeInMM = 5.0;
Expand Down
3 changes: 1 addition & 2 deletions src/Mod/TechDraw/Gui/QGIView.h
Expand Up @@ -111,7 +111,7 @@ class TechDrawGuiExport QGIView : public QObject, public QGraphicsItemGroup

static Gui::ViewProvider* getViewProvider(App::DocumentObject* obj);
static QGVPage* getGraphicsView(TechDraw::DrawView* dv);
static double calculateFontPointSizeF(const QGraphicsItem *graphicsItem, double sizeInMillimetres);
static int calculateFontPixelSize(double sizeInMillimetres);
static const double DefaultFontSizeInMM;

MDIViewPage* getMDIViewPage(void) const;
Expand All @@ -138,7 +138,6 @@ public Q_SLOTS:
QString getPrefFont(void);
double getPrefFontSize(void);
double getDimFontSize(void);
double calculateFontPointSizeF(double sizeInMillimetres) const;

Base::Reference<ParameterGrp> getParmGroupCol(void);

Expand Down
4 changes: 2 additions & 2 deletions src/Mod/TechDraw/Gui/QGIViewBalloon.cpp
Expand Up @@ -181,7 +181,7 @@ void QGIViewBalloon::placeBalloon(QPointF pos)
balloon->Text.setValue(std::to_string(idx).c_str());

QFont font = balloonLabel->getFont();
font.setPointSizeF(calculateFontPointSizeF(vp->Fontsize.getValue()));
font.setPixelSize(calculateFontPixelSize(vp->Fontsize.getValue()));
font.setFamily(QString::fromUtf8(vp->Font.getValue()));
balloonLabel->setFont(font);
prepareGeometryChange();
Expand Down Expand Up @@ -263,7 +263,7 @@ void QGIViewBalloon::updateBalloon(bool obtuse)
}

QFont font = balloonLabel->getFont();
font.setPointSizeF(calculateFontPointSizeF(vp->Fontsize.getValue()));
font.setPixelSize(calculateFontPixelSize(vp->Fontsize.getValue()));
font.setFamily(QString::fromUtf8(vp->Font.getValue()));
balloonLabel->setFont(font);

Expand Down
6 changes: 3 additions & 3 deletions src/Mod/TechDraw/Gui/QGIViewDimension.cpp
Expand Up @@ -208,9 +208,9 @@ void QGIDatumLabel::setFont(QFont f)
{
m_dimText->setFont(f);
QFont tFont(f);
double fontSize = f.pointSizeF();
double fontSize = f.pixelSize();
double tolAdj = getTolAdjust();
tFont.setPointSizeF(fontSize * tolAdj);
tFont.setPixelSize((int) (fontSize * tolAdj));
m_tolText->setFont(tFont);
}

Expand Down Expand Up @@ -481,7 +481,7 @@ void QGIViewDimension::updateDim(bool obtuse)

QFont font = datumLabel->getFont();
font.setFamily(QString::fromUtf8(vp->Font.getValue()));
font.setPointSizeF(calculateFontPointSizeF(this, vp->Fontsize.getValue()));
font.setPixelSize(calculateFontPixelSize(vp->Fontsize.getValue()));
datumLabel->setFont(font);

prepareGeometryChange();
Expand Down

0 comments on commit 28b2df0

Please sign in to comment.