--- ./src/gdevpsf2.c.org	2003-01-17 09:49:01.000000000 +0900
+++ ./src/gdevpsf2.c	2005-10-03 18:36:30.000000000 +0900
@@ -901,6 +901,28 @@
     *pcount = j;
     return offset - 1;
 }
+private uint
+cff_write_GSubrs_offsets(cff_writer_t *pcw, uint *pcount, gs_font_type1 *pfont)
+{
+    int extra_lenIV = cff_extra_lenIV(pcw, pfont);
+    int j, offset;
+    int code;
+    gs_const_string str;
+
+    for (j = 0, offset = 1;
+	 (code = pfont->data.procs.subr_data(pfont, j, true, &str)) !=
+	     gs_error_rangecheck;
+	 ++j) {
+	if (code >= 0 && str.size >= extra_lenIV)
+	    offset += str.size - extra_lenIV;
+	put_offset(pcw, offset);
+	if (code > 0)
+	    gs_free_const_string(pfont->memory, str.data, str.size,
+				 "cff_write_Subrs_offsets");
+    }
+    *pcount = j;
+    return offset - 1;
+}
 private void
 cff_write_Subrs(cff_writer_t *pcw, uint subrs_count, uint subrs_size,
 		gs_font_type1 *pfont)
@@ -924,6 +946,29 @@
 	}
     }
 }
+private void
+cff_write_GSubrs(cff_writer_t *pcw, uint subrs_count, uint subrs_size,
+		 gs_font_type1 *pfont)
+{
+    int j;
+    uint ignore_count;
+    gs_const_string str;
+    int code;
+
+    cff_put_Index_header(pcw, subrs_count, subrs_size);
+    cff_write_GSubrs_offsets(pcw, &ignore_count, pfont);
+    for (j = 0;
+	 (code = pfont->data.procs.subr_data(pfont, j, true, &str)) !=
+	     gs_error_rangecheck;
+	 ++j) {
+	if (code >= 0) {
+	    cff_put_CharString(pcw, str.data, str.size, pfont);
+	    if (code > 0)
+		gs_free_const_string(pfont->memory, str.data, str.size,
+				     "cff_write_Subrs");
+	}
+    }
+}
 
 /* ------ Encoding/charset ------ */
 
@@ -1471,6 +1516,7 @@
     gs_const_string font_name;
     stream poss;
     uint charstrings_count, charstrings_size;
+    uint gsubrs_count, gsubrs_size;
     uint charset_size, fdselect_size, fdselect_format;
     uint subrs_count[256], subrs_size[256];
     /*
@@ -1479,6 +1525,7 @@
      */
     uint
 	Top_size = 0x7fffff,
+	GSubrs_offset = 0x7fffff,
 	charset_offset = 0x7fffff,
 	FDSelect_offset = 0x7fffff,
 	CharStrings_offset = 0x7fffff,
@@ -1516,6 +1563,8 @@
     writer.glyph_data = cid0_glyph_data;
     writer.offset_size = 1;	/* arbitrary */
     writer.start_pos = stell(s);
+    writer.FontBBox.p.x = writer.FontBBox.p.y =
+        writer.FontBBox.q.x = writer.FontBBox.q.y = 0;
 
     /* Set the font name. */
     if (alt_font_name)
@@ -1553,6 +1602,13 @@
 	FDArray_offsets[j] = Private_offsets[j] = Subrs_offsets[j] =
 	    0x7effffff / num_fonts * j + 0x1000000;
 
+    if (cff_convert_charstrings(&writer,
+			(const gs_font_base *)pfont->cidata.FDArray[0]))
+	gsubrs_count = gsubrs_size = 0;
+    else
+	gsubrs_size = cff_write_GSubrs_offsets(&writer, &gsubrs_count,
+					       pfont->cidata.FDArray[0]);
+
     /*
      * Compute the size of the charset.  For simplicity, we currently
      * always store the charset explicitly.
@@ -1604,10 +1660,11 @@
     writer.strm = &poss;
 
     /* Compute the offsets. */
-    charset_offset = 4 + cff_Index_size(1, font_name.size) +
+    GSubrs_offset = 4 + cff_Index_size(1, font_name.size) +
 	cff_Index_size(1, Top_size) +
-	cff_Index_size(writer.strings.count, writer.strings.total) +
-	cff_Index_size(0, 0);
+	cff_Index_size(writer.strings.count, writer.strings.total);
+    charset_offset = GSubrs_offset +
+	cff_Index_size(gsubrs_count, gsubrs_size);
     FDSelect_offset = charset_offset + charset_size;
     CharStrings_offset = FDSelect_offset + fdselect_size;
     if_debug3('l', "[l]charset at %u, FDSelect at %u, CharStrings at %u\n",
@@ -1636,8 +1693,17 @@
     /* Write the strings Index. */
     cff_put_Index(&writer, &writer.strings);
 
-    /* Write the (empty) gsubrs Index. */
-    cff_put_Index_header(&writer, 0, 0);
+    /* Write the gsubrs Index. */
+    offset = stell(writer.strm) - start_pos;
+    if_debug2('l', "[l]GSubrs = %u => %u\n", GSubrs_offset, offset);
+    if (offset > GSubrs_offset)
+	return_error(gs_error_rangecheck);
+    GSubrs_offset = offset;
+    if (gsubrs_count == 0)
+	cff_put_Index_header(&writer, 0, 0);
+    else
+	cff_write_GSubrs(&writer, gsubrs_count, gsubrs_size,
+			 pfont->cidata.FDArray[0]);
 
     /* Write the charset. */
     cff_write_cidset(&writer, &genum);
