processSubtitleAttributes static method

(TextOriginAndExtent?, TextStyle?, AlignmentGeometry, Color, double, Paint) processSubtitleAttributes(
  1. List<SubtitleAttribute> subtitleAttributes
)

Process subtitle attributes and return the text alignment and text style.

Implementation

static (
  TextOriginAndExtent?,
  TextStyle?,
  AlignmentGeometry,
  Color,
  double,
  Paint
) processSubtitleAttributes(List<SubtitleAttribute> subtitleAttributes) {
  TextOriginAndExtent actualTextOriginAndExtent = TextOriginAndExtent.none;
  TextStyle actualTextStyle =
      const TextStyle(height: 1.0, fontSize: 1 / 15.0);
  AlignmentGeometry actualTextAlign = Alignment.center;
  Color actualWindowBgColor =
      const Color(0x00000000); // default transparent color.
  double actualFontSize = 1 / 15.0;
  Paint foreground = Paint()..style = PaintingStyle.stroke;

  if (subtitleAttributes.isEmpty) {
    return (
      actualTextOriginAndExtent,
      actualTextStyle,
      actualTextAlign,
      actualWindowBgColor,
      actualFontSize,
      foreground
    );
  }

  for (final SubtitleAttribute attr in subtitleAttributes) {
    switch (attr.attrType) {
      // For text origin and extent.
      case SubtitleAttrType.subAttrRegionXPos:
        final double xPos = attr.attrValue as double;
        if (xPos > 0) {
          actualTextOriginAndExtent =
              actualTextOriginAndExtent.addValue(originX: xPos);
        }
      case SubtitleAttrType.subAttrRegionYPos:
        final double yPos = attr.attrValue as double;
        if (yPos > 0) {
          actualTextOriginAndExtent =
              actualTextOriginAndExtent.addValue(originY: yPos);
        }
      case SubtitleAttrType.subAttrRegionWidth:
        final double width = attr.attrValue as double;
        if (width > 0) {
          actualTextOriginAndExtent =
              actualTextOriginAndExtent.addValue(extentWidth: width);
        }
      case SubtitleAttrType.subAttrRegionHeight:
        final double height = attr.attrValue as double;
        if (height > 0) {
          actualTextOriginAndExtent =
              actualTextOriginAndExtent.addValue(extentHeight: height);
        }
      // For text style.
      case SubtitleAttrType.subAttrFontFamily:
        actualTextStyle =
            actualTextStyle.copyWith(fontFamily: attr.attrValue as String);
      case SubtitleAttrType.subAttrFontSize:
        final double fontSize = attr.attrValue as double;
        if (fontSize > 0) {
          actualFontSize = fontSize;
        }
      case SubtitleAttrType.subAttrFontWeight:
        actualTextStyle = actualTextStyle.copyWith(
            fontWeight: _intToFontWeight(attr.attrValue as int));
      case SubtitleAttrType.subAttrFontStyle:
        actualTextStyle = actualTextStyle.copyWith(
            fontStyle: _intToFontStyle(attr.attrValue as int));
      case SubtitleAttrType.subAttrFontColor:
        if (actualTextStyle.foreground == null) {
          actualTextStyle = actualTextStyle.copyWith(
              color: _intToColor(attr.attrValue as int));
        }
      case SubtitleAttrType.subAttrFontBgColor:
        if (actualTextStyle.background == null) {
          actualTextStyle = actualTextStyle.copyWith(
              backgroundColor: _intToColor(attr.attrValue as int));
        }
      case SubtitleAttrType.subAttrFontOpacity:
        final double fontOpacity = attr.attrValue as double;
        if (fontOpacity >= 0.0 && fontOpacity <= 1.0) {
          if (actualTextStyle.color == null) {
            actualTextStyle = actualTextStyle.copyWith(color: Colors.white);
          }
          actualTextStyle = actualTextStyle.copyWith(
              color: actualTextStyle.color!.withValues(alpha: fontOpacity));
        }
      case SubtitleAttrType.subAttrFontBgOpacity:
        final double fontBgOpacity = attr.attrValue as double;
        if (fontBgOpacity >= 0.0 &&
            fontBgOpacity <= 1.0 &&
            actualTextStyle.backgroundColor != null) {
          actualTextStyle = actualTextStyle.copyWith(
              backgroundColor: actualTextStyle.backgroundColor!
                  .withValues(alpha: fontBgOpacity));
        }
      // For text vertical and horizontal align.
      case SubtitleAttrType.subAttrFontVerticalAlign:
        actualTextAlign = actualTextAlign
            .add(_intToTextVerticalAlign(attr.attrValue as int));
      case SubtitleAttrType.subAttrFontHorizontalAlign:
        actualTextAlign = actualTextAlign
            .add(_intToTextHorizontalAlign(attr.attrValue as int));
      // For text background window.
      case SubtitleAttrType.subAttrWindowBgColor:
        actualWindowBgColor = _intToColor(attr.attrValue as int);
      case SubtitleAttrType.subAttrWindowOpacity:
        final double windowOpacity = attr.attrValue as double;
        if (windowOpacity >= 0.0 && windowOpacity <= 1.0) {
          actualWindowBgColor =
              actualWindowBgColor.withValues(alpha: windowOpacity);
        }
      // For text outline.
      case SubtitleAttrType.subAttrFontTextOutlineColor:
        foreground = foreground..color = _intToColor(attr.attrValue as int);
      case SubtitleAttrType.subAttrFontTextOutlineThickness:
        foreground = foreground..strokeWidth = (attr.attrValue as int) / 1.0;
      case SubtitleAttrType.subAttrFontTextOutlineBlurRadius:
      case SubtitleAttrType.subAttrWindowXPadding:
      case SubtitleAttrType.subAttrWindowYPadding:
      case SubtitleAttrType.subAttrWindowLeftMargin:
      case SubtitleAttrType.subAttrWindowRightMargin:
      case SubtitleAttrType.subAttrWindowTopMargin:
      case SubtitleAttrType.subAttrWindowBottomMargin:
      case SubtitleAttrType.subAttrWindowShowBg:
      case SubtitleAttrType.subAttrRawSubtitle:
      case SubtitleAttrType.subAttrWebvttCueLine:
      case SubtitleAttrType.subAttrWebvttCueLineNum:
      case SubtitleAttrType.subAttrWebvttCueLineAlign:
      case SubtitleAttrType.subAttrWebvttCueAlign:
      case SubtitleAttrType.subAttrWebvttCueSize:
      case SubtitleAttrType.subAttrWebvttCuePosition:
      case SubtitleAttrType.subAttrWebvttCuePositionAlign:
      case SubtitleAttrType.subAttrWebvttCueVertical:
      case SubtitleAttrType.subAttrTimestamp:
      case SubtitleAttrType.subAttrExtsubIndex:
      case SubtitleAttrType.subAttrTypeNone:
        break;
    }
  }
  return (
    actualTextOriginAndExtent == TextOriginAndExtent.none
        ? null
        : actualTextOriginAndExtent,
    actualTextStyle == const TextStyle(height: 1.0, fontSize: 1 / 15.0)
        ? null
        : actualTextStyle,
    actualTextAlign,
    actualWindowBgColor,
    actualFontSize,
    foreground
  );
}