//// charms.js // This file contains snippets of code to make PDF documents created by Prince more beautiful // For a description of the features, see https://princexml.com/howcome/2021/guides/ // This is experimental code for demonstration purposes. // You may freely copy and reuse this code // howcome@yeslogic.com var DEBUG=false; var UNIT=0; // 0=mm 1=pt // present number in preferred unit function uni(val) { var ret; switch (UNIT) { case 0: ret = val/2.83464; return ret.toFixed(2)+"mm"; case 1: return val; } } if (typeof Prince != "undefined") { addBoxInfoMethods(); } function consoleLog() { if (DEBUG) { console.log.apply(null,arguments); } } function debug() { DEBUG=true; consoleLog("Debugging on"); } //// turn on multipass Prince.trackBoxes = true; var funcsarr = []; var argsarr = []; function printObject(o) { var out = ''; for (var p in o) { out += p + ': ' + o[p] + '\n'; } console.log(out); } function postlayout() { consoleLog("postlayout starting, there are",funcsarr.length,"functions in the queue"); if (funcsarr.length > 0) { var func = funcsarr.shift(); var arg = argsarr.shift(); consoleLog(" postlayout:",func.name); func(arg); consoleLog(" returning from ",func.name,"re-registering postlayout"); Prince.registerPostLayoutFunc(postlayout); } } function schedule(callback, args) { funcsarr.push(callback); argsarr.push(args); if (args) { consoleLog(" scheduling function",callback.name,"with arguments:",args); } else { consoleLog(" scheduling function",callback.name); } Prince.registerPostLayoutFunc(postlayout); } //// baseline alignment // snap() provides simple baseline aligment in Prince by sizing elements // so that they are a multiple of the line-height. Also, the funcion can push elements downwards so that // the distance to the top of the page box is a multiple of the line height. // //
// the arguments to the snap function is a the snap size, and a list of selectors/method pairs // the snap size should be equal to the line-height of the body text // selected elements will be adjusted to that their size is divisible by the snap size // selected elements will be increased in size, either through increasing 'padding', 'margin' or 'height' // the selected elements should all have "break-inside: avoid" set function snap(lh) { var snapSize = topt(lh); var selectors = []; var methods = []; // consoleLog("snap",size); for (var i = 1; i < arguments.length; i++) { if (i % 2) { // odd number, i.e. a selector selectors.push(arguments[i]); // consoleLog("Registering selector",arguments[i]); } else { // even number if ((arguments[i]=="margin") || (arguments[i]=="margin-top") || (arguments[i]=="margin-bottom") || (arguments[i]=="padding") || (arguments[i]=="padding-top") || (arguments[i]=="padding-bottom") || (arguments[i]=="padding-even") || (arguments[i]=="height") || (arguments[i]=="growHeight") ||(arguments[i]=="shrinkHeight") || (arguments[i]=="push") || (arguments[i]=="pull") || (arguments[i]=="info")) { methods.push(arguments[i]); // consoleLog(" method:",arguments[i]); } else { // consoleLog(" method",arguments[i],"not recognized, defaulting to padding"); methods.push("padding"); } } } schedule(snap_postlayout,[snapSize,selectors,methods]); } function snap_postlayout(snap_args) { var snapSize = snap_args[0]; var selectors = snap_args[1]; var methods = snap_args[2]; console.log("snap_postlayout",snapSize,selectors,methods); var dbox = document.body.getPrinceBoxes(); var y1 = dbox[0].y; // using first page var y2 = dbox[0].y - dbox[0].h; consoleLog(" first page, page area height",uni(dbox[0].h)); for(var i=0; i\n"); printObject(PDF.pages[10]); console.log("\n"); if (querypage) { if (querypage <= pages.length) { printBoxes(" ",pages[querypage-1]); } } else { for(var i=0; i