title: Subsetting OpenType Collection font in pdfbox
body: <p>I'm trying to embed a subset of noto-regular in my code. but I keeping on getting:</p>
  
  <pre><code>java.lang.UnsupportedOperationException: OTF fonts do not have a glyf table
  
      at org.apache.fontbox.ttf.OpenTypeFont.getGlyph(OpenTypeFont.java:66)
      at org.apache.fontbox.ttf.TTFSubsetter.addCompoundReferences(TTFSubsetter.java:481)
      at org.apache.fontbox.ttf.TTFSubsetter.getGIDMap(TTFSubsetter.java:136)
      at org.apache.pdfbox.pdmodel.font.TrueTypeEmbedder.subset(TrueTypeEmbedder.java:306)
      at org.apache.pdfbox.pdmodel.font.PDType0Font.subset(PDType0Font.java:162)
      at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1138)
  </code></pre>
  
  <p>I downloaded the font file <code>NotoSansCJK-Regular.ttc</code> from <a href="https://www.google.com/get/noto/help/cjk/" rel="nofollow noreferrer">https://www.google.com/get/noto/help/cjk/</a></p>
  
  <p>Font subsetting works for <code>.ttf</code> fonts, as I haven't had any issues if the document I saved contains no special characters.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>It appears that true type collection fonts can have <a href="https://docs.microsoft.com/en-us/typography/opentype/spec/otff#collections" rel="nofollow noreferrer">shared glyf table</a> (makes sense since the font collection contains Japanese glyphs). So the individual PDType0Font parsed from <code>.ttc</code> can't be treated as an individual font.</p>
  
  <p>I loaded the font using:</p>
  
  <pre><code>            ttc.processAllFonts((TrueTypeFont ttf) -&gt; {
                  PDFont font = PDType0Font.load(doc, ttf, true);
                  fontList.add(font);
              });
  </code></pre>
  
  <p>I'm guessing that there are extra work I needed to do to make this work, but I can't find any code samples anywhere.</p>
  
  <p><strong>EDIT2</strong></p>
  
  <p>Seems like the problem is that when subsetting specific OpenType font files, (which font collection contains) turns on an internal flag <code>isPostScript</code>. The flag is then checked and process is aborted when <code>getGlyph()</code> is called.</p>
  
  <p>The following code generates the <code>glyf</code> table error when creating the pdf documents</p>
  
  <pre><code>        // downloaded from Noto project site
          String OTF_FILE = "./src/test/resources/NotoSansJP-Regular.otf";
          PDDocument doc = new PDDocument();
          PDFont otf = null;
          try (InputStream inputStream = new FileInputStream(new File(OTF_FILE))) {
              otf = PDType0Font.load(doc, new OTFParser().parse(inputStream), true);
              PDPage page = new PDPage();
              PDPageContentStream stream = new PDPageContentStream(doc, page);
              stream.setFont(otf, 10f);
              stream.beginText();
              stream.newLineAtOffset(100f, 600f);
              stream.showText("二ろほス反2化みた大第リきやね景手ハニエ者性ルヤリウ円脱");
              stream.endText();
              stream.close();
              doc.addPage(page);
              doc.save("test.pdf");
          } catch (IOException iox) {
              // failed
          }
  </code></pre>
  
  <p>but it will generate the pdf fine as soon as I set the subsetting parameter to true in the <code>PDType0Font.load</code> call</p>
  
  <p>Similarily if I load the otf font through the collection:</p>
  
  <pre><code>        String OTF_FILE = "./src/test/resources/NotoSansCJK-Regular.ttc";
          PDDocument doc = new PDDocument();
          PDFont otf = null;
          try (InputStream inputStream = new FileInputStream(new File(OTF_FILE))) {
              TrueTypeCollection ttc = new TrueTypeCollection(inputStream);
              otf = PDType0Font.load(doc, ttc.getFontByName("NotoSansCJKjp-Regular"), true);
              PDPage page = new PDPage();
              PDPageContentStream stream = new PDPageContentStream(doc, page);
              stream.setFont(otf, 10f);
              stream.beginText();
              stream.newLineAtOffset(100f, 600f);
              stream.showText("二ろほス反2化みた大第リきやね景手ハニエ者性ルヤリウ円脱");
              stream.endText();
              stream.close();
              doc.addPage(page);
              doc.save("test.pdf");
          } catch (IOException iox) {
              // failed
          }
  </code></pre>
  
  <p>I either need to embed the whole font or subsetting will throw the error</p>
  
  <p><strong>EDIT 3</strong>
  I ended up circumvent this by downloading the OTF font from "Language-specific OpenType/CFF (OTF)", which contains characters from all 4 regions and converted it using otf2ttf from <a href="https://github.com/fonttools/fonttools" rel="nofollow noreferrer">fonttools</a></p>