# string-pixel-width

> String pixel width measurement on the backend in Javascript

Latest version **1.11.0** (published 2022-11-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install string-pixel-width
pnpm add string-pixel-width
yarn add string-pixel-width
bun add string-pixel-width
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.11.0 |
| Published | 2022-11-20 |
| First published | 2017-01-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/string-pixel-width) |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 263.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 107 |
| Author | Adam Bisek |
| Maintainers | adambisek |
| Keywords | string, pixel width |

## Links

- npm: https://www.npmjs.com/package/string-pixel-width
- Repository: https://github.com/adambisek/string-pixel-width
- npm.io page: https://npm.io/package/string-pixel-width

## Dependencies (1)

- [lodash.deburr](https://npm.io/package/lodash.deburr.md) ^4.1.0

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 1.11.0 (latest) — 2022-11-20
- 1.10.0 — 2019-07-13
- 1.9.0 — 2019-01-09
- 1.8.0 — 2018-11-06
- 1.7.0 — 2018-05-19
- 1.6.0 — 2018-05-16
- 1.5.0 — 2018-02-11
- 1.4.0 — 2017-11-14
- 1.3.0 — 2017-11-09
- 1.2.0 — 2017-09-09
- 1.1.1 — 2017-07-18
- 1.1.0 — 2017-07-18
- 1.0.7 — 2017-03-02
- 1.0.6 — 2017-01-03
- 1.0.5 — 2017-01-03
- … 4 more at https://npm.io/package/string-pixel-width/versions

## README

[string-pixel-width](https://github.com/adambisek/string-pixel-width)
===================================
[![Build Status](https://travis-ci.org/adambisek/string-pixel-width.svg?branch=master)](https://travis-ci.org/adambisek/string-pixel-width)
[![npm version](https://img.shields.io/npm/v/string-pixel-width.svg?style=flat-square)](https://www.npmjs.com/package/string-pixel-width)
[![Coverage Status](https://coveralls.io/repos/github/adambisek/string-pixel-width/badge.svg?branch=master)](https://coveralls.io/github/adambisek/string-pixel-width?branch=master)

String pixel width measurement on the backend in Javascript.

[Why I created this package? Read more about purpose](https://medium.com/@adambisek/text-pixel-width-measuring-on-javascript-backend-node-js-2b82bea97fab#.8ypyiffyw)

Installation
------------
To install the stable version:

<code>
npm install --save string-pixel-width
</code>

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](docs/string-pixel-width-1.png)
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.**

---
_Source: https://npm.io/package/string-pixel-width · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
