This repository has been archived by the owner on May 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
76 lines (58 loc) · 2.42 KB
/
index.js
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
function ScaleText(listOfText, height, width) {
var fontSizeForSingleLetter = 10
var widthOfSingleLetter = 6
var heightOfSingleLetter = 13
var fontPaddingInCharacters = 2
function flatten(array) {
return [].concat.apply([], array);
}
function findLongestWord() {
var individualWords = flatten(listOfText.map(function (item) {
return item.split(' ')
}))
return longestStringInArray(individualWords);
}
function longestStringInArray(words) {
return words.reduce(function (largestFound, candidate) {
return Math.max(candidate.length, largestFound)
}, 0)
}
function linesForWord(words) {
var longestWordSize = findLongestWord() + fontPaddingInCharacters
var lineNumber = 1
var currentLinePosition = 0
words.forEach(function (word) {
if ((word.length + currentLinePosition) > longestWordSize) {
lineNumber++
currentLinePosition = 0
}
currentLinePosition += word.length + 1
})
return lineNumber
}
this.maxFontSizeByWidth = function () {
var longestWord = findLongestWord() + fontPaddingInCharacters
var longestWordSize = (widthOfSingleLetter * longestWord)
return (width / longestWordSize) * fontSizeForSingleLetter
}
this.maxFontSizeByHeight = function () {
var numberOfLines = listOfText.map(function (item) {
return linesForWord(item.split(' '))
})
var largestNumberOfLines = Math.max.apply(Math, numberOfLines)
var maximumFontHeightInPixels = (height / largestNumberOfLines) / heightOfSingleLetter
return maximumFontHeightInPixels * fontSizeForSingleLetter
}
this.singleLineIdeal = function() {
var longestSentence = longestStringInArray(listOfText) + fontPaddingInCharacters
var longestSentenceSize = (widthOfSingleLetter * longestSentence)
var fontSizeByWidth = (width / longestSentenceSize) * fontSizeForSingleLetter
var maximumFontHeightInPixels = height / heightOfSingleLetter
var fontSizeByHeight = maximumFontHeightInPixels * fontSizeForSingleLetter
return Math.floor(Math.min(fontSizeByWidth, fontSizeByHeight))
}
this.ideal = function () {
return Math.floor(Math.min(this.maxFontSizeByWidth(), this.maxFontSizeByHeight()))
}
}
module.exports = ScaleText