├── .gitignore ├── .babelrc ├── .npmignore ├── src ├── index.js ├── matches-selector.js ├── get-bounding-client-rect.js ├── create-toc.js ├── defaults.js ├── page-counters.js ├── paginate-for-print.js ├── cut-content.js └── apply-layout.js ├── README.md ├── bower.json ├── package.json ├── index.html ├── LICENSE.txt ├── demo ├── paginate-for-print-list.html ├── paginate-for-print.html └── paginate-for-print-two-column.html └── bundle └── paginate-for-print.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | dist/ 3 | -------------------------------------------------------------------------------- /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015"] 3 | } 4 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | bundle/ 3 | demo/ 4 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import {PaginateForPrint} from "./paginate-for-print" 2 | 3 | module.exports = function (configValues) { 4 | let paginator = new PaginateForPrint(configValues) 5 | paginator.initiate() 6 | return function() { 7 | paginator.tearDown() 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This project aims to create a simple alternative to cut up content into pages for printing and PDF generation in browsers. 2 | 3 | Check out the demo here. 4 | 5 | License: 6 | The project is licensed under the LGPL v.3 license. 7 | -------------------------------------------------------------------------------- /src/matches-selector.js: -------------------------------------------------------------------------------- 1 | 2 | export function matchesSelector(element, selector) { 3 | 4 | if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) { 5 | // Firefox 6 | return element.mozMatchesSelector(selector) 7 | } else { 8 | // Webkit + Chrome + Edge 9 | return element.webkitMatchesSelector(selector) 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/get-bounding-client-rect.js: -------------------------------------------------------------------------------- 1 | // Chrome (+ possibly others) currently has issues when trying to find the real coordinates of elements when in multicol. 2 | // This is a workaround that uses a range over the elements contents and combines all client rects around it. 3 | 4 | export function getBoundingClientRect(element) { 5 | let r = document.createRange() 6 | r.setStart(element, 0) 7 | r.setEnd(element, element.childNodes.length) 8 | return r.getBoundingClientRect() 9 | } 10 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "paginate-for-print", 3 | "version": "0.0.7", 4 | "homepage": "https://github.com/paginate-for-print", 5 | "authors": [ 6 | "Johannes Wilm " 7 | ], 8 | "description": "This project aims to create a simple alternative to cut up content into pages for printing and PDF generation in browsers.", 9 | "main": "bundle/paginate-for-print.js", 10 | "moduleType": [ 11 | "globals" 12 | ], 13 | "keywords": [ 14 | "pdf", 15 | "print", 16 | "pagination" 17 | ], 18 | "license": "LGPL", 19 | "ignore": [ 20 | "**/.*", 21 | "node_modules", 22 | "bower_components", 23 | "test", 24 | "tests" 25 | ] 26 | } 27 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "paginate-for-print", 3 | "version": "0.0.7", 4 | "author": "Johannes Wilm", 5 | "license": "LGPL", 6 | "main": "dist/index.js", 7 | "devDependencies": { 8 | "blint": "^0.5.0", 9 | "babel-cli": "^6.4.0", 10 | "babel-core": "^6.4.5", 11 | "babel-preset-es2015": "^6.3.13", 12 | "browserify": "13.1.0", 13 | "babelify": "7.3.0" 14 | }, 15 | "jshintConfig": { 16 | "esversion": 6 17 | }, 18 | "scripts": { 19 | "bundle": "browserify src/index.js -t [ babelify --presets [ es2015 ] ] --standalone paginate-for-print > bundle/paginate-for-print.js ", 20 | "build": "babel -d dist src", 21 | "lint": "blint --browser --ecmaVersion 6 --forbidSemicolons src || true", 22 | "prepublish": "npm run lint && npm run build && npm run bundle" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |

Paginate for print

5 |

This project aims to create a simple alternative to cut up content into pages for printing and PDF generation in browsers.

6 |

Demos

7 |

There is a general demo, a two column demo 8 | and a demo involving a list.

9 |

How to use

10 |

Paginate for Print is available as ES6 modules, as CommonJS modules and as the simplest version as a bundled JavaScript file that can be used directly in a browser.

11 |

For examples on how to use it, check the source code of the above mentioned examples. The available configuration options can be found in the list of default config options.

12 |

License

13 |

This project is licensed under the LGPL v.3 license. See the LICENSE.txt file for more information.

