View Issue Details

IDProjectCategoryView StatusLast Update
0017302ScribusScripterpublic2024-10-30 20:44
Reporterhugowett Assigned Tocbradney  
PrioritynormalSeverityfeatureReproducibilityN/A
Status resolvedResolutionfixed 
Product Version1.6.2 
Fixed in Version1.6.3.svn 
Summary0017302: Python functions for unit conversion
DescriptionAdds three functions for working with points and the document unit.

* pointsToDocUnit - Returns a value in the measurement units of the document converted from points.
* docUnitToPoints - Returns a value in points converted from the measurement units of the document.
* stringValueToPoints - Returns a value in points converted from a string value (\"5mm\", \"2in\" et.c.).

Adjaecent, but strictly speaking unrelated: changed the comments for
scribus_setunit and scribus_getunit to clarify that they return the unit itself
as opposed to f.ex. the ratio between the document unit and points. Maybe I'm
just not used to the Scribus terminology, so feel very free to ignore that
change.
Additional InformationI sent in another patch that extends the Scripter API https://bugs.scribus.net/view.php?id=17298, but I forgot to actually set Patch to Yes. I don't know if that pushes it to the bottom of the queue so just mentioning it here as a ping :)
TagsNo tags attached.
PatchYes

Activities

hugowett

2024-10-30 14:18

reporter  

doc_units.patch (4,710 bytes)   
diff --git Scribus/scribus/plugins/scriptplugin/cmddoc.cpp Scribus/scribus/plugins/scriptplugin/cmddoc.cpp
index 2af149c1..452b5b0f 100644
--- Scribus/scribus/plugins/scriptplugin/cmddoc.cpp
+++ Scribus/scribus/plugins/scriptplugin/cmddoc.cpp
@@ -350,6 +350,43 @@ PyObject *scribus_getunit(PyObject* /* self */)
 	return PyLong_FromLong(static_cast<long>(ScCore->primaryMainWindow()->doc->unitIndex()));
 }
 
