npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

linefold

v1.0.15

Published

Convert given text into folded lines with maximum width and given font / given text length measuring function before rendering, so as to render paragraphs onto platforms without line-folding support, e.g. HTML5 Canvas.

Downloads

12

Readme

linefold

English | 中文

Convert given text into folded lines with maximum width and given font / given text length measuring function before rendering, so as to render paragraphs onto platforms without line-folding support, e.g. HTML5 Canvas.

This tool only supports languages like English, French, German, Russian, Chinese, Japanese, Korean, etc. Text with complex scripts (e.g. Arabic, Hebrew, Hindi, Thai) not supported.

Click here to open demo page

Loading

WebPack

import linefold from 'linefold';

Node.js

const linefold = require('linefold');

Script Tag

<script type='text/javascript' src='./dist/linefold.js'></script>

Usage

linefold(text,max_length,font);
linefold(text,max_length,(text)=>{
	//Your function for measuring text length
	return text.length;
});

Examples

Default text length measuring function based on HTML Canvas, with text length in pixel:

var text='Convert given text into folded lines with given font and maximum width before rendering, so as to render paragraphs onto platforms without line-folding support, e.g. HTML5 Canvas.';
var lines=linefold(text,320,'16px Times New Roman');
/*
Folding result:
[
	"Convert given text into folded lines with given",
	"font and maximum width before rendering, so as",
	"to render paragraphs onto platforms without line-",
	"folding support, e.g. HTML5 Canvas."
]
*/

Fold text under terminal environment with maximum width of 36 rows:

var text='Convert given text into folded lines with given font and maximum width before rendering, so as to render paragraphs onto platforms without line-folding support, e.g. HTML5 Canvas.';
var lines=linefold(text,36,(text)=>{
	let len=0;
	for(let char of text){
		len += (/[\u4E00-\u9FA5\u3000-\u303f\uFF01-\uFF5E]+/.test(char) ? 2 : 1);
	}
	return len;
});
/*
Folding result:
[
	"Convert given text into folded lines",
	"with given font and maximum width",
	"before rendering, so as to render",
	"paragraphs onto platforms without",
	"line-folding support, e.g. HTML5",
	"Canvas."
]
*/

Fold text with Chinese characters under terminal environment (each Chinese characters and punctions takes space of 2 characters):

var text='根据给定的文本、最大宽度、字体等条件,对一段文本进行换行操作。然后渲染到一些不支持自动换行的环境上,比如HTML5 Canvas。';
var lines=linefold(text,36,(text)=>{
	let len=0;
	for(let char of text){
		len += (/[\u4E00-\u9FA5\u3000-\u303f\uFF01-\uFF5E]+/.test(char) ? 2 : 1);
	}
	return len;
});
/*
Folding result:
[
	"根据给定的文本、最大宽度、字体等条",
	"件,对一段文本进行换行操作。然后渲染",
	"到一些不支持自动换行的环境上,比如",
	"HTML5 Canvas。"
]
*/

linefold

English | 中文

根据给定的文本、最大宽度、字体/文本长度测量函数,对一段文本进行换行操作。用于将文本渲染到一些不支持自动换行的环境上,比如HTML5 Canvas。

本工具仅支持英语、法语、德语、俄语、中日韩文字等文字。不支持含有复杂文字(如希伯来语、阿拉伯语、印地语、泰语)的文本。

单击此处打开演示页面

引入

WebPack

import linefold from 'linefold';

Node.js

const linefold = require('linefold');

Script Tag

<script type='text/javascript' src='path/to/linefold.js'></script>

使用方法

linefold(text,max_length,font);
linefold(text,max_length,(text)=>{
	//自定义文本长度测量函数
	return text.length;
});

范例

浏览器中使用默认的基于HTML Canvas的文本长度测量方式,单位为像素:

var text='根据给定的文本、最大宽度、字体等条件,对一段文本进行换行操作。然后渲染到一些不支持自动换行的环境上,比如HTML5 Canvas。';
var lines=linefold(text,320,'16px 宋体');
/*
换行结果:
[
	"根据给定的文本、最大宽度、字体等条件,对",
	"一段文本进行换行操作。然后渲染到一些不支",
	"持自动换行的环境上,比如HTML5 Canvas。"
]
*/

终端环境下将文本按最宽36列进行换行(中文字符和中文标点符号占2个字符的位置):

var text='根据给定的文本、最大宽度、字体等条件,对一段文本进行换行操作。然后渲染到一些不支持自动换行的环境上,比如HTML5 Canvas。';
var lines=linefold(text,36,(text)=>{
	let len=0;
	for(let char of text){
		len += (/[\u4E00-\u9FA5\u3000-\u303f\uFF01-\uFF5E]+/.test(char) ? 2 : 1);
	}
	return len;
});
/*
换行结果:
[
	"根据给定的文本、最大宽度、字体等条",
	"件,对一段文本进行换行操作。然后渲染",
	"到一些不支持自动换行的环境上,比如",
	"HTML5 Canvas。"
]
*/