]> git.sesse.net Git - vlc/blob - doc/subtitles/vlc-font.pl
Fix detection of dvb headerfiles and fix GTK2_MAIN selection for PDA interface.
[vlc] / doc / subtitles / vlc-font.pl
1 #! /usr/bin/perl
2
3 $file_input="font.pnm";
4 $file_output="eutopiabold36.rle";
5 $border=-1;
6 $spaceborder=4;
7
8 $|=1;
9
10 open(INPUT,$file_input) || die "Couldn't open font: $!\n";
11
12 $tag=<INPUT>; chop($tag);
13 if($tag ne "P6")
14   { die "Couldn't process image: not a pnm file ($tag)"; }
15
16 $comment=<INPUT>;
17
18 $dimensions=<INPUT>; chop($dimensions);
19
20 if($dimensions =~ /(\d+) (\d+)/)
21   { $width=$1; $height=$2; }
22 else
23   { die "Couldn't process image: couldn't get dimensions"; }
24
25 $bits=<INPUT>;
26
27 print "width $width height $height\n";
28
29 for($j=0; $j<$height; $j++)
30   { for($i=0; $i<$width; $i++)
31       {
32         $red[$i][$j]=ord(getc(INPUT));
33         $green[$i][$j]=ord(getc(INPUT));
34         $blue[$i][$j]=ord(getc(INPUT));
35       }
36     print ".";
37   }
38
39 print "\n";
40
41 close(INPUT);
42
43 open(OUTPUT,">".$file_output) || die "Couldn't open output: $!\n";
44
45 # Put header
46 print OUTPUT pack("C2",0x36,0x05);
47
48 # Put font height
49 print OUTPUT pack("C",$height);
50
51 $xstart=0;
52
53 # Search for space
54
55 $xstart=2;
56 $x=$xstart; $blank=0;
57 while($x<$width && !$blank)
58   { $blank=1;
59     for($y=0; $y<$height; $y++)
60       { if($blue[$x][$y]!=255)
61           { $blank=0; }
62       }
63     if(!$blank)
64       { $x++; }
65   }
66
67 $xstart=$x;
68
69 $x=$xstart; $blank=1;
70 while($x<$width && $blank)
71   { $blank=1;
72     for($y=0; $y<$height; $y++)
73       { if($blue[$x][$y]!=255)
74           { $blank=0; }
75       }
76     if($blank)
77       { $x++; }
78   }
79 $xend=$x;
80
81 $spacewidth=$xend-$xstart+$spaceborder;
82 $spacewidth=$spacewidth/2;
83 if($spacewidth==0)
84   { $spacewidth=1; }
85
86 print "space start=$xstart end=$xend -> width=$spacewidth\n\n";
87
88 # Put space character code
89 print OUTPUT pack("C",32);
90
91 # Put space width
92 print OUTPUT pack("C",$spacewidth);
93
94 # Put space RLE data
95 for($y=0;$y<$height;$y++)
96   { print OUTPUT pack("C",1);
97     print OUTPUT pack("C",0);
98     print OUTPUT pack("C",$spacewidth);
99   }
100
101 $char=33;
102
103 while($xstart<$width)
104   {
105     print "looking for character $char \"".chr($char)."\"\n";
106
107     $x=$xstart; $blank=1;
108     while($x<$width && $blank)
109       { $blank=1;
110         for($y=0; $y<$height; $y++)
111           { if($blue[$x][$y]!=255)
112               { $blank=0; }
113           }
114         if($blank)
115           { $x++; }
116       }
117     $xstart=$x;
118
119     $x=$xstart; $blank=0;
120     while($x<$width && !$blank)
121       { $blank=1;
122         for($y=0; $y<$height; $y++)
123           { if($blue[$x][$y]!=255)
124               { $blank=0; }
125           }
126         if(!$blank)
127           { $x++; }
128       }
129     $xend=$x;
130     print "start=$xstart end=$xend\n";
131
132     $dstart=$xstart-$border;
133     if($dstart < 0)
134       { $dstart = 0; }
135     $dend=$xend+$border;
136     if($dend > $width)
137       { $dend = $width; }
138
139     # Put character
140     print OUTPUT pack("C",$char);
141
142     # Put character width
143     print OUTPUT pack("C",$dend-$dstart);
144     
145     for($y=0; $y<$height; $y++)
146       { $linecode=""; $bytecode=""; $lastcolour=-1; $count=0;
147         for($x=$dstart; $x<$dend; $x++)
148           {
149             # Transparent background
150             $c=":"; $colour=0;
151
152             # Anti-aliased foreground 
153             if($blue[$x][$y]<255 && $red[$x][$y]>0)
154               { $c="+"; $colour=1; }
155
156             # Solid foreground
157             if($blue[$x][$y]==255 && $red[$x][$y]==255 )
158               { $c="#"; $colour=2; }
159
160             # Anti-aliased shadow (same as shadow)
161             if($blue[$x][$y]<255 && $red[$x][$y]==0)
162               { $c="."; $colour=3; }
163
164             # Solid shadow
165             if($blue[$x][$y]==0 && $red[$x][$y]==0)
166               { $c=" "; $colour=3; }
167
168             print $c;
169
170             if($colour != $lastcolour)
171               {
172                 if($lastcolour!=-1)
173                   { $linecode.=" $lastcolour,$count";
174                     $bytecode.=pack("C2",$lastcolour,$count);
175                   }
176                 $lastcolour=$colour; $count=1;
177               }
178             else
179               { $count++; }
180           }
181         if($lastcolour!=-1)
182           { $linecode.=" $lastcolour,$count";
183             $bytecode.=pack("C2",$lastcolour,$count);
184           }
185          print " [$linecode]\n";
186
187         # Put length of RLE line
188         print OUTPUT pack("C*",length($bytecode)/2);
189
190         # Put RLE line
191         print OUTPUT $bytecode;
192
193       }
194
195     print "\n";
196
197     $xstart=$xend+1;
198     $char++;
199   }
200
201 print OUTPUT pack("C",255);
202
203 print "Done!\n";
204
205 close(OUTPUT);