]> granicus.if.org Git - libass/commitdiff
renderer: rewrite measure_text() to correctly account for leading newlines
authorDr.Smile <vabnick@gmail.com>
Sun, 2 Dec 2018 19:20:25 +0000 (22:20 +0300)
committerDr.Smile <vabnick@gmail.com>
Sun, 19 May 2019 17:21:29 +0000 (20:21 +0300)
libass/ass_render.c

index 0a7cd778b45d3467ca5fb6f553b0c83a742501db..e1cdb4f3e09dad03b849ee920260231442634211 100644 (file)
@@ -1285,40 +1285,31 @@ get_bitmap_glyph(ASS_Renderer *render_priv, GlyphInfo *info)
 static void measure_text(ASS_Renderer *render_priv)
 {
     TextInfo *text_info = &render_priv->text_info;
+    text_info->height = 0;
+
     int cur_line = 0;
-    double max_asc = 0., max_desc = 0.;
-    GlyphInfo *last = NULL;
-    int i;
-    int empty_line = 1;
-    text_info->height = 0.;
-    for (i = 0; i < text_info->length + 1; ++i) {
-        if ((i == text_info->length) || text_info->glyphs[i].linebreak) {
-            if (empty_line && cur_line > 0 && last) {
-                max_asc = d6_to_double(last->asc) / 2.0;
-                max_desc = d6_to_double(last->desc) / 2.0;
-            }
-            text_info->lines[cur_line].asc = max_asc;
-            text_info->lines[cur_line].desc = max_desc;
-            text_info->height += max_asc + max_desc;
+    double scale = 0.5 / 64;
+    int max_asc = 0, max_desc = 0;
+    for (int i = 0; i < text_info->length; i++) {
+        if (text_info->glyphs[i].linebreak) {
+            text_info->lines[cur_line].asc  = scale * max_asc;
+            text_info->lines[cur_line].desc = scale * max_desc;
+            text_info->height += scale * max_asc + scale * max_desc;
+            max_asc = max_desc = 0;
+            scale = 0.5 / 64;
             cur_line++;
-            max_asc = max_desc = 0.;
-            empty_line = 1;
-        }
-        if (i < text_info->length) {
-            GlyphInfo *cur = text_info->glyphs + i;
-            if (d6_to_double(cur->asc) > max_asc)
-                max_asc = d6_to_double(cur->asc);
-            if (d6_to_double(cur->desc) > max_desc)
-                max_desc = d6_to_double(cur->desc);
-            if (cur->symbol != '\n' && cur->symbol != 0) {
-                empty_line = 0;
-                last = cur;
-            }
         }
-    }
-    text_info->height +=
-        (text_info->n_lines -
-         1) * render_priv->settings.line_spacing;
+        GlyphInfo *cur = text_info->glyphs + i;
+        max_asc  = FFMAX(max_asc,  cur->asc);
+        max_desc = FFMAX(max_desc, cur->desc);
+        if (cur->symbol != '\n' && cur->symbol != 0)
+            scale = 1.0 / 64;
+    }
+    assert(cur_line == text_info->n_lines - 1);
+    text_info->lines[cur_line].asc  = scale * max_asc;
+    text_info->lines[cur_line].desc = scale * max_desc;
+    text_info->height += scale * max_asc + scale * max_desc;
+    text_info->height += cur_line * render_priv->settings.line_spacing;
 }
 
 /**