Git Product home page Git Product logo

parse-font-name's Introduction

parse-font-name

Extract font weight (in steps of spec-valid 100s) and style (italic) data from the name of a font in a more robust way than most libraries do.

I'll be honest – font names are a bitch since they follow no actual standard at all and sometimes want to be creative in their naming which is not helpful for developers. Lots of implementations out there only catch the most regular font names, but I wanted to have something that catches more obscure and rare cases. Also anything that is not a multiple of 100 between 100 and 900 is simply not valid, so an attempt was made to clamp weights into these values. You can supply font-filenames with or without extensions, it doesn't matter.

We try to find direct matches of keywords in your filenames and then pick the best match using the Sørensen–Dice coefficient, if there are no string matches, but there is already a valid X00 numeric value in the filename, we return that, otherwise we fall back to a default value. The reason we prefer string matches over numeric values is that it's easy to have a typo on a single digit and more unlikely to accidentally have "Regular" in a filename. Also there could be triple digits in the actual font family name like "Nimbus500-Regular.ttf".

Recognizing weights and styles of 100% of font names is obviously impossible, but the heart of this repo, the numericFontWeightMap has a lot more than you'd typically find out there in the internet. Feel free to open issues or direct PRs for more cases you found!

  • zero dependencies
  • fully typed in typescript
  • 100% code test coverage (100% Statements 28/28, 100% Branches 36/36, 100% Functions 7/7, 100% Lines 27/27)
  • small footprint (2.2kb minified)
  • CommonJS bundle, .mjs bundle, .d.ts bundle + type-checking & Source maps
  • written in an actually readable way

Installation

npm i parse-font-name or yarn add parse-font-name

Usage

import { parseNumericWeightFromName, parseStyleFromName, numericFontWeightMap } from 'parse-font-name'

// parse numeric weights
parseNumericWeightFromName('Helvetica Neue Light.ttf') // returns 300
parseNumericWeightFromName(['Akkurat Pro Regular', 'Akkurat Pro Fett Kursiv']) // returns [400, 700]
parseNumericWeightFromName('Bebas Neue') // returns 400 (fallbackValue)
parseNumericWeightFromName('Bebas Neue 500') // returns 500 (no string, but valid numeric value found)
parseNumericWeightFromName('Bebas Neue', 700) // returns 700 (fallbackValue)

// parse font styles
parseStyleFromName('Akkurat Pro Fett Italic') // returns 'italic'
parseStyleFromName('Akkurat Pro Fett Kursiv', 'boolean') // returns true
parseStyleFromName('Akkurat-Pro-Fett-Oblique.otf') // returns 'oblique'
parseStyleFromName(['Akkurat Pro Fett Kursiv', 'Helvetica Neue Light'], 'boolean') // returns [true, false]

// or just toy around with the hardcoded map, which is also exported as a type
console.log(numericFontWeightMap)
parseNumericWeightFromName(
  fontStrings: string | string[],
  fallbackValue?: number // fallbackValue has a default of 400
) => number | number[] // providing strings gives you strings and is properly typed to do so
parseStyleFromName(
  fontStrings: string | string[],
  format?: 'cssString' | 'boolean', // format has a default of 'cssString'
  fallbackValue?: boolean | ('normal' | 'oblique' | 'italic') // fallbackValue defaults to false or 'normal' depending on format
) => (boolean | ('normal' | 'oblique' | 'italic'))[]

See src/index.test.js for more examples and src/index.ts for types.

Performance

parseNumericWeightFromName from 100 font strings 605 ops/s, ±0.51% parseStyle from 100 font strings 109 111 ops/s, ±0.23%

Which is something that could be improved, but so far I didn't need to parse 100 fonts more than a couple of hundred times a second, so whatever.

parse-font-name's People

Contributors

tom2strobl avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

parse-font-name's Issues

Incorrect definition of a SemiBold font weight.

While using the library, I noticed that fonts with SemiBold in the name return an incorrect value when processing their weight.

Expected:
let weight = parseNumericWeightFromName('Helvetica Neue SemiBold')
console.log(weight) // 600
Exit:
let weight = parseNumericWeightFromName('Helvetica Neue SemiBold')
console.log(weight) // 700

I tried three names: "Helvetica Neue SemiBold", "HelveticaNeueSemiBold", "HelveticaNeue-SemiBold", but in none of the cases was the weight determined correctly.
Your library was uploaded via NPM.

Incorrect parsing of some font names.

During the operation of your library, it turned out that when parsing some font names, incorrect values are returned as a result.

This reaction occurs in these cases:

  • centurygothic_bold expected => 700 normal exit => 400 normal
  • MYRIADPRO-BOLDCONDIT expected => 700 italic exit => 400 normal
  • MYRIADPRO-CONDIT expected => 400 italic exit => 400 normal
  • MYRIADPRO-SEMIBOLD expected => 600 normal exit => 400 normal
  • MYRIADPRO-SEMIBOLDIT expected => 600 italic exit => 400 normal
  • MyriadPro CondIt expected => 400 italic exit => 400 normal
  • alegreya-v29-latin-500 expected => 500 normal exit => 400 normal
  • alegreya-v29-latin-500italic expected => 500 italic exit => 400 italic

Testing was carried out on the latest version of the library downloaded from NPM.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.