-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
163 lines (156 loc) · 4.67 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* @file mofron-effect-font/index.js
* @brief set text font for mofron-comp-text component
* format configure for each file automatically if you use local font
* @attention this effect is private, this function is included in text component. so you don't need to use directly
* @license MIT
*/
const comutl = mofron.util.common;
module.exports = class extends mofron.class.Effect {
/**
* initialize effect
*
* @param (mixed) fname parameter
* key-value: effect config
* @param (string) path parameter
* @short fname,path
* @type private
*/
constructor (p1,p2) {
try {
super();
this.name("Font");
this.shortForm("fname", "path");
/* init config */
this.confmng().add("fname", { type: "array" });
this.confmng().add("path", { type: "string" });
/* set config */
if (0 < arguments.length) {
this.config(p1,p2);
}
} catch (e) {
console.error(e.stack);
throw e;
}
}
/**
* font name setter/getter
*
* @param (mixed) string: primary font name
* undefined: call as getter
* @param (string) secondary font name (not required)
* @return (array) font name [primary, secondary]
* null: not set
* @type parameter
*/
fname (p1, p2) {
try {
if (0 === arguments.length) {
/* getter */
return this.confmng("fname");
}
/* setter */
if ( ("string" !== typeof p1) ||
((undefined !== p2) && ("string" !== typeof p2)) ) {
throw new Error("invalid parameter");
}
let set_val = [undefined,undefined];
set_val[0] = (-1 !== p1.indexOf(" ")) ? "'" + p1 + "'" : p1;
if ("string" === typeof p2) {
set_val[1] = (-1 !== p2.indexOf(" ")) ? "'" + p2 + "'" : p2;
}
this.confmng("fname", set_val);
} catch (e) {
console.error(e.stack);
throw e;
}
}
/**
* setter/getter path to font
*
* @param (mixed) string: path to font
* undefined: call as getter
* @return (string) path to font
* null: not set
* @type parameter
*/
path (prm) {
try {
return this.confmng("path", prm);
} catch (e) {
console.error(e.stack);
throw e;
}
}
/**
* setter/getter path to font, same as path config
*
* @param (mixed) string: path to font
* undefined: call as getter
* @return (string) path to font
* null: not set
* @type parameter
*/
src (prm) {
try {
return this.confmng("path", prm);
} catch (e) {
console.error(e.stack);
throw e;
}
}
/**
* set font-face setting in style tag
*
* @param (string) font-family value
* @type private
*/
addFontFace (fnm) {
try {
/* format */
let pth_spt = this.path().split('.');
let format = '';
if ('woff' === pth_spt[pth_spt.length-1]) {
format = "format('woff')";
} else if ('ttf' === pth_spt[pth_spt.length-1]) {
format = "format('truetype')";
} else if ('otf' === pth_spt[pth_spt.length-1]) {
format = "format('opentype')";
} else if ('eot' === pth_spt[pth_spt.length-1]) {
format = "format('embedded-opentype')";
} else if ( ('svg' === pth_spt[pth_spt.length-1]) || ('svgz' === pth_spt[pth_spt.length-1])) {
format = "format('svg')";
}
let val = "@font-face {";
val += "font-family:" + fnm + ",";
val += "src:" + "url('" + this.path() + "') " + format + "}";
comutl.addhead("style", { type: "text/css", test:"test" }, val);
} catch (e) {
console.error(e.stack);
throw e;
}
}
/**
* set text font
*
* @param (mofron.class.Component) component object
* @type private
*/
contents (cmp) {
try {
let fname = this.fname();
if (null === fname) {
throw new Error("could not find font name config");
}
let set_fnt = (undefined === fname[1]) ? fname[0] : fname[0]+','+fname[1];
if (null !== this.path()) {
this.addFontFace(set_fnt);
}
cmp.style({ 'font-family' : set_fnt });
} catch (e) {
console.error(e.stack);
throw e;
}
}
}
/* end of file */