1#!/usr/bin/perl -w
2#
3#  Copyright (C) 2006 Apple Computer, Inc.
4#
5#  This library is free software; you can redistribute it and/or
6#  modify it under the terms of the GNU Library General Public
7#  License as published by the Free Software Foundation; either
8#  version 2 of the License, or (at your option) any later version.
9#
10#  This library is distributed in the hope that it will be useful,
11#  but WITHOUT ANY WARRANTY; without even the implied warranty of
12#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13#  Library General Public License for more details.
14#
15#  You should have received a copy of the GNU Library General Public License
16#  along with this library; see the file COPYING.LIB.  If not, write to
17#  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18#  Boston, MA 02110-1301, USA.
19#
20
21# Usage: make-css-file-arrays.pl <header> <output> <input> ...
22
23use strict;
24use Getopt::Long;
25
26my $defines;
27my $preprocessor;
28GetOptions('defines=s' => \$defines,
29           'preprocessor=s' => \$preprocessor);
30
31my $header = $ARGV[0];
32shift;
33
34my $out = $ARGV[0];
35shift;
36
37open HEADER, ">", $header or die;
38open OUT, ">", $out or die;
39
40print HEADER "namespace WebCore {\n";
41print OUT "namespace WebCore {\n";
42
43for my $in (@ARGV) {
44    $in =~ /(\w+)\.css$/ or $in =~ /(\w+)\.js$/ or die;
45    my $name = $1;
46
47    # Slurp in the CSS file.
48    my $text;
49    # We should not set --defines option and run "moc" preprocessor on Qt.
50    # See http://webkit.org/b/37296.
51    if (!$defines) {
52        open IN, "<", $in or die;
53        { local $/; $text = <IN>; }
54        close IN;
55        # Remove preprocessor directives.
56        $text =~ s|^#.*?$||mg;
57    } else {
58        require preprocessor;
59        $text = join('', applyPreprocessor($in, $defines, $preprocessor));
60    }
61
62    # Remove comments in a simple-minded way that will work fine for our files.
63    # Could do this a fancier way if we were worried about arbitrary CSS source.
64    $text =~ s|/\*.*?\*/||gs;
65
66    # Crunch whitespace just to make it a little smaller.
67    # Could do work to avoid doing this inside quote marks but our files don't have runs of spaces in quotes.
68    # Could crunch further based on places where whitespace is optional.
69    $text =~ s|\s+| |gs;
70    $text =~ s|^ ||;
71    $text =~ s| $||;
72
73    # Write out a C array of the characters.
74    my $length = length $text;
75    if ($in =~ /(\w+)\.css$/) {
76        print HEADER "extern const char ${name}UserAgentStyleSheet[${length}];\n";
77        print OUT "extern const char ${name}UserAgentStyleSheet[${length}] = {\n";
78    } else {
79        print HEADER "extern const char ${name}JavaScript[${length}];\n";
80        print OUT "extern const char ${name}JavaScript[${length}] = {\n";
81    }
82    my $i = 0;
83    while ($i < $length) {
84        print OUT "    ";
85        my $j = 0;
86        while ($j < 16 && $i < $length) {
87            print OUT ", " unless $j == 0;
88            print OUT ord substr $text, $i, 1;
89            ++$i;
90            ++$j;
91        }
92        print OUT "," unless $i == $length;
93        print OUT "\n";
94    }
95    print OUT "};\n";
96
97}
98
99print HEADER "}\n";
100print OUT "}\n";
101