Rename GnuBook to BookReader
[bookreader.git] / BookReaderDemo / BookReaderJSSimple.js
1 // 
2 // This file shows the minimum you need to provide to BookReader to display a book
3 //
4 // Copyright(c)2008-2009 Internet Archive. Software license AGPL version 3.
5
6 // Create the BookReader object
7 br = new BookReader();
8
9 // Return the width of a given page.  Here we assume all images are 800 pixels wide
10 br.getPageWidth = function(index) {
11     return 800;
12 }
13
14 // Return the height of a given page.  Here we assume all images are 1200 pixels high
15 br.getPageHeight = function(index) {
16     return 1200;
17 }
18
19 // We load the images from archive.org -- you can modify this function to retrieve images
20 // using a different URL structure
21 br.getPageURI = function(index) {
22     var leafStr = '000';            
23     var imgStr = (index+1).toString();
24     var re = new RegExp("0{"+imgStr.length+"}$");
25     var url = 'http://www.archive.org/download/BookReader/img/page'+leafStr.replace(re, imgStr) + '.jpg';
26     return url;
27 }
28
29 // Return which side, left or right, that a given page should be displayed on
30 br.getPageSide = function(index) {
31     if (0 == (index & 0x1)) {
32         return 'R';
33     } else {
34         return 'L';
35     }
36 }
37
38 // This function returns the left and right indices for the user-visible
39 // spread that contains the given index.  The return values may be
40 // null if there is no facing page or the index is invalid.
41 br.getSpreadIndices = function(pindex) {   
42     var spreadIndices = [null, null]; 
43     if ('rl' == this.pageProgression) {
44         // Right to Left
45         if (this.getPageSide(pindex) == 'R') {
46             spreadIndices[1] = pindex;
47             spreadIndices[0] = pindex + 1;
48         } else {
49             // Given index was LHS
50             spreadIndices[0] = pindex;
51             spreadIndices[1] = pindex - 1;
52         }
53     } else {
54         // Left to right
55         if (this.getPageSide(pindex) == 'L') {
56             spreadIndices[0] = pindex;
57             spreadIndices[1] = pindex + 1;
58         } else {
59             // Given index was RHS
60             spreadIndices[1] = pindex;
61             spreadIndices[0] = pindex - 1;
62         }
63     }
64     
65     return spreadIndices;
66 }
67
68 // For a given "accessible page index" return the page number in the book.
69 //
70 // For example, index 5 might correspond to "Page 1" if there is front matter such
71 // as a title page and table of contents.
72 br.getPageNum = function(index) {
73     return index+1;
74 }
75
76 // Total number of leafs
77 br.numLeafs = 15;
78
79 // Book title and the URL used for the book title link
80 br.bookTitle= 'Open Library Bookreader Presentation';
81 br.bookUrl  = 'http://openlibrary.org';
82
83 // Let's go!
84 br.init();