-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcolorify.pl
executable file
·124 lines (93 loc) · 3.44 KB
/
colorify.pl
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
#!/usr/bin/env perl
use strict;
use Data::Dumper;
use Getopt::Long;
my %codeColors =
(
"0xFE06" => "#DB2C38",
"0xFE07" => "#41B645",
"0xFE08" => "#C67C48",
"0xFE09" => "#00A0BE",
"0xFE0A" => "#B21889",
);
foreach my $fontName ("ColoredConsole-Bold")
{
my $content = read_file('UTF-8', $fontName.".ttx");
my @glypIds = $content =~ m~<GlyphID name="([^"\.]+)"/>~g;
# die Dumper(\@glypIds);
my $numPaletteEntries = keys %codeColors;
my $extraGlyphIDs = "";
my $extraMetrics = "";
my $extraMaps = "";
my $extraGlyphs = "";
my $ligatureSets = "";
my $colorGlyphs = "";
my $colors = "";
my $colorIndex = 0;
foreach my $code (sort keys %codeColors)
{
$extraGlyphIDs .= sprintf qq( <GlyphID name="%s"/>\n), $code;
$extraMetrics .= sprintf qq( <mtx name="%s" width="0" lsb="50"/>\n), $code;
$extraMaps .= sprintf qq( <map code="%s" name="%s"/>\n), $code, $code;
$extraGlyphs .= sprintf qq( <TTGlyph name="%s"/><!-- contains no outline data -->\n), $code;
$colors .= sprintf qq( <color index="%d" value="%s"/>\n), $colorIndex++, $codeColors{$code};
}
foreach my $glypId (@glypIds)
{
$ligatureSets .= sprintf qq( <LigatureSet glyph="%s">\n), $glypId;
my $colorIndex = 0;
foreach my $code (sort keys %codeColors)
{
$extraGlyphIDs .= sprintf qq( <GlyphID name="%s.%s"/>\n), $glypId, $code;
$extraMetrics .= sprintf qq( <mtx name="%s.%s" width="600" lsb="50"/>\n), $glypId, $code;
$extraGlyphs .= sprintf qq( <TTGlyph name="%s.%s"/><!-- contains no outline data -->\n), $glypId, $code;
$ligatureSets .= sprintf qq( <Ligature components="%s" glyph="%s.%s"/>\n), $code, $glypId, $code;
$colorGlyphs .= sprintf qq( <ColorGlyph name="%s.%s"><layer colorID="%d" name="%s"/></ColorGlyph>\n), $glypId, $code, $colorIndex++, $glypId;
}
$ligatureSets .= sprintf qq( </LigatureSet>\n);
}
$content =~ s~ <!-- extra GlyphIDs -->\n~$extraGlyphIDs~;
$content =~ s~ <!-- extra mtxs -->\n~$extraMetrics~;
$content =~ s~ <!-- extra maps -->\n~$extraMaps~;
$content =~ s~ <!-- extra TTGlyphs -->\n~$extraGlyphs~;
$content =~ s~ <!-- LigatureSets -->\n~$ligatureSets~;
$content =~ s~ <!-- ColorGlyphs -->\n~$colorGlyphs~;
$content =~ s~ <!-- colors -->\n~$colors~;
$content =~ s~<numPaletteEntries value="0"/>~<numPaletteEntries value="$numPaletteEntries"/>~;
write_file('UTF-8', $fontName.".colored.ttx", $content);
system "ttx", "-o", $fontName.".ttf", $fontName.".colored.ttx";
}
sub read_file
{
my ($encoding, $path) = @_;
open(my($file), '<:encoding('.$encoding.')', $path) || die "error $!: $path\n";
my $content = "";
while(<$file>) {
$content .= $_;
}
close $file;
return $content;
}
sub write_file
{
my ($encoding, $path, $content) = @_;
if (defined $content)
{
my $parentPath = $path;
$parentPath =~ s!/?[^/]*$!!;
if (length $parentPath && !-e $parentPath)
{
system "mkdir", "-p", $parentPath;
}
if (!-e $path || read_file($encoding, $path) ne $content)
{
open(my($file), '>:encoding('.$encoding.')', $path) || die "error $!: $path\n";
print $file $content;
close $file;
}
}
elsif (-e $path)
{
system "rm", "-f", $path;
}
}