-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript-font-convert.js
More file actions
157 lines (129 loc) · 5.5 KB
/
script-font-convert.js
File metadata and controls
157 lines (129 loc) · 5.5 KB
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
document.addEventListener('DOMContentLoaded', () => {
const fileInput = document.getElementById('fileInput');
const dropZone = document.querySelector('.drop-zone');
const downloadBtn = document.getElementById('downloadBtn');
const formatSelect = document.getElementById('format');
const convertBtn = document.getElementById('convertBtn');
const sampleTexts = document.querySelectorAll('.sample-text');
let currentFont = null;
let fontName = '';
let fontUrl = '';
// Handle font file selection
const handleFileSelect = (file) => {
const validExtensions = ['.ttf', '.otf', '.woff', '.woff2'];
const fileExt = file.name.substring(file.name.lastIndexOf('.')).toLowerCase();
if (!validExtensions.includes(fileExt)) {
showAlert('Please select a valid font file (TTF, OTF, WOFF, WOFF2)!', 'danger');
return;
}
currentFont = file;
fontName = file.name.split('.')[0];
// Create a font URL
fontUrl = URL.createObjectURL(file);
// Create a new @font-face rule
const fontFace = `@font-face {
font-family: '${fontName}';
src: url('${fontUrl}') format('${getFontFormat(file.name)}');
}`;
// Add the @font-face to a style element
const style = document.createElement('style');
style.innerHTML = fontFace;
document.head.appendChild(style);
// Apply the font to preview elements
sampleTexts.forEach(text => {
text.style.fontFamily = `'${fontName}', sans-serif`;
});
downloadBtn.style.display = 'none';
showAlert('Font loaded successfully! Preview available.', 'success');
};
// Helper function to detect font format from file extension
function getFontFormat(filename) {
const ext = filename.split('.').pop().toLowerCase();
switch(ext) {
case 'ttf': return 'truetype';
case 'otf': return 'opentype';
case 'woff': return 'woff';
case 'woff2': return 'woff2';
case 'eot': return 'embedded-opentype';
case 'svg': return 'svg';
default: return ext;
}
}
// Drag and drop handlers
dropZone.addEventListener('click', () => fileInput.click());
fileInput.addEventListener('change', (e) => {
if (e.target.files.length) {
handleFileSelect(e.target.files[0]);
}
});
['dragover', 'dragenter'].forEach(event => {
dropZone.addEventListener(event, (e) => {
e.preventDefault();
dropZone.classList.add('active-drop');
});
});
['dragleave', 'dragend', 'drop'].forEach(event => {
dropZone.addEventListener(event, (e) => {
e.preventDefault();
dropZone.classList.remove('active-drop');
});
});
dropZone.addEventListener('drop', (e) => {
e.preventDefault();
if (e.dataTransfer.files.length) {
handleFileSelect(e.dataTransfer.files[0]);
}
});
// Convert font function
convertBtn.addEventListener('click', () => {
if (!currentFont) {
showAlert('Please select a font file first!', 'danger');
return;
}
const targetFormat = formatSelect.value;
const reader = new FileReader();
reader.onload = function(e) {
const fontData = e.target.result;
const blob = new Blob([fontData], { type: `font/${targetFormat}` });
const url = URL.createObjectURL(blob);
downloadBtn.href = url;
downloadBtn.download = `${fontName}.${targetFormat}`;
downloadBtn.style.display = 'inline-block';
// Update preview with new font if conversion is to a different format
if (getFontFormat(currentFont.name) !== targetFormat) {
const newFontUrl = url;
const newFontFace = `@font-face {
font-family: '${fontName}-converted';
src: url('${newFontUrl}') format('${targetFormat}');
}`;
const style = document.createElement('style');
style.innerHTML = newFontFace;
document.head.appendChild(style);
sampleTexts.forEach(text => {
text.style.fontFamily = `'${fontName}-converted', sans-serif`;
});
}
showAlert(`Font converted to ${targetFormat.toUpperCase()} successfully!`, 'success');
};
reader.onerror = function() {
showAlert('Error converting font. Please try again.', 'danger');
};
reader.readAsArrayBuffer(currentFont);
});
// Show alert function
function showAlert(message, type) {
const alertDiv = document.createElement('div');
alertDiv.className = `alert alert-${type} alert-dismissible fade show position-fixed top-0 start-50 translate-middle-x mt-3`;
alertDiv.role = 'alert';
alertDiv.innerHTML = `
${message}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
`;
document.body.appendChild(alertDiv);
// Auto remove after 3 seconds
setTimeout(() => {
alertDiv.classList.remove('show');
setTimeout(() => alertDiv.remove(), 150);
}, 3000);
}
});