nextTextElement static method

TextElement? nextTextElement(
  1. List<TextElement> startElements,
  2. List<TextBlock> blocks,
  3. HorizontalDirection direction
)

Return the next BrsTextElement to the left or right of "startElement" Ex :

List<BrsTextBlock> blocks =  ['How', 'are', 'you', '?'];
HorizontalDirection direction = HorizontalDirection.right;
BrsTextElement startElement = 'are';
Result : BrsTextElement = ['you']

Implementation

static TextElement? nextTextElement(
  List<TextElement> startElements,
  List<TextBlock> blocks,
  HorizontalDirection direction,
) {
  double angle;

  Trapezoid? primaryTrapezoid = _findPrimaryBlock(blocks)?.trapezoid;
  if (primaryTrapezoid == null) {
    return null;
  }

  /// TODO: If the biggest block is not in the same angle as startElement, it doesn't work.
  /// TODO: This case should not happen
  angle = MathHelper.retrieveAngle(
    primaryTrapezoid.topLeftOffset,
    primaryTrapezoid.topRightOffset,
  );

  Offset startPoint = Offset(
    startElements.last.trapezoid.topLeftOffset.dx,
    startElements.last.trapezoid.topLeftOffset.dy +
        (startElements.last.trapezoid.bottomLeftOffset.dy -
                startElements.last.trapezoid.topLeftOffset.dy) /
            2,
  );

  // 10000 is an arbitrary number, we just want to make a big line
  Offset endPoint = Offset(
    startPoint.dx +
        (direction == HorizontalDirection.left ? -10000 : 10000) * cos(angle),
    startPoint.dy +
        (direction == HorizontalDirection.left ? -10000 : 10000) * sin(angle),
  );

  List<TextElement> sortedElement =
      _sortTextElement(List.from(blocks), direction);
  for (TextElement element in sortedElement) {
    bool duplicated = false;
    for (TextElement startElement in startElements) {
      if (startElement == element) {
        duplicated = true;
        continue;
      }
    }

    if (duplicated) {
      continue;
    }

    if (MathHelper.doSegmentsIntersect(
      startPoint,
      endPoint,
      element.trapezoid.topLeftOffset,
      element.trapezoid.bottomLeftOffset,
    )) {
      return element;
    }
  }
  return null;
}