swf2ass/src/ass/containerTag.php

136 lines
4.2 KiB
PHP

<?php
namespace swf2ass\ass;
use swf2ass\ColorTransform;
use swf2ass\DrawPath;
use swf2ass\MatrixTransform;
use swf2ass\Shape;
use swf2ass\StyleRecord;
class containerTag implements ASSColorTag, ASSPositioningTag, ASSStyleTag {
/** @var ASSTag[] */
private array $tags = [];
public function transitionColor(ColorTransform $transform): ?containerTag {
$container = new containerTag();
foreach ($this->tags as $tag) {
if ($tag instanceof ASSColorTag) {
$newTag = $tag->transitionColor($transform);
if ($newTag === null) {
return null;
}
$container->tags[] = $newTag;
} else {
$container->tags[] = $tag;
}
}
return $container;
}
public function transitionMatrixTransform(MatrixTransform $transform): ?containerTag {
$container = new containerTag();
foreach ($this->tags as $tag) {
if ($tag instanceof ASSPositioningTag) {
$newTag = $tag->transitionMatrixTransform($transform);
if ($newTag === null) {
return null;
}
$container->tags[] = $newTag;
} else {
$container->tags[] = $tag;
}
}
return $container;
}
public function transitionStyleRecord(StyleRecord $record): ?containerTag {
$container = new containerTag();
foreach ($this->tags as $tag) {
if ($tag instanceof ASSStyleTag) {
$newTag = $tag->transitionStyleRecord($record);
if ($newTag === null) {
return null;
}
$container->tags[] = $newTag;
} else {
$container->tags[] = $tag;
}
}
return $container;
}
protected function try_append(?ASSTag $tag, $throw_on_null = false) {
if ($tag !== null) {
$this->tags[] = $tag;
return;
}
if ($throw_on_null) {
throw new \Exception();
}
}
public static function fromPathEntry(DrawPath $path, ?Shape $clip, ?ColorTransform $colorTransform, ?MatrixTransform $matrixTransform): containerTag {
$container = new containerTag();
$container->try_append(borderTag::fromStyleRecord($path->style));
$container->try_append(shadowTag::fromStyleRecord($path->style));
$container->try_append(lineColorTag::fromStyleRecord($path->style));
$container->try_append(fillColorTag::fromStyleRecord($path->style));
if ($matrixTransform !== null) {
$container->try_append(matrixTransformTag::fromMatrixTransform($matrixTransform));
}
if ($colorTransform !== null) {
$container = $container->transitionColor($colorTransform);
}
if ($clip !== null) {
//TODO: matrix transform?
//$container->try_append(new clipTag($clip));
}
$container->try_append(new drawTag($path->commands));
return $container;
}
public static function fromMatrixTransform(MatrixTransform $transform): ?ASSPositioningTag {
throw new \Exception();
}
public static function fromStyleRecord(StyleRecord $record): ?ASSStyleTag {
throw new \Exception();
}
public function equals(ASSTag $tag): bool {
if ($tag instanceof $this and count($this->tags) === count($tag->tags)) {
$tags = $this->tags;
$otherTags = $tag->tags;
foreach ($tags as $i => $t) {
foreach ($otherTags as $j => $t2) {
if ($t->equals($t2)) {
unset($tags[$i]);
unset($otherTags[$j]);
}
break;
}
if (isset($tags[$i])) {
break;
}
}
return count($tags) === 0 and count($otherTags) === 0;
}
return false;
}
public function encode(): string {
$ret = "";
foreach ($this->tags as $tag) {
$ret .= $tag->encode();
}
return $ret;
}
}