buildFont static method

PdfContentFont buildFont(
  1. PdfDocumentParser parser,
  2. PdfDictToken fontDict, {
  3. String? resourceName,
})

Builds a PdfContentFont from the font dictionary fontDict.

Implementation

static PdfContentFont buildFont(
  PdfDocumentParser parser,
  PdfDictToken fontDict, {
  String? resourceName,
}) {
  final subtype = PdfParserObjects.asName(fontDict.values['/Subtype']);
  final baseFont = PdfParserObjects.asName(fontDict.values['/BaseFont']);
  final composite = subtype == '/Type0';

  final toUnicode = _readToUnicode(parser, fontDict);
  var codeByteLength = composite ? 2 : 1;
  if (composite) {
    final encoding = PdfParserObjects.asName(fontDict.values['/Encoding']);
    if (encoding != null &&
        (encoding == '/Identity-H' || encoding == '/Identity-V')) {
      codeByteLength = 2;
    } else if (toUnicode != null && toUnicode.codeByteLength > 0) {
      codeByteLength = toUnicode.codeByteLength;
    }
  } else if (toUnicode != null && toUnicode.codeByteLength == 2) {
    // A simple font with a two-byte CMap is anomalous, but it exists.
    codeByteLength = 2;
  }

  if (composite) {
    final descendant = _descendantFont(parser, fontDict);
    final descriptor = descendant == null
        ? null
        : parser.resolve(descendant.values['/FontDescriptor']);
    return PdfContentFont(
      resourceName: resourceName,
      baseFont: baseFont,
      subtype: subtype,
      composite: true,
      codeByteLength: codeByteLength,
      toUnicode: toUnicode?.map,
      defaultWidth: descendant == null
          ? 1000
          : _numberOf(parser.resolve(descendant.values['/DW'])) ?? 1000,
      cidWidths: descendant == null
          ? null
          : _readCidWidths(parser, descendant.values['/W']),
      missingWidth: descriptor is PdfDictToken
          ? _numberOf(parser.resolve(descriptor.values['/MissingWidth']))
          : null,
    );
  }

  final widths = <double>[];
  final widthsValue = parser.resolve(fontDict.values['/Widths']);
  if (widthsValue is PdfArrayToken) {
    for (final item in widthsValue.values) {
      widths.add(_numberOf(parser.resolve(item)) ?? 0);
    }
  }

  final descriptor = parser.resolve(fontDict.values['/FontDescriptor']);
  final encodingInfo = _readEncoding(parser, fontDict.values['/Encoding']);

  return PdfContentFont(
    resourceName: resourceName,
    baseFont: baseFont,
    subtype: subtype,
    codeByteLength: codeByteLength,
    toUnicode: toUnicode?.map,
    differences: encodingInfo.differences,
    winAnsi: encodingInfo.winAnsi,
    firstChar: PdfParserObjects.asInt(
            parser.resolve(fontDict.values['/FirstChar'])) ??
        0,
    widths: widths,
    missingWidth: descriptor is PdfDictToken
        ? _numberOf(parser.resolve(descriptor.values['/MissingWidth']))
        : null,
  );
}