<commit_before>type Size = { width: number, height: number };

/**
 * Wraps the native Canvas element and overrides its CanvasRenderingContext2D to
 * provide resolution independent rendering based on `window.devicePixelRatio`.
 */
export class HdpiCanvas {
    // The width/height attributes of the Canvas element default to
    // 300/150 according to w3.org.
    constructor(width = 300, height = 150) {
        this.updatePixelRatio(0, false);
        this.resize(width, height);
    }

    private _parent: HTMLElement | null = null;
    set parent(value: HTMLElement | null) {
        if (this._parent !== value) {
            this.remove();
            if (value) {
                value.appendChild(this.canvas);
            }
            this._parent = value;
        }

    }
    get parent(): HTMLElement | null {
        return this._parent;
    }

    private remove() {
        const parent = this.canvas.parentNode;

        if (parent !== null) {
            parent.removeChild(this.canvas);
        }
    }

    readonly canvas = document.createElement('canvas');

    readonly context = this.canvas.getContext('2d')!;

    destroy() {
        this.canvas.remove();
        (this as any)._canvas = undefined;
        Object.freeze(this);
    }

    toImage(): HTMLImageElement {
        const img = document.createElement('img');
        img.src = this.canvas.toDataURL();
        return img;
    }

    /**
     * @param fileName The `.png` extension is going to be added automatically.
     */
    download(fileName: string) {
        // Chart images saved as JPEG are a few times larger at 50% quality than PNG images,
        // so we don't support saving to JPEG.
        const type = 'image/png';
        // The background of our canvas is transparent, so we create a temporary canvas
        // with the white background and paint our canvas on top of it.
        const canvas = document.createElement('canvas');
        canvas.width = this.canvas.width;
        canvas.height = this.canvas.height;
        const ctx = canvas.getContext('2d')!;
        ctx.fillStyle = 'white';
        ctx.fillRect(0, 0, canvas.width, canvas.height);
        ctx.drawImage(this.canvas, 0, 0);

        const dataUrl = canvas.toDataURL(type);

        if (navigator.msSaveOrOpenBlob) { // IE11
            const binary = atob(dataUrl.split(',')[1]); // strip the `data:image/png;base64,` part
            const array = [];
            for (let i = 0, n = binary.length; i < n; i++) {
                array.push(binary.charCodeAt(i));
            }
            const blob = new Blob([new Uint8Array(array)], {type});

            navigator.msSaveOrOpenBlob(blob, fileName + '.png');
        } else {
            const a = document.createElement('a');
            a.href = dataUrl;
            a.download = fileName + '.png';
            a.click();
        }
    }

    // `NaN` is deliberate here, so that overrides are always applied
    // and the `resetTransform` inside the `resize` method works in IE11.
    _pixelRatio: number = NaN;
    get pixelRatio(): number {
        return this._pixelRatio;
    }

    private overrides: any;

    /**
     * Updates the pixel ratio of the Canvas element with the given value,
     * or uses the window.devicePixelRatio (default), then resizes the Canvas
     * element accordingly (default).
     * @param ratio
     * @param resize
     */
    updatePixelRatio(ratio = 0, resize = true) {
        const pixelRatio = ratio || window.devicePixelRatio;

        if (pixelRatio === this.pixelRatio) {
            return;
        }

        const canvas = this.canvas;<commit_msg>Make chart download work in Firefox.<commit_after>type Size = { width: number, height: number };

/**
 * Wraps the native Canvas element and overrides its CanvasRenderingContext2D to
 * provide resolution independent rendering based on `window.devicePixelRatio`.
 */
export class HdpiCanvas {
    // The width/height attributes of the Canvas element default to
    // 300/150 according to w3.org.
    constructor(width = 300, height = 150) {
        this.updatePixelRatio(0, false);
        this.resize(width, height);
    }

    private _parent: HTMLElement | null = null;
    set parent(value: HTMLElement | null) {
        if (this._parent !== value) {
            this.remove();
            if (value) {
                value.appendChild(this.canvas);
            }
            this._parent = value;
        }

    }
    get parent(): HTMLElement | null {
        return this._parent;
    }

    private remove() {
        const parent = this.canvas.parentNode;

        if (parent !== null) {
            parent.removeChild(this.canvas);
        }
    }

    readonly canvas = document.createElement('canvas');

    readonly context = this.canvas.getContext('2d')!;

    destroy() {
        this.canvas.remove();
        (this as any)._canvas = undefined;
        Object.freeze(this);
    }

    toImage(): HTMLImageElement {
        const img = document.createElement('img');
        img.src = this.canvas.toDataURL();
        return img;
    }

    /**
     * @param fileName The `.png` extension is going to be added automatically.
     */
    download(fileName: string) {
        // Chart images saved as JPEG are a few times larger at 50% quality than PNG images,
        // so we don't support saving to JPEG.
        const type = 'image/png';
        // The background of our canvas is transparent, so we create a temporary canvas
        // with the white background and paint our canvas on top of it.
        const canvas = document.createElement('canvas');
        canvas.width = this.canvas.width;
        canvas.height = this.canvas.height;
        const ctx = canvas.getContext('2d')!;
        ctx.fillStyle = 'white';
        ctx.fillRect(0, 0, canvas.width, canvas.height);
        ctx.drawImage(this.canvas, 0, 0);

        const dataUrl = canvas.toDataURL(type);

        if (navigator.msSaveOrOpenBlob) { // IE11
            const binary = atob(dataUrl.split(',')[1]); // strip the `data:image/png;base64,` part
            const array = [];
            for (let i = 0, n = binary.length; i < n; i++) {
                array.push(binary.charCodeAt(i));
            }
            const blob = new Blob([new Uint8Array(array)], {type});

            navigator.msSaveOrOpenBlob(blob, fileName + '.png');
        } else {
            const a = document.createElement('a');
            a.href = dataUrl;
            a.download = fileName + '.png';
            a.style.display = 'none';
            document.body.appendChild(a); // required for the `click` to work in Firefox
            a.click();
            document.body.removeChild(a);
        }
    }

    // `NaN` is deliberate here, so that overrides are always applied
    // and the `resetTransform` inside the `resize` method works in IE11.
    _pixelRatio: number = NaN;
    get pixelRatio(): number {
        return this._pixelRatio;
    }

    private overrides: any;

    /**
     * Updates the pixel ratio of the Canvas element with the given value,
     * or uses the window.devicePixelRatio (default), then resizes the Canvas
     * element accordingly (default).
     * @param ratio
     * @param resize
     */
    updatePixelRatio(ratio = 0, resize = true) {
        const pixelRatio = ratio || window.devicePixelRatio;

        if (pixelRatio === this.pixelRatio) {
            return;
        }

        const canvas = this.canvas;
{'fraction_non_alphanumeric': 0.11288590604026846, 'fraction_numerical': 0.008859060402684563, 'mean_word_length': 2.0878574388727724, 'pattern_counts': {'":': 0, '<': 5, '<?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': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '254887', 'n_tokens_mistral': 2105, 'n_tokens_neox': 2029, 'n_words': 622}