Skip to content

Instantly share code, notes, and snippets.

@dankessler
Created September 2, 2015 14:54
Show Gist options
  • Save dankessler/1353bb973850e1b0e6d1 to your computer and use it in GitHub Desktop.
Save dankessler/1353bb973850e1b0e6d1 to your computer and use it in GitHub Desktop.
function sortindex(array){
var ind = [];
for (var i = 0; i < array.length; i++){
ind.push([array[i],i]);
}
ind.sort(function(left,right){
return left[0] < right[0] ? -1 : 1;
});
var indices = [];
for (var i = 0; i < ind.length; i++){
indices.push(ind[i][1]);
}
return indices;
}
function getxs(quad){
var xs = [];
for (var i = 0; i < quad.length; i+=2){
xs = xs.concat(quad[i]);
}
return xs;
}
function getys(quad){
var ys = [];
for (var i = 1; i < quad.length; i+=2){
ys = ys.concat(quad[i]);
}
return ys;
}
function fixquad(quad){
var xs = getxs(quad)
var ys = getys(quad)
var indxs = sortindex(xs);
var indys = sortindex(ys);
// find candidates for top left based on greatest y
var A = indys.slice(2,4);
if (xs[A[0]] < xs[A[1]]){ // the first A candidate is better
var B = A[1];
A = A[0];
} else { // the second A candidate is better
var B = A[0];
A = A[1];
}
var C = indys.slice(0,2);
if (xs[C[0]] < xs[C[1]]){ // the first C candidate is better
var D = C[1];
C = C[0];
} else{ // the second C candidate is better
var D = C[0];
C = C[1];
}
var newind = [A, B, C, D];
var newquad = [];
for (var i = 0; i < newind.length; i++){
newquad.push(xs[newind[i]]);
newquad.push(ys[newind[i]]);
}
return newquad;
}
function fixquads(quads){
for (var i = 0; i < quads.length; i++) {
quads[i] = fixquad(quads[i]);
}
return quads;
}
this.syncAnnotScan();
var annots = this.getAnnots({});
var changeflag = 0;
if (annots){
for (var i = 0; i < annots.length; i++) {
if (annots[i].type == "Highlight"){
if (annots[i].contents == "" & ! color.equal(annots[i].strokeColor,color.green)) {
annots[i].strokeColor = color.green;
changeflag = 1;
}
if (annots[i].contents != "" & ! color.equal(annots[i].strokeColor,color.blue)){
annots[i].strokeColor = color.blue;
changeflag = 1;
}
if (annots[i].opacity != 0.5){
annots[i].opacity = 0.5;
changeflag = 1;
}
newquads = fixquads(annots[i].quads);
if (newquads.toString() != annots[i].quads.toString()){
changeflag = 1;
annots[i].quads = newquads;
}
}
if (annots[i].type == "Ink" & annots[i].Subject != "Pencil"){
var copy_props = annots[i].getProps();
annots[i].destroy();
var copy_annot = this.addAnnot(copy_props);
copy_annot.Subject = "Pencil"
changeflag = 1;
}
}
}
if (changeflag == 1) app.execMenuItem("Save", this);
this.closeDoc(true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment