Java Code Examples for com.google.android.exoplayer2.text.Cue#ANCHOR_TYPE_MIDDLE

The following examples show how to use com.google.android.exoplayer2.text.Cue#ANCHOR_TYPE_MIDDLE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebvttCue.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
private static float deriveMaxSize(@AnchorType int positionAnchor, float position) {
  switch (positionAnchor) {
    case Cue.ANCHOR_TYPE_START:
      return 1.0f - position;
    case Cue.ANCHOR_TYPE_END:
      return position;
    case Cue.ANCHOR_TYPE_MIDDLE:
      if (position <= 0.5f) {
        return position * 2;
      } else {
        return (1.0f - position) * 2;
      }
    case Cue.TYPE_UNSET:
    default:
      throw new IllegalStateException(String.valueOf(positionAnchor));
  }
}
 
Example 2
Source File: WebvttCue.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
private Builder derivePositionAnchorFromAlignment() {
  if (textAlignment == null) {
    positionAnchor = Cue.TYPE_UNSET;
  } else {
    switch (textAlignment) {
      case ALIGN_NORMAL:
        positionAnchor = Cue.ANCHOR_TYPE_START;
        break;
      case ALIGN_CENTER:
        positionAnchor = Cue.ANCHOR_TYPE_MIDDLE;
        break;
      case ALIGN_OPPOSITE:
        positionAnchor = Cue.ANCHOR_TYPE_END;
        break;
      default:
        Log.w(TAG, "Unrecognized alignment: " + textAlignment);
        positionAnchor = Cue.ANCHOR_TYPE_START;
        break;
    }
  }
  return this;
}
 
Example 3
Source File: WebvttCue.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
private Builder derivePositionAnchorFromAlignment() {
  if (textAlignment == null) {
    positionAnchor = Cue.TYPE_UNSET;
  } else {
    switch (textAlignment) {
      case ALIGN_NORMAL:
        positionAnchor = Cue.ANCHOR_TYPE_START;
        break;
      case ALIGN_CENTER:
        positionAnchor = Cue.ANCHOR_TYPE_MIDDLE;
        break;
      case ALIGN_OPPOSITE:
        positionAnchor = Cue.ANCHOR_TYPE_END;
        break;
      default:
        Log.w(TAG, "Unrecognized alignment: " + textAlignment);
        positionAnchor = Cue.ANCHOR_TYPE_START;
        break;
    }
  }
  return this;
}
 
Example 4
Source File: WebvttCue.java    From K-Sonic with MIT License 6 votes vote down vote up
private Builder derivePositionAnchorFromAlignment() {
  if (textAlignment == null) {
    positionAnchor = Cue.TYPE_UNSET;
  } else {
    switch (textAlignment) {
      case ALIGN_NORMAL:
        positionAnchor = Cue.ANCHOR_TYPE_START;
        break;
      case ALIGN_CENTER:
        positionAnchor = Cue.ANCHOR_TYPE_MIDDLE;
        break;
      case ALIGN_OPPOSITE:
        positionAnchor = Cue.ANCHOR_TYPE_END;
        break;
      default:
        Log.w(TAG, "Unrecognized alignment: " + textAlignment);
        positionAnchor = Cue.ANCHOR_TYPE_START;
        break;
    }
  }
  return this;
}
 
Example 5
Source File: WebvttCue.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
private Builder derivePositionAnchorFromAlignment() {
  if (textAlignment == null) {
    positionAnchor = Cue.TYPE_UNSET;
  } else {
    switch (textAlignment) {
      case ALIGN_NORMAL:
        positionAnchor = Cue.ANCHOR_TYPE_START;
        break;
      case ALIGN_CENTER:
        positionAnchor = Cue.ANCHOR_TYPE_MIDDLE;
        break;
      case ALIGN_OPPOSITE:
        positionAnchor = Cue.ANCHOR_TYPE_END;
        break;
      default:
        Log.w(TAG, "Unrecognized alignment: " + textAlignment);
        positionAnchor = Cue.ANCHOR_TYPE_START;
        break;
    }
  }
  return this;
}
 
