<commit_before>'use strict';

import jsPDF from 'jspdf';
import {Table, Row, Cell, Column} from './models.js';
import {Config, themes, FONT_ROW_RATIO} from './config.js';
import './polyfills.js';

var cursor, // An object keeping track of the x and y position of the next table cell to draw
    settings, // Default options merged with user options
    globalAddPageContent = function() {}, // Override with doc.autoTableAddPageContent
    table; // The current Table instance

/**
 * Create a table from a set of rows and columns.
 *
 * @param {Object[]|String[]} headers Either as an array of objects or array of strings
 * @param {Object[][]|String[][]} data Either as an array of objects or array of strings
 * @param {Object} [userOptions={}] Options that will override the default ones
 */
jsPDF.API.autoTable = function (headers, data, userOptions = {}) {
    validateInput(headers, data, userOptions);
    Config.setJspdfInstance(this);
    var doc = Config.getJspdfInstance();
    settings = Config.initSettings(userOptions);

    // Need a cursor y as it needs to be reset after each page (row.y can't do that)
    // Also prefer cursor to column.x as the cursor is easier to modify in the hooks
    cursor = {
        x: settings.margin.left,
        y: settings.startY === false ? settings.margin.top : settings.startY
    };

    // Create the table model with its columns, rows and cells
    createModels(headers, data);
    calculateWidths(this, doc.internal.pageSize.width);

    // Page break if there is room for only the first data row
    var firstRowHeight = table.rows[0] && settings.pageBreak === 'auto' ? table.rows[0].height : 0;
    var minTableBottomPos = settings.startY + settings.margin.bottom + table.headerRow.height + firstRowHeight;
    if (settings.pageBreak === 'avoid') {
        minTableBottomPos += table.height;
    }
    var pageHeight = doc.internal.pageSize.height;
    if ((settings.pageBreak === 'always' && settings.startY !== false) ||
        (settings.startY !== false && minTableBottomPos > pageHeight)) {
        addPage();
        cursor.y = settings.margin.top;
    }

    Config.applyStyles(Config.getUserStyles());
    if (settings.drawHeaderRow(table.headerRow, hooksData({row: table.headerRow})) !== false) {
        printRow(table.headerRow, settings.drawHeaderCell);
    }
    Config.applyStyles(Config.getUserStyles());
    printRows();

    settings.addPageContent(hooksData());
    Config.applyStyles(Config.getUserStyles());
    globalAddPageContent(hooksData());
    Config.applyStyles(Config.getUserStyles());

    return this;
};

/**
 * Returns the Y position of the last drawn cell
 * @returns int
 */
jsPDF.API.autoTableEndPosY = function () {
    if (typeof cursor === 'undefined' || typeof cursor.y === 'undefined') {
        return 0;
    }
    return cursor.y;
};

jsPDF.API.autoTableAddPageContent = function (hook) {
    var isBeforeCallsToAutoTable = true; // TODO How to know this?
    if (!isBeforeCallsToAutoTable) {
        console.error("autoTableAddPageContent has to be called before any calls to autoTable.");
    } else if (typeof hook !== "function") {
        console.error("A function has to be provided to autoTableAddPageContent, got: " + typeof hook)
    }
    globalAddPageContent = hook;
};

/**<commit_msg>Reset cursor after creating new jspdf document (fixes #186)<commit_after>'use strict';

import jsPDF from 'jspdf';
import {Table, Row, Cell, Column} from './models.js';
import {Config, themes, FONT_ROW_RATIO} from './config.js';
import './polyfills.js';

var cursor, // An object keeping track of the x and y position of the next table cell to draw
    settings, // Default options merged with user options
    globalAddPageContent = function() {}, // Override with doc.autoTableAddPageContent
    table; // The current Table instance

/**
 * Create a table from a set of rows and columns.
 *
 * @param {Object[]|String[]} headers Either as an array of objects or array of strings
 * @param {Object[][]|String[][]} data Either as an array of objects or array of strings
 * @param {Object} [userOptions={}] Options that will override the default ones
 */
jsPDF.API.autoTable = function (headers, data, userOptions = {}) {
    validateInput(headers, data, userOptions);
    Config.setJspdfInstance(this);
    var doc = Config.getJspdfInstance();
    settings = Config.initSettings(userOptions);

    // Need a cursor y as it needs to be reset after each page (row.y can't do that)
    // Also prefer cursor to column.x as the cursor is easier to modify in the hooks
    cursor = {
        x: settings.margin.left,
        y: settings.startY === false ? settings.margin.top : settings.startY
    };

    // Create the table model with its columns, rows and cells
    createModels(headers, data);
    calculateWidths(this, doc.internal.pageSize.width);

    // Page break if there is room for only the first data row
    var firstRowHeight = table.rows[0] && settings.pageBreak === 'auto' ? table.rows[0].height : 0;
    var minTableBottomPos = settings.startY + settings.margin.bottom + table.headerRow.height + firstRowHeight;
    if (settings.pageBreak === 'avoid') {
        minTableBottomPos += table.height;
    }
    var pageHeight = doc.internal.pageSize.height;
    if ((settings.pageBreak === 'always' && settings.startY !== false) ||
        (settings.startY !== false && minTableBottomPos > pageHeight)) {
        addPage();
        cursor.y = settings.margin.top;
    }

    Config.applyStyles(Config.getUserStyles());
    if (settings.drawHeaderRow(table.headerRow, hooksData({row: table.headerRow})) !== false) {
        printRow(table.headerRow, settings.drawHeaderCell);
    }
    Config.applyStyles(Config.getUserStyles());
    printRows();

    settings.addPageContent(hooksData());
    Config.applyStyles(Config.getUserStyles());
    globalAddPageContent(hooksData());
    Config.applyStyles(Config.getUserStyles());

    return this;
};

/**
 * Returns the Y position of the last drawn cell
 * @returns int
 */
jsPDF.API.autoTableEndPosY = function () {
    var sameDocument = Config.getJspdfInstance() === this;
    if (sameDocument && cursor && typeof cursor.y === 'number') {
        return cursor.y;
    } else {
        return 0;
    }
};

jsPDF.API.autoTableAddPageContent = function (hook) {
    if (typeof hook !== "function") {
        console.error("A function has to be provided to autoTableAddPageContent, got: " + typeof hook)
    }
    globalAddPageContent = hook;
};

/**
{'fraction_non_alphanumeric': 0.12286636936798401, 'fraction_numerical': 0.0016915269875442103, 'mean_word_length': 3.4762560220234, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6145139', 'n_tokens_mistral': 1852, 'n_tokens_neox': 1850, 'n_words': 585}