unifont-8.0.01.tar.gz
[unifont.git] / src / hex2bdf
1 #!/usr/bin/perl
2 #
3 # Copyright (C) 1998, 2013 Roman Czyborra, Paul Hardy
4 #
5 # LICENSE:
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU General Public License as published by
9 #    the Free Software Foundation, either version 2 of the License, or
10 #    (at your option) any later version.
11 #  
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
15 #    GNU General Public License for more details.
16 #  
17 #    You should have received a copy of the GNU General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 #    First, parse command-line options.  Two are available:
21 #
22 #       - Font name (-f name or --font name)
23 #       - Lines per glyph (-lnn or --lines nn, where nn is a decimal number)
24 #
25 use Getopt::Long;
26
27 $result = GetOptions (
28              "copyright|c=s" => \$copyright, # Copyright string
29              "font|f=s"      => \$font_name, # XLFD FAMILY_NAME
30              "rows|r=i"      => \$vpixels,   # XLFD PIXEL_SIZE; vertical pixels
31              "version|v=s"   => \$version    # Version of this font
32              );
33
34 if (not $font_name) {
35    $font_name = "Unifont";
36 }
37 if (not $vpixels) {
38    $vpixels  = 16;
39 }
40 if (not $version) {
41    $version = "1.0";
42 }
43
44 $point_size   = $vpixels;
45 $point_size10 = 10 * $point_size;
46
47 while (<>) { chomp; $glyph{$1} = $2 if /^([0-9a-fA-F]+):([0-9a-fA-F]+)/; }
48 @chars = sort keys %glyph;
49 $nchars = $#chars + 1;
50
51
52 print "STARTFONT 2.1
53 FONT -gnu-${font_name}-Medium-R-Normal-Sans-${vpixels}-${point_size10}-75-75-c-80-iso10646-1
54 SIZE $point_size 75 75
55 FONTBOUNDINGBOX $vpixels $vpixels 0 -2
56 STARTPROPERTIES 24
57 COPYRIGHT \"${copyright}\"
58 FONT_VERSION \"${version}\"
59 FONT_TYPE \"Bitmap\"
60 FOUNDRY \"GNU\"
61 FAMILY_NAME \"${font_name}\"
62 WEIGHT_NAME \"Medium\"
63 SLANT \"R\"
64 SETWIDTH_NAME \"Normal\"
65 ADD_STYLE_NAME \"Sans Serif\"
66 PIXEL_SIZE ${vpixels}
67 POINT_SIZE ${point_size10}
68 RESOLUTION_X 75
69 RESOLUTION_Y 75
70 SPACING \"C\"
71 AVERAGE_WIDTH 80
72 CHARSET_REGISTRY \"ISO10646\"
73 CHARSET_ENCODING \"1\"
74 UNDERLINE_POSITION -2
75 UNDERLINE_THICKNESS 1
76 CAP_HEIGHT 10
77 X_HEIGHT 8
78 FONT_ASCENT 14
79 FONT_DESCENT 2
80 DEFAULT_CHAR 65533
81 ENDPROPERTIES
82 CHARS $nchars\n" or die ("Cannot print to stdout.\n");
83
84 # TODO: $swidth needs to be modified to accomodate different widths better
85 #       for glyphs with height different than 16 pixels.
86
87 foreach $character (@chars)
88 {
89         $encoding = hex($character); $glyph = $glyph{$character};
90         $width    = length ($glyph) / $vpixels;  # hex digits per row
91         $dwidth   = $width * 4;     # device width, in pixels; 1 digit = 4 px
92         # scalable width, units of 1/1000th full-width glyph
93         $swidth   = (1000 * $width) / ($vpixels / 4);
94         $glyph    =~ s/((.){$width})/\n$1/g;
95         $character = "$character $charname"
96             if $charname = $charname{pack("n",hex($character))};
97
98         print "STARTCHAR U+$character
99 ENCODING $encoding
100 SWIDTH $swidth 0
101 DWIDTH $dwidth 0
102 BBX $dwidth $vpixels 0 -2
103 BITMAP $glyph
104 ENDCHAR\n" or die ("Cannot print to stdout.\n");
105 }
106
107 print "ENDFONT\n" or die ("Cannot print to stdout.\n");