From 8fb0e3989a6867b67d5cafb04b622701d383a832 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Tue, 24 Apr 2018 16:28:27 +0200 Subject: Flesh out some more javascript convenience objects. Add some missing convenience data objects: font, highlight, position, etc. Add color.convert and color.equal functions. --- source/pdf/js/util.js | 184 +++++++++++++++++++++++++++++++++++++++--------- source/pdf/js/util.js.h | 184 +++++++++++++++++++++++++++++++++++++++--------- source/pdf/pdf-js.c | 6 +- 3 files changed, 303 insertions(+), 71 deletions(-) (limited to 'source') diff --git a/source/pdf/js/util.js b/source/pdf/js/util.js index 06c332d7..c634f781 100644 --- a/source/pdf/js/util.js +++ b/source/pdf/js/util.js @@ -59,21 +59,6 @@ var MuPDF = { 'HH:MM:ss', 'h:MM:ss tt' ], - - padZeros: function (num, places) { - var s = String(num) - while (s.length < places) - s = '0' + s; - return s; - }, - - convertCase: function (str, cmd) { - switch (cmd) { - case '>': return str.toUpperCase(); - case '<': return str.toLowerCase(); - default: return str; - } - }, }; // display must be kept in sync with an enum in pdf_form.c @@ -85,31 +70,149 @@ var display = { }; var border = { - s: 'Solid', - d: 'Dashed', - b: 'Beveled', - i: 'Inset', - u: 'Underline', + b: 'beveled', + d: 'dashed', + i: 'inset', + s: 'solid', + u: 'underline', }; var color = { transparent: [ 'T' ], black: [ 'G', 0 ], white: [ 'G', 1 ], + gray: [ 'G', 0.5 ], + ltGray: [ 'G', 0.75 ], + dkGray: [ 'G', 0.25 ], red: [ 'RGB', 1, 0, 0 ], green: [ 'RGB', 0, 1, 0 ], blue: [ 'RGB', 0, 0, 1 ], cyan: [ 'CMYK', 1, 0, 0, 0 ], magenta: [ 'CMYK', 0, 1, 0, 0 ], yellow: [ 'CMYK', 0, 0, 1, 0 ], - dkGray: [ 'G', 0.25 ], - gray: [ 'G', 0.5 ], - ltGray: [ 'G', 0.75 ], +}; + +color.convert = function (c, colorspace) { + switch (colorspace) { + case 'G': + if (c[0] === 'RGB') + return [ 'G', c[1] * 0.3 + c[2] * 0.59 + c[3] * 0.11 ]; + if (c[0] === 'CMYK') + return [ 'CMYK', 1 - Math.min(1, c[1] * 0.3 + c[2] * 0.59 + c[3] * 0.11 + c[4])]; + break; + case 'RGB': + if (c[0] === 'G') + return [ 'RGB', c[1], c[1], c[1] ]; + if (c[0] === 'CMYK') + return [ 'RGB', + 1 - Math.min(1, c[1] + c[4]), + 1 - Math.min(1, c[2] + c[4]), + 1 - Math.min(1, c[3] + c[4]) ]; + break; + case 'CMYK': + if (c[0] === 'G') + return [ 'CMYK', 0, 0, 0, 1 - c[1] ]; + if (c[0] === 'RGB') + return [ 'CMYK', 1 - c[1], 1 - c[2], 1 - c[3], 0 ]; + break; + } + return c; +} + +color.equal = function (a, b) { + var i, n + if (a[0] === 'G') + a = color.convert(a, b[0]); + else + b = color.convert(b, a[0]); + if (a[0] !== b[0]) + return false; + switch (a[0]) { + case 'G': n = 1; break; + case 'RGB': n = 3; break; + case 'CMYK': n = 4; break; + default: n = 0; break; + } + for (i = 1; i <= n; ++i) + if (a[i] !== b[i]) + return false; + return true; +} + +var font = { + Cour: 'Courier', + CourB: 'Courier-Bold', + CourBI: 'Courier-BoldOblique', + CourI: 'Courier-Oblique', + Helv: 'Helvetica', + HelvB: 'Helvetica-Bold', + HelvBI: 'Helvetica-BoldOblique', + HelvI: 'Helvetica-Oblique', + Symbol: 'Symbol', + Times: 'Times-Roman', + TimesB: 'Times-Bold', + TimesBI: 'Times-BoldItalic', + TimesI: 'Times-Italic', + ZapfD: 'ZapfDingbats', +}; + +var highlight = { + i: 'invert', + n: 'none', + o: 'outline', + p: 'push', +}; + +var position = { + textOnly: 0, + iconOnly: 1, + iconTextV: 2, + textIconV: 3, + iconTextH: 4, + textIconH: 5, + overlay: 6, +}; + +var scaleHow = { + proportional: 0, + anamorphic: 1, +}; + +var scaleWhen = { + always: 0, + never: 1, + tooBig: 2, + tooSmall: 3, +}; + +var style = { + ch: 'check', + ci: 'circle', + cr: 'cross', + di: 'diamond', + sq: 'square', + st: 'star', +}; + +var zoomtype = { + fitH: 'FitHeight', + fitP: 'FitPage', + fitV: 'FitVisibleWidth', + fitW: 'FitWidth', + none: 'NoVary', + pref: 'Preferred', + refW: 'ReflowWidth', }; var util = {}; util.printd = function (fmt, d) { + function padZeros(num, places) { + var s = String(num) + while (s.length < places) + s = '0' + s; + return s; + } if (!d) return null; var res = ''; var tokens = fmt.match(/(m+|d+|y+|H+|h+|M+|s+|t+|[^mdyHhMst]+)/g); @@ -144,6 +247,13 @@ util.printd = function (fmt, d) { } util.printx = function (fmt, val) { + function convertCase(str, cmd) { + switch (cmd) { + case '>': return str.toUpperCase(); + case '<': return str.toLowerCase(); + default: return str; + } + } var cs = '='; var res = ''; var i, m; @@ -157,14 +267,14 @@ util.printx = function (fmt, val) { case 'X': m = val.match(/\w/); if (m) { - res += MuPDF.convertCase(m[0], cs); + res += convertCase(m[0], cs); val = val.replace(/^\W*\w/, ''); } break; case 'A': m = val.match(/[A-Za-z]/); if (m) { - res += MuPDF.convertCase(m[0], cs); + res += convertCase(m[0], cs); val = val.replace(/^[^A-Za-z]*[A-Za-z]/, ''); } break; @@ -181,7 +291,7 @@ util.printx = function (fmt, val) { break; case '?': if (val) { - res += MuPDF.convertCase(val.charAt(0), cs); + res += convertCase(val.charAt(0), cs); val = val.substring(1); } break; @@ -191,7 +301,7 @@ util.printx = function (fmt, val) { cs = fmt.charAt(i); break; default: - res += MuPDF.convertCase(fmt.charAt(i), cs); + res += convertCase(fmt.charAt(i), cs); break; } } @@ -523,6 +633,14 @@ function AFSpecial_KeystrokeEx(fmt) { var m; var length = fmt ? fmt.length : 0; + function convertCase(str, cmd) { + switch (cmd) { + case '>': return str.toUpperCase(); + case '<': return str.toLowerCase(); + default: return str; + } + } + while (i < length) { switch (fmt.charAt(i)) { case '\\': @@ -540,7 +658,7 @@ function AFSpecial_KeystrokeEx(fmt) { event.rc = false; break; } - res += MuPDF.convertCase(m[0], cs); + res += convertCase(m[0], cs); val = val.substring(1); break; @@ -550,7 +668,7 @@ function AFSpecial_KeystrokeEx(fmt) { event.rc = false; break; } - res += MuPDF.convertCase(m[0], cs); + res += convertCase(m[0], cs); val = val.substring(1); break; @@ -574,7 +692,7 @@ function AFSpecial_KeystrokeEx(fmt) { event.rc = false; break; } - res += MuPDF.convertCase(val.charAt(0), cs); + res += convertCase(val.charAt(0), cs); val = val.substring(1); break; @@ -781,12 +899,10 @@ function AFSimple_Calculate(op, list) { } function AFRange_Validate(lowerCheck, lowerLimit, upperCheck, upperLimit) { - if (upperCheck && event.value > upperLimit) { + if (upperCheck && event.value > upperLimit) event.rc = false; - } - if (lowerCheck && event.value < lowerLimit) { + if (lowerCheck && event.value < lowerLimit) event.rc = false; - } if (!event.rc) { if (lowerCheck && upperCheck) app.alert(util.printf('The entered value ('+event.value+') must be greater than or equal to %s and less than or equal to %s', lowerLimit, upperLimit)); diff --git a/source/pdf/js/util.js.h b/source/pdf/js/util.js.h index 3f9a8884..a4e5124f 100644 --- a/source/pdf/js/util.js.h +++ b/source/pdf/js/util.js.h @@ -59,21 +59,6 @@ "'HH:MM:ss',\n" "'h:MM:ss tt'\n" "],\n" -"\n" -"padZeros: function (num, places) {\n" -"var s = String(num)\n" -"while (s.length < places)\n" -"s = '0' + s;\n" -"return s;\n" -"},\n" -"\n" -"convertCase: function (str, cmd) {\n" -"switch (cmd) {\n" -"case '>': return str.toUpperCase();\n" -"case '<': return str.toLowerCase();\n" -"default: return str;\n" -"}\n" -"},\n" "};\n" "\n" "\n" @@ -85,31 +70,149 @@ "};\n" "\n" "var border = {\n" -"s: 'Solid',\n" -"d: 'Dashed',\n" -"b: 'Beveled',\n" -"i: 'Inset',\n" -"u: 'Underline',\n" +"b: 'beveled',\n" +"d: 'dashed',\n" +"i: 'inset',\n" +"s: 'solid',\n" +"u: 'underline',\n" "};\n" "\n" "var color = {\n" "transparent: [ 'T' ],\n" "black: [ 'G', 0 ],\n" "white: [ 'G', 1 ],\n" +"gray: [ 'G', 0.5 ],\n" +"ltGray: [ 'G', 0.75 ],\n" +"dkGray: [ 'G', 0.25 ],\n" "red: [ 'RGB', 1, 0, 0 ],\n" "green: [ 'RGB', 0, 1, 0 ],\n" "blue: [ 'RGB', 0, 0, 1 ],\n" "cyan: [ 'CMYK', 1, 0, 0, 0 ],\n" "magenta: [ 'CMYK', 0, 1, 0, 0 ],\n" "yellow: [ 'CMYK', 0, 0, 1, 0 ],\n" -"dkGray: [ 'G', 0.25 ],\n" -"gray: [ 'G', 0.5 ],\n" -"ltGray: [ 'G', 0.75 ],\n" +"};\n" +"\n" +"color.convert = function (c, colorspace) {\n" +"switch (colorspace) {\n" +"case 'G':\n" +"if (c[0] === 'RGB')\n" +"return [ 'G', c[1] * 0.3 + c[2] * 0.59 + c[3] * 0.11 ];\n" +"if (c[0] === 'CMYK')\n" +"return [ 'CMYK', 1 - Math.min(1, c[1] * 0.3 + c[2] * 0.59 + c[3] * 0.11 + c[4])];\n" +"break;\n" +"case 'RGB':\n" +"if (c[0] === 'G')\n" +"return [ 'RGB', c[1], c[1], c[1] ];\n" +"if (c[0] === 'CMYK')\n" +"return [ 'RGB',\n" +"1 - Math.min(1, c[1] + c[4]),\n" +"1 - Math.min(1, c[2] + c[4]),\n" +"1 - Math.min(1, c[3] + c[4]) ];\n" +"break;\n" +"case 'CMYK':\n" +"if (c[0] === 'G')\n" +"return [ 'CMYK', 0, 0, 0, 1 - c[1] ];\n" +"if (c[0] === 'RGB')\n" +"return [ 'CMYK', 1 - c[1], 1 - c[2], 1 - c[3], 0 ];\n" +"break;\n" +"}\n" +"return c;\n" +"}\n" +"\n" +"color.equal = function (a, b) {\n" +"var i, n\n" +"if (a[0] === 'G')\n" +"a = color.convert(a, b[0]);\n" +"else\n" +"b = color.convert(b, a[0]);\n" +"if (a[0] !== b[0])\n" +"return false;\n" +"switch (a[0]) {\n" +"case 'G': n = 1; break;\n" +"case 'RGB': n = 3; break;\n" +"case 'CMYK': n = 4; break;\n" +"default: n = 0; break;\n" +"}\n" +"for (i = 1; i <= n; ++i)\n" +"if (a[i] !== b[i])\n" +"return false;\n" +"return true;\n" +"}\n" +"\n" +"var font = {\n" +"Cour: 'Courier',\n" +"CourB: 'Courier-Bold',\n" +"CourBI: 'Courier-BoldOblique',\n" +"CourI: 'Courier-Oblique',\n" +"Helv: 'Helvetica',\n" +"HelvB: 'Helvetica-Bold',\n" +"HelvBI: 'Helvetica-BoldOblique',\n" +"HelvI: 'Helvetica-Oblique',\n" +"Symbol: 'Symbol',\n" +"Times: 'Times-Roman',\n" +"TimesB: 'Times-Bold',\n" +"TimesBI: 'Times-BoldItalic',\n" +"TimesI: 'Times-Italic',\n" +"ZapfD: 'ZapfDingbats',\n" +"};\n" +"\n" +"var highlight = {\n" +"i: 'invert',\n" +"n: 'none',\n" +"o: 'outline',\n" +"p: 'push',\n" +"};\n" +"\n" +"var position = {\n" +"textOnly: 0,\n" +"iconOnly: 1,\n" +"iconTextV: 2,\n" +"textIconV: 3,\n" +"iconTextH: 4,\n" +"textIconH: 5,\n" +"overlay: 6,\n" +"};\n" +"\n" +"var scaleHow = {\n" +"proportional: 0,\n" +"anamorphic: 1,\n" +"};\n" +"\n" +"var scaleWhen = {\n" +"always: 0,\n" +"never: 1,\n" +"tooBig: 2,\n" +"tooSmall: 3,\n" +"};\n" +"\n" +"var style = {\n" +"ch: 'check',\n" +"ci: 'circle',\n" +"cr: 'cross',\n" +"di: 'diamond',\n" +"sq: 'square',\n" +"st: 'star',\n" +"};\n" +"\n" +"var zoomtype = {\n" +"fitH: 'FitHeight',\n" +"fitP: 'FitPage',\n" +"fitV: 'FitVisibleWidth',\n" +"fitW: 'FitWidth',\n" +"none: 'NoVary',\n" +"pref: 'Preferred',\n" +"refW: 'ReflowWidth',\n" "};\n" "\n" "var util = {};\n" "\n" "util.printd = function (fmt, d) {\n" +"function padZeros(num, places) {\n" +"var s = String(num)\n" +"while (s.length < places)\n" +"s = '0' + s;\n" +"return s;\n" +"}\n" "if (!d) return null;\n" "var res = '';\n" "var tokens = fmt.match(/(m+|d+|y+|H+|h+|M+|s+|t+|[^mdyHhMst]+)/g);\n" @@ -144,6 +247,13 @@ "}\n" "\n" "util.printx = function (fmt, val) {\n" +"function convertCase(str, cmd) {\n" +"switch (cmd) {\n" +"case '>': return str.toUpperCase();\n" +"case '<': return str.toLowerCase();\n" +"default: return str;\n" +"}\n" +"}\n" "var cs = '=';\n" "var res = '';\n" "var i, m;\n" @@ -157,14 +267,14 @@ "case 'X':\n" "m = val.match(/\\w/);\n" "if (m) {\n" -"res += MuPDF.convertCase(m[0], cs);\n" +"res += convertCase(m[0], cs);\n" "val = val.replace(/^\\W*\\w/, '');\n" "}\n" "break;\n" "case 'A':\n" "m = val.match(/[A-Za-z]/);\n" "if (m) {\n" -"res += MuPDF.convertCase(m[0], cs);\n" +"res += convertCase(m[0], cs);\n" "val = val.replace(/^[^A-Za-z]*[A-Za-z]/, '');\n" "}\n" "break;\n" @@ -181,7 +291,7 @@ "break;\n" "case '?':\n" "if (val) {\n" -"res += MuPDF.convertCase(val.charAt(0), cs);\n" +"res += convertCase(val.charAt(0), cs);\n" "val = val.substring(1);\n" "}\n" "break;\n" @@ -191,7 +301,7 @@ "cs = fmt.charAt(i);\n" "break;\n" "default:\n" -"res += MuPDF.convertCase(fmt.charAt(i), cs);\n" +"res += convertCase(fmt.charAt(i), cs);\n" "break;\n" "}\n" "}\n" @@ -523,6 +633,14 @@ "var m;\n" "var length = fmt ? fmt.length : 0;\n" "\n" +"function convertCase(str, cmd) {\n" +"switch (cmd) {\n" +"case '>': return str.toUpperCase();\n" +"case '<': return str.toLowerCase();\n" +"default: return str;\n" +"}\n" +"}\n" +"\n" "while (i < length) {\n" "switch (fmt.charAt(i)) {\n" "case '\\\\':\n" @@ -540,7 +658,7 @@ "event.rc = false;\n" "break;\n" "}\n" -"res += MuPDF.convertCase(m[0], cs);\n" +"res += convertCase(m[0], cs);\n" "val = val.substring(1);\n" "break;\n" "\n" @@ -550,7 +668,7 @@ "event.rc = false;\n" "break;\n" "}\n" -"res += MuPDF.convertCase(m[0], cs);\n" +"res += convertCase(m[0], cs);\n" "val = val.substring(1);\n" "break;\n" "\n" @@ -574,7 +692,7 @@ "event.rc = false;\n" "break;\n" "}\n" -"res += MuPDF.convertCase(val.charAt(0), cs);\n" +"res += convertCase(val.charAt(0), cs);\n" "val = val.substring(1);\n" "break;\n" "\n" @@ -781,12 +899,10 @@ "}\n" "\n" "function AFRange_Validate(lowerCheck, lowerLimit, upperCheck, upperLimit) {\n" -"if (upperCheck && event.value > upperLimit) {\n" +"if (upperCheck && event.value > upperLimit)\n" "event.rc = false;\n" -"}\n" -"if (lowerCheck && event.value < lowerLimit) {\n" +"if (lowerCheck && event.value < lowerLimit)\n" "event.rc = false;\n" -"}\n" "if (!event.rc) {\n" "if (lowerCheck && upperCheck)\n" "app.alert(util.printf('The entered value ('+event.value+') must be greater than or equal to %s and less than or equal to %s', lowerLimit, upperLimit));\n" diff --git a/source/pdf/pdf-js.c b/source/pdf/pdf-js.c index a8e1d8a3..49b9c1e6 100644 --- a/source/pdf/pdf-js.c +++ b/source/pdf/pdf-js.c @@ -583,7 +583,7 @@ static void preload_helpers(pdf_js *js) js_dostring(js->imp, #include "js/util.js.h" - ); + ); } void pdf_drop_js(fz_context *ctx, pdf_js *js) @@ -696,14 +696,14 @@ void pdf_js_execute(pdf_js *js, char *source) { if (js_ploadstring(js->imp, "[pdf]", source)) { - fz_warn(js->ctx, "%s", js_tostring(js->imp, -1)); + fz_warn(js->ctx, "%s", js_trystring(js->imp, -1, "Error")); js_pop(js->imp, 1); return; } js_getregistry(js->imp, "Doc"); /* set 'this' to the Doc object */ if (js_pcall(js->imp, 0)) { - fz_warn(js->ctx, "%s", js_tostring(js->imp, -1)); + fz_warn(js->ctx, "%s", js_trystring(js->imp, -1, "Error")); js_pop(js->imp, 1); return; } -- cgit v1.2.3