1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.google.android.exoplayer2.text;
17 
18 import android.graphics.Bitmap;
19 import android.graphics.Color;
20 import android.text.Layout;
21 import android.text.Layout.Alignment;
22 import androidx.annotation.ColorInt;
23 import androidx.annotation.IntDef;
24 import androidx.annotation.Nullable;
25 import com.google.android.exoplayer2.util.Assertions;
26 import java.lang.annotation.Documented;
27 import java.lang.annotation.Retention;
28 import java.lang.annotation.RetentionPolicy;
29 
30 /** Contains information about a specific cue, including textual content and formatting data. */
31 // This class shouldn't be sub-classed. If a subtitle format needs additional fields, either they
32 // should be generic enough to be added here, or the format-specific decoder should pass the
33 // information around in a sidecar object.
34 public final class Cue {
35 
36   /** The empty cue. */
37   public static final Cue EMPTY = new Cue("");
38 
39   /** An unset position, width or size. */
40   // Note: We deliberately don't use Float.MIN_VALUE because it's positive & very close to zero.
41   public static final float DIMEN_UNSET = -Float.MAX_VALUE;
42 
43   /**
44    * The type of anchor, which may be unset. One of {@link #TYPE_UNSET}, {@link #ANCHOR_TYPE_START},
45    * {@link #ANCHOR_TYPE_MIDDLE} or {@link #ANCHOR_TYPE_END}.
46    */
47   @Documented
48   @Retention(RetentionPolicy.SOURCE)
49   @IntDef({TYPE_UNSET, ANCHOR_TYPE_START, ANCHOR_TYPE_MIDDLE, ANCHOR_TYPE_END})
50   public @interface AnchorType {}
51 
52   /** An unset anchor, line, text size or vertical type value. */
53   public static final int TYPE_UNSET = Integer.MIN_VALUE;
54 
55   /**
56    * Anchors the left (for horizontal positions) or top (for vertical positions) edge of the cue
57    * box.
58    */
59   public static final int ANCHOR_TYPE_START = 0;
60 
61   /**
62    * Anchors the middle of the cue box.
63    */
64   public static final int ANCHOR_TYPE_MIDDLE = 1;
65 
66   /**
67    * Anchors the right (for horizontal positions) or bottom (for vertical positions) edge of the cue
68    * box.
69    */
70   public static final int ANCHOR_TYPE_END = 2;
71 
72   /**
73    * The type of line, which may be unset. One of {@link #TYPE_UNSET}, {@link #LINE_TYPE_FRACTION}
74    * or {@link #LINE_TYPE_NUMBER}.
75    */
76   @Documented
77   @Retention(RetentionPolicy.SOURCE)
78   @IntDef({TYPE_UNSET, LINE_TYPE_FRACTION, LINE_TYPE_NUMBER})
79   public @interface LineType {}
80 
81   /**
82    * Value for {@link #lineType} when {@link #line} is a fractional position.
83    */
84   public static final int LINE_TYPE_FRACTION = 0;
85 
86   /**
87    * Value for {@link #lineType} when {@link #line} is a line number.
88    */
89   public static final int LINE_TYPE_NUMBER = 1;
90 
91   /**
92    * The type of default text size for this cue, which may be unset. One of {@link #TYPE_UNSET},
93    * {@link #TEXT_SIZE_TYPE_FRACTIONAL}, {@link #TEXT_SIZE_TYPE_FRACTIONAL_IGNORE_PADDING} or {@link
94    * #TEXT_SIZE_TYPE_ABSOLUTE}.
95    */
96   @Documented
97   @Retention(RetentionPolicy.SOURCE)
98   @IntDef({
99     TYPE_UNSET,
100     TEXT_SIZE_TYPE_FRACTIONAL,
101     TEXT_SIZE_TYPE_FRACTIONAL_IGNORE_PADDING,
102     TEXT_SIZE_TYPE_ABSOLUTE
103   })
104   public @interface TextSizeType {}
105 
106   /** Text size is measured as a fraction of the viewport size minus the view padding. */
107   public static final int TEXT_SIZE_TYPE_FRACTIONAL = 0;
108 
109   /** Text size is measured as a fraction of the viewport size, ignoring the view padding */
110   public static final int TEXT_SIZE_TYPE_FRACTIONAL_IGNORE_PADDING = 1;
111 
112   /** Text size is measured in number of pixels. */
113   public static final int TEXT_SIZE_TYPE_ABSOLUTE = 2;
114 
115   /**
116    * The type of vertical layout for this cue, which may be unset (i.e. horizontal). One of {@link
117    * #TYPE_UNSET}, {@link #VERTICAL_TYPE_RL} or {@link #VERTICAL_TYPE_LR}.
118    */
119   @Documented
120   @Retention(RetentionPolicy.SOURCE)
121   @IntDef({
122     TYPE_UNSET,
123     VERTICAL_TYPE_RL,
124     VERTICAL_TYPE_LR,
125   })
126   public @interface VerticalType {}
127 
128   /** Vertical right-to-left (e.g. for Japanese). */
129   public static final int VERTICAL_TYPE_RL = 1;
130 
131   /** Vertical left-to-right (e.g. for Mongolian). */
132   public static final int VERTICAL_TYPE_LR = 2;
133 
134   /**
135    * The cue text, or null if this is an image cue. Note the {@link CharSequence} may be decorated
136    * with styling spans.
137    */
138   @Nullable public final CharSequence text;
139 
140   /** The alignment of the cue text within the cue box, or null if the alignment is undefined. */
141   @Nullable public final Alignment textAlignment;
142 
143   /** The cue image, or null if this is a text cue. */
144   @Nullable public final Bitmap bitmap;
145 
146   /**
147    * The position of the {@link #lineAnchor} of the cue box within the viewport in the direction
148    * orthogonal to the writing direction (determined by {@link #verticalType}), or {@link
149    * #DIMEN_UNSET}. When set, the interpretation of the value depends on the value of {@link
150    * #lineType}.
151    *
152    * <p>The measurement direction depends on {@link #verticalType}:
153    *
154    * <ul>
155    *   <li>For {@link #TYPE_UNSET} (i.e. horizontal), this is the vertical position relative to the
156    *       top of the viewport.
157    *   <li>For {@link #VERTICAL_TYPE_LR} this is the horizontal position relative to the left of the
158    *       viewport.
159    *   <li>For {@link #VERTICAL_TYPE_RL} this is the horizontal position relative to the right of
160    *       the viewport.
161    * </ul>
162    */
163   public final float line;
164 
165   /**
166    * The type of the {@link #line} value.
167    *
168    * <ul>
169    *   <li>{@link #LINE_TYPE_FRACTION} indicates that {@link #line} is a fractional position within
170    *       the viewport.
171    *   <li>{@link #LINE_TYPE_NUMBER} indicates that {@link #line} is a line number, where the size
172    *       of each line is taken to be the size of the first line of the cue.
173    *       <ul>
174    *         <li>When {@link #line} is greater than or equal to 0 lines count from the start of the
175    *             viewport, with 0 indicating zero offset from the start edge. When {@link #line} is
176    *             negative lines count from the end of the viewport, with -1 indicating zero offset
177    *             from the end edge.
178    *         <li>For horizontal text the line spacing is the height of the first line of the cue,
179    *             and the start and end of the viewport are the top and bottom respectively.
180    *       </ul>
181    * </ul>
182    *
183    * <p>Note that it's particularly important to consider the effect of {@link #lineAnchor} when
184    * using {@link #LINE_TYPE_NUMBER}.
185    *
186    * <ul>
187    *   <li>{@code (line == 0 && lineAnchor == ANCHOR_TYPE_START)} positions a (potentially
188    *       multi-line) cue at the very start of the viewport.
189    *   <li>{@code (line == -1 && lineAnchor == ANCHOR_TYPE_END)} positions a (potentially
190    *       multi-line) cue at the very end of the viewport.
191    *   <li>{@code (line == 0 && lineAnchor == ANCHOR_TYPE_END)} and {@code (line == -1 && lineAnchor
192    *       == ANCHOR_TYPE_START)} position cues entirely outside of the viewport.
193    *   <li>{@code (line == 1 && lineAnchor == ANCHOR_TYPE_END)} positions a cue so that only the
194    *       last line is visible at the start of the viewport.
195    *   <li>{@code (line == -2 && lineAnchor == ANCHOR_TYPE_START)} position a cue so that only its
196    *       first line is visible at the end of the viewport.
197    * </ul>
198    */
199   public final @LineType int lineType;
200 
201   /**
202    * The cue box anchor positioned by {@link #line}. One of {@link #ANCHOR_TYPE_START}, {@link
203    * #ANCHOR_TYPE_MIDDLE}, {@link #ANCHOR_TYPE_END} and {@link #TYPE_UNSET}.
204    *
205    * <p>For the normal case of horizontal text, {@link #ANCHOR_TYPE_START}, {@link
206    * #ANCHOR_TYPE_MIDDLE} and {@link #ANCHOR_TYPE_END} correspond to the top, middle and bottom of
207    * the cue box respectively.
208    */
209   public final @AnchorType int lineAnchor;
210 
211   /**
212    * The fractional position of the {@link #positionAnchor} of the cue box within the viewport in
213    * the direction orthogonal to {@link #line}, or {@link #DIMEN_UNSET}.
214    *
215    * <p>The measurement direction depends on {@link #verticalType}.
216    *
217    * <ul>
218    *   <li>For {@link #TYPE_UNSET} (i.e. horizontal), this is the horizontal position relative to
219    *       the left of the viewport. Note that positioning is relative to the left of the viewport
220    *       even in the case of right-to-left text.
221    *   <li>For {@link #VERTICAL_TYPE_LR} and {@link #VERTICAL_TYPE_RL} (i.e. vertical), this is the
222    *       vertical position relative to the top of the viewport.
223    * </ul>
224    */
225   public final float position;
226 
227   /**
228    * The cue box anchor positioned by {@link #position}. One of {@link #ANCHOR_TYPE_START}, {@link
229    * #ANCHOR_TYPE_MIDDLE}, {@link #ANCHOR_TYPE_END} and {@link #TYPE_UNSET}.
230    *
231    * <p>For the normal case of horizontal text, {@link #ANCHOR_TYPE_START}, {@link
232    * #ANCHOR_TYPE_MIDDLE} and {@link #ANCHOR_TYPE_END} correspond to the left, middle and right of
233    * the cue box respectively.
234    */
235   public final @AnchorType int positionAnchor;
236 
237   /**
238    * The size of the cue box in the writing direction specified as a fraction of the viewport size
239    * in that direction, or {@link #DIMEN_UNSET}.
240    */
241   public final float size;
242 
243   /**
244    * The bitmap height as a fraction of the of the viewport size, or {@link #DIMEN_UNSET} if the
245    * bitmap should be displayed at its natural height given the bitmap dimensions and the specified
246    * {@link #size}.
247    */
248   public final float bitmapHeight;
249 
250   /**
251    * Specifies whether or not the {@link #windowColor} property is set.
252    */
253   public final boolean windowColorSet;
254 
255   /**
256    * The fill color of the window.
257    */
258   public final int windowColor;
259 
260   /**
261    * The default text size type for this cue's text, or {@link #TYPE_UNSET} if this cue has no
262    * default text size.
263    */
264   public final @TextSizeType int textSizeType;
265 
266   /**
267    * The default text size for this cue's text, or {@link #DIMEN_UNSET} if this cue has no default
268    * text size.
269    */
270   public final float textSize;
271 
272   /**
273    * The vertical formatting of this Cue, or {@link #TYPE_UNSET} if the cue has no vertical setting
274    * (and so should be horizontal).
275    */
276   public final @VerticalType int verticalType;
277 
278   /**
279    * Creates a text cue whose {@link #textAlignment} is null, whose type parameters are set to
280    * {@link #TYPE_UNSET} and whose dimension parameters are set to {@link #DIMEN_UNSET}.
281    *
282    * @param text See {@link #text}.
283    * @deprecated Use {@link Builder}.
284    */
285   @Deprecated
Cue(CharSequence text)286   public Cue(CharSequence text) {
287     this(
288         text,
289         /* textAlignment= */ null,
290         /* line= */ DIMEN_UNSET,
291         /* lineType= */ TYPE_UNSET,
292         /* lineAnchor= */ TYPE_UNSET,
293         /* position= */ DIMEN_UNSET,
294         /* positionAnchor= */ TYPE_UNSET,
295         /* size= */ DIMEN_UNSET);
296   }
297 
298   /**
299    * Creates a text cue.
300    *
301    * @param text See {@link #text}.
302    * @param textAlignment See {@link #textAlignment}.
303    * @param line See {@link #line}.
304    * @param lineType See {@link #lineType}.
305    * @param lineAnchor See {@link #lineAnchor}.
306    * @param position See {@link #position}.
307    * @param positionAnchor See {@link #positionAnchor}.
308    * @param size See {@link #size}.
309    * @deprecated Use {@link Builder}.
310    */
311   @Deprecated
Cue( CharSequence text, @Nullable Alignment textAlignment, float line, @LineType int lineType, @AnchorType int lineAnchor, float position, @AnchorType int positionAnchor, float size)312   public Cue(
313       CharSequence text,
314       @Nullable Alignment textAlignment,
315       float line,
316       @LineType int lineType,
317       @AnchorType int lineAnchor,
318       float position,
319       @AnchorType int positionAnchor,
320       float size) {
321     this(
322         text,
323         textAlignment,
324         line,
325         lineType,
326         lineAnchor,
327         position,
328         positionAnchor,
329         size,
330         /* windowColorSet= */ false,
331         /* windowColor= */ Color.BLACK);
332   }
333 
334   /**
335    * Creates a text cue.
336    *
337    * @param text See {@link #text}.
338    * @param textAlignment See {@link #textAlignment}.
339    * @param line See {@link #line}.
340    * @param lineType See {@link #lineType}.
341    * @param lineAnchor See {@link #lineAnchor}.
342    * @param position See {@link #position}.
343    * @param positionAnchor See {@link #positionAnchor}.
344    * @param size See {@link #size}.
345    * @param textSizeType See {@link #textSizeType}.
346    * @param textSize See {@link #textSize}.
347    * @deprecated Use {@link Builder}.
348    */
349   @Deprecated
Cue( CharSequence text, @Nullable Alignment textAlignment, float line, @LineType int lineType, @AnchorType int lineAnchor, float position, @AnchorType int positionAnchor, float size, @TextSizeType int textSizeType, float textSize)350   public Cue(
351       CharSequence text,
352       @Nullable Alignment textAlignment,
353       float line,
354       @LineType int lineType,
355       @AnchorType int lineAnchor,
356       float position,
357       @AnchorType int positionAnchor,
358       float size,
359       @TextSizeType int textSizeType,
360       float textSize) {
361     this(
362         text,
363         textAlignment,
364         /* bitmap= */ null,
365         line,
366         lineType,
367         lineAnchor,
368         position,
369         positionAnchor,
370         textSizeType,
371         textSize,
372         size,
373         /* bitmapHeight= */ DIMEN_UNSET,
374         /* windowColorSet= */ false,
375         /* windowColor= */ Color.BLACK,
376         /* verticalType= */ TYPE_UNSET);
377   }
378 
379   /**
380    * Creates a text cue.
381    *
382    * @param text See {@link #text}.
383    * @param textAlignment See {@link #textAlignment}.
384    * @param line See {@link #line}.
385    * @param lineType See {@link #lineType}.
386    * @param lineAnchor See {@link #lineAnchor}.
387    * @param position See {@link #position}.
388    * @param positionAnchor See {@link #positionAnchor}.
389    * @param size See {@link #size}.
390    * @param windowColorSet See {@link #windowColorSet}.
391    * @param windowColor See {@link #windowColor}.
392    * @deprecated Use {@link Builder}.
393    */
394   @Deprecated
Cue( CharSequence text, @Nullable Alignment textAlignment, float line, @LineType int lineType, @AnchorType int lineAnchor, float position, @AnchorType int positionAnchor, float size, boolean windowColorSet, int windowColor)395   public Cue(
396       CharSequence text,
397       @Nullable Alignment textAlignment,
398       float line,
399       @LineType int lineType,
400       @AnchorType int lineAnchor,
401       float position,
402       @AnchorType int positionAnchor,
403       float size,
404       boolean windowColorSet,
405       int windowColor) {
406     this(
407         text,
408         textAlignment,
409         /* bitmap= */ null,
410         line,
411         lineType,
412         lineAnchor,
413         position,
414         positionAnchor,
415         /* textSizeType= */ TYPE_UNSET,
416         /* textSize= */ DIMEN_UNSET,
417         size,
418         /* bitmapHeight= */ DIMEN_UNSET,
419         windowColorSet,
420         windowColor,
421         /* verticalType= */ TYPE_UNSET);
422   }
423 
Cue( @ullable CharSequence text, @Nullable Alignment textAlignment, @Nullable Bitmap bitmap, float line, @LineType int lineType, @AnchorType int lineAnchor, float position, @AnchorType int positionAnchor, @TextSizeType int textSizeType, float textSize, float size, float bitmapHeight, boolean windowColorSet, int windowColor, @VerticalType int verticalType)424   private Cue(
425       @Nullable CharSequence text,
426       @Nullable Alignment textAlignment,
427       @Nullable Bitmap bitmap,
428       float line,
429       @LineType int lineType,
430       @AnchorType int lineAnchor,
431       float position,
432       @AnchorType int positionAnchor,
433       @TextSizeType int textSizeType,
434       float textSize,
435       float size,
436       float bitmapHeight,
437       boolean windowColorSet,
438       int windowColor,
439       @VerticalType int verticalType) {
440     // Exactly one of text or bitmap should be set.
441     if (text == null) {
442       Assertions.checkNotNull(bitmap);
443     } else {
444       Assertions.checkArgument(bitmap == null);
445     }
446     this.text = text;
447     this.textAlignment = textAlignment;
448     this.bitmap = bitmap;
449     this.line = line;
450     this.lineType = lineType;
451     this.lineAnchor = lineAnchor;
452     this.position = position;
453     this.positionAnchor = positionAnchor;
454     this.size = size;
455     this.bitmapHeight = bitmapHeight;
456     this.windowColorSet = windowColorSet;
457     this.windowColor = windowColor;
458     this.textSizeType = textSizeType;
459     this.textSize = textSize;
460     this.verticalType = verticalType;
461   }
462 
463   /** A builder for {@link Cue} objects. */
464   public static final class Builder {
465     @Nullable private CharSequence text;
466     @Nullable private Bitmap bitmap;
467     @Nullable private Alignment textAlignment;
468     private float line;
469     @LineType private int lineType;
470     @AnchorType private int lineAnchor;
471     private float position;
472     @AnchorType private int positionAnchor;
473     @TextSizeType private int textSizeType;
474     private float textSize;
475     private float size;
476     private float bitmapHeight;
477     private boolean windowColorSet;
478     @ColorInt private int windowColor;
479     @VerticalType private int verticalType;
480 
Builder()481     public Builder() {
482       text = null;
483       bitmap = null;
484       textAlignment = null;
485       line = DIMEN_UNSET;
486       lineType = TYPE_UNSET;
487       lineAnchor = TYPE_UNSET;
488       position = DIMEN_UNSET;
489       positionAnchor = TYPE_UNSET;
490       textSizeType = TYPE_UNSET;
491       textSize = DIMEN_UNSET;
492       size = DIMEN_UNSET;
493       bitmapHeight = DIMEN_UNSET;
494       windowColorSet = false;
495       windowColor = Color.BLACK;
496       verticalType = TYPE_UNSET;
497     }
498 
499     /**
500      * Sets the cue text.
501      *
502      * <p>Note that {@code text} may be decorated with styling spans.
503      *
504      * @see Cue#text
505      */
setText(CharSequence text)506     public Builder setText(CharSequence text) {
507       this.text = text;
508       return this;
509     }
510 
511     /**
512      * Gets the cue text.
513      *
514      * @see Cue#text
515      */
516     @Nullable
getText()517     public CharSequence getText() {
518       return text;
519     }
520 
521     /**
522      * Sets the cue image.
523      *
524      * @see Cue#bitmap
525      */
setBitmap(Bitmap bitmap)526     public Builder setBitmap(Bitmap bitmap) {
527       this.bitmap = bitmap;
528       return this;
529     }
530 
531     /**
532      * Gets the cue image.
533      *
534      * @see Cue#bitmap
535      */
536     @Nullable
getBitmap()537     public Bitmap getBitmap() {
538       return bitmap;
539     }
540 
541     /**
542      * Sets the alignment of the cue text within the cue box.
543      *
544      * <p>Passing null means the alignment is undefined.
545      *
546      * @see Cue#textAlignment
547      */
setTextAlignment(@ullable Layout.Alignment textAlignment)548     public Builder setTextAlignment(@Nullable Layout.Alignment textAlignment) {
549       this.textAlignment = textAlignment;
550       return this;
551     }
552 
553     /**
554      * Gets the alignment of the cue text within the cue box, or null if the alignment is undefined.
555      *
556      * @see Cue#textAlignment
557      */
558     @Nullable
getTextAlignment()559     public Alignment getTextAlignment() {
560       return textAlignment;
561     }
562 
563     /**
564      * Sets the position of the {@code lineAnchor} of the cue box within the viewport in the
565      * direction orthogonal to the writing direction.
566      *
567      * <p>The interpretation of the {@code line} depends on the value of {@code lineType}.
568      *
569      * <ul>
570      *   <li>{@link #LINE_TYPE_FRACTION} indicates that {@code line} is a fractional position within
571      *       the viewport.
572      *   <li>{@link #LINE_TYPE_NUMBER} indicates that {@code line} is a line number, where the size
573      *       of each line is taken to be the size of the first line of the cue.
574      *       <ul>
575      *         <li>When {@code line} is greater than or equal to 0 lines count from the start of the
576      *             viewport, with 0 indicating zero offset from the start edge.
577      *         <li>When {@code line} is negative lines count from the end of the viewport, with -1
578      *             indicating zero offset from the end edge.
579      *         <li>For horizontal text the line spacing is the height of the first line of the cue,
580      *             and the start and end of the viewport are the top and bottom respectively.
581      *       </ul>
582      * </ul>
583      *
584      * <p>Note that it's particularly important to consider the effect of {@link #setLineAnchor(int)
585      * lineAnchor} when using {@link #LINE_TYPE_NUMBER}.
586      *
587      * <ul>
588      *   <li>{@code (line == 0 && lineAnchor == ANCHOR_TYPE_START)} positions a (potentially
589      *       multi-line) cue at the very start of the viewport.
590      *   <li>{@code (line == -1 && lineAnchor == ANCHOR_TYPE_END)} positions a (potentially
591      *       multi-line) cue at the very end of the viewport.
592      *   <li>{@code (line == 0 && lineAnchor == ANCHOR_TYPE_END)} and {@code (line == -1 &&
593      *       lineAnchor == ANCHOR_TYPE_START)} position cues entirely outside of the viewport.
594      *   <li>{@code (line == 1 && lineAnchor == ANCHOR_TYPE_END)} positions a cue so that only the
595      *       last line is visible at the start of the viewport.
596      *   <li>{@code (line == -2 && lineAnchor == ANCHOR_TYPE_START)} position a cue so that only its
597      *       first line is visible at the end of the viewport.
598      * </ul>
599      *
600      * @see Cue#line
601      * @see Cue#lineType
602      */
setLine(float line, @LineType int lineType)603     public Builder setLine(float line, @LineType int lineType) {
604       this.line = line;
605       this.lineType = lineType;
606       return this;
607     }
608 
609     /**
610      * Gets the position of the {@code lineAnchor} of the cue box within the viewport in the
611      * direction orthogonal to the writing direction.
612      *
613      * @see Cue#line
614      */
getLine()615     public float getLine() {
616       return line;
617     }
618 
619     /**
620      * Gets the type of the value of {@link #getLine()}.
621      *
622      * @see Cue#lineType
623      */
624     @LineType
getLineType()625     public int getLineType() {
626       return lineType;
627     }
628 
629     /**
630      * Sets the cue box anchor positioned by {@link #setLine(float, int) line}.
631      *
632      * <p>For the normal case of horizontal text, {@link #ANCHOR_TYPE_START}, {@link
633      * #ANCHOR_TYPE_MIDDLE} and {@link #ANCHOR_TYPE_END} correspond to the top, middle and bottom of
634      * the cue box respectively.
635      *
636      * @see Cue#lineAnchor
637      */
setLineAnchor(@nchorType int lineAnchor)638     public Builder setLineAnchor(@AnchorType int lineAnchor) {
639       this.lineAnchor = lineAnchor;
640       return this;
641     }
642 
643     /**
644      * Gets the cue box anchor positioned by {@link #setLine(float, int) line}.
645      *
646      * @see Cue#lineAnchor
647      */
648     @AnchorType
getLineAnchor()649     public int getLineAnchor() {
650       return lineAnchor;
651     }
652 
653     /**
654      * Sets the fractional position of the {@link #setPositionAnchor(int) positionAnchor} of the cue
655      * box within the viewport in the direction orthogonal to {@link #setLine(float, int) line}.
656      *
657      * <p>For horizontal text, this is the horizontal position relative to the left of the viewport.
658      * Note that positioning is relative to the left of the viewport even in the case of
659      * right-to-left text.
660      *
661      * @see Cue#position
662      */
setPosition(float position)663     public Builder setPosition(float position) {
664       this.position = position;
665       return this;
666     }
667 
668     /**
669      * Gets the fractional position of the {@link #setPositionAnchor(int) positionAnchor} of the cue
670      * box within the viewport in the direction orthogonal to {@link #setLine(float, int) line}.
671      *
672      * @see Cue#position
673      */
getPosition()674     public float getPosition() {
675       return position;
676     }
677 
678     /**
679      * Sets the cue box anchor positioned by {@link #setPosition(float) position}.
680      *
681      * <p>For the normal case of horizontal text, {@link #ANCHOR_TYPE_START}, {@link
682      * #ANCHOR_TYPE_MIDDLE} and {@link #ANCHOR_TYPE_END} correspond to the left, middle and right of
683      * the cue box respectively.
684      *
685      * @see Cue#positionAnchor
686      */
setPositionAnchor(@nchorType int positionAnchor)687     public Builder setPositionAnchor(@AnchorType int positionAnchor) {
688       this.positionAnchor = positionAnchor;
689       return this;
690     }
691 
692     /**
693      * Gets the cue box anchor positioned by {@link #setPosition(float) position}.
694      *
695      * @see Cue#positionAnchor
696      */
697     @AnchorType
getPositionAnchor()698     public int getPositionAnchor() {
699       return positionAnchor;
700     }
701 
702     /**
703      * Sets the default text size and type for this cue's text.
704      *
705      * @see Cue#textSize
706      * @see Cue#textSizeType
707      */
setTextSize(float textSize, @TextSizeType int textSizeType)708     public Builder setTextSize(float textSize, @TextSizeType int textSizeType) {
709       this.textSize = textSize;
710       this.textSizeType = textSizeType;
711       return this;
712     }
713 
714     /**
715      * Gets the default text size type for this cue's text.
716      *
717      * @see Cue#textSizeType
718      */
719     @TextSizeType
getTextSizeType()720     public int getTextSizeType() {
721       return textSizeType;
722     }
723 
724     /**
725      * Gets the default text size for this cue's text.
726      *
727      * @see Cue#textSize
728      */
getTextSize()729     public float getTextSize() {
730       return textSize;
731     }
732 
733     /**
734      * Sets the size of the cue box in the writing direction specified as a fraction of the viewport
735      * size in that direction.
736      *
737      * @see Cue#size
738      */
setSize(float size)739     public Builder setSize(float size) {
740       this.size = size;
741       return this;
742     }
743 
744     /**
745      * Gets the size of the cue box in the writing direction specified as a fraction of the viewport
746      * size in that direction.
747      *
748      * @see Cue#size
749      */
getSize()750     public float getSize() {
751       return size;
752     }
753 
754     /**
755      * Sets the bitmap height as a fraction of the viewport size.
756      *
757      * @see Cue#bitmapHeight
758      */
setBitmapHeight(float bitmapHeight)759     public Builder setBitmapHeight(float bitmapHeight) {
760       this.bitmapHeight = bitmapHeight;
761       return this;
762     }
763 
764     /**
765      * Gets the bitmap height as a fraction of the viewport size.
766      *
767      * @see Cue#bitmapHeight
768      */
getBitmapHeight()769     public float getBitmapHeight() {
770       return bitmapHeight;
771     }
772 
773     /**
774      * Sets the fill color of the window.
775      *
776      * <p>Also sets {@link Cue#windowColorSet} to true.
777      *
778      * @see Cue#windowColor
779      * @see Cue#windowColorSet
780      */
setWindowColor(@olorInt int windowColor)781     public Builder setWindowColor(@ColorInt int windowColor) {
782       this.windowColor = windowColor;
783       this.windowColorSet = true;
784       return this;
785     }
786 
787     /**
788      * Returns true if the fill color of the window is set.
789      *
790      * @see Cue#windowColorSet
791      */
isWindowColorSet()792     public boolean isWindowColorSet() {
793       return windowColorSet;
794     }
795 
796     /**
797      * Gets the fill color of the window.
798      *
799      * @see Cue#windowColor
800      */
801     @ColorInt
getWindowColor()802     public int getWindowColor() {
803       return windowColor;
804     }
805 
806     /**
807      * Sets the vertical formatting for this Cue.
808      *
809      * @see Cue#verticalType
810      */
setVerticalType(@erticalType int verticalType)811     public Builder setVerticalType(@VerticalType int verticalType) {
812       this.verticalType = verticalType;
813       return this;
814     }
815 
816     /**
817      * Gets the vertical formatting for this Cue.
818      *
819      * @see Cue#verticalType
820      */
821     @VerticalType
getVerticalType()822     public int getVerticalType() {
823       return verticalType;
824     }
825 
826     /** Build the cue. */
build()827     public Cue build() {
828       return new Cue(
829           text,
830           textAlignment,
831           bitmap,
832           line,
833           lineType,
834           lineAnchor,
835           position,
836           positionAnchor,
837           textSizeType,
838           textSize,
839           size,
840           bitmapHeight,
841           windowColorSet,
842           windowColor,
843           verticalType);
844     }
845   }
846 }
847