From 23cd86a2e3e0b93779df1b0615c67f486ecf1c96 Mon Sep 17 00:00:00 2001 From: Ad5001 Date: Sat, 5 Mar 2022 18:19:20 +0100 Subject: [PATCH] Starting latex rendering --- LogarithmPlotter/logarithmplotter.py | 2 + .../LogarithmPlotter/Setting/ListSetting.qml | 18 ++++++ LogarithmPlotter/util/latex.py | 58 +++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 LogarithmPlotter/util/latex.py diff --git a/LogarithmPlotter/logarithmplotter.py b/LogarithmPlotter/logarithmplotter.py index 53b29e0..6a3cc9d 100644 --- a/LogarithmPlotter/logarithmplotter.py +++ b/LogarithmPlotter/logarithmplotter.py @@ -46,6 +46,7 @@ from LogarithmPlotter import __VERSION__ from LogarithmPlotter.util import config, native from LogarithmPlotter.util.update import check_for_updates from LogarithmPlotter.util.helper import Helper +from LogarithmPlotter.util.latex import Latex config.init() @@ -107,6 +108,7 @@ def run(): engine = QQmlApplicationEngine() global tmpfile helper = Helper(pwd, tmpfile) + latex = Latex(tempdir, app.palette()) engine.rootContext().setContextProperty("Helper", helper) engine.rootContext().setContextProperty("TestBuild", "--test-build" in argv) engine.rootContext().setContextProperty("StartTime", dep_time) diff --git a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/Setting/ListSetting.qml b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/Setting/ListSetting.qml index 718181f..c71fddd 100644 --- a/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/Setting/ListSetting.qml +++ b/LogarithmPlotter/qml/eu/ad5001/LogarithmPlotter/Setting/ListSetting.qml @@ -1,3 +1,21 @@ +/** + * LogarithmPlotter - 2D plotter software to make BODE plots, sequences and distribution functions. + * Copyright (C) 2022 Ad5001 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + import QtQuick 2.12 import QtQuick.Controls 2.12 import QtQml.Models 2.12 diff --git a/LogarithmPlotter/util/latex.py b/LogarithmPlotter/util/latex.py new file mode 100644 index 0000000..943edfc --- /dev/null +++ b/LogarithmPlotter/util/latex.py @@ -0,0 +1,58 @@ +""" + * LogarithmPlotter - 2D plotter software to make BODE plots, sequences and distribution functions. + * Copyright (C) 2022 Ad5001 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +""" + +from PySide2.QtCore import QObject, Slot +from PySide2.QtGui import QImage, QColor +from PySide2.QtWidgets import QApplication + +from os import path +from sympy import preview +from tempfile import TemporaryDirectory + +class Latex(QObject): + def __init__(self, tempdir: str, palette): + QObject.__init__(self) + self.tempdir = tempdir + self.palette = palette + fg = self.palette.windowText().color().convertTo(QColor.Rgb) + + @Slot(str, float, bool, result=str) + def render(self, latexstring, font_size, themeFg = True): + exprpath = path.join(self.tempdir.name, str(hash(latexstring)) + '.png') + print(latexstring, exprpath) + if not path.exists(exprpath): + if themeFg: + fg = self.palette.windowText().color().convertTo(QColor.Rgb) + fg = f'rgb {fg.redF()} {fg.greenF()} {fg.blueF()}' + else: + fg = 'rgb 1.0 1.0 1.0' + preview('$$' + latexstring + '$$', viewer='file', filename=exprpath, dvioptions=[ + "-T", "tight", + "-z", "0", + "--truecolor", + f"-D {font_size * 72.27 / 10}", # See https://linux.die.net/man/1/dvipng#-D for convertion + "-bg", "Transparent", + "-fg", fg], + euler=False) + return exprpath + + @Slot(str) + def copyLatexImageToClipboard(self, latexstring): + global tempfile + clipboard = QApplication.clipboard() + clipboard.setImage(QImage(self.render(latexstring)))