-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
143 lines (123 loc) · 4.13 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
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
/**
* MIT License
*
* Copyright (c) 2017 Harsh Gupta
* https://github.com/hgupta/svg-text-wrap
*/
'use strict'
;((context, name) => {
if (
!(
context.document &&
context.location &&
context.alert &&
context.setInterval
)
) {
throw new Error('[SVGTextWrap] Browser not detected')
}
const CONTINUATOR = '\u2026'
const getCanvasContext = (fontSize, fontName) => {
const ctx = context.document.createElement('canvas').getContext('2d')
ctx.font = `${fontSize}px ${fontName}`
return ctx
}
const getMaxChords = (radius, fontSize) => Math.floor(radius * 2 / fontSize)
const _wrapper = (opts, ctx, words, maxChordsAllowed) => {
const measureTextWidthInCanvas = (ctx, text) => ctx.measureText(text).width
const trimWord = (appendTo, word, availableWidth) =>
appendTo +
' ' +
Array(word.length)
.fill(word)
.map((w, i) => w.substr(0, word.length - i - 2) + CONTINUATOR)
.slice(0, -2)
.filter(w => measureTextWidthInCanvas(ctx, w) < availableWidth)
.shift()
const getChordWidth = height =>
Math.sqrt(opts.radius * opts.radius - height * height) * 2
const createEmptyChord = (chordCount, chordIndex) => {
let halfChordCount = chordCount / 2
let rule = (1 + chordIndex - halfChordCount) * opts.lineHeight
let atHeight = chordIndex < halfChordCount ? rule - opts.lineHeight : rule
let remainingWidth = getChordWidth(atHeight)
return { rule, remainingWidth, text: '' }
}
const areAllChordsFilled = chords =>
chords.filter(chord => chord.text.length === 0).length === 0
const fitTextInChord = (remainingWidth, wordsConsumed = 0, text = '') => {
const textWidth = measureTextWidthInCanvas(
ctx,
` ${words[wordsConsumed]}`
)
if (wordsConsumed >= words.length || textWidth > remainingWidth) {
return [wordsConsumed, remainingWidth, text]
}
text = `${text} ${words[wordsConsumed]}`
remainingWidth = remainingWidth - textWidth
return fitTextInChord(remainingWidth, wordsConsumed + 1, text)
}
const fitLabelInChords = (
chordCount,
chords = [],
wordsConsumed = 0,
chordIndex = 0
) => {
if (chordIndex === chordCount) {
if (wordsConsumed < words.length) {
let chord = chords[chordCount - 1]
chord.text = trimWord(
chord.text,
words[wordsConsumed],
chord.remainingWidth
)
}
return { chords, wordsConsumed }
}
let chord = createEmptyChord(chordCount, chordIndex)
;[wordsConsumed, chord.remainingWidth, chord.text] = fitTextInChord(
chord.remainingWidth,
wordsConsumed
)
chords.push(chord)
return fitLabelInChords(chordCount, chords, wordsConsumed, chordIndex + 1)
}
const wrap = (currentChordCount = 1, betterChords = []) => {
if (currentChordCount >= maxChordsAllowed) return betterChords
let { chords, wordsConsumed } = fitLabelInChords(currentChordCount)
if (areAllChordsFilled(chords)) betterChords = chords
if (wordsConsumed >= words.length) return betterChords
return wrap(currentChordCount + 1, betterChords)
}
return wrap
}
const setDefaultOptions = options =>
Object.assign(
{
splitter: lbl => lbl.split(' '),
fontSize: 10,
fontName: 'sans-serif',
lineHeight: 12,
radius: 40
},
options
)
const main = (label, options = {}) => {
if (typeof label !== 'string') {
throw new Error('[SVGTextWrap] [TypeMismatch] Label must be a string.')
}
const opts = setDefaultOptions(options)
const words = opts.splitter.call(null, label)
const ctx = getCanvasContext(opts.fontSize, opts.fontName)
const maxChords = getMaxChords(opts.radius, opts.fontSize)
const wrap = _wrapper(opts, ctx, words, maxChords)
return wrap()
}
if (typeof context !== 'undefined') {
if (context.hasOwnProperty(name)) {
context[`@__${name}__`] = context[name]
context[name] = undefined
}
context[name] = main
}
})(window, 'SVGTextWrap')