Skip to content

Commit

Permalink
TechDraw: Fix font size issues when exporting to SVG
Browse files Browse the repository at this point in the history
  • Loading branch information
pavltom authored and WandererFan committed May 28, 2019
1 parent 5a4373a commit fad4db3
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 79 deletions.
2 changes: 1 addition & 1 deletion src/Mod/TechDraw/Gui/DlgPrefsTechDraw.ui
Expand Up @@ -707,7 +707,7 @@
<string>View Label size in units</string>
</property>
<property name="value">
<double>6.000000000000000</double>
<double>8.000000000000000</double>
</property>
<property name="prefEntry" stdset="0">
<cstring>LabelSize</cstring>
Expand Down
2 changes: 1 addition & 1 deletion src/Mod/TechDraw/Gui/DlgPrefsTechDraw2.ui
Expand Up @@ -259,7 +259,7 @@
<string>Dimension font size in units</string>
</property>
<property name="value">
<double>3.500000000000000</double>
<double>5.000000000000000</double>
</property>
<property name="prefEntry" stdset="0">
<cstring>FontSize</cstring>
Expand Down
9 changes: 9 additions & 0 deletions src/Mod/TechDraw/Gui/MDIViewPage.cpp
Expand Up @@ -1374,4 +1374,13 @@ void MDIViewPage::showStatusMsg(const char* s1, const char* s2, const char* s3)
}
}

MDIViewPage *MDIViewPage::getFromScene(const QGraphicsScene *scene)
{
if (scene != nullptr && scene->parent() != nullptr) {
return dynamic_cast<MDIViewPage *>(scene->parent());
}

return nullptr;
}

#include <Mod/TechDraw/Gui/moc_MDIViewPage.cpp>
4 changes: 3 additions & 1 deletion src/Mod/TechDraw/Gui/MDIViewPage.h
Expand Up @@ -71,7 +71,7 @@ class TechDrawGuiExport MDIViewPage : public Gui::MDIView, public Gui::Selection
void matchSceneRectToTemplate(void);

bool onMsg(const char* pMsg,const char** ppReturn);
bool onHasMsg(const char* pMsg) const;
bool onHasMsg(const char* pMsg) const;

void print();
void print(QPrinter* printer);
Expand Down Expand Up @@ -101,6 +101,8 @@ class TechDrawGuiExport MDIViewPage : public Gui::MDIView, public Gui::Selection

bool addView(const App::DocumentObject *obj);

static MDIViewPage *getFromScene(const QGraphicsScene *scene);

