-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfontFile.php
More file actions
169 lines (165 loc) · 5.86 KB
/
fontFile.php
File metadata and controls
169 lines (165 loc) · 5.86 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
158
159
160
161
162
163
164
165
166
167
168
169
<?php
require_once('config.php');
require_once('mysql.php');
require_once('font.php');
require_once('vendor/autoload.php');
function GetAllFontsFilename(string $fontdir) {
$dir = new RecursiveDirectoryIterator(
$fontdir,
RecursiveDirectoryIterator::KEY_AS_FILENAME |
RecursiveDirectoryIterator::CURRENT_AS_FILEINFO
);
$files = new RegexIterator(
new RecursiveIteratorIterator($dir),
'#^.*\.([tT][tT][fF]|[tT][tT][cC]|[oO][tT][fF])$#',
RegexIterator::MATCH,
RegexIterator::USE_KEY
);
return $files;
}
function ParseFontByFile(string $fontPath): array {
$fontsInfoArr = [];
try {
$fontsInfo = FontLib\Font::load($fontPath);
if ($fontsInfo instanceof FontLib\TrueType\Collection) {
while ($fontsInfo->valid()) {
$font = $fontsInfo->current();
$font->parse();
for ($i = 0; $i < 5; $i++) {
foreach (LanguageID as &$languageID) {
$fontname = $font->getFontName(3, $i, $languageID);
$fontfullname = $font->getFontFullName(3, $i, $languageID);
$fontpsname = $font->getFontPostscriptName(3, $i, $languageID);
if (empty($fontfullname)) {
if (empty($fontname)) {
if (empty($fontpsname)) {
continue;
}
$fontfullname = $fontpsname;
} else {
$fontfullname = $fontname;
}
} else if (empty($fontname)) {
$fontname = $fontfullname;
}
$fontsInfoArr[] = [$fontname, $fontfullname, $fontpsname, $font->getFontSubfamily(3, $i, $languageID), $font->getFontVersion(3, $i, $languageID)];
}
}
$fontsInfo->next();
}
} else {
$fontsInfo->parse();
for ($i = 0; $i < 5; $i++) {
foreach (LanguageID as &$languageID) {
$fontname = $fontsInfo->getFontName(3, $i, $languageID);
$fontfullname = $fontsInfo->getFontFullName(3, $i, $languageID);
$fontpsname = $fontsInfo->getFontPostscriptName(3, $i, $languageID);
if (empty($fontfullname)) {
if (empty($fontname)) {
if (empty($fontpsname)) {
continue;
}
$fontfullname = $fontpsname;
} else {
$fontfullname = $fontname;
}
} else if (empty($fontname)) {
$fontname = $fontfullname;
}
if (substr_count($fontname, '?') > 3 && substr_count($fontfullname, '?') > 3) {
continue;
}
$fontsInfoArr[] = [$fontname, $fontfullname, $fontpsname, $fontsInfo->getFontSubfamily(3, $i, $languageID), $fontsInfo->getFontVersion(3, $i, $languageID)];
}
}
}
try {
$fontsInfo->close();
} catch (Throwable $e) {
}
unset($font, $fontsInfo);
} catch (Throwable $e) {
return [-1, "跳过错误字体: {$e->getMessage()}", $fontsInfoArr];
}
return [0, '', $fontsInfoArr];
}
function AddFontFile(string $fontPath, bool $localScraper = false): array {
$additionalMsgList = [];
if (!is_file($fontPath)) {
return [-1, '跳过错误文件', $additionalMsgList];
}
$fileSize = filesize($fontPath);
if ($fileSize <= 0) {
return [-1, '跳过空文件', $additionalMsgList];
}
$fontFileInfo = pathinfo($fontPath);
$fontExt = strtolower($fontFileInfo['extension']);
if (!in_array($fontExt, ['ttf', 'ttc'])) {
if (!$localScraper || $fontExt !== 'otf') {
return [-1, '跳过不支持文件', $additionalMsgList];
}
}
if ($fontExt === 'otf') {
if (!is_file('/usr/local/bin/ftcli')) {
return [-1, '找不到 ftcli', $additionalMsgList];
}
system("/usr/local/bin/ftcli converter otf2ttf -out " . escapeshellarg("{$fontFileInfo['dirname']}/") . " " . escapeshellarg($fontPath), $retcode);
if ($retcode > 0) {
return [-1, "转换 otf 失败: {$fontPath}", $additionalMsgList];
}
return [-1, "转换 otf 成功: {$fontPath}", $additionalMsgList];
//unlink($fontPath);
$fontExt = 'ttf';
$fontPath = ($fontFileInfo['dirname'] . '/' . $fontFileInfo['filename'] . ".ttf");
}
$fontFilename = preg_replace('/\d{10,}/', '', $fontFileInfo['filename']);
$fontsInfoArr = [];
list($fontParseErr, $fontParseErrMsg, $fontsInfoArr) = ParseFontByFile($fontPath);
if ($fontParseErr > 0) {
return [$fontParseErr, $fontParseErrMsg, $additionalMsgList];
}
$hasDupe = false;
foreach ($fontsInfoArr as $key => &$fontsInfo) {
$fontArr = DetectDuplicateFont($fontExt, $fontsInfo[0], $fontsInfo[1], $fontsInfo[3], true);
if ($fontArr[0] > 0) {
//if (preg_replace('/\d{10,}/', '', strtolower($fontArr[1])) === strtolower("{$fontFilename}.{$fontExt}")) {
$hasDupe = true;
$additionalMsgList[] = [-1, "跳过重复字体: {$fontsInfo[0]}, {$fontsInfo[1]}, {$fontsInfo[2]}, {$fontsInfo[3]}"];
unset($fontsInfoArr[$key]);
continue;
//}
} else if ($fontArr[0] < 0) {
$additionalMsgList[] = [-1, "跳过错误字体: {$fontsInfo[0]}, {$fontsInfo[1]}, {$fontsInfo[2]}, {$fontsInfo[3]} (Errno: {$fontArr[0]}"];
unset($fontsInfoArr[$key]);
}
}
if (count($fontsInfoArr) <= 0) {
return [-1, "跳过空字体", $additionalMsgList];
}
if ($hasDupe) {
$fontFilename .= time();
}
$fontFilename .= ".{$fontExt}";
$newFontPath = GetMainFontPath($fontFilename);
if (!rename($fontPath, $newFontPath)) {
return [-1, "移动字体失败: {$fontPath} -> {$newFontPath}", $additionalMsgList];
}
$additionalMsgList[] = [0, "移动字体成功: {$fontPath} -> {$newFontPath}"];
$rowID = AddFontMeta(1, $fontFilename, $fileSize, true);
if ($rowID <= 0) {
if ($localScraper) {
unlink($newFontPath);
} else {
copy($newFontPath, $fontPath);
}
return [-1, "添加字体元数据失败: {$newFontPath}", $additionalMsgList];
}
foreach ($fontsInfoArr as &$fontsInfo) {
if (!AddFont($rowID, $fontsInfo[0], $fontsInfo[1], $fontsInfo[2], $fontsInfo[3], $fontsInfo[4])) {
$additionalMsgList[] = [-1, "添加字体失败: {$rowID}, {$fontsInfo[0]}, {$fontsInfo[1]}, {$fontsInfo[2]}, {$fontsInfo[3]}, {$fontsInfo[4]}"];
}
$additionalMsgList[] = [0, "添加字体成功: {$rowID}, {$fontsInfo[0]}, {$fontsInfo[1]}, {$fontsInfo[2]}, {$fontsInfo[3]}, {$fontsInfo[4]}"];
}
return [0, '', $additionalMsgList];
}
?>