All files / src Symbol.ts

98.08% Statements 51/52
100% Branches 6/6
92.31% Functions 12/13
97.92% Lines 47/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118  5x                             622x 622x 622x 622x 622x 622x           412x       412x 412x 412x       412x 412x 412x 412x 412x 2118x 2118x 2118x   412x       412x 412x 203x       412x       412x       412x                   210x 210x 209x       5x                   412x 412x 412x 412x 412x 412x 412x       213x   210x 210x 210x 210x 412x 412x   210x       8x 8x      
import { getInstance } from './instance';
import { ZBarSymbolType } from './enum';
 
export interface Point {
  x: number;
  y: number;
}
 
class TypePointer {
  protected ptr: number;
  protected ptr32: number;
  protected buf: ArrayBuffer;
  protected HEAP8: Int8Array;
  protected HEAP32: Int32Array;
  protected HEAPU32: Uint32Array;
  constructor(ptr: number, buf: ArrayBuffer) {
    this.ptr = ptr;
    this.ptr32 = ptr >> 2;
    this.buf = buf;
    this.HEAP8 = new Int8Array(buf);
    this.HEAPU32 = new Uint32Array(buf);
    this.HEAP32 = new Int32Array(buf);
  }
}
 
class SymbolPtr extends TypePointer {
  get type(): ZBarSymbolType {
    return this.HEAPU32[this.ptr32] as ZBarSymbolType;
  }
 
  get data(): Int8Array {
    const len = this.HEAPU32[this.ptr32 + 2];
    const ptr = this.HEAPU32[this.ptr32 + 3];
    return Int8Array.from(this.HEAP8.subarray(ptr, ptr + len));
  }
 
  get points(): Array<Point> {
    const len = this.HEAPU32[this.ptr32 + 5];
    const ptr = this.HEAPU32[this.ptr32 + 6];
    const ptr32 = ptr >> 2;
    const res = [];
    for (let i = 0; i < len; ++i) {
      const x = this.HEAP32[ptr32 + i * 2];
      const y = this.HEAP32[ptr32 + i * 2 + 1];
      res.push({ x, y } as Point);
    }
    return res;
  }
 
  get next(): SymbolPtr | null {
    const ptr = this.HEAPU32[this.ptr32 + 8];
    if (!ptr) return null;
    return new SymbolPtr(ptr, this.buf);
  }
 
  get time(): number {
    return this.HEAPU32[this.ptr32 + 10];
  }
 
  get cacheCount(): number {
    return this.HEAP32[this.ptr32 + 11];
  }
 
  get quality(): number {
    return this.HEAP32[this.ptr32 + 12];
  }
}
 
class SymbolSetPtr extends TypePointer {
  get nsyms(): number {
    return this.HEAP32[this.ptr32 + 1];
  }
 
  get head(): SymbolPtr | null {
    const ptr = this.HEAPU32[this.ptr32 + 2];
    if (!ptr) return null;
    return new SymbolPtr(ptr, this.buf);
  }
}
 
export class Symbol {
  type: ZBarSymbolType;
  typeName: string;
  data: Int8Array;
  points: Array<Point>;
  time: number;
  cacheCount: number;
  quality: number;
 
  private constructor(ptr: SymbolPtr) {
    this.type = ptr.type;
    this.typeName = ZBarSymbolType[this.type];
    this.data = ptr.data;
    this.points = ptr.points;
    this.time = ptr.time;
    this.cacheCount = ptr.cacheCount;
    this.quality = ptr.quality;
  }
 
  static createSymbolsFromPtr(ptr: number, buf: ArrayBuffer): Array<Symbol> {
    if (ptr == 0) return [];
 
    const set = new SymbolSetPtr(ptr, buf);
    let symbol = set.head;
    const res = [];
    while (symbol !== null) {
      res.push(new Symbol(symbol));
      symbol = symbol.next;
    }
    return res;
  }
 
  decode(encoding?: string) {
    const decoder = new TextDecoder(encoding);
    return decoder.decode(this.data);
  }
}