summaryrefslogtreecommitdiff
blob: 8accd552b710a4445ea435ad0e10307461f3e15b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/**
 * External dependencies
 */
import {
	drop,
	every,
	isEqual,
	map,
	overEvery,
	some,
	sum,
	take,
	takeRight,
	takeWhile,
	zipWith,
} from 'lodash';

export function imagesToRatios( images ) {
	return map( images, ratioFromImage );
}

export function ratioFromImage( { height, width } ) {
	return height && width ? width / height : 1;
}

/**
 * Build three columns, each of which should contain approximately 1/3 of the total ratio
 *
 * @param  {Array.<number>}         ratios      Ratios of images put into shape
 * @param  {number}                 columnCount Number of columns
 *
 * @return {Array.<Array.<number>>}             Shape of rows and columns
 */
export function ratiosToColumns( ratios, columnCount ) {
	// If we don't have more than 1 per column, just return a simple 1 ratio per column shape
	if ( ratios.length <= columnCount ) {
		return [ Array( ratios.length ).fill( 1 ) ];
	}

	const total = sum( ratios );
	const targetColRatio = total / columnCount;

	const row = [];
	let toProcess = ratios;
	let accumulatedRatio = 0;

	// We skip the last column in the loop and add rest later
	for ( let i = 0; i < columnCount - 1; i++ ) {
		const colSize = takeWhile( toProcess, ratio => {
			const shouldTake = accumulatedRatio <= ( i + 1 ) * targetColRatio;
			if ( shouldTake ) {
				accumulatedRatio += ratio;
			}
			return shouldTake;
		} ).length;
		row.push( colSize );
		toProcess = drop( toProcess, colSize );
	}

	// Don't calculate last column, just add what's left
	row.push( toProcess.length );

	// A shape is an array of rows. Wrap our row in an array.
	return [ row ];
}

/**
 * These are partially applied functions.
 * They rely on helper function (defined below) to create a function that expects to be passed ratios
 * during processing.
 *
 * …FitsNextImages() functions should be passed ratios to be processed
 * …IsNotRecent() functions should be passed the processed shapes
 */

const reverseSymmetricRowIsNotRecent = isNotRecentShape( [ 2, 1, 2 ], 5 );
const reverseSymmetricFitsNextImages = checkNextRatios( [
	isLandscape,
	isLandscape,
	isPortrait,
	isLandscape,
	isLandscape,
] );
const longSymmetricRowFitsNextImages = checkNextRatios( [
	isLandscape,
	isLandscape,
	isLandscape,
	isPortrait,
	isLandscape,
	isLandscape,
	isLandscape,
] );
const longSymmetricRowIsNotRecent = isNotRecentShape( [ 3, 1, 3 ], 5 );
const symmetricRowFitsNextImages = checkNextRatios( [
	isPortrait,
	isLandscape,
	isLandscape,
	isPortrait,
] );
const symmetricRowIsNotRecent = isNotRecentShape( [ 1, 2, 1 ], 5 );
const oneThreeFitsNextImages = checkNextRatios( [
	isPortrait,
	isLandscape,
	isLandscape,
	isLandscape,
] );
const oneThreeIsNotRecent = isNotRecentShape( [ 1, 3 ], 3 );
const threeOneIsFitsNextImages = checkNextRatios( [
	isLandscape,
	isLandscape,
	isLandscape,
	isPortrait,
] );
const threeOneIsNotRecent = isNotRecentShape( [ 3, 1 ], 3 );
const oneTwoFitsNextImages = checkNextRatios( [
	lt( 1.6 ),
	overEvery( gte( 0.9 ), lt( 2 ) ),
	overEvery( gte( 0.9 ), lt( 2 ) ),
] );
const oneTwoIsNotRecent = isNotRecentShape( [ 1, 2 ], 3 );
const fiveIsNotRecent = isNotRecentShape( [ 1, 1, 1, 1, 1 ], 1 );
const fourIsNotRecent = isNotRecentShape( [ 1, 1, 1, 1 ], 1 );
const threeIsNotRecent = isNotRecentShape( [ 1, 1, 1 ], 3 );
const twoOneFitsNextImages = checkNextRatios( [
	overEvery( gte( 0.9 ), lt( 2 ) ),
	overEvery( gte( 0.9 ), lt( 2 ) ),
	lt( 1.6 ),
] );
const twoOneIsNotRecent = isNotRecentShape( [ 2, 1 ], 3 );
const panoramicFitsNextImages = checkNextRatios( [ isPanoramic ] );