Example 6
Source File: WebvttCueParser.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
private static int parsePositionAnchor(String s) {
  switch (s) {
    case "start":
      return Cue.ANCHOR_TYPE_START;
    case "center":
    case "middle":
      return Cue.ANCHOR_TYPE_MIDDLE;
    case "end":
      return Cue.ANCHOR_TYPE_END;
    default:
      Log.w(TAG, "Invalid anchor value: " + s);
      return Cue.TYPE_UNSET;
  }
}
 
Example 7
Source File: WebvttCueParser.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
@Cue.AnchorType
private static int parsePositionAnchor(String s) {
  switch (s) {
    case "start":
      return Cue.ANCHOR_TYPE_START;
    case "center":
    case "middle":
      return Cue.ANCHOR_TYPE_MIDDLE;
    case "end":
      return Cue.ANCHOR_TYPE_END;
    default:
      Log.w(TAG, "Invalid anchor value: " + s);
      return Cue.TYPE_UNSET;
  }
}
 
Example 8
Source File: WebvttCueParser.java    From no-player with Apache License 2.0 5 votes vote down vote up
private static int parsePositionAnchor(String s) {
    switch (s) {
        case "start":
            return Cue.ANCHOR_TYPE_START;
        case "center":
        case "middle":
            return Cue.ANCHOR_TYPE_MIDDLE;
        case "end":
            return Cue.ANCHOR_TYPE_END;
        default:
            Log.w(TAG, "Invalid anchor value: " + s);
            return Cue.TYPE_UNSET;
    }
}
 
Example 9
Source File: SsaDecoder.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
private static float computeDefaultLineOrPosition(@Cue.AnchorType int anchor) {
  switch (anchor) {
    case Cue.ANCHOR_TYPE_START:
      return DEFAULT_MARGIN;
    case Cue.ANCHOR_TYPE_MIDDLE:
      return 0.5f;
    case Cue.ANCHOR_TYPE_END:
      return 1.0f - DEFAULT_MARGIN;
    case Cue.TYPE_UNSET:
    default:
      return Cue.DIMEN_UNSET;
  }
}
 
Example 10
Source File: SubripDecoder.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
static float getFractionalPositionForAnchorType(@Cue.AnchorType int anchorType) {
  switch (anchorType) {
    case Cue.ANCHOR_TYPE_START:
      return SubripDecoder.START_FRACTION;
    case Cue.ANCHOR_TYPE_MIDDLE:
      return SubripDecoder.MID_FRACTION;
    case Cue.ANCHOR_TYPE_END:
      return SubripDecoder.END_FRACTION;
    case Cue.TYPE_UNSET:
    default:
      // Should never happen.
      throw new IllegalArgumentException();
  }
}
 
Example 11
Source File: WebvttCueParser.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private static int parsePositionAnchor(String s) {
  switch (s) {
    case "start":
      return Cue.ANCHOR_TYPE_START;
    case "center":
    case "middle":
      return Cue.ANCHOR_TYPE_MIDDLE;
    case "end":
      return Cue.ANCHOR_TYPE_END;
    default:
      Log.w(TAG, "Invalid anchor value: " + s);
      return Cue.TYPE_UNSET;
  }
}
 
