summaryrefslogtreecommitdiffstats
path: root/TOOLS/subfont-gimp/mplayer_subfont
blob: e5d9f0256911ad236bd9ff6c81a790cbfe81a669 (plain)
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
#!/usr/bin/perl
use 5.6.0;
use warnings;
use strict;
use Gtk; # just so that compilation fails without it
use Gimp qw(:auto);
use Gimp::Feature qw(gimp-1.2);
use Gimp::Fu;

my $head = <<EOF;
[files]
alpha arpi_osd_a.raw
bitmap arpi_osd_b.raw

[characters]
0x01 0 36
0x02 35 71
0x03 70 106
0x04 116 152
0x05 164 200
0x06 209 245
0x07 256 292
0x08 305 342
0x09 354 400
0x0A 407 442
0x0B 457 494

[files]
alpha arpi_progress_a.raw
bitmap arpi_progress_b.raw

[characters]
0x10 4 21
0x11 30 41
0x12 50 66
0x13 74 85

EOF

sub geninfo {
  my ($font, $height) = @_;

  my $size = (split("-", $font))[7];
  $font = (split("-", $font))[2];
  
  return(sprintf(<<EOF, $font, $size, int($size/2), -3-int($size/10), $size));
[info]
name "%s \@%d; created in gimp; plugin by lanzz\@lanzz.org"
descversion 1
spacewidth %d
charspace %d
height %d

EOF
}

sub basename($) {
  my ($f) = @_;
  $f =~ m#([^/]*)$#;
  return($1);
}

sub render_subfont {
  my ($font, $bfile, $afile, $dfile, $keep) = @_;

  my ($raw) = gimp_procedural_db_query("^file_raw_save\$", "","","","","","");
  if ($raw ne "file_raw_save") {
    gimp_message("HSI Raw plugin not installed");
    return(undef);
  }

  unless (open(D, "> $dfile")) {
    gimp_message("Cannot write to $dfile");
    return(undef);
  }

  my @size = xlfd_size($font);
  $size[0] *= 2;

  my (undef, $h) = gimp_text_get_extents_fontname(join("", map(chr($_), 33 .. 255)), @size, $font);
  $h += 10;
  my $w = 0;
  gimp_palette_set_foreground([255, 255, 255]);
  gimp_palette_set_background([0, 0, 0]);

  my $img = gimp_image_new(1, $h, GRAY);
  gimp_image_undo_disable($img);
  gimp_image_set_filename($img, $bfile);
  my $draw = gimp_layer_new($img, 1, $h, GRAY_IMAGE, "subfont", 100, NORMAL_MODE);
  gimp_image_add_layer($img, $draw, 0);
  gimp_edit_fill($draw, BG_IMAGE_FILL);

  my $x = 0;
  print D (geninfo($font));
  print D ($head);
  printf D (<<EOF, basename($afile), basename($bfile));
[files]
alpha %s
bitmap %s

EOF
  
  print D ("[characters]\n");
  gimp_progress_init("Rendering font...");
  for (my $c = 33; $c <= 255; $c++) {
    my ($cw) = gimp_text_get_extents_fontname(chr($c), @size, $font);
    printf D ("0x%02X %d %d\n", $c, int($x / 2), int(($x + $cw + 5) / 2));
    $cw = (int($cw / 8) + 2) * 8;
    $w += $cw;
    gimp_image_resize($img, $w, $h, 0, 0);
    gimp_layer_resize($draw, $w, $h, 0, 0);
    gimp_floating_sel_anchor(gimp_text_fontname($img, $draw, $x + 5, 5, chr($c), -1, 1, @size, $font));
    $x += $cw;
    gimp_progress_update(($c - 33) / 222);
  }
  close(D);

  gimp_image_scale($img, int($w / 2), int($h / 2));
  gimp_image_undo_enable($img);
  file_raw_save($img, $draw, $bfile, $bfile);

  my $aimg = gimp_channel_ops_duplicate($img);
  gimp_image_undo_disable($aimg);
  gimp_image_set_filename($aimg, $afile);
  $draw = gimp_image_flatten($aimg);
  gimp_by_color_select($draw, [0, 0, 0], 15, REPLACE, 1, 0, 0, 0);
  gimp_selection_invert($aimg);
  gimp_selection_grow($aimg, 1);
  gimp_edit_fill($draw, FG_IMAGE_FILL);
  gimp_selection_clear($aimg);
  plug_in_gauss_rle2($img, $draw, 3, 3);
  gimp_image_undo_enable($aimg);
  file_raw_save($aimg, $draw, $afile, $afile);

  gimp_message(<<EOF);
Render done.

Bitmap: $bfile
Alpha: $afile
Desc: $dfile
EOF
  if ($keep) {
    gimp_image_clean_all($img);
    gimp_image_clean_all($aimg);
    return($img, $aimg);
  } else {
    return(undef);
  }
}

sub render_subfont_alpha {
  my ($img) = @_;
  
  my $aimg = gimp_channel_ops_duplicate($img);
  gimp_image_undo_disable($aimg);
  gimp_image_set_filename($aimg, "alpha.raw");
  my $draw = gimp_image_flatten($aimg);
  gimp_by_color_select($draw, [0, 0, 0], 15, REPLACE, 1, 0, 0, 0);
  gimp_selection_invert($aimg);
  gimp_selection_grow($aimg, 1);
  gimp_edit_fill($draw, FG_IMAGE_FILL);
  gimp_selection_clear($aimg);
  plug_in_gauss_rle2($img, $draw, 3, 3);
  gimp_image_undo_enable($aimg);

  return($aimg);
}

register(
  "render_subfont",
  "Render a grayscale MPlayer subtitle font",
  "No help (yet)",
  "lanzz\@lanzz.org",
  "Copyright 2001, lanzz\@lanzz.org",
  "2001-07-31",
  "<Toolbox>/Xtns/MPlayer/Render Subfont",
  undef,
  [
    [PF_FONT, "font", "", "-*-arial-medium-r-normal-*-16-*-*-*-*-*-*-*", undef],
    [PF_FILE, "bitmap", "", "bitmap.raw", undef],
    [PF_FILE, "alpha", "", "alpha.raw", undef],
    [PF_FILE, "desc", "", "font.desc", undef],
    [PF_TOGGLE, "toggle", "Keep images opened", 0, undef]
  ],
  [ ],
  [ ],
  \&render_subfont
);

register(
  "render_subfont_alpha",
  "Render alpha shadow for MPlayer subtitle font",
  "No help (yet)",
  "lanzz\@lanzz.org",
  "Copyright 2001, lanzz\@lanzz.org",
  "2001-07-31",
  "<Image>/Filters/MPlayer/Render Shadow",
  "GRAY",
  [ ],
  [ PF_IMAGE ],
  [ ],
  \&render_subfont_alpha
);

exit(main());