<commit_before>/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */

/**
 * Maximum file size of the font.
 */
var kMaxFontFileSize = 40000;

/**
 * Maximum number of glyphs per font.
*/
var kMaxGlyphsCount = 65526;

/**
 * Maximum time to wait for a font to be loaded by @font-face
 */
var kMaxWaitForFontFace = 1000;

 /*
  * Useful for debugging when you want to certains operations depending on how
  * many fonts are loaded.
  */
var fontCount = 0;

/**
 * Hold a map of decoded fonts and of the standard fourteen Type1 fonts and
 * their acronyms.
 * TODO Add the standard fourteen Type1 fonts list by default
 *      http://cgit.freedesktop.org/poppler/poppler/tree/poppler/GfxFont.cc#n65
 */
var Fonts = {
  _active: null,
  get active() {
    return this._active;
  },

  set active(aFontName) {
    this._active = this[aFontName];
  },

  unicodeFromCode: function fonts_unicodeFromCode(aCode) {
    var active = this._active;
    if (!active || !active.encoding)
      return aCode;

    var difference = active.encoding[aCode];
    var unicode = GlyphsUnicode[difference];
    return unicode ? "0x" + unicode : aCode;
  }
};

/**
 * 'Font' is the class the outside world should use, it encapsulate all the font
 * decoding logics whatever type it is (assuming the font type is supported).
 *
 * For example to read a Type1 font and to attach it to the document:
 *   var type1Font = new Font("MyFontName", binaryData, aFontEncoding, "Type1");
 *   type1Font.bind();
 *
 * As an improvment the last parameter can be replaced by an automatic guess
 * of the font type based on the first byte of the file.
 *
 * XXX There is now too many parameters, this should be turned into an
 * object containing all the required informations about the font
 */
var Font = function(aName, aFile, aEncoding, aCharset, aBBox, aType) {
  this.name = aName;

  // If the font has already been decoded simply return
  if (Fonts[aName]) {
    this.font = Fonts[aName].data;
    return;
  }
  fontCount++;

  var start = Date.now();
  switch (aType) {
    case "Type1":
      var cff = new CFF(aName, aBBox, aFile);
      this.mimetype = "font/otf";

      // Wrap the CFF data inside an OTF font file
      this.font = this.cover(cff);
      break;

    case "TrueType":
      this.mimetype = "font/ttf";
      var ttf = new TrueType(aFile);
      this.font = ttf.data;
      break;

    default:
      warn("Font " + aType + " is not supported");
      break;
  }
  var end = Date.now();

  Fonts[aName] = {
    data: this.font,
    encoding: aEncoding,
    charset: aCharset ? aCharset.slice() : null,
    loading: true
  }

  // Attach the font to the document
  this.bind();
};

Font.prototype = {
  name: null,
  font: null,
  mimetype: null,

  bind: function font_bind() {
    var data = this.font;

    // Compute the binary data to base 64
    var str = [];
    var count = data.length;
    for (var i = 0; i < count; i++)
      str.push(data.getChar ? data.getChar()
                            : String.fromCharCode(data[i]));

    var dataBase64 = window.btoa(str.join(""));
    var fontName = this.name;

    /** Hack begin */

    // Actually there is not event when a font has finished downloading so
    // the following tons of code are a dirty hack to 'guess' when a font is
    // ready
    var debug = false;

    var canvas = document.createElement("canvas");
    var style = "border: 1px solid black; position:absolute; top: " + 
                (debug ? (80 * fontCount) : "-200") + "px; left: 100px;";
    canvas.setAttribute("style", style);
    canvas.setAttribute("width", 100);
    canvas.setAttribute("heigth", 70);
    document.body.appendChild(canvas);

    // Retrieve font charset
    var charset = Fonts[fontName].charset || [];
    // if the charset is too small make it repeat a few times
    var count = 30;
    while (count-- && charset.length <= 30)
     charset = charset.concat(charset.slice());

    // Get the font size canvas think it will be
    var ctx = canvas.getContext("2d");
    var testString = "";
    for (var i = 0; i < charset.length; i++) {
      var unicode = new Number("0x" + GlyphsUnicode[charset[i]]);
      if (!unicode)
        error("Unicode for " + charset[i] + " is has not been found in the glyphs list");
      testString += String.fromCharCode(unicode);
    }
    ctx.font = "20px " + fontName + ", Symbol";
    var textWidth = ctx.mozMeasureText(testString);

    if (debug)
      ctx.fillText(testString, 20, 20);

    var start = Date.now();
    var interval = window.setInterval(function(self) {
      ctx.font = "20px " + fontName + ", Symbol";
<commit_msg>Use spaces to compare font size to detect when the @font-face rule works, this seems to be more accurate<commit_after>/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */

/**
 * Maximum file size of the font.
 */
var kMaxFontFileSize = 40000;

/**
 * Maximum number of glyphs per font.
*/
var kMaxGlyphsCount = 65526;

/**
 * Maximum time to wait for a font to be loaded by @font-face
 */
var kMaxWaitForFontFace = 1000;

 /*
  * Useful for debugging when you want to certains operations depending on how
  * many fonts are loaded.
  */
var fontCount = 0;

/**
 * Hold a map of decoded fonts and of the standard fourteen Type1 fonts and
 * their acronyms.
 * TODO Add the standard fourteen Type1 fonts list by default
 *      http://cgit.freedesktop.org/poppler/poppler/tree/poppler/GfxFont.cc#n65
 */
var Fonts = {
  _active: null,
  get active() {
    return this._active;
  },

  set active(aFontName) {
    this._active = this[aFontName];
  },

  unicodeFromCode: function fonts_unicodeFromCode(aCode) {
    var active = this._active;
    if (!active || !active.encoding)
      return aCode;

    var difference = active.encoding[aCode];
    var unicode = GlyphsUnicode[difference];
    return unicode ? "0x" + unicode : aCode;
  }
};

/**
 * 'Font' is the class the outside world should use, it encapsulate all the font
 * decoding logics whatever type it is (assuming the font type is supported).
 *
 * For example to read a Type1 font and to attach it to the document:
 *   var type1Font = new Font("MyFontName", binaryData, aFontEncoding, "Type1");
 *   type1Font.bind();
 *
 * As an improvment the last parameter can be replaced by an automatic guess
 * of the font type based on the first byte of the file.
 *
 * XXX There is now too many parameters, this should be turned into an
 * object containing all the required informations about the font
 */
var Font = function(aName, aFile, aEncoding, aCharset, aBBox, aType) {
  this.name = aName;

  // If the font has already been decoded simply return
  if (Fonts[aName]) {
    this.font = Fonts[aName].data;
    return;
  }
  fontCount++;

  var start = Date.now();
  switch (aType) {
    case "Type1":
      var cff = new CFF(aName, aBBox, aFile);
      this.mimetype = "font/otf";

      // Wrap the CFF data inside an OTF font file
      this.font = this.cover(cff);
      break;

    case "TrueType":
      this.mimetype = "font/ttf";
      var ttf = new TrueType(aFile);
      this.font = ttf.data;
      break;

    default:
      warn("Font " + aType + " is not supported");
      break;
  }
  var end = Date.now();

  Fonts[aName] = {
    data: this.font,
    encoding: aEncoding,
    charset: aCharset ? aCharset.slice() : null,
    loading: true
  }

  // Attach the font to the document
  this.bind();
};

Font.prototype = {
  name: null,
  font: null,
  mimetype: null,

  bind: function font_bind() {
    var data = this.font;

    // Compute the binary data to base 64
    var str = [];
    var count = data.length;
    for (var i = 0; i < count; i++)
      str.push(data.getChar ? data.getChar()
                            : String.fromCharCode(data[i]));

    var dataBase64 = window.btoa(str.join(""));
    var fontName = this.name;

    /** Hack begin */

    // Actually there is not event when a font has finished downloading so
    // the following tons of code are a dirty hack to 'guess' when a font is
    // ready
    var debug = false;

    if (debug) {
      var name = document.createElement("font");
      name.setAttribute("style", "position: absolute; left: 20px; top: " +
                                 (100 * fontCount + 60) + "px");
      name.innerHTML = fontName;
      document.body.appendChild(name);
    }

    var canvas = document.createElement("canvas");
    var style = "border: 1px solid black; position:absolute; top: " +
                (debug ? (100 * fontCount) : "-200") + "px; left: 2px; width: 340px; height: 100px";
    canvas.setAttribute("style", style);
    canvas.setAttribute("width", 340);
    canvas.setAttribute("heigth", 100);
    document.body.appendChild(canvas);

    // Retrieve font charset
    var charset = Fonts[fontName].charset || [];
    // if the charset is too small make it repeat a few times
    var count = 30;
    while (count-- && charset.length <= 30)
     charset = charset.concat(charset.slice());

    // Get the font size canvas think it will be for 'spaces'
    var ctx = canvas.getContext("2d");
    var testString = "     ";

    // When debugging use the characters provided by the charsets to visually
    // see what's happening
    if (debug) {
      for (var i = 0; i < charset.length; i++) {
        var unicode = new Number("0x" + GlyphsUnicode[charset[i]]);
        if (!unicode)
          error("Unicode for " + charset[i] + " is has not been found in the glyphs list");
        testString += String.fromCharCode(unicode);
      }
    }
    ctx.font = "bold italic 20px " + fontName + ", Symbol, Arial";
    var textWidth = ctx.mozMeasureText(testString);

    if (debug)
      ctx.fillText(testString, 20, 20);

    var start = Date.now();
    var interval = window.setInterval(function(self) {
      ctx.font = "bold italic 20px " + fontName + ", Symbol, Arial";

{'fraction_non_alphanumeric': 0.12055395038358076, 'fraction_numerical': 0.01384875958951878, 'mean_word_length': 2.724675324675325, 'pattern_counts': {'":': 4, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '699045', 'n_tokens_mistral': 3316, 'n_tokens_neox': 3097, 'n_words': 1074}