Example 12
Source File: Cea708Decoder.java    From K-Sonic with MIT License 4 votes vote down vote up
public Cea708Cue build() {
  if (isEmpty()) {
    // The cue is empty.
    return null;
  }

  SpannableStringBuilder cueString = new SpannableStringBuilder();

  // Add any rolled up captions, separated by new lines.
  for (int i = 0; i < rolledUpCaptions.size(); i++) {
    cueString.append(rolledUpCaptions.get(i));
    cueString.append('\n');
  }
  // Add the current line.
  cueString.append(buildSpannableString());

  // TODO: Add support for right-to-left languages (i.e. where right would correspond to normal
  // alignment).
  Alignment alignment;
  switch (justification) {
    case JUSTIFICATION_FULL:
      // TODO: Add support for full justification.
    case JUSTIFICATION_LEFT:
      alignment = Alignment.ALIGN_NORMAL;
      break;
    case JUSTIFICATION_RIGHT:
      alignment = Alignment.ALIGN_OPPOSITE;
      break;
    case JUSTIFICATION_CENTER:
      alignment = Alignment.ALIGN_CENTER;
      break;
    default:
      throw new IllegalArgumentException("Unexpected justification value: " + justification);
  }

  float position;
  float line;
  if (relativePositioning) {
    position = (float) horizontalAnchor / RELATIVE_CUE_SIZE;
    line = (float) verticalAnchor / RELATIVE_CUE_SIZE;
  } else {
    position = (float) horizontalAnchor / HORIZONTAL_SIZE;
    line = (float) verticalAnchor / VERTICAL_SIZE;
  }
  // Apply screen-edge padding to the line and position.
  position = (position * 0.9f) + 0.05f;
  line = (line * 0.9f) + 0.05f;

  // anchorId specifies where the anchor should be placed on the caption cue/window. The 9
  // possible configurations are as follows:
  //   0-----1-----2
  //   |           |
  //   3     4     5
  //   |           |
  //   6-----7-----8
  @AnchorType int verticalAnchorType;
  if (anchorId % 3 == 0) {
    verticalAnchorType = Cue.ANCHOR_TYPE_START;
  } else if (anchorId % 3 == 1) {
    verticalAnchorType = Cue.ANCHOR_TYPE_MIDDLE;
  } else {
    verticalAnchorType = Cue.ANCHOR_TYPE_END;
  }
  // TODO: Add support for right-to-left languages (i.e. where start is on the right).
  @AnchorType int horizontalAnchorType;
  if (anchorId / 3 == 0) {
    horizontalAnchorType = Cue.ANCHOR_TYPE_START;
  } else if (anchorId / 3 == 1) {
    horizontalAnchorType = Cue.ANCHOR_TYPE_MIDDLE;
  } else {
    horizontalAnchorType = Cue.ANCHOR_TYPE_END;
  }

  boolean windowColorSet = (windowFillColor != COLOR_SOLID_BLACK);

  return new Cea708Cue(cueString, alignment, line, Cue.LINE_TYPE_FRACTION, verticalAnchorType,
      position, horizontalAnchorType, Cue.DIMEN_UNSET, windowColorSet, windowFillColor,
      priority);
}
 
Example 13
Source File: Cea608Decoder.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
public Cue build() {
  SpannableStringBuilder cueString = new SpannableStringBuilder();
  // Add any rolled up captions, separated by new lines.
  for (int i = 0; i < rolledUpCaptions.size(); i++) {
    cueString.append(rolledUpCaptions.get(i));
    cueString.append('\n');
  }
  // Add the current line.
  cueString.append(buildSpannableString());

  if (cueString.length() == 0) {
    // The cue is empty.
    return null;
  }

  float position;
  int positionAnchor;
  // The number of empty columns before the start of the text, in the range [0-31].
  int startPadding = indent + tabOffset;
  // The number of empty columns after the end of the text, in the same range.
  int endPadding = SCREEN_CHARWIDTH - startPadding - cueString.length();
  int startEndPaddingDelta = startPadding - endPadding;
  if (captionMode == CC_MODE_POP_ON && (Math.abs(startEndPaddingDelta) < 3 || endPadding < 0)) {
    // Treat approximately centered pop-on captions as middle aligned. We also treat captions
    // that are wider than they should be in this way. See
    // https://github.com/google/ExoPlayer/issues/3534.
    position = 0.5f;
    positionAnchor = Cue.ANCHOR_TYPE_MIDDLE;
  } else if (captionMode == CC_MODE_POP_ON && startEndPaddingDelta > 0) {
    // Treat pop-on captions with less padding at the end than the start as end aligned.
    position = (float) (SCREEN_CHARWIDTH - endPadding) / SCREEN_CHARWIDTH;
    // Adjust the position to fit within the safe area.
    position = position * 0.8f + 0.1f;
    positionAnchor = Cue.ANCHOR_TYPE_END;
  } else {
    // For all other cases assume start aligned.
    position = (float) startPadding / SCREEN_CHARWIDTH;
    // Adjust the position to fit within the safe area.
    position = position * 0.8f + 0.1f;
    positionAnchor = Cue.ANCHOR_TYPE_START;
  }

  int lineAnchor;
  int line;
  // Note: Row indices are in the range [1-15].
  if (captionMode == CC_MODE_ROLL_UP || row > (BASE_ROW / 2)) {
    lineAnchor = Cue.ANCHOR_TYPE_END;
    line = row - BASE_ROW;
    // Two line adjustments. The first is because line indices from the bottom of the window
    // start from -1 rather than 0. The second is a blank row to act as the safe area.
    line -= 2;
  } else {
    lineAnchor = Cue.ANCHOR_TYPE_START;
    // Line indices from the top of the window start from 0, but we want a blank row to act as
    // the safe area. As a result no adjustment is necessary.
    line = row;
  }

  return new Cue(cueString, Alignment.ALIGN_NORMAL, line, Cue.LINE_TYPE_NUMBER, lineAnchor,
      position, positionAnchor, Cue.DIMEN_UNSET);
}
 
Example 14
Source File: Cea608Decoder.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
public Cue build(@Cue.AnchorType int forcedPositionAnchor) {
  SpannableStringBuilder cueString = new SpannableStringBuilder();
  // Add any rolled up captions, separated by new lines.
  for (int i = 0; i < rolledUpCaptions.size(); i++) {
    cueString.append(rolledUpCaptions.get(i));
    cueString.append('\n');
  }
  // Add the current line.
  cueString.append(buildCurrentLine());

  if (cueString.length() == 0) {
    // The cue is empty.
    return null;
  }

  int positionAnchor;
  // The number of empty columns before the start of the text, in the range [0-31].
  int startPadding = indent + tabOffset;
  // The number of empty columns after the end of the text, in the same range.
  int endPadding = SCREEN_CHARWIDTH - startPadding - cueString.length();
  int startEndPaddingDelta = startPadding - endPadding;
  if (forcedPositionAnchor != Cue.TYPE_UNSET) {
    positionAnchor = forcedPositionAnchor;
  } else if (captionMode == CC_MODE_POP_ON
      && (Math.abs(startEndPaddingDelta) < 3 || endPadding < 0)) {
    // Treat approximately centered pop-on captions as middle aligned. We also treat captions
    // that are wider than they should be in this way. See
    // https://github.com/google/ExoPlayer/issues/3534.
    positionAnchor = Cue.ANCHOR_TYPE_MIDDLE;
  } else if (captionMode == CC_MODE_POP_ON && startEndPaddingDelta > 0) {
    // Treat pop-on captions with less padding at the end than the start as end aligned.
    positionAnchor = Cue.ANCHOR_TYPE_END;
  } else {
    // For all other cases assume start aligned.
    positionAnchor = Cue.ANCHOR_TYPE_START;
  }

  float position;
  switch (positionAnchor) {
    case Cue.ANCHOR_TYPE_MIDDLE:
      position = 0.5f;
      break;
    case Cue.ANCHOR_TYPE_END:
      position = (float) (SCREEN_CHARWIDTH - endPadding) / SCREEN_CHARWIDTH;
      // Adjust the position to fit within the safe area.
      position = position * 0.8f + 0.1f;
      break;
    case Cue.ANCHOR_TYPE_START:
    default:
      position = (float) startPadding / SCREEN_CHARWIDTH;
      // Adjust the position to fit within the safe area.
      position = position * 0.8f + 0.1f;
      break;
  }

  int lineAnchor;
  int line;
  // Note: Row indices are in the range [1-15].
  if (captionMode == CC_MODE_ROLL_UP || row > (BASE_ROW / 2)) {
    lineAnchor = Cue.ANCHOR_TYPE_END;
    line = row - BASE_ROW;
    // Two line adjustments. The first is because line indices from the bottom of the window
    // start from -1 rather than 0. The second is a blank row to act as the safe area.
    line -= 2;
  } else {
    lineAnchor = Cue.ANCHOR_TYPE_START;
    // Line indices from the top of the window start from 0, but we want a blank row to act as
    // the safe area. As a result no adjustment is necessary.
    line = row;
  }

  return new Cue(
      cueString,
      Alignment.ALIGN_NORMAL,
      line,
      Cue.LINE_TYPE_NUMBER,
      lineAnchor,
      position,
      positionAnchor,
      Cue.DIMEN_UNSET);
}
 
Example 15
Source File: Cea608Decoder.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
public Cue build() {
  SpannableStringBuilder cueString = new SpannableStringBuilder();
  // Add any rolled up captions, separated by new lines.
  for (int i = 0; i < rolledUpCaptions.size(); i++) {
    cueString.append(rolledUpCaptions.get(i));
    cueString.append('\n');
  }
  // Add the current line.
  cueString.append(buildSpannableString());

  if (cueString.length() == 0) {
    // The cue is empty.
    return null;
  }

  float position;
  int positionAnchor;
  // The number of empty columns before the start of the text, in the range [0-31].
  int startPadding = indent + tabOffset;
  // The number of empty columns after the end of the text, in the same range.
  int endPadding = SCREEN_CHARWIDTH - startPadding - cueString.length();
  int startEndPaddingDelta = startPadding - endPadding;
  if (captionMode == CC_MODE_POP_ON && (Math.abs(startEndPaddingDelta) < 3 || endPadding < 0)) {
    // Treat approximately centered pop-on captions as middle aligned. We also treat captions
    // that are wider than they should be in this way. See
    // https://github.com/google/ExoPlayer/issues/3534.
    position = 0.5f;
    positionAnchor = Cue.ANCHOR_TYPE_MIDDLE;
  } else if (captionMode == CC_MODE_POP_ON && startEndPaddingDelta > 0) {
    // Treat pop-on captions with less padding at the end than the start as end aligned.
    position = (float) (SCREEN_CHARWIDTH - endPadding) / SCREEN_CHARWIDTH;
    // Adjust the position to fit within the safe area.
    position = position * 0.8f + 0.1f;
    positionAnchor = Cue.ANCHOR_TYPE_END;
  } else {
    // For all other cases assume start aligned.
    position = (float) startPadding / SCREEN_CHARWIDTH;
    // Adjust the position to fit within the safe area.
    position = position * 0.8f + 0.1f;
    positionAnchor = Cue.ANCHOR_TYPE_START;
  }

  int lineAnchor;
  int line;
  // Note: Row indices are in the range [1-15].
  if (captionMode == CC_MODE_ROLL_UP || row > (BASE_ROW / 2)) {
    lineAnchor = Cue.ANCHOR_TYPE_END;
    line = row - BASE_ROW;
    // Two line adjustments. The first is because line indices from the bottom of the window
    // start from -1 rather than 0. The second is a blank row to act as the safe area.
    line -= 2;
  } else {
    lineAnchor = Cue.ANCHOR_TYPE_START;
    // Line indices from the top of the window start from 0, but we want a blank row to act as
    // the safe area. As a result no adjustment is necessary.
    line = row;
  }

  return new Cue(cueString, Alignment.ALIGN_NORMAL, line, Cue.LINE_TYPE_NUMBER, lineAnchor,
      position, positionAnchor, Cue.DIMEN_UNSET);
}
 
Example 16
Source File: Cea608Decoder.java    From K-Sonic with MIT License 4 votes vote down vote up
public Cue build() {
  SpannableStringBuilder cueString = new SpannableStringBuilder();
  // Add any rolled up captions, separated by new lines.
  for (int i = 0; i < rolledUpCaptions.size(); i++) {
    cueString.append(rolledUpCaptions.get(i));
    cueString.append('\n');
  }
  // Add the current line.
  cueString.append(buildSpannableString());

  if (cueString.length() == 0) {
    // The cue is empty.
    return null;
  }

  float position;
  int positionAnchor;
  // The number of empty columns before the start of the text, in the range [0-31].
  int startPadding = indent + tabOffset;
  // The number of empty columns after the end of the text, in the same range.
  int endPadding = SCREEN_CHARWIDTH - startPadding - cueString.length();
  int startEndPaddingDelta = startPadding - endPadding;
  if (captionMode == CC_MODE_POP_ON && Math.abs(startEndPaddingDelta) < 3) {
    // Treat approximately centered pop-on captions are middle aligned.
    position = 0.5f;
    positionAnchor = Cue.ANCHOR_TYPE_MIDDLE;
  } else if (captionMode == CC_MODE_POP_ON && startEndPaddingDelta > 0) {
    // Treat pop-on captions with less padding at the end than the start as end aligned.
    position = (float) (SCREEN_CHARWIDTH - endPadding) / SCREEN_CHARWIDTH;
    // Adjust the position to fit within the safe area.
    position = position * 0.8f + 0.1f;
    positionAnchor = Cue.ANCHOR_TYPE_END;
  } else {
    // For all other cases assume start aligned.
    position = (float) startPadding / SCREEN_CHARWIDTH;
    // Adjust the position to fit within the safe area.
    position = position * 0.8f + 0.1f;
    positionAnchor = Cue.ANCHOR_TYPE_START;
  }

  int lineAnchor;
  int line;
  // Note: Row indices are in the range [1-15].
  if (captionMode == CC_MODE_ROLL_UP || row > (BASE_ROW / 2)) {
    lineAnchor = Cue.ANCHOR_TYPE_END;
    line = row - BASE_ROW;
    // Two line adjustments. The first is because line indices from the bottom of the window
    // start from -1 rather than 0. The second is a blank row to act as the safe area.
    line -= 2;
  } else {
    lineAnchor = Cue.ANCHOR_TYPE_START;
    // Line indices from the top of the window start from 0, but we want a blank row to act as
    // the safe area. As a result no adjustment is necessary.
    line = row;
  }

  return new Cue(cueString, Alignment.ALIGN_NORMAL, line, Cue.LINE_TYPE_NUMBER, lineAnchor,
      position, positionAnchor, Cue.DIMEN_UNSET);
}
 
Example 17
Source File: SubripDecoder.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Build a {@link Cue} based on the given text and alignment tag.
 *
 * @param text The text.
 * @param alignmentTag The alignment tag, or {@code null} if no alignment tag is available.
 * @return Built cue
 */
private Cue buildCue(Spanned text, @Nullable String alignmentTag) {
  if (alignmentTag == null) {
    return new Cue(text);
  }

  // Horizontal alignment.
  @Cue.AnchorType int positionAnchor;
  switch (alignmentTag) {
    case ALIGN_BOTTOM_LEFT:
    case ALIGN_MID_LEFT:
    case ALIGN_TOP_LEFT:
      positionAnchor = Cue.ANCHOR_TYPE_START;
      break;
    case ALIGN_BOTTOM_RIGHT:
    case ALIGN_MID_RIGHT:
    case ALIGN_TOP_RIGHT:
      positionAnchor = Cue.ANCHOR_TYPE_END;
      break;
    case ALIGN_BOTTOM_MID:
    case ALIGN_MID_MID:
    case ALIGN_TOP_MID:
    default:
      positionAnchor = Cue.ANCHOR_TYPE_MIDDLE;
      break;
  }

  // Vertical alignment.
  @Cue.AnchorType int lineAnchor;
  switch (alignmentTag) {
    case ALIGN_BOTTOM_LEFT:
    case ALIGN_BOTTOM_MID:
    case ALIGN_BOTTOM_RIGHT:
      lineAnchor = Cue.ANCHOR_TYPE_END;
      break;
    case ALIGN_TOP_LEFT:
    case ALIGN_TOP_MID:
    case ALIGN_TOP_RIGHT:
      lineAnchor = Cue.ANCHOR_TYPE_START;
      break;
    case ALIGN_MID_LEFT:
    case ALIGN_MID_MID:
    case ALIGN_MID_RIGHT:
    default:
      lineAnchor = Cue.ANCHOR_TYPE_MIDDLE;
      break;
  }

  return new Cue(
      text,
      /* textAlignment= */ null,
      getFractionalPositionForAnchorType(lineAnchor),
      Cue.LINE_TYPE_FRACTION,
      lineAnchor,
      getFractionalPositionForAnchorType(positionAnchor),
      positionAnchor,
      Cue.DIMEN_UNSET);
}
 
