Fix division by zero in "\t" parsing.
[mplayer.git] / libass / ass_render.c
index b273b0f..f69c0c9 100644 (file)
@@ -61,6 +61,15 @@ typedef struct ass_settings_s {
        char* default_family;
 } ass_settings_t;
 
+// a rendered event
+typedef struct event_images_s {
+       ass_image_t* imgs;
+       int top, height;
+       int detect_collisions;
+       int shift_direction;
+       ass_event_t* event;
+} event_images_t;
+
 struct ass_renderer_s {
        ass_library_t* library;
        FT_Library ftlibrary;
@@ -70,6 +79,10 @@ struct ass_renderer_s {
        ass_synth_priv_t* synth_priv;
 
        ass_image_t* images_root; // rendering result is stored here
+       ass_image_t* prev_images_root;
+
+       event_images_t* eimg; // temporary buffer for sorting rendered events
+       int eimg_size; // allocated buffer size
 };
 
 typedef enum {EF_NONE = 0, EF_KARAOKE, EF_KARAOKE_KF, EF_KARAOKE_KO} effect_t;
@@ -97,9 +110,9 @@ typedef struct glyph_info_s {
 //     int height;
        int be; // blur edges
        int shadow;
-       double frz; // z-axis rotation
+       double frx, fry, frz; // rotation
        
-       glyph_hash_key_t hash_key;
+       bitmap_hash_key_t hash_key;
 } glyph_info_t;
 
 typedef struct line_info_s {
@@ -127,7 +140,7 @@ typedef struct render_context_s {
        
        FT_Stroker stroker;
        int alignment; // alignment overrides go here; if zero, style value will be used
-       double rotation;
+       double frx, fry, frz;
        enum {  EVENT_NORMAL, // "normal" top-, sub- or mid- title
                EVENT_POSITIONED, // happens after pos(,), margins are ignored
                EVENT_HSCROLL, // "Banner" transition effect, text_width is unlimited
@@ -135,8 +148,9 @@ typedef struct render_context_s {
                } evt_type;
        int pos_x, pos_y; // position
        int org_x, org_y; // origin
+       char have_origin; // origin is explicitly defined; if 0, get_base_point() is used
        double scale_x, scale_y;
-       int hspacing; // distance between letters, in pixels
+       double hspacing; // distance between letters, in pixels
        double border; // outline width
        uint32_t c[4]; // colors(Primary, Secondary, so on) in RGBA
        int clip_x0, clip_y0, clip_x1, clip_y1;
@@ -182,15 +196,6 @@ static text_info_t text_info;
 static render_context_t render_context;
 static frame_context_t frame_context;
 
-// a rendered event
-typedef struct event_images_s {
-       ass_image_t* imgs;
-       int top, height;
-       int detect_collisions;
-       int shift_direction;
-       ass_event_t* event;
-} event_images_t;
-
 struct render_priv_s {
        int top, height;
        int render_id;
@@ -247,6 +252,7 @@ ass_renderer_t* ass_renderer_init(ass_library_t* library)
        // images_root and related stuff is zero-filled in calloc
        
        ass_font_cache_init();
+       ass_bitmap_cache_init();
        ass_glyph_cache_init();
 
        text_info.glyphs = calloc(MAX_GLYPHS, sizeof(glyph_info_t));
@@ -261,6 +267,7 @@ ass_init_exit:
 void ass_renderer_done(ass_renderer_t* priv)
 {
        ass_font_cache_done();
+       ass_bitmap_cache_done();
        ass_glyph_cache_done();
        if (render_context.stroker) {
                FT_Stroker_Done(render_context.stroker);
@@ -269,6 +276,7 @@ void ass_renderer_done(ass_renderer_t* priv)
        if (priv && priv->ftlibrary) FT_Done_FreeType(priv->ftlibrary);
        if (priv && priv->fontconfig_priv) fontconfig_done(priv->fontconfig_priv);
        if (priv && priv->synth_priv) ass_synth_done(priv->synth_priv);
+       if (priv && priv->eimg) free(priv->eimg);
        if (priv) free(priv);
        if (text_info.glyphs) free(text_info.glyphs);
 }
@@ -372,7 +380,7 @@ static ass_image_t** render_glyph(bitmap_t* bm, int dst_x, int dst_y, uint32_t c
 }
 
 /**
- * \brief Render text_info_t struct into ass_images_t list
+ * \brief Render text_info_t struct into ass_image_t list
  * Rasterize glyphs and put them in glyph cache.
  */
 static ass_image_t* render_text(text_info_t* text_info, int dst_x, int dst_y)
@@ -380,12 +388,12 @@ static ass_image_t* render_text(text_info_t* text_info, int dst_x, int dst_y)
        int pen_x, pen_y;
        int i, error;
        bitmap_t* bm;
-       glyph_hash_val_t hash_val;
+       bitmap_hash_val_t hash_val;
        ass_image_t* head;
        ass_image_t** tail = &head;
 
        for (i = 0; i < text_info->length; ++i) {
-               if (text_info->glyphs[i].glyph) {
+               if (text_info->glyphs[i].glyph && text_info->glyphs[i].bm == 0) {
                        if ((text_info->glyphs[i].symbol == '\n') || (text_info->glyphs[i].symbol == 0))
                                continue;
                        error = glyph_to_bitmap(ass_renderer->synth_priv,
@@ -394,23 +402,23 @@ static ass_image_t* render_text(text_info_t* text_info, int dst_x, int dst_y)
                                        &text_info->glyphs[i].bm_s, text_info->glyphs[i].be);
                        if (error)
                                text_info->glyphs[i].symbol = 0;
-                       FT_Done_Glyph(text_info->glyphs[i].glyph);
-                       if (text_info->glyphs[i].outline_glyph)
-                               FT_Done_Glyph(text_info->glyphs[i].outline_glyph);
 
                        // cache
-                       hash_val.bbox_scaled = text_info->glyphs[i].bbox;
                        hash_val.bm_o = text_info->glyphs[i].bm_o;
                        hash_val.bm = text_info->glyphs[i].bm;
                        hash_val.bm_s = text_info->glyphs[i].bm_s;
-                       hash_val.advance.x = text_info->glyphs[i].advance.x;
-                       hash_val.advance.y = text_info->glyphs[i].advance.y;
-                       cache_add_glyph(&(text_info->glyphs[i].hash_key), &hash_val);
-
+                       cache_add_bitmap(&(text_info->glyphs[i].hash_key), &hash_val);
                }
        }
 
        for (i = 0; i < text_info->length; ++i) {
+               if (text_info->glyphs[i].glyph)
+                       FT_Done_Glyph(text_info->glyphs[i].glyph);
+               if (text_info->glyphs[i].outline_glyph)
+                       FT_Done_Glyph(text_info->glyphs[i].outline_glyph);
+       }
+
+       for (i = 0; i < text_info->length; ++i) {
                glyph_info_t* info = text_info->glyphs + i;
                if ((info->symbol == 0) || (info->symbol == '\n') || !info->bm_s || (info->shadow == 0))
                        continue;
@@ -495,12 +503,12 @@ static void compute_string_bbox( text_info_t* info, FT_BBox *abbox ) {
        if (text_info.length > 0) {
                bbox.xMin = 32000;
                bbox.xMax = -32000;
-               bbox.yMin = - (text_info.lines[0].asc >> 6) + text_info.glyphs[0].pos.y;
-               bbox.yMax = ((text_info.height - text_info.lines[0].asc) >> 6) + text_info.glyphs[0].pos.y;
+               bbox.yMin = - d6_to_int(text_info.lines[0].asc) + text_info.glyphs[0].pos.y;
+               bbox.yMax = d6_to_int(text_info.height - text_info.lines[0].asc) + text_info.glyphs[0].pos.y;
 
                for (i = 0; i < text_info.length; ++i) {
                        int s = text_info.glyphs[i].pos.x;
-                       int e = s + (text_info.glyphs[i].advance.x >> 6);
+                       int e = s + d6_to_int(text_info.glyphs[i].advance.x);
                        bbox.xMin = FFMIN(bbox.xMin, s);
                        bbox.xMax = FFMAX(bbox.xMax, e);
                }
@@ -561,7 +569,7 @@ static void update_font(void)
        else if (val == 1) val = 110; //italic
        desc.italic = val;
 
-       render_context.font = ass_font_new(priv->ftlibrary, priv->fontconfig_priv, &desc);
+       render_context.font = ass_font_new(priv->library, priv->ftlibrary, priv->fontconfig_priv, &desc);
        
        if (render_context.font)
                change_font_size(render_context.font_size);
@@ -594,7 +602,7 @@ static void change_border(double border)
 #if (FREETYPE_MAJOR > 2) || ((FREETYPE_MAJOR == 2) && (FREETYPE_MINOR > 1))
                        error = FT_Stroker_New( ass_renderer->ftlibrary, &render_context.stroker );
 #else // < 2.2
-                       error = FT_Stroker_New( render_context.font->face->memory, &render_context.stroker );
+                       error = FT_Stroker_New( render_context.font->faces[0]->memory, &render_context.stroker );
 #endif
                        if (error) {
                                mp_msg(MSGT_ASS, MSGL_V, "failed to get stroker\n");
@@ -674,7 +682,7 @@ static unsigned interpolate_alpha(long long now,
        return a;
 }
 
-static void reset_render_context();
+static void reset_render_context(void);
 
 /**
  * \brief Parse style override tag.
@@ -696,22 +704,22 @@ static char* parse_tag(char* p, double pwr) {
                if (tp == 'x') {
                        if (mystrtod(&p, &val)) {
                                val /= 100;
-                               render_context.scale_x = (val - 1.) * pwr + 1.;
+                               render_context.scale_x = render_context.scale_x * ( 1 - pwr) + val * pwr;
                        } else
                                render_context.scale_x = render_context.style->ScaleX;
                } else if (tp == 'y') {
                        if (mystrtod(&p, &val)) {
                                val /= 100;
-                               render_context.scale_y = (val - 1.) * pwr + 1.;
+                               render_context.scale_y = render_context.scale_y * ( 1 - pwr) + val * pwr;
                        } else
                                render_context.scale_y = render_context.style->ScaleY;
                }
        } else if (mystrcmp(&p, "fsp")) {
-               int val;
-               if (mystrtoi(&p, 10, &val))
-                       render_context.hspacing = val * pwr;
+               double val;
+               if (mystrtod(&p, &val))
+                       render_context.hspacing = render_context.hspacing * ( 1 - pwr ) + val * pwr;
                else
-                       render_context.hspacing = 0;
+                       render_context.hspacing = render_context.style->Spacing;
        } else if (mystrcmp(&p, "fs")) {
                int val;
                if (mystrtoi(&p, 10, &val))
@@ -766,24 +774,37 @@ static char* parse_tag(char* p, double pwr) {
                render_context.pos_y = y;
                render_context.detect_collisions = 0;
                render_context.evt_type = EVENT_POSITIONED;
-       } else if (mystrcmp(&p, "frx") || mystrcmp(&p, "fry")) {
+       } else if (mystrcmp(&p, "frx")) {
+               double val;
+               if (mystrtod(&p, &val)) {
+                       val *= M_PI / 180;
+                       render_context.frx = val * pwr + render_context.frx * (1-pwr);
+               } else
+                       render_context.frx = 0.;
+       } else if (mystrcmp(&p, "fry")) {
                double val;
-               mystrtod(&p, &val);
-               mp_msg(MSGT_ASS, MSGL_V, "frx/fry unimplemented \n");
+               if (mystrtod(&p, &val)) {
+                       val *= M_PI / 180;
+                       render_context.fry = val * pwr + render_context.fry * (1-pwr);
+               } else
+                       render_context.fry = 0.;
        } else if (mystrcmp(&p, "frz") || mystrcmp(&p, "fr")) {
-               double angle;
                double val;
-               mystrtod(&p, &val);
-               mp_msg(MSGT_ASS, MSGL_DBG2, "setting rotation to %.2f\n", val * pwr);
-               angle = M_PI * val / 180;
-               render_context.rotation = angle * pwr;
+               if (mystrtod(&p, &val)) {
+                       val *= M_PI / 180;
+                       render_context.frz = val * pwr + render_context.frz * (1-pwr);
+               } else
+                       render_context.frz = M_PI * render_context.style->Angle / 180.;
        } else if (mystrcmp(&p, "fn")) {
                char* start = p;
                char* family;
                skip_all('\\');
-               family = malloc(p - start + 1);
-               strncpy(family, start, p - start);
-               family[p - start] = '\0';
+               if (p > start) {
+                       family = malloc(p - start + 1);
+                       strncpy(family, start, p - start);
+                       family[p - start] = '\0';
+               } else
+                       family = strdup(render_context.style->FontName);
                if (render_context.family)
                        free(render_context.family);
                render_context.family = family;
@@ -803,17 +824,23 @@ static char* parse_tag(char* p, double pwr) {
                }
                // FIXME: simplify
        } else if (mystrcmp(&p, "an")) {
-               int val = strtol(p, &p, 10);
-               int v = (val - 1) / 3; // 0, 1 or 2 for vertical alignment
-               mp_msg(MSGT_ASS, MSGL_DBG2, "an %d\n", val);
-               if (v != 0) v = 3 - v;
-               val = ((val - 1) % 3) + 1; // horizontal alignment
-               val += v*4;
-               mp_msg(MSGT_ASS, MSGL_DBG2, "align %d\n", val);
-               render_context.alignment = val;
+               int val;
+               if (mystrtoi(&p, 10, &val) && val) {
+                       int v = (val - 1) / 3; // 0, 1 or 2 for vertical alignment
+                       mp_msg(MSGT_ASS, MSGL_DBG2, "an %d\n", val);
+                       if (v != 0) v = 3 - v;
+                       val = ((val - 1) % 3) + 1; // horizontal alignment
+                       val += v*4;
+                       mp_msg(MSGT_ASS, MSGL_DBG2, "align %d\n", val);
+                       render_context.alignment = val;
+               } else
+                       render_context.alignment = render_context.style->Alignment;
        } else if (mystrcmp(&p, "a")) {
-               int val = strtol(p, &p, 10);
-               render_context.alignment = val;
+               int val;
+               if (mystrtoi(&p, 10, &val) && val)
+                       render_context.alignment = val;
+               else
+                       render_context.alignment = render_context.style->Alignment;
        } else if (mystrcmp(&p, "pos")) {
                int v1, v2;
                skip('(');
@@ -871,6 +898,7 @@ static char* parse_tag(char* p, double pwr) {
                //                              render_context.evt_type = EVENT_POSITIONED;
                render_context.org_x = v1;
                render_context.org_y = v2;
+               render_context.have_origin = 1;
        } else if (mystrcmp(&p, "t")) {
                double v[3];
                int v1, v2;
@@ -901,11 +929,14 @@ static char* parse_tag(char* p, double pwr) {
                if (v3 < 0.)
                        v3 = 0.;
                t = frame_context.time - render_context.event->Start; // FIXME: move to render_context
-               if (t < t1)
+               if (t <= t1)
                        k = 0.;
-               else if (t > t2)
+               else if (t >= t2)
                        k = 1.;
-               else k = pow(((double)(t - t1)) / delta_t, v3);
+               else {
+                       assert(delta_t != 0.);
+                       k = pow(((double)(t - t1)) / delta_t, v3);
+               }
                while (*p == '\\')
                        p = parse_tag(p, k); // maybe k*pwr ? no, specs forbid nested \t's 
                skip_all(')'); // FIXME: better skip(')'), but much more tags support required
@@ -968,17 +999,19 @@ static char* parse_tag(char* p, double pwr) {
                        render_context.be = 0;
        } else if (mystrcmp(&p, "b")) {
                int b;
-               if (mystrtoi(&p, 10, &b))
-                       render_context.bold = b;
-               else
-                       render_context.bold = - render_context.style->Bold;
+               if (mystrtoi(&p, 10, &b)) {
+                       if (pwr >= .5)
+                               render_context.bold = b;
+               } else
+                       render_context.bold = render_context.style->Bold;
                update_font();
        } else if (mystrcmp(&p, "i")) {
                int i;
-               if (mystrtoi(&p, 10, &i))
-                       render_context.italic = i;
-               else
-                       render_context.italic = - render_context.style->Italic;
+               if (mystrtoi(&p, 10, &i)) {
+                       if (pwr >= .5)
+                               render_context.italic = i;
+               } else
+                       render_context.italic = render_context.style->Italic;
                update_font();
        } else if (mystrcmp(&p, "kf") || mystrcmp(&p, "K")) {
                int val = strtol(p, &p, 10);
@@ -1140,16 +1173,18 @@ static void reset_render_context(void)
        if (render_context.family)
                free(render_context.family);
        render_context.family = strdup(render_context.style->FontName);
-       render_context.bold = render_context.style->Bold;
-       render_context.italic = render_context.style->Italic;
+       render_context.bold = render_context.style->Bold;
+       render_context.italic = render_context.style->Italic;
        update_font();
 
        change_border(-1.);
        render_context.scale_x = render_context.style->ScaleX;
        render_context.scale_y = render_context.style->ScaleY;
-       render_context.hspacing = 0; // FIXME
+       render_context.hspacing = render_context.style->Spacing;
        render_context.be = 0;
        render_context.shadow = render_context.style->Shadow;
+       render_context.frx = render_context.fry = 0.;
+       render_context.frz = M_PI * render_context.style->Angle / 180.;
 
        // FIXME: does not reset unsupported attributes.
 }
@@ -1165,12 +1200,12 @@ static void init_render_context(ass_event_t* event)
        reset_render_context();
 
        render_context.evt_type = EVENT_NORMAL;
-       render_context.alignment = 0;
-       render_context.rotation = M_PI * render_context.style->Angle / 180.;
+       render_context.alignment = render_context.style->Alignment;
        render_context.pos_x = 0;
        render_context.pos_y = 0;
        render_context.org_x = 0;
        render_context.org_y = 0;
+       render_context.have_origin = 0;
        render_context.clip_x0 = 0;
        render_context.clip_y0 = 0;
        render_context.clip_x1 = frame_context.track->PlayResX;
@@ -1188,6 +1223,57 @@ static void free_render_context(void)
 {
 }
 
+static void get_outline_glyph(int symbol, glyph_info_t* info, FT_Vector* advance)
+{
+       int error;
+       glyph_hash_val_t* val;
+       glyph_hash_key_t key;
+       key.font = render_context.font;
+       key.size = render_context.font_size;
+       key.ch = symbol;
+       key.scale_x = (render_context.scale_x * 0xFFFF);
+       key.scale_y = (render_context.scale_y * 0xFFFF);
+       key.advance = *advance;
+       key.bold = render_context.bold;
+       key.italic = render_context.italic;
+
+       info->glyph = info->outline_glyph = 0;
+
+       val = cache_find_glyph(&key);
+       if (val) {
+               FT_Glyph_Copy(val->glyph, &info->glyph);
+               if (val->outline_glyph)
+                       FT_Glyph_Copy(val->outline_glyph, &info->outline_glyph);
+               info->bbox = val->bbox_scaled;
+               info->advance.x = val->advance.x;
+               info->advance.y = val->advance.y;
+       } else {
+               glyph_hash_val_t v;
+               info->glyph = ass_font_get_glyph(frame_context.ass_priv->fontconfig_priv, render_context.font, symbol);
+               if (!info->glyph)
+                       return;
+               info->advance.x = d16_to_d6(info->glyph->advance.x);
+               info->advance.y = d16_to_d6(info->glyph->advance.y);
+               FT_Glyph_Get_CBox( info->glyph, FT_GLYPH_BBOX_PIXELS, &info->bbox);
+
+               if (render_context.stroker) {
+                       info->outline_glyph = info->glyph;
+                       error = FT_Glyph_StrokeBorder( &(info->outline_glyph), render_context.stroker, 0 , 0 ); // don't destroy original
+                       if (error) {
+                               mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FT_Glyph_Stroke_Error, error);
+                       }
+               }
+
+               memset(&v, 0, sizeof(v));
+               FT_Glyph_Copy(info->glyph, &v.glyph);
+               if (info->outline_glyph)
+                       FT_Glyph_Copy(info->outline_glyph, &v.outline_glyph);
+               v.advance = info->advance;
+               v.bbox_scaled = info->bbox;
+               cache_add_glyph(&key, &v);
+       }
+}
+
 /**
  * \brief Get normal and outline glyphs from cache (if possible) or font face
  * \param index face glyph index
@@ -1196,62 +1282,20 @@ static void free_render_context(void)
  * \param advance advance vector of the extracted glyph
  * \return 0 on success
  */
-static int get_glyph(int symbol, glyph_info_t* info, FT_Vector* advance)
+static void get_bitmap_glyph(glyph_info_t* info)
 {
-       int error;
-       glyph_hash_val_t* val;
-       glyph_hash_key_t* key = &(info->hash_key);
+       bitmap_hash_val_t* val;
+       bitmap_hash_key_t* key = &info->hash_key;
        
-       key->font = render_context.font;
-       key->size = render_context.font_size;
-       key->ch = symbol;
-       key->outline = (render_context.border * 0xFFFF); // convert to 16.16
-       key->scale_x = (render_context.scale_x * 0xFFFF);
-       key->scale_y = (render_context.scale_y * 0xFFFF);
-       key->angle = (render_context.rotation * 0xFFFF);
-       key->advance = *advance;
-       key->bold = render_context.bold;
-       key->italic = render_context.italic;
-       key->be = render_context.be;
-
-       val = cache_find_glyph(key);
-//     val = 0;
+       val = cache_find_bitmap(key);
+/*     val = 0; */
        
        if (val) {
-               info->glyph = info->outline_glyph = 0;
                info->bm = val->bm;
                info->bm_o = val->bm_o;
                info->bm_s = val->bm_s;
-               info->bbox = val->bbox_scaled;
-               info->advance.x = val->advance.x;
-               info->advance.y = val->advance.y;
-
-               return 0;
-       }
-
-       // not found, get a new outline glyph from face
-//     mp_msg(MSGT_ASS, MSGL_INFO, "miss, index = %d, symbol = %c, adv = (%d, %d)\n", index, symbol, advance->x, advance->y);
-
-       info->glyph = ass_font_get_glyph(frame_context.ass_priv->fontconfig_priv, render_context.font, symbol);
-       if (!info->glyph)
-               return 0;
-
-       info->advance.x = info->glyph->advance.x >> 10;
-       info->advance.y = info->glyph->advance.y >> 10;
-
-       if (render_context.stroker) {
-               info->outline_glyph = info->glyph;
-               error = FT_Glyph_Stroke( &(info->outline_glyph), render_context.stroker, 0 ); // don't destroy original
-               if (error) {
-                       mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_FT_Glyph_Stroke_Error, error);
-               }
-       } else {
-               info->outline_glyph = 0;
-       }
-
-       info->bm = info->bm_o = info->bm_s = 0;
-
-       return 0;
+       } else
+               info->bm = info->bm_o = info->bm_s = 0;
 }
 
 /**
@@ -1263,7 +1307,7 @@ static int get_glyph(int symbol, glyph_info_t* info, FT_Vector* advance)
  *   lines[].asc
  *   lines[].desc
  */
-static void measure_text()
+static void measure_text(void)
 {
        int cur_line = 0, max_asc = 0, max_desc = 0;
        int i;
@@ -1279,9 +1323,9 @@ static void measure_text()
                if (i < text_info.length) {
                        glyph_info_t* cur = text_info.glyphs + i;
                        if (cur->asc > max_asc)
-                               max_asc = cur->asc * render_context.scale_y;
+                               max_asc = cur->asc;
                        if (cur->desc > max_desc)
-                               max_desc = cur->desc * render_context.scale_y;
+                               max_desc = cur->desc;
                }
        }
 }
@@ -1355,6 +1399,11 @@ static void wrap_lines_smart(int max_text_width)
                
                if (cur->symbol == ' ')
                        last_space = i;
+
+               // make sure the hard linebreak is not forgotten when
+               // there was a new soft linebreak just inserted
+               if (cur->symbol == '\n' && break_type == 1)
+                       i--;
        }
 #define DIFF(x,y) (((x) < (y)) ? (y - x) : (x - y))
        exit = 0;
@@ -1409,7 +1458,7 @@ static void wrap_lines_smart(int max_text_width)
                        int height = text_info.lines[cur_line - 1].desc + text_info.lines[cur_line].asc;
                        cur_line ++;
                        pen_shift_x = - cur->pos.x;
-                       pen_shift_y += (height >> 6) + global_settings->line_spacing;
+                       pen_shift_y += d6_to_int(height) + global_settings->line_spacing;
                        mp_msg(MSGT_ASS, MSGL_DBG2, "shifting from %d to %d by (%d, %d)\n", i, text_info.length - 1, pen_shift_x, pen_shift_y);
                }
                cur->pos.x += pen_shift_x;
@@ -1484,26 +1533,6 @@ static void process_karaoke_effects(void)
        }
 }
 
-static int get_face_ascender(FT_Face face)
-{
-       int v = face->size->metrics.ascender;
-       int v2 = FT_MulFix(face->bbox.yMax, face->size->metrics.y_scale);
-       if (v > v2 * 0.9)
-               return v;
-       else
-               return v2;
-}
-
-static int get_face_descender(FT_Face face)
-{
-       int v = - face->size->metrics.descender;
-       int v2 = - FT_MulFix(face->bbox.yMin, face->size->metrics.y_scale);
-       if (v > v2 * 0.9)
-               return v;
-       else
-               return v2;
-}
-
 /**
  * \brief Calculate base point for positioning and rotation
  * \param bbox text bbox
@@ -1541,6 +1570,110 @@ static void get_base_point(FT_BBox bbox, int alignment, int* bx, int* by)
 }
 
 /**
+ * \brief Multiply 4-vector by 4-matrix
+ * \param a 4-vector
+ * \param m 4-matrix]
+ * \param b out: 4-vector
+ * Calculates a * m and stores result in b
+ */
+static inline void transform_point_3d(double *a, double *m, double *b)
+{
+       b[0] = a[0] * m[0] + a[1] * m[4] + a[2] * m[8] +  a[3] * m[12];
+       b[1] = a[0] * m[1] + a[1] * m[5] + a[2] * m[9] +  a[3] * m[13];
+       b[2] = a[0] * m[2] + a[1] * m[6] + a[2] * m[10] + a[3] * m[14];
+       b[3] = a[0] * m[3] + a[1] * m[7] + a[2] * m[11] + a[3] * m[15];
+}
+
+/**
+ * \brief Apply 3d transformation to a vector
+ * \param v FreeType vector (2d)
+ * \param m 4-matrix
+ * Transforms v by m, projects the result back to the screen plane
+ * Result is returned in v.
+ */
+static inline void transform_vector_3d(FT_Vector* v, double *m) {
+       const double camera = 2500 * frame_context.border_scale; // camera distance
+       double a[4], b[4];
+       a[0] = d6_to_double(v->x);
+       a[1] = d6_to_double(v->y);
+       a[2] = 0.;
+       a[3] = 1.;
+       transform_point_3d(a, m, b);
+       /* Apply perspective projection with the following matrix:
+          2500     0     0     0
+             0  2500     0     0
+             0     0     0     0
+             0     0     8     2500
+          where 2500 is camera distance, 8 - z-axis scale.
+          Camera is always located in (org_x, org_y, -2500). This means
+          that different subtitle events can be displayed at the same time
+          using different cameras. */
+       b[0] *= camera;
+       b[1] *= camera;
+       b[3] = 8 * b[2] + camera;
+       if (b[3] < 0.001 && b[3] > -0.001)
+               b[3] = b[3] < 0. ? -0.001 : 0.001;
+       v->x = double_to_d6(b[0] / b[3]);
+       v->y = double_to_d6(b[1] / b[3]);
+}
+
+/**
+ * \brief Apply 3d transformation to a glyph
+ * \param glyph FreeType glyph
+ * \param m 4-matrix
+ * Transforms glyph by m, projects the result back to the screen plane
+ * Result is returned in glyph.
+ */
+static inline void transform_glyph_3d(FT_Glyph glyph, double *m, FT_Vector shift) {
+       int i;
+       FT_Outline* outline = &((FT_OutlineGlyph)glyph)->outline;
+       FT_Vector* p = outline->points;
+
+       for (i=0; i<outline->n_points; i++) {
+               p[i].x += shift.x;
+               p[i].y += shift.y;
+               transform_vector_3d(p + i, m);
+               p[i].x -= shift.x;
+               p[i].y -= shift.y;
+       }
+
+       //transform_vector_3d(&glyph->advance, m);
+}
+
+/**
+ * \brief Apply 3d transformation to several objects
+ * \param shift FreeType vector
+ * \param glyph FreeType glyph
+ * \param glyph2 FreeType glyph
+ * \param frx x-axis rotation angle
+ * \param fry y-axis rotation angle
+ * \param frz z-axis rotation angle
+ * Rotates both glyphs by frx, fry and frz. Shift vector is added before rotation and subtracted after it.
+ */
+void transform_3d(FT_Vector shift, FT_Glyph* glyph, FT_Glyph* glyph2, double frx, double fry, double frz)
+{
+       if (frx != 0. || fry != 0. || frz != 0.) {
+               double m[16];
+               double sx = sin(frx);
+               double sy = sin(fry);
+               double sz = sin(frz);
+               double cx = cos(frx);
+               double cy = cos(fry);
+               double cz = cos(frz);
+               m[0] = cy * cz;   m[1] = cz*sx*sy + sz*cx;   m[2]  = sz*sx - cz*cx*sy;   m[3] = 0.0;
+               m[4] = -sz*cy;    m[5] = cz*cx - sz*sy*sx;   m[6]  = sz*sy*cx + cz*sx;   m[7] = 0.0;
+               m[8] = sy;        m[9] = -sx*cy;             m[10] = cx*cy;              m[11] = 0.0;
+               m[12] = 0.0;      m[13] = 0.0;               m[14] = 0.0;                m[15] = 1.0;
+
+               if (glyph && *glyph)
+                       transform_glyph_3d(*glyph, m, shift);
+
+               if (glyph2 && *glyph2)
+                       transform_glyph_3d(*glyph2, m, shift);
+       }
+}
+
+/**
  * \brief Main ass rendering function, glues everything together
  * \param event event to render
  * Process event, appending resulting ass_image_t's to images_root.
@@ -1548,12 +1681,9 @@ static void get_base_point(FT_BBox bbox, int alignment, int* bx, int* by)
 static int ass_render_event(ass_event_t* event, event_images_t* event_images)
 {
        char* p;
-       FT_UInt glyph_index; 
-       FT_Bool use_kerning; 
        FT_UInt previous; 
        FT_UInt num_glyphs;
        FT_Vector pen;
-       int error;
        unsigned code;
        FT_BBox bbox;
        int i, j;
@@ -1595,19 +1725,15 @@ static int ass_render_event(ass_event_t* event, event_images_t* event_images)
                if (code == 0)
                        break;
 
-               use_kerning = FT_HAS_KERNING(render_context.font->face);
-
                if (text_info.length >= MAX_GLYPHS) {
                        mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_MAX_GLYPHS_Reached, 
                                        (int)(event - frame_context.track->events), event->Start, event->Duration, event->Text);
                        break;
                }
 
-               glyph_index = FT_Get_Char_Index( render_context.font->face, code);
-
-               if ( use_kerning && previous && glyph_index ) {
+               if ( previous && code ) {
                        FT_Vector delta;
-                       FT_Get_Kerning( render_context.font->face, previous, glyph_index, FT_KERNING_DEFAULT, &delta );
+                       delta = ass_font_get_kerning(render_context.font, previous, code);
                        pen.x += delta.x * render_context.scale_x;
                        pen.y += delta.y * render_context.scale_y;
                }
@@ -1624,28 +1750,18 @@ static int ass_render_event(ass_event_t* event, event_images_t* event_images)
 
                        ass_font_set_transform(render_context.font, &matrix, &shift );
                }
-               
-               error = get_glyph(code, text_info.glyphs + text_info.length, &shift);
 
-               if (error) {
-                       continue;
-               }
+               get_outline_glyph(code, text_info.glyphs + text_info.length, &shift);
                
                text_info.glyphs[text_info.length].pos.x = pen.x >> 6;
                text_info.glyphs[text_info.length].pos.y = pen.y >> 6;
                
                pen.x += text_info.glyphs[text_info.length].advance.x;
-               pen.x += render_context.hspacing;
+               pen.x += double_to_d6(render_context.hspacing);
                pen.y += text_info.glyphs[text_info.length].advance.y;
                
-               // if it's an outline glyph, we still need to fill the bbox
-               if (text_info.glyphs[text_info.length].glyph) {
-                       FT_Glyph_Get_CBox( text_info.glyphs[text_info.length].glyph, FT_GLYPH_BBOX_PIXELS, &(text_info.glyphs[text_info.length].bbox) );
-               }
+               previous = code;
 
-               
-               previous = glyph_index;
-               
                text_info.glyphs[text_info.length].symbol = code;
                text_info.glyphs[text_info.length].linebreak = 0;
                for (i = 0; i < 4; ++i) {
@@ -1656,11 +1772,31 @@ static int ass_render_event(ass_event_t* event, event_images_t* event_images)
                text_info.glyphs[text_info.length].effect_type = render_context.effect_type;
                text_info.glyphs[text_info.length].effect_timing = render_context.effect_timing;
                text_info.glyphs[text_info.length].effect_skip_timing = render_context.effect_skip_timing;
-               text_info.glyphs[text_info.length].asc = get_face_ascender(render_context.font->face);
-               text_info.glyphs[text_info.length].desc = get_face_descender(render_context.font->face);
                text_info.glyphs[text_info.length].be = render_context.be;
                text_info.glyphs[text_info.length].shadow = render_context.shadow;
-               text_info.glyphs[text_info.length].frz = render_context.rotation;
+               text_info.glyphs[text_info.length].frx = render_context.frx;
+               text_info.glyphs[text_info.length].fry = render_context.fry;
+               text_info.glyphs[text_info.length].frz = render_context.frz;
+               ass_font_get_asc_desc(render_context.font, code,
+                                     &text_info.glyphs[text_info.length].asc,
+                                     &text_info.glyphs[text_info.length].desc);
+               text_info.glyphs[text_info.length].asc *= render_context.scale_y;
+               text_info.glyphs[text_info.length].desc *= render_context.scale_y;
+
+               // fill bitmap_hash_key
+               text_info.glyphs[text_info.length].hash_key.font = render_context.font;
+               text_info.glyphs[text_info.length].hash_key.size = render_context.font_size;
+               text_info.glyphs[text_info.length].hash_key.outline = render_context.border * 0xFFFF;
+               text_info.glyphs[text_info.length].hash_key.scale_x = render_context.scale_x * 0xFFFF;
+               text_info.glyphs[text_info.length].hash_key.scale_y = render_context.scale_y * 0xFFFF;
+               text_info.glyphs[text_info.length].hash_key.frx = render_context.frx * 0xFFFF;
+               text_info.glyphs[text_info.length].hash_key.fry = render_context.fry * 0xFFFF;
+               text_info.glyphs[text_info.length].hash_key.frz = render_context.frz * 0xFFFF;
+               text_info.glyphs[text_info.length].hash_key.bold = render_context.bold;
+               text_info.glyphs[text_info.length].hash_key.italic = render_context.italic;
+               text_info.glyphs[text_info.length].hash_key.ch = code;
+               text_info.glyphs[text_info.length].hash_key.advance = shift;
+               text_info.glyphs[text_info.length].hash_key.be = render_context.be;
 
                text_info.length++;
 
@@ -1680,8 +1816,6 @@ static int ass_render_event(ass_event_t* event, event_images_t* event_images)
        
        // alignments
        alignment = render_context.alignment;
-       if (!alignment)
-               alignment = render_context.style->Alignment;
        halign = alignment & 3;
        valign = alignment & 12;
 
@@ -1702,16 +1836,16 @@ static int ass_render_event(ass_event_t* event, event_images_t* event_images)
                last_break = -1;
                for (i = 1; i < text_info.length + 1; ++i) { // (text_info.length + 1) is the end of the last line
                        if ((i == text_info.length) || text_info.glyphs[i].linebreak) {
-                               int width, shift;
+                               int width, shift = 0;
                                glyph_info_t* first_glyph = text_info.glyphs + last_break + 1;
                                glyph_info_t* last_glyph = text_info.glyphs + i - 1;
 
                                while ((last_glyph > first_glyph) && ((last_glyph->symbol == '\n') || (last_glyph->symbol == 0)))
                                        last_glyph --;
 
-                               width = last_glyph->pos.x + last_glyph->bbox.xMax - first_glyph->pos.x - first_glyph->bbox.xMin;
-                               shift = - first_glyph->bbox.xMin; // now text line starts exactly at 0 (left margin)
+                               width = last_glyph->pos.x + d6_to_int(last_glyph->advance.x) - first_glyph->pos.x;
                                if (halign == HALIGN_LEFT) { // left aligned, no action
+                                       shift = 0;
                                } else if (halign == HALIGN_RIGHT) { // right aligned
                                        shift = max_text_width - width;
                                } else if (halign == HALIGN_CENTER) { // centered
@@ -1747,7 +1881,7 @@ static int ass_render_event(ass_event_t* event, event_images_t* event_images)
        if (render_context.evt_type == EVENT_NORMAL ||
            render_context.evt_type == EVENT_HSCROLL) {
                if (valign == VALIGN_TOP) { // toptitle
-                       device_y = y2scr_top(MarginV) + (text_info.lines[0].asc >> 6);
+                       device_y = y2scr_top(MarginV) + d6_to_int(text_info.lines[0].asc);
                } else if (valign == VALIGN_CENTER) { // midtitle
                        int scr_y = y2scr(frame_context.track->PlayResY / 2);
                        device_y = scr_y - (bbox.yMax - bbox.yMin) / 2;
@@ -1757,8 +1891,8 @@ static int ass_render_event(ass_event_t* event, event_images_t* event_images)
                                mp_msg(MSGT_ASS, MSGL_V, "Invalid valign, supposing 0 (subtitle)\n");
                        scr_y = y2scr_sub(frame_context.track->PlayResY - MarginV);
                        device_y = scr_y;
-                       device_y -= (text_info.height >> 6);
-                       device_y += (text_info.lines[0].asc >> 6);
+                       device_y -= d6_to_int(text_info.height);
+                       device_y += d6_to_int(text_info.lines[0].asc);
                }
        } else if (render_context.evt_type == EVENT_VSCROLL) {
                if (render_context.scroll_direction == SCROLL_TB)
@@ -1800,13 +1934,11 @@ static int ass_render_event(ass_event_t* event, event_images_t* event_images)
 
        // rotate glyphs if needed
        {
-               double angle = 0.;
                FT_Vector center;
-               FT_Matrix matrix_rotate;
                
-               if (((render_context.org_x != 0) || (render_context.org_y != 0)) && (render_context.evt_type == EVENT_POSITIONED)) {
-                       center.x = render_context.org_x;
-                       center.y = render_context.org_y;
+               if (render_context.have_origin) {
+                       center.x = x2scr(render_context.org_x);
+                       center.y = y2scr(render_context.org_y);
                } else {
                        int bx, by;
                        get_base_point(bbox, alignment, &bx, &by);
@@ -1815,45 +1947,30 @@ static int ass_render_event(ass_event_t* event, event_images_t* event_images)
                }
 
                for (i = 0; i < text_info.length; ++i) {
-                       FT_Vector start;
-                       FT_Vector start_old;
+                       FT_Vector shift;
                        glyph_info_t* info = text_info.glyphs + i;
 
-                       if (info->frz < 0.00001 && info->frz > -0.00001)
-                               continue;
-                       
-                       if (info->frz != angle) {
-                               angle = info->frz;
-                               matrix_rotate.xx = (FT_Fixed)( cos( angle ) * 0x10000L );
-                               matrix_rotate.xy = (FT_Fixed)( -sin( angle ) * 0x10000L );
-                               matrix_rotate.yx = (FT_Fixed)( sin( angle ) * 0x10000L );
-                               matrix_rotate.yy = (FT_Fixed)( cos( angle ) * 0x10000L );
+                       if (info->hash_key.frx || info->hash_key.fry || info->hash_key.frz) {
+                               info->hash_key.shift_x = info->pos.x + device_x - center.x;
+                               info->hash_key.shift_y = - (info->pos.y + device_y - center.y);
+                       } else {
+                               info->hash_key.shift_x = 0;
+                               info->hash_key.shift_y = 0;
                        }
+                       get_bitmap_glyph(info);
 
-                       // calculating shift vector
-                       // shift = (position - center)*M - (position - center)
-                       start.x = (info->pos.x + device_x - center.x) << 6;
-                       start.y = - (info->pos.y + device_y - center.y) << 6;
-                       start_old.x = start.x;
-                       start_old.y = start.y;
-
-                       FT_Vector_Transform(&start, &matrix_rotate);
-                       
-                       start.x -= start_old.x;
-                       start.y -= start_old.y;
-
-                       info->pos.x += start.x >> 6;
-                       info->pos.y -= start.y >> 6;
-
-                       if (info->glyph)
-                               FT_Glyph_Transform( info->glyph, &matrix_rotate, 0 );
-                       if (info->outline_glyph)
-                               FT_Glyph_Transform( info->outline_glyph, &matrix_rotate, 0 );
+                       if (info->bm == 0) {
+                               // calculating shift vector
+                               shift.x = int_to_d6(info->pos.x + device_x - center.x);
+                               shift.y = - int_to_d6(info->pos.y + device_y - center.y);
+
+                               transform_3d(shift, &info->glyph, &info->outline_glyph, info->frx, info->fry, info->frz);
+                       }
                }
        }
 
-       event_images->top = device_y - (text_info.lines[0].asc >> 6);
-       event_images->height = text_info.height >> 6;
+       event_images->top = device_y - d6_to_int(text_info.lines[0].asc);
+       event_images->height = d6_to_int(text_info.height);
        event_images->detect_collisions = render_context.detect_collisions;
        event_images->shift_direction = (valign == VALIGN_TOP) ? 1 : -1;
        event_images->event = event;
@@ -1864,10 +1981,26 @@ static int ass_render_event(ass_event_t* event, event_images_t* event_images)
        return 0;
 }
 
+/**
+ * \brief deallocate image list
+ * \param img list pointer
+ */
+void ass_free_images(ass_image_t* img)
+{
+       while (img) {
+               ass_image_t* next = img->next;
+               free(img);
+               img = next;
+       }
+}
+
 static void ass_reconfigure(ass_renderer_t* priv)
 {
        priv->render_id = ++last_render_id;
        ass_glyph_cache_reset();
+       ass_bitmap_cache_reset();
+       ass_free_images(priv->prev_images_root);
+       priv->prev_images_root = 0;
 }
 
 void ass_set_frame_size(ass_renderer_t* priv, int w, int h)
@@ -1928,7 +2061,7 @@ int ass_set_fonts(ass_renderer_t* priv, const char* default_font, const char* de
 
        if (priv->fontconfig_priv)
                fontconfig_done(priv->fontconfig_priv);
-       priv->fontconfig_priv = fontconfig_init(priv->library->fonts_dir, default_family, default_font);
+       priv->fontconfig_priv = fontconfig_init(priv->library, priv->ftlibrary, default_family, default_font);
 
        return !!priv->fontconfig_priv;
 }
@@ -1938,8 +2071,6 @@ int ass_set_fonts(ass_renderer_t* priv, const char* default_font, const char* de
  */
 static int ass_start_frame(ass_renderer_t *priv, ass_track_t* track, long long now)
 {
-       ass_image_t* img;
-
        ass_renderer = priv;
        global_settings = &priv->settings;
 
@@ -1965,12 +2096,7 @@ static int ass_start_frame(ass_renderer_t *priv, ass_track_t* track, long long n
        else
                frame_context.font_scale_x = ((double)(frame_context.orig_width * track->PlayResY)) / (frame_context.orig_height * track->PlayResX);
 
-       img = priv->images_root;
-       while (img) {
-               ass_image_t* next = img->next;
-               free(img);
-               img = next;
-       }
+       priv->prev_images_root = priv->images_root;
        priv->images_root = 0;
 
        return 0;
@@ -1984,10 +2110,6 @@ static int cmp_event_layer(const void* p1, const void* p2)
                return -1;
        if (e1->Layer > e2->Layer)
                return 1;
-       if (e1->Start < e2->Start)
-               return -1;
-       if (e1->Start > e2->Start)
-               return 1;
        if (e1->ReadOrder < e2->ReadOrder)
                return -1;
        if (e1->ReadOrder > e2->ReadOrder)
@@ -2134,15 +2256,72 @@ static void fix_collisions(event_images_t* imgs, int cnt)
 }
 
 /**
+ * \brief compare two images
+ * \param i1 first image
+ * \param i2 second image
+ * \return 0 if identical, 1 if different positions, 2 if different content
+ */
+int ass_image_compare(ass_image_t *i1, ass_image_t *i2)
+{
+       if (i1->w != i2->w) return 2;
+       if (i1->h != i2->h) return 2;
+       if (i1->stride != i2->stride) return 2;
+       if (i1->color != i2->color) return 2;
+       if (i1->bitmap != i2->bitmap)
+               return 2;
+       if (i1->dst_x != i2->dst_x) return 1;
+       if (i1->dst_y != i2->dst_y) return 1;
+       return 0;
+}
+
+/**
+ * \brief compare current and previous image list
+ * \param priv library handle
+ * \return 0 if identical, 1 if different positions, 2 if different content
+ */
+int ass_detect_change(ass_renderer_t *priv)
+{
+       ass_image_t* img, *img2;
+       int diff;
+
+       img = priv->prev_images_root;
+       img2 = priv->images_root;
+       diff = 0;
+       while (img && diff < 2) {
+               ass_image_t* next, *next2;
+               next = img->next;
+               if (img2) {
+                       int d = ass_image_compare(img, img2);
+                       if (d > diff) diff = d;
+                       next2 = img2->next;
+               } else {
+                       // previous list is shorter
+                       diff = 2;
+                       break;
+               }
+               img = next;
+               img2 = next2;
+       }
+
+       // is the previous list longer?
+       if (img2)
+               diff = 2;
+
+       return diff;
+}
+
+/**
  * \brief render a frame
  * \param priv library handle
  * \param track track
  * \param now current video timestamp (ms)
+ * \param detect_change a value describing how the new images differ from the previous ones will be written here:
+ *        0 if identical, 1 if different positions, 2 if different content.
+ *        Can be NULL, in that case no detection is performed.
  */
-ass_image_t* ass_render_frame(ass_renderer_t *priv, ass_track_t* track, long long now)
+ass_image_t* ass_render_frame(ass_renderer_t *priv, ass_track_t* track, long long now, int* detect_change)
 {
        int i, cnt, rc;
-       event_images_t eimg[MAX_EVENTS];
        event_images_t* last;
        ass_image_t** tail;
        
@@ -2156,40 +2335,46 @@ ass_image_t* ass_render_frame(ass_renderer_t *priv, ass_track_t* track, long lon
        for (i = 0; i < track->n_events; ++i) {
                ass_event_t* event = track->events + i;
                if ( (event->Start <= now) && (now < (event->Start + event->Duration)) ) {
-                       if (cnt < MAX_EVENTS) {
-                               rc = ass_render_event(event, eimg + cnt);
-                               if (!rc) ++cnt;
-                       } else {
-                               mp_msg(MSGT_ASS, MSGL_WARN, MSGTR_LIBASS_TooManySimultaneousEvents);
-                               break;
+                       if (cnt >= priv->eimg_size) {
+                               priv->eimg_size += 100;
+                               priv->eimg = realloc(priv->eimg, priv->eimg_size * sizeof(event_images_t));
                        }
+                       rc = ass_render_event(event, priv->eimg + cnt);
+                       if (!rc) ++cnt;
                }
        }
 
        // sort by layer
-       qsort(eimg, cnt, sizeof(event_images_t), cmp_event_layer);
+       qsort(priv->eimg, cnt, sizeof(event_images_t), cmp_event_layer);
 
        // call fix_collisions for each group of events with the same layer
-       last = eimg;
+       last = priv->eimg;
        for (i = 1; i < cnt; ++i)
-               if (last->event->Layer != eimg[i].event->Layer) {
-                       fix_collisions(last, eimg + i - last);
-                       last = eimg + i;
+               if (last->event->Layer != priv->eimg[i].event->Layer) {
+                       fix_collisions(last, priv->eimg + i - last);
+                       last = priv->eimg + i;
                }
        if (cnt > 0)
-               fix_collisions(last, eimg + cnt - last);
+               fix_collisions(last, priv->eimg + cnt - last);
 
        // concat lists
        tail = &ass_renderer->images_root;
        for (i = 0; i < cnt; ++i) {
-               ass_image_t* cur = eimg[i].imgs;
+               ass_image_t* cur = priv->eimg[i].imgs;
                while (cur) {
                        *tail = cur;
                        tail = &cur->next;
                        cur = cur->next;
                }
        }
+
+       if (detect_change)
+               *detect_change = ass_detect_change(priv);
        
+       // free the previous image list
+       ass_free_images(priv->prev_images_root);
+       priv->prev_images_root = 0;
+
        return ass_renderer->images_root;
 }