Home | History | Annotate | Download | only in css
      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 
     23 use strict;
     24 use Getopt::Long;
     25 
     26 my $header = $ARGV[0];
     27 shift;
     28 
     29 my $out = $ARGV[0];
     30 shift;
     31 
     32 open HEADER, ">", $header or die;
     33 open OUT, ">", $out or die;
     34 
     35 print HEADER "namespace WebCore {\n";
     36 print OUT "namespace WebCore {\n";
     37 
     38 for my $in (@ARGV) {
     39     $in =~ /(\w+)\.css$/ or die;
     40     my $name = $1;
     41 
     42     # Slurp in the CSS file.
     43     open IN, "<", $in or die;
     44     my $text; { local $/; $text = <IN>; }
     45     close IN;
     46 
     47     # Remove comments in a simple-minded way that will work fine for our files.
     48     # Could do this a fancier way if we were worried about arbitrary CSS source.
     49     $text =~ s|/\*.*?\*/||gs;
     50 
     51     # Crunch whitespace just to make it a little smaller.
     52     # Could do work to avoid doing this inside quote marks but our files don't have runs of spaces in quotes.
     53     # Could crunch further based on places where whitespace is optional.
     54     $text =~ s|\s+| |gs;
     55     $text =~ s|^ ||;
     56     $text =~ s| $||;
     57 
     58     # Write out a C array of the characters.
     59     my $length = length $text;
     60     print HEADER "extern const char ${name}UserAgentStyleSheet[${length}];\n";
     61     print OUT "extern const char ${name}UserAgentStyleSheet[${length}] = {\n";
     62     my $i = 0;
     63     while ($i < $length) {
     64         print OUT "    ";
     65         my $j = 0;
     66         while ($j < 16 && $i < $length) {
     67             print OUT ", " unless $j == 0;
     68             print OUT ord substr $text, $i, 1;
     69             ++$i;
     70             ++$j;
     71         }
     72         print OUT "," unless $i == $length;
     73         print OUT "\n";
     74     }
     75     print OUT "};\n";
     76 
     77 }
     78 
     79 print HEADER "}\n";
     80 print OUT "}\n";
     81