Skip to content

Commit

Permalink
JS -- Implement app object
Browse files Browse the repository at this point in the history
  • Loading branch information
calixteman committed Nov 17, 2020
1 parent a06f487 commit 7a7ac3b
Show file tree
Hide file tree
Showing 8 changed files with 1,049 additions and 8 deletions.
513 changes: 510 additions & 3 deletions src/scripting_api/app.js

Large diffs are not rendered by default.

154 changes: 154 additions & 0 deletions src/scripting_api/color.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/* Copyright 2020 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { PDFObject } from "./pdf_object.js";

class Color extends PDFObject {
constructor() {
super({});

this.transparent = ["T"];
this.black = ["G", 0];
this.white = ["G", 1];
this.red = ["RGB", 1, 0, 0];
this.green = ["RGB", 0, 1, 0];
this.blue = ["RGB", 0, 0, 1];
this.cyan = ["CMYK", 1, 0, 0, 0];
this.magenta = ["CMYK", 0, 1, 0, 0];
this.yellow = ["CMYK", 0, 0, 1, 0];
this.dkGray = ["G", 0.25];
this.gray = ["G", 0.5];
this.ltGray = ["G", 0.75];

this._converters = {
CMYK_G(components) {
const [c, y, m, k] = components;
return ["G", 1 - Math.min(1, 0.3 * c + 0.59 * m + 0.11 * y + k)];
},
G_CMYK(components) {
const [g] = components;
return ["CMYK", 0, 0, 0, 1 - g];
},
G_RGB(components) {
const [g] = components;
return ["RGB", g, g, g];
},
RGB_G(components) {
const [r, g, b] = components;
return ["G", 0.3 * r + 0.59 * g + 0.11 * b];
},
CMYK_RGB(components) {
const [c, y, m, k] = components;
return [
"RGB",
1 - Math.min(1, c + k),
1 - Math.min(1, m + k),
1 - Math.min(1, y + k),
];
},
RGB_CMYK(components) {
const [r, g, b] = components;
const c = 1 - r;
const m = 1 - g;
const y = 1 - b;
return ["CMYK", c, m, y, Math.min(c, Math.min(m, y))];
},
};
}

_isValidSpace(cColorSpace) {
return (
typeof cColorSpace === "string" &&
(cColorSpace === "T" ||
cColorSpace === "G" ||
cColorSpace === "RGB" ||
cColorSpace === "CMYK")
);
}

_getCorrectColor(colorArray) {
if (!Array.isArray(colorArray) || colorArray.length === 0) {
return this.black;
}
const space = colorArray[0];
if (!this._isValidSpace(space)) {
return this.black;
}

if (space === "T" && colorArray.length !== 1) {
return this.black;
}

if (space === "G" && colorArray.length !== 2) {
return this.black;
}

if (space === "RGB" && colorArray.length !== 4) {
return this.black;
}

if (space === "CMYK" && colorArray.length !== 5) {
return this.black;
}

if (
colorArray.slice(1).some(c => typeof c !== "number" || c < 0 || c > 1)
) {
return this.black;
}

return colorArray;
}

convert(colorArray, cColorSpace) {
if (!this._isValidSpace(cColorSpace)) {
return this.black;
}

if (cColorSpace === "T") {
return ["T"];
}

colorArray = this._getCorrectColor(colorArray);
if (colorArray[0] === cColorSpace) {
return colorArray;
}

if (colorArray[0] === "T") {
return this.convert(this.black, cColorSpace);
}

return this._converters[`${colorArray[0]}_${cColorSpace}`](
colorArray.slice(1)
);
}

equal(colorArray1, colorArray2) {
colorArray1 = this._getCorrectColor(colorArray1);
colorArray2 = this._getCorrectColor(colorArray2);

if (colorArray1[0] === "T" || colorArray2[0] === "T") {
return colorArray1[0] === "T" && colorArray2[0] === "T";
}

if (colorArray1[0] !== colorArray2[0]) {
colorArray2 = this.convert(colorArray2, colorArray1[0]);
}

return colorArray1.slice(1).every((c, i) => c === colorArray2[i + 1]);
}
}

export { Color };
113 changes: 112 additions & 1 deletion src/scripting_api/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,105 @@
* limitations under the License.
*/

const Border = Object.freeze({
s: "solid",
d: "dashed",
b: "beveled",
i: "inset",
u: "underline",
});

const Cursor = Object.freeze({
visible: 0,
hidden: 1,
delay: 2,
});

const Display = Object.freeze({
visible: 0,
hidden: 1,
noPrint: 2,
noView: 3,
});

const Font = Object.freeze({
Times: "Times-Roman",
TimesB: "Times-Bold",
TimesI: "Times-Italic",
TimesBI: "Times-BoldItalic",
Helv: "Helvetica",
HelvB: "Helvetica-Bold",
HelvI: "Helvetica-Oblique",
HelvBI: "Helvetica-BoldOblique",
Cour: "Courier",
CourB: "Courier-Bold",
CourI: "Courier-Oblique",
CourBI: "Courier-BoldOblique",
Symbol: "Symbol",
ZapfD: "ZapfDingbats",
KaGo: "HeiseiKakuGo-W5-UniJIS-UCS2-H",
KaMi: "HeiseiMin-W3-UniJIS-UCS2-H",
});

const Highlight = Object.freeze({
n: "none",
i: "invert",
p: "push",
o: "outline",
});

const Position = Object.freeze({
textOnly: 0,
iconOnly: 1,
iconTextV: 2,
textIconV: 3,
iconTextH: 4,
textIconH: 5,
overlay: 6,
});

const ScaleHow = Object.freeze({
proportional: 0,
anamorphic: 1,
});

const ScaleWhen = Object.freeze({
always: 0,
never: 1,
tooBig: 2,
tooSmall: 3,
});

const Style = Object.freeze({
ch: "check",
cr: "cross",
di: "diamond",
ci: "circle",
st: "star",
sq: "square",
});

const Trans = Object.freeze({
blindsH: "BlindsHorizontal",
blindsV: "BlindsVertical",
boxI: "BoxIn",
boxO: "BoxOut",
dissolve: "Dissolve",
glitterD: "GlitterDown",
glitterR: "GlitterRight",
glitterRD: "GlitterRightDown",
random: "Random",
replace: "Replace",
splitHI: "SplitHorizontalIn",
splitHO: "SplitHorizontalOut",
splitVI: "SplitVerticalIn",
splitVO: "SplitVerticalOut",
wipeD: "WipeDown",
wipeL: "WipeLeft",
wipeR: "WipeRight",
wipeU: "WipeUp",
});

const ZoomType = Object.freeze({
none: "NoVary",
fitP: "FitPage",
Expand All @@ -23,4 +122,16 @@ const ZoomType = Object.freeze({
refW: "ReflowWidth",
});

export { ZoomType };
export {
Border,
Cursor,
Display,
Font,
Highlight,
Position,
ScaleHow,
ScaleWhen,
Style,
Trans,
ZoomType,
};
Loading

0 comments on commit 7a7ac3b

Please sign in to comment.