Skip to content

Commit

Permalink
Add support for underline annotation import and export
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtcode committed Jul 21, 2023
1 parent 23ba495 commit b534796
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/annotations/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function getAnnotationID(rawAnnot) {

function isTransferable(rawAnnot) {
let id = getAnnotationID(rawAnnot);
return !!(['/Text', '/Highlight'].includes(rawAnnot['/Subtype'])
return !!(['/Text', '/Highlight', '/Underline'].includes(rawAnnot['/Subtype'])
|| ['/Square', '/Ink'].includes(rawAnnot['/Subtype']) && id);
}

Expand Down
7 changes: 2 additions & 5 deletions src/annotations/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,7 @@ exports.readRawAnnotation = function (rawAnnot, pageIndex, view) {
}

type = type.toLowerCase();
if (type === 'underline') {
type = 'highlight';
}
else if (type === 'text') {
if (type === 'text') {
type = 'note';
}
else if (type === 'square') {
Expand All @@ -133,7 +130,7 @@ exports.readRawAnnotation = function (rawAnnot, pageIndex, view) {
return null;
}

if (['highlight', 'note', 'image'].includes(annotation.type)) {
if (['highlight', 'underline', 'note', 'image'].includes(annotation.type)) {
let rects;
if (Array.isArray(rawAnnot['/QuadPoints'])
&& rawAnnot['/QuadPoints'].length % 8 === 0
Expand Down
62 changes: 61 additions & 1 deletion src/annotations/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ exports.writeRawAnnotations = function (structure, annotations) {
continue;
}
page['/Annots'].push(rawAnnotation);
if (annotation.type === 'highlight' && annotation.comment) {
if (['highlight', 'underline'].includes(annotation.type) && annotation.comment) {
page['/Annots'].push(addPopup(rawAnnotation));
}
}
Expand Down Expand Up @@ -198,6 +198,66 @@ function annotationToRaw(annotation) {

return res;
}
else if (annotation.type === 'underline') {
let p = '';
for (let rect of annotation.position.rects) {
p += rect[0] + ' ' + rect[1] + ' m\r';
p += rect[2] + ' ' + rect[1] + ' l\r';
p += rect[2] + ' ' + (rect[1] + 3) + ' l\r';
p += rect[0] + ' ' + (rect[1] + 3) + ' l\rh\r';
}

let res = {
'/Type': '/Annot',
'/Rect': containerRect,
'/Subtype': '/Underline',
'/QuadPoints': rectsToQuads(annotation.position.rects),
'/M': '(' + dateToRaw(annotation.dateModified) + ')',
'/T': '(' + stringToRaw(annotation.authorName) + ')',
'/Contents': '(' + stringToRaw(annotation.comment) + ')',
'/NM': '(' + 'Zotero-' + annotation.id + ')',
'/Zotero:Key': '(' + annotation.id + ')',
'/Zotero:AuthorName': '(' + stringToRaw(annotation.authorName) + ')',
'/F': 4,
'/C': colorToRaw(annotation.color),
'/AP': {
'/N': {
'/BBox': containerRect,
'/FormType': 1,
'/Resources': {
'/ExtGState': {
'/G0': {
'/BM': '/Multiply',
'/CA': 1,
'/ca': 1,
num: 0,
gen: 0
},
num: 0,
gen: 0
}, num: 0, gen: 0
},
'/Subtype': '/Form',
'/Type': '/XObject',
stream: '/G0 gs\r' + colorToRaw(annotation.color).join(' ') + ' rg\r' + p + 'f\r',
num: 0,
gen: 0
}
},
num: 0,
gen: 0
};

if (!annotation.comment) {
delete res['/Contents'];
}

if (annotation.tags.length) {
res['/Zotero:Tags'] = '(' + stringToRaw(JSON.stringify(annotation.tags)) + ')';
}

return res;
}
else if (annotation.type === 'image') {
let p = [
containerRect[0],
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ async function importAnnotations(buf, existingAnnotations, password, transfer, c
annotation.pageLabel = pageLabel || (pageIndex + 1).toString();

let offset = 0;
if (annotation.type === 'highlight') {
if (['highlight', 'underline'].includes(annotation.type)) {
let range = getRangeByHighlight(structuredText, annotation.position.rects);
if (range) {
offset = range.offset;
Expand All @@ -210,7 +210,7 @@ async function importAnnotations(buf, existingAnnotations, password, transfer, c
}

let top = 0;
if (['highlight', 'note', 'image'].includes(annotation.type)) {
if (['highlight', 'underline', 'note', 'image'].includes(annotation.type)) {
top = pageHeight - annotation.position.rects[0][3];
}
// Ink
Expand Down

0 comments on commit b534796

Please sign in to comment.