/
iiif.js
104 lines (87 loc) · 2.1 KB
/
iiif.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
'use strict'
const { URL } = require('url')
const { rotate, isHorizontal } = require('./math')
class Rotation {
constructor(input = {}) {
if (typeof input === 'string') {
this.parse(input)
} else {
this.set(input)
}
}
set({ angle = 0, mirror = false }) {
this.angle = angle
this.mirror = mirror
}
add({ angle, mirror = false }) {
if (angle != null) {
this.angle = rotate(this.angle, angle)
}
if (mirror) {
this.mirror = !this.mirror
}
return this
}
subtract({ angle = 0, mirror = false }) {
return this.add({ angle: -angle, mirror })
}
parse(input = '') {
this.angle = parseFloat(input.slice(1))
this.mirror = input.startsWith('!')
return this
}
format(symbol = '!') {
return `${this.mirror ? symbol : ''}${this.angle}`
}
mode({ width, height }, h = this.isHorizontal) {
return (width < height) ?
(h ? 'portrait' : 'landscape') :
(h ? 'landscape' : 'portrait')
}
get isHorizontal() {
return isHorizontal(this.angle)
}
get orientation() {
switch (this.angle) {
case 0:
return this.mirror ? 2 : 1
case 90:
return this.mirror ? 7 : 6
case 180:
return this.mirror ? 4 : 3
case 270:
return this.mirror ? 5 : 8
default:
throw new Error('invalid angle')
}
}
toJSON() {
return { angle: this.angle, mirror: this.mirror }
}
static fromExifOrientation(orientation) {
switch (orientation) {
case 2:
return new Rotation({ angle: 0, mirror: true })
case 3:
return new Rotation({ angle: 180, mirror: false })
case 4:
return new Rotation({ angle: 180, mirror: true })
case 5:
return new Rotation({ angle: 270, mirror: true })
case 6:
return new Rotation({ angle: 90, mirror: false })
case 7:
return new Rotation({ angle: 90, mirror: true })
case 8:
return new Rotation({ angle: 270, mirror: false })
default:
return new Rotation()
}
}
}
class IIIF extends URL {
}
module.exports = {
IIIF,
Rotation
}