<commit_before>/* Copyright 2021 Mozilla Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

const WIDTH_FACTOR = 1.2;
const HEIGHT_FACTOR = 1.2;

class FontInfo {
  constructor(xfaFont, fonts) {
    if (!xfaFont) {
      [this.pdfFont, this.xfaFont] = this.defaultFont(fonts);
      return;
    }

    this.xfaFont = xfaFont;
    let typeface = fonts[xfaFont.typeface];
    if (!typeface) {
      typeface = fonts[`${xfaFont.typeface}-PdfJS-XFA`];
    }
    if (!typeface) {
      [this.pdfFont, this.xfaFont] = this.defaultFont(fonts);
      return;
    }

    this.pdfFont = null;
    if (xfaFont.posture === "italic") {
      if (xfaFont.weight === "bold") {
        this.pdfFont = typeface.bolditalic;
      } else {
        this.pdfFont = typeface.italic;
      }
    } else if (xfaFont.weigth === "bold") {
      this.pdfFont = typeface.bold;
    } else {
      this.pdfFont = typeface.regular;
    }

    if (!this.pdfFont) {
      [this.pdfFont, this.xfaFont] = this.defaultFont(fonts);
    }
  }

  defaultFont(fonts) {
    // TODO: Add a default font based on Liberation.
    const font =
      fonts.Helvetica ||
      fonts["Myriad Pro"] ||
      fonts.Arial ||
      fonts.ArialMT ||
      Object.values(fonts)[0];
    const pdfFont = font.regular;
    const info = pdfFont.cssFontInfo;
    const xfaFont = {
      typeface: info.fontFamily,
      posture: "normal",
      weight: "normal",
      size: 10,
    };
    return [pdfFont, xfaFont];
  }
}

class FontSelector {
  constructor(defaultXfaFont, fonts) {
    this.fonts = fonts;
    this.stack = [new FontInfo(defaultXfaFont, fonts)];
  }

  pushFont(xfaFont) {
    const lastFont = this.stack[this.stack.length - 1];
    for (const name of ["typeface", "posture", "weight", "size"]) {
      if (!xfaFont[name]) {
        xfaFont[name] = lastFont.xfaFont[name];
      }
    }

    const fontInfo = new FontInfo(xfaFont, this.fonts);
    if (!fontInfo.pdfFont) {
      fontInfo.pdfFont = lastFont.pdfFont;
    }
    this.stack.push(fontInfo);
  }

  popFont() {
    this.stack.pop();
  }

  topFont() {
    return this.stack[this.stack.length - 1];
  }
}

/**
 * Compute a text area dimensions based on font metrics.
 */
class TextMeasure {
  constructor(defaultXfaFont, fonts) {
    this.glyphs = [];
    this.fontSelector = new FontSelector(defaultXfaFont, fonts);
  }

  pushFont(xfaFont) {
    return this.fontSelector.pushFont(xfaFont);
  }

  popFont(xfaFont) {
    return this.fontSelector.popFont();
  }

  addString(str) {
    if (!str) {
      return;
    }

    const lastFont = this.fontSelector.topFont();
    const pdfFont = lastFont.pdfFont;
    const fontSize = lastFont.xfaFont.size;
    const lineHeight = Math.round(Math.max(1, pdfFont.lineHeight) * fontSize);
    const scale = fontSize / 1000;

    for (const line of str.split(/[\u2029\n]/)) {
      const encodedLine = pdfFont.encodeString(line).join("");
      const glyphs = pdfFont.charsToGlyphs(encodedLine);

      for (const glyph of glyphs) {
        this.glyphs.push([
          glyph.width * scale,
          lineHeight,
          glyph.unicode === " ",
          false,
        ]);
      }

      this.glyphs.push([0, 0, false, true]);
    }

    this.glyphs.pop();
  }

