parseToUnicode static method

PdfToUnicodeCMap parseToUnicode(
  1. Uint8List bytes
)

Reads an already decompressed /ToUnicode CMap.

Handles begincodespacerange, beginbfchar and beginbfrange — the form with an array of destinations included. begincidrange is ignored: a CID CMap says nothing about Unicode.

Implementation

static PdfToUnicodeCMap parseToUnicode(Uint8List bytes) {
  final map = <int, String>{};
  var codeLength = 0;

  final lexer = PdfContentLexer(bytes);
  while (true) {
    final token = lexer.next();
    if (token.kind == PdfContentTokenKind.endOfData) break;
    if (token.kind != PdfContentTokenKind.keyword) continue;

    switch (token.text) {
      case 'begincodespacerange':
        final length = _readCodespace(lexer);
        if (length > codeLength) codeLength = length;
        break;
      case 'beginbfchar':
        _readBfChar(lexer, map);
        break;
      case 'beginbfrange':
        _readBfRange(lexer, map);
        break;
    }
  }

  return PdfToUnicodeCMap(map, codeLength);
}