| 1 | /* |
| 2 | * This file is part of the MediaWiki extension MultimediaViewer. |
| 3 | * |
| 4 | * MultimediaViewer is free software: you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation, either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * MultimediaViewer 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 MultimediaViewer. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | const ThumbnailWidth = require( './model/mmv.model.ThumbnailWidth.js' ); |
| 19 | const { thumbnailBucketSizes } = require( './config.json' ); |
| 20 | |
| 21 | /** |
| 22 | * A helper class for bucketing image sizes. |
| 23 | * Bucketing helps to avoid cache fragmentation and thus speed up image loading: |
| 24 | * instead of generating potentially hundreds of different thumbnail sizes, we restrict |
| 25 | * ourselves to a short list of acceptable thumbnail widths, and only ever load thumbnails |
| 26 | * of that size. Final size adjustment is done in a thumbnail. |
| 27 | * |
| 28 | * See also the [Standardized thumbnail sizes RFC][1]. |
| 29 | * |
| 30 | * [1]: https://www.mediawiki.org/wiki/Talk:Requests_for_comment/Standardized_thumbnails_sizes |
| 31 | */ |
| 32 | class ThumbnailWidthCalculator { |
| 33 | /** |
| 34 | * @param {Object} [options] |
| 35 | * @param {number[]} [options.widthBuckets] see {@link ThumbnailWidthCalculator#widthBuckets} |
| 36 | * @param {number} [options.devicePixelRatio] see {@link ThumbnailWidthCalculator#devicePixelRatio}; |
| 37 | * will be autodetected if omitted |
| 38 | */ |
| 39 | constructor( options ) { |
| 40 | options = Object.assign( { |
| 41 | // default image widths |
| 42 | widthBuckets: thumbnailBucketSizes, |
| 43 | |
| 44 | // screen pixel per CSS pixel |
| 45 | devicePixelRatio: window.devicePixelRatio || 1 |
| 46 | }, options ); |
| 47 | |
| 48 | if ( !options.widthBuckets.length ) { |
| 49 | throw new Error( 'No buckets!' ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * List of thumbnail width bucket sizes, in pixels. |
| 54 | * |
| 55 | * @property {number[]} |
| 56 | */ |
| 57 | this.widthBuckets = options.widthBuckets; |
| 58 | this.widthBuckets.sort( ( a, b ) => a - b ); |
| 59 | |
| 60 | /** |
| 61 | * Screen pixel count per CSS pixel. |
| 62 | * |
| 63 | * @property {number} |
| 64 | */ |
| 65 | this.devicePixelRatio = options.devicePixelRatio; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Finds the smallest bucket which is large enough to hold the target size |
| 70 | * (i. e. the smallest bucket whose size is equal to or greater than the target). |
| 71 | * If none of the buckets are large enough, returns the largest bucket. |
| 72 | * |
| 73 | * @param {number} target |
| 74 | * @return {number} |
| 75 | */ |
| 76 | findNextBucket( target ) { |
| 77 | let bucket; |
| 78 | const buckets = this.widthBuckets; |
| 79 | |
| 80 | for ( let i = 0; i < buckets.length; i++ ) { |
| 81 | bucket = buckets[ i ]; |
| 82 | |
| 83 | if ( bucket >= target ) { |
| 84 | return bucket; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // If we failed to find a high enough size...good luck |
| 89 | return bucket; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Finds the largest width for an image so that it will still fit into a given bounding box, |
| 94 | * based on the size of a sample (some smaller version of the same image, like the thumbnail |
| 95 | * shown in the article) which is used to calculate the ratio. |
| 96 | * |
| 97 | * This is for internal use, you should probably use calculateWidths() instead. |
| 98 | * |
| 99 | * @protected |
| 100 | * @param {number} boundingWidth width of the bounding box |
| 101 | * @param {number} boundingHeight height of the bounding box |
| 102 | * @param {number} sampleWidth width of the sample image |
| 103 | * @param {number} sampleHeight height of the sample image |
| 104 | * @return {number} the largest width so that the scaled version of the sample image fits |
| 105 | * into the bounding box (either horizontal or vertical edges touch on both sides). |
| 106 | */ |
| 107 | calculateFittingWidth( boundingWidth, boundingHeight, sampleWidth, sampleHeight ) { |
| 108 | if ( ( boundingWidth / boundingHeight ) > ( sampleWidth / sampleHeight ) ) { |
| 109 | // we are limited by height; we need to calculate the max width that fits |
| 110 | return Math.round( ( sampleWidth / sampleHeight ) * boundingHeight ); |
| 111 | } else { |
| 112 | // simple case, ratio tells us we're limited by width |
| 113 | return boundingWidth; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Finds the largest width for an image so that it will still fit into a given bounding box, |
| 119 | * based on the size of a sample (some smaller version of the same image, like the thumbnail |
| 120 | * shown in the article) which is used to calculate the ratio. |
| 121 | * |
| 122 | * Returns two values, a CSS width which is the size in pixels that should be used so the image |
| 123 | * fits exactly into the bounding box, and a real width which should be the size of the |
| 124 | * downloaded image in pixels. The two will be different for two reasons: |
| 125 | * - Images are bucketed for more efficient caching, so the real width will always be one of |
| 126 | * the numbers in this.widthBuckets. The resulting thumbnail will be slightly larger than |
| 127 | * the bounding box so that it takes roughly the same amount of bandwidth and |
| 128 | * looks decent when resized by the browser. |
| 129 | * - For devices with high pixel density (multiple actual pixels per CSS pixel) we want to use |
| 130 | * a larger image so that there will be roughly one image pixel per physical display pixel. |
| 131 | * |
| 132 | * @param {number} boundingWidth width of the bounding box, in CSS pixels |
| 133 | * @param {number} boundingHeight height of the bounding box, in CSS pixels |
| 134 | * @param {number} sampleWidth width of the sample image (in whatever - only used for aspect ratio) |
| 135 | * @param {number} sampleHeight height of the sample image (in whatever - only used for aspect ratio) |
| 136 | * @return {ThumbnailWidth} |
| 137 | */ |
| 138 | calculateWidths( boundingWidth, boundingHeight, sampleWidth, sampleHeight ) { |
| 139 | const ratio = sampleHeight / sampleWidth; |
| 140 | |
| 141 | const cssWidth = this.calculateFittingWidth( boundingWidth, boundingHeight, sampleWidth, sampleHeight ); |
| 142 | const cssHeight = Math.max( 1, Math.round( cssWidth * ratio ) ); |
| 143 | |
| 144 | const screenPixelWidth = Math.max( 1, cssWidth * this.devicePixelRatio ); |
| 145 | |
| 146 | const bucketedWidth = this.findNextBucket( screenPixelWidth ); |
| 147 | |
| 148 | return new ThumbnailWidth( cssWidth, cssHeight, screenPixelWidth, bucketedWidth ); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | module.exports = ThumbnailWidthCalculator; |