File: bdftogd

package info (click to toggle)
libgd2 2.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 10,360 kB
  • sloc: ansic: 48,194; sh: 5,603; cpp: 1,298; makefile: 323; perl: 225; tcl: 45
file content (204 lines) | stat: -rwxr-xr-x 3,997 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
#!/usr/bin/perl -w

#
# Simple convertor from bdf to gd font format.
#
# Author: Jan Pazdziora, adelton@fi.muni.cz, http://www.fi.muni.cz/~adelton/
# at Faculty of Informatics, Masaryk University in Brno, Czech Republic.
#
# Example of use:
# fstobdf -s fontserverhost:7100 -fn 8x16 | ./bdftogd FontLarge gdfontl
#

use strict;

my $VERSION = '0.60';
my $now = localtime;

if (@ARGV != 2)
	{ die "usage: bdftogd fontname filename, eg. bdftogd FontLarge gdfontl\n"; }

my $gdname = shift;
$gdname = 'gd' . $gdname unless $gdname =~ /^gd/i;

my $filename = shift;
$filename = 'gd' . $filename unless $filename =~ /^gd/i;

if (-f "$filename.c") { die "File $filename.c already exists, won't overwrite\n"; }
if (-f "$filename.h") { die "File $filename.h already exists, won't overwrite\n"; }

my ($width, $height);
my (@data, @left, @bottom);
my ($globalleft, $globaltop);

my ($minchar, $maxchar);

my ($copyright, $fontdef);

my $currentchar;
my $gobitmap = 0;


while (<>)
	{
	chomp;
	s/\r$//;
	my ($tag, $value) = split / /, $_, 2;
	die "Font is not fixed width\n"
			if $tag eq 'SPACING' and not $value =~ /[CM]/i;

	$currentchar = $value if $tag eq 'ENCODING';
	$minchar = $currentchar if not defined $minchar
		or $currentchar < $minchar;
	$maxchar = $currentchar if not defined $maxchar
		or $currentchar > $maxchar;

	if ($tag eq 'ENDCHAR')
		{
		$gobitmap = 0;
		my $bottom = $globaltop - $bottom[$currentchar];


		if ($bottom > 0)
			{ $data[$currentchar] = substr $data[$currentchar], 0, length($data[$currentchar]) - $bottom * $width; }
		else
			{ $data[$currentchar] .= '0' x (-$bottom * $width); }
		}

	if ($tag eq 'FONTBOUNDINGBOX')
		{
		my ($tag, $wid, $hei, $left, $top) = split / /;
		if (defined $top)
			{
			$globalleft = $left;
			$globaltop = $top;
			$height = $hei;
			$width = $wid;
			}
		}
	if ($tag eq 'FONT' and not defined $fontdef)
		{ $fontdef = $value; }
	if ($tag eq 'COPYRIGHT' and not defined $copyright)
		{ $copyright = $value; }

	if ($tag eq 'BBX')
		{
		my ($tag, $wid, $hei, $left, $bottom) = split / /;
		if (defined $bottom)
			{
			$left[$currentchar] = $left;
			$bottom[$currentchar] = $bottom;
			}
		}

	if ($gobitmap)
		{
		my $value = pack 'H*', $_;
		my $bits = unpack 'B*', $value;
		$bits = ('0' x $left[$currentchar]) . $bits;
		$bits .= '0' x ($width - length $bits);
		$bits = substr $bits, 0, $width;
		$data[$currentchar] .= $bits;
		}

	if ($tag eq 'BITMAP')
		{
		$gobitmap = 1;
		$data[$currentchar] = '';
		}
	}

my $info = <<"EOF";
/*
	This is a header file for gd font, generated using
	bdftogd version $VERSION by Jan Pazdziora, adelton\@fi.muni.cz
	from bdf font
	$fontdef
	at $now.
EOF

if (defined $copyright)
	{
	$info .= <<"EOF";
	The original bdf was holding following copyright:
	$copyright
 */
EOF
	}
else
	{
	$info .= <<"EOF";
	No copyright info was found in the original bdf.
 */
EOF
	}

open FILEC, "> $filename.c" or die "Error writing $filename.c: $!\n";
open FILEH, "> $filename.h" or die "Error writing $filename.h: $!\n";
print FILEC <<"EOF";

$info

#include "$filename.h"

char ${gdname}Data[] = {
EOF

$minchar = 0 unless defined $minchar;
$maxchar = 255 unless defined $maxchar;
for (my $i = $minchar; $i <= $maxchar; $i++)
	{
	$data[$i] = '' unless defined $data[$i];
	$data[$i] = '0' x ($width * $height - length $data[$i]) . $data[$i];

	print FILEC "/* Char $i */\n";
	for my $line (0 .. $height - 1)
		{ print FILEC join ',', split(//, substr($data[$i], $line * $width, $width)), "\n"; }

	print FILEC "\n";

	next;

	for my $line (0 .. $height - 1)
		{ print substr($data[$i], $line * $width, $width), "\n"; }
	}

my $capdef = "\U_${filename}_H_";

print FILEC <<"EOF";

};

gdFont ${gdname}Rep = {
	@{[ $maxchar - $minchar + 1]},
	$minchar,
	$width,
	$height,
	${gdname}Data
};

gdFontPtr ${gdname} = &${gdname}Rep;

/* This file has not been truncated. */

EOF


close FILEC;

print FILEH <<"EOF";

#ifndef $capdef
#define $capdef 1

$info

#include "gd.h"

extern gdFontPtr $gdname;

#endif

EOF

1;