export function ratiosToMosaicRows( ratios, { isWide } = {} ) {
	// This function will recursively process the input until it is consumed
	const go = ( processed, toProcess ) => {
		if ( ! toProcess.length ) {
			return processed;
		}

		let next;

		if (
			/* Reverse_Symmetric_Row */
			toProcess.length > 15 &&
			reverseSymmetricFitsNextImages( toProcess ) &&
			reverseSymmetricRowIsNotRecent( processed )
		) {
			next = [ 2, 1, 2 ];
		} else if (
			/* Long_Symmetric_Row */
			toProcess.length > 15 &&
			longSymmetricRowFitsNextImages( toProcess ) &&
			longSymmetricRowIsNotRecent( processed )
		) {
			next = [ 3, 1, 3 ];
		} else if (
			/* Symmetric_Row */
			toProcess.length !== 5 &&
			symmetricRowFitsNextImages( toProcess ) &&
			symmetricRowIsNotRecent( processed )
		) {
			next = [ 1, 2, 1 ];
		} else if (
			/* One_Three */
			oneThreeFitsNextImages( toProcess ) &&
			oneThreeIsNotRecent( processed )
		) {
			next = [ 1, 3 ];
		} else if (
			/* Three_One */
			threeOneIsFitsNextImages( toProcess ) &&
			threeOneIsNotRecent( processed )
		) {
			next = [ 3, 1 ];
		} else if (
			/* One_Two */
			oneTwoFitsNextImages( toProcess ) &&
			oneTwoIsNotRecent( processed )
		) {
			next = [ 1, 2 ];
		} else if (
			/* Five */
			isWide &&
			( toProcess.length === 5 || ( toProcess.length !== 10 && toProcess.length > 6 ) ) &&
			fiveIsNotRecent( processed ) &&
			sum( take( toProcess, 5 ) ) < 5
		) {
			next = [ 1, 1, 1, 1, 1 ];
		} else if (
			/* Four */
			isFourValidCandidate( processed, toProcess )
		) {
			next = [ 1, 1, 1, 1 ];
		} else if (
			/* Three */
			isThreeValidCandidate( processed, toProcess, isWide )
		) {
			next = [ 1, 1, 1 ];
		} else if (
			/* Two_One */
			twoOneFitsNextImages( toProcess ) &&
			twoOneIsNotRecent( processed )
		) {
			next = [ 2, 1 ];
		} else if ( /* Panoramic */ panoramicFitsNextImages( toProcess ) ) {
			next = [ 1 ];
		} else if ( /* One_One */ toProcess.length > 3 ) {
			next = [ 1, 1 ];
		} else {
			// Everything left
			next = Array( toProcess.length ).fill( 1 );
		}

		// Add row
		const nextProcessed = processed.concat( [ next ] );

		// Trim consumed images from next processing step
		const consumedImages = sum( next );
		const nextToProcess = toProcess.slice( consumedImages );

		return go( nextProcessed, nextToProcess );
	};
	return go( [], ratios );
}

function isThreeValidCandidate( processed, toProcess, isWide ) {
	const ratio = sum( take( toProcess, 3 ) );
	return (
		toProcess.length >= 3 &&
		toProcess.length !== 4 &&
		toProcess.length !== 6 &&
		threeIsNotRecent( processed ) &&
		( ratio < 2.5 ||
			( ratio < 5 &&
				/* nextAreSymettric */
				( toProcess.length >= 3 &&
					/* @FIXME floating point equality?? */ toProcess[ 0 ] === toProcess[ 2 ] ) ) ||
			isWide )
	);
}

function isFourValidCandidate( processed, toProcess ) {
	const ratio = sum( take( toProcess, 4 ) );
	return (
		( fourIsNotRecent( processed ) && ( ratio < 3.5 && toProcess.length > 5 ) ) ||
		( ratio < 7 && toProcess.length === 4 )
	);
}

function isNotRecentShape( shape, numRecents ) {
	return recents =>
		! some( takeRight( recents, numRecents ), recentShape => isEqual( recentShape, shape ) );
}

function checkNextRatios( shape ) {
	return ratios =>
		ratios.length >= shape.length &&
		every( zipWith( shape, ratios.slice( 0, shape.length ), ( f, r ) => f( r ) ) );
}

function isLandscape( ratio ) {
	return ratio >= 1 && ratio < 2;
}

function isPortrait( ratio ) {
	return ratio < 1;
}

function isPanoramic( ratio ) {
	return ratio >= 2;
}

// >=
function gte( n ) {
	return m => m >= n;
}

// <
function lt( n ) {
	return m => m < n;
}