]> mj.ucw.cz Git - paperjam.git/blobdiff - pdf-tools.cc
Better formatting of PDF numbers
[paperjam.git] / pdf-tools.cc
index 2077969cdac6d5ab10006dda5b1b8b28db55a589..68a8f8cae4201373f2de193619a7a338badafd80 100644 (file)
@@ -22,7 +22,7 @@ string pdf_matrix::to_string() {
        for (int i=0; i<6; i++) {
                if (i)
                        s += " ";
-               s += pdf_coord(m[i], 0);
+               s += pdf_coord(m[i], 6);
        }
        return s;
 }
@@ -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);
@@ -198,7 +205,12 @@ QPDFObjectHandle page_to_xobject(QPDF *out, QPDFObjectHandle page)
 
 string pdf_coord(double x, uint digits)
 {
-  char buf[16];
-  snprintf(buf, sizeof(buf), "%.*f", digits, x);
-  return buf;
+       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;
 }