+PyObject *scribus_pointstodocunit(PyObject* /* self */, PyObject *args)
+{
+    double points;
+    if (!PyArg_ParseTuple(args, "d", &points))
+        return nullptr;
+    if (!checkHaveDocument())
+        return nullptr;
+
+    return Py_BuildValue("d", PointToValue(points));
+}
+
+PyObject *scribus_docunittopoints(PyObject* /* self */, PyObject *args)
+{
+    double value;
+    if (!PyArg_ParseTuple(args, "d", &value))
+        return nullptr;
+    if (!checkHaveDocument())
+        return nullptr;
+
+    return Py_BuildValue("d", ValueToPoint(value));
+}
+
+PyObject *scribus_stringvaluetopoints(PyObject* /* self */, PyObject *args)
+{
+    PyESString strValue;
+    if (!PyArg_ParseTuple(args, "es", "utf-8", strValue.ptr()))
+        return nullptr;
+
+    QString qv = QString::fromUtf8(strValue.c_str());
+
+    int uIdx = unitIndexFromString(qv);
+    double value = unitValueFromString(qv);
+    double points = value / unitGetRatioFromIndex(uIdx);
+
+    return Py_BuildValue("d", points);
+}
+
 PyObject *scribus_loadstylesfromfile(PyObject* /* self */, PyObject *args)
 {
 	PyESString fileName;
diff --git Scribus/scribus/plugins/scriptplugin/cmddoc.h Scribus/scribus/plugins/scriptplugin/cmddoc.h
index 2fda991d..8a01a35e 100644
--- Scribus/scribus/plugins/scriptplugin/cmddoc.h
+++ Scribus/scribus/plugins/scriptplugin/cmddoc.h
@@ -268,7 +268,7 @@ defined as constants UNIT_<type>.\n\
 \n\
 May raise ValueError if an invalid unit is passed.\n\
 "));
-/** Changes unit scale. */
+/** Changes the document unit. */
 PyObject *scribus_setunit(PyObject * /*self*/, PyObject* args);
 
 /*! docstring */
@@ -279,9 +279,36 @@ Returns the measurement units of the document. The returned value will be one\n\
 of the UNIT_* constants:\n\
 UNIT_INCHES, UNIT_MILLIMETERS, UNIT_PICAS, UNIT_POINTS.\n\
 "));
-/** Returns actual unit scale. */
+/** Returns actual document unit. */
 PyObject *scribus_getunit(PyObject * /*self*/);
 
+/*! docstring */
+PyDoc_STRVAR(scribus_pointstodocunit__doc__,
+QT_TR_NOOP("pointsToDocUnit(points) -> value\n\
+\n\
+Returns a value in the measurement units of the document converted from points.\n\
+"));
+/** Converts from points to the document unit. */
+PyObject *scribus_pointstodocunit(PyObject * /*self*/, PyObject* args);
+
+/*! docstring */
+PyDoc_STRVAR(scribus_docunittopoints__doc__,
+QT_TR_NOOP("docUnitToPoints(value) -> points\n\
+\n\
+Returns a value in points converted from the measurement units of the document.\n\
+"));
+/** Converts from the document unit to points. */
+PyObject *scribus_docunittopoints(PyObject * /*self*/, PyObject* args);
+
+/*! docstring */
+PyDoc_STRVAR(scribus_stringvaluetopoints__doc__,
+QT_TR_NOOP("stringValueToPoints(\"10mm\") -> points\n\
+\n\
+Returns a value in points converted from a string value (\"5mm\", \"2in\" et.c.).\n\
+"));
+/** Converts a string value ("5mm", "2in" et.c.) to points. */
+PyObject *scribus_stringvaluetopoints(PyObject * /*self*/, PyObject *args);
+
 /*! docstring */
 PyDoc_STRVAR(scribus_loadstylesfromfile__doc__,
 QT_TR_NOOP("loadStylesFromFile(\"filename\")\n\
diff --git Scribus/scribus/plugins/scriptplugin/scriptplugin.cpp Scribus/scribus/plugins/scriptplugin/scriptplugin.cpp
index 0162e3b9..49c00b15 100644
--- Scribus/scribus/plugins/scriptplugin/scriptplugin.cpp
+++ Scribus/scribus/plugins/scriptplugin/scriptplugin.cpp
@@ -432,6 +432,9 @@ PyMethodDef scribus_methods[] = {
 	{ "getTextShade", scribus_gettextshade, METH_VARARGS, tr(scribus_gettextshade__doc__)},
 	{ "getTextVerticalAlignment", scribus_gettextverticalalignment, METH_VARARGS, tr(scribus_gettextverticalalignment__doc__)},
 	{ "getUnit", (PyCFunction) scribus_getunit, METH_NOARGS, tr(scribus_getunit__doc__)},
+	{ "pointsToDocUnit", scribus_pointstodocunit, METH_VARARGS, tr(scribus_pointstodocunit__doc__)},
+	{ "docUnitToPoints", scribus_docunittopoints, METH_VARARGS, tr(scribus_docunittopoints__doc__)},
+	{ "stringValueToPoints", scribus_stringvaluetopoints, METH_VARARGS, tr(scribus_stringvaluetopoints__doc__)},
 	{ "getVGuides", (PyCFunction) scribus_getVguides, METH_NOARGS, tr(scribus_getVguides__doc__)},
 	{ "getXFontNames", (PyCFunction) scribus_xfontnames, METH_NOARGS, tr(scribus_xfontnames__doc__)},
 	{ "gotoPage", scribus_gotopage, METH_VARARGS, tr(scribus_gotopage__doc__)},
doc_units.patch (4,710 bytes)   

cbradney

2024-10-30 20:44

administrator   ~0051505

Thanks, committed to 1.6.3.svn and 1.7.0.svn

Issue History

Date Modified Username Field Change
2024-10-30 14:18 hugowett New Issue
2024-10-30 14:18 hugowett File Added: doc_units.patch
2024-10-30 20:44 cbradney Assigned To => cbradney
2024-10-30 20:44 cbradney Status new => resolved
2024-10-30 20:44 cbradney Resolution open => fixed
2024-10-30 20:44 cbradney Fixed in Version => 1.6.3.svn
2024-10-30 20:44 cbradney Note Added: 0051505