Example 18
Source File: Cea708Decoder.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
public Cea708Cue build() {
  if (isEmpty()) {
    // The cue is empty.
    return null;
  }

  SpannableStringBuilder cueString = new SpannableStringBuilder();

  // Add any rolled up captions, separated by new lines.
  for (int i = 0; i < rolledUpCaptions.size(); i++) {
    cueString.append(rolledUpCaptions.get(i));
    cueString.append('\n');
  }
  // Add the current line.
  cueString.append(buildSpannableString());

  // TODO: Add support for right-to-left languages (i.e. where right would correspond to normal
  // alignment).
  Alignment alignment;
  switch (justification) {
    case JUSTIFICATION_FULL:
      // TODO: Add support for full justification.
    case JUSTIFICATION_LEFT:
      alignment = Alignment.ALIGN_NORMAL;
      break;
    case JUSTIFICATION_RIGHT:
      alignment = Alignment.ALIGN_OPPOSITE;
      break;
    case JUSTIFICATION_CENTER:
      alignment = Alignment.ALIGN_CENTER;
      break;
    default:
      throw new IllegalArgumentException("Unexpected justification value: " + justification);
  }

  float position;
  float line;
  if (relativePositioning) {
    position = (float) horizontalAnchor / RELATIVE_CUE_SIZE;
    line = (float) verticalAnchor / RELATIVE_CUE_SIZE;
  } else {
    position = (float) horizontalAnchor / HORIZONTAL_SIZE;
    line = (float) verticalAnchor / VERTICAL_SIZE;
  }
  // Apply screen-edge padding to the line and position.
  position = (position * 0.9f) + 0.05f;
  line = (line * 0.9f) + 0.05f;

  // anchorId specifies where the anchor should be placed on the caption cue/window. The 9
  // possible configurations are as follows:
  //   0-----1-----2
  //   |           |
  //   3     4     5
  //   |           |
  //   6-----7-----8
  @AnchorType int verticalAnchorType;
  if (anchorId % 3 == 0) {
    verticalAnchorType = Cue.ANCHOR_TYPE_START;
  } else if (anchorId % 3 == 1) {
    verticalAnchorType = Cue.ANCHOR_TYPE_MIDDLE;
  } else {
    verticalAnchorType = Cue.ANCHOR_TYPE_END;
  }
  // TODO: Add support for right-to-left languages (i.e. where start is on the right).
  @AnchorType int horizontalAnchorType;
  if (anchorId / 3 == 0) {
    horizontalAnchorType = Cue.ANCHOR_TYPE_START;
  } else if (anchorId / 3 == 1) {
    horizontalAnchorType = Cue.ANCHOR_TYPE_MIDDLE;
  } else {
    horizontalAnchorType = Cue.ANCHOR_TYPE_END;
  }

  boolean windowColorSet = (windowFillColor != COLOR_SOLID_BLACK);

  return new Cea708Cue(cueString, alignment, line, Cue.LINE_TYPE_FRACTION, verticalAnchorType,
      position, horizontalAnchorType, Cue.DIMEN_UNSET, windowColorSet, windowFillColor,
      priority);
}
 
Example 19
Source File: Cea608Decoder.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
public Cue build(@Cue.AnchorType int forcedPositionAnchor) {
  SpannableStringBuilder cueString = new SpannableStringBuilder();
  // Add any rolled up captions, separated by new lines.
  for (int i = 0; i < rolledUpCaptions.size(); i++) {
    cueString.append(rolledUpCaptions.get(i));
    cueString.append('\n');
  }
  // Add the current line.
  cueString.append(buildCurrentLine());

  if (cueString.length() == 0) {
    // The cue is empty.
    return null;
  }

  int positionAnchor;
  // The number of empty columns before the start of the text, in the range [0-31].
  int startPadding = indent + tabOffset;
  // The number of empty columns after the end of the text, in the same range.
  int endPadding = SCREEN_CHARWIDTH - startPadding - cueString.length();
  int startEndPaddingDelta = startPadding - endPadding;
  if (forcedPositionAnchor != Cue.TYPE_UNSET) {
    positionAnchor = forcedPositionAnchor;
  } else if (captionMode == CC_MODE_POP_ON
      && (Math.abs(startEndPaddingDelta) < 3 || endPadding < 0)) {
    // Treat approximately centered pop-on captions as middle aligned. We also treat captions
    // that are wider than they should be in this way. See
    // https://github.com/google/ExoPlayer/issues/3534.
    positionAnchor = Cue.ANCHOR_TYPE_MIDDLE;
  } else if (captionMode == CC_MODE_POP_ON && startEndPaddingDelta > 0) {
    // Treat pop-on captions with less padding at the end than the start as end aligned.
    positionAnchor = Cue.ANCHOR_TYPE_END;
  } else {
    // For all other cases assume start aligned.
    positionAnchor = Cue.ANCHOR_TYPE_START;
  }

  float position;
  switch (positionAnchor) {
    case Cue.ANCHOR_TYPE_MIDDLE:
      position = 0.5f;
      break;
    case Cue.ANCHOR_TYPE_END:
      position = (float) (SCREEN_CHARWIDTH - endPadding) / SCREEN_CHARWIDTH;
      // Adjust the position to fit within the safe area.
      position = position * 0.8f + 0.1f;
      break;
    case Cue.ANCHOR_TYPE_START:
    default:
      position = (float) startPadding / SCREEN_CHARWIDTH;
      // Adjust the position to fit within the safe area.
      position = position * 0.8f + 0.1f;
      break;
  }

  int lineAnchor;
  int line;
  // Note: Row indices are in the range [1-15].
  if (captionMode == CC_MODE_ROLL_UP || row > (BASE_ROW / 2)) {
    lineAnchor = Cue.ANCHOR_TYPE_END;
    line = row - BASE_ROW;
    // Two line adjustments. The first is because line indices from the bottom of the window
    // start from -1 rather than 0. The second is a blank row to act as the safe area.
    line -= 2;
  } else {
    lineAnchor = Cue.ANCHOR_TYPE_START;
    // Line indices from the top of the window start from 0, but we want a blank row to act as
    // the safe area. As a result no adjustment is necessary.
    line = row;
  }

  return new Cue(
      cueString,
      Alignment.ALIGN_NORMAL,
      line,
      Cue.LINE_TYPE_NUMBER,
      lineAnchor,
      position,
      positionAnchor,
      Cue.DIMEN_UNSET);
}
 
