| 1 | /* |
| 2 | Copyright - 2020 - wwwouaiebe - Contact: http//www.ouaie.be/ |
| 3 | |
| 4 | This program is free software; |
| 5 | you can redistribute it and/or modify it under the terms of the |
| 6 | GNU General Public License as published by the Free Software Foundation; |
| 7 | either version 3 of the License, or any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License |
| 15 | along with this program; if not, write to the Free Software |
| 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | /* |
| 19 | Changes: |
| 20 | - v4.0.0: |
| 21 | - created from v3.6.0 |
| 22 | Doc reviewed 202208 |
| 23 | */ |
| 24 | |
| 25 | import theErrorsUI from '../uis/errorsUI/ErrorsUI.js'; |
| 26 | import theHTMLElementsFactory from '../core/uiLib/HTMLElementsFactory.js'; |
| 27 | import theTravelNotesData from '../data/TravelNotesData.js'; |
| 28 | import theDataSearchEngine from '../data/DataSearchEngine.js'; |
| 29 | import theGeometry from '../core/lib/Geometry.js'; |
| 30 | import theConfig from '../data/Config.js'; |
| 31 | import theTranslator from '../core/uiLib/Translator.js'; |
| 32 | import PrintViewsFactory from './PrintViewsFactory.js'; |
| 33 | import ViewSize from './ViewSize.js'; |
| 34 | import PrintPageBuilder from './PrintPageBuilder.js'; |
| 35 | |
| 36 | import { ZERO, TWO, LAT, LNG } from '../main/Constants.js'; |
| 37 | |
| 38 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
| 39 | /** |
| 40 | This class manages the print of a route |
| 41 | */ |
| 42 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
| 43 | |
| 44 | class RoutePrinter { |
| 45 | |
| 46 | /** |
| 47 | The number of tiles needed for printing a page |
| 48 | @type {Number} |
| 49 | */ |
| 50 | |
| 51 | #tilesPerPage = ZERO; |
| 52 | |
| 53 | /** |
| 54 | The tiles dimension in pixel |
| 55 | @type {Number} |
| 56 | */ |
| 57 | |
| 58 | // eslint-disable-next-line no-magic-numbers |
| 59 | static get #TILE_SIZE ( ) { return 256; } |
| 60 | |
| 61 | /** |
| 62 | Compute the view size in lat and lng transforming the dimension given in mm by the user. |
| 63 | @param {PrintRouteMapOptions} printRouteMapOptions the print options returned by the PrintRouteMapDialog |
| 64 | */ |
| 65 | |
| 66 | #computeMaxViewSize ( printRouteMapOptions ) { |
| 67 | |
| 68 | // creating a dummy HTMLElement to compute the view size |
| 69 | const dummyDiv = theHTMLElementsFactory.create ( 'div', { }, document.body ); |
| 70 | dummyDiv.style.position = 'absolute'; |
| 71 | dummyDiv.style.top = '0'; |
| 72 | dummyDiv.style.left = '0'; |
| 73 | dummyDiv.style.width = String ( printRouteMapOptions.paperWidth - ( TWO * printRouteMapOptions.borderWidth ) ) + 'mm'; |
| 74 | dummyDiv.style.height = String ( printRouteMapOptions.paperHeight - ( TWO * printRouteMapOptions.borderWidth ) ) + 'mm'; |
| 75 | |
| 76 | // transform the screen coordinates to lat and lng |
| 77 | const topLeftScreen = theGeometry.screenCoordToLatLng ( ZERO, ZERO ); |
| 78 | const bottomRightScreen = theGeometry.screenCoordToLatLng ( |
| 79 | dummyDiv.clientWidth, |
| 80 | dummyDiv.clientHeight |
| 81 | ); |
| 82 | |
| 83 | // computing the tiles needed for a page |
| 84 | this.#tilesPerPage = |
| 85 | Math.ceil ( dummyDiv.clientWidth / RoutePrinter.#TILE_SIZE ) * |
| 86 | Math.ceil ( dummyDiv.clientHeight / RoutePrinter.#TILE_SIZE ); |
| 87 | |
| 88 | document.body.removeChild ( dummyDiv ); |
| 89 | |
| 90 | // computing the scale |
| 91 | const scale = theTravelNotesData.map.getZoomScale ( |
| 92 | theTravelNotesData.map.getZoom ( ), |
| 93 | printRouteMapOptions.zoomFactor |
| 94 | ); |
| 95 | |
| 96 | // computing the size and return. |
| 97 | return new ViewSize ( |
| 98 | Math.abs ( topLeftScreen [ LNG ] - bottomRightScreen [ LNG ] ) * scale, |
| 99 | Math.abs ( topLeftScreen [ LAT ] - bottomRightScreen [ LAT ] ) * scale |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | The constructor |
| 105 | */ |
| 106 | |
| 107 | constructor ( ) { |
| 108 | Object.freeze ( this ); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | Modify the main page, creating views on the page, so the page can be printed easily |
| 113 | @param {PrintRouteMapOptions} printRouteMapOptions the PrintRouteMapOptions returned by the PrintRouteMapDialog |
| 114 | @param {Number} routeObjId The objId of the route to print |
| 115 | */ |
| 116 | |
| 117 | print ( printRouteMapOptions, routeObjId ) { |
| 118 | |
| 119 | const route = theDataSearchEngine.getRoute ( routeObjId ); |
| 120 | if ( ! route ) { |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | // Computing the needed views |
| 125 | const printViewsFactory = new PrintViewsFactory ( |
| 126 | route, |
| 127 | this.#computeMaxViewSize ( printRouteMapOptions ) |
| 128 | ); |
| 129 | |
| 130 | // Remain for debugging |
| 131 | /* |
| 132 | printViewsFactory.printViews.forEach ( |
| 133 | view => new LeafletRectangle ( [ view.bottomLeft, view.upperRight ] ).addTo ( theTravelNotesData.map ) |
| 134 | ); |
| 135 | console.log ( 'views :' + printViewsFactory.printViews.length ); |
| 136 | */ |
| 137 | // Verify the tiles needed and stop the command if too mutch tiles needed |
| 138 | |
| 139 | if ( theConfig.printRouteMap.maxTiles < printViewsFactory.printViews.length * this.#tilesPerPage ) { |
| 140 | theErrorsUI.showError ( theTranslator.getText ( 'RoutePrinter - The maximum of allowed pages is reached.' ) ); |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | // Prepare the main page, for printing, hidding the map, adding the views and a print toolbar |
| 145 | const printPageBuilder = new PrintPageBuilder ( |
| 146 | route, |
| 147 | printViewsFactory.printViews, |
| 148 | printRouteMapOptions |
| 149 | ); |
| 150 | printPageBuilder.preparePage ( ); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | export default RoutePrinter; |
| 155 | |
| 156 | /* --- End of file --------------------------------------------------------------------------------------------------------- */ |
| 157 |