-
Notifications
You must be signed in to change notification settings - Fork 0
/
bsp_openseadragon.module
332 lines (305 loc) · 10.3 KB
/
bsp_openseadragon.module
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php
/**
* @file
* Hooks and callbacks for this module.
*/
/**
* Implements hook_menu().
*/
function bsp_openseadragon_menu() {
return array(
'admin/config/media/openseadragon' => array(
'title' => 'OpenSeadragon',
'description' => 'Configure the OpenSeadragon viewer.',
'page callback' => 'drupal_get_form',
'access arguments' => array('administer site configuration'),
'page arguments' => array('bsp_openseadragon_admin'),
'file' => 'includes/admin.form.inc',
'type' => MENU_NORMAL_ITEM,
),
'openseadragon/viewer' => array(
'type' => MENU_CALLBACK,
'access callback' => TRUE,
'page callback' => 'bsp_openseadragon_viewer_page',
),
);
}
/**
* Implements hook_theme().
*/
function bsp_openseadragon_theme() {
return array(
'bsp_openseadragon_viewer' => array(
'variables' => array(
'uri' => '',
),
'template' => 'theme/bsp-openseadragon',
),
);
}
/**
* Implements hook_bsp_viewer_info().
*/
function bsp_openseadragon_bsp_viewer_info() {
return array(
'bsp_openseadragon' => array(
'label' => t('OpenSeadragon'),
'description' => t('OpenSeadragon viewer with an IIIF-compatible as Tile Source.'),
'configuration' => 'admin/config/media/openseadragon',
'callback' => 'bsp_openseadragon_callback',
'mimetype' => array('image/jp2'),
),
);
}
/**
* Implements hook_field_formatter_info().
*/
function bsp_openseadragon_field_formatter_info() {
return array(
'textfield_openseadragon' => array(
'label' => t('OpenSeadragon text field URI viewer'),
'field types' => array('text'),
),
);
}
/**
* Implements hook_field_formatter_view().
*/
function bsp_openseadragon_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
switch ($display['type']) {
case 'textfield_openseadragon':
if (!empty($items[0])) {
$library_path = libraries_get_path('openseadragon');
$module_path = drupal_get_path('module', 'bsp_openseadragon');
$settings = bsp_openseadragon_add_settings();
$params = array(
'bspOpenSeadragon' => array(
'resourceUri' => _get_iiif_uri($items[0]['value']),
'tileSize' => (int) variable_get('bsp_openseadragon_tile_size', '256'),
'tileOverlap' => (int) variable_get('bsp_openseadragon_tile_overlap', '0'),
'settings' => $settings),
);
drupal_add_js($params, 'setting');
drupal_add_js("$library_path/openseadragon.js");
drupal_add_js("$module_path/js/bsp_openseadragon.js");
drupal_add_css("$module_path/css/bsp_openseadragon.theme.css");
$element[0] = array(
'#markup' => '<div id="bsp-openseadragon" class="bsp-openseadragon bsp-openseadragon--field-formatter"></div>',
);
}
break;
}
return $element;
}
/**
* Callback function for the viewer.
*
* @param array $params
* Params required by the theme. We require the keys:
* - jp2_url: The URL to the JP2 image.
* @param bspAbstractObject $fedora_object
* The object that we are viewing.
*
* @return string
* HTML repersentation of the OpenSeadragon viewer
*/
function bsp_openseadragon_callback($params = NULL, $fedora_object = NULL) {
if (isset($params['jp2_url'])) {
$uri = $params['jp2_url'];
return theme('bsp_openseadragon_viewer', array('uri' => $uri, 'fedora_object' => $fedora_object));
}
}
/**
* Implements hook_preprocess().
*/
function bsp_openseadragon_preprocess_bsp_openseadragon_viewer(&$variables) {
$variables['viewer_id'] = 'bsp-openseadragon';
$settings = bsp_openseadragon_add_settings();
// This is where we jam in some HOCR coordinates.
if (isset($variables['fedora_object']) && isset($_GET['solr'])) {
$params = $_GET['solr']['params'];
$params['fq'][] = format_string('PID:"!pid"', array(
'!pid' => $variables['fedora_object']->id,
));
module_load_include('inc', 'bsp_paged_content', 'includes/highlighting');
$highlighting_info = bsp_paged_content_perform_solr_highlighting_query($_GET['solr']['query'], $params);
$highlights = array();
foreach ($highlighting_info as $pid => $info) {
foreach ($info['snippets'] as $snippet => $bounding_boxes) {
foreach ($bounding_boxes as $bounding_box) {
// The size on the x axis is used to calculate the proportional values
// under the openseadragon coordinate system.
$highlights[] = array(
'x' => $bounding_box['left'] / $info['page']['width'],
'y' => $bounding_box['top'] / $info['page']['width'],
'width' => ($bounding_box['right'] - $bounding_box['left']) / $info['page']['width'],
'height' => ($bounding_box['bottom'] - $bounding_box['top']) / $info['page']['width'],
'className' => "bsp-openseadragon-highlight",
);
}
}
}
$settings['overlays'] = $highlights;
}
drupal_add_js(array(
'bspOpenSeadragon' => array(
'pid' => isset($variables['fedora_object']) ? $variables['fedora_object']->id : NULL,
'resourceUri' => _get_iiif_uri($variables['uri']),
'tileSize' => (int) variable_get('bsp_openseadragon_tile_size', '256'),
'tileOverlap' => (int) variable_get('bsp_openseadragon_tile_overlap', '0'),
'settings' => $settings,
),
), 'setting');
drupal_add_js("$module_path/js/bsp_openseadragon.js");
drupal_add_js("$library_path/openseadragon.js");
drupal_add_css("$module_path/css/bsp_openseadragon.theme.css");
}
function bsp_openseadragon_add_settings() {
// Variable fedora_object included in results, but not used.
$library_path = libraries_get_path('openseadragon');
$module_path = drupal_get_path('module', 'bsp_openseadragon');
$settings = array(
'id' => 'bsp-openseadragon',
'prefixUrl' => file_create_url("$module_path/images/"),
'navigatorPosition' => 'TOP_RIGHT',
);
module_load_include('inc', 'bsp_openseadragon', 'includes/utilities');
$installed_version = bsp_openseadragon_get_installed_version();
if (version_compare($installed_version, '0.9.82') <= 0) {
// XXX: Version numbers don't quite line-up for the change in the nav image
// prefix URL, so now, let the hacking begin!... Just for compatibility
// with older versions.
$types = array('zoomIn', 'zoomOut', 'home', 'fullpage', 'previous', 'next');
$actions = array(
'REST' => 'rest',
'GROUP' => 'grouphover',
'HOVER' => 'hover',
'DOWN' => 'pressed',
);
foreach ($types as $type) {
foreach ($actions as $action => $namepart) {
$settings['navImages'][$type][$action] = format_string('!type_!action.png', array(
'!type' => strtolower($type),
'!action' => $namepart,
));
}
}
}
$settings = array_merge($settings, bsp_openseadragon_get_settings());
foreach ($settings as $key => $val) {
if (filter_var($val, FILTER_VALIDATE_FLOAT)) {
$settings[$key] = (float) $val;
}
elseif (filter_var($val, FILTER_VALIDATE_INT)) {
$settings[$key] = (int) $val;
}
}
return $settings;
}
/**
* Get OpenSeadragon settings.
*
* @return array
* Configuration options for OpenSeadragon.
*/
function bsp_openseadragon_get_settings() {
return variable_get('bsp_openseadragon_settings', array(
'debugMode' => FALSE,
'animationTime' => '1.5',
'blendTime' => '0.1',
'alwaysBlend' => FALSE,
'autoHideControls' => TRUE,
'immediateRender' => FALSE,
'wrapHorizontal' => FALSE,
'wrapVertical' => FALSE,
'wrapOverlays' => FALSE,
'panHorizontal' => TRUE,
'panVertical' => TRUE,
'minZoomImageRatio' => '0.8',
'maxZoomPixelRatio' => '2',
'visibilityRatio' => '0.5',
'springStiffness' => '5.0',
'imageLoaderLimit' => '5',
'clickTimeThreshold' => '300',
'clickDistThreshold' => '5',
'zoomPerClick' => '2.0',
'zoomPerScroll' => '1.2',
'zoomPerSecond' => '2.0',
'showNavigator' => TRUE,
)
);
}
/**
* Implements hook_process_theme().
*/
function bsp_openseadragon_preprocess_bsp_object_print(array &$variables) {
$object = $variables['object'];
if (isset($_GET['clip'])) {
$dimensions = array();
if (isset($_GET['dimensions'])) {
$dimensions = explode(',', $_GET['dimensions']);
$dimensions = array(
'width' => $dimensions[0],
'height' => $dimensions[1],
);
}
$variables['clip'] = $clip = $_GET['clip'];
$variables['content']['clip'] = array(
'#weight' => 0,
'#prefix' => "<div id='clip'>",
'#markup' => theme_image(array(
'path' => $_GET['clip'], 'attributes' => $dimensions)),
'#suffix' => '</div>',
);
}
}
/**
* Page callback to display a seadragon viewer.
*
* @param null $id
* @return array|string
*/
function bsp_openseadragon_viewer_page($id = NULL) {
if (!$id) {
return '';
}
$library_path = libraries_get_path('openseadragon');
$module_path = drupal_get_path('module', 'bsp_openseadragon');
$settings = bsp_openseadragon_add_settings();
$params = array(
'bspOpenSeadragon' => array(
'resourceUri' => _get_iiif_uri($id),
'tileSize' => (int) variable_get('bsp_openseadragon_tile_size', '256'),
'tileOverlap' => (int) variable_get('bsp_openseadragon_tile_overlap', '0'),
'settings' => $settings),
);
drupal_add_js($params, 'setting');
drupal_add_js("$library_path/openseadragon.js");
drupal_add_js("$module_path/js/bsp_openseadragon.js");
drupal_add_css("$module_path/css/bsp_openseadragon.theme.css");
$output = array(
'#markup' => '<div id="bsp-openseadragon" class="bsp-openseadragon bsp-openseadragon--viewer-page"></div>',
);
return $output;
}
/**
* Helper function to convert an Islandora uri into an iiif uri.
*/
function _get_iiif_uri($string) {
$islandora = preg_quote('https://dl.library.ucla.edu/islandora/object/', '/');
$iiif_prefix = 'https://dl.library.ucla.edu/iiif/2/';
$iiif_suffix = '~JP2~~default';
if (strpos($string, $iiif_prefix) === 0) {
$result = $string;
}
else if (preg_match("/^$islandora(?<pid>.*)$/", $string, $matches)) {
$pid = $matches['pid'];
$result = $iiif_prefix . $pid . $iiif_suffix;
}
else {
$pid = $string;
$result = $iiif_prefix . $pid . $iiif_suffix;
}
return $result;
}