-
Notifications
You must be signed in to change notification settings - Fork 1
/
titles.11tydata.js
67 lines (65 loc) · 2.08 KB
/
titles.11tydata.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
function scaledRectangle(rectangle, canvas, { imageHeight, imageWidth }) {
const scale = Math.max((canvas.height / imageHeight), (canvas.width / imageWidth))
const { x_center, y_center, width, height } = rectangle
const x = parseInt((x_center - (width / 2)) * scale, 10)
const y = parseInt((y_center - (height / 2)) * scale, 10)
const w = parseInt(width * scale, 10)
const h = parseInt(height * scale, 10)
return { x, y, w, h }
}
function transformRectangles(annotations, canvas, config, partOf) {
const drawing = annotations.find(annotation => annotation.task === 'T0')
const rectangles = drawing.value
const source = {
id: canvas['@id'],
type: 'Canvas',
partOf
}
return rectangles.map((rectangle, index) => {
const { x, y, w, h } = scaledRectangle(rectangle, canvas, config)
const selector = {
type: 'FragmentSelector',
conformsTo: 'http://www.w3.org/TR/media-frags/',
value: `xywh=${x},${y},${w},${h}`
}
const textAnnotation = annotations.find(annotation => annotation.markIndex === index)
const label = textAnnotation.value
return {
type: 'Annotation',
motivation: 'tagging',
body: {
type: 'TextualBody',
value: label,
language: 'en',
format: 'text/plain'
},
target: {
type: 'SpecificResource',
source,
selector
}
}
})
}
function annotations({ config, manifest, titles: classifications }) {
const partOf = {
id: manifest[`@id`],
type: 'Manifest'
}
const titles = classifications.map(classification => {
const { metadata, annotations, subjectMetadata } = classification
const canvasIndex = subjectMetadata['#priority'] - 1
const [ sequence ] = manifest.sequences
const canvas = sequence.canvases[canvasIndex]
return transformRectangles(annotations, canvas, config, partOf)
}).flat()
titles.forEach((title, index) => {
title.id = `https://zooniverse.github.io/iiif-annotations/bldigital/in-the-spotlight/titles/${index}`
})
return titles
}
module.exports = {
eleventyComputed: {
annotations
}
}