load static method

Future<UPdfFunction?> load(
  1. UPdfDocument document,
  2. Object? value, {
  3. int depth = 0,
})

Implementation

static Future<UPdfFunction?> load(UPdfDocument document, Object? value, {int depth = 0}) async {
  if (depth > 8) return null;
  final Object? resolved = await document.resolve(value);
  if (resolved is List<Object?>) {
    final List<UPdfFunction> parts = <UPdfFunction>[];
    for (final Object? entry in resolved) {
      final UPdfFunction? part = await load(document, entry, depth: depth + 1);
      if (part != null) parts.add(part);
    }
    return parts.isEmpty ? null : UPdfFunctionArray(parts);
  }
  UPdfDict? dict;
  Uint8List? data;
  if (resolved is UPdfStream) {
    dict = resolved.dict;
    data = (await document.decodeStream(resolved)).bytes;
  } else if (resolved is UPdfDict) {
    dict = resolved;
  }
  if (dict == null) return null;
  final int type = ((await document.resolve(dict["FunctionType"])) as num?)?.toInt() ?? -1;
  final List<double> domain = await _numbers(document, dict["Domain"]);
  final List<double>? outputRange = dict.has("Range") ? await _numbers(document, dict["Range"]) : null;
  switch (type) {
    case 0:
      if (data == null) return null;
      return UPdfSampledFunction(
        domain: domain,
        range: outputRange ?? <double>[0, 1],
        size: (await _numbers(document, dict["Size"])).map((double value) => value.toInt()).toList(),
        bitsPerSample: ((await document.resolve(dict["BitsPerSample"])) as num?)?.toInt() ?? 8,
        encode: dict.has("Encode") ? await _numbers(document, dict["Encode"]) : null,
        decode: dict.has("Decode") ? await _numbers(document, dict["Decode"]) : null,
        samples: data,
      );
    case 2:
      return UPdfExponentialFunction(
        domain: domain.isEmpty ? <double>[0, 1] : domain,
        range: outputRange,
        c0: dict.has("C0") ? await _numbers(document, dict["C0"]) : <double>[0],
        c1: dict.has("C1") ? await _numbers(document, dict["C1"]) : <double>[1],
        exponent: ((await document.resolve(dict["N"])) as num?)?.toDouble() ?? 1,
      );
    case 3:
      final Object? functionsObject = await document.resolve(dict["Functions"]);
      final List<UPdfFunction> functions = <UPdfFunction>[];
      if (functionsObject is List<Object?>) {
        for (final Object? entry in functionsObject) {
          final UPdfFunction? part = await load(document, entry, depth: depth + 1);
          if (part != null) functions.add(part);
        }
      }
      return UPdfStitchingFunction(
        domain: domain.isEmpty ? <double>[0, 1] : domain,
        range: outputRange,
        functions: functions,
        bounds: await _numbers(document, dict["Bounds"]),
        encode: await _numbers(document, dict["Encode"]),
      );
    case 4:
      if (data == null) return null;
      return UPdfPostScriptFunction(domain: domain, range: outputRange ?? <double>[0, 1], program: String.fromCharCodes(data));
    default:
      return null;
  }
}