Example 20
Source File: Cea608Decoder.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
public Cue build(@Cue.AnchorType int forcedPositionAnchor) {
  SpannableStringBuilder cueString = new SpannableStringBuilder();
  // Add any rolled up captions, separated by new lines.
  for (int i = 0; i < rolledUpCaptions.size(); i++) {
    cueString.append(rolledUpCaptions.get(i));
    cueString.append('\n');
  }
  // Add the current line.
  cueString.append(buildCurrentLine());

  if (cueString.length() == 0) {
    // The cue is empty.
    return null;
  }

  int positionAnchor;
  // The number of empty columns before the start of the text, in the range [0-31].
  int startPadding = indent + tabOffset;
  // The number of empty columns after the end of the text, in the same range.
  int endPadding = SCREEN_CHARWIDTH - startPadding - cueString.length();
  int startEndPaddingDelta = startPadding - endPadding;
  if (forcedPositionAnchor != Cue.TYPE_UNSET) {
    positionAnchor = forcedPositionAnchor;
  } else if (captionMode == CC_MODE_POP_ON
      && (Math.abs(startEndPaddingDelta) < 3 || endPadding < 0)) {
    // Treat approximately centered pop-on captions as middle aligned. We also treat captions
    // that are wider than they should be in this way. See
    // https://github.com/google/ExoPlayer/issues/3534.
    positionAnchor = Cue.ANCHOR_TYPE_MIDDLE;
  } else if (captionMode == CC_MODE_POP_ON && startEndPaddingDelta > 0) {
    // Treat pop-on captions with less padding at the end than the start as end aligned.
    positionAnchor = Cue.ANCHOR_TYPE_END;
  } else {
    // For all other cases assume start aligned.
    positionAnchor = Cue.ANCHOR_TYPE_START;
  }

  float position;
  switch (positionAnchor) {
    case Cue.ANCHOR_TYPE_MIDDLE:
      position = 0.5f;
      break;
    case Cue.ANCHOR_TYPE_END:
      position = (float) (SCREEN_CHARWIDTH - endPadding) / SCREEN_CHARWIDTH;
      // Adjust the position to fit within the safe area.
      position = position * 0.8f + 0.1f;
      break;
    case Cue.ANCHOR_TYPE_START:
    default:
      position = (float) startPadding / SCREEN_CHARWIDTH;
      // Adjust the position to fit within the safe area.
      position = position * 0.8f + 0.1f;
      break;
  }

  int lineAnchor;
  int line;
  // Note: Row indices are in the range [1-15].
  if (captionMode == CC_MODE_ROLL_UP || row > (BASE_ROW / 2)) {
    lineAnchor = Cue.ANCHOR_TYPE_END;
    line = row - BASE_ROW;
    // Two line adjustments. The first is because line indices from the bottom of the window
    // start from -1 rather than 0. The second is a blank row to act as the safe area.
    line -= 2;
  } else {
    lineAnchor = Cue.ANCHOR_TYPE_START;
    // Line indices from the top of the window start from 0, but we want a blank row to act as
    // the safe area. As a result no adjustment is necessary.
    line = row;
  }

  return new Cue(
      cueString,
      Alignment.ALIGN_NORMAL,
      line,
      Cue.LINE_TYPE_NUMBER,
      lineAnchor,
      position,
      positionAnchor,
      Cue.DIMEN_UNSET);
}