In af_cjk_metrics_scale_dim(), when we scale the blue zone to the font size, the blue zone is also rounded to pixel grid. The offset introduced in the rounding at both the bottom and top could have undesirable effect on the glyph height at certain font size.

There are 4 states for the sum of top and bottom fit offset:

1. top + bottom >= 1.5 px    Round to 2 px

2. top + bottom >= 1.0 px    Round to 1 px
       Bad Case: 0.5 + 0.5 => 1 + 1 = 2px > 1px

3. top + bottom >= 0.5 px    Round to 1 px
       Bad Case: 0.4 + 0.4 => 0 + 0 = 0px < 1px

4. top + bottom < 0.5  px    Round to 0 px

For the 2 bad results:

1st bad is OK, since we are OK with a pixel increase in size at small
font size.

2nd bad is not OK. It shrink our already limited space by 1px at small
font size. So we should compensate 1px in that situation.

The patch only tried to fix the ref blue zone.

The compensation kicked in at 11.5pt and 10pt and add 1px to the glyph height for wqy-zenhei.ttc.

diff --git a/src/autofit/afcjk.c b/src/autofit/afcjk.c
index 513b0c0..d3d8cdc 100644
--- a/src/autofit/afcjk.c
+++ b/src/autofit/afcjk.c
@@ -607,7 +607,7 @@
         delta2 = FT_MulFix( delta2, scale );
 
         FT_TRACE5(( "delta: %d", delta1 ));
-        if ( delta2 < 32 )
+        if ( delta2 <= 32 )
           delta2 = 0;
         /*
         else if ( delta2 < 64 )
@@ -632,6 +632,76 @@
         blue->flags |= AF_CJK_BLUE_ACTIVE;
       }
     }
+
+    /* Compensate glyph height from top and bottom blue zone offsets. */
+    /* There are 4 states for the sum of top and bottom fit offset: 
+    *  
+    *  1. top + bottom >= 1.5 px    Round to 2 px
+    *
+    *  2. top + bottom >= 1.0 px    Round to 1 px
+    *       Bad Result: 0.5 + 0.5 => 1 + 1 = 2px > 1px
+    *
+    *  3. top + bottom >= 0.5 px    Round to 1 px
+    *       Bad Result: 0.4 + 0.4 => 0 + 0 = 0px < 1px
+    *
+    *  4. top + bottom < 0.5  px    Round to 0 px
+    *
+    *  For the 2 bad result,
+    *
+    *  1st bad is OK, since we are OK with a pixel increase in size at small
+    *  font size.
+    *
+    *  2nd bad is not OK. It shrink our already limited space by 1px at small
+    *  fonr size. So we should compensate 1px in that situation.
+    */
+    {
+      FT_Pos      delta_t, delta_b, delta_sum;
+      AF_CJKBlue  blue_t, blue_b;
+
+      blue_t = &axis->blues[AF_CJK_BLUE_TOP];
+      blue_b = &axis->blues[AF_CJK_BLUE_BOTTOM];
+
+      
+      delta_t =    blue_t->ref.cur - blue_t->ref.fit;
+      delta_b = -( blue_b->ref.cur - blue_b->ref.fit );
+      
+      delta_sum = delta_t + delta_b;
+
+      FT_TRACE6(( "COMPEN: ref.cur: %li, ref.fit: %li, %li\n",
+        blue_t->ref.cur, blue_t->ref.fit, delta_t ));
+      FT_TRACE6(( "COMPEN: ref.cur: %li, ref.fit: %li, %li, sum: %li\n",
+        blue_b->ref.cur, blue_b->ref.fit, delta_b, delta_sum ));
+
+      if ( delta_t > 0     && delta_b > 0    &&  /* rounded to floor. */
+           delta_sum >= 32 && delta_sum <= 64 && 
+           delta_t <= 32    && delta_b <= 32      ) 
+      {
+        FT_Pos      shoot_delta;
+        AF_CJKBlue  blue;
+
+        if ( delta_t >= delta_b )
+        {
+          blue = &axis->blues[AF_CJK_BLUE_TOP];
+          shoot_delta = blue->shoot.fit - blue->ref.fit;
+          blue->ref.fit += 64;
+        }
+        else
+        {
+          blue = &axis->blues[AF_CJK_BLUE_BOTTOM];
+          shoot_delta = blue->shoot.fit - blue->ref.fit;
+          blue->ref.fit -= 64;
+        }
+
+        blue->shoot.fit = blue->ref.fit + shoot_delta;
+
+        FT_TRACE5(( ">> compensated cjk blue zone %c%d[%ld/%ld]: "
+                     "ref: cur=%.2f fit=%.2f shoot: cur=%.2f fit=%.2f\n",
+                       ( dim == AF_DIMENSION_HORZ ) ? 'H':'V',
+                       nn, blue->ref.org, blue->shoot.org,
+                       blue->ref.cur/64.0, blue->ref.fit/64.0,
+                       blue->shoot.cur/64.0, blue->shoot.fit/64.0 ));
+      }
+    }
   }
 
 
_______________________________________________
Freetype-devel mailing list
Freetype-devel@nongnu.org
https://lists.nongnu.org/mailman/listinfo/freetype-devel

Reply via email to