File: x11.type1

package info (click to toggle)
dtm 0.4.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 508 kB
  • ctags: 67
  • sloc: perl: 1,348; sh: 61; makefile: 45
file content (282 lines) | stat: -rw-r--r-- 7,280 bytes parent folder | download | duplicates (2)
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
279
280
281
282
# x11.type1 -- X11 type1 hooks for the DTM system
#
# Copyright (C) 1997-1998 Federico Di Gregorio.
#
# This program is part of the Definitive Type Manager package.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.

#### configuration options ####
#
my $verbose = 0;
#
# the outlines path for the user and the system fonts can
# be specified with the following variables. these are
# reasonable defaults used by the DTM system, change them at
# your own risk...
my $sys_outlines = '/usr/share/fonts/type1/outlines';
my $user_outlines = "$ENV{HOME}/fonts/type1/outlines";
#
# there are no other configuration options because X11 wants the
# fonts.* files in the directory containing the fonts themselves.
# just remember to add your type1 directory to the fontpath with
# something like:
#
#     xset +fp $HOME/fonts/type1/outlines
#
# read the INSTALL file for more information about setting up
# your X server.
#
#### end of configuration ####


use strict;

# gets outlines path right
my $outlines = ($> == 0 ? $sys_outlines : $user_outlines);


# install a new font by creating the right fonts.scale
# entry and then by running mkfontdir
my $x11_install_font = sub {
    my $catalog = shift;
    my $snip = shift;
    
    my $id = $snip->get_attr('ID');
    my $x11name = $snip->get_attr('Name', 'x11specific') or do {
	dtm_warning("font with ID $id misses X11 specific " .
		    "information; skipping");
	return;
    };

    # it's an alias: writes the entry to the fonts.alias file
    # in the aliased font directory
    if (my $alias_id = $snip->get_attr('Alias')) {
	my $alias = $catalog->find($alias_id);
	my $file = $alias->get_attr('FontPath');

	# warns if the path is not the right one, but goes on
	if ($file ne $outlines) {
	#   dtm_warning("the FontPath attribute doesn't " .
	#    "match with the default " .
	#	    "outlines path;");
	#    dtm_warning("the X11 system may have some problems finding " .
	#	    "the fonts"); 
	#    dtm_warning("  $file <-> $outlines");
	}

	my $aliasx11name = $alias->get_attr('Name', 'x11specific') or do {
	    dtm_warning("missing X11 specific information; skipping");
	    return;
	};

	$file .= '/fonts.alias';
	safe_system("cp $file $file.old") 
	    if -r $file;
	open(FONTS, ">>$file") or do {
	    dtm_warning("can't open `$file' for appending");
	    return;
	};

	print "x11: creating alias: $x11name -> $aliasx11name\n"
	    if $verbose;

	print FONTS "$x11name $aliasx11name \n";
	close FONTS;

	safe_system("mkfontdir " . $alias->get_attr('FontPath'));
    }

    # this is a real font, writes the entries to the fonts.scale file
    else
    {
	my $file = $snip->get_attr('FontPath');

	# warns if the path is not the right one, but goes on
	if ($outlines ne $file) {
	#   dtm_warning("the FontPath attribute doesn't " .
	#    "match with the default " .
	#	    "outlines path;");
	#    dtm_warning("the X11 system may have some problems finding " .
	#	    "the fonts"); 
	#    dtm_warning("  $file <-> $outlines");
	}

	$file .= '/fonts.scale';
	if (-r $file) {
	    safe_system("cp $file $file.old"); 
	}
	else {
	    dtm_warning("the `$file' file doesn't exist; " .
			"can't add the font with ID $id");
	    return;
	}

	open(OLD, "$file.old") or do {
	    dtm_warning("can't open `$file.old' for reading");
	    return;
	};
	open(SCALE, ">$file") or do { 
	    dtm_warning("can't open `$file' for writing");
	    return;
	};

	# reads in number of fonts
	my $num = <OLD> + 1;

	print "x11: installing `" . $snip->get_attr('FontFile') .
	    "' as `$x11name'\n"
		if $verbose;
 
	print SCALE "$num\n";
	print SCALE $snip->get_attr('FontFile') . " $x11name\n";

	while (<OLD>) {
	    print SCALE $_;
	}
	close OLD; close SCALE;

	safe_system("mkfontdir " . $snip->get_attr('FontPath'));
    }
};

