Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using custom fonts from a ttf file #4208

Open
muezz opened this issue Sep 4, 2024 · 5 comments
Open

Using custom fonts from a ttf file #4208

muezz opened this issue Sep 4, 2024 · 5 comments
Labels

Comments

@muezz
Copy link

muezz commented Sep 4, 2024

How can I use a custom font from a ttf file?

I have a file called Righteous.ttf. I am not able to use this font in the image.

In my NextJS project, I have an API that is supposed to merge two images and overlay a title on it in a particular font. The font file is in the format .ttf and exists in the same folder as the api:

image

Here is the code where I am trying to use this font:

const result = await sharp(resizedUrlImage)
    .composite([
      { input: await base.toBuffer(), blend: "over" },
      {
        input: {
          text: {
            // text: `<span foreground='white'>${title}</span>`,
            text: title,
            font: "Righteous",
            fontfile: path.join(
              process.cwd(),
              "./app/api/webhooks/pins/Righteous.ttf"
            ),
            wrap: "word",
            width: w - 300,
            dpi,
            rgba: true,
          },
        },
        blend: "over",
        left,
        top,
      },
    ])
    .toBuffer();

What am I doing wrong here? The docs dont have an example for this (or at least I can't find it).

@muezz muezz added the question label Sep 4, 2024
@lovell
Copy link
Owner

lovell commented Sep 4, 2024

@muezz
Copy link
Author

muezz commented Sep 4, 2024

@lovell I have seen the first link but not the second. This is not an installed font in the OS. In this case I want to read from a .ttf file because I dont think there is a way to install it when this gets deployed to vercel.

About the font config, I have never created one before. If I use the example config file from the second link, where would it go when it eventually gets deployed to vercel?

@lovell
Copy link
Owner

lovell commented Sep 4, 2024

What is the output from fontconfig when you set the FC_DEBUG=1 environment variable and run your example?

@muezz
Copy link
Author

muezz commented Sep 4, 2024

@lovell I get this error:
image

@Hashiphoto
Copy link

Hashiphoto commented Sep 16, 2024

@muezz I was running into the same issue. Here's how I solved it for a font called Beaufort. I put the font and all its variations into a dir in my repo.

Then, create a fonts.conf file like this:

<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
  <dir>/path/to/the/dir/containing/your/font</dir>
  <match>
    <test name="family" compare="contains">
      <string>Beaufort</string>
    </test>
    <edit name="antialias" mode="assign">
      <bool>true</bool>
    </edit>
    <edit name="hinting" mode="assign">
      <bool>true</bool>
    </edit>
  </match>
</fontconfig>

You can do this in your console, but I run it on node app start

    const configFilePath = path.resolve("yourConfigDir", "fonts.conf");

    // Set environment variable for fontconfig
    process.env.FONTCONFIG_FILE = configFilePath;

    // Run fc-cache to refresh the font cache with your custom fonts directory
    await execPromise("fc-cache -fv");

I am really no expert on this stuff, but this is what got it working in my project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants