Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

164
Visualizações
How do I use system font information to populate a canvas font property?

I am experimenting with Node Canvas (an implementation of the HTML5 Canvas in NodeJS), trying to write simple text in different fonts.

I can pull system font data fine using the node-system-fonts library's getavailablefonts function which gives me data like

[
  {
    "path": "/Library/Fonts/AcademyEngravedLet.ttf",
    "postscriptName": "AcademyEngravedLetPlain",
    "family": "Academy Engraved LET",
    "style": "Plain",
    "weight": 400,
    "width": 5
  },
  {
    "path": "/Library/Fonts/Impact.ttf",
    "postscriptName": "Impact",
    "family": "Impact",
    "style": "Regular",
    "weight": 900,
    "width": 4
  }
]

In the Canvas itself I can specify the font info using a font string, as documented in w3schools.com/TAgs/canvas_font.


import { createCanvas } from "canvas";

import { WIDTH, HEIGHT } from "../config";

const write = (text, font, fontStyle) => {
  const canvas = createCanvas(WIDTH, HEIGHT);
  const ctx = canvas.getContext("2d");

  ctx.fillStyle = "black";
  ctx.font = `${Math.abs(WIDTH / Math.PI)}px ${font}`;
  ctx.fontStyle = fontStyle;
  ctx.textAlign = "center";
  ctx.textBaseline = "middle";
  ctx.fillText(text, WIDTH / 2, HEIGHT / 2, WIDTH - 20); // text, x, y, max width

  return canvas;
};

But I'm struggling to correctly associate the Font family and style from the font data with what ought to go into the ctx.font property.

In the case of Impact, a font string 200px Impact gives me a system default font rather than the Impact font. But 200px Academy Engraved LET works fine.

Also setting the ctx.fontStyle property to the value of the font's style property doesn't seem to do anything.

Is there documentation somewhere that explains how to map system fonts to the canvas font property?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

After a bit of digging around in the actual canvas source I've got this working.

/**
 * Convert the font data into a font string.
 *
 * @param {string} size A font size string (eg 100px or 50%/55% etc)
 * @param {object} font The font data, including `{ style, family }`
 * @returns {string} The font string
 */
const stringifyFont = (size, { style, family }) =>
  [style, size, family].filter(Boolean).map(String).join(" ");

export default stringifyFont;

I also found it helped to register the fonts as that helped me weed out fonts that, for various reasons, just don't work.

import { registerFont } from "canvas";

const registerFonts = (fonts) => {
  fonts.forEach(({ path, family, weight, style }) => {
    registerFont(path, { family, weight, style });
  });
};

export default registerFonts;
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda