]> mj.ucw.cz Git - paperjam.git/blobdiff - pdf-tools.cc
Better error messages from instantiating and running of commands
[paperjam.git] / pdf-tools.cc
index 29e79bff9f835e4ca10eb595883f7cca09eaee6c..809238c3b662e6138f55eecd2f968584c61e89c5 100644 (file)
@@ -22,9 +22,7 @@ string pdf_matrix::to_string() {
        for (int i=0; i<6; i++) {
                if (i)
                        s += " ";
-               char buf[16];
-               snprintf(buf, sizeof(buf), "%.3f", m[i]);
-               s += buf;
+               s += pdf_coord(m[i], 6);
        }
        return s;
 }
@@ -43,9 +41,11 @@ QPDFObjectHandle BBox::to_array()
 
 string BBox::to_rect()
 {
-       char buf[64];
-       snprintf(buf, sizeof(buf), "%.1f %.1f %.1f %.1f", x_min, y_min, width(), height());
-       return string(buf);
+       return
+               pdf_coord(x_min) + " " +
+               pdf_coord(y_min) + " " +
+               pdf_coord(width()) + " " +
+               pdf_coord(height());
 }
 
 bool BBox::parse(QPDFObjectHandle h)
@@ -84,6 +84,13 @@ void BBox::transform(pdf_matrix &m)
                swap(y_min, y_max);
 }
 
+BBox BBox::transformed(pdf_matrix &m)
+{
+       BBox b = *this;
+       b.transform(m);
+       return b;
+}
+
 void BBox::join(BBox &with)
 {
        x_min = min(x_min, with.x_min);
@@ -92,6 +99,21 @@ void BBox::join(BBox &with)
        y_max = max(y_max, with.y_max);
 }
 
+void BBox::enlarge(double by)
+{
+       x_min -= by;
+       x_max += by;
+       y_min -= by;
+       y_max += by;
+}
+
+BBox BBox::enlarged(double by)
+{
+       BBox b = *this;
+       b.enlarge(by);
+       return b;
+}
+
 /*** Unicode strings ***/
 
 // Construct PDF representation of a UTF-8 string
@@ -193,3 +215,17 @@ QPDFObjectHandle page_to_xobject(QPDF *out, QPDFObjectHandle page)
        xo_stream.replaceStreamData(ph, QPDFObjectHandle::newNull(), QPDFObjectHandle::newNull());
        return xo_stream;
 }
+
+/*** Formatting of coordinates ***/
+
+string pdf_coord(double x, uint digits)
+{
+       char buf[16];
+       snprintf(buf, sizeof(buf), "%.*f", digits, x);
+       int n = strlen(buf);
+       while (n > 0 && buf[n-1] == '0')
+               buf[--n] = 0;
+       if (n > 0 && buf[n-1] == '.')
+               buf[--n] = 0;
+       return buf;
+}