Bug 696824 - Loading IPAex fonts raises an error
Summary: Loading IPAex fonts raises an error
Status: RESOLVED FIXED
Alias: None
Product: Ghostscript
Classification: Unclassified
Component: Font API (show other bugs)
Version: 9.19
Hardware: PC Windows 8
: P4 normal
Assignee: Chris Liddell (chrisl)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-06-06 06:41 UTC by Masamichi Hosoda
Modified: 2016-06-23 02:29 UTC (History)
0 users

See Also:
Customer:
Word Size: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Masamichi Hosoda 2016-06-06 06:41:27 UTC
I'd like to use "IPAexMincho" and "IPAexGothic" font.
https://www.ctan.org/pkg/ipaex

I've tried the following but ghostscript raises an error.

```
GPL Ghostscript 9.19 (2016-03-23)
Copyright (C) 2016 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
GS>(c:/Windows/Fonts/ipaexm.ttf) (r) file .loadfont
Error: /typecheck in --known--
Operand stack:
   --nostringval--   CharStrings   --nostringval--   --dict:12154/12239(L)--   1
2154   12154   --nostringval--   --dict:12154/12239(L)--   --nostringval--
Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval-
-   2   %stopped_push   --nostringval--   --nostringval--   %loop_continue   --n
ostringval--   --nostringval--   false   1   %stopped_push   .runexec2   --nostr
ingval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval
--   --nostringval--   --nostringval--   %loop_continue   --nostringval--   1
--nostringval--   --nostringval--   false   1   %stopped_push   --nostringval--
  --nostringval--   --nostringval--   12155   1   12237   --nostringval--   %for
_pos_int_continue   --nostringval--
Dictionary stack:
   --dict:1194/1684(ro)(G)--   --dict:0/20(G)--   --dict:78/200(L)--   --dict:40
/50(ro)(G)--   --dict:55/80(L)--
Current allocation mode is local
Current file position is 49
GS<9>
```

```
GPL Ghostscript 9.19 (2016-03-23)
Copyright (C) 2016 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
GS>(c:/Windows/Fonts/ipaexg.ttf) (r) file .loadfont
Error: /typecheck in --known--
Operand stack:
   --nostringval--   CharStrings   --nostringval--   --dict:12219/12239(L)--   1
2219   12219   --nostringval--   --dict:12219/12239(L)--   --nostringval--
Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval-
-   2   %stopped_push   --nostringval--   --nostringval--   %loop_continue   --n
ostringval--   --nostringval--   false   1   %stopped_push   .runexec2   --nostr
ingval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval
--   --nostringval--   --nostringval--   %loop_continue   --nostringval--   1
--nostringval--   --nostringval--   false   1   %stopped_push   --nostringval--
  --nostringval--   --nostringval--   12220   1   12237   --nostringval--   %for
_pos_int_continue   --nostringval--
Dictionary stack:
   --dict:1194/1684(ro)(G)--   --dict:0/20(G)--   --dict:78/200(L)--   --dict:40
/50(ro)(G)--   --dict:55/80(L)--
Current allocation mode is local
Current file position is 49
GS<9>
```

Can GS load these fonts by .loadfont?
Comment 1 Ken Sharp 2016-06-06 06:52:25 UTC
Please don't try to use language extensions. If you want to load a font you need to use the documented method (add it to fontmap).

If these are not PostScript fonts then you need first to realise that there is no support in the language for TrueType fonts, Ghostscript has extensions that allow you to load TrueType fonts in various ways. I assume from the names that these are CJKV fonts and so you will need to load them by modifying cidfmap, as I described in your other bug report.
Comment 2 Masamichi Hosoda 2016-06-07 06:11:48 UTC
Thank you for your advice.
Comment 3 Chris Liddell (chrisl) 2016-06-08 09:46:14 UTC
The is genuinely invalid.

The 'post' table contains glyph indices beyond the number of glyph names in the table. This ends up leaving some of our internal data unset, and hence we end up with a typecheck Postscript error.

I think we can work around the problem.... as long as it doesn't end up breaking some other font......
Comment 4 Chris Liddell (chrisl) 2016-06-09 00:59:40 UTC
This commit

http://git.ghostscript.com/?p=user/chrisl/ghostpdl.git;a=commitdiff;h=c74fe6dfa

makes our 'post' table handling more tolerant of this type of issue.
Comment 5 Masamichi Hosoda 2016-06-20 05:12:14 UTC
Thank you for your commit.

If I understand correctly, IPAex font 'post' table is not invalid.

However, in IPAex fonts, numberNewGlyphs is larger than numberOfGlyphs.

i.e.
https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6post.html
Table 73: 'post' format2,

numberOfGlyphs < numberNewGlyphs

In other words, there are a larger number of glyph names than the number of glyphs.
I think that Ghostscript cannot find a glyph name which has a larger index than the number of glyphs.

I've tried your commit for ".loadfont".
(Sorry, I know it is non-recommended way.)
It can load IPAex fonts without errors.
However, some glyph names which have larger indexes than the number of glyphs cannot be used.
Comment 6 Chris Liddell (chrisl) 2016-06-20 08:28:37 UTC
(In reply to Masamichi Hosoda from comment #5)
> Thank you for your commit.
> 
> If I understand correctly, IPAex font 'post' table is not invalid.
> 
> However, in IPAex fonts, numberNewGlyphs is larger than numberOfGlyphs.
> 
> i.e.
> https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6post.
> html
> Table 73: 'post' format2,
> 
> numberOfGlyphs < numberNewGlyphs
> 
> In other words, there are a larger number of glyph names than the number of
> glyphs.
> I think that Ghostscript cannot find a glyph name which has a larger index
> than the number of glyphs.
> 
> I've tried your commit for ".loadfont".
> (Sorry, I know it is non-recommended way.)
> It can load IPAex fonts without errors.
> However, some glyph names which have larger indexes than the number of
> glyphs cannot be used.


You are correct, it is not explicitly forbidden to have more names in the table that glyph indices (I hadn't noticed the size of the names list was a separate value from the list of indices).

Although, I cannot imagine what utility you think you gain from having name entries that cannot be used, other than making the font file larger.

I'm testing a solution now.....
Comment 7 Chris Liddell (chrisl) 2016-06-20 10:32:30 UTC
(In reply to Chris Liddell (chrisl) from comment #6)
> 
> I'm testing a solution now.....

Done:
http://git.ghostscript.com/?p=ghostpdl.git;a=commitdiff;h=f3ed5db
Comment 8 Masamichi Hosoda 2016-06-21 07:02:26 UTC
Thank you for your solution.

I've tried both your commits.
It seems to work fine.

IPA / IPAex / IPAmj fonts are the most useful Japanese free fonts.
I'm not a developer of them. I'm only a user.
So I don't know that the reason of the larger glyph name entries.

Thank you again
Comment 9 Masamichi Hosoda 2016-06-21 07:52:07 UTC
I've found another problem related to 'post' table.
If you would like a new bug report, I'll create it.

".loadfont" cannot load all glyph names of IPAmjMincho font.
(Sorry, again, I know it is non-recommended way.)

IPAmj font:
http://mojikiban.ipa.go.jp/1300.html#LicenseEng
http://dl.mojikiban.ipa.go.jp/IPAmjMincho/ipamjm00301.zip

This font has a lot of glyphs.
The number of the glyph names exceeds 32768.

In OpenType specification v1.6 (April 2009, July 2010),
glyph name index numbers 32768 through 65535 were reserved.
https://www.microsoft.com/typography/otspec160/default.htm
https://www.microsoft.com/typography/otspec160/post.htm

However, in OpenType specification v1.7 (March 2015),
it allows the index number up to 65535.
https://www.microsoft.com/typography/otspec170/default.htm
https://www.microsoft.com/typography/otspec170/post.htm
https://www.microsoft.com/typography/otspec/changes.htm#v1.7

So I've tried the following patch.
It can load all glyph names of IPAmjMincho font.

```
--- a/Resource/Init/gs_ttf.ps	2016-06-21 23:46:16.165959300 +0900
+++ b/Resource/Init/gs_ttf.ps	2016-06-21 23:46:43.101499900 +0900
@@ -753,7 +753,7 @@
         } {
           % If the index we read from the table is outside the range of glyph
           % names available, just use the /.notdef name, and carry on
-          dup 32768 ge exch 258 sub dup postnames length ge
+          dup 65536 ge exch 258 sub dup postnames length ge
           3 -1 roll or {
             % According to the published TrueType spec, such values are
             % "reserved for future use", but at least some PDF files
```
Comment 10 Chris Liddell (chrisl) 2016-06-22 09:18:51 UTC
(In reply to Masamichi Hosoda from comment #9)
> I've found another problem related to 'post' table.
> If you would like a new bug report, I'll create it.

Generally, we do prefer a new bug for cases like this, but this is fine for this case.

I'm not adopting quite your suggestion because since we maximum possible value (as we only read two bytes from the stream) is 65535, there's no point checking that the value is less than 65536.

I'm rejigging the procedure a bit more, as I can make it a bit more efficient.
Comment 11 Chris Liddell (chrisl) 2016-06-22 09:49:55 UTC
This should address the problem:
http://git.ghostscript.com/?p=ghostpdl.git;a=commitdiff;h=d278f6a3
Comment 12 Masamichi Hosoda 2016-06-23 02:29:33 UTC
It works fine.
Thank you so much.