From 76cc21b8950d57b6430ed120bbcda539f8666c2e Mon Sep 17 00:00:00 2001 From: hayabousa Date: Thu, 25 Jul 2024 15:09:30 +0200 Subject: [PATCH] fix: read page size from svg xml element --- CMakeLists.txt | 3 ++- MainWindow.cpp | 37 ++++++++++++++++++++++++++++--------- MainWindow.h | 2 ++ 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index daf0d34a..c13e078c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ project(Robocut LANGUAGES C CXX) # Set the CMAKE_PREFIX_PATH environment variable to (e.g) ~/Qt/5.8/clang_64 so it can find Qt. -find_package(Qt5 COMPONENTS Core Widgets Svg REQUIRED) +find_package(Qt5 COMPONENTS Core Widgets Svg Xml REQUIRED) set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED TRUE) @@ -70,6 +70,7 @@ add_executable(Robocut MACOSX_BUNDLE WIN32 target_link_libraries(Robocut Qt5::Widgets Qt5::Svg + Qt5::Xml libusb ) diff --git a/MainWindow.cpp b/MainWindow.cpp index 4cd3f3c0..a2252c45 100644 --- a/MainWindow.cpp +++ b/MainWindow.cpp @@ -47,6 +47,8 @@ #include #include +#include + using namespace std; const double InitialZoom = 2.0; @@ -123,6 +125,29 @@ void MainWindow::on_actionIdentify_triggered() cout << " " << id->msg << endl; } +constexpr double defaultPpm = 96.0/25.4; // Pixels per mm. + +inline float stringSizeToMm(const QString& repr, double ppm=defaultPpm) +{ + if (repr.endsWith("mm")) { + return repr.left(repr.size() - 2).toFloat(); + } + return repr.toFloat() / defaultPpm; +} + +void MainWindow::readPageSize() +{ + QDomDocument xmlDocument; + QFile file(filename); + + xmlDocument.setContent(&file); + + QDomElement svgNode = xmlDocument.elementsByTagName("svg").item(0).toElement(); + const QString widthXmlValue = svgNode.attribute("width"); + const QString heightXmlValue = svgNode.attribute("height"); + + mediaSize = {stringSizeToMm(widthXmlValue), stringSizeToMm(heightXmlValue)}; +} void MainWindow::loadFile() { @@ -139,19 +164,13 @@ void MainWindow::loadFile() return; } + readPageSize(); + qDebug() << "SVG default size: " << rend.defaultSize() << endl; qDebug() << "SVG view box: " << rend.viewBoxF() << endl; - - // Geqt size from SVG. TODO: Sanity check. - // Also TODO: This won't handle offset viewboxes... need to get the offset and subtract it from - // all the objects. - mediaSize = rend.viewBoxF().size() * 25.4 / 96.0; - - double ppm = 96.0/25.4; // Pixels per mm. - qDebug() << "Page size (mm): " << mediaSize << endl; - PathPaintDevice pg(mediaSize.width(), mediaSize.height(), ppm); + PathPaintDevice pg(mediaSize.width(), mediaSize.height(), defaultPpm); QPainter p(&pg); rend.render(&p); diff --git a/MainWindow.h b/MainWindow.h index 7d319e60..8244fdd0 100644 --- a/MainWindow.h +++ b/MainWindow.h @@ -70,6 +70,8 @@ class MainWindow : public QMainWindow QString filename; + void readPageSize(); + public: int sortFlag; int tspFlag;