# removes the given font
my $x11_remove_font = sub {
    my $catalog = shift;
    my $snip = shift;

    my $id = $snip->get_attr('ID');
    my $x11name = $snip->get_attr('Name', 'x11specific') or do {
	dtm_warning("font with ID $id misses X11 specific " .
		    "information; skipping");
	return;
    };

    if (my $alias_id = $snip->get_attr('Alias')) {
	my $alias = $catalog->find($alias_id);
	my $file = $alias->get_attr('FontPath');

	$file .= '/fonts.alias';
	if (-r $file) {
	    safe_system("cp $file $file.old"); 
	}
	else {
	    dtm_warning("the `$file' file doesn't exist; " .
			"can't add the font with ID $id");
	    return;
	}

	open(OLD, "$file.old") or do {
	    dtm_warning("can't open `$file.old' for reading");
	    return;
	};
	open(ALIAS, ">$file") or do { 
	    dtm_warning("can't open `$file' for writing");
	    return;
	};

	print "x11: purging entry `$x11name'\n"
	    if $verbose;

	while (<OLD>) {
	    next if /^$x11name/;  # skips removed one
	    print ALIAS $_;
	}

	close OLD; close ALIAS;

	safe_system("mkfontdir " . $alias->get_attr('FontPath'));
    }

    # it isn't an alias, I proceed exactly the same way but I
    # need to decrease the numer of fonts by one	
    else {
	my $file = $snip->get_attr('FontPath');
	my $fontfile = $snip->get_attr('FontFile');

	$file .= '/fonts.scale';
	if (-r $file) {
	    safe_system("cp $file $file.old"); 
	}
	else {
	    dtm_warning("the `$file' file doesn't exist; " .
			"can't add the font with ID $id");
	    return;
	}

	open(OLD, "$file.old") or do {
	    dtm_warning("can't open `$file.old' for reading");
	    return;
	};
	open(SCALE, ">$file") or do { 
	    dtm_warning("can't open `$file' for writing");
	    return;
	};
	
	print "x11: purging entry `$x11name'\n"
	    if $verbose;

	# read number of fonts and writes new one
	my $num = <OLD> - 1;
	print SCALE "$num\n";
	
	while (<OLD>) {
	    next if /^$fontfile\s$x11name/;  # skips removed one
	    print SCALE $_;
	}

	close OLD; close SCALE;

	safe_system("mkfontdir " . $snip->get_attr('FontPath'));
    }
};

# we need an empty catalog with 0 fonts, so I create it
# when the directory is first accessed
my $x11_add_path = sub {
    my $catalog = shift;
    my $snip = shift;
    my $path = shift;

    open(FONTS, ">$path/fonts.scale") or do { 
	dtm_warning("can't open `$path/fonts.scale' for writing");
	return;
    };

    print "x11: installing empty fonts.scale in `$path'\n"
	if $verbose;

    print FONTS "0\n";
    close FONTS;
};

# simply removes old fonts in the path
my $x11_delete_path = sub {
    my $catalog = shift;
    my $snip = shift;    
    my $path = shift;

    print "x11: removing fonts.* files from `$path'\n"
	if $verbose;

    safe_system("rm -f $path/fonts.scale $path/fonts.scale.old");
    safe_system("rm -f $path/fonts.alias $path/fonts.alias.old");
    safe_system("rm -f $path/fonts.dir $path/fonts.dir.old");
};

# install the hooks
add_hooks(0,
	  InstallingFont => $x11_install_font,
	  RemovingFont => $x11_remove_font,
	  AddingPath => $x11_add_path,
	  DeletingPath => $x11_delete_path);

1;