Skip to content

Commit 16f2ab2

Browse files
committed
Annotation - Use border and background colors from MK dictionary
- aims to fix #13003; - pass the colors to js sandbox; - fix checkbox color.
1 parent 4b49db7 commit 16f2ab2

File tree

7 files changed

+71
-29
lines changed

7 files changed

+71
-29
lines changed

src/core/annotation.js

+37-21
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,12 @@ class AnnotationFactory {
192192
}
193193
}
194194

195-
function getRgbColor(color) {
196-
const rgbColor = new Uint8ClampedArray(3);
195+
function getRgbColor(color, defaultColor) {
197196
if (!Array.isArray(color)) {
198-
return rgbColor;
197+
return defaultColor;
199198
}
200199

200+
const rgbColor = defaultColor || new Uint8ClampedArray(3);
201201
switch (color.length) {
202202
case 0: // Transparent, which we indicate with a null value
203203
return null;
@@ -215,7 +215,7 @@ function getRgbColor(color) {
215215
return rgbColor;
216216

217217
default:
218-
return rgbColor;
218+
return defaultColor;
219219
}
220220
}
221221

@@ -325,6 +325,7 @@ class Annotation {
325325
this.setColor(dict.getArray("C"));
326326
this.setBorderStyle(dict);
327327
this.setAppearance(dict);
328+
this.setBorderAndBackgroundColors(dict.get("MK"));
328329

329330
this._streams = [];
330331
if (this.appearance) {
@@ -336,6 +337,8 @@ class Annotation {
336337
annotationFlags: this.flags,
337338
borderStyle: this.borderStyle,
338339
color: this.color,
340+
backgroundColor: this.backgroundColor,
341+
borderColor: this.borderColor,
339342
contents: this.contents,
340343
hasAppearance: !!this.appearance,
341344
id: params.id,
@@ -489,7 +492,22 @@ class Annotation {
489492
* 4 (CMYK) elements
490493
*/
491494
setColor(color) {
492-
this.color = getRgbColor(color);
495+
this.color = getRgbColor(color, new Uint8ClampedArray(3));
496+
}
497+
498+
/**
499+
* Set the color for background and border if any.
500+
* The default values are transparent.
501+
*
502+
* @public
503+
* @memberof Annotation
504+
* @param {Dict} - The MK dictionary
505+
*/
506+
setBorderAndBackgroundColors(mk) {
507+
if (mk instanceof Dict) {
508+
this.borderColor = getRgbColor(mk.getArray("BC"), null);
509+
this.backgroundColor = getRgbColor(mk.getArray("BG"), null);
510+
}
493511
}
494512

495513
/**
@@ -1743,6 +1761,8 @@ class TextWidgetAnnotation extends WidgetAnnotation {
17431761
name: this.data.fieldName,
17441762
rect: this.data.rect,
17451763
actions: this.data.actions,
1764+
strokeColor: this.data.borderColor,
1765+
fillColor: this.data.backgroundColor,
17461766
type: "text",
17471767
};
17481768
}
@@ -2074,6 +2094,8 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
20742094
rect: this.data.rect,
20752095
hidden: this.data.hidden,
20762096
actions: this.data.actions,
2097+
strokeColor: this.data.borderColor,
2098+
fillColor: this.data.backgroundColor,
20772099
type,
20782100
};
20792101
}
@@ -2154,6 +2176,8 @@ class ChoiceWidgetAnnotation extends WidgetAnnotation {
21542176
hidden: this.data.hidden,
21552177
actions: this.data.actions,
21562178
items: this.data.options,
2179+
strokeColor: this.data.borderColor,
2180+
fillColor: this.data.backgroundColor,
21572181
type,
21582182
};
21592183
}
@@ -2324,14 +2348,10 @@ class SquareAnnotation extends MarkupAnnotation {
23242348
: [0, 0, 0];
23252349

23262350
// The default fill color is transparent.
2327-
let fillColor = null;
2328-
let interiorColor = parameters.dict.getArray("IC");
2329-
if (interiorColor) {
2330-
interiorColor = getRgbColor(interiorColor);
2331-
fillColor = interiorColor
2332-
? Array.from(interiorColor).map(c => c / 255)
2333-
: null;
2334-
}
2351+
const interiorColor = getRgbColor(parameters.dict.getArray("IC"), null);
2352+
const fillColor = interiorColor
2353+
? Array.from(interiorColor).map(c => c / 255)
2354+
: null;
23352355

23362356
this._setDefaultAppearance({
23372357
xref: parameters.xref,
@@ -2369,14 +2389,10 @@ class CircleAnnotation extends MarkupAnnotation {
23692389
: [0, 0, 0];
23702390

23712391
// The default fill color is transparent.
2372-
let fillColor = null;
2373-
let interiorColor = parameters.dict.getArray("IC");
2374-
if (interiorColor) {
2375-
interiorColor = getRgbColor(interiorColor);
2376-
fillColor = interiorColor
2377-
? Array.from(interiorColor).map(c => c / 255)
2378-
: null;
2379-
}
2392+
const interiorColor = getRgbColor(parameters.dict.getArray("IC"), null);
2393+
const fillColor = interiorColor
2394+
? Array.from(interiorColor).map(c => c / 255)
2395+
: null;
23802396

23812397
// Circles are approximated by Bézier curves with four segments since
23822398
// there is no circle primitive in the PDF specification. For the control

src/display/annotation_layer.js

+24
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,16 @@ class WidgetAnnotationElement extends AnnotationElement {
615615
}
616616
}
617617
}
618+
619+
_setBorderAndBackgroundColors(element) {
620+
for (const name of ["borderColor", "backgroundColor"]) {
621+
const color = this.data[name];
622+
element.style[name] =
623+
color === null
624+
? "transparent"
625+
: Util.makeHexColor(color[0], color[1], color[2]);
626+
}
627+
}
618628
}
619629

620630
class TextWidgetAnnotationElement extends WidgetAnnotationElement {
@@ -860,6 +870,7 @@ class TextWidgetAnnotationElement extends WidgetAnnotationElement {
860870
}
861871

862872
this._setTextStyle(element);
873+
this._setBorderAndBackgroundColors(element);
863874

864875
this.container.appendChild(element);
865876
return this.container;
@@ -976,6 +987,15 @@ class CheckboxWidgetAnnotationElement extends WidgetAnnotationElement {
976987
);
977988
}
978989

990+
this._setBorderAndBackgroundColors(element);
991+
992+
const { fontColor } = this.data.defaultAppearanceData;
993+
element.style.color = Util.makeHexColor(
994+
fontColor[0],
995+
fontColor[1],
996+
fontColor[2]
997+
);
998+
979999
this.container.appendChild(element);
9801000
return this.container;
9811001
}
@@ -1066,6 +1086,8 @@ class RadioButtonWidgetAnnotationElement extends WidgetAnnotationElement {
10661086
);
10671087
}
10681088

1089+
this._setBorderAndBackgroundColors(element);
1090+
10691091
this.container.appendChild(element);
10701092
return this.container;
10711093
}
@@ -1297,6 +1319,8 @@ class ChoiceWidgetAnnotationElement extends WidgetAnnotationElement {
12971319
});
12981320
}
12991321

1322+
this._setBorderAndBackgroundColors(selectElement);
1323+
13001324
this.container.appendChild(selectElement);
13011325
return this.container;
13021326
}

test/integration/scripting_spec.js

+1
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,7 @@ describe("Interaction", () => {
664664
);
665665

666666
await page.click(`[data-annotation-id='${id}R']`);
667+
await page.focus(ref);
667668
await page.waitForFunction(
668669
(_ref, _current, _propName) =>
669670
getComputedStyle(document.querySelector(_ref))[_propName] !==

test/pdfs/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@
260260
!issue4061.pdf
261261
!issue4668.pdf
262262
!PDFJS-7562-reduced.pdf
263+
!issue13003.pdf
263264
!issue11768_reduced.pdf
264265
!issue5039.pdf
265266
!issue5070.pdf

test/pdfs/issue13003.pdf

10.4 KB
Binary file not shown.

test/test_manifest.json

+7
Original file line numberDiff line numberDiff line change
@@ -2343,6 +2343,13 @@
23432343
"rounds": 1,
23442344
"type": "eq"
23452345
},
2346+
{ "id": "issue13003",
2347+
"file": "pdfs/issue13003.pdf",
2348+
"md5": "a4c00bd6456d3c16b9bd44957caa4ab6",
2349+
"rounds": 1,
2350+
"type": "eq",
2351+
"annotations": true
2352+
},
23462353
{ "id": "issue7200",
23472354
"file": "pdfs/issue7200.pdf",
23482355
"md5": "ddae17424ea23930eecf8b612a66ed0f",

web/annotation_layer_builder.css

+1-8
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
.annotationLayer .choiceWidgetAnnotation select,
4646
.annotationLayer .buttonWidgetAnnotation.checkBox input,
4747
.annotationLayer .buttonWidgetAnnotation.radioButton input {
48-
background-color: rgba(0, 54, 255, 0.13);
48+
background-color: transparent;
4949
border: 1px solid transparent;
5050
box-sizing: border-box;
5151
font-size: 9px;
@@ -88,13 +88,6 @@
8888
border: 1px solid rgba(0, 0, 0, 1);
8989
}
9090

91-
.annotationLayer .textWidgetAnnotation input:focus,
92-
.annotationLayer .textWidgetAnnotation textarea:focus,
93-
.annotationLayer .choiceWidgetAnnotation select:focus {
94-
background: none;
95-
border: 1px solid transparent;
96-
}
97-
9891
.annotationLayer .buttonWidgetAnnotation.checkBox input:checked:before,
9992
.annotationLayer .buttonWidgetAnnotation.checkBox input:checked:after,
10093
.annotationLayer .buttonWidgetAnnotation.radioButton input:checked:before {

0 commit comments

Comments
 (0)