More changes to support first page and last page buttons [mang].
[bookreader.git] / GnuBookIA / GnuBookJSIA.php
1 <?
2 /*
3 Copyright(c)2008 Internet Archive. Software license AGPL version 3.
4
5 This file is part of GnuBook.
6
7     GnuBook is free software: you can redistribute it and/or modify
8     it under the terms of the GNU Affero General Public License as published by
9     the Free Software Foundation, either version 3 of the License, or
10     (at your option) any later version.
11
12     GnuBook is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU Affero General Public License for more details.
16
17     You should have received a copy of the GNU Affero General Public License
18     along with GnuBook.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 $id = $_REQUEST['id'];
22 $itemPath = $_REQUEST['itemPath'];
23 $server = $_REQUEST['server']; 
24
25 if ("" == $id) {
26     GBFatal("No identifier specified!");
27 }
28
29 if ("" == $itemPath) {
30     GBFatal("No itemPath specified!");
31 }
32
33 if ("" == $server) {
34     GBFatal("No server specified!");
35 }
36
37
38 if (!preg_match("|^/[0-3]/items/{$id}$|", $itemPath)) {
39     GBFatal("Bad id!");
40 }
41
42 $imageFormat = 'unknown';
43 $zipFile = "$itemPath/{$id}_jp2.zip";
44 if (file_exists($zipFile)) {
45     $imageFormat = 'jp2';
46 } else {
47   $zipFile = "$itemPath/${id}_tif.zip";
48   if (file_exists($zipFile)) {
49     $imageFormat = 'tif';
50   }
51 }
52
53 if ("unknown" == $imageFormat) {
54   GBfatal("Unknown image format");
55 }
56
57 $scanDataFile = "$itemPath/{$id}_scandata.xml";
58 $scanDataZip  = "$itemPath/scandata.zip";
59 if (file_exists($scanDataFile)) {
60     $scanData = simplexml_load_file($scanDataFile);
61 } else if (file_exists($scanDataZip)) {
62     $cmd  = 'unzip -p ' . escapeshellarg($scanDataZip) . ' scandata.xml';
63     exec($cmd, $output, $retval);
64     if ($retval != 0) GBFatal("Could not unzip ScanData!");
65     
66     $dump = join("\n", $output);
67     $scanData = simplexml_load_string($dump);
68 } else if (file_exists("$itemPath/scandata.xml")) {
69     // For e.g. Scribe v.0 books!
70     $scanData = simplexml_load_file("$itemPath/scandata.xml");
71 } else {
72     GBFatal("ScanData file not found!");
73 }
74
75 $metaDataFile = "$itemPath/{$id}_meta.xml";
76 if (!file_exists($metaDataFile)) {
77     GBFatal("MetaData file not found!");
78 }
79
80
81 $metaData = simplexml_load_file($metaDataFile);
82
83 //$firstLeaf = $scanData->pageData->page[0]['leafNum'];
84 ?>
85
86 gb = new GnuBook();
87
88 gb.getPageWidth = function(index) {
89     //return parseInt(this.pageW[index]/this.reduce);
90     return this.pageW[index];
91 }
92
93 gb.getPageHeight = function(index) {
94     //return parseInt(this.pageH[index]/this.reduce);
95     return this.pageH[index];
96 }
97
98 gb.getPageURI = function(index) {
99     var leafStr = '0000';            
100     var imgStr = this.leafMap[index].toString();
101     var re = new RegExp("0{"+imgStr.length+"}$");
102
103     if (1==this.mode) {
104         var url = 'http://'+this.server+'/GnuBook/GnuBookImages.php?zip='+this.zip+'&file='+this.bookId+'_'+this.imageFormat+'/'+this.bookId+'_'+leafStr.replace(re, imgStr) + '.'+this.imageFormat+'&scale='+this.reduce;
105     } else {
106         var ratio = this.getPageHeight(index) / this.twoPageH;
107         var scale;
108         // $$$ we make an assumption here that the scales are available pow2 (like kakadu)
109         if (ratio <= 2) {
110             scale = 1;
111         } else if (ratio <= 4) {
112             scale = 2;
113         } else {
114             scale = 4;
115         }        
116     
117         //var url = 'http://'+this.server+'/GnuBook/GnuBookImages.php?zip='+this.zip+'&file='+this.bookId+'_jp2/'+this.bookId+'_'+leafStr.replace(re, imgStr) + '.jp2&height='+this.twoPageH+'&origHeight='+this.getPageHeight(index);
118         var url = 'http://'+this.server+'/GnuBook/GnuBookImages.php?zip='+this.zip+'&file='+this.bookId+'_'+this.imageFormat+'/'+this.bookId+'_'+leafStr.replace(re, imgStr) + '.'+this.imageFormat+'&scale='+scale;
119         
120     }
121     return url;
122 }
123
124 gb.getPageSide = function(index) {
125     //assume the book starts with a cover (right-hand leaf)
126     //we should really get handside from scandata.xml
127     
128     // $$$ we should get this from scandata instead of assuming the accessible
129     //     leafs are contiguous
130     if ('rl' != this.pageProgression) {
131         // If pageProgression is not set RTL we assume it is LTR
132         if (0 == (index & 0x1)) {
133             // Even-numbered page
134             return 'R';
135         } else {
136             // Odd-numbered page
137             return 'L';
138         }
139     } else {
140         // RTL
141         if (0 == (index & 0x1)) {
142             return 'L';
143         } else {
144             return 'R';
145         }
146     }
147 }
148
149 gb.getPageNum = function(index) {
150     return this.pageNums[index];
151 }
152
153 gb.leafNumToIndex = function(leafNum) {
154     var index = jQuery.inArray(leafNum, this.leafMap);
155     if (-1 == index) {
156         return null;
157     } else {
158         return index;
159     }
160 }
161
162 // This function returns the left and right indices for the user-visible
163 // spread that contains the given index.  The return values may be
164 // null if there is no facing page or the index is invalid.
165 gb.getSpreadIndices = function(pindex) {
166     // $$$ we could make a separate function for the RTL case and
167     //      only bind it if necessary instead of always checking
168     // $$$ we currently assume there are no gaps
169     
170     var spreadIndices = [null, null]; 
171     if ('rl' == this.pageProgression) {
172         // Right to Left
173         if (this.getPageSide(pindex) == 'R') {
174             spreadIndices[1] = pindex;
175             spreadIndices[0] = pindex + 1;
176         } else {
177             // Given index was LHS
178             spreadIndices[0] = pindex;
179             spreadIndices[1] = pindex - 1;
180         }
181     } else {
182         // Left to right
183         if (this.getPageSide(pindex) == 'L') {
184             spreadIndices[0] = pindex;
185             spreadIndices[1] = pindex + 1;
186         } else {
187             // Given index was RHS
188             spreadIndices[1] = pindex;
189             spreadIndices[0] = pindex - 1;
190         }
191     }
192     
193     //console.log("   index %d mapped to spread %d,%d", pindex, spreadIndices[0], spreadIndices[1]);
194     
195     return spreadIndices;
196 }
197
198 gb.pageW =              [
199             <?
200             $i=0;
201             foreach ($scanData->pageData->page as $page) {
202                 if (shouldAddPage($page)) {
203                     if(0 != $i) echo ",";   //stupid IE
204                     echo "{$page->cropBox->w}";
205                     $i++;
206                 }
207             }
208             ?>
209             ];
210
211 gb.pageH =              [
212             <?
213             $totalHeight = 0;
214             $i=0;            
215             foreach ($scanData->pageData->page as $page) {
216                 if (shouldAddPage($page)) {
217                     if(0 != $i) echo ",";   //stupid IE                
218                     echo "{$page->cropBox->h}";
219                     $totalHeight += intval($page->cropBox->h/4) + 10;
220                     $i++;
221                 }
222             }
223             ?>
224             ];
225 gb.leafMap = [
226             <?
227             $i=0;
228             foreach ($scanData->pageData->page as $page) {
229                 if (shouldAddPage($page)) {
230                     if(0 != $i) echo ",";   //stupid IE
231                     echo "{$page['leafNum']}";
232                     $i++;
233                 }
234             }
235             ?>    
236             ];
237
238 gb.pageNums = [
239             <?
240             $i=0;
241             foreach ($scanData->pageData->page as $page) {
242                 if (shouldAddPage($page)) {
243                     if(0 != $i) echo ",";   //stupid IE                
244                     if (array_key_exists('pageNumber', $page) && ('' != $page->pageNumber)) {
245                         echo "'{$page->pageNumber}'";
246                     } else {
247                         echo "null";
248                     }
249                     $i++;
250                 }
251             }
252             ?>    
253             ];
254             
255 <?
256 /* Output title leaf if marked */
257 $titleLeaf = '';
258 foreach ($scanData->pageData->page as $page) {
259     if (("Title Page" == $page->pageType) || ("Title" == $page->pageType)) {
260         $titleLeaf = "{$page['leafNum']}";
261         break;
262     }
263 }
264     
265 if ('' != $titleLeaf) {
266     printf("gb.titleLeaf = %d;\n", $titleLeaf);
267 }
268 ?>
269       
270 gb.numLeafs = gb.pageW.length;
271
272 gb.bookId   = '<?echo $id;?>';
273 gb.zip      = '<?echo $zipFile;?>';
274 gb.server   = '<?echo $server;?>';
275 gb.bookTitle= '<?echo preg_replace("/\'/", "\\'", $metaData->title);?>';
276 gb.bookPath = '<?echo $itemPath;?>';
277 gb.bookUrl  = '<?echo "http://www.archive.org/details/$id";?>';
278 gb.imageFormat = '<?echo $imageFormat;?>';
279 <?
280
281 # Load some values from meta.xml
282 if ('' != $metaData->{'page-progression'}) {
283   echo "gb.pageProgression = '" . $metaData->{"page-progression"} . "';";
284 } else {
285   // Assume page progression is Left To Right
286   echo "gb.pageProgression = 'lr';";
287 }
288
289 # Special cases
290 if ('bandersnatchhsye00scarrich' == $id) {
291     echo "gb.mode     = 2;\n";
292     echo "gb.auto     = true;\n";
293 }
294
295 ?>
296
297
298 // Check for config object
299 if (typeof(gbConfig) != 'undefined') {
300     if (gbConfig['mode'] == 1) {
301       gb.mode = 1;
302     } else if (gbConfig['mode'] == 2) {
303       gb.mode = 2;
304       
305       //$$$mang hack to override request for 2up for RTL until we have full RTL support
306       //        we need a better way to determine the mode and pass config options
307       //if ((typeof(gb.pageProgression) != 'undefined') && (gb.pageProgression == 'rl')) {
308       //  gb.mode = 1;
309       //}
310   
311     }
312 }
313
314 gb.init();
315
316 <?
317
318
319 function GBFatal($string) {
320     echo "alert('$string')\n";
321     die(-1);
322 }
323
324 // Returns true if a page should be added based on it's information in
325 // the metadata
326 function shouldAddPage($page) {
327     // Return false only if the page is marked addToAccessFormats false.
328     // If there is no assertion we assume it should be added.
329     if (isset($page->addToAccessFormats)) {
330         if ("false" == strtolower(trim($page->addToAccessFormats))) {
331             return false;
332         }
333     }
334     
335     return true;
336 }
337
338 ?>