Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
github.com/Jam3/opentype-layout
Experimental word-wrapping and layout for Opentype.js.
Best used with npm and browserify. This should also work in Node.js and other environments.
npm install opentype-layout --save
The demo shows Canvas2D vector text (red stroke) rendered on top of DOM/CSS (black fill). The demo reads the computed CSS style and converts units into the proper EM font units. Some lines also visualize some of the available metrics.
See the demo folder for a complete example.
var opentype = require('opentype.js');
var computeLayout = require('opentype-layout');
opentype.load('Font.ttf', function (err, font) {
if (err) throw err;
var fontSizePx = 72;
var text = 'Hello\nWorld! This box should start word-wrapping!'
var scale = 1 / font.unitsPerEm * fontSizePx;
// Layout some text - notice everything is in em units!
var result = computeLayout(font, text, {
lineHeight: 2.5 * font.unitsPerEm, // '2.5em' in font units
width: 500 / scale // '500px' in font units
});
// Array of characters after layout
console.log(result.glyphs);
// Computed height after word-wrap
console.log(result.height);
});
layout = computeLayout(font, text, [opt])
Computes a new layout from the given Opentype.js Font
interface and a text
string.
All units should be in raw font units in the EM square, assuming a lower-left origin. For example, a lineHeight
of '2em'
should be passed as 2 * font.unitsPerEm
. It is up to the user to scale the results to a pixel/point size after the fact.
Options:
width
the width of the box in font units, will cause word-wrapping (default Infinity
)align
string alignment of the text within its width
(default 'left'
)letterSpacing
the additional letter spacing in font units (default 0)lineHeight
the line height in font units as per CSS spec, default 1.175 * font.unitsPerEm
to match browsersstart
the starting character index into text
to layout, default 0end
the ending index into text
to layout (exclusive), default text.length
mode
can be 'pre' (maintain spacing), or 'nowrap' (collapse whitespace but only break on newline characters), otherwise defaults to normal word-wrap behaviourSee word-wrapper for details on how word wrapping is computed.
The returned object has the following metrics.
layout.glyphs
This provides an array of characters after layout, useful for rendering. Each element in the array has the following properties:
{
position: [ x, y ],
data: { ... Opentype.js Glyph object ... },
index: charIndex,
row: lineIndex,
column: columnInLineIndex
}
The position is in raw font units.
layout.baseline
This is the value from pen origin to the baseline of the last line of text in the layout.
layout.leading
This is the L
value in the CSS line-height spec. Divide this by two for the "half-leading", which tells you how far above the first ascender and below the last descender the text box extends to.
layout.lines
This is an array of line objects with the following properties:
{
start: startCharIndex, // inclusive
end: endCharIndex, // exclusive
width: lineWidth // in font units
}
layout.lineHeight
This is the computed lineHeight
in font units. If no lineHeight
is specified in options, it will be equivalent to 1.175 * font.unitsPerEm
.
layout.left
This is the distance from the left of the text box to the widest line of text in the box. This is zero when align
is left, but changes with other alignments.
layout.right
This is the distance from the right of the box to the widest line of text in the box. This is zero when align
is right, but changes with other alignments.
layout.width
The width of the text box. If no opt.width
is passed, this will equal layout.maxLineWidth
(i.e. length of a single line of text). If opt.width
is passed, this value should equal it.
layout.height
The height of the text box, including the half leadings above the first ascender and below the last descender.
layout.maxLineWidth
This is the maximum line width in all lines. This can be used to determine the "real" width of the text box after word wrap, instead of the layout.width
which may be larger.
This module is not yet finished — below are some areas that need improvement. PRs welcome.
'center'
and 'right'
alignment do not match exactly with DOM/CSSMIT, see LICENSE.md for details.
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.