All files / core character.ts

100% Statements 26/26
100% Branches 4/4
100% Functions 8/8
100% Lines 26/26
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93  6x         6x 224x                         224x 224x 224x 223x   1x     223x 222x   1x           5624981x         4061295x         212x         345x               176573x   176571x 176571x 176575x     176571x               18x   16x 16x 176x     16x               181x    
import BitArray, { bit } from "../utils/bit-array";
import { Exception } from "../utils/exception";
 
/**
 * The character holds information about the visual representation of a character
 */
export default class Character {
  readonly CLASS_NAME = Character.name;
  private _pattern: string;
  private _output: BitArray;
  private _width: number;
  private _height: number; 
 
  /**
   * Creates a character
   * @param patterns The strings for which the dictionary will pick this character
   * @param output The bit representation of the character
   * @param width  The width of the character
   */
  constructor(pattern: string, output: BitArray, width: number) {
    this._pattern = pattern;
    this._output = output;
    if (output.size >= width) {
      this._width = width;
    } else {
      throw `Output size (${output.size}) can't be smaller than the character's width (${width})`;
    }
    
    if (output.size % width === 0) {
      this._height = output.size / width;
    } else {
      throw `Output size (${output.size}) must be divisible by the character's width (${width})`; 
    }
  }
 
  /** Gets the width of the character */
  public get width() {
    return this._width;
  }
 
  /** Gets the height of the character */
  public get height() {
    return this._height;
  }
 
  /** Gets the pattern of the character */
  public get pattern() {
    return this._pattern;
  }
 
  /** Gets the output of the character */
  public get output() {
    return this._output;
  }
  
  /**
   * Gets a column of the character at an index
   * @param index The index of the column
   */
  public getColumn(index: number): bit[] {
    Exception.throwIfNotBetween(index, Exception.getDescriptionForProperty(this.CLASS_NAME, 'getColumn'), 0, this._width - 1);
 
    let column:bit[] = [];
    for(let i = 0; i < this._height; i++) {
      column.push(this._output.atIndex(i * this._width + index));
    }
 
    return column;
  } 
 
  /**
   * Gets a row of the character at an index
   * @param index The index of the row
   */
  public getRow(index: number): bit[] {
    Exception.throwIfNotBetween(index, Exception.getDescriptionForProperty(this.CLASS_NAME, 'getRow'), 0, this._height - 1);
 
    let row: bit[] = [];
    for(let i = 0; i < this._width; i++) {
      row.push(this._output.atIndex(index * this._width + i));
    }
 
    return row;
  }
 
  /**
   * Matches the character against a string to determine whether the input produces this character
   * @param input The input string to match the character against
   */
  public hasPattern(input: string): boolean {
    return this._pattern == input;
  }
};