tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

genLanguageTagList.pl (2222B)


      1 #!/usr/bin/env perl
      2 
      3 # This Source Code Form is subject to the terms of the Mozilla Public
      4 # License, v. 2.0. If a copy of the MPL was not distributed with this
      5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
      6 
      7 # This tool is used to prepare a list of valid language subtags from the
      8 # IANA registry (http://www.iana.org/assignments/language-subtag-registry).
      9 
     10 # Run as
     11 #
     12 #   perl genLanguageTagList.pl language-subtag-registry > gfxLanguageTagList.cpp
     13 #
     14 # where language-subtag-registry is a copy of the IANA registry file.
     15 
     16 use strict;
     17 
     18 my $timestamp = gmtime();
     19 print <<__END;
     20 /* This Source Code Form is subject to the terms of the Mozilla Public
     21 * License, v. 2.0. If a copy of the MPL was not distributed with this
     22 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     23 
     24 /*
     25 * Derived from the IANA language subtag registry by genLanguageTagList.pl.
     26 *
     27 * Created on $timestamp.
     28 *
     29 * * * * * This file contains MACHINE-GENERATED DATA, do not edit! * * * * *
     30 */
     31 
     32 __END
     33 
     34 my $isLanguage = 0;
     35 
     36 while (<>) {
     37  # strip leading/trailing whitespace, if any
     38  chomp;
     39  s/^\s+//;
     40  s/\s+$//;
     41 
     42  # assume File-Date precedes the actual list;
     43  # record the date, and begin assignment to an array of valid tags
     44  if (m/^File-Date:\s*(.+)$/) {
     45    print "// Based on IANA registry dated $1\n\n";
     46    print "static const uint32_t sLanguageTagList[] = {";
     47    next;
     48  }
     49 
     50  if (m/^%%/) {
     51    $isLanguage = 0;
     52    next;
     53  }
     54 
     55  # we only care about records of type 'language'
     56  if (m/^Type:\s*(.+)$/) {
     57    $isLanguage = ($1 eq 'language');
     58    next;
     59  }
     60 
     61  # append the tag to our string, with ";" as a delimiter
     62  if ($isLanguage && m/^Subtag:\s*([a-z]{2,3})\s*$/) {
     63    my $tagstr = $1;
     64    print "\n  TRUETYPE_TAG(",
     65      join(",", map { $_ eq " " ? " 0 " : "'" . $_ . "'" } split(//, substr($tagstr . "  ", 0, 4))),
     66      "), // ", $tagstr;
     67    next;
     68  }
     69 
     70  if ($isLanguage && m/^Description:\s*(.+)$/) {
     71    print " = $1";
     72    $isLanguage = 0; # only print first Description field
     73    next;
     74  }
     75 }
     76 
     77 # at end of file, terminate our assignment to the array of tags
     78 print <<__END;
     79 
     80  0x0 // end of language code list
     81 };
     82 
     83 /*
     84 * * * * * This file contains MACHINE-GENERATED DATA, do not edit! * * * * *
     85 */
     86 __END