foliojs / fontkit

An advanced font engine for Node and the browser
1.49k stars 221 forks source link

Get one font buffer from collection of fonts buffer. #156

Open Rishab-1065 opened 6 years ago

Rishab-1065 commented 6 years ago

I am trying to read a ttc font file and get the buffer for particular font but the font object gives the buffer for all fonts. Is there any way I can get particular font buffer.

Pomax commented 6 years ago

What is the code you're trying atm? Are you using https://github.com/devongovett/fontkit#collectiongetfontpostscriptname to get a contained font?

Rishab-1065 commented 6 years ago
function getTtcFile(filePath, fontPostScriptName) {
  fontkit.open(
    "/Library/Fonts/AmericanTypewriter.ttc",
    "AmericanTypewriter-Bold",
    (err, font) => {
      const str = iconv.decode(font.stream.buffer, "base64");
      fs.writeFile(
        "$(USER_ROOTDIR)/AmericanTypewriter-Bold.ttf",
        font.stream.buffer
      );
    }
  );
}

I am getting the buffer for all the fonts not for the specific one.

saar62097 commented 4 years ago

Having the same issue. Trying to save a single TTF file from a TTC collection. The code below saves the entire original collection and not just the selected font ([0]).

const fs = require('fs');
const fontkit = require('fontkit');
const ttfFont = fontkit.openSync('/System/Library/Fonts/Avenir.ttc').fonts[0];

fs.writeFileSync('Avenir-Book.ttf', ttfFont.stream.buffer);
Pomax commented 4 years ago

Could you try this again but rather than accessing .fonts[0] after loading, directly grabbing the font in question by using its postscript name? (mostly in order to find clues on what might be going wrong)

saar62097 commented 4 years ago

Same results of course. Below is the code from TrueTypeCollection.js I think the stream should be sliced somehow before assigned to each new TTFFont (sorry, my knowledge in buffers and streams is weak..). Also, and regardless, in case of DFonts, the getFont(postcriptName) doesn't work. WDYT?

getFont(name) {
    for (let offset of this.header.offsets) {
      let stream = new r.DecodeStream(this.stream.buffer);
      stream.pos = offset;
      let font = new TTFFont(stream);
      if (font.postscriptName === name) {
        return font;
      }
    }

    return null;
  }

  get fonts() {
    let fonts = [];
    for (let offset of this.header.offsets) {
      let stream = new r.DecodeStream(this.stream.buffer);
      stream.pos = offset;
      fonts.push(new TTFFont(stream));
    }

    return fonts;
  }
JonSnow-vn commented 4 years ago

do you have solution?? i got same problem

mrspartak commented 2 years ago

Still not working

Source-Code-Sivir commented 1 year ago

is there any resolution?