<commit_before>import {colorStringToArray, colorArrayToString} from './util-colors';
import ImageData from './ImageData';
import GC from './GC';

export default class CanvasContext {

  constructor(gc) {
    this._gc = gc;
    this._state = createState();
    this._savedStates = [];
    this.canvas = {
      width: 0,
      height: 0,
      style: {}
    };
    for (let name in properties) {
      defineProperty(this, name);
    }
  }

  measureText(text) {
    // TODO: delegate to native function, once it is implemented (#56)
    return {width: text.length * 5 + 5};
  }

  // ImageData operations

  getImageData(x, y, width, height) {
    checkRequiredArgs(arguments, 4, 'CanvasContext.getImageData');
    this._gc.flush();
    // TODO check validity of args
    let array = this._gc.getImageData(x, y, width, height);
    return new ImageData(array, width, height);
  }

  putImageData(imageData, x, y) {
    checkRequiredArgs(arguments, 3, 'CanvasContext.putImageData');
    this._gc.flush();
    this._gc.putImageData(imageData, x, y);
  }

  createImageData(width, height) {
    if (arguments[0] instanceof ImageData) {
      let data = arguments[0];
      width = data.width;
      height = data.height;
    } else {
      checkRequiredArgs(arguments, 2, 'CanvasContext.createImageData');
    }
    return new ImageData(width, height);
  }

  _init(width, height) {
    this.canvas.width = width;
    this.canvas.height = height;
    this._gc.init({
      width,
      height,
      font: [['sans-serif'], 12, false, false],
      fillStyle: [0, 0, 0, 255],
      strokeStyle: [0, 0, 0, 255]
    });
  }

}

// State operations

defineMethod('save', 0, function() {
  this._savedStates.push(Object.assign({}, this._state));
});

defineMethod('restore', 0, function() {
  this._state = this._savedStates.pop() || this._state;
});

// Path operations

defineMethod('beginPath');

defineMethod('closePath');

defineMethod('lineTo', 2, function(x, y) {
  this._gc.addDouble(x, y);
});

defineMethod('moveTo', 2, function(x, y) {
  this._gc.addDouble(x, y);
});

defineMethod('bezierCurveTo', 6, function(cp1x, cp1y, cp2x, cp2y, x, y) {
  this._gc.addDouble(cp1x, cp1y, cp2x, cp2y, x, y);
});<commit_msg>Skip fixed inital values in GC init call<commit_after>import {colorStringToArray, colorArrayToString} from './util-colors';
import ImageData from './ImageData';
import GC from './GC';

export default class CanvasContext {

  constructor(gc) {
    this._gc = gc;
    this._state = createState();
    this._savedStates = [];
    this.canvas = {
      width: 0,
      height: 0,
      style: {}
    };
    for (let name in properties) {
      defineProperty(this, name);
    }
  }

  measureText(text) {
    // TODO: delegate to native function, once it is implemented (#56)
    return {width: text.length * 5 + 5};
  }

  // ImageData operations

  getImageData(x, y, width, height) {
    checkRequiredArgs(arguments, 4, 'CanvasContext.getImageData');
    this._gc.flush();
    // TODO check validity of args
    let array = this._gc.getImageData(x, y, width, height);
    return new ImageData(array, width, height);
  }

  putImageData(imageData, x, y) {
    checkRequiredArgs(arguments, 3, 'CanvasContext.putImageData');
    this._gc.flush();
    this._gc.putImageData(imageData, x, y);
  }

  createImageData(width, height) {
    if (arguments[0] instanceof ImageData) {
      let data = arguments[0];
      width = data.width;
      height = data.height;
    } else {
      checkRequiredArgs(arguments, 2, 'CanvasContext.createImageData');
    }
    return new ImageData(width, height);
  }

  _init(width, height) {
    this.canvas.width = width;
    this.canvas.height = height;
    this._gc.init({width, height});
  }

}

// State operations

defineMethod('save', 0, function() {
  this._savedStates.push(Object.assign({}, this._state));
});

defineMethod('restore', 0, function() {
  this._state = this._savedStates.pop() || this._state;
});

// Path operations

defineMethod('beginPath');

defineMethod('closePath');

defineMethod('lineTo', 2, function(x, y) {
  this._gc.addDouble(x, y);
});

defineMethod('moveTo', 2, function(x, y) {
  this._gc.addDouble(x, y);
});

defineMethod('bezierCurveTo', 6, function(cp1x, cp1y, cp2x, cp2y, x, y) {
  this._gc.addDouble(cp1x, cp1y, cp2x, cp2y, x, y);
});
{'fraction_non_alphanumeric': 0.15373929150266266, 'fraction_numerical': 0.01435517480898356, 'mean_word_length': 3.3636363636363638, 'pattern_counts': {'":': 0, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1179427', 'n_tokens_mistral': 1490, 'n_tokens_neox': 1434, 'n_words': 343}