-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-character-list.php
158 lines (135 loc) · 3.62 KB
/
generate-character-list.php
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
<?php
/**
* For the purpose of generating a list of characters, to be fed into https://yal.cc/r/20/pixelfont/
*
* Assumes:
* - that the tile is 9 rows wide
* - that the glyph files are contained in ./glyphs/
* - that all glyph files are raster images, preferably PNGs
* - that all glyph filenames match indexes in the array generated by generate_lookup_table()
* - that you have the `montage` program from ImageMagick installed: https://www.imagemagick.org/Usage/montage/
*/
/**
* Turn an array of characters into a string terminated by a newline
*
* @param Array $row Array of single characters
* @echo
*/
function output_row( $row ) {
return implode( $row, '' ) . "\n";
}
/**
* Draw from the AGLFN to create an array of character name lookups
*
* @uses fgetcsv
*
* @return Array of string Glyph name => string Unicode Value (hex)
*/
function generate_lookup_table() {
$semicolon_delimited = file( './agl-aglfn/aglfn.txt' );
foreach ( $semicolon_delimited as $iterator => $row ) {
if ( '#' === substr( $row, 0, 1 ) ) {
unset( $semicolon_delimited[$iterator] );
}
}
$lookup_table = array();
foreach ( $semicolon_delimited as $row ) {
$array = str_getcsv( $row, ';' );
// there are three items in this $array at this time:
// (1) Standard UV or CUS UV--four uppercase hexadecimal digits
// (2) Glyph name--upper/lowercase letters and digits
// (3) Character names: Unicode character names for standard UVs, and
// descriptive names for CUS UVs--uppercase letters, hyphen, and
// space
//
// Now, we turn that into 2 => 1
$lookup_table[$array[1]] = $array[0];
}
return $lookup_table;
}
/**
* Call out to imagemagick montage to generate the montage
*
* @param Array $files List of files with filename
* @uses exec
*/
function montage( $files ) {
error_log( "Beginning montage..." );
$output = null;
$return_var = null;
$file_list = implode( $files, ' ' );
$command = sprintf(
'cd glyphs/; montage %1$s -geometry +0+0 -tile 9x ../tileset.png',
$file_list
);
exec( $command, $output, $return_var );
error_log( implode( $output, "\n" ) );
error_log( "montage return: " . (string) $return_var );
return $return_var;
}
/**
* Load the files, generate the lists
*
* @return text of the character list
*/
function generate_list( $files ) {
$row = array();
$lookup_table = generate_lookup_table();
ob_start();
try {
foreach ( $files as $file ) {
if ( count( $row ) === 9 ) {
echo output_row( $row );
$row = array();
}
/*
* The bit where we generate the rows for the list
*/
$glyph_name = basename( $file, '.png' );
$uv = $lookup_table[$glyph_name];
if ( empty( $uv ) ) {
// what if it's not in the lookup table?
error_log( sprintf(
'Error: no lookup table mapping for glyph "%1$s" file named %2$s',
$glyph_name,
$file
) );
$character = $glyph_name;
} else {
// convert hex code to character:
// https://stackoverflow.com/a/6058533
$character = json_decode(sprintf(
'"\u%1$s"',
$uv
));
}
$row[] = $character;
}
} finally {
echo output_row( $row );
}
return ob_get_clean();
}
/**
* Write the list to ./character-list.txt
*
* @param String $list The character list as it is meant to be written
*/
function write_list( $list ) {
file_put_contents(
'./character-list.txt',
$list
);
}
function main() {
$path = './glyphs/';
$files = scandir( $path );
$files = array_diff( $files, array( '.', '..' ) );
montage( $files );
$list = generate_list( $files );
write_list( $list );
echo 'cut after this line:' . "\n";
echo '8<------------------' . "\n";
echo $list;
}
echo main();