public Q_SLOTS:
void viewAll();
void saveSVG(void);
Expand Down
18 changes: 0 additions & 18 deletions src/Mod/TechDraw/Gui/QGCustomLabel.cpp
Expand Up @@ -77,23 +77,5 @@ void QGCustomLabel::paint ( QPainter * painter, const QStyleOptionGraphicsItem *
QStyleOptionGraphicsItem myOption(*option);
myOption.state &= ~QStyle::State_Selected;

//see QGCustomText for explanation of this code
double dppt = 3.53;
double svgMagicX = Rez::guiX(8.0);
double svgMagicY = Rez::guiX(12.0);
double svgMagicYoffset = Rez::guiX(3.0);
double fontSize = Rez::appX(font().pointSizeF());
double ty = svgMagicY + (svgMagicYoffset*fontSize)/dppt;
QPointF svgMove(-svgMagicX/dppt,ty);

QPaintDevice* hw = painter->device();
QSvgGenerator* svg = dynamic_cast<QSvgGenerator*>(hw);
if (svg) {
painter->scale(Rez::appX(dppt),Rez::appX(dppt));
painter->translate(svgMove);
} else {
painter->scale(1.0,1.0);
}

QGraphicsTextItem::paint (painter, &myOption, widget);
}
26 changes: 0 additions & 26 deletions src/Mod/TechDraw/Gui/QGCustomText.cpp
Expand Up @@ -131,32 +131,6 @@ void QGCustomText::paint ( QPainter * painter, const QStyleOptionGraphicsItem *
QStyleOptionGraphicsItem myOption(*option);
myOption.state &= ~QStyle::State_Selected;

//svg text is much larger than screen text. scene units(mm or 0.1mm in hirez) vs points.
//need to scale text if going to svg.
//TODO: magic translation happens. why? approx: right ~8mm down: 12mm + (3mm per mm of text height)
//SVG transform matrix translation values are different for same font size + different fonts (Sans vs Ubuntu vs Arial)???
// scale values are same for same font size + different fonts.
//calculate dots/mm
//in hirez - say factor = 10, we have 10 dpmm in scene space.
// so 254dpi / 72pts/in => 3.53 dppt
double dpmm = 3.53; //dots/pt
double svgMagicX = Rez::guiX(8.0); //8mm -> 80 gui dots
double svgMagicY = Rez::guiX(12.0);
double svgMagicYoffset = Rez::guiX(3.0); // 3mm per mm of font size => 30gunits / mm of font size
double fontSize = Rez::appX(font().pointSizeF()); //gui pts 4mm text * 10 scunits/mm = size 40 text but still only 4mm
double ty = svgMagicY + (svgMagicYoffset*fontSize)/dpmm;
// 12mm (in gunits) + [3mm (in gunits) * (# of mm)]/ [dots per mm] works out to dots?
QPointF svgMove(-svgMagicX/dpmm,ty);

QPaintDevice* hw = painter->device();
QSvgGenerator* svg = dynamic_cast<QSvgGenerator*>(hw);
if (svg) {
painter->scale(Rez::appX(dpmm),Rez::appX(dpmm));
painter->translate(svgMove);
} else {
painter->scale(1.0,1.0);
}

// painter->drawRect(boundingRect()); //good for debugging

setDefaultTextColor(m_colCurrent);
Expand Down
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.setPointSize(m_refSize);
m_refFont.setPointSizeF(QGIView::calculateFontPointSizeF(this, m_refSize));
m_reference->setFont(m_refFont);
m_reference->setPlainText(QString::fromUtf8(m_refText));
double fudge = Rez::guiX(1.0);
Expand Down
4 changes: 2 additions & 2 deletions src/Mod/TechDraw/Gui/QGISectionLine.cpp
Expand Up @@ -203,7 +203,7 @@ void QGISectionLine::makeSymbolsTrad()
extLineStart = m_start + offset;
extLineEnd = m_end + offset;
prepareGeometryChange();
m_symFont.setPointSize(m_symSize);
m_symFont.setPointSizeF(QGIView::calculateFontPointSizeF(this, 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 @@ -246,7 +246,7 @@ void QGISectionLine::makeSymbolsISO()
symPosEnd = m_end - offset;

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

//Border/Label styling
m_font.setPointSize(getPrefFontSize()); //scene units (mm), not points
m_font.setPointSizeF(calculateFontPointSizeF(getPrefFontSize()));

m_decorPen.setStyle(Qt::DashLine);
m_decorPen.setWidth(0); // 0 => 1px "cosmetic pen"
Expand Down Expand Up @@ -430,7 +430,7 @@ void QGIView::drawCaption()
QRectF displayArea = customChildrenBoundingRect();
m_caption->setDefaultTextColor(m_colCurrent);
m_font.setFamily(getPrefFont());
m_font.setPointSize(getPrefFontSize()); //scene units (0.1 mm), not points
m_font.setPointSizeF(calculateFontPointSizeF(getPrefFontSize()));
m_caption->setFont(m_font);
QString captionStr = QString::fromUtf8(getViewObject()->Caption.getValue());
m_caption->setPlainText(captionStr);
Expand Down Expand Up @@ -473,7 +473,8 @@ void QGIView::drawBorder()

m_label->setDefaultTextColor(m_colCurrent);
m_font.setFamily(getPrefFont());
m_font.setPointSize(getPrefFontSize()); //scene units (0.1 mm), not points
m_font.setPointSizeF(calculateFontPointSizeF(getPrefFontSize()));

m_label->setFont(m_font);
QString labelStr = QString::fromUtf8(getViewObject()->Label.getValue());
m_label->setPlainText(labelStr);
Expand Down Expand Up @@ -608,21 +609,10 @@ QGVPage* QGIView::getGraphicsView(TechDraw::DrawView* dv)
}
return graphicsView;
}

MDIViewPage* QGIView::getMDIViewPage(void) const
{
MDIViewPage* result = nullptr;
QGraphicsScene* s = scene();
QObject* parent = nullptr;
if (s != nullptr) {
parent = s->parent();
}
if (parent != nullptr) {
MDIViewPage* mdi = dynamic_cast<MDIViewPage*>(parent);
if (mdi != nullptr) {
result = mdi;
}
}
return result;
return MDIViewPage::getFromScene(scene());
}

bool QGIView::getFrameState(void)
Expand Down Expand Up @@ -690,10 +680,37 @@ double QGIView::getPrefFontSize()
{
Base::Reference<ParameterGrp> hGrp = App::GetApplication().GetUserParameter().
GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Mod/TechDraw/Labels");
double fontSize = hGrp->GetFloat("LabelSize", 3.5);
return Rez::guiX(fontSize);
return hGrp->GetFloat("LabelSize", DefaultFontSizeInMM);
}

double QGIView::calculateFontPointSizeF(const QGraphicsItem *item, 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);
}

const double QGIView::DefaultFontSizeInMM = 5.0;

void QGIView::dumpRect(char* text, QRectF r) {
Base::Console().Message("DUMP - %s - rect: (%.3f,%.3f) x (%.3f,%.3f)\n",text,
r.left(),r.top(),r.right(),r.bottom());
Expand Down
6 changes: 6 additions & 0 deletions src/Mod/TechDraw/Gui/QGIView.h
Expand Up @@ -111,7 +111,11 @@ 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 const double DefaultFontSizeInMM;

MDIViewPage* getMDIViewPage(void) const;

// Mouse handling
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
boost::signals2::signal<void (QGIView*, QPointF)> signalSelectPoint;
Expand All @@ -133,6 +137,8 @@ public Q_SLOTS:

QString getPrefFont(void);
double getPrefFontSize(void);
double calculateFontPointSizeF(double sizeInMillimetres) const;

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

TechDraw::DrawView *viewObj;
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(Rez::guiX(vp->Fontsize.getValue()));
font.setPointSizeF(calculateFontPointSizeF(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(Rez::guiX(vp->Fontsize.getValue()));
font.setPointSizeF(calculateFontPointSizeF(vp->Fontsize.getValue()));
font.setFamily(QString::fromUtf8(vp->Font.getValue()));
balloonLabel->setFont(font);

Expand Down
8 changes: 2 additions & 6 deletions src/Mod/TechDraw/Gui/QGIViewDimension.cpp
Expand Up @@ -402,10 +402,6 @@ void QGIViewDimension::updateView(bool update)
}
else if(vp->Fontsize.isTouched() ||
vp->Font.isTouched()) {
QFont font = datumLabel->getFont();
font.setPointSizeF(Rez::guiX(vp->Fontsize.getValue()));
font.setFamily(QString::fromLatin1(vp->Font.getValue()));
datumLabel->setFont(font);
updateDim();
} else if (vp->LineWidth.isTouched()) { //never happens!!
m_lineWidth = vp->LineWidth.getValue();
Expand All @@ -432,10 +428,10 @@ void QGIViewDimension::updateDim(bool obtuse)
QString labelText = QString::fromUtf8(dim->getFormatedValue(m_obtuse).c_str());

QFont font = datumLabel->getFont();
font.setPointSizeF(Rez::guiX(vp->Fontsize.getValue()));
font.setFamily(QString::fromUtf8(vp->Font.getValue()));

font.setPointSizeF(calculateFontPointSizeF(this, vp->Fontsize.getValue()));
datumLabel->setFont(font);

prepareGeometryChange();
datumLabel->setDimString(labelText);
datumLabel->setTolString();
Expand Down
5 changes: 4 additions & 1 deletion src/Mod/TechDraw/Gui/QGVPage.cpp
Expand Up @@ -96,6 +96,7 @@
#include "ZVALUE.h"
#include "ViewProviderPage.h"
#include "QGVPage.h"
#include "MDIViewPage.h"

using namespace Gui;
using namespace TechDraw;
Expand Down Expand Up @@ -746,7 +747,9 @@ void QGVPage::saveSvg(QString filename)
// the width and height attributes of the <svg> element." >> but Inkscape won't read it without size info??
svgGen.setViewBox(QRect(0, 0, Rez::guiX(page->getPageWidth()), Rez::guiX(page->getPageHeight())));

svgGen.setResolution(Rez::guiX(25.4)); // docs say this is DPI. Rez::guiX(1dot/mm) so 254 dpi?
// Set resolution in DPI. To keep text dimensions as they are on screen,
// use the very same resolution the screen paint device reports.
svgGen.setResolution(MDIViewPage::getFromScene(scene())->logicalDpiY());

svgGen.setTitle(QObject::tr("FreeCAD SVG Export"));
svgGen.setDescription(svgDescription);
Expand Down
2 changes: 1 addition & 1 deletion src/Mod/TechDraw/Gui/ViewProviderBalloon.cpp
Expand Up @@ -70,7 +70,7 @@ ViewProviderBalloon::ViewProviderBalloon()

hGrp = App::GetApplication().GetUserParameter()
.GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Mod/TechDraw/Dimensions");
double fontSize = hGrp->GetFloat("FontSize", 3.5);
double fontSize = hGrp->GetFloat("FontSize", QGIView::DefaultFontSizeInMM);
ADD_PROPERTY_TYPE(Font ,(fontName.c_str()),group,App::Prop_None, "The name of the font to use");
ADD_PROPERTY_TYPE(Fontsize,(fontSize) ,group,(App::PropertyType)(App::Prop_None),"Dimension text size in units");

Expand Down
2 changes: 1 addition & 1 deletion src/Mod/TechDraw/Gui/ViewProviderDimension.cpp
Expand Up @@ -60,7 +60,7 @@ ViewProviderDimension::ViewProviderDimension()

hGrp = App::GetApplication().GetUserParameter()
.GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Mod/TechDraw/Dimensions");
double fontSize = hGrp->GetFloat("FontSize", 3.5);
double fontSize = hGrp->GetFloat("FontSize", QGIView::DefaultFontSizeInMM);
ADD_PROPERTY_TYPE(Font ,(fontName.c_str()),group,App::Prop_None, "The name of the font to use");
ADD_PROPERTY_TYPE(Fontsize,(fontSize) ,group,(App::PropertyType)(App::Prop_None),"Dimension text size in units");

Expand Down

0 comments on commit fad4db3

Please sign in to comment.