blob: 05c8fd17c7594343326f30c6ab74da754f9154f1 [file] [log] [blame]
darin4ecb97e2006-01-18 08:56:19 +00001#!/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
ddkilzerc8eccec2007-09-26 02:29:57 +000017# the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18# Boston, MA 02110-1301, USA.
darin4ecb97e2006-01-18 08:56:19 +000019#
20
21# Usage: make-css-file-arrays.pl <header> <output> <input> ...
22
23use strict;
hausmann57237322007-07-27 08:28:48 +000024use Getopt::Long;
25
26my $preprocessor;
27
28GetOptions('preprocessor=s' => \$preprocessor);
29
30if (!$preprocessor) {
31 $preprocessor = "/usr/bin/gcc -E -P -x c++";
32}
darin4ecb97e2006-01-18 08:56:19 +000033
34my $header = $ARGV[0];
35shift;
36
37my $out = $ARGV[0];
38shift;
39
40open HEADER, ">", $header or die;
41open OUT, ">", $out or die;
42
43print HEADER "namespace WebCore {\n";
44print OUT "namespace WebCore {\n";
45
46for my $in (@ARGV) {
47 $in =~ /(\w+)\.css$/ or die;
48 my $name = $1;
49
50 # Slurp in the CSS file.
hausmann57237322007-07-27 08:28:48 +000051 open IN, $preprocessor . " " . $in . "|" or die;
darin4ecb97e2006-01-18 08:56:19 +000052 my $text; { local $/; $text = <IN>; }
53 close IN;
54
55 # Remove comments in a simple-minded way that will work fine for our files.
56 # Could do this a fancier way if we were worried about arbitrary CSS source.
57 $text =~ s|/\*.*?\*/||gs;
58
59 # Crunch whitespace just to make it a little smaller.
60 # Could do work to avoid doing this inside quote marks but our files don't have runs of spaces in quotes.
61 # Could crunch further based on places where whitespace is optional.
62 $text =~ s|\s+| |gs;
63 $text =~ s|^ ||;
64 $text =~ s| $||;
65
66 # Write out a C array of the characters.
67 my $length = length $text;
darinf5427d22006-04-03 01:11:28 +000068 print HEADER "extern const char ${name}UserAgentStyleSheet[${length}];\n";
69 print OUT "extern const char ${name}UserAgentStyleSheet[${length}] = {\n";
darin4ecb97e2006-01-18 08:56:19 +000070 my $i = 0;
71 while ($i < $length) {
darin67322632006-01-23 02:35:47 +000072 print OUT " ";
darin4ecb97e2006-01-18 08:56:19 +000073 my $j = 0;
74 while ($j < 16 && $i < $length) {
75 print OUT ", " unless $j == 0;
76 print OUT ord substr $text, $i, 1;
77 ++$i;
78 ++$j;
79 }
80 print OUT "," unless $i == $length;
81 print OUT "\n";
82 }
83 print OUT "};\n";
84
85}
86
87print HEADER "}\n";
88print OUT "}\n";