GithubHelp home page GithubHelp logo

adambisek / string-pixel-width Goto Github PK

View Code? Open in Web Editor NEW
101.0 4.0 41.0 440 KB

Blazingly fast measure string width in pixels on the server in Javascript (Node.Js)

Home Page: https://medium.com/@adambisek/text-pixel-width-measuring-on-javascript-backend-node-js-2b82bea97fab#.8ypyiffyw

License: MIT License

JavaScript 97.49% HTML 2.51%
javascript string width measure nodejs

string-pixel-width's Introduction

Build Status npm version Coverage Status

String pixel width measurement on the backend in Javascript.

Why I created this package? Read more about purpose

Installation

To install the stable version:

npm install --save string-pixel-width

Supported fonts:

  • Andale Mono
  • Arial
  • Avenir
  • Avenir Next
  • Comic Sans MS
  • Courier New
  • Georgia
  • Helvetica
  • Impact
  • Inter
  • Times New Roman
  • Trebuchet MS
  • Verdana
  • Webdings
  • Open Sans
  • Tahoma

Example

var pixelWidth = require('string-pixel-width');

const width = pixelWidth('My text ...', { size: 10 });
console.log('This text is ' + width + 'px long in the size of 10px.');

// This text is 43.5px long in the size of 10px.
var pixelWidth = require('string-pixel-width');

const width = pixelWidth('My text ...', { font: 'impact', size: 10 });
console.log('This text is ' + width + 'px long in the size of 10px.');

// This text is 42px long in the size of 10px.
var pixelWidth = require('string-pixel-width');

const width = pixelWidth('My text ...', { font: 'open sans', size: 10, bold: true, italic: true });
console.log('This text is ' + width + 'px long in the size of 10px.');

// This text is 47px long in the size of 10px using bold and italic proportions.

How to contribute

PRs are welcome :) This library uses static map of width of every ASCII letter for all supported fonts.

  1. Clone the repository
  2. Open file src/pixelWidthCalculator.html in your editor
  3. Add your font name into array of fonts (currently var websafe) and save
  4. Open up src/pixelWidthCalculator.html in Google Chrome. It should look like this: PHPStan
  5. Map of withs will be generated for you - copy it and replace content of file src/widthsMap.js

Please everytime make sure you don't broke existing fonts - remove any font or broke his widths.

string-pixel-width's People

Contributors

adambisek avatar adambrgmn avatar dependabot[bot] avatar draftproducts avatar higgins avatar l1nknyto avatar rikkertkoppes avatar s-cardenas avatar ziacik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

string-pixel-width's Issues

Custom fonts

When you add new custom fonts, in addition to the steps described should not be needed to add also the font-faces definitions so when the character is displayed inside the sandbox to calculate the width will take it into account ?

Guide for adding a new font?

Hello! Thanks for this tool, I have been trying to solve this problem forever. I was wondering if you'd be able to provide some guidance on how to add a custom font, and maybe allow a kind of plugin system that allows you to pass in a custom font size map? If you can show me how to get the map, I'd be happy to submit a PR for the second part. Thanks!

Export as ES5 module

Today many build config does not support ES6 modules because it uses an old version of Uglify.

This is the case for create-react-app (see here)

Until uglify-es use spreads (see here), would it be possible to distribute this lib as an ES5 module ?

Only latin characters?

Looks like it doesn't work with non-latin characters, e.g. cyrillic.

Please make that very clear in Readme start, not in "How to contribute" section.

So that people don't waste their time.

License?

Is there any plan to add a license to this package such as MIT?

More polices

Hello,

I would like to know if you plan to add more fonts such as Roboto, Rubik, etc ...

If no, could you explain how to do it?

Thank you in advance

Freeze

Validate relation to font-size

Could you validate that you consider the font size correctly? Plus maybe add a thought on "px" vs. "pt" vs. "em"? For example: "Provide the font size in pixels. Other units like pt, em, and rem are not supported".

Your size calculator creates a <span> element with a font-size: 100; CSS property. However, the number 100 without a unit of measure seems to be no acceptable value for the font-size property. Correct would be to provide it as 100px.

Providing the font size without unit will make the calculated size depend on a lot of things, such as surrounding HTML elements etc. In your calculator's page, ultimately the defaults hard-coded into the browser the page runs in, will apply. According to the StackOverflow response linked above, this will usually be something smaller than 100px.

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.