|
@@ -6,73 +6,145 @@ module.exports = function(theme) {
|
6
|
6
|
var left = ifNumeric(theme.captionLeft, 0),
|
7
|
7
|
right = ifNumeric(theme.captionRight, theme.width),
|
8
|
8
|
bottom = ifNumeric(theme.captionBottom, null),
|
9
|
|
- top = ifNumeric(theme.captionTop, null);
|
|
9
|
+ top = ifNumeric(theme.captionTop, null),
|
|
10
|
+ labelLeft = ifNumeric(theme.labelLeft, 0),
|
|
11
|
+ labelRight = ifNumeric(theme.labelRight, theme.width),
|
|
12
|
+ labelBottom = ifNumeric(theme.labelBottom, null),
|
|
13
|
+ labelTop = ifNumeric(theme.labelTop, null),
|
|
14
|
+
|
|
15
|
+ renderfunctions ;
|
10
|
16
|
|
11
|
17
|
if (bottom === null && top === null) {
|
12
|
18
|
top = 0;
|
13
|
19
|
}
|
14
|
20
|
|
15
|
|
- var captionWidth = right - left;
|
|
21
|
+ if (labelBottom === null && labelTop === null) {
|
|
22
|
+ labelTop = 0;
|
|
23
|
+ }
|
|
24
|
+
|
|
25
|
+ var captionWidth = right - left,
|
|
26
|
+ labelWidth = labelRight - labelLeft;
|
16
|
27
|
|
17
|
|
- return function(context, caption) {
|
|
28
|
+ return function(context, caption, type) {
|
18
|
29
|
|
19
|
30
|
if (!caption) {
|
20
|
31
|
return;
|
21
|
32
|
}
|
22
|
33
|
|
23
|
|
- var lines = [[]],
|
24
|
|
- maxWidth = 0,
|
25
|
|
- words = smartquotes(caption + "").trim().replace(/\s\s+/g, " \n").split(/ /g);
|
|
34
|
+ if (type === 'caption') {
|
|
35
|
+ var lines = [[]],
|
|
36
|
+ maxWidth = 0,
|
|
37
|
+ words = smartquotes(caption + "").trim().replace(/\s\s+/g, " \n").split(/ /g);
|
26
|
38
|
|
27
|
|
- context.font = theme.captionFont;
|
28
|
|
- context.textBaseline = "top";
|
29
|
|
- context.textAlign = theme.captionAlign || "center";
|
|
39
|
+ context.font = theme.captionFont;
|
|
40
|
+ context.textBaseline = "top";
|
|
41
|
+ context.textAlign = theme.captionAlign || "center";
|
30
|
42
|
|
31
|
|
-
|
32
|
|
-
|
33
|
|
- words.forEach(function(word,i){
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+ words.forEach(function(word,i){
|
34
|
46
|
|
35
|
|
- var width = context.measureText(lines[lines.length - 1].concat([word]).join(" ")).width;
|
|
47
|
+ var width = context.measureText(lines[lines.length - 1].concat([word]).join(" ")).width;
|
36
|
48
|
|
37
|
|
- if (word[0] === "\n" || (lines[lines.length - 1].length && width > captionWidth)) {
|
|
49
|
+ if (word[0] === "\n" || (lines[lines.length - 1].length && width > captionWidth)) {
|
38
|
50
|
|
39
|
|
- word = word.trim();
|
40
|
|
- lines.push([word]);
|
41
|
|
- width = context.measureText(word).width;
|
|
51
|
+ word = word.trim();
|
|
52
|
+ lines.push([word]);
|
|
53
|
+ width = context.measureText(word).width;
|
42
|
54
|
|
43
|
|
- } else {
|
|
55
|
+ } else {
|
|
56
|
+
|
|
57
|
+ lines[lines.length - 1].push(word);
|
|
58
|
+
|
|
59
|
+ }
|
|
60
|
+
|
|
61
|
+ maxWidth = Math.max(maxWidth,width);
|
44
|
62
|
|
45
|
|
- lines[lines.length - 1].push(word);
|
|
63
|
+ });
|
46
|
64
|
|
|
65
|
+ var totalHeight = lines.length * theme.captionLineHeight + (lines.length - 1) * theme.captionLineSpacing;
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+ var x = theme.captionAlign === "left" ? left : theme.captionAlign === "right" ? right : (left + right) / 2;
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+ var y;
|
|
72
|
+
|
|
73
|
+ if (top !== null && bottom !== null) {
|
|
74
|
+
|
|
75
|
+ y = (bottom + top - totalHeight) / 2;
|
|
76
|
+ } else if (bottom !== null) {
|
|
77
|
+
|
|
78
|
+ y = bottom - totalHeight;
|
|
79
|
+ } else {
|
|
80
|
+
|
|
81
|
+ y = top;
|
47
|
82
|
}
|
48
|
83
|
|
49
|
|
- maxWidth = Math.max(maxWidth,width);
|
|
84
|
+
|
|
85
|
+ context.fillStyle = theme.captionColor;
|
|
86
|
+ lines.forEach(function(line, i){
|
|
87
|
+ context.fillText(line.join(" "), x, y + i * (theme.captionLineHeight + theme.captionLineSpacing));
|
|
88
|
+ });
|
|
89
|
+ }
|
50
|
90
|
|
51
|
|
- });
|
52
|
91
|
|
53
|
|
- var totalHeight = lines.length * theme.captionLineHeight + (lines.length - 1) * theme.captionLineSpacing;
|
|
92
|
+ if (type === 'label') {
|
|
93
|
+ var lines = [[]],
|
|
94
|
+ maxWidth = 0,
|
|
95
|
+ words = smartquotes(caption + "").trim().replace(/\s\s+/g, " \n").split(/ /g);
|
54
|
96
|
|
55
|
|
-
|
56
|
|
- var x = theme.captionAlign === "left" ? left : theme.captionAlign === "right" ? right : (left + right) / 2;
|
|
97
|
+ context.font = theme.labelFont;
|
|
98
|
+ context.textBaseline = "top";
|
|
99
|
+ context.textAlign = theme.labelAlign || "center";
|
57
|
100
|
|
58
|
|
-
|
59
|
|
- var y;
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+ words.forEach(function(word,i){
|
60
|
104
|
|
61
|
|
- if (top !== null && bottom !== null) {
|
62
|
|
-
|
63
|
|
- y = (bottom + top - totalHeight) / 2;
|
64
|
|
- } else if (bottom !== null) {
|
65
|
|
-
|
66
|
|
- y = bottom - totalHeight;
|
67
|
|
- } else {
|
68
|
|
-
|
69
|
|
- y = top;
|
70
|
|
- }
|
|
105
|
+ var width = context.measureText(lines[lines.length - 1].concat([word]).join(" ")).width;
|
|
106
|
+
|
|
107
|
+ if (word[0] === "\n" || (lines[lines.length - 1].length && width > labelWidth)) {
|
71
|
108
|
|
72
|
|
- context.fillStyle = theme.captionColor;
|
73
|
|
- lines.forEach(function(line, i){
|
74
|
|
- context.fillText(line.join(" "), x, y + i * (theme.captionLineHeight + theme.captionLineSpacing));
|
75
|
|
- });
|
|
109
|
+ word = word.trim();
|
|
110
|
+ lines.push([word]);
|
|
111
|
+ width = context.measureText(word).width;
|
|
112
|
+
|
|
113
|
+ } else {
|
|
114
|
+
|
|
115
|
+ lines[lines.length - 1].push(word);
|
|
116
|
+
|
|
117
|
+ }
|
|
118
|
+
|
|
119
|
+ maxWidth = Math.max(maxWidth,width);
|
|
120
|
+
|
|
121
|
+ });
|
|
122
|
+
|
|
123
|
+ var totalHeight = lines.length * theme.labelLineHeight + (lines.length - 1) * theme.labelLineSpacing;
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+ var x = theme.labelAlign === "left" ? left : theme.labelAlign === "right" ? right : (left + right) / 2;
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+ var y;
|
|
130
|
+
|
|
131
|
+ if (top !== null && bottom !== null) {
|
|
132
|
+
|
|
133
|
+ y = (bottom + top - totalHeight) / 2;
|
|
134
|
+ } else if (bottom !== null) {
|
|
135
|
+
|
|
136
|
+ y = bottom - totalHeight;
|
|
137
|
+ } else {
|
|
138
|
+
|
|
139
|
+ y = top;
|
|
140
|
+ }
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+ context.fillStyle = theme.labelColor;
|
|
144
|
+ lines.forEach(function(line, i){
|
|
145
|
+ context.fillText(line.join(" "), x, y + i * (theme.labelLineHeight + theme.labelLineSpacing));
|
|
146
|
+ });
|
|
147
|
+ }
|
76
|
148
|
|
77
|
149
|
};
|
78
|
150
|
|