How to use linebreak - 4 common examples

To help you get started, we’ve selected a few linebreak examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github foliojs / textkit / packages / linebreaker / index.js View on Github external
findBreakPreceeding(string, index) {
      const breaker = new LineBreak(string);
      let last = null;
      let bk = null;

      while ((bk = breaker.nextBreak())) {
        // console.log(bk);
        if (bk.position > index) {
          if (last) {
            last.next = bk.position;
          }

          return last;
        }

        if (bk.required) {
          return bk;
        }
github HearthSim / Sunwell / src / Components / BodyText.ts View on Github external
let italic = 0;
		let bold = 0;
		let lineCount = 0;
		let justLineBreak: boolean;
		// size of the description text box
		const bodyWidth = this.parent.bodyTextCoords.dWidth;
		const bodyHeight = this.parent.bodyTextCoords.dHeight;
		// center of description box (x, y)
		const centerLeft = this.parent.bodyTextCoords.dx + bodyWidth / 2;
		const centerTop = this.parent.bodyTextCoords.dy + bodyHeight / 2;

		const bodyText = this.parseBodyText(this.parent.getBodyText());
		this.sunwell.log("Body text", bodyText);

		const words: string[] = [];
		const breaker = new LineBreaker(bodyText);
		let last = 0;
		let bk;
		while ((bk = breaker.nextBreak())) {
			words.push(bodyText.slice(last, bk.position).replace("\n", ""));
			last = bk.position;
			if (bk.required) {
				words.push("\n");
			}
		}
		this.sunwell.log("Words", words);

		const bufferText = this.sunwell.getBuffer(bodyWidth, bodyHeight, true);
		const bufferTextCtx = bufferText.getContext("2d");
		bufferTextCtx.fillStyle = this.parent.bodyTextColor;

		let fontSize = this.sunwell.options.bodyFontSize;
github bpampuch / pdfmake / src / TextBreaker.js View on Github external
const splitWords = (text, noWrap) => {
	let words = [];

	if (noWrap) {
		words.push({ text: text });
		return words;
	}

	let breaker = new LineBreaker(text);
	let last = 0;
	let bk;

	while ((bk = breaker.nextBreak())) {
		let word = text.slice(last, bk.position);

		if (bk.required || word.match(/\r?\n$|\r$/)) { // new line
			word = word.replace(/\r?\n$|\r$/, '');
			words.push({ text: word, lineEnd: true });
		} else {
			words.push({ text: word });
		}

		last = bk.position;
	}
github foliojs / pdfkit / lib / line_wrapper.js View on Github external
eachWord(text, fn) {
    // setup a unicode line breaker
    let bk;
    const breaker = new LineBreaker(text);
    let last = null;
    const wordWidths = Object.create(null);

    while ((bk = breaker.nextBreak())) {
      var shouldContinue;
      let word = text.slice(
        (last != null ? last.position : undefined) || 0,
        bk.position
      );
      let w =
        wordWidths[word] != null
          ? wordWidths[word]
          : (wordWidths[word] = this.wordWidth(word));

      // if the word is longer than the whole line, chop it up
      // TODO: break by grapheme clusters, not JS string characters

linebreak

An implementation of the Unicode Line Breaking Algorithm (UAX #14)

MIT
Latest version published 3 years ago

Package Health Score

65 / 100
Full package analysis

Popular linebreak functions