swf2ass/src/ass/ASSLine.php

53 lines
1.6 KiB
PHP

<?php
namespace swf2ass\ass;
use swf2ass\FrameInformation;
use swf2ass\RenderedFrame;
use swf2ass\RenderedObject;
class ASSLine {
const HOURS_MS = 1000 * 3600;
const MINUTES_MS = 1000 * 60;
public int $layer;
public int $start;
public int $frames = 0;
public int $end;
public string $style;
public string $name = "";
public int $marginLeft = 0;
public int $marginRight = 0;
public int $marginVertical = 0;
public string $effect = "";
public bool $isComment = false;
public string $text;
public function __construct(string $text) {
$this->text = $text;
}
public static function encodeTime(int $ms, $msPrecision = 2): string {
if ($ms < 0) {
throw new \LogicException("ms less than 0: $ms");
}
$hours = intdiv($ms, self::HOURS_MS);
$ms -= $hours * self::HOURS_MS;
$minutes = intdiv($ms, self::MINUTES_MS);
$ms -= $minutes * self::MINUTES_MS;
$s = explode(".", strval(round($ms / 1000, $msPrecision)));
if (!isset($s[1])) {
$s[1] = 0;
}
return ((string)$hours) . ":" . str_pad((string)$minutes, 2, "0", STR_PAD_LEFT) . ':' . str_pad($s[0], 2, "0", STR_PAD_LEFT) . "." . str_pad($s[1], $msPrecision, "0", STR_PAD_RIGHT);
}
public function encode(): string {
return ($this->isComment ? "Comment" : "Dialogue") . ": " . $this->layer . "," . self::encodeTime($this->start) . "," . self::encodeTime($this->end) . "," . $this->style . "," . $this->name . "," . $this->marginLeft . "," . $this->marginRight . "," . $this->marginVertical . "," . $this->effect . "," . $this->text;
}
}