bakeAutoOrient function

AutoOrientBakeResult bakeAutoOrient(
  1. Map<String, dynamic> doc
)

Walks doc and bakes auto-orient on every layer that has it, mutating doc in place. Meant to run after the expression bakes, so a position they baked is what the layer gets oriented along. Safe to call repeatedly: a baked layer no longer has ao on.

Implementation

AutoOrientBakeResult bakeAutoOrient(Map<String, dynamic> doc) {
  var baked = 0;
  final skipped = <String>[];

  void walkLayers(List<dynamic> layers) {
    for (final layer in layers) {
      if (layer is! Map<String, dynamic> || layer['ao'] != 1) continue;
      final ks = layer['ks'];
      // A layer with no transform block is sanitizeCrashingLayers' to report
      // (layersMissingTransform); there's nothing here to orient.
      if (ks is! Map<String, dynamic>) continue;
      final problem = _bakeLayer(layer, ks);
      if (problem == null) {
        baked++;
      } else {
        skipped.add('ty=${layer['ty']} nm=${layer['nm']}: $problem');
      }
    }
  }

  walkLayers((doc['layers'] as List?) ?? const []);
  for (final asset in (doc['assets'] as List? ?? const [])) {
    if (asset is Map && asset['layers'] is List) {
      walkLayers(asset['layers'] as List);
    }
  }

  return AutoOrientBakeResult(layersBaked: baked, skippedLayers: skipped);
}