From f18b7c859c92111446ca991743dd709e347d0301 Mon Sep 17 00:00:00 2001
From: Werner Lemberg <wl@gnu.org>
Date: Sat, 6 Oct 2018 09:04:35 +0200
Subject: [PATCH] tafpgm.c: Fix stem width offset handling in storage area.

Problem reported by Hin-Tak Leung <htl10@users.sourceforge.net>.
---
 lib/tabytecode.h |  5 +++++
 lib/tafpgm.c     | 24 ++++++++----------------
 3 files changed, 21 insertions(+), 16 deletions(-)

diff --git a/lib/tabytecode.h b/lib/tabytecode.h
index c35a3fa..8851241 100644
--- a/lib/tabytecode.h
+++ b/lib/tabytecode.h
@@ -301,6 +301,7 @@
 
 /* symbolic names for storage area locations */
 
+/* 0 */
 #define sal_i 0
 #define sal_j sal_i + 1
 #define sal_k sal_j + 1
@@ -311,6 +312,8 @@
 #define sal_best sal_temp3 + 1
 #define sal_ref sal_best + 1
 #define sal_func sal_ref + 1
+
+/* 10 */
 #define sal_anchor sal_func + 1
 #define sal_stem_width_function sal_anchor + 1
 #define sal_base_delta sal_stem_width_function + 1
@@ -321,6 +324,8 @@
 #define sal_base sal_point_max + 1
 #define sal_num_packed_segments sal_base + 1
 #define sal_num_stem_widths sal_num_packed_segments + 1
+
+/* 20 */
 #define sal_stem_width_offset sal_num_stem_widths + 1
 #define sal_have_cached_width sal_stem_width_offset + 1
 #define sal_cached_width_offset sal_have_cached_width + 1
diff --git a/lib/tafpgm.c b/lib/tafpgm.c
index 7057cf4..2274b27 100644
--- a/lib/tafpgm.c
+++ b/lib/tafpgm.c
@@ -378,7 +378,7 @@ static const unsigned char FPGM(bci_quantize_stem_width) [] =
     sal_k,
   RS,
   EQ, /* sal_limit == sal_k ? */
-  JROT,/* goto not_in_array */
+  JROT, /* goto not_in_array */
 
   DUP,
   PUSHB_1,
@@ -2068,6 +2068,7 @@ static const unsigned char FPGM(bci_create_segment) [] =
  *      sal_num_packed_segments
  *      sal_base (the base for delta values in nibbles)
  *      sal_vwidth_data_offset
+ *      sal_stem_width_offset
  *      sal_scale
  *
  * CVT: cvtl_is_subglyph
@@ -2129,16 +2130,7 @@ static const unsigned char FPGM(bci_create_segments_b) [] =
     WS, /* sal_vwidth_data_offset = data_offset + num_used_styles */
 
     DUP,
-    PUSHB_1,
-      sal_stem_width_offset,
-    SWAP,
-    WS, /* sal_stem_width_offset = num_segments (more to come) */
-
-    DUP,
-    ADD,
-    PUSHB_1,
-      1,
-    SUB, /* delta = (2*num_segments - 1) */
+    ADD, /* delta = 2*num_segments */
 
     PUSHB_8,
       sal_segment_offset,
@@ -2160,12 +2152,12 @@ static const unsigned char FPGM(bci_create_segments_b) [] =
     DUP,
     PUSHB_1,
       sal_stem_width_offset,
-    RS,
-    ADD,
-    PUSHB_1,
-      sal_stem_width_offset,
     SWAP,
-    WS, /* sal_stem_width_offset += sal_segment_offset + delta */
+    WS, /* sal_stem_width_offset = sal_segment_offset + delta */
+
+    PUSHB_1,
+      1,
+    SUB, /* s: ... sal_segment_offset (sal_segment_offset + delta - 1) */
 
     PUSHB_2,
       bci_create_segment,
-- 
2.10.5.GIT

