-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
EFont.ts
84 lines (77 loc) 路 1.79 KB
/
EFont.ts
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
import type { PropType } from 'vue'
import { defineComponent, h } from 'vue'
type FallbackFont =
| 'Arial'
| 'Helvetica'
| 'Verdana'
| 'Georgia'
| 'Times New Roman'
| 'serif'
| 'sans-serif'
| 'monospace'
| 'cursive'
| 'fantasy'
type FontFormat =
| 'woff'
| 'woff2'
| 'truetype'
| 'opentype'
| 'embedded-opentype'
| 'svg'
type FontWeight = 'normal' | 'bold' | 'bolder' | 'lighter' | number
type FontStyle = 'normal' | 'italic' | 'oblique'
export default defineComponent({
name: 'EFont',
props: {
fontFamily: {
type: String,
required: true,
},
fallbackFontFamily: {
type: [String, Array] as PropType<FallbackFont | FallbackFont[]>,
default: 'Arial',
},
webFont: {
type: Object as PropType<{ url: string, format: FontFormat }>,
default: undefined,
},
fontStyle: {
type: String as PropType<FontStyle>,
default: 'normal',
},
fontWeight: {
type: [String, Number] as PropType<FontWeight>,
default: 400,
},
},
setup({ fontFamily, fallbackFontFamily, webFont, fontStyle, fontWeight }) {
const src = webFont
? `src: url(${webFont.url}) format('${webFont.format}');`
: ''
const style = `
@font-face {
font-family: '${fontFamily}';
font-style: ${fontStyle};
font-weight: ${fontWeight};
mso-font-alt: '${
Array.isArray(fallbackFontFamily)
? fallbackFontFamily[0]
: fallbackFontFamily
}';
${src}
}
* {
font-family: '${fontFamily}', ${
Array.isArray(fallbackFontFamily)
? fallbackFontFamily.join(', ')
: fallbackFontFamily
};
}
`
return () => {
return h('style', {
innerHTML: style,
})
}
},
})