14 | 15 | 16 | -------------------------------------------------------------------------------- /src/create-toc.js: -------------------------------------------------------------------------------- 1 | import {matchesSelector} from "./matches-selector" 2 | 3 | export function createToc() { 4 | let tocDiv = document.createElement('div'), 5 | tocTitleH1 = document.createElement('h1'), 6 | tocItems = document.getElementById('pagination-layout').querySelectorAll( 7 | '.pagination-body'), 8 | itemType 9 | 10 | tocDiv.id = 'pagination-toc' 11 | tocTitleH1.id = 'pagination-toc-title' 12 | tocDiv.appendChild(tocTitleH1) 13 | 14 | for (let i = 0; i < tocItems.length; i++) { 15 | if (matchesSelector(tocItems[i], 16 | '.pagination-chapter')) { 17 | itemType = 'chapter' 18 | } else if (matchesSelector(tocItems[i], 19 | '.pagination-section')) { 20 | itemType = 'section' 21 | } else { 22 | continue 23 | } 24 | let tocItemDiv = document.createElement('div') 25 | tocItemDiv.classList.add('pagination-toc-entry') 26 | let tocItemTextSpan = document.createElement('span') 27 | tocItemTextSpan.classList.add('pagination-toc-text') 28 | 29 | tocItemTextSpan.appendChild(document.createTextNode( 30 | tocItems[i].querySelector('.pagination-header-' + 31 | itemType).textContent.trim())) 32 | tocItemDiv.appendChild(tocItemTextSpan) 33 | 34 | let tocItemPnSpan = document.createElement('span') 35 | tocItemPnSpan.classList.add('pagination-toc-pagenumber') 36 | 37 | tocItemPnSpan.appendChild(document.createTextNode(tocItems[ 38 | i].querySelector('.pagination-pagenumber').textContent 39 | .trim())) 40 | 41 | tocItemDiv.appendChild(tocItemPnSpan) 42 | 43 | tocDiv.appendChild(tocItemDiv) 44 | } 45 | 46 | return tocDiv 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/defaults.js: -------------------------------------------------------------------------------- 1 | export const DEFAULT_CONFIG_VALUES = { 2 | // SELECTORS 3 | sectionStartSelector: 'h1', // The CSS selector that marks the start of a new section. 4 | sectionTitleSelector: 'h1', // The CSS selector at a start of a section that marks the title of that section. 5 | chapterStartSelector: 'h2', // The CSS selector that marks the start of a new chapter. 6 | chapterTitleSelector: 'h2', // The CSS selector at a start of a chapter that marks the title of that chapter. 7 | footnoteSelector: '.pagination-footnote', // The CSS selector of elements that are to be converted to footnotes. 8 | pagebreakSelector: '.pagination-pagebreak', // The CSS selector of elements that are to be converted to page breaks. 9 | topfloatSelector: '.pagination-topfloat', // The CSS selector of elements that are to be converted to top floating elements. 10 | // 'marginnoteSelector': '.pagination-marginnote', 11 | 12 | // FLOW ELEMENTS 13 | flowFromElement: false, // An element where to flow from (if false, document.body will be taken) 14 | frontmatterFlowFromElement: false, // An element that holds the contents to be flown into the frontmatter 15 | flowToElement: false, // An element where to flow to (if false, document.body will be taken) 16 | 17 | // LAYOUT OPTIONS 18 | numberPages: true, // Whether to number pages 19 | alwaysEven: true, // Whether every section/chapter always should have an even number of pages 20 | enableFrontmatter: true, // Whether to add frontmatter (Title page, Table-of-Contents, etc.) 21 | // 'enableTableOfFigures': false, 22 | // 'enableTableOfTables': false, 23 | // 'enableMarginNotes': false, 24 | // 'enableCrossReferences': true, 25 | // 'enableWordIndex': true, 26 | 27 | // CALLBACK 28 | callback: function() {}, 29 | 30 | // STYLING OpTIONS (Can be overriden with CSS) 31 | outerMargin: 0.5, 32 | innerMargin: 0.8, 33 | contentsTopMargin: 0.8, 34 | headerTopMargin: 0.3, 35 | contentsBottomMargin: 0.8, 36 | pagenumberBottomMargin: 0.3, 37 | pageHeight: 8.3, 38 | pageWidth: 5.8, 39 | // 'marginNotesWidth': 1.5, 40 | // 'marginNotesSeparatorWidth': 0.09, 41 | // 'marginNotesVerticalSeparatorWidth': 0.09, 42 | lengthUnit: 'in' 43 | } 44 | -------------------------------------------------------------------------------- /src/page-counters.js: -------------------------------------------------------------------------------- 1 | export class PageCounterArab { 2 | // arab is the page counter used by the main body contents. 3 | 4 | /* Create a pagecounter. cssClass is the CSS class employed by this page 5 | * counter to mark all page numbers associated with it. 6 | */ 7 | constructor() { 8 | this.cssClass = 'arab' 9 | this.counterValue = 0 10 | 11 | } 12 | 13 | show() { 14 | /* Standard show function for page counter is to show the value itself 15 | * using arabic numbers. 16 | */ 17 | return this.counterValue 18 | } 19 | 20 | incrementAndShow() { 21 | /* Increment the page count by one and return the reuslt page count 22 | * using the show function. 23 | */ 24 | this.counterValue++ 25 | return this.show() 26 | } 27 | 28 | numberPages() { 29 | /* If the pages associated with this page counter need to be updated, 30 | * go through all of them from the start of the book and number them, 31 | * thereby potentially removing old page numbers. 32 | */ 33 | this.counterValue = 0 34 | 35 | let pagenumbersToNumber = document.querySelectorAll( 36 | '.pagination-page .pagination-pagenumber.pagination-' + 37 | this.cssClass) 38 | for (let i = 0; i < pagenumbersToNumber.length; i++) { 39 | pagenumbersToNumber[i].innerHTML = this.incrementAndShow() 40 | } 41 | } 42 | } 43 | 44 | 45 | 46 | export class PageCounterRoman extends PageCounterArab { 47 | // roman is the page counter used by the frontmatter. 48 | constructor() { 49 | super() 50 | this.cssClass = 'roman' 51 | } 52 | 53 | show() { 54 | // Create roman numeral representations of numbers. 55 | let digits = String(+this.counterValue).split(""), 56 | key = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", 57 | "CM", 58 | "", 59 | "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC", 60 | "", 61 | "I", "II", 62 | "III", "IV", "V", "VI", "VII", "VIII", "IX" 63 | ], 64 | roman = "", 65 | i = 3 66 | while (i--) { 67 | roman = (key[+digits.pop() + (i * 10)] || "") + roman 68 | } 69 | return new Array(+digits.join("") + 1).join("M") + roman 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /src/paginate-for-print.js: -------------------------------------------------------------------------------- 1 | import {DEFAULT_CONFIG_VALUES} from "./defaults" 2 | import {LayoutApplier} from "./apply-layout" 3 | 4 | /*! 5 | * PaginateForPrint 6 | * Copyright 2014-2016 Johannes Wilm. Freely available under the AGPL. For further details see LICENSE.txt 7 | * 8 | */ 9 | 10 | export class PaginateForPrint { 11 | 12 | constructor(config) { 13 | this.config = Object.assign(DEFAULT_CONFIG_VALUES, config) 14 | this.stylesheets = [] 15 | this.layoutApplier = new LayoutApplier(this.config) 16 | } 17 | 18 | initiate() { 19 | /* Initiate PaginateForPrint by setting basic CSS style. and initiating 20 | the layout mechanism. 21 | */ 22 | this.setStyle() 23 | this.setPageStyle() 24 | this.setBrowserSpecifics() 25 | this.layoutApplier.initiate() 26 | } 27 | 28 | setBrowserSpecifics() { 29 | if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) { 30 | let stylesheet = document.createElement('style') 31 | // Small fix for Firefox to not print first two pages on top of oneanother. 32 | stylesheet.innerHTML = ".pagination-page:first-child {page-break-before: always;}" 33 | document.head.appendChild(stylesheet) 34 | this.stylesheets.push(stylesheet) 35 | } 36 | } 37 | 38 | setStyle() { 39 | /* Set style for the regions and pages used by Paginate for Print and add it 40 | * to the head of the DOM. 41 | */ 42 | let stylesheet = document.createElement('style') 43 | let footnoteSelector = this.config['footnoteSelector'] 44 | 45 | stylesheet.innerHTML = ` 46 | .pagination-footnotes ${footnoteSelector} {display: block;} 47 | .pagination-contents ${footnoteSelector} > * {display:none;} 48 | .pagination-main-contents-container ${footnoteSelector}, figure { 49 | -webkit-column-break-inside: avoid; 50 | page-break-inside: avoid; 51 | } 52 | body { 53 | counter-reset: pagination-footnote pagination-footnote-reference; 54 | } 55 | .pagination-contents ${footnoteSelector}::before { 56 | counter-increment: pagination-footnote-reference; 57 | content: counter(pagination-footnote-reference); 58 | } 59 | ${footnoteSelector} > * > *:first-child::before { 60 | counter-increment: pagination-footnote; 61 | content: counter(pagination-footnote); 62 | } 63 | .pagination-page { 64 | position: relative; 65 | } 66 | .pagination-page { 67 | page-break-after: always; 68 | page-break-before: always; 69 | margin-left: auto; 70 | margin-right: auto; 71 | } 72 | .pagination-page:first-child { 73 | page-break-before: avoid; 74 | } 75 | .pagination-page:last-child { 76 | page-break-after: avoid; 77 | } 78 | .pagination-main-contents-container, .pagination-pagenumber, .pagination-header { 79 | position: absolute; 80 | } 81 | li.hide { 82 | list-style-type: none; 83 | } 84 | ` 85 | document.head.appendChild(stylesheet) 86 | this.stylesheets.push(stylesheet) 87 | } 88 | 89 | setPageStyle() { 90 | // Set style for a particular page size. 91 | let unit = this.config['lengthUnit'], 92 | contentsWidthNumber = this.config['pageWidth'] - 93 | this.config['innerMargin'] - this.config['outerMargin'], 94 | contentsWidth = contentsWidthNumber + unit, 95 | contentsHeightNumber = this.config['pageHeight'] - 96 | this.config['contentsTopMargin'] - 97 | this.config['contentsBottomMargin'], 98 | contentsHeight = contentsHeightNumber + unit, 99 | pageWidth = this.config['pageWidth'] + unit, 100 | pageHeight = this.config['pageHeight'] + unit, 101 | contentsBottomMargin = this.config['contentsBottomMargin'] + 102 | unit, 103 | innerMargin = this.config['innerMargin'] + unit, 104 | outerMargin = this.config['outerMargin'] + unit, 105 | pagenumberBottomMargin = this.config 106 | ['pagenumberBottomMargin'] + 107 | unit, 108 | headerTopMargin = this.config['headerTopMargin'] + 109 | unit, 110 | imageMaxHeight = contentsHeightNumber - 0.1 + unit, 111 | footnoteSelector = this.config['footnoteSelector'] 112 | let pageStyleSheet = document.createElement('style') 113 | pageStyleSheet.innerHTML = ` 114 | .pagination-page {height: ${pageHeight}; width: ${pageWidth};background-color: #fff;} 115 | @page {size:${pageWidth} ${pageHeight};} 116 | body {background-color: #efefef; margin:0;} 117 | @media screen{.pagination-page {border:solid 1px #000; margin-bottom:.2in;}} 118 | .pagination-main-contents-container { 119 | width: ${contentsWidth}; 120 | height: ${contentsHeight}; 121 | bottom: ${contentsBottomMargin}; 122 | } 123 | .pagination-contents-container { 124 | bottom: ${contentsBottomMargin}; 125 | height: ${contentsHeight}; 126 | } 127 | .pagination-contents { 128 | height: ${contentsHeight}; 129 | width: ${contentsWidth}; 130 | } 131 | img {max-height: ${imageMaxHeight}; max-width: 100%;} 132 | .pagination-pagenumber { 133 | bottom: ${pagenumberBottomMargin}; 134 | } 135 | .pagination-header { 136 | top: ${headerTopMargin}; 137 | } 138 | .pagination-page:nth-child(odd) .pagination-main-contents-container, 139 | .pagination-page:nth-child(odd) .pagination-pagenumber, 140 | .pagination-page:nth-child(odd) .pagination-header { 141 | right: ${outerMargin}; 142 | left: ${innerMargin}; 143 | } 144 | .pagination-page:nth-child(even) .pagination-main-contents-container, 145 | .pagination-page:nth-child(even) .pagination-pagenumber, 146 | .pagination-page:nth-child(even) .pagination-header { 147 | right: ${innerMargin}; 148 | left: ${outerMargin}; 149 | } 150 | .pagination-page:nth-child(odd) .pagination-pagenumber, 151 | .pagination-page:nth-child(odd) .pagination-header {text-align:right;} 152 | .pagination-page:nth-child(odd) .pagination-header-section {display:none;} 153 | .pagination-page:nth-child(even) .pagination-header-chapter {display:none;} 154 | .pagination-page:nth-child(even) .pagination-pagenumber, 155 | .pagination-page:nth-child(even) .pagination-header { text-align:left;} 156 | ${footnoteSelector} > * > * {font-size: 0.7em; margin:.25em;} 157 | ${footnoteSelector} > * > *::before, ${footnoteSelector}::before { 158 | position: relative; 159 | top: -0.5em; 160 | font-size: 80%; 161 | } 162 | #pagination-toc-title:before { 163 | content:'Contents'; 164 | } 165 | .pagination-toc-entry .pagination-toc-pagenumber {float:right;} 166 | ` 167 | document.head.insertBefore(pageStyleSheet,document.head.firstChild) 168 | this.stylesheets.push(pageStyleSheet) 169 | } 170 | 171 | // Remove stylesheets and all contents of the flow to element. 172 | tearDown() { 173 | // Remove stylesheets from DOM 174 | this.stylesheets.forEach(function(stylesheet){ 175 | stylesheet.parentNode.removeChild(stylesheet) 176 | }) 177 | let flowToElement = this.config['flowToElement'] ? this.config['flowToElement'] : document.body 178 | while (flowToElement.firstChild) { 179 | flowToElement.removeChild(flowToElement.firstChild) 180 | } 181 | } 182 | 183 | } 184 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | -------------------------------------------------------------------------------- /demo/paginate-for-print-list.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 13 | 14 | 15 | 16 | 17 |
18 |

This is an introduction.

19 |
    20 |
  1. An item
  2. 21 |
  3. An item
  4. 22 |
  5. An item
  6. 23 |
  7. An item
  8. 24 |
  9. An item
  10. 25 |
  11. An item
  12. 26 |
  13. An item
  14. 27 |
  15. An item
  16. 28 |
  17. An item
  18. 29 |
  19. 10 An item
  20. 30 |
  21. An item
  22. 31 |
  23. An item
  24. 32 |
  25. An item
  26. 33 |
  27. An item
  28. 34 |
  29. An item
  30. 35 |
  31. An item
  32. 36 |
  33. An item
  34. 37 |
  35. An item
  36. 38 |
  37. An item
  38. 39 |
  39. 20 An item
  40. 40 |
  41. An item
  42. 41 |
  43. An item
  44. 42 |
  45. An item
  46. 43 |
  47. An item
  48. 44 |
  49. An item
  50. 45 |
  51. An item
  52. 46 |
  53. An item
  54. 47 |
  55. An item
  56. 48 |
  57. An item
  58. 49 |
  59. 30 An item
  60. 50 |
  61. An item
  62. 51 |
  63. An item
  64. 52 |
  65. An item. And the first on the page
  66. 53 |
  67. An item
  68. 54 |
  69. An item
  70. 55 |
  71. An item
  72. 56 |
  73. An item
  74. 57 |
  75. An item
  76. 58 |
  77. An item
  78. 59 |
  79. 40 An item
  80. 60 |
  81. An item
  82. 61 |
  83. An item
  84. 62 |
  85. An item
  86. 63 |
  87. An item
  88. 64 |
  89. An item
  90. 65 |
  91. An item
  92. 66 |
  93. An item
  94. 67 |
  95. An item
  96. 68 |
  97. An item
  98. 69 |
  99. 50 An item
  100. 70 |
  101. An item
  102. 71 |
  103. An item
  104. 72 |
  105. An item
  106. 73 |
  107. An item
  108. 74 |
  109. An item
  110. 75 |
  111. An item
  112. 76 |
  113. An item
  114. 77 |
  115. An item
  116. 78 |
  117. An item
  118. 79 |
  119. 60 - An item
  120. 80 |
  121. An item
  122. 81 |
  123. An item
  124. 82 |
  125. An item
  126. 83 |
  127. An item
  128. 84 |
  129. An item
  130. 85 |
  131. An item, which will go on and on and on and on, until it is broken 86 | across the page. An item, which will go on and on and on and on, until 87 | it is broken across the page. An item, which will go on and on and on 88 | and on, until it is broken across the page.
  132. 89 |
  133. An item
  134. 90 |
  135. An item
  136. 91 |
  137. An item
  138. 92 |
  139. 70 - An item
  140. 93 |
  141. An item
  142. 94 |
  143. An item
  144. 95 |
  145. An item
  146. 96 |
  147. An item
  148. 97 |
  149. An item
  150. 98 |
  151. An item
  152. 99 |
  153. An item
  154. 100 |
  155. An item
  156. 101 |
  157. An item
  158. 102 |
  159. 80 - An item
  160. 103 |
  161. An item
  162. 104 |
  163. An item
  164. 105 |
  165. An item
  166. 106 |
  167. An item
  168. 107 |
  169. An item
  170. 108 |
  171. An item
  172. 109 |
  173. An item
  174. 110 |
  175. An item
  176. 111 |
  177. An item
  178. 112 |
  179. 90 - An item
  180. 113 |
  181. An item
  182. 114 |
  183. An item
  184. An item
  185. 115 |
  186. An item
  187. 116 |
  188. An item
  189. 117 |
  190. An item
  191. 118 |
  192. An item
  193. 119 |
  194. An item
  195. 120 |
  196. An item
  197. 121 |
  198. 100 - An item
  199. 122 |
  200. An item
  201. 123 |
  202. An item
  203. 124 |
  204. An item
  205. 125 |
  206. An item
  207. 126 |
  208. An item
  209. 127 |
  210. An item
  211. 128 |
  212. An item
  213. 129 |
  214. An item
  215. 130 |
  216. An item
  217. 131 |
  218. 110 - An item
  219. 132 |
  220. An item
  221. 133 |
  222. An item
  223. 134 |
  224. An item
  225. 135 |
  226. An item
  227. 136 |
  228. An item
  229. 137 |
  230. An item
  231. 138 |
  232. An item
  233. 139 |
  234. An item
  235. 140 |
  236. An item
  237. 141 |
  238. 120 - An item
  239. 142 |
  240. An item
  241. 143 |
  242. An item
  243. 144 |
  244. An item
  245. 145 |
  246. An item
  247. 146 |
  248. An item
  249. An item
  250. 147 |
  251. An item
  252. 148 |
  253. An item
  254. 149 |
  255. An item
  256. 150 |
  257. 130 - An item
  258. 151 |
  259. An item
  260. 152 |
  261. An item
  262. 153 |
  263. An item
  264. 154 |
  265. An item
  266. 155 |
  267. An item
  268. 156 |
  269. An item
  270. 157 |
  271. An item
  272. 158 |
  273. An item
  274. 159 |
  275. An item
  276. 160 |
  277. 140 An item
  278. 161 |
  279. An item
  280. 162 |
  281. An item
  282. 163 |
  283. An item
  284. 164 |
  285. An item
  286. 165 |
  287. An item
  288. 166 |
  289. An item
  290. 167 |
  291. An item
  292. 168 |
  293. An item
  294. 169 |
  295. An item
  296. 170 |
  297. 150 An item
  298. 171 |
  299. An item
  300. 172 |
  301. An item
  302. 173 |
  303. An item
  304. 174 |
  305. An item
  306. 175 |
  307. An item
  308. 176 |
  309. An item
  310. 177 |
  311. An item
  312. 178 |
  313. An item
  314. 179 |
  315. An item
  316. 180 |
  317. 160 An item
  318. 181 |
  319. An item
  320. 182 |
  321. An item
  322. 183 |
  323. An item
  324. 184 |
  325. An item
  326. 185 |
  327. An item
  328. 186 |
  329. An item
  330. 187 |
  331. An item
  332. 188 |
  333. An item
  334. 189 |
  335. An item
  336. 190 |
  337. 170 An item
  338. 191 |
  339. An item
  340. 192 |
  341. An item
  342. 193 |
  343. An item
  344. 194 |
  345. An item
  346. 195 |
  347. An item
  348. 196 |
  349. An item
  350. 197 |
  351. An item
  352. 198 |
  353. An item
  354. 199 |
  355. An item
  356. 200 |
  357. 180 An item
  358. 201 |
  359. An item
  360. 202 |
  361. An item
  362. 203 |
  363. An item
  364. 204 |
  365. An item
  366. 205 |
  367. An item
  368. 206 |
  369. An item
  370. 207 |
  371. An item
  372. 208 |
    1. 209 |
    2. A subitem
    3. 210 |
    4. A subitem that goes on and on, trying to find the break. A 211 | subitem that goes on and on, trying to find the break. A subitem 212 | that goes on and on, trying to find the break. A subitem that goes 213 | on and on, trying to find the break. A subitem that goes on and 214 | on, trying to find the break. A subitem that goes on and on, 215 | trying to find the break. A subitem that goes on and on, trying to 216 | find the break. A subitem that goes on and on, trying to find the 217 | break. A subitem that goes on and on, trying to find the break. A 218 | subitem that goes on and on, trying to find the break. A subitem 219 | that goes on and on, trying to find the break. A subitem that goes 220 | on and on, trying to find the break. A subitem that goes on and 221 | on, trying to find the break. A subitem that goes on and on, 222 | trying to find the break. A subitem that goes on and on, trying to 223 | find the break. A subitem that goes on and on, trying to find the 224 | break. A subitem that goes on and on, trying to find the break. 225 |
    5. 226 |
    6. A subitem
    7. 227 |
    8. A subitem
    9. 228 |
    10. A subitem
    11. 229 |
    12. A subitem
    13. 230 |
  373. 231 |
  374. An item
  375. 232 |
  376. 190 An item
  377. 233 |
  378. An item
  379. 234 |
  380. An item
  381. 235 |
236 |
237 | 238 | 239 | 240 | -------------------------------------------------------------------------------- /src/cut-content.js: -------------------------------------------------------------------------------- 1 | import {getBoundingClientRect} from "./get-bounding-client-rect" 2 | 3 | export class ContentCutter { 4 | 5 | constructor (config) { 6 | this.config = config 7 | } 8 | 9 | // main cut method 10 | cutToFit(contents) { 11 | 12 | let range, overflow, manualPageBreak, 13 | ignoreLastLIcut = false, 14 | cutLIs, pageBreak, 15 | // contentHeight = height of page - height of top floats - height of footnotes. 16 | contentHeight = (contents.parentElement.clientHeight - 17 | contents.previousSibling.clientHeight - contents.nextSibling 18 | .clientHeight), 19 | contentWidth = contents.parentElement.clientWidth, 20 | boundingRect, rightCutOff 21 | 22 | // set height to contentHeight 23 | contents.style.height = contentHeight + "px" 24 | 25 | if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) { 26 | // Firefox has some insane bug which means that the new content height 27 | // isn't applied immediately when dealing with multicol -- unless one 28 | // removes the content and re-adds it. 29 | let nSib = contents.nextSibling 30 | let pEl = contents.parentElement 31 | pEl.removeChild(contents) 32 | pEl.insertBefore(contents,nSib) 33 | } 34 | 35 | // Set height temporarily to "auto" so the page flows beyond where 36 | // it should end and we can find the page break. 37 | contents.style.width = (contentWidth * 2 + 100) + 'px' 38 | contents.style.columnWidth = contentWidth + 'px' 39 | contents.style.columnGap = '100px' 40 | contents.style.columnFill = 'auto' 41 | 42 | contents.style.MozColumnWidth = contentWidth + 'px' 43 | contents.style.MozColumnGap = '100px' 44 | contents.style.MozColumnFill = 'auto' 45 | 46 | boundingRect = contents.getBoundingClientRect() 47 | rightCutOff = boundingRect.left + contentWidth + 20 48 | 49 | 50 | manualPageBreak = contents.querySelector(this.config[ 51 | 'pagebreakSelector']) 52 | 53 | if (manualPageBreak && manualPageBreak.getBoundingClientRect().left < 54 | rightCutOff) { 55 | range = document.createRange() 56 | range.setStartBefore(manualPageBreak) 57 | } else if (boundingRect.right <= rightCutOff) { 58 | contents.style.width = contentWidth + "px" 59 | return false 60 | } else { 61 | pageBreak = this.findPageBreak(contents, rightCutOff) 62 | if (!pageBreak) { 63 | contents.style.width = contentWidth + "px" 64 | return false 65 | } 66 | range = document.createRange() 67 | range.setStart(pageBreak.node, pageBreak.offset) 68 | } 69 | 70 | contents.style.width = contentWidth + "px" 71 | // We find that the first item is an OL/UL which may have started on the previous page. 72 | if (['OL','UL'].indexOf(range.startContainer.nodeName) !== -1 || range.startContainer.nodeName === 73 | '#text' && range.startContainer.parentNode && 74 | ['OL','UL'].indexOf(range.startContainer.parentNode.nodeName) !== -1 && 75 | range.startContainer.length === range.startOffset) { 76 | // We are cutting from inside a List, don't touch the innermost list items. 77 | ignoreLastLIcut = true 78 | } 79 | range.setEndAfter(contents.lastChild) 80 | overflow = range.extractContents() 81 | cutLIs = this.countOLItemsAndFixLI(contents) 82 | if (ignoreLastLIcut) { 83 | // Because the cut happened exactly between two LI items, don't try to unify the two lowest level LIs. 84 | cutLIs[cutLIs.length - 1].hideFirstLI = false 85 | if (cutLIs[cutLIs.length - 1].start) { 86 | cutLIs[cutLIs.length - 1].start++ 87 | } 88 | } 89 | this.applyInitialOLcount(overflow, cutLIs) 90 | 91 | if (!contents.lastChild || (contents.textContent.trim().length === 92 | 0 && contents.querySelectorAll('img,svg,canvas').length === 93 | 0)) { 94 | contents.appendChild(overflow) 95 | overflow = false 96 | } 97 | return overflow 98 | } 99 | 100 | 101 | countOLItemsAndFixLI(element, countList) { 102 | let start = 1, 103 | hideFirstLI = false 104 | 105 | if (typeof countList === 'undefined') { 106 | countList = [] 107 | } 108 | if (element.nodeName === 'OL') { 109 | if (element.hasAttribute('start')) { 110 | start = parseInt(element.getAttribute('start')) 111 | } 112 | if (element.lastElementChild.textContent.length === 0) { 113 | element.removeChild(element.lastElementChild) 114 | } else { 115 | start-- 116 | hideFirstLI = true 117 | } 118 | countList.push({ 119 | start: start + element.childElementCount, 120 | hideFirstLI: hideFirstLI 121 | }) 122 | } else if (element.nodeName === 'UL') { 123 | if (element.lastElementChild.textContent.length === 0) { 124 | element.removeChild(element.lastElementChild) 125 | } else { 126 | hideFirstLI = true 127 | } 128 | countList.push({ 129 | hideFirstLI: hideFirstLI 130 | }) 131 | } 132 | 133 | if (element.childElementCount > 0) { 134 | return this.countOLItemsAndFixLI(element.lastElementChild, countList) 135 | } else { 136 | return countList 137 | } 138 | 139 | } 140 | 141 | applyInitialOLcount(element, countList) { 142 | if (element.nodeName === '#document-fragment') { 143 | element = element.childNodes[0] 144 | } 145 | let listCount 146 | if (countList.length === 0) { 147 | return 148 | } 149 | if (element.nodeName === 'OL') { 150 | listCount = countList.shift() 151 | element.setAttribute('start', listCount.start) 152 | if (listCount.hideFirstLI) { 153 | element.firstElementChild.classList.add('hide') 154 | } 155 | } else if (element.nodeName === 'UL') { 156 | listCount = countList.shift() 157 | if (listCount.hideFirstLI) { 158 | element.firstElementChild.classList.add('hide') 159 | } 160 | } 161 | if (element.childElementCount > 0) { 162 | this.applyInitialOLcount(element.firstElementChild, countList) 163 | } else { 164 | return 165 | } 166 | } 167 | 168 | findPrevNode(node) { 169 | if (node.previousSibling) { 170 | return node.previousSibling 171 | } else { 172 | return this.findPrevNode(node.parentElement) 173 | } 174 | } 175 | 176 | // Go through a node (contents) and find the exact position where it goes 177 | // further to the right than the right cutoff. 178 | findPageBreak(contents, rightCutOff) { 179 | let contentCoords, found, prevNode 180 | if (contents.nodeType === 1) { 181 | contentCoords = getBoundingClientRect(contents) 182 | if (contentCoords.left < rightCutOff) { 183 | if (contentCoords.right > rightCutOff) { 184 | found = false 185 | let i = 0 186 | while (found === false && i < contents.childNodes.length) { 187 | found = this.findPageBreak(contents.childNodes[ 188 | i], rightCutOff) 189 | i++ 190 | } 191 | if (found) { 192 | return found 193 | } 194 | } else { 195 | return false 196 | } 197 | } 198 | prevNode = this.findPrevNode(contents) 199 | return { 200 | node: prevNode, 201 | offset: prevNode.length ? prevNode.length : prevNode.childNodes.length 202 | } 203 | 204 | } else if (contents.nodeType === 3) { 205 | let range = document.createRange(), 206 | offset = contents.length 207 | range.setStart(contents, 0) 208 | range.setEnd(contents, offset) 209 | contentCoords = range.getBoundingClientRect() 210 | 211 | if (contentCoords.bottom === contentCoords.top) { 212 | // A text node that doesn't have any output. 213 | return false 214 | } else if (contentCoords.left < rightCutOff) { 215 | if (contentCoords.right > rightCutOff) { 216 | found = false 217 | while (found === false && offset > 0) { 218 | offset-- 219 | range.setEnd(contents, offset) 220 | contentCoords = range.getBoundingClientRect() 221 | if (contentCoords.right <= rightCutOff) { 222 | found = { 223 | node: contents, 224 | offset: offset 225 | } 226 | } 227 | } 228 | if (found) { 229 | return found 230 | } 231 | 232 | } else { 233 | return false 234 | } 235 | } 236 | prevNode = this.findPrevNode(contents) 237 | return { 238 | node: prevNode, 239 | offset: prevNode.length ? prevNode.length : prevNode.childNodes 240 | .length 241 | } 242 | } else { 243 | return false 244 | } 245 | } 246 | 247 | 248 | 249 | } 250 | -------------------------------------------------------------------------------- /demo/paginate-for-print.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 23 | 24 | 25 | 26 |
27 |
28 |

The Malay Archipelago

29 | 30 |
31 | Malay Archipelago Chief's House 32 |
33 | Malay Archipelago Chief's House and Rice-shed in a Sumatran Village 34 |
35 |
36 |
37 |

The Malay Archipelago is a book by the British naturalist Alfred Russel Wallace that chronicles his scientific exploration, during the eight-year period 1854 to 1862, of the southern portion of the Malay Archipelago including Malaysia, Singapore, 38 | the islands of Indonesia, then known as the Dutch East Indies, and the island of New Guinea. It was published in two volumes in 1869, delayed by Wallace's ill health and the work needed to describe the many specimens he brought 39 | home. The book went through ten editions in the nineteenth century; it has been reprinted many times since, and has been translated into at least eight languages.

40 |

The book described each island that he visited in turn, giving a detailed account of its physical and human geography, its volcanoes, and the variety of animals of plants that he found and collected. At the same time, he describes his 41 | experiences, the difficulties of travel, and the help he received from the different peoples that he met. The preface notes that he travelled over 14,000 miles and collected 125,660 natural history specimens, mostly of insects though 42 | also with thousands of molluscs, birds, mammals and reptiles.

43 |

The Malay Archipelago attracted many reviews, with interest from scientific, geographic, church and general periodicals. Reviewers noted and sometimes disagreed with various of his theories, especially the division of fauna and flora along 44 | what soon became known as the Wallace line, natural selection and uniformitarianism. Nearly all agreed that he had provided an interesting and comprehensive account of the geography, natural history, and peoples of the 45 | archipelago, which was little known to their readers at the time, and that he had collected an astonishing number of specimens. The book is much cited, and is Wallace's most successful, both commercially and as a piece of literature.

46 |

Context

47 |

In 1847, Wallace and his friend Henry Walter Bates, both in their early twenties,Bates was 22, Wallace was 24. 48 | agreed that they would jointly make a collecting trip to the Amazon "towards solving the problem of origin of species";Mallet, Jim. "Henry Walter Bates". University College London. Retrieved December 11, 2012. 49 | Charles Darwin's book on the Origin of Species was not published until 11 years later, in 1859, itself precipitated by a famous letter from Wallace which described the theory in outline.Shoumatoff, Alex (22 August 1988). "A Critic at Large, Henry Walter Bates". New Yorker. 50 | They had been inspired by reading the American entomologist William Henry Edwards's pioneering 1847 book A Voyage Up the River Amazon, with a residency at Pará.Edwards, 1847. 51 | Bates stayed in the Amazons for 11 years, going on to write The Naturalist on the River Amazons (1863); Wallace, ill with fever, went home in 1852 with thousands of specimens, some for science and some for sale. The ship and his collection 52 | were destroyed by fire at sea near the Guianas. Rather than giving up, Wallace wrote about the Amazon in both prose and poetry, and then set sail again, this time for the Malay Archipelago.Edwards, 1847. 53 | 54 |

55 |

Publication

56 |

The Malay Archipelago was first published in 1869 in two volumes by Macmillan (London), and the same year in one volume by Harper & Brothers (New York). Wallace returned to England in 1862, but explains in the Preface that given the 57 | large quantity of specimens and his poor health after his stay in the tropics, it took a long time. He noted that he could at once have printed his notes and journals, but felt that doing that would have been disappointing and unhelpful. Instead, 58 | therefore, he waited until he had published papers on his discoveries, and other scientists had described and named as new species some 2,000 of his beetles (Coleoptera), and over 900 Hymenoptera including 200 new species of ant. 59 | Wallace, 1869. pp. vii–ix. 61 | The book went through 10 editions, with the last published in 1890.

62 |

Overview

63 | 64 |
65 | Wallace's map 66 |
67 | Fold-out coloured map at front of book, showing Wallace's travels around the archipelago 68 |
69 |
70 |
71 |

The preface summarizes Wallace’s travels, the thousands of specimens he collected, and some of the results from their analysis after his return to England. In the preface he notes that he travelled over 14,000 miles and collected 125,660 72 | specimens, mostly of insects: 83,200 beetles, 13,100 butterflies and moths, 13,400 other insects. He also returned to England 7,500 "shells" (such as molluscs), 8,050 birds, 310 mammals and 100 reptiles. 73 | Wallace, 1869. p. xiv. 75 | 76 |

77 |

The book is dedicated to Charles Darwin, but as Wallace explains in the preface, he has chosen to avoid discussing the evolutionary implications of his discoveries. Instead he confines himself to the "interesting facts of the problem, 78 | whose solution is to be found in the principles developed by Mr. Darwin",Wallace, 1869. p. xii. 79 | so from a scientific point of view, the book is largely a descriptive natural history. This modesty belies the fact that while in Sarawak in 1855 Wallace wrote the paper On the Law which has Regulated the Introduction of New Species, 80 | concluding with the evolutionary "Sarawak Law", "Every species has come into existence coincident both in space and time with a closely allied species", three years before he fatefully wrote to Darwin proposing the concept of natural selection. 81 | Wallace, Alfred Russel (1855). "On the Law Which has Regulated the Introduction of Species". Western Kentucky University. Retrieved 22 March 2013. 83 | 84 |

85 |

The first chapter describes the physical geography and geology of the islands with particular attention to the role of volcanoes and earthquakes. It also discusses the overall pattern of the flora and fauna including the fact that 86 | the islands can be divided, by what would eventually become known as the Wallace line, into two parts, those whose animals are more closely related to those of Asia and those whose fauna is closer to that of Australia.

87 |

The following chapters describe in detail the places Wallace visited. Wallace includes numerous observations on the people, their languages, ways of living, and social organization, as well as on the plants and animals found in each location. 88 | He talks about the biogeographic patterns he observes and their implications for natural history, in terms both of the movement of speciesImplying adaptive radiation. 89 | and of the geologic history of the region. He also narrates some of his personal experiences during his travels. The final chapter is an overview of the ethnic, linguistic, and cultural divisions among the people who live in the region and 90 | speculation about what such divisions might indicate about their history.

91 |

Illustrations

92 | 93 |
94 | Great-shielded Grasshopper 95 |
96 | "Great-shielded Grasshopper" drawn and signed by E. W. Robinson 97 |
98 |
99 |
100 |

The illustrations are, according to the Preface, made from Wallace's own sketches, photographs, or specimens. Wallace thanks Walter and Henry Woodbury for some photographs of scenery and native people. He acknowledges William Wilson Saunders 101 | and Mr Pascoe for horned flies and very rare Longhorn beetles: all the rest were from his own enormous collection.

102 | 103 |
104 | 105 | 106 | 107 | -------------------------------------------------------------------------------- /demo/paginate-for-print-two-column.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 13 | 14 | 31 | 32 | 33 | 34 |
35 |
36 |

The Malay Archipelago

37 | 38 |
39 | Malay Archipelago Chief's House 40 |
41 | Malay Archipelago Chief's House and Rice-shed in a Sumatran Village 42 |
43 |
44 |
45 |

The Malay Archipelago is a book by the British naturalist Alfred Russel Wallace that chronicles his scientific exploration, during the eight-year period 1854 to 1862, of the southern portion of the Malay Archipelago including Malaysia, Singapore, 46 | the islands of Indonesia, then known as the Dutch East Indies, and the island of New Guinea. It was published in two volumes in 1869, delayed by Wallace's ill health and the work needed to describe the many specimens he brought 47 | home. The book went through ten editions in the nineteenth century; it has been reprinted many times since, and has been translated into at least eight languages.

48 |

The book described each island that he visited in turn, giving a detailed account of its physical and human geography, its volcanoes, and the variety of animals of plants that he found and collected. At the same time, he describes his 49 | experiences, the difficulties of travel, and the help he received from the different peoples that he met. The preface notes that he travelled over 14,000 miles and collected 125,660 natural history specimens, mostly of insects though 50 | also with thousands of molluscs, birds, mammals and reptiles.

51 |

The Malay Archipelago attracted many reviews, with interest from scientific, geographic, church and general periodicals. Reviewers noted and sometimes disagreed with various of his theories, especially the division of fauna and flora along 52 | what soon became known as the Wallace line, natural selection and uniformitarianism. Nearly all agreed that he had provided an interesting and comprehensive account of the geography, natural history, and peoples of the 53 | archipelago, which was little known to their readers at the time, and that he had collected an astonishing number of specimens. The book is much cited, and is Wallace's most successful, both commercially and as a piece of literature.

54 |

Context

55 |

In 1847, Wallace and his friend Henry Walter Bates, both in their early twenties,Bates was 22, Wallace was 24. 56 | agreed that they would jointly make a collecting trip to the Amazon "towards solving the problem of origin of species";Mallet, Jim. "Henry Walter Bates". University College London. Retrieved December 11, 2012. 57 | Charles Darwin's book on the Origin of Species was not published until 11 years later, in 1859, itself precipitated by a famous letter from Wallace which described the theory in outline.Shoumatoff, Alex (22 August 1988). "A Critic at Large, Henry Walter Bates". New Yorker. 58 | They had been inspired by reading the American entomologist William Henry Edwards's pioneering 1847 book A Voyage Up the River Amazon, with a residency at Pará.Edwards, 1847. 59 | Bates stayed in the Amazons for 11 years, going on to write The Naturalist on the River Amazons (1863); Wallace, ill with fever, went home in 1852 with thousands of specimens, some for science and some for sale. The ship and his collection 60 | were destroyed by fire at sea near the Guianas. Rather than giving up, Wallace wrote about the Amazon in both prose and poetry, and then set sail again, this time for the Malay Archipelago.Edwards, 1847. 61 | 62 |

63 |

Publication

64 |

The Malay Archipelago was first published in 1869 in two volumes by Macmillan (London), and the same year in one volume by Harper & Brothers (New York). Wallace returned to England in 1862, but explains in the Preface that given the 65 | large quantity of specimens and his poor health after his stay in the tropics, it took a long time. He noted that he could at once have printed his notes and journals, but felt that doing that would have been disappointing and unhelpful. Instead, 66 | therefore, he waited until he had published papers on his discoveries, and other scientists had described and named as new species some 2,000 of his beetles (Coleoptera), and over 900 Hymenoptera including 200 new species of ant. 67 | Wallace, 1869. pp. vii–ix. 69 | The book went through 10 editions, with the last published in 1890.

70 |

Overview

71 | 72 |
73 | Wallace's map 74 |
75 | Fold-out coloured map at front of book, showing Wallace's travels around the archipelago 76 |
77 |
78 |
79 |

The preface summarizes Wallace’s travels, the thousands of specimens he collected, and some of the results from their analysis after his return to England. In the preface he notes that he travelled over 14,000 miles and collected 125,660 80 | specimens, mostly of insects: 83,200 beetles, 13,100 butterflies and moths, 13,400 other insects. He also returned to England 7,500 "shells" (such as molluscs), 8,050 birds, 310 mammals and 100 reptiles. 81 | Wallace, 1869. p. xiv. 83 | 84 |

85 |

The book is dedicated to Charles Darwin, but as Wallace explains in the preface, he has chosen to avoid discussing the evolutionary implications of his discoveries. Instead he confines himself to the "interesting facts of the problem, 86 | whose solution is to be found in the principles developed by Mr. Darwin",Wallace, 1869. p. xii. 87 | so from a scientific point of view, the book is largely a descriptive natural history. This modesty belies the fact that while in Sarawak in 1855 Wallace wrote the paper On the Law which has Regulated the Introduction of New Species, 88 | concluding with the evolutionary "Sarawak Law", "Every species has come into existence coincident both in space and time with a closely allied species", three years before he fatefully wrote to Darwin proposing the concept of natural selection. 89 | Wallace, Alfred Russel (1855). "On the Law Which has Regulated the Introduction of Species". Western Kentucky University. Retrieved 22 March 2013. 91 | 92 |

93 |

The first chapter describes the physical geography and geology of the islands with particular attention to the role of volcanoes and earthquakes. It also discusses the overall pattern of the flora and fauna including the fact that 94 | the islands can be divided, by what would eventually become known as the Wallace line, into two parts, those whose animals are more closely related to those of Asia and those whose fauna is closer to that of Australia.

95 |

The following chapters describe in detail the places Wallace visited. Wallace includes numerous observations on the people, their languages, ways of living, and social organization, as well as on the plants and animals found in each location. 96 | He talks about the biogeographic patterns he observes and their implications for natural history, in terms both of the movement of speciesImplying adaptive radiation. 97 | and of the geologic history of the region. He also narrates some of his personal experiences during his travels. The final chapter is an overview of the ethnic, linguistic, and cultural divisions among the people who live in the region and 98 | speculation about what such divisions might indicate about their history.

99 |

Illustrations

100 | 101 |
102 | Great-shielded Grasshopper 103 |
104 | "Great-shielded Grasshopper" drawn and signed by E. W. Robinson 105 |
106 |
107 |
108 |

The illustrations are, according to the Preface, made from Wallace's own sketches, photographs, or specimens. Wallace thanks Walter and Henry Woodbury for some photographs of scenery and native people. He acknowledges William Wilson Saunders 109 | and Mr Pascoe for horned flies and very rare Longhorn beetles: all the rest were from his own enormous collection.

110 | 111 |
112 | 113 | 114 | 115 | -------------------------------------------------------------------------------- /src/apply-layout.js: -------------------------------------------------------------------------------- 1 | import {matchesSelector} from "./matches-selector" 2 | import {ContentCutter} from "./cut-content" 3 | import {PageCounterArab, PageCounterRoman} from "./page-counters" 4 | import {createToc} from "./create-toc" 5 | 6 | export class LayoutApplier { 7 | 8 | constructor(config) { 9 | this.config = config 10 | this.bodyFlowObjects = [] 11 | //this.currentChapter = false 12 | //this.currentSection = false 13 | this.currentFragment = -1 14 | 15 | /* pageCounters contains all the page counters we use in a book -- 16 | * typically these are two -- roman for the frontmatter and arab for the main 17 | * body contents. 18 | */ 19 | this.pageCounters = { 20 | arab: new PageCounterArab(), 21 | roman: new PageCounterRoman() 22 | } 23 | 24 | this.cutter = new ContentCutter(this.config) 25 | 26 | } 27 | 28 | initiate() { 29 | // Create div for layout 30 | let layoutDiv = document.createElement('div'), 31 | flowedElement = this.config['flowFromElement'] ? this.config['flowFromElement'] : document.body, 32 | chapterStartSelector = this.config['chapterStartSelector'], 33 | sectionStartSelector = this.config['sectionStartSelector'], 34 | dividerSelector = chapterStartSelector + ',' + sectionStartSelector, 35 | dividers = flowedElement.querySelectorAll(dividerSelector), 36 | range = document.createRange(), nextChapter = false, 37 | nextSection = false, 38 | flowTo = this.config['flowToElement'] ? this.config['flowToElement'] : document.body 39 | 40 | layoutDiv.id = 'pagination-layout' 41 | for (let i = 0; i < dividers.length; i++) { 42 | let flowObject = { 43 | chapter: false, 44 | section: false 45 | } 46 | if (nextChapter) { 47 | flowObject.chapter = nextChapter 48 | nextChapter = false 49 | } 50 | if (nextSection) { 51 | flowObject.section = nextSection 52 | nextSection = false 53 | } 54 | range.setStart(flowedElement.firstChild, 0) 55 | range.setEnd(dividers[i], 0) 56 | flowObject.fragment = range.extractContents() 57 | this.bodyFlowObjects.push(flowObject) 58 | 59 | let extraElement = flowObject.fragment.querySelectorAll( 60 | dividerSelector)[1] 61 | if (extraElement && extraElement.parentElement) { 62 | extraElement.parentElement.removeChild(extraElement) 63 | } 64 | if (matchesSelector(dividers[i], 65 | chapterStartSelector)) { 66 | let tempNode = flowedElement.querySelector(this.config['chapterTitleSelector']) 67 | if (!tempNode) { 68 | tempNode = document.createElement('div') 69 | } 70 | tempNode = tempNode.cloneNode(true) 71 | nextChapter = document.createDocumentFragment() 72 | while (tempNode.firstChild) { 73 | nextChapter.appendChild(tempNode.firstChild) 74 | } 75 | } else { 76 | let tempNode = flowedElement.querySelector(this.config['sectionTitleSelector']).cloneNode(true) 77 | nextSection = document.createDocumentFragment() 78 | while (tempNode.firstChild) { 79 | nextSection.appendChild(tempNode.firstChild) 80 | } 81 | } 82 | 83 | if (i === 0) { 84 | if (flowObject.fragment.textContent.trim().length === 85 | 0 && flowObject.fragment.querySelectorAll( 86 | 'img,svg,canvas,hr').length === 0) { 87 | this.bodyFlowObjects.pop() 88 | } 89 | } 90 | } 91 | 92 | let flowObject = { 93 | chapter: false, 94 | section: false 95 | } 96 | if (nextChapter) { 97 | flowObject.chapter = nextChapter 98 | } 99 | if (nextSection) { 100 | flowObject.section = nextSection 101 | } 102 | 103 | flowObject.fragment = document.createDocumentFragment() 104 | 105 | while (flowedElement.firstChild) { 106 | flowObject.fragment.appendChild(flowedElement.firstChild) 107 | } 108 | 109 | 110 | this.bodyFlowObjects.push(flowObject) 111 | 112 | flowTo.appendChild(layoutDiv) 113 | 114 | this.paginateDivision(layoutDiv, 'arab') 115 | 116 | } 117 | 118 | paginateDivision(layoutDiv, pageCounterStyle) { 119 | if (++this.currentFragment < this.bodyFlowObjects.length) { 120 | let newContainer = document.createElement('div') 121 | layoutDiv.appendChild(newContainer) 122 | newContainer.classList.add('pagination-body') 123 | newContainer.classList.add('pagination-body-' + this.currentFragment) 124 | if (this.bodyFlowObjects[this.currentFragment].section) { 125 | this.currentSection = this.bodyFlowObjects[ 126 | this.currentFragment].section 127 | newContainer.classList.add('pagination-section') 128 | } 129 | if (this.bodyFlowObjects[this.currentFragment].chapter) { 130 | this.currentChapter = this.bodyFlowObjects[ 131 | this.currentFragment].chapter 132 | newContainer.classList.add('pagination-chapter') 133 | } 134 | this.flowElement(this.bodyFlowObjects[ 135 | this.currentFragment].fragment, 136 | newContainer, pageCounterStyle, this.bodyFlowObjects[ 137 | this.currentFragment].section, 138 | this.bodyFlowObjects[this.currentFragment].chapter 139 | ) 140 | } else { 141 | this.currentChapter = false 142 | this.currentSection = false 143 | this.pageCounters[pageCounterStyle].numberPages() 144 | if (this.config['enableFrontmatter']) { 145 | layoutDiv.insertBefore(document.createElement('div'), 146 | layoutDiv.firstChild) 147 | layoutDiv.firstChild.classList.add( 148 | 'pagination-frontmatter') 149 | let flowObject = { 150 | fragment: document.createDocumentFragment() 151 | } 152 | if (this.config['frontmatterFlowFromElement']) { 153 | let fmNode = this.config['frontmatterFlowFromElement'] 154 | while (fmNode.firstChild) { 155 | flowObject.fragment.appendChild(fmNode.firstChild) 156 | } 157 | } 158 | if (this.config['numberPages']) { 159 | flowObject.fragment.appendChild(createToc()) 160 | } 161 | this.flowElement(flowObject.fragment, layoutDiv.firstChild, 162 | 'roman') 163 | } 164 | } 165 | 166 | } 167 | 168 | fillPage(node, container, pageCounterStyle) { 169 | 170 | let lastPage = this.createPage(container, pageCounterStyle), 171 | clonedNode = node.cloneNode(true), 172 | footnoteSelector = this.config['footnoteSelector'], 173 | topfloatSelector = this.config['topfloatSelector'], 174 | that = this 175 | 176 | lastPage.appendChild(node) 177 | 178 | let overflow = this.cutter.cutToFit(lastPage) 179 | 180 | let topfloatsLength = lastPage.querySelectorAll(topfloatSelector).length 181 | 182 | if (topfloatsLength > 0) { 183 | let topfloats = clonedNode.querySelectorAll(topfloatSelector) 184 | 185 | for (let i = 0; i < topfloatsLength; i++) { 186 | lastPage.previousSibling.appendChild(topfloats[i]) 187 | } 188 | while (lastPage.firstChild) { 189 | lastPage.removeChild(lastPage.firstChild) 190 | } 191 | node = clonedNode.cloneNode(true) 192 | lastPage.appendChild(node) 193 | overflow = this.cutter.cutToFit(lastPage) 194 | } 195 | 196 | let footnotes = lastPage.querySelectorAll(footnoteSelector) 197 | let footnotesLength = footnotes.length 198 | if (footnotesLength > 0) { 199 | 200 | while (lastPage.nextSibling.firstChild) { 201 | lastPage.nextSibling.removeChild(lastPage.nextSibling.firstChild) 202 | } 203 | 204 | for (let i = 0; i < footnotesLength; i++) { 205 | let clonedFootnote = footnotes[i].cloneNode(true) 206 | lastPage.nextSibling.appendChild(clonedFootnote) 207 | } 208 | 209 | while (lastPage.firstChild) { 210 | lastPage.removeChild(lastPage.firstChild) 211 | } 212 | 213 | lastPage.appendChild(clonedNode) 214 | 215 | overflow = this.cutter.cutToFit(lastPage) 216 | for (let i = lastPage.querySelectorAll(footnoteSelector).length; i < 217 | footnotesLength; i++) { 218 | let oldFn = lastPage.nextSibling.children[i] 219 | 220 | while (oldFn.firstChild) { 221 | oldFn.removeChild(oldFn.firstChild) 222 | } 223 | } 224 | } 225 | 226 | 227 | if (overflow.firstChild && overflow.firstChild.textContent.trim() 228 | .length === 0 && ['P','DIV'].indexOf(overflow.firstChild.nodeName) !== -1) { 229 | overflow.removeChild(overflow.firstChild) 230 | } 231 | 232 | if (lastPage.firstChild && 233 | lastPage.firstChild.nodeType != 3 && 234 | lastPage.firstChild.textContent.trim().length === 0 && 235 | lastPage.firstChild.querySelectorAll('img,svg,canvas').length === 236 | 0) { 237 | lastPage.removeChild(lastPage.firstChild) 238 | 239 | 240 | } else if (overflow.firstChild && lastPage.firstChild) { 241 | setTimeout(function() { 242 | that.fillPage(overflow, container, 243 | pageCounterStyle) 244 | }, 1) 245 | } else { 246 | this.finish(container, pageCounterStyle) 247 | } 248 | } 249 | 250 | createPage(container, pageCounterClass) { 251 | let page = document.createElement('div'), 252 | contentsContainer = document.createElement('div'), 253 | mainContentsContainer = document.createElement('div'), 254 | topfloats = document.createElement('div'), 255 | contents = document.createElement('div'), 256 | footnotes = document.createElement('div') 257 | 258 | 259 | page.classList.add('pagination-page') 260 | contentsContainer.classList.add('pagination-contents-container') 261 | mainContentsContainer.classList.add( 262 | 'pagination-main-contents-container') 263 | 264 | if (this.currentChapter || this.currentSection) { 265 | 266 | let header = document.createElement('div') 267 | 268 | header.classList.add('pagination-header') 269 | 270 | if (this.currentChapter) { 271 | 272 | let chapterHeader = document.createElement('span') 273 | 274 | chapterHeader.classList.add('pagination-header-chapter') 275 | chapterHeader.appendChild(this.currentChapter.cloneNode( 276 | true)) 277 | header.appendChild(chapterHeader) 278 | } 279 | 280 | if (this.currentSection) { 281 | 282 | let sectionHeader = document.createElement('span') 283 | sectionHeader.classList.add('pagination-header-section') 284 | sectionHeader.appendChild(this.currentSection.cloneNode( 285 | true)) 286 | header.appendChild(sectionHeader) 287 | } 288 | page.appendChild(header) 289 | } 290 | 291 | topfloats.classList.add('pagination-topfloats') 292 | //topfloats.appendChild(document.createElement('p')) 293 | 294 | contents.classList.add('pagination-contents') 295 | 296 | footnotes.classList.add('pagination-footnotes') 297 | footnotes.appendChild(document.createElement('p')) 298 | 299 | mainContentsContainer.appendChild(topfloats) 300 | mainContentsContainer.appendChild(contents) 301 | mainContentsContainer.appendChild(footnotes) 302 | 303 | page.appendChild(mainContentsContainer) 304 | 305 | if (this.config['numberPages']) { 306 | 307 | let pagenumberField = document.createElement('div') 308 | pagenumberField.classList.add('pagination-pagenumber') 309 | pagenumberField.classList.add('pagination-' + 310 | pageCounterClass) 311 | 312 | page.appendChild(pagenumberField) 313 | } 314 | 315 | container.appendChild(page) 316 | return contents 317 | } 318 | 319 | flowElement(overflow, container, pageCounterStyle) { 320 | let that = this 321 | setTimeout(function() { 322 | that.fillPage(overflow, container, 323 | pageCounterStyle) 324 | }, 1) 325 | } 326 | 327 | finish(container, pageCounterStyle) { 328 | let layoutDiv = container.parentElement 329 | if (this.config['alwaysEven'] && container.querySelectorAll( 330 | '.pagination-page').length % 2 === 1) { 331 | this.createPage(container, pageCounterStyle) 332 | } 333 | if (container.classList.contains('pagination-body')) { 334 | this.paginateDivision(layoutDiv, pageCounterStyle) 335 | if (this.bodyFlowObjects.length===this.currentFragment && this.config['enableFrontmatter']===false) { 336 | this.config['callback']() 337 | } 338 | } else { 339 | this.pageCounters[pageCounterStyle].numberPages() 340 | this.config['callback']() 341 | } 342 | } 343 | 344 | } 345 | -------------------------------------------------------------------------------- /bundle/paginate-for-print.js: -------------------------------------------------------------------------------- 1 | (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.paginateForPrint = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o 0) { 186 | var topfloats = clonedNode.querySelectorAll(topfloatSelector); 187 | 188 | for (var i = 0; i < topfloatsLength; i++) { 189 | lastPage.previousSibling.appendChild(topfloats[i]); 190 | } 191 | while (lastPage.firstChild) { 192 | lastPage.removeChild(lastPage.firstChild); 193 | } 194 | node = clonedNode.cloneNode(true); 195 | lastPage.appendChild(node); 196 | overflow = this.cutter.cutToFit(lastPage); 197 | } 198 | 199 | var footnotes = lastPage.querySelectorAll(footnoteSelector); 200 | var footnotesLength = footnotes.length; 201 | if (footnotesLength > 0) { 202 | 203 | while (lastPage.nextSibling.firstChild) { 204 | lastPage.nextSibling.removeChild(lastPage.nextSibling.firstChild); 205 | } 206 | 207 | for (var _i = 0; _i < footnotesLength; _i++) { 208 | var clonedFootnote = footnotes[_i].cloneNode(true); 209 | lastPage.nextSibling.appendChild(clonedFootnote); 210 | } 211 | 212 | while (lastPage.firstChild) { 213 | lastPage.removeChild(lastPage.firstChild); 214 | } 215 | 216 | lastPage.appendChild(clonedNode); 217 | 218 | overflow = this.cutter.cutToFit(lastPage); 219 | for (var _i2 = lastPage.querySelectorAll(footnoteSelector).length; _i2 < footnotesLength; _i2++) { 220 | var oldFn = lastPage.nextSibling.children[_i2]; 221 | 222 | while (oldFn.firstChild) { 223 | oldFn.removeChild(oldFn.firstChild); 224 | } 225 | } 226 | } 227 | 228 | if (overflow.firstChild && overflow.firstChild.textContent.trim().length === 0 && ['P', 'DIV'].indexOf(overflow.firstChild.nodeName) !== -1) { 229 | overflow.removeChild(overflow.firstChild); 230 | } 231 | 232 | if (lastPage.firstChild && lastPage.firstChild.nodeType != 3 && lastPage.firstChild.textContent.trim().length === 0 && lastPage.firstChild.querySelectorAll('img,svg,canvas').length === 0) { 233 | lastPage.removeChild(lastPage.firstChild); 234 | } else if (overflow.firstChild && lastPage.firstChild) { 235 | setTimeout(function () { 236 | that.fillPage(overflow, container, pageCounterStyle); 237 | }, 1); 238 | } else { 239 | this.finish(container, pageCounterStyle); 240 | } 241 | } 242 | }, { 243 | key: "createPage", 244 | value: function createPage(container, pageCounterClass) { 245 | var page = document.createElement('div'), 246 | contentsContainer = document.createElement('div'), 247 | mainContentsContainer = document.createElement('div'), 248 | topfloats = document.createElement('div'), 249 | contents = document.createElement('div'), 250 | footnotes = document.createElement('div'); 251 | 252 | page.classList.add('pagination-page'); 253 | contentsContainer.classList.add('pagination-contents-container'); 254 | mainContentsContainer.classList.add('pagination-main-contents-container'); 255 | 256 | if (this.currentChapter || this.currentSection) { 257 | 258 | var header = document.createElement('div'); 259 | 260 | header.classList.add('pagination-header'); 261 | 262 | if (this.currentChapter) { 263 | 264 | var chapterHeader = document.createElement('span'); 265 | 266 | chapterHeader.classList.add('pagination-header-chapter'); 267 | chapterHeader.appendChild(this.currentChapter.cloneNode(true)); 268 | header.appendChild(chapterHeader); 269 | } 270 | 271 | if (this.currentSection) { 272 | 273 | var sectionHeader = document.createElement('span'); 274 | sectionHeader.classList.add('pagination-header-section'); 275 | sectionHeader.appendChild(this.currentSection.cloneNode(true)); 276 | header.appendChild(sectionHeader); 277 | } 278 | page.appendChild(header); 279 | } 280 | 281 | topfloats.classList.add('pagination-topfloats'); 282 | //topfloats.appendChild(document.createElement('p')) 283 | 284 | contents.classList.add('pagination-contents'); 285 | 286 | footnotes.classList.add('pagination-footnotes'); 287 | footnotes.appendChild(document.createElement('p')); 288 | 289 | mainContentsContainer.appendChild(topfloats); 290 | mainContentsContainer.appendChild(contents); 291 | mainContentsContainer.appendChild(footnotes); 292 | 293 | page.appendChild(mainContentsContainer); 294 | 295 | if (this.config['numberPages']) { 296 | 297 | var pagenumberField = document.createElement('div'); 298 | pagenumberField.classList.add('pagination-pagenumber'); 299 | pagenumberField.classList.add('pagination-' + pageCounterClass); 300 | 301 | page.appendChild(pagenumberField); 302 | } 303 | 304 | container.appendChild(page); 305 | return contents; 306 | } 307 | }, { 308 | key: "flowElement", 309 | value: function flowElement(overflow, container, pageCounterStyle) { 310 | var that = this; 311 | setTimeout(function () { 312 | that.fillPage(overflow, container, pageCounterStyle); 313 | }, 1); 314 | } 315 | }, { 316 | key: "finish", 317 | value: function finish(container, pageCounterStyle) { 318 | var layoutDiv = container.parentElement; 319 | if (this.config['alwaysEven'] && container.querySelectorAll('.pagination-page').length % 2 === 1) { 320 | this.createPage(container, pageCounterStyle); 321 | } 322 | if (container.classList.contains('pagination-body')) { 323 | this.paginateDivision(layoutDiv, pageCounterStyle); 324 | if (this.bodyFlowObjects.length === this.currentFragment && this.config['enableFrontmatter'] === false) { 325 | this.config['callback'](); 326 | } 327 | } else { 328 | this.pageCounters[pageCounterStyle].numberPages(); 329 | this.config['callback'](); 330 | } 331 | } 332 | }]); 333 | 334 | return LayoutApplier; 335 | }(); 336 | 337 | },{"./create-toc":2,"./cut-content":3,"./matches-selector":7,"./page-counters":8}],2:[function(require,module,exports){ 338 | 'use strict'; 339 | 340 | Object.defineProperty(exports, "__esModule", { 341 | value: true 342 | }); 343 | exports.createToc = createToc; 344 | 345 | var _matchesSelector = require('./matches-selector'); 346 | 347 | function createToc() { 348 | var tocDiv = document.createElement('div'), 349 | tocTitleH1 = document.createElement('h1'), 350 | tocItems = document.getElementById('pagination-layout').querySelectorAll('.pagination-body'), 351 | itemType = void 0; 352 | 353 | tocDiv.id = 'pagination-toc'; 354 | tocTitleH1.id = 'pagination-toc-title'; 355 | tocDiv.appendChild(tocTitleH1); 356 | 357 | for (var i = 0; i < tocItems.length; i++) { 358 | if ((0, _matchesSelector.matchesSelector)(tocItems[i], '.pagination-chapter')) { 359 | itemType = 'chapter'; 360 | } else if ((0, _matchesSelector.matchesSelector)(tocItems[i], '.pagination-section')) { 361 | itemType = 'section'; 362 | } else { 363 | continue; 364 | } 365 | var tocItemDiv = document.createElement('div'); 366 | tocItemDiv.classList.add('pagination-toc-entry'); 367 | var tocItemTextSpan = document.createElement('span'); 368 | tocItemTextSpan.classList.add('pagination-toc-text'); 369 | 370 | tocItemTextSpan.appendChild(document.createTextNode(tocItems[i].querySelector('.pagination-header-' + itemType).textContent.trim())); 371 | tocItemDiv.appendChild(tocItemTextSpan); 372 | 373 | var tocItemPnSpan = document.createElement('span'); 374 | tocItemPnSpan.classList.add('pagination-toc-pagenumber'); 375 | 376 | tocItemPnSpan.appendChild(document.createTextNode(tocItems[i].querySelector('.pagination-pagenumber').textContent.trim())); 377 | 378 | tocItemDiv.appendChild(tocItemPnSpan); 379 | 380 | tocDiv.appendChild(tocItemDiv); 381 | } 382 | 383 | return tocDiv; 384 | } 385 | 386 | },{"./matches-selector":7}],3:[function(require,module,exports){ 387 | "use strict"; 388 | 389 | Object.defineProperty(exports, "__esModule", { 390 | value: true 391 | }); 392 | exports.ContentCutter = undefined; 393 | 394 | var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); 395 | 396 | var _getBoundingClientRect = require("./get-bounding-client-rect"); 397 | 398 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 399 | 400 | var ContentCutter = exports.ContentCutter = function () { 401 | function ContentCutter(config) { 402 | _classCallCheck(this, ContentCutter); 403 | 404 | this.config = config; 405 | } 406 | 407 | // main cut method 408 | 409 | 410 | _createClass(ContentCutter, [{ 411 | key: "cutToFit", 412 | value: function cutToFit(contents) { 413 | 414 | var range = void 0, 415 | overflow = void 0, 416 | manualPageBreak = void 0, 417 | ignoreLastLIcut = false, 418 | cutLIs = void 0, 419 | pageBreak = void 0, 420 | 421 | // contentHeight = height of page - height of top floats - height of footnotes. 422 | contentHeight = contents.parentElement.clientHeight - contents.previousSibling.clientHeight - contents.nextSibling.clientHeight, 423 | contentWidth = contents.parentElement.clientWidth, 424 | boundingRect = void 0, 425 | rightCutOff = void 0; 426 | 427 | // set height to contentHeight 428 | contents.style.height = contentHeight + "px"; 429 | 430 | if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) { 431 | // Firefox has some insane bug which means that the new content height 432 | // isn't applied immediately when dealing with multicol -- unless one 433 | // removes the content and re-adds it. 434 | var nSib = contents.nextSibling; 435 | var pEl = contents.parentElement; 436 | pEl.removeChild(contents); 437 | pEl.insertBefore(contents, nSib); 438 | } 439 | 440 | // Set height temporarily to "auto" so the page flows beyond where 441 | // it should end and we can find the page break. 442 | contents.style.width = contentWidth * 2 + 100 + 'px'; 443 | contents.style.columnWidth = contentWidth + 'px'; 444 | contents.style.columnGap = '100px'; 445 | contents.style.columnFill = 'auto'; 446 | 447 | contents.style.MozColumnWidth = contentWidth + 'px'; 448 | contents.style.MozColumnGap = '100px'; 449 | contents.style.MozColumnFill = 'auto'; 450 | 451 | boundingRect = contents.getBoundingClientRect(); 452 | rightCutOff = boundingRect.left + contentWidth + 20; 453 | 454 | manualPageBreak = contents.querySelector(this.config['pagebreakSelector']); 455 | 456 | if (manualPageBreak && manualPageBreak.getBoundingClientRect().left < rightCutOff) { 457 | range = document.createRange(); 458 | range.setStartBefore(manualPageBreak); 459 | } else if (boundingRect.right <= rightCutOff) { 460 | contents.style.width = contentWidth + "px"; 461 | return false; 462 | } else { 463 | pageBreak = this.findPageBreak(contents, rightCutOff); 464 | if (!pageBreak) { 465 | contents.style.width = contentWidth + "px"; 466 | return false; 467 | } 468 | range = document.createRange(); 469 | range.setStart(pageBreak.node, pageBreak.offset); 470 | } 471 | 472 | contents.style.width = contentWidth + "px"; 473 | // We find that the first item is an OL/UL which may have started on the previous page. 474 | if (['OL', 'UL'].indexOf(range.startContainer.nodeName) !== -1 || range.startContainer.nodeName === '#text' && range.startContainer.parentNode && ['OL', 'UL'].indexOf(range.startContainer.parentNode.nodeName) !== -1 && range.startContainer.length === range.startOffset) { 475 | // We are cutting from inside a List, don't touch the innermost list items. 476 | ignoreLastLIcut = true; 477 | } 478 | range.setEndAfter(contents.lastChild); 479 | overflow = range.extractContents(); 480 | cutLIs = this.countOLItemsAndFixLI(contents); 481 | if (ignoreLastLIcut) { 482 | // Because the cut happened exactly between two LI items, don't try to unify the two lowest level LIs. 483 | cutLIs[cutLIs.length - 1].hideFirstLI = false; 484 | if (cutLIs[cutLIs.length - 1].start) { 485 | cutLIs[cutLIs.length - 1].start++; 486 | } 487 | } 488 | this.applyInitialOLcount(overflow, cutLIs); 489 | 490 | if (!contents.lastChild || contents.textContent.trim().length === 0 && contents.querySelectorAll('img,svg,canvas').length === 0) { 491 | contents.appendChild(overflow); 492 | overflow = false; 493 | } 494 | return overflow; 495 | } 496 | }, { 497 | key: "countOLItemsAndFixLI", 498 | value: function countOLItemsAndFixLI(element, countList) { 499 | var start = 1, 500 | hideFirstLI = false; 501 | 502 | if (typeof countList === 'undefined') { 503 | countList = []; 504 | } 505 | if (element.nodeName === 'OL') { 506 | if (element.hasAttribute('start')) { 507 | start = parseInt(element.getAttribute('start')); 508 | } 509 | if (element.lastElementChild.textContent.length === 0) { 510 | element.removeChild(element.lastElementChild); 511 | } else { 512 | start--; 513 | hideFirstLI = true; 514 | } 515 | countList.push({ 516 | start: start + element.childElementCount, 517 | hideFirstLI: hideFirstLI 518 | }); 519 | } else if (element.nodeName === 'UL') { 520 | if (element.lastElementChild.textContent.length === 0) { 521 | element.removeChild(element.lastElementChild); 522 | } else { 523 | hideFirstLI = true; 524 | } 525 | countList.push({ 526 | hideFirstLI: hideFirstLI 527 | }); 528 | } 529 | 530 | if (element.childElementCount > 0) { 531 | return this.countOLItemsAndFixLI(element.lastElementChild, countList); 532 | } else { 533 | return countList; 534 | } 535 | } 536 | }, { 537 | key: "applyInitialOLcount", 538 | value: function applyInitialOLcount(element, countList) { 539 | if (element.nodeName === '#document-fragment') { 540 | element = element.childNodes[0]; 541 | } 542 | var listCount = void 0; 543 | if (countList.length === 0) { 544 | return; 545 | } 546 | if (element.nodeName === 'OL') { 547 | listCount = countList.shift(); 548 | element.setAttribute('start', listCount.start); 549 | if (listCount.hideFirstLI) { 550 | element.firstElementChild.classList.add('hide'); 551 | } 552 | } else if (element.nodeName === 'UL') { 553 | listCount = countList.shift(); 554 | if (listCount.hideFirstLI) { 555 | element.firstElementChild.classList.add('hide'); 556 | } 557 | } 558 | if (element.childElementCount > 0) { 559 | this.applyInitialOLcount(element.firstElementChild, countList); 560 | } else { 561 | return; 562 | } 563 | } 564 | }, { 565 | key: "findPrevNode", 566 | value: function findPrevNode(node) { 567 | if (node.previousSibling) { 568 | return node.previousSibling; 569 | } else { 570 | return this.findPrevNode(node.parentElement); 571 | } 572 | } 573 | 574 | // Go through a node (contents) and find the exact position where it goes 575 | // further to the right than the right cutoff. 576 | 577 | }, { 578 | key: "findPageBreak", 579 | value: function findPageBreak(contents, rightCutOff) { 580 | var contentCoords = void 0, 581 | found = void 0, 582 | prevNode = void 0; 583 | if (contents.nodeType === 1) { 584 | contentCoords = (0, _getBoundingClientRect.getBoundingClientRect)(contents); 585 | if (contentCoords.left < rightCutOff) { 586 | if (contentCoords.right > rightCutOff) { 587 | found = false; 588 | var i = 0; 589 | while (found === false && i < contents.childNodes.length) { 590 | found = this.findPageBreak(contents.childNodes[i], rightCutOff); 591 | i++; 592 | } 593 | if (found) { 594 | return found; 595 | } 596 | } else { 597 | return false; 598 | } 599 | } 600 | prevNode = this.findPrevNode(contents); 601 | return { 602 | node: prevNode, 603 | offset: prevNode.length ? prevNode.length : prevNode.childNodes.length 604 | }; 605 | } else if (contents.nodeType === 3) { 606 | var range = document.createRange(), 607 | offset = contents.length; 608 | range.setStart(contents, 0); 609 | range.setEnd(contents, offset); 610 | contentCoords = range.getBoundingClientRect(); 611 | 612 | if (contentCoords.bottom === contentCoords.top) { 613 | // A text node that doesn't have any output. 614 | return false; 615 | } else if (contentCoords.left < rightCutOff) { 616 | if (contentCoords.right > rightCutOff) { 617 | found = false; 618 | while (found === false && offset > 0) { 619 | offset--; 620 | range.setEnd(contents, offset); 621 | contentCoords = range.getBoundingClientRect(); 622 | if (contentCoords.right <= rightCutOff) { 623 | found = { 624 | node: contents, 625 | offset: offset 626 | }; 627 | } 628 | } 629 | if (found) { 630 | return found; 631 | } 632 | } else { 633 | return false; 634 | } 635 | } 636 | prevNode = this.findPrevNode(contents); 637 | return { 638 | node: prevNode, 639 | offset: prevNode.length ? prevNode.length : prevNode.childNodes.length 640 | }; 641 | } else { 642 | return false; 643 | } 644 | } 645 | }]); 646 | 647 | return ContentCutter; 648 | }(); 649 | 650 | },{"./get-bounding-client-rect":5}],4:[function(require,module,exports){ 651 | 'use strict'; 652 | 653 | Object.defineProperty(exports, "__esModule", { 654 | value: true 655 | }); 656 | var DEFAULT_CONFIG_VALUES = exports.DEFAULT_CONFIG_VALUES = { 657 | // SELECTORS 658 | sectionStartSelector: 'h1', // The CSS selector that marks the start of a new section. 659 | sectionTitleSelector: 'h1', // The CSS selector at a start of a section that marks the title of that section. 660 | chapterStartSelector: 'h2', // The CSS selector that marks the start of a new chapter. 661 | chapterTitleSelector: 'h2', // The CSS selector at a start of a chapter that marks the title of that chapter. 662 | footnoteSelector: '.pagination-footnote', // The CSS selector of elements that are to be converted to footnotes. 663 | pagebreakSelector: '.pagination-pagebreak', // The CSS selector of elements that are to be converted to page breaks. 664 | topfloatSelector: '.pagination-topfloat', // The CSS selector of elements that are to be converted to top floating elements. 665 | // 'marginnoteSelector': '.pagination-marginnote', 666 | 667 | // FLOW ELEMENTS 668 | flowFromElement: false, // An element where to flow from (if false, document.body will be taken) 669 | frontmatterFlowFromElement: false, // An element that holds the contents to be flown into the frontmatter 670 | flowToElement: false, // An element where to flow to (if false, document.body will be taken) 671 | 672 | // LAYOUT OPTIONS 673 | numberPages: true, // Whether to number pages 674 | alwaysEven: true, // Whether every section/chapter always should have an even number of pages 675 | enableFrontmatter: true, // Whether to add frontmatter (Title page, Table-of-Contents, etc.) 676 | // 'enableTableOfFigures': false, 677 | // 'enableTableOfTables': false, 678 | // 'enableMarginNotes': false, 679 | // 'enableCrossReferences': true, 680 | // 'enableWordIndex': true, 681 | 682 | // CALLBACK 683 | callback: function callback() {}, 684 | 685 | // STYLING OpTIONS (Can be overriden with CSS) 686 | outerMargin: 0.5, 687 | innerMargin: 0.8, 688 | contentsTopMargin: 0.8, 689 | headerTopMargin: 0.3, 690 | contentsBottomMargin: 0.8, 691 | pagenumberBottomMargin: 0.3, 692 | pageHeight: 8.3, 693 | pageWidth: 5.8, 694 | // 'marginNotesWidth': 1.5, 695 | // 'marginNotesSeparatorWidth': 0.09, 696 | // 'marginNotesVerticalSeparatorWidth': 0.09, 697 | lengthUnit: 'in' 698 | }; 699 | 700 | },{}],5:[function(require,module,exports){ 701 | "use strict"; 702 | 703 | Object.defineProperty(exports, "__esModule", { 704 | value: true 705 | }); 706 | exports.getBoundingClientRect = getBoundingClientRect; 707 | // Chrome (+ possibly others) currently has issues when trying to find the real coordinates of elements when in multicol. 708 | // This is a workaround that uses a range over the elements contents and combines all client rects around it. 709 | 710 | function getBoundingClientRect(element) { 711 | var r = document.createRange(); 712 | r.setStart(element, 0); 713 | r.setEnd(element, element.childNodes.length); 714 | return r.getBoundingClientRect(); 715 | } 716 | 717 | },{}],6:[function(require,module,exports){ 718 | "use strict"; 719 | 720 | var _paginateForPrint = require("./paginate-for-print"); 721 | 722 | module.exports = function (configValues) { 723 | var paginator = new _paginateForPrint.PaginateForPrint(configValues); 724 | paginator.initiate(); 725 | return function () { 726 | paginator.tearDown(); 727 | }; 728 | }; 729 | 730 | },{"./paginate-for-print":9}],7:[function(require,module,exports){ 731 | 'use strict'; 732 | 733 | Object.defineProperty(exports, "__esModule", { 734 | value: true 735 | }); 736 | exports.matchesSelector = matchesSelector; 737 | function matchesSelector(element, selector) { 738 | 739 | if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) { 740 | // Firefox 741 | return element.mozMatchesSelector(selector); 742 | } else { 743 | // Webkit + Chrome + Edge 744 | return element.webkitMatchesSelector(selector); 745 | } 746 | } 747 | 748 | },{}],8:[function(require,module,exports){ 749 | 'use strict'; 750 | 751 | Object.defineProperty(exports, "__esModule", { 752 | value: true 753 | }); 754 | 755 | var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); 756 | 757 | function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } 758 | 759 | function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } 760 | 761 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 762 | 763 | var PageCounterArab = exports.PageCounterArab = function () { 764 | // arab is the page counter used by the main body contents. 765 | 766 | /* Create a pagecounter. cssClass is the CSS class employed by this page 767 | * counter to mark all page numbers associated with it. 768 | */ 769 | function PageCounterArab() { 770 | _classCallCheck(this, PageCounterArab); 771 | 772 | this.cssClass = 'arab'; 773 | this.counterValue = 0; 774 | } 775 | 776 | _createClass(PageCounterArab, [{ 777 | key: 'show', 778 | value: function show() { 779 | /* Standard show function for page counter is to show the value itself 780 | * using arabic numbers. 781 | */ 782 | return this.counterValue; 783 | } 784 | }, { 785 | key: 'incrementAndShow', 786 | value: function incrementAndShow() { 787 | /* Increment the page count by one and return the reuslt page count 788 | * using the show function. 789 | */ 790 | this.counterValue++; 791 | return this.show(); 792 | } 793 | }, { 794 | key: 'numberPages', 795 | value: function numberPages() { 796 | /* If the pages associated with this page counter need to be updated, 797 | * go through all of them from the start of the book and number them, 798 | * thereby potentially removing old page numbers. 799 | */ 800 | this.counterValue = 0; 801 | 802 | var pagenumbersToNumber = document.querySelectorAll('.pagination-page .pagination-pagenumber.pagination-' + this.cssClass); 803 | for (var i = 0; i < pagenumbersToNumber.length; i++) { 804 | pagenumbersToNumber[i].innerHTML = this.incrementAndShow(); 805 | } 806 | } 807 | }]); 808 | 809 | return PageCounterArab; 810 | }(); 811 | 812 | var PageCounterRoman = exports.PageCounterRoman = function (_PageCounterArab) { 813 | _inherits(PageCounterRoman, _PageCounterArab); 814 | 815 | // roman is the page counter used by the frontmatter. 816 | function PageCounterRoman() { 817 | _classCallCheck(this, PageCounterRoman); 818 | 819 | var _this = _possibleConstructorReturn(this, (PageCounterRoman.__proto__ || Object.getPrototypeOf(PageCounterRoman)).call(this)); 820 | 821 | _this.cssClass = 'roman'; 822 | return _this; 823 | } 824 | 825 | _createClass(PageCounterRoman, [{ 826 | key: 'show', 827 | value: function show() { 828 | // Create roman numeral representations of numbers. 829 | var digits = String(+this.counterValue).split(""), 830 | key = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM", "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC", "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"], 831 | roman = "", 832 | i = 3; 833 | while (i--) { 834 | roman = (key[+digits.pop() + i * 10] || "") + roman; 835 | } 836 | return new Array(+digits.join("") + 1).join("M") + roman; 837 | } 838 | }]); 839 | 840 | return PageCounterRoman; 841 | }(PageCounterArab); 842 | 843 | },{}],9:[function(require,module,exports){ 844 | "use strict"; 845 | 846 | Object.defineProperty(exports, "__esModule", { 847 | value: true 848 | }); 849 | exports.PaginateForPrint = undefined; 850 | 851 | var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); 852 | 853 | var _defaults = require("./defaults"); 854 | 855 | var _applyLayout = require("./apply-layout"); 856 | 857 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 858 | 859 | /*! 860 | * PaginateForPrint 861 | * Copyright 2014-2016 Johannes Wilm. Freely available under the AGPL. For further details see LICENSE.txt 862 | * 863 | */ 864 | 865 | var PaginateForPrint = exports.PaginateForPrint = function () { 866 | function PaginateForPrint(config) { 867 | _classCallCheck(this, PaginateForPrint); 868 | 869 | this.config = Object.assign(_defaults.DEFAULT_CONFIG_VALUES, config); 870 | this.stylesheets = []; 871 | this.layoutApplier = new _applyLayout.LayoutApplier(this.config); 872 | } 873 | 874 | _createClass(PaginateForPrint, [{ 875 | key: "initiate", 876 | value: function initiate() { 877 | /* Initiate PaginateForPrint by setting basic CSS style. and initiating 878 | the layout mechanism. 879 | */ 880 | this.setStyle(); 881 | this.setPageStyle(); 882 | this.setBrowserSpecifics(); 883 | this.layoutApplier.initiate(); 884 | } 885 | }, { 886 | key: "setBrowserSpecifics", 887 | value: function setBrowserSpecifics() { 888 | if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) { 889 | var stylesheet = document.createElement('style'); 890 | // Small fix for Firefox to not print first two pages on top of oneanother. 891 | stylesheet.innerHTML = ".pagination-page:first-child {page-break-before: always;}"; 892 | document.head.appendChild(stylesheet); 893 | this.stylesheets.push(stylesheet); 894 | } 895 | } 896 | }, { 897 | key: "setStyle", 898 | value: function setStyle() { 899 | /* Set style for the regions and pages used by Paginate for Print and add it 900 | * to the head of the DOM. 901 | */ 902 | var stylesheet = document.createElement('style'); 903 | var footnoteSelector = this.config['footnoteSelector']; 904 | 905 | stylesheet.innerHTML = "\n.pagination-footnotes " + footnoteSelector + " {display: block;}\n.pagination-contents " + footnoteSelector + " > * {display:none;}\n.pagination-main-contents-container " + footnoteSelector + ", figure {\n -webkit-column-break-inside: avoid;\n page-break-inside: avoid;\n}\nbody {\n counter-reset: pagination-footnote pagination-footnote-reference;\n}\n.pagination-contents " + footnoteSelector + "::before {\n counter-increment: pagination-footnote-reference;\n content: counter(pagination-footnote-reference);\n}\n" + footnoteSelector + " > * > *:first-child::before {\n counter-increment: pagination-footnote;\n content: counter(pagination-footnote);\n}\n.pagination-page {\n position: relative;\n}\n.pagination-page {\n page-break-after: always;\n page-break-before: always;\n margin-left: auto;\n margin-right: auto;\n}\n.pagination-page:first-child {\n page-break-before: avoid;\n}\n.pagination-page:last-child {\n page-break-after: avoid;\n}\n.pagination-main-contents-container, .pagination-pagenumber, .pagination-header {\n position: absolute;\n}\nli.hide {\n list-style-type: none;\n}\n "; 906 | document.head.appendChild(stylesheet); 907 | this.stylesheets.push(stylesheet); 908 | } 909 | }, { 910 | key: "setPageStyle", 911 | value: function setPageStyle() { 912 | // Set style for a particular page size. 913 | var unit = this.config['lengthUnit'], 914 | contentsWidthNumber = this.config['pageWidth'] - this.config['innerMargin'] - this.config['outerMargin'], 915 | contentsWidth = contentsWidthNumber + unit, 916 | contentsHeightNumber = this.config['pageHeight'] - this.config['contentsTopMargin'] - this.config['contentsBottomMargin'], 917 | contentsHeight = contentsHeightNumber + unit, 918 | pageWidth = this.config['pageWidth'] + unit, 919 | pageHeight = this.config['pageHeight'] + unit, 920 | contentsBottomMargin = this.config['contentsBottomMargin'] + unit, 921 | innerMargin = this.config['innerMargin'] + unit, 922 | outerMargin = this.config['outerMargin'] + unit, 923 | pagenumberBottomMargin = this.config['pagenumberBottomMargin'] + unit, 924 | headerTopMargin = this.config['headerTopMargin'] + unit, 925 | imageMaxHeight = contentsHeightNumber - 0.1 + unit, 926 | footnoteSelector = this.config['footnoteSelector']; 927 | var pageStyleSheet = document.createElement('style'); 928 | pageStyleSheet.innerHTML = "\n.pagination-page {height: " + pageHeight + "; width: " + pageWidth + ";background-color: #fff;}\n@page {size:" + pageWidth + " " + pageHeight + ";}\nbody {background-color: #efefef; margin:0;}\n@media screen{.pagination-page {border:solid 1px #000; margin-bottom:.2in;}}\n.pagination-main-contents-container {\n width: " + contentsWidth + ";\n height: " + contentsHeight + ";\n bottom: " + contentsBottomMargin + ";\n}\n.pagination-contents-container {\n bottom: " + contentsBottomMargin + ";\n height: " + contentsHeight + ";\n}\n.pagination-contents {\n height: " + contentsHeight + ";\n width: " + contentsWidth + ";\n}\nimg {max-height: " + imageMaxHeight + "; max-width: 100%;}\n.pagination-pagenumber {\n bottom: " + pagenumberBottomMargin + ";\n}\n.pagination-header {\n top: " + headerTopMargin + ";\n}\n.pagination-page:nth-child(odd) .pagination-main-contents-container,\n.pagination-page:nth-child(odd) .pagination-pagenumber,\n.pagination-page:nth-child(odd) .pagination-header {\n right: " + outerMargin + ";\n left: " + innerMargin + ";\n}\n.pagination-page:nth-child(even) .pagination-main-contents-container,\n.pagination-page:nth-child(even) .pagination-pagenumber,\n.pagination-page:nth-child(even) .pagination-header {\n right: " + innerMargin + ";\n left: " + outerMargin + ";\n}\n.pagination-page:nth-child(odd) .pagination-pagenumber,\n.pagination-page:nth-child(odd) .pagination-header {text-align:right;}\n.pagination-page:nth-child(odd) .pagination-header-section {display:none;}\n.pagination-page:nth-child(even) .pagination-header-chapter {display:none;}\n.pagination-page:nth-child(even) .pagination-pagenumber,\n.pagination-page:nth-child(even) .pagination-header { text-align:left;}\n" + footnoteSelector + " > * > * {font-size: 0.7em; margin:.25em;}\n" + footnoteSelector + " > * > *::before, " + footnoteSelector + "::before {\n position: relative;\n top: -0.5em;\n font-size: 80%;\n}\n#pagination-toc-title:before {\n content:'Contents';\n}\n.pagination-toc-entry .pagination-toc-pagenumber {float:right;}\n "; 929 | document.head.insertBefore(pageStyleSheet, document.head.firstChild); 930 | this.stylesheets.push(pageStyleSheet); 931 | } 932 | 933 | // Remove stylesheets and all contents of the flow to element. 934 | 935 | }, { 936 | key: "tearDown", 937 | value: function tearDown() { 938 | // Remove stylesheets from DOM 939 | this.stylesheets.forEach(function (stylesheet) { 940 | stylesheet.parentNode.removeChild(stylesheet); 941 | }); 942 | var flowToElement = this.config['flowToElement'] ? this.config['flowToElement'] : document.body; 943 | while (flowToElement.firstChild) { 944 | flowToElement.removeChild(flowToElement.firstChild); 945 | } 946 | } 947 | }]); 948 | 949 | return PaginateForPrint; 950 | }(); 951 | 952 | },{"./apply-layout":1,"./defaults":4}]},{},[6])(6) 953 | }); --------------------------------------------------------------------------------