Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Paper Sizes #10

Open
bradparker opened this issue Nov 20, 2014 · 8 comments
Open

Custom Paper Sizes #10

bradparker opened this issue Nov 20, 2014 · 8 comments

Comments

@bradparker
Copy link

Being able to feed in GTK paper size names is awesome, thank you. However we wondered what might involved in adding something even more flexible.

We'd love to contribute more directly but we don't speak c/c++, sorry. We're also unfamiliar with v8 / WebKitGTK. We figured we'd talk through how we thought this might work and try to help as much as we can.

I'm not sure how this would work on the c++ side but having that paper param be polymorphic would be pretty cool, perhaps...

Webkit(99).load('google.com').wait('idle').pdf({
  paper: { 
    width: 210, 
    height: 297, 
    unit: 'mm' 
  }
});

// Or

Webkit(99).load('google.com').wait('idle').pdf({
  paper: 'iso_a4'
});

... and here's some pseudo code which may / may not be of any help.

GtkPageSetup* setup = gtk_page_setup_new();
  Local<Object> paper = opts.paper->ToObject();
  if (paper.width == NULL) { 
    paper = gtk_paper_size_get_default();
    GtkPaperSize* paperSize = gtk_paper_size_new(paper);
  } else {
    const gchar custom_paper_name = 'some_placeholder_name';
    const gchar custom_paper_display_name = 'again_some_placeholder_name';
    const gdouble width = NanUInt32OptionValue(paper, H("width"), 210);
    const gdouble height = NanUInt32OptionValue(paper, H("height"), 297);
    const gchar unitStr = getStr(paper, "unit"); // assign the correct GTK unit based on this string...
    const GtkUnit unit = //  ... here? GTK_UNIT_POINTS || GTK_UNIT_MM || GTK_UNIT_INCH
    GtkPaperSize* paperSize = gtk_paper_size_new_custom(
      custom_paper_name,
      custom_paper_display_name,
      width,
      height,
      unit
    );   
  }

  gtk_page_setup_set_paper_size_and_default_margins(setup, paperSize);
  if (NanBooleanOptionValue(opts, H("fullpage"), false)) {
    gtk_page_setup_set_right_margin(setup, 0, GTK_UNIT_POINTS);
    gtk_page_setup_set_left_margin(setup, 0, GTK_UNIT_POINTS);
    gtk_page_setup_set_top_margin(setup, 0, GTK_UNIT_POINTS);
    gtk_page_setup_set_bottom_margin(setup, 0, GTK_UNIT_POINTS);
  }
  webkit_print_operation_set_page_setup(op, setup);

I really hope that's helpful. Thanks.

@kapouer
Copy link
Owner

kapouer commented Nov 20, 2014

Also it's probably good to allow setting margins in GtkPageSetup.
This will have to wait a little longer though.

@bradparker
Copy link
Author

Yeah for sure! Another good polymorphic candidate?

Webkit().load('google.com').wait('idle').pdf({
  margins: 0
});

// Or

Webkit().load('google.com').wait('idle').pdf({
  margins: {
    left: 20,
    right: 20,
    top: 15,
    bottom: 25,
    unit: 'mm'
  }
});

Removes the need for full page?

@bradparker
Copy link
Author

Would it be easier to translate the opts object on the JS end?

WebKit.prototype.pdf = function() {
  var filepath = arguments[0];
  var opts, cb;

  if (arguments[1] instanceof Function) {
    opts = {};
    cb = arguments[1];
  } else {
    opts = arguments[1] || {};
    cb = arguments[2] || noop;
  }

  if (typeof opts.paper === 'string') {
    opts.gtkPaperName = opts.paper;
  } else if (opts.paper) {
    opts.customPaperWidth   = opts.paper.width;
    opts.customPaperHeight  = opts.paper.height;
    opts.customPaperUnit    = opts.paper.unit || 'mm';
  }
  delete opts.paper;

  if (typeof opts.margins === 'number') {
    opts.marginLeft = opts.marginRight = opts.marginTop = opts.marginBottom = opts.margins;
  } else if (opts.margins) {
    opts.marginLeft   = opts.margins.left || 0;
    opts.marginRight  = opts.margins.right || 0;
    opts.marginTop    = opts.margins.top || 0;
    opts.marginBottom = opts.margins.bottom || 0;
    opts.marginUnit   = opts.margins.unit;
  }
  opts.marginUnit = opts.marginUnit || 'mm';
  delete opts.margins;

  pdf.call(this, filepath, opts, cb);
};

... roughly. This might make the job on the extension side easier.

Or would you prefer no translation between the opts in the JS / c++ APIs?

@bradparker
Copy link
Author

Having a look at this. Nearly there, going to add some tests, and seek out c++ advice :)
net-engine/node-webkitgtk@kapouer:master...master

@kapouer
Copy link
Owner

kapouer commented Dec 1, 2014

Great !
But

  • talk about "vm in osx" in the wiki, not in the readme
  • do not include npm-debug.log in git
  • +!test/shots/.gitkeep this should go into your userdir .gitignore, not into this project
  • i don't understand why you had to change lib -> obj in binding.gyp - a change in latest node-gyp ?
  • and about the code itself : good start - you have to make sure you destroy what you create, like gtk_paper_size_new_custom maybe.
  • return gtk_paper_size_new(gtk_paper_size_get_default()); isn't reached ?

@kapouer
Copy link
Owner

kapouer commented Dec 1, 2014

Also note that you can also leave it like this and i can take inspiration from your code instead of merging your commits if you don't want to bother with the details :)

@bradparker
Copy link
Author

No problem! Happy to help as much as I can.

Will add tests for variations of that paper object, including not having one (to see if that default ever gets executed).

I've been looking around for info on lib > obj... no idea, I can't find any relevant information unfortunately. My node-gyp version is 0.12.2.

The cache/.gitkeep and test/shots/.gitkeepss were intended to make setup easier for people running the tests the first time. Tests fail without the cache and test/shots folders. Happy to remove them if you think it's better as user configuration.

Will remove the OSX notes and set up an Installation guide in the wiki.

Oh, and will definitely make sure to keep npm-debug.log out, whoops.

Thanks.

@kapouer kapouer closed this as completed in 5c3bac2 Dec 6, 2014
@kapouer
Copy link
Owner

kapouer commented Sep 28, 2015

@bradparker if you're still printing pdf out of webkitgtk, maybe you'd have something to say about https://lists.webkit.org/pipermail/webkit-gtk/2015-September/002435.html

@kapouer kapouer reopened this Nov 19, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants