Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to load custom font? #404

Answered by jcupitt
9mm asked this question in Q&A
Sep 23, 2024 · 1 comments · 6 replies
Discussion options

I'm trying to figure out how to load a font from a path, as this will be running on ubuntu and i dont want to mess with trying to install fonts when I deploy. Id rather use a few fonts in the git repo

 FONTS = [
 { name: "Akira Expanded Not-Rotated 12", filename: "akira.otf", spacing: -25 },
 { name: "Vogue Regular", filename: "vogue.ttf", spacing: -25 },
 ]
 
 font = FONTS.sample
 text = Vips::Image.text "test text",
 fontfile: Rails.root.join("lib/assets/fonts/#{font[:filename]}").to_path,
 font: font[:name],
 width: render_width,
 height: render_height,
 spacing: font[:spacing]

This always renders Arial no matter what. I can tell its loading the proper file as when i put a bogus filename it expectedly errors... yet there is no error about "font not found from loaded files" so it makes it difficult to debug if i have the name correct, and something else is wrong... or if the name is just wrong.

Ive tried...

Vogue, Regular
Voge
Vogue Regular

etc

Do I need some extra library installed?

You must be logged in to vote

Yes, I think cairo on mac is using the native macOS font renderer, so you can't load fonts dynamically, you have to install them. It should work on ubuntu though.

Replies: 1 comment 6 replies

Comment options

Hello @9mm,

It should work (I think?). You can use fc-scan to get the name from a font file, for example:

$ fc-scan MissFajardose-Regular.ttf 
Pattern has 28 elts (size 32)
	family: "Miss Fajardose"(s)
	familylang: "en"(s)
	style: "Regular"(s)
	stylelang: "en"(s)
	fullname: "Miss Fajardose Regular"(s)
...

So I can use:

$ vips text x.png "Hello World" --fontfile MissFajardose-Regular.ttf --font "Miss Fajardose" --dpi 600

There's also FC_DEBUG to debug font loading issues.

You must be logged in to vote
6 replies
Comment options

Just set the env var before running the command, for example:

$ FC_DEBUG=1 vips text x.png "Hello World" --fontfile MissFajardose-Regular.ttf --font "Miss Fajardose" --dpi 600

It's incredibly chatty and it'll take a while to read the output, but it should have the info you need.

See:

https://www.freedesktop.org/software/fontconfig/fontconfig-user.html#DEBUG

Comment options

hmmm.. it just exits immediately with no error or output

FC_DEBUG=1 vips text x.png "Hello World" --fontfile baberry.otf --font "Baberry" --dpi 600
FC_DEBUG=1

Is there some kind of other package i need to install? all i did was brew install vips on OSX

Comment options

Ah you said you were on ubuntu. I don't know if macOS can use fontconfig in the same way, I'll check.

Comment options

Yes, I think cairo on mac is using the native macOS font renderer, so you can't load fonts dynamically, you have to install them. It should work on ubuntu though.

Answer selected by 9mm
Comment options

Ughh I'm very sorry about that. I meant to type "ubuntu in production and OSX locally" but got sidetracked halfway through my sentence.

Ok thank you, i'll just install them but try on ubuntu. I really appreciate it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /