From 34a044b969ed41128bca33061bfa1da5fe3b8e15 Mon Sep 17 00:00:00 2001 From: WandererFan Date: Mon, 2 Jan 2017 13:02:48 -0500 Subject: [PATCH] Fix Svg export page & text size in HiRez --- src/Mod/TechDraw/Gui/QGCustomLabel.cpp | 20 +++++++++++++++ src/Mod/TechDraw/Gui/QGCustomLabel.h | 2 +- src/Mod/TechDraw/Gui/QGCustomText.cpp | 30 +++++++++++----------- src/Mod/TechDraw/Gui/QGIViewAnnotation.cpp | 2 ++ src/Mod/TechDraw/Gui/QGVPage.cpp | 23 ++++++++--------- src/Mod/TechDraw/Gui/Rez.cpp | 8 +++++- src/Mod/TechDraw/Gui/Rez.h | 4 ++- 7 files changed, 58 insertions(+), 31 deletions(-) diff --git a/src/Mod/TechDraw/Gui/QGCustomLabel.cpp b/src/Mod/TechDraw/Gui/QGCustomLabel.cpp index ef72c42341..c2b5db434b 100644 --- a/src/Mod/TechDraw/Gui/QGCustomLabel.cpp +++ b/src/Mod/TechDraw/Gui/QGCustomLabel.cpp @@ -40,6 +40,7 @@ #include #include +#include "Rez.h" #include "QGCustomLabel.h" using namespace TechDrawGui; @@ -75,5 +76,24 @@ void QGCustomLabel::centerAt(double cX, double cY) void QGCustomLabel::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget) { 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(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); } diff --git a/src/Mod/TechDraw/Gui/QGCustomLabel.h b/src/Mod/TechDraw/Gui/QGCustomLabel.h index e34d4b2887..75f13e9e6f 100644 --- a/src/Mod/TechDraw/Gui/QGCustomLabel.h +++ b/src/Mod/TechDraw/Gui/QGCustomLabel.h @@ -54,6 +54,6 @@ private: }; -} // namespace MDIViewPageGui +} #endif // DRAWINGGUI_QGCUSTOMLABEL_H diff --git a/src/Mod/TechDraw/Gui/QGCustomText.cpp b/src/Mod/TechDraw/Gui/QGCustomText.cpp index 5bed7c15db..c52aecf3f7 100644 --- a/src/Mod/TechDraw/Gui/QGCustomText.cpp +++ b/src/Mod/TechDraw/Gui/QGCustomText.cpp @@ -40,6 +40,7 @@ #include #include +#include "Rez.h" #include "QGIView.h" #include "QGCustomText.h" @@ -124,28 +125,27 @@ 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) vs points. + //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? approx: right ~8mm down: 12mm + (3mm per mm of text height) + //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. - //double svgScale = 2.835; //72dpi/(25.4mm/in) - //double svgScale = 3.84; //96dpi/(25mm/in) - //double svgScale = 3.6; //90dpi/(25mm/in) more/less CSS standard? - double svgScale = 2.88; //72dpi/(25mm/in) Qt logicalDpiY() is int - double svgMagicX = 8.0; - //double svgMagicY = 7.5; //idk - double fontSize = font().pointSizeF(); - //double ty = (12.0/svgScale + 3.0*fontSize/svgScale) + (svgMagicY/svgScale); - double ty = (12.0/svgScale + 3.0*fontSize/svgScale); - QPointF svgMove(-svgMagicX/svgScale,-ty); + //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(); - //QPaintDeviceMetrics hwm(hw); - //QPrinter* pr = dynamic_cast(hw); //printer does not rescale vs screen? QSvgGenerator* svg = dynamic_cast(hw); if (svg) { - painter->scale(svgScale,svgScale); + painter->scale(Rez::appX(dpmm),Rez::appX(dpmm)); painter->translate(svgMove); } else { painter->scale(1.0,1.0); diff --git a/src/Mod/TechDraw/Gui/QGIViewAnnotation.cpp b/src/Mod/TechDraw/Gui/QGIViewAnnotation.cpp index 1eabc80897..e1b0801230 100644 --- a/src/Mod/TechDraw/Gui/QGIViewAnnotation.cpp +++ b/src/Mod/TechDraw/Gui/QGIViewAnnotation.cpp @@ -116,6 +116,8 @@ void QGIViewAnnotation::draw() } } +//TODO: text is position slightly high (and left??) on page save to SVG file + void QGIViewAnnotation::drawAnnotation() { auto viewAnno( dynamic_cast(getViewObject()) ); diff --git a/src/Mod/TechDraw/Gui/QGVPage.cpp b/src/Mod/TechDraw/Gui/QGVPage.cpp index 4bce2f6700..b5e549c618 100644 --- a/src/Mod/TechDraw/Gui/QGVPage.cpp +++ b/src/Mod/TechDraw/Gui/QGVPage.cpp @@ -506,22 +506,19 @@ void QGVPage::saveSvg(QString filename) docName; //Base::Console().Message("TRACE - saveSVG - page width: %d height: %d\n",width,height); //A4 297x210 + + //with Rez set to 10 we make a dot 10 times/mm => 254dpi? + // 12 => 304.8 dpi? approx printer dpi 300dpi + QSvgGenerator svgGen; svgGen.setFileName(filename); - svgGen.setSize(QSize((int) page->getPageWidth(), (int)page->getPageHeight())); - svgGen.setViewBox(QRect(0, 0, page->getPageWidth(), page->getPageHeight())); - //TODO: Exported Svg file is not quite right. getPageWidth()), (int) Rez::guiX(page->getPageHeight()))); //expects pixels, gets mm + //"By default this property is set to QSize(-1, -1), which indicates that the generator should not output + // the width and height attributes of the 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. 1dot/mm so 25.4dpi -// TODO: Was svgGen.setResolution(25.4000508); // mm/inch?? docs say this is DPI //really "user space units/inch"? - svgGen.setResolution(25); // mm/inch?? docs say this is DPI - - //svgGen.setResolution(600); // resulting page is ~12.5x9mm - //svgGen.setResolution(96); // page is ~78x55mm svgGen.setTitle(QObject::tr("FreeCAD SVG Export")); svgGen.setDescription(svgDescription); diff --git a/src/Mod/TechDraw/Gui/Rez.cpp b/src/Mod/TechDraw/Gui/Rez.cpp index e4a1b59058..6799763633 100644 --- a/src/Mod/TechDraw/Gui/Rez.cpp +++ b/src/Mod/TechDraw/Gui/Rez.cpp @@ -80,6 +80,12 @@ QPointF Rez::guiPt(QPointF p) return result; } +QPointF Rez::appPt(QPointF p) +{ + QPointF result(appX(p.x()),appX(p.y())); + return result; +} + QRectF Rez::guiRect(QRectF r) { QRectF result(guiX(r.left()), @@ -105,7 +111,7 @@ double Rez::getParameter() { Base::Reference hGrp = App::GetApplication().GetUserParameter() .GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("Mod/TechDraw/Rez"); - double rezFactor = hGrp->GetFloat("Resolution", 12.0); + double rezFactor = hGrp->GetFloat("Resolution", 1.0); return rezFactor; } diff --git a/src/Mod/TechDraw/Gui/Rez.h b/src/Mod/TechDraw/Gui/Rez.h index 3409a33954..aabe99f2f7 100644 --- a/src/Mod/TechDraw/Gui/Rez.h +++ b/src/Mod/TechDraw/Gui/Rez.h @@ -43,10 +43,12 @@ public: static double guiX(double x); static Base::Vector2d guiX(Base::Vector2d v); static Base::Vector3d guiX(Base::Vector3d v); - //turn Gui side value to App side value static double appX(double x); + static QPointF guiPt(QPointF p); + static QPointF appPt(QPointF p); + static QRectF guiRect(QRectF r); static QSize guiSize(QSize s); static QSize appSize(QSize s);