From 25483582a48b122b76c134e01167fc561d630c78 Mon Sep 17 00:00:00 2001
From: Jakob Leben <jleben@videolan.org>
Date: Sun, 29 Nov 2009 14:44:40 +0100
Subject: [PATCH 3/3] vout: better correction for wrong subtitle positioning

Keeps previous behavior when subtitles overlapped in time,
except when trying to move subtitles below the bottom picture edge
-> they start to visually overlap because of bounding.
---
 src/video_output/vout_subpictures.c |   51 +++++++++++++++++++++++++---------
 1 files changed, 37 insertions(+), 14 deletions(-)

diff --git a/src/video_output/vout_subpictures.c b/src/video_output/vout_subpictures.c
index 7ce0df4..e9c6038 100644
--- a/src/video_output/vout_subpictures.c
+++ b/src/video_output/vout_subpictures.c
@@ -1224,8 +1224,6 @@ static void SpuAreaFixOverlap( spu_area_t *p_dst,
             {
                 /* We go down */
                 int i_y = sub.i_y + sub.i_height;
-                if( i_y + a.i_height > p_master->i_height )
-                    break;
                 a.i_y = i_y;
                 b_moved = true;
             }
@@ -1233,8 +1231,6 @@ static void SpuAreaFixOverlap( spu_area_t *p_dst,
             {
                 /* We go up */
                 int i_y = sub.i_y - a.i_height;
-                if( i_y < 0 )
-                    break;
                 a.i_y = i_y;
                 b_moved = true;
             }
@@ -1255,13 +1251,31 @@ static void SpuAreaFixOverlap( spu_area_t *p_dst,
 }
 
 
+static void SpuAreaFitInside( spu_area_t *p_area, const spu_area_t *p_container )
+{
+  spu_area_t a = spu_area_scaled( *p_area );
+
+  const int i_error_x = (a.i_x + a.i_width) - p_container->i_width;
+  if( i_error_x > 0 )
+      a.i_x -= i_error_x;
+  if( a.i_x < 0 )
+      a.i_x = 0;
+
+  const int i_error_y = (a.i_y + a.i_height) - p_container->i_height;
+  if( i_error_y > 0 )
+      a.i_y -= i_error_y;
+  if( a.i_y < 0 )
+      a.i_y = 0;
+
+  *p_area = spu_area_unscaled( a, p_area->scale );
+}
+
 /**
  * Place a region
  */
 static void SpuRegionPlace( int *pi_x, int *pi_y,
                             const subpicture_t *p_subpic,
-                            const subpicture_region_t *p_region,
-                            int i_margin_y )
+                            const subpicture_region_t *p_region )
 {
     const int i_delta_x = p_region->i_x;
     const int i_delta_y = p_region->i_y;
@@ -1301,10 +1315,12 @@ static void SpuRegionPlace( int *pi_x, int *pi_y,
     }
 
     /* Margin shifts all subpictures */
+    /* NOTE We have margin only for subtitles, so we don't really need this here
     if( i_margin_y != 0 )
-        i_y -= i_margin_y;
+        i_y -= i_margin_y;*/
 
     /* Clamp offset to not go out of the screen (when possible) */
+    /* NOTE Again, useful only for subtitles, otherwise goes against the alignment logic above
     const int i_error_x = (i_x + p_region->fmt.i_width) - p_subpic->i_original_picture_width;
     if( i_error_x > 0 )
         i_x -= i_error_x;
@@ -1315,7 +1331,7 @@ static void SpuRegionPlace( int *pi_x, int *pi_y,
     if( i_error_y > 0 )
         i_y -= i_error_y;
     if( i_y < 0 )
-        i_y = 0;
+        i_y = 0;*/
 
     *pi_x = i_x;
     *pi_y = i_y;
@@ -1402,7 +1418,7 @@ static void SpuRenderRegion( spu_t *p_spu,
     /* Place the picture
      * We compute the position in the rendered size */
     SpuRegionPlace( &i_x_offset, &i_y_offset,
-                    p_subpic, p_region, i_margin_y );
+                    p_subpic, p_region );
 
     /* Save this position for subtitle overlap support
      * it is really important that there are given without scale_size applied */
@@ -1413,16 +1429,23 @@ static void SpuRenderRegion( spu_t *p_spu,
     /* Handle overlapping subtitles when possible */
     if( p_subpic->b_subtitle && !p_subpic->b_absolute )
     {
+        SpuAreaFixOverlap( p_area, NULL, p_subtitle_area, i_subtitle_area,
+                           p_region->i_align );
+    }
+
+    spu_area_t p_restrained = *p_area;
+
+    if( p_subpic->b_subtitle )
+    {
+        p_restrained.i_y -= i_margin_y;
         spu_area_t display = spu_area_create( 0, 0, p_fmt->i_width, p_fmt->i_height,
                                               spu_scale_unit() );
-
-        SpuAreaFixOverlap( p_area, &display, p_subtitle_area, i_subtitle_area,
-                           p_region->i_align );
+        SpuAreaFitInside( &p_restrained, &display );
     }
 
     /* Fix the position for the current scale_size */
-    i_x_offset = spu_scale_w( p_area->i_x, p_area->scale );
-    i_y_offset = spu_scale_h( p_area->i_y, p_area->scale );
+    i_x_offset = spu_scale_w( p_restrained.i_x, p_restrained.scale );
+    i_y_offset = spu_scale_h( p_restrained.i_y, p_restrained.scale );
 
     /* */
     if( b_force_palette )
-- 
1.6.3.3

