<commit_before>/*jslint node: true, unparam: true, nomen: true */
'use strict';

var lodash = require('lodash'),
    EventEmitter = require('events').EventEmitter,

    getPaddingValue = function (direction, p) {
        var l =  p && p.length;
        if (direction === 'vertical' || direction === 'horizontal') {
            if (l === 1) {
                return p[0] * 2;
            }
            if (l === 2) {
                return direction === 'vertical' ? p[0] * 2 : p[1] * 2;
            }
            if (l === 3) {
                return direction === 'vertical' ? p[0] + p[2] : p[1] * 2;
            }
            if (l === 4) {
                return direction === 'vertical' ? p[0] + p[2] : p[1] + p[3];
            }
        }
        if (l === 1) {
            return p[0];
        }
        if (l === 2) {
            return direction === 'top' || direction === 'bottom' ? p[0] : p[1];
        }
        if (l === 3) {
            return direction === 'top' ? p[0] : (direction === 'bottom' ? p[2] : p[1]);
        }
        if (l === 4) {
            return direction === 'top' ? p[0] : (
                direction === 'bottom' ? p[2] : (
                    direction === 'left' ? p[3] : p[1]
                )
            );
        }
        return 0;
    },

    addCellBackground = function (self, column, row, pos, isHeader) {
        self.pdf
            .rect(pos.x, pos.y, column.width, row._renderedContent.height)
            .fill();
    },

    addCellBorder = function (self, column, row, pos, isHeader) {
        var border = isHeader ? column.headerBorder : column.border,
            bpos = {
                x: pos.x + column.width,
                y: pos.y + row._renderedContent.height
            };

        if (border.indexOf('L') !== -1) {
            self.pdf.moveTo(pos.x, pos.y).lineTo(pos.x, bpos.y).stroke();
        }
        if (border.indexOf('T') !== -1) {
            self.pdf.moveTo(pos.x, pos.y).lineTo(bpos.x, pos.y).stroke();
        }
        if (border.indexOf('B') !== -1) {
            self.pdf.moveTo(pos.x, bpos.y).lineTo(bpos.x, bpos.y).stroke();
        }
        if (border.indexOf('R') !== -1) {
            self.pdf.moveTo(bpos.x, pos.y).lineTo(bpos.x, bpos.y).stroke();
        }
    },

    addCell = function (self, column, row, pos, isHeader) {
        var width = column.width,
            padding = {
                left: 0,
                top: 0
            },
            data = row._renderedContent.data[column.id] || '',
            renderer = isHeader ? column.headerRenderer : column.renderer,
            y = pos.y;

        if (!isHeader && column.padding) {
            padding.left = getPaddingValue('left', column.padding);
            padding.top = getPaddingValue('top', column.padding);
            width -= getPaddingValue('horizontal', column.padding);
        }
        y += padding.top;
        // if specified, cache is not used and renderer is called one more time
        if (renderer && column.cache === false) {
            data = renderer(self, row, true, column, lodash.clone(pos), padding);
        }
        // manage vertical alignement
        if (column.valign === 'center') {
            y += (row._renderedContent.height - row._renderedContent.dataHeight[column.id]) / 2;
        } else if (column.valign === 'bottom') {
            y += (row._renderedContent.height - row._renderedContent.dataHeight[column.id]);
        }

        self.pdf.text(data, pos.x + padding.left, y, lodash.assign({
            height: row._renderedContent.height,
            width: width
        }, column));

        pos.x += column.width;
    },

    addRow = function (self, row, isHeader) {
        var pos = {
                x: self.pdf.page.margins.left,
                y: self.pdf.y
            },
            ev = {
                cancel: false
            };

        // the content might be higher than the remaining height on the page.
        if (self.pdf.y + row._renderedContent.height > self.pdf.page.height - self.pdf.page.margins.bottom - self.bottomMargin) {
            self.emitter.emit('page-add', self, row, ev);
            if (!ev.cancel) {
                self.pdf.addPage();
            }
            self.emitter.emit('page-added', self, row);
        }

        lodash.forEach(self.getColumns(), function (column) {
            if ((!isHeader && column.fill) || (isHeader && column.headerFill)) {
                addCellBackground(self, column, row, pos, isHeader);
            }
            if ((!isHeader && column.border) || (isHeader && column.headerBorder)) {
                addCellBorder(self, column, row, pos, isHeader);
            }
            addCell(self, column, row, pos, isHeader);
        });
<commit_msg>Resets Y position on when a new page is added<commit_after>/*jslint node: true, unparam: true, nomen: true */
'use strict';

var lodash = require('lodash'),
    EventEmitter = require('events').EventEmitter,

    getPaddingValue = function (direction, p) {
        var l =  p && p.length;
        if (direction === 'vertical' || direction === 'horizontal') {
            if (l === 1) {
                return p[0] * 2;
            }
            if (l === 2) {
                return direction === 'vertical' ? p[0] * 2 : p[1] * 2;
            }
            if (l === 3) {
                return direction === 'vertical' ? p[0] + p[2] : p[1] * 2;
            }
            if (l === 4) {
                return direction === 'vertical' ? p[0] + p[2] : p[1] + p[3];
            }
        }
        if (l === 1) {
            return p[0];
        }
        if (l === 2) {
            return direction === 'top' || direction === 'bottom' ? p[0] : p[1];
        }
        if (l === 3) {
            return direction === 'top' ? p[0] : (direction === 'bottom' ? p[2] : p[1]);
        }
        if (l === 4) {
            return direction === 'top' ? p[0] : (
                direction === 'bottom' ? p[2] : (
                    direction === 'left' ? p[3] : p[1]
                )
            );
        }
        return 0;
    },

    addCellBackground = function (self, column, row, pos, isHeader) {
        self.pdf
            .rect(pos.x, pos.y, column.width, row._renderedContent.height)
            .fill();
    },

    addCellBorder = function (self, column, row, pos, isHeader) {
        var border = isHeader ? column.headerBorder : column.border,
            bpos = {
                x: pos.x + column.width,
                y: pos.y + row._renderedContent.height
            };

        if (border.indexOf('L') !== -1) {
            self.pdf.moveTo(pos.x, pos.y).lineTo(pos.x, bpos.y).stroke();
        }
        if (border.indexOf('T') !== -1) {
            self.pdf.moveTo(pos.x, pos.y).lineTo(bpos.x, pos.y).stroke();
        }
        if (border.indexOf('B') !== -1) {
            self.pdf.moveTo(pos.x, bpos.y).lineTo(bpos.x, bpos.y).stroke();
        }
        if (border.indexOf('R') !== -1) {
            self.pdf.moveTo(bpos.x, pos.y).lineTo(bpos.x, bpos.y).stroke();
        }
    },

    addCell = function (self, column, row, pos, isHeader) {
        var width = column.width,
            padding = {
                left: 0,
                top: 0
            },
            data = row._renderedContent.data[column.id] || '',
            renderer = isHeader ? column.headerRenderer : column.renderer,
            y = pos.y;

        if (!isHeader && column.padding) {
            padding.left = getPaddingValue('left', column.padding);
            padding.top = getPaddingValue('top', column.padding);
            width -= getPaddingValue('horizontal', column.padding);
        }
        y += padding.top;
        // if specified, cache is not used and renderer is called one more time
        if (renderer && column.cache === false) {
            data = renderer(self, row, true, column, lodash.clone(pos), padding);
        }
        // manage vertical alignement
        if (column.valign === 'center') {
            y += (row._renderedContent.height - row._renderedContent.dataHeight[column.id]) / 2;
        } else if (column.valign === 'bottom') {
            y += (row._renderedContent.height - row._renderedContent.dataHeight[column.id]);
        }

        self.pdf.text(data, pos.x + padding.left, y, lodash.assign({
            height: row._renderedContent.height,
            width: width
        }, column));

        pos.x += column.width;
    },

    addRow = function (self, row, isHeader) {
        var pos = {
                x: self.pdf.page.margins.left,
                y: self.pdf.y
            },
            ev = {
                cancel: false
            };

        // the content might be higher than the remaining height on the page.
        if (self.pdf.y + row._renderedContent.height > self.pdf.page.height - self.pdf.page.margins.bottom - self.bottomMargin) {
            self.emitter.emit('page-add', self, row, ev);
            if (!ev.cancel) {
                self.pdf.addPage();
                // Reset Y position for next page
                pos.y = self.pdf.page.margins.top;
            }
            self.emitter.emit('page-added', self, row);
        }

        lodash.forEach(self.getColumns(), function (column) {
            if ((!isHeader && column.fill) || (isHeader && column.headerFill)) {
                addCellBackground(self, column, row, pos, isHeader);
            }
            if ((!isHeader && column.border) || (isHeader && column.headerBorder)) {
                addCellBorder(self, column, row, pos, isHeader);
            }
            addCell(self, column, row, pos, isHeader);
        });

{'fraction_non_alphanumeric': 0.15604532695706413, 'fraction_numerical': 0.008316872855806217, 'mean_word_length': 1.553756304751792, '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': '6368526', 'n_tokens_mistral': 2952, 'n_tokens_neox': 2847, 'n_words': 659}