  compute(maxWidth) {
    let lastSpacePos = -1,
      lastSpaceWidth = 0,
      width = 0,
      height = 0,
      currentLineWidth = 0,
      currentLineHeight = 0;

    for (let i = 0, ii = this.glyphs.length; i < ii; i++) {
      const [glyphWidth, glyphHeight, isSpace, isEOL] = this.glyphs[i];
      if (isEOL) {
        width = Math.max(width, currentLineWidth);<commit_msg>XFA - When no fonts in the pdf just use font size as width when measuring text<commit_after>/* Copyright 2021 Mozilla Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

const WIDTH_FACTOR = 1.2;
const HEIGHT_FACTOR = 1.2;

class FontInfo {
  constructor(xfaFont, fonts) {
    if (!xfaFont) {
      [this.pdfFont, this.xfaFont] = this.defaultFont(fonts);
      return;
    }

    this.xfaFont = xfaFont;
    let typeface = fonts[xfaFont.typeface];
    if (!typeface) {
      typeface = fonts[`${xfaFont.typeface}-PdfJS-XFA`];
    }
    if (!typeface) {
      [this.pdfFont, this.xfaFont] = this.defaultFont(fonts);
      return;
    }

    this.pdfFont = null;
    if (xfaFont.posture === "italic") {
      if (xfaFont.weight === "bold") {
        this.pdfFont = typeface.bolditalic;
      } else {
        this.pdfFont = typeface.italic;
      }
    } else if (xfaFont.weigth === "bold") {
      this.pdfFont = typeface.bold;
    } else {
      this.pdfFont = typeface.regular;
    }

    if (!this.pdfFont) {
      [this.pdfFont, this.xfaFont] = this.defaultFont(fonts);
    }
  }

  defaultFont(fonts) {
    // TODO: Add a default font based on Liberation.
    const font =
      fonts.Helvetica ||
      fonts["Myriad Pro"] ||
      fonts.Arial ||
      fonts.ArialMT ||
      Object.values(fonts)[0];
    if (font && font.regular) {
      const pdfFont = font.regular;
      const info = pdfFont.cssFontInfo;
      const xfaFont = {
        typeface: info.fontFamily,
        posture: "normal",
        weight: "normal",
        size: 10,
      };
      return [pdfFont, xfaFont];
    }

    const xfaFont = {
      typeface: "Courier",
      posture: "normal",
      weight: "normal",
      size: 10,
    };
    return [null, xfaFont];
  }
}

class FontSelector {
  constructor(defaultXfaFont, fonts) {
    this.fonts = fonts;
    this.stack = [new FontInfo(defaultXfaFont, fonts)];
  }

  pushFont(xfaFont) {
    const lastFont = this.stack[this.stack.length - 1];
    for (const name of ["typeface", "posture", "weight", "size"]) {
      if (!xfaFont[name]) {
        xfaFont[name] = lastFont.xfaFont[name];
      }
    }

    const fontInfo = new FontInfo(xfaFont, this.fonts);
    if (!fontInfo.pdfFont) {
      fontInfo.pdfFont = lastFont.pdfFont;
    }
    this.stack.push(fontInfo);
  }

  popFont() {
    this.stack.pop();
  }

  topFont() {
    return this.stack[this.stack.length - 1];
  }
}

/**
 * Compute a text area dimensions based on font metrics.
 */
class TextMeasure {
  constructor(defaultXfaFont, fonts) {
    this.glyphs = [];
    this.fontSelector = new FontSelector(defaultXfaFont, fonts);
  }

  pushFont(xfaFont) {
    return this.fontSelector.pushFont(xfaFont);
  }

  popFont(xfaFont) {
    return this.fontSelector.popFont();
  }

  addString(str) {
    if (!str) {
      return;
    }

    const lastFont = this.fontSelector.topFont();
    const fontSize = lastFont.xfaFont.size;
    if (lastFont.pdfFont) {
      const pdfFont = lastFont.pdfFont;
      const lineHeight = Math.round(Math.max(1, pdfFont.lineHeight) * fontSize);
      const scale = fontSize / 1000;

      for (const line of str.split(/[\u2029\n]/)) {
        const encodedLine = pdfFont.encodeString(line).join("");
        const glyphs = pdfFont.charsToGlyphs(encodedLine);

        for (const glyph of glyphs) {
          this.glyphs.push([
            glyph.width * scale,
            lineHeight,
            glyph.unicode === " ",
            false,
          ]);
        }

        this.glyphs.push([0, 0, false, true]);
      }
      this.glyphs.pop();
      return;
    }

    // When we have no font in the pdf, just use the font size as default width.
    for (const line of str.split(/[\u2029\n]/)) {
      for (const char of line.split("")) {
        this.glyphs.push([fontSize, fontSize, char === " ", false]);
      }

      this.glyphs.push([0, 0, false, true]);
    }
    this.glyphs.pop();
  }

  compute(maxWidth) {
    let lastSpacePos = -1,
      lastSpaceWidth = 0,
      width = 0,
      height = 0,
      currentLineWidth = 0,
      currentLineHeight = 0;

    for (let i = 0, ii = this.glyphs.length; i < ii; i++) {
      const [glyphWidth, glyphHeight, isSpace, isEOL] = this.glyphs[i];
      if (isEOL) {
        width = Math.max(width, currentLineWidth);
{'fraction_non_alphanumeric': 0.12985559162655322, 'fraction_numerical': 0.008731669092130303, 'mean_word_length': 2.4722114263505635, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4893165', 'n_tokens_mistral': 3078, 'n_tokens_neox': 2905, 'n_words': 722}