]> mj.ucw.cz Git - paperjam.git/commitdiff
draw_bbox
authorMartin Mares <mj@ucw.cz>
Sun, 1 Apr 2018 21:58:29 +0000 (23:58 +0200)
committerMartin Mares <mj@ucw.cz>
Sun, 1 Apr 2018 21:58:29 +0000 (23:58 +0200)
cmds.cc
paperjam.cc
parse.cc
pdf-tools.cc
pdf-tools.h

diff --git a/cmds.cc b/cmds.cc
index 1906623b61e7ffa0ac811cf008eb1c983a553cde..24fb0a4d4bb038ad277d17f76e72d64ac7f19a66 100644 (file)
--- a/cmds.cc
+++ b/cmds.cc
@@ -337,6 +337,45 @@ static cmd_exec *modulo_ctor(cmd *c)
   return m;
 }
 
+/*** draw_bbox ***/
+
+class draw_bbox_cmd : public cmd_exec {
+public:
+  vector<page *> process(vector<page *> &pages);
+};
+
+class draw_bbox_page : public page {
+  page *orig_page;
+public:
+  void render(out_context *out, pdf_matrix xform);
+  draw_bbox_page(page *p) : page(p) { orig_page = p; }
+};
+
+void draw_bbox_page::render(out_context *out, pdf_matrix xform)
+{
+  orig_page->render(out, xform);
+  out->contents +=
+     "q " +
+     xform.to_string() + " cm " +
+     "0 1 0 RG " +
+     bbox.to_rect() + " re S " +
+     "Q ";
+}
+
+vector<page *> draw_bbox_cmd::process(vector<page *> &pages)
+{
+  vector<page *> out;
+  for (auto p: pages)
+    out.push_back(new draw_bbox_page(p));
+  return out;
+}
+
+static cmd_exec *draw_bbox_ctor(cmd *c UNUSED)
+{
+  draw_bbox_cmd *m = new draw_bbox_cmd;
+  return m;
+}
+
 /*** Command table ***/
 
 const cmd_def cmd_table[] = {
@@ -347,5 +386,6 @@ const cmd_def cmd_table[] = {
   { "select",  no_args,        1,      select_ctor     },
   { "apply",   no_args,        1,      apply_ctor      },
   { "modulo",  modulo_args,    1,      modulo_ctor     },
+  { "draw_bbox",no_args,       0,      draw_bbox_ctor  },
   { NULL,      NULL,           0,      NULL    }
 };
index 22dd9f5e8d47068bd8c169d3f6f9252d297166f2..fa2c6ddaf63d266217998a14fb528025b6c7d79d 100644 (file)
@@ -61,7 +61,7 @@ void in_page::render(out_context *out, pdf_matrix xform)
   m.shift(-media_box.x_min, -media_box.y_min);
   m.concat(xform);
 
-  out->contents += "q " + m.to_string() + " cm " + xobj_res + " Do Q";
+  out->contents += "q " + m.to_string() + " cm " + xobj_res + " Do Q ";
 }
 
 static void debug_pages(vector<page *> &pages)
index 2e38d3ad403204451ea2ace07e6a8d3079c78b57..3719a4e5ccbf1927ccfd6495d2cedbcd88c914d0 100644 (file)
--- a/parse.cc
+++ b/parse.cc
@@ -71,7 +71,8 @@ static token_type get_next_token()
     {
       while (*in_pos >= 'A' && *in_pos <= 'Z' ||
             *in_pos >= 'a' && *in_pos <= 'z' ||
-            *in_pos >= '0' && *in_pos <= '9')
+            *in_pos >= '0' && *in_pos <= '9' ||
+            *in_pos == '_')
        token += *in_pos++;
       return TOK_IDENT;
     }
index 71dde6c9b7744d1d2c0b8176824ba8d519324143..c170d8ae9a3b0fc2dfbddadfb249ca59d9c77ab0 100644 (file)
@@ -94,6 +94,13 @@ QPDFObjectHandle BBox::to_array()
        return a;
 }
 
+string BBox::to_rect()
+{
+       char buf[64];
+       snprintf(buf, sizeof(buf), "%.1f %.1f %.1f %.1f", x_min, y_min, width(), height());
+       return string(buf);
+}
+
 bool BBox::parse(QPDFObjectHandle h)
 {
        if (!h.isArray() || h.getArrayNItems() != 4)
index 5dfd95b923c33aeacaa1b16abffbbef4cf090229..9563ec024ceb132c7a1fb457b83f04c8b61bada6 100644 (file)
@@ -145,6 +145,7 @@ struct BBox {
                }
        }
        QPDFObjectHandle to_array();
+       string to_rect();
        double width() { return x_max - x_min; }
        double height() { return y_max - y_min; }
        void transform(pdf_matrix &m);