]> mj.ucw.cz Git - paperjam.git/blobdiff - pdf-tools.cc
Top and bottom margins were swapped
[paperjam.git] / pdf-tools.cc
index 4c10313d371f5f12f4d887de2f1c4b09027785c0..6a0b9b6e01db0d82a594f3e51cae8a51a45ca212 100644 (file)
@@ -99,6 +99,38 @@ void BBox::join(BBox &with)
        y_max = max(y_max, with.y_max);
 }
 
+static double clamp(double x, double min, double max)
+{
+       if (x < min)
+               return min;
+       if (x > max)
+               return max;
+       return x;
+}
+
+void BBox::intersect(BBox &with)
+{
+       x_min = clamp(x_min, with.x_min, with.x_max);
+       x_max = clamp(x_max, with.x_min, with.x_max);
+       y_min = clamp(y_min, with.y_min, with.y_max);
+       y_max = clamp(y_max, with.y_min, 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
@@ -205,7 +237,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;
 }