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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
#!/usr/bin/perl -w
#
# Simple Tk script to create a button that prints "Hello, world". Click on
# the button to terminate the program.
#
# The first line below imports the Tk objects into the application, the second
# line creates the main window, the third through sixth lines create the button
# and defines the code to execute when the button is pressed, the seventh line
# asks the packer to shrink-wrap the application's main window around the
# button, and the eight line starts the event loop.
use Tk;
use Tk::FileDialog;
use DTM::Catalog;
use DTM::Snippet;
use DTM::Type1Utils;
use DTM::Utils;
use strict;
my $main;
my $lists;
my ($list,$list_scroll,$list_box,$list_label);
my ($flist,$flist_scroll,$flist_box,$flist_label,$flist_sel);
my ($buttons,$quitb, $delb, $addb);
my %snip_names;
my $catalog;
my $path = $ENV{HOME};
my $catalog_path;
sub change_path {
my $ask = shift;
if ($ask) {
my $dialog = $main->FileDialog(-Title => 'Select a path',
-Create => 0,
-SelDir => 1,
-Path => $path);
my $tmppath = $dialog->Show;
$path = ($tmppath eq "" ? $path : $tmppath);
}
chdir $path;
my @files = glob("*.pf?");
$flist_box->delete(0, $flist_box->size-1);
$flist_box->insert(0, sort @files);
}
my $OK;
sub add_font {
# first i get the names from the listbox
my @indices = $flist_box->curselection();
my @labels = ('Name',
'Foundry',
'Typeface',
'Weight',
'Width',
'Slantness',
'Variant',
'PS Name',
'X11 Name');
my $top = $main->Toplevel();
my (@txt, @lab, $ok);
# builds widgets
for my $i (0..8) {
$lab[$i] = $top->Label(-text => "$labels[$i]:");
$txt[$i] = $top->Entry(-width => 80);
$lab[$i]->grid(-row => $i, -column => 0);
$txt[$i]->grid(-column => 1, -row => $i);
}
$ok = $top->Button(-text => 'Install the font',
-command => sub {$OK = 1});
$ok->grid(-row => 9, -column => 1);
foreach my $idx (@indices) {
# builds type1 snippet
my $file = $flist_box->get($idx);
my $snip = build_type1snippet($path, $file);
foreach my $i (0..6) {
$txt[$i]->delete(0,'end');
$txt[$i]->insert(0, $snip->get_attr($labels[$i]));
}
$txt[7]->delete(0,'end');
$txt[8]->delete(0,'end');
$txt[7]->insert(0, $snip->get_attr('Name', 'psspecific'));
$txt[8]->insert(0, $snip->get_attr('Name', 'x11specific'));
$OK = 0;
$top->waitVariable(\$OK);
# updates and install snippet
foreach my $i (0..6) {
$snip->set_attr($labels[$i], $txt[$i]->get());
}
$snip->set_attr('Name', $txt[7]->get(), 'psspecific');
$snip->set_attr('Name', $txt[8]->get(), 'x11specific');
$snip->set_attrs(undef,
FontFile => $file,
FontPath => $catalog_path);
$OK = 0;
my $thetext;
my $msg = $main->Toplevel(-width => 600);
my $out = $msg->Message(-textvariable => \$thetext,
-width => 600);
my $oki = $msg->Button(-text => 'OK',
-command => sub {$OK = 1});
$out->pack(-side => 'top');
$oki->pack(-side => 'top');
# copy the font
safe_system("cp $path/$file $catalog_path/$file");
safe_system("chmod 644 $catalog_path/$file");
if ($catalog->add($snip) == 0) {
$thetext = $catalog->error();
}
else {
$thetext = "Font installed";
}
update_catalog();
$msg->waitVariable(\$OK);
$msg->destroy();
}
$top->destroy();
$catalog->save();
}
sub del_font {
# first i get the names from the listbox
my @indices = $list_box->curselection();
# then I simply get the right snippets and I
# remove the fonts
my $thetext;
my $top = $main->Toplevel(-width => 600);
my $out = $top->Message(-textvariable => \$thetext,
-width => 600);
my $ok = $top->Button(-text => 'OK',
-command => [$top => 'destroy']);
my @removed;
$out->pack(-side => 'top');
$ok->pack(-side => 'top');
foreach my $i (@indices) {
my $name = $list_box->get($i);
my $snip = $catalog->find($snip_names{$name});
$thetext .= "Removing font `$name' ... ";
$out->update;
$catalog->remove($snip);
my $err = $catalog->error();
safe_system("rm -f $path/" . $snip->get_attr('FontFile'))
unless $snip->get_attr('Alias');;
if ($err eq "") {
$thetext .= "done\n";
@removed = (@removed, $i);
}
else {
$thetext .= "error!\n $err\n";
}
}
# updates the listbox, etc...
foreach my $i (@removed) {
$list_box->delete($i);
}
$out->update();
$catalog->save();
}
sub quit {
exit;
}
sub update_catalog {
foreach my $snip ($catalog->filter()) {
$snip_names{$snip->get_attr('Name')} = $snip->get_attr('ID');
}
$list_box->delete(0, 'end');
$list_box->insert(0, sort keys %snip_names);
}
$main = MainWindow->new;
#$lists = $main->Frame(-relief => 'flat');
# font list
$list = $main->Frame(-relief => 'flat');
$list_box = $list->Listbox(-width => 30,
-selectmode => 'extended');
$list_scroll = $list->Scrollbar(-command, ['yview', $list_box]);
$list_label = $list->Label(-text => 'Installed fonts');
$list_box->configure(-yscrollcommand => ['set', $list_scroll]);
#packs
$list_label->pack(-side => 'top');
$list_box->pack(-side => 'left', -fill => 'both');
$list_scroll->pack(-side => 'right', -fill => 'y');
# file list
$flist = $main->Frame(-relief => 'flat');
$flist_box = $flist->Listbox(-width => 30,
-selectmode => 'extended');
$flist_scroll = $flist->Scrollbar(-command, ['yview', $flist_box]);
$flist_label = $flist->Label(-text => 'Available font files');
$flist_box->configure(-yscrollcommand => ['set', $flist_scroll]);
$flist_sel = $flist->Button(-text => 'Select path...',
-command => [\&change_path => 1]);
#packs
$flist_sel->pack(-side => 'bottom', -fill => 'x');
$flist_label->pack(-side => 'top');
$flist_box->pack(-side => 'left', -fill => 'both');
$flist_scroll->pack(-side => 'right', -fill => 'y');
# action buttons
$buttons = $main->Frame(-relief => 'flat');
$addb = $buttons->Button(-command => \&add_font,
-width => 12,
-text => 'Add file(s)...');
$delb = $buttons->Button(-command => \&del_font,
-width => 12,
-text => 'Remove font(s)');
$quitb = $buttons->Button(-command => \&quit,
-width => 12,
-text => 'Quit');
$addb->pack(-side => 'left');
$delb->pack(-side => 'left');
$quitb->pack(-side => 'left');
# places the widgets
#$lists->pack(-side => 'top', -fill => 'both');
$buttons->pack(-side => 'bottom', -anchor => 'e');
$list->pack(-side => 'left', -fill => 'both');
$flist->pack(-side => 'left', -fill => 'both');
# options pretty bad done at now
if ($> == 0) {
$catalog_path = '/usr/share/fonts/type1/outlines';
$catalog = DTM::Catalog::sys_catalog('type1');
}
else {
$catalog_path = "$ENV{HOME}/fonts/type1/outlines";
if ($ARGV[0] and $ARGV[0] eq '-l') {
$catalog = DTM::Catalog::user_catalog('type1',
"$ENV{HOME}/fonts/methods");
shift @ARGV;
}
else {
$catalog = DTM::Catalog::user_catalog('type1');
}
# creates some std dirs
safe_system("install -d $ENV{HOME}/fonts/catalogs")
if !(-d "$ENV{HOME}/fonts/catalogs");
safe_system("install -d $ENV{HOME}/fonts/methods")
if !(-d "$ENV{HOME}/fonts/methods");
safe_system("install -d $ENV{HOME}/fonts/type1/outlines")
if !(-d "$ENV{HOME}/fonts/type1/outlines");
}
$path = $ARGV[0] if $ARGV[0];
change_path();
update_catalog();
MainLoop;
|