tadasajon

tadasajon

I downloaded a font called “American Typewriter” and I now have a file named American_Typewriter_Regular.ttf in my priv/static/fonts directory.

But I can’t figure out how to get this file to work with my CSS.

At the top of my assets/css/app.css file I have the following lines:


@font-face {
  font-family: 'American Typewriter Regular';
  src: url('/fonts/American_Typewriter_Regular.ttf') format('ttf');
  font-weight: normal;
  font-style: normal
}

.my-logo {
  font-family: 'American Typewriter Regular';
  font-size: 28px;
}

What do I have to do to get ESBuild to use my font properly?

I’ve been reading ESBiuld docs and found this info:

You can @import other CSS files and reference image and font files with url() and esbuild will bundle everything together. Note that you will have to configure a loader for image and font files, since esbuild doesn’t have any pre-configured. Usually this is either the data URLloader or the external file loader.

So I guess my problem is that I’m not using a loader – but I don’t know exactly how to specify to ESBuild how to use a loader.

The docs tell me to set some configuration similar to this:

require('esbuild').buildSync({
  entryPoints: ['app.js'],
  bundle: true,
  loader: { '.png': 'dataurl' },
  outfile: 'out.js',
})

but I don’t know where to set this configuration.

TLDR: how do I use a font that I’ve downloaded and have saved in my fonts/ directory?

Showing Posts 1 to 10

thomas.fortes

thomas.fortes

I answered (almost) the same question here:

Just change the extensions or follow the links :slight_smile:

tadasajon

tadasajon OP

Wow, thanks much. Following you suggestion I have adjusted my config/dev.exs file and it now reads as follows:

watchers: [
    esbuild:
        {Esbuild, :install_and_run,
        [:default, ~w(--sourcemap=inline --watch --loader:.ttf=file)]}
]

I have also adjusted my mix.exs file and it now shows the following:

"assets.deploy": [
    "cmd --cd assets npm run deploy",
    "esbuild default --minify --loader:.ttf=file",
    "phx.digest"
]

But I’m still not getting the font to work on my website.\

Could my problem have something to do with the fact that I’m telling ESBuild to use a file loader, but in my app.css file I have specified a URL?

Also, it is not clear to me how the ESBuild file loader is supposed to know where to find the file. How does it know whether to look in priv/static/assets/fonts vs assets/css/fonts vs assets/fonts, etc?

thomas.fortes

thomas.fortes

Could my problem have something to do with the fact that I’m telling ESBuild to use a file loader, but in my app.css file I have specified a URL?

Not really, what this loader does is copy the file to the output folder, if you used the dataURL it would convert the font to base64 and paste it at the start of the generated css.

Can you access the font file going straight to its url in the browser?

What exactly the error says?
Could not resolveor No loader is configured for ".ttf" files, if it is Could not resolve it means that the loader isn’t finding the file, in my referenced post the person put the font in priv and used a relative path like: background-image: url('../../priv/static/images/background.jpg');

tadasajon

tadasajon OP

Yes, if I visit http://localhost:4000/fonts/American_Typewriter_Regular.ttf in my browser, then I get a popup saying “Do you want to allow downloads on “localhost”?” – which suggests to me that the browser can find the file and is prompting me to download it because displaying it is not an option.

I’m not getting any error, though – when I run mix phx.server it just runs normally. I can use my website as expected. The only problem is that the font is not showing up so I don’t get the cool American Typewriter look.

thomas.fortes

thomas.fortes

Can you try @import instead of @font-face?

tadasajon

tadasajon OP

ok, I’ve adjusted my assets/css/app.css file and it now has the following:

@import {
  font-family: 'American Typewriter';
  src: url('/fonts/American_Typewriter_Regular.ttf') format('ttf');
  font-weight: normal;
  font-style: normal;
}

This causes my VSCode editor to give me some red underlines and warnings, though. The font is still not showing up.

I have also tried different paths, such as:

  src: url('/fonts/American_Typewriter_Regular.ttf') format('ttf');
  src: url('fonts/American_Typewriter_Regular.ttf') format('ttf');
  src: url('/assets/fonts/American_Typewriter_Regular.ttf') format('ttf');
  src: url('/static/fonts/American_Typewriter_Regular.ttf') format('ttf');
  src: url('../fonts/American_Typewriter_Regular.ttf') format('ttf');
  src: url('static/assets/fonts/American_Typewriter_Regular.ttf') format('ttf');
  src: url('../assets/fonts/American_Typewriter_Regular.ttf') format('ttf');

I have the .ttf file stored in two places:

  1. priv/static/fonts/American_Typewriter_Regular.ttf
  2. assets/fonts/American_Typewriter_Regular.ttf
thomas.fortes

thomas.fortes

Forget about the @import, I was thinking of other stuff…

My last guess is to use ../../priv/static/fonts/American_Typewriter_Regular.ttf and hope for the best…

tadasajon

tadasajon OP

Ok, so I’ve made a repo here: GitHub - tadasajon/use_local_font: use a local font in Phoenix 1.6.2 using ESBuild · GitHub

This was generated with mix phx.new use_local_font --no-ecto and it tries to do just one thing: use a local font.

I’ll obviously be delighted to receive a pull request from anyone.

tadasajon

tadasajon OP

Ok, well, the font works fine for me in that repo that I just made. I used the following settings:

In my assets/css/app.css:

@font-face {
  font-family: 'American Typewriter';
  src: url('/fonts/American_Typewriter_Regular.ttf') format('ttf');
  font-weight: normal;
  font-style: normal;
}

.use-local-font {
  color: darkblue; 
  font-family: 'American Typewriter'

}

in my lib/use_local_font_web/templates/layout/root.html.heex file:

<div style="margin:16px; padding:16px;outline:4px solid red;font-size:28px;text-align:center;background-color:lightyellow;">
    <span>
        This text should be in the default font. &nbsp;&nbsp;&nbsp;&nbsp; Q
    </span>
    <br />
    <span class="use-local-font">
        This text should be in American Typewriter Regular font. &nbsp;&nbsp; Q
    </span>
</div>

in my mix.exs file:

"assets.deploy": ["esbuild default --minify --loader:.ttf=file", "phx.digest"]

in my config/dev.exs file:

watchers: [
    # Start the esbuild watcher by calling Esbuild.install_and_run(:default, args)
    esbuild:
      {Esbuild, :install_and_run, [:default, ~w(--sourcemap=inline --watch --loader:.ttf=file)]}
  ]

tadasajon

tadasajon OP

So I guess my problem has something to do with the way I have tailwind configured in my app, since it isn’t working in the setup I have in my real app, but it is working in this toy setup.

Wow, what a time suck this has been for me.

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews