unifont-6.3.20131215.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 /(....):(.+)/; }
48 @chars = sort keys %glyph;
49 $nchars = $#chars + 1;
50
51 print "STARTFONT 2.1
52 FONT -gnu-${font_name}-Medium-R-Normal-Sans-${vpixels}-${point_size10}-75-75-c-80-iso10646-1
53 SIZE $point_size 75 75
54 FONTBOUNDINGBOX $vpixels $vpixels 0 -2
55 STARTPROPERTIES 24
56 COPYRIGHT \"${copyright}\"
57 FONT_VERSION \"${version}\"
58 FONT_TYPE \"Bitmap\"
59 FOUNDRY \"GNU\"
60 FAMILY_NAME \"${font_name}\"
61 WEIGHT_NAME \"Medium\"
62 SLANT \"R\"
63 SETWIDTH_NAME \"Normal\"
64 ADD_STYLE_NAME \"Sans Serif\"
65 PIXEL_SIZE ${vpixels}
66 POINT_SIZE ${point_size10}
67 RESOLUTION_X 75
68 RESOLUTION_Y 75
69 SPACING \"C\"
70 AVERAGE_WIDTH 80
71 CHARSET_REGISTRY \"ISO10646\"
72 CHARSET_ENCODING \"1\"
73 FONT_ASCENT 14
74 FONT_DESCENT 2
75 UNDERLINE_POSITION -2
76 UNDERLINE_THICKNESS 1
77 CAP_HEIGHT 10
78 X_HEIGHT 8
79 DEFAULT_CHAR 65533
80 ENDPROPERTIES
81 CHARS $nchars\n";
82
83 # TODO: $swidth needs to be modified to accomodate different widths better
84 #       for glyphs with height different than 16 pixels.
85
86 foreach $character (@chars)
87 {
88         $encoding = hex($character); $glyph = $glyph{$character};
89         $width    = length ($glyph) / $vpixels;  # hex digits per row
90         $dwidth   = $width * 4;     # device width, in pixels; 1 digit = 4 px
91         # scalable width, units of 1/1000th full-width glyph
92         $swidth   = (1000 * $width) / ($vpixels / 4);
93         $glyph    =~ s/((.){$width})/\n$1/g;
94         $character = "$character $charname"
95             if $charname = $charname{pack("n",hex($character))};
96
97         print "STARTCHAR U+$character
98 ENCODING $encoding
99 SWIDTH $swidth 0
100 DWIDTH $dwidth 0
101 BBX $dwidth $vpixels 0 -2
102 BITMAP $glyph
103 ENDCHAR\n";
104 }
105
106 print "ENDFONT\n";