+ else //close tag
+ s->hlit.end = s->text_pos;
+ /* If there are more than one secondary color changes in ASS,
+ take start of first section and end of last section. Movtext
+ allows only one highlight box per sample.
+ */
+ }
+ // Movtext does not support changes to other color_id (outline, background)
+}
+
+static void mov_text_alpha_set(MovTextContext *s, uint8_t alpha)
+{
+ if (!s->style_attributes_temp ||
+ (s->style_attributes_temp->style_color & 0xff) == alpha) {
+ // color hasn't changed
+ return;
+ }
+ if (mov_text_style_start(s))
+ s->style_attributes_temp->style_color =
+ (s->style_attributes_temp->style_color & 0xffffff00) | alpha;
+}
+
+static void mov_text_alpha_cb(void *priv, int alpha, int alpha_id)
+{
+ MovTextContext *s = priv;
+
+ alpha = 255 - alpha;
+ if (alpha_id == 1) // primary alpha changes
+ mov_text_alpha_set(s, alpha);
+ else if (alpha_id == 2) { //secondary alpha changes
+ if (!(s->box_flags & HCLR_BOX))
+ // Highlight color not set yet, use current primary color
+ s->hclr.color = s->style_attributes_temp->style_color;
+ if (!(s->box_flags & HLIT_BOX) || s->hlit.start == s->text_pos) {
+ s->box_flags |= HCLR_BOX;
+ s->box_flags |= HLIT_BOX;
+ s->hlit.start = s->text_pos;
+ s->hclr.color = (s->hclr.color & 0xffffff00) | alpha;
+ }
+ else //close tag
+ s->hlit.end = s->text_pos;
+ }
+ // Movtext does not support changes to other alpha_id (outline, background)
+}
+
+static uint16_t find_font_id(MovTextContext * s, const char * name)
+{
+ int i;
+ for (i = 0; i < s->font_count; i++) {
+ if (!strcmp(name, s->fonts[i]))
+ return i + 1;
+ }
+ return 1;
+}
+
+static void mov_text_font_name_set(MovTextContext *s, const char *name)
+{
+ int fontID = find_font_id(s, name);
+ if (!s->style_attributes_temp ||
+ s->style_attributes_temp->style_fontID == fontID) {
+ // color hasn't changed
+ return;
+ }
+ if (mov_text_style_start(s))
+ s->style_attributes_temp->style_fontID = fontID;
+}
+
+static void mov_text_font_name_cb(void *priv, const char *name)
+{
+ mov_text_font_name_set((MovTextContext*)priv, name);
+}
+
+static void mov_text_font_size_set(MovTextContext *s, int size)
+{
+ size = FONTSIZE_SCALE(s, size);
+ if (!s->style_attributes_temp ||
+ s->style_attributes_temp->style_fontsize == size) {
+ // color hasn't changed
+ return;
+ }
+ if (mov_text_style_start(s))
+ s->style_attributes_temp->style_fontsize = size;
+}
+
+static void mov_text_font_size_cb(void *priv, int size)
+{
+ mov_text_font_size_set((MovTextContext*)priv, size);
+}
+
+static void mov_text_end_cb(void *priv)
+{
+ // End of text, close any open style record
+ mov_text_style_start((MovTextContext*)priv);
+}
+
+static void mov_text_ass_style_set(MovTextContext *s, ASSStyle *style)
+{
+ uint8_t style_flags, alpha;
+ uint32_t color;
+
+ if (style) {
+ style_flags = (!!style->bold * STYLE_FLAG_BOLD) |
+ (!!style->italic * STYLE_FLAG_ITALIC) |
+ (!!style->underline * STYLE_FLAG_UNDERLINE);
+ mov_text_style_set(s, style_flags);
+ color = BGR_TO_RGB(style->primary_color & 0xffffff) << 8;
+ mov_text_color_set(s, color);
+ alpha = 255 - ((uint32_t)style->primary_color >> 24);
+ mov_text_alpha_set(s, alpha);
+ mov_text_font_size_set(s, style->font_size);
+ mov_text_font_name_set(s, style->font_name);
+ } else {
+ // End current style record, go back to defaults
+ mov_text_style_start(s);