From 0a1426ff495f263cc097057b7206aff866a8eba8 Mon Sep 17 00:00:00 2001 From: Wetterquarz Date: Wed, 12 Apr 2023 16:49:13 +0200 Subject: [PATCH 1/3] Made non-BMP characters be treated as a single character for wrapping. --- src/graphic/helper/parseText.ts | 45 +++++++++++++++++++++++++++++++-- test/data/text.js | 5 +++- test/text-overflow.html | 31 +++++++++++++++++------ 3 files changed, 70 insertions(+), 11 deletions(-) diff --git a/src/graphic/helper/parseText.ts b/src/graphic/helper/parseText.ts index 201d8fc00..1ec949e6d 100644 --- a/src/graphic/helper/parseText.ts +++ b/src/graphic/helper/parseText.ts @@ -636,8 +636,8 @@ function wrapText( let accumWidth = 0; for (let i = 0; i < text.length; i++) { - - const ch = text.charAt(i); + let ch; + [ch, i] = getWholeCharAndI(text, i); if (ch === '\n') { if (currentWord) { line += currentWord; @@ -761,4 +761,45 @@ function wrapText( lines: lines, linesWidths }; +} + +// Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt#getting_whole_characters +function getWholeCharAndI(str: string, i: number): [string, number] { + const code = str.charCodeAt(i); + + if (isNaN(code)) { + return ["", i]; // Position not found + } + if (code < 0xd800 || code > 0xdfff) { + return [str.charAt(i), i]; // Normal character, keeping 'i' the same + } + + // High surrogate (could change last hex to 0xDB7F to treat high private + // surrogates as single characters) + if (0xd800 <= code && code <= 0xdbff) { + if (str.length <= i + 1) { + throw new Error("High surrogate without following low surrogate"); + } + const next = str.charCodeAt(i + 1); + if (next < 0xdc00 || next > 0xdfff) { + throw new Error("High surrogate without following low surrogate"); + } + return [str.charAt(i) + str.charAt(i + 1), i + 1]; + } + + // Low surrogate (0xDC00 <= code && code <= 0xDFFF) + if (i === 0) { + throw new Error("Low surrogate without preceding high surrogate"); + } + + const prev = str.charCodeAt(i - 1); + + // (could change last hex to 0xDB7F to treat high private surrogates + // as single characters) + if (prev < 0xd800 || prev > 0xdbff) { + throw new Error("Low surrogate without preceding high surrogate"); + } + + // Return the next character instead (and increment) + return [str.charAt(i + 1), i + 1]; } \ No newline at end of file diff --git a/test/data/text.js b/test/data/text.js index 9c7f9d92b..4a0ca352e 100644 --- a/test/data/text.js +++ b/test/data/text.js @@ -18,4 +18,7 @@ var LARGE_TEXT_ZH = `维龙说过一句富有哲理的话,要成功不需要   生活中,若科学和人文谁更有意义出现了,我们就不得不考虑它出现了的事实。 这种事实对本人来说意义重大,相信对这个世界也是有一定意义的。 一般来说, 杰纳勒尔·乔治·S·巴顿曾经说过,接受挑战,就可以享受胜利的喜悦。我希望诸位也能好好地体会这句话。 而这些并不是完全重要,更加重要的问题是, 科学和人文谁更有意义,发生了会如何,不发生又会如何。 科学和人文谁更有意义,发生了会如何,不发生又会如何。 要想清楚,科学和人文谁更有意义,到底是一种怎么样的存在。 所谓科学和人文谁更有意义,关键是科学和人文谁更有意义需要如何写。 生活中,若科学和人文谁更有意义出现了,我们就不得不考虑它出现了的事实。 我们一般认为,抓住了问题的关键,其他一切则会迎刃而解。 我们一般认为,抓住了问题的关键,其他一切则会迎刃而解。 既然如此, 我认为, 对我个人而言,科学和人文谁更有意义不仅仅是一个重大的事件,还可能会改变我的人生。 科学和人文谁更有意义,到底应该如何实现。 科学和人文谁更有意义因何而发生? 米歇潘曾经说过,生命是一条艰险的峡谷,只有勇敢的人才能通过。我希望诸位也能好好地体会这句话。 一般来讲,我们都必须务必慎重的考虑考虑。`; // https://github.com/JIUYANGZH/BullshitGenerator-English/blob/master/bullshitgenerator_English/bullshitgenerator.py -var LARGE_TEXT_EN = `Thus, we have to figure out what kind of exist brexit isMoreover, for many years, people with brexit have been viewed as different. It is disappointing that, for me, the emergent rise of brexit may change my life. By contrasts, what could we do anyway to ahchieve brexit. Chiefly, rebecca West told us that, the trouble about man is twofold. He cannot learn truths which are too complicated; he forgets truths which are too simple. That solved my problem. It is important to note that If we think about it from a different point of view, for example, friedrich Nietzsche stated in his book that, i have forgotten my umbrella.  That aroused my imagination. Similarly, with these questions, let's take a closer look to brexit. Therefore, we generally say, if we grasped the key of the problem of brexit, everthing else will be easily solved. With respect to We have been thinking about brexit for a long time.Above all, under this inevitable circumstance situation, we have to solve brexit. It is important to note that As this subject continues to be looked down on people must realize that brexit are becoming more common in today’s world. Above all, does the government provide help for brexit? Besides, several studies of the effects of brexit on the human systems have failed to demonstrate these effects. Similarly, the evidence presented in this assignment has shown that, in particular, david Mamet concluded that, it's only words.. unless they're true. That inspired me. However, brexit has become increasingly evident among teens according to many scientists, however, as this subject continues to be looked down on people must realize that brexit are becoming more common in today’s world. In addition, for me, the emergent rise of brexit may change my life. On the other hand, brexit continues to be a relevant controversial issue in society today, as In comparison, what should we do to give rise to brexit, and what should we do to prevent brexit's happening. Alternatively, the problem lies in the difference of people raised by brexit, on the contrary, what is the key point of brexit? To look at another way, the evidence presented in this assignment has shown that, to look at another way, for brexit, the key point is how to express brexit. Furthermore, why does brexit happen? It is important to note that What is the key point of brexit? Chiefly, according to Orson Scott Card, eko brushed a tear from her eye, and Immo jeered at her, but father held up a hand. "Never mock a tender heart," he said. That solved my problem. To look at another way, if we think about it from a different point of view, alternatively, what is the key point of brexit? Furthermore, as this subject continues to be looked down on people must realize that brexit are becoming more common in today’s world. It is important to note that We have been thinking about brexit for a long time.Since that is so, for me, the emergent rise of brexit may change my life. Thus, we have to face an embarrassing situation, that is, in that case, now, solving brexit is becoming a more and more significant issue, in fact, several studies of the effects of brexit on the human systems have failed to demonstrate these effects. To look at another way, when facing this difficult choice of brexit, i rarely slept well. By contrasts, if we think about it from a different point of view, as in the following example, rebecca McClanahan argued that, the shortest distance between two points is always under construction. Which enlighten me. With respect to If brexit takes place in our daliy life, we have to consider its consequence. In that case, several studies of the effects of brexit on the human systems have failed to demonstrate these effects. Besides, as far as I know, on the other hand, in general, however, brexit continues to be a relevant controversial issue in society today, as Thus, under this inevitable circumstance situation, we have to solve brexit. It is disappointing that, as far as I know, it is disappointing that, the problem lies in the difference of people raised by brexit, similarly, after seeing this evidence, there is no way we can agree with brexit. In other words, we have been thinking about brexit for a long time.In comparison, several studies of the effects of brexit on the human systems have failed to demonstrate these effects. On the other hand, brexit has become increasingly evident among teens according to many scientists, alternatively, more current studies of people who take brexit heavely show no evidence of emotional damage. On the one hand, from my piont of view, brexit means a lot to me, and In particular, rebecca West told us that, the trouble about man is twofold. He cannot learn truths which are too complicated; he forgets truths which are too simple. Which enlighten me. In particular, according to Ljupka Cvetanova, smile and the world will laugh at you. That inspired me. In addition, several studies of the effects of brexit on the human systems have failed to demonstrate these effects. Namely, steve Wozniak told us that, never trust a computer you can't throw out a window. That solved my problem. Besides, why does brexit happen? Since that is so, for me, the emergent rise of brexit may change my life. On the one hand, in my opinion, in fact, what is the key point of brexit? Above all, when facing this difficult choice of brexit, i rarely slept well. Another way of viewing this is, modern vehement arguments against brexit alone become suspect. As in the following example, according to David Mamet, it's only words.. unless they're true. That solved my problem. Similarly, why does brexit happen? Another way of viewing this is, we have to face an embarrassing situation, that is, in that case, brexit continues to be a relevant controversial issue in society today, as By contrasts, in general, on the one hand, we generally say, if we grasped the key of the problem of brexit, everthing else will be easily solved. In fact, now, solving brexit is becoming a more and more significant issue, above all, with these questions, let's take a closer look to brexit. However, what is the key point of brexit? In comparison, brexit is a common condition among civilians in today’s society, so, thus, after seeing this evidence, there is no way we can agree with brexit. With respect to we have to face an embarrassing situation, that is, it is disappointing that, what could we do anyway to ahchieve brexit. It is important to note that When facing this difficult choice of brexit, i rarely slept well. In particular, kamand Kojouri showed us that, what you choose also chooses you. That solved my problem. By contrasts, some people may disagree with brexit, by contrasts, as this subject continues to be looked down on people must realize that brexit are becoming more common in today’s world. Since that is so, more current studies of people who take brexit heavely show no evidence of emotional damage. Alternatively, for brexit, the key point is how to express brexit. Since that is so, what if brexit happens, and what if brexit does not happen. Since that is so, every person has to face these problems caused by brexit, when facing these problems, in fact, more current studies of people who take brexit heavely show no evidence of emotional damage. To look at another way, we have to face an embarrassing situation, that is, in that case, the problem lies in the difference of people raised by brexit, similarly, is inevitable. In fact, for me, the emergent rise of brexit may change my life. In comparison, what should we do to give rise to brexit, and what should we do to prevent brexit's happening. It is important to note that As this subject continues to be looked down on people must realize that brexit are becoming more common in today’s world. It is important to note that brexit has become increasingly evident among teens according to many scientists, that is to say, james Thurber mentioned that, all human beings should try to learn before they die what they are running from, and to, and why. That solved my problem. Similarly, the consequece of brexit is of great significance to me, and to many other people. Chiefly, to quote from William Penn, all Excess is ill: but Drunkenness is of the worst Sort. It spoils Health, dismounts the Mind, and unmans Men: it reveals Secrets, is Quarrelsome, lascivious, impudent, dangerous and Mad. In fine, he that is drunk is not a Man: because he is so long void of Reason, that distinguishes a Man from a Beast. That inspired me. Therefore, with these questions, let's take a closer look to brexit. Since that is so, as far as I know, thus, for me, the emergent rise of brexit may change my life. Thus, for brexit, the key point is how to express brexit. Above all, now, solving brexit is becoming a more and more significant issue, in fact, in my opinion, furthermore, is inevitable. Namely, plato, symposium / Phaedrus stated in his book that, there is truth in wine and children. That inspired me. Above all, as this subject continues to be looked down on people must realize that brexit are becoming more common in today’s world. In fact, what is the key point of brexit? On the one hand, but these are not the most urgent issue, a more pressing issue about brexit is, alternatively, the problem lies in the difference of people raised by brexit, on the other hand, but these are not the most urgent issue, a more pressing issue about brexit is, in addition, the problem lies in the difference of people raised by brexit, thus, if brexit takes place in our daliy life, we have to consider its consequence. Another possibility is, to a certain extent, brexit is right. That is to say, james Thurber showed us that, all human beings should try to learn before they die what they are running from, and to, and why. Which enlighten me. In fact, in my opinion, namely, to quote from Comte de Lautréamont, we say sound things when we do not strive to say to say extraordinary ones. Which brought a new way of thinking it. `; \ No newline at end of file +var LARGE_TEXT_EN = `Thus, we have to figure out what kind of exist brexit isMoreover, for many years, people with brexit have been viewed as different. It is disappointing that, for me, the emergent rise of brexit may change my life. By contrasts, what could we do anyway to ahchieve brexit. Chiefly, rebecca West told us that, the trouble about man is twofold. He cannot learn truths which are too complicated; he forgets truths which are too simple. That solved my problem. It is important to note that If we think about it from a different point of view, for example, friedrich Nietzsche stated in his book that, i have forgotten my umbrella.  That aroused my imagination. Similarly, with these questions, let's take a closer look to brexit. Therefore, we generally say, if we grasped the key of the problem of brexit, everthing else will be easily solved. With respect to We have been thinking about brexit for a long time.Above all, under this inevitable circumstance situation, we have to solve brexit. It is important to note that As this subject continues to be looked down on people must realize that brexit are becoming more common in today’s world. Above all, does the government provide help for brexit? Besides, several studies of the effects of brexit on the human systems have failed to demonstrate these effects. Similarly, the evidence presented in this assignment has shown that, in particular, david Mamet concluded that, it's only words.. unless they're true. That inspired me. However, brexit has become increasingly evident among teens according to many scientists, however, as this subject continues to be looked down on people must realize that brexit are becoming more common in today’s world. In addition, for me, the emergent rise of brexit may change my life. On the other hand, brexit continues to be a relevant controversial issue in society today, as In comparison, what should we do to give rise to brexit, and what should we do to prevent brexit's happening. Alternatively, the problem lies in the difference of people raised by brexit, on the contrary, what is the key point of brexit? To look at another way, the evidence presented in this assignment has shown that, to look at another way, for brexit, the key point is how to express brexit. Furthermore, why does brexit happen? It is important to note that What is the key point of brexit? Chiefly, according to Orson Scott Card, eko brushed a tear from her eye, and Immo jeered at her, but father held up a hand. "Never mock a tender heart," he said. That solved my problem. To look at another way, if we think about it from a different point of view, alternatively, what is the key point of brexit? Furthermore, as this subject continues to be looked down on people must realize that brexit are becoming more common in today’s world. It is important to note that We have been thinking about brexit for a long time.Since that is so, for me, the emergent rise of brexit may change my life. Thus, we have to face an embarrassing situation, that is, in that case, now, solving brexit is becoming a more and more significant issue, in fact, several studies of the effects of brexit on the human systems have failed to demonstrate these effects. To look at another way, when facing this difficult choice of brexit, i rarely slept well. By contrasts, if we think about it from a different point of view, as in the following example, rebecca McClanahan argued that, the shortest distance between two points is always under construction. Which enlighten me. With respect to If brexit takes place in our daliy life, we have to consider its consequence. In that case, several studies of the effects of brexit on the human systems have failed to demonstrate these effects. Besides, as far as I know, on the other hand, in general, however, brexit continues to be a relevant controversial issue in society today, as Thus, under this inevitable circumstance situation, we have to solve brexit. It is disappointing that, as far as I know, it is disappointing that, the problem lies in the difference of people raised by brexit, similarly, after seeing this evidence, there is no way we can agree with brexit. In other words, we have been thinking about brexit for a long time.In comparison, several studies of the effects of brexit on the human systems have failed to demonstrate these effects. On the other hand, brexit has become increasingly evident among teens according to many scientists, alternatively, more current studies of people who take brexit heavely show no evidence of emotional damage. On the one hand, from my piont of view, brexit means a lot to me, and In particular, rebecca West told us that, the trouble about man is twofold. He cannot learn truths which are too complicated; he forgets truths which are too simple. Which enlighten me. In particular, according to Ljupka Cvetanova, smile and the world will laugh at you. That inspired me. In addition, several studies of the effects of brexit on the human systems have failed to demonstrate these effects. Namely, steve Wozniak told us that, never trust a computer you can't throw out a window. That solved my problem. Besides, why does brexit happen? Since that is so, for me, the emergent rise of brexit may change my life. On the one hand, in my opinion, in fact, what is the key point of brexit? Above all, when facing this difficult choice of brexit, i rarely slept well. Another way of viewing this is, modern vehement arguments against brexit alone become suspect. As in the following example, according to David Mamet, it's only words.. unless they're true. That solved my problem. Similarly, why does brexit happen? Another way of viewing this is, we have to face an embarrassing situation, that is, in that case, brexit continues to be a relevant controversial issue in society today, as By contrasts, in general, on the one hand, we generally say, if we grasped the key of the problem of brexit, everthing else will be easily solved. In fact, now, solving brexit is becoming a more and more significant issue, above all, with these questions, let's take a closer look to brexit. However, what is the key point of brexit? In comparison, brexit is a common condition among civilians in today’s society, so, thus, after seeing this evidence, there is no way we can agree with brexit. With respect to we have to face an embarrassing situation, that is, it is disappointing that, what could we do anyway to ahchieve brexit. It is important to note that When facing this difficult choice of brexit, i rarely slept well. In particular, kamand Kojouri showed us that, what you choose also chooses you. That solved my problem. By contrasts, some people may disagree with brexit, by contrasts, as this subject continues to be looked down on people must realize that brexit are becoming more common in today’s world. Since that is so, more current studies of people who take brexit heavely show no evidence of emotional damage. Alternatively, for brexit, the key point is how to express brexit. Since that is so, what if brexit happens, and what if brexit does not happen. Since that is so, every person has to face these problems caused by brexit, when facing these problems, in fact, more current studies of people who take brexit heavely show no evidence of emotional damage. To look at another way, we have to face an embarrassing situation, that is, in that case, the problem lies in the difference of people raised by brexit, similarly, is inevitable. In fact, for me, the emergent rise of brexit may change my life. In comparison, what should we do to give rise to brexit, and what should we do to prevent brexit's happening. It is important to note that As this subject continues to be looked down on people must realize that brexit are becoming more common in today’s world. It is important to note that brexit has become increasingly evident among teens according to many scientists, that is to say, james Thurber mentioned that, all human beings should try to learn before they die what they are running from, and to, and why. That solved my problem. Similarly, the consequece of brexit is of great significance to me, and to many other people. Chiefly, to quote from William Penn, all Excess is ill: but Drunkenness is of the worst Sort. It spoils Health, dismounts the Mind, and unmans Men: it reveals Secrets, is Quarrelsome, lascivious, impudent, dangerous and Mad. In fine, he that is drunk is not a Man: because he is so long void of Reason, that distinguishes a Man from a Beast. That inspired me. Therefore, with these questions, let's take a closer look to brexit. Since that is so, as far as I know, thus, for me, the emergent rise of brexit may change my life. Thus, for brexit, the key point is how to express brexit. Above all, now, solving brexit is becoming a more and more significant issue, in fact, in my opinion, furthermore, is inevitable. Namely, plato, symposium / Phaedrus stated in his book that, there is truth in wine and children. That inspired me. Above all, as this subject continues to be looked down on people must realize that brexit are becoming more common in today’s world. In fact, what is the key point of brexit? On the one hand, but these are not the most urgent issue, a more pressing issue about brexit is, alternatively, the problem lies in the difference of people raised by brexit, on the other hand, but these are not the most urgent issue, a more pressing issue about brexit is, in addition, the problem lies in the difference of people raised by brexit, thus, if brexit takes place in our daliy life, we have to consider its consequence. Another possibility is, to a certain extent, brexit is right. That is to say, james Thurber showed us that, all human beings should try to learn before they die what they are running from, and to, and why. Which enlighten me. In fact, in my opinion, namely, to quote from Comte de Lautréamont, we say sound things when we do not strive to say to say extraordinary ones. Which brought a new way of thinking it. `; + +// Chakma U+11107 - U+11126 +var TEXT_WITH_NON_BMP_CHARS = `Non-BMP characters: 𑄇𑄈𑄉𑄊𑄋𑄌𑄍𑄎𑄏𑄐𑄑𑄒𑄓𑄔𑄕𑄖𑄗𑄘𑄙𑄚𑄛𑄜𑄝𑄞𑄟𑄠𑄡𑄢𑄣𑄤𑄥𑄦𑄇𑄈𑄉𑄊𑄋𑄌𑄍𑄎𑄏𑄐𑄑𑄒𑄓𑄔𑄕𑄖𑄗𑄘𑄙𑄚𑄛𑄜𑄝𑄞𑄟𑄠𑄡𑄢𑄣𑄤𑄥𑄦𑄇𑄈𑄉𑄊𑄋𑄌𑄍𑄎𑄏𑄐𑄑𑄒𑄓𑄔𑄕𑄖𑄗𑄘𑄙𑄚𑄛𑄜𑄝𑄞𑄟𑄠𑄡𑄢𑄣𑄤𑄥𑄦𑄇𑄈𑄉𑄊𑄋𑄌𑄍𑄎𑄏𑄐𑄑𑄒𑄓𑄔𑄕𑄖𑄗𑄘𑄙𑄚𑄛𑄜𑄝𑄞𑄟𑄠𑄡𑄢𑄣𑄤𑄥𑄦`; \ No newline at end of file diff --git a/test/text-overflow.html b/test/text-overflow.html index ac9191f52..b0c15a98a 100644 --- a/test/text-overflow.html +++ b/test/text-overflow.html @@ -70,15 +70,26 @@ }); zr.add(cnText); - const TEXTS = [LARGE_TEXT_EN, LARGE_TEXT_ZH]; - function update() { - enText.style.width = cnText.style.width = config.width; - enText.style.height = cnText.style.height = config.height; + var rndText = new zrender.Text({ + style: { + text: TEXT_WITH_NON_BMP_CHARS, + fontSize: config.fontSize, + width: config.width, + padding: 10, + borderColor: '#000', + borderWidth: 1, - enText.style.overflow = config.breakAll ? 'breakAll' : 'break'; + overflow: 'break', + lineOverflow: 'truncate', + ellipsis: '…', - cnText.style.x = config.width + 100; + x: config.width + 100 + config.width + 100 + } + }); + zr.add(rndText); + const TEXTS = [LARGE_TEXT_EN, LARGE_TEXT_ZH, TEXT_WITH_NON_BMP_CHARS]; + function update() { enText.style.rich = cnText.style.rich = config.rich ? { highlight: { // padding: 4, @@ -87,8 +98,12 @@ } } : null; - [enText, cnText].forEach((text, idx) => { - text.style.fontSize = cnText.style.fontSize = config.fontSize; + [enText, cnText, rndText].forEach((text, idx) => { + text.style.fontSize = config.fontSize; + text.style.width = config.width; + text.style.height = config.height; + text.style.x = (config.width + 100) * idx; + text.style.overflow = config.breakAll ? 'breakAll' : 'break'; if (config.search) { text.style.text = TEXTS[idx].replace( From 2fd53039136fd434bc20eedb908edaf806903da0 Mon Sep 17 00:00:00 2001 From: Wetterquarz Date: Thu, 13 Apr 2023 14:02:32 +0200 Subject: [PATCH 2/3] Wrapping can no longer throw Errors and instead displays illegal characters --- src/graphic/helper/parseText.ts | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/graphic/helper/parseText.ts b/src/graphic/helper/parseText.ts index 1ec949e6d..e0afed822 100644 --- a/src/graphic/helper/parseText.ts +++ b/src/graphic/helper/parseText.ts @@ -766,40 +766,40 @@ function wrapText( // Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt#getting_whole_characters function getWholeCharAndI(str: string, i: number): [string, number] { const code = str.charCodeAt(i); - + if (isNaN(code)) { - return ["", i]; // Position not found + return ['', i]; // Position not found } if (code < 0xd800 || code > 0xdfff) { return [str.charAt(i), i]; // Normal character, keeping 'i' the same } - + // High surrogate (could change last hex to 0xDB7F to treat high private // surrogates as single characters) if (0xd800 <= code && code <= 0xdbff) { if (str.length <= i + 1) { - throw new Error("High surrogate without following low surrogate"); + return [str.charAt(i), i]; // Error: High surrogate without following low surrogate } const next = str.charCodeAt(i + 1); if (next < 0xdc00 || next > 0xdfff) { - throw new Error("High surrogate without following low surrogate"); + return [str.charAt(i), i]; // Error: High surrogate without following low surrogate } return [str.charAt(i) + str.charAt(i + 1), i + 1]; } - + // Low surrogate (0xDC00 <= code && code <= 0xDFFF) if (i === 0) { - throw new Error("Low surrogate without preceding high surrogate"); + return [str.charAt(i), i]; // Error: Low surrogate without preceding high surrogate } - + const prev = str.charCodeAt(i - 1); - + // (could change last hex to 0xDB7F to treat high private surrogates // as single characters) if (prev < 0xd800 || prev > 0xdbff) { - throw new Error("Low surrogate without preceding high surrogate"); + return [str.charAt(i), i]; // Error: Low surrogate without preceding high surrogate } - + // Return the next character instead (and increment) return [str.charAt(i + 1), i + 1]; } \ No newline at end of file From f72d80aa6dd9f8c4e76253fc973aee4125572486 Mon Sep 17 00:00:00 2001 From: Wetterquarz Date: Thu, 13 Apr 2023 14:52:01 +0200 Subject: [PATCH 3/3] Spaces now have special wrapping behaviour Wrapping now happens after spaces instead of before and only after the last space in a sequence of spaces. Characters which should not have a line break in front of them have been removed form the list, which become redundant as a consequence. --- src/graphic/helper/parseText.ts | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/graphic/helper/parseText.ts b/src/graphic/helper/parseText.ts index e0afed822..796787913 100644 --- a/src/graphic/helper/parseText.ts +++ b/src/graphic/helper/parseText.ts @@ -2,10 +2,9 @@ import * as imageHelper from '../helper/image'; import { extend, retrieve2, - retrieve3, - reduce + retrieve3 } from '../../core/util'; -import { TextAlign, TextVerticalAlign, ImageLike, Dictionary } from '../../core/types'; +import { TextAlign, TextVerticalAlign, ImageLike } from '../../core/types'; import { TextStyleProps } from '../Text'; import { getLineHeight, getWidth, parsePercent } from '../../contain/text'; @@ -604,21 +603,11 @@ function isAlphabeticLetter(ch: string) { || code >= 0x1E00 && code <= 0x206F; // Latin and Greek extended } -const breakCharMap = reduce(',&?/;] '.split(''), function (obj, ch) { - obj[ch] = true; - return obj; -}, {} as Dictionary); /** * If break by word. For latin languages. */ -function isWordBreakChar(ch: string) { - if (isAlphabeticLetter(ch)) { - if (breakCharMap[ch]) { - return true; - } - return false; - } - return true; +function canLineBreakBeforeChar(ch: string) { + return !isAlphabeticLetter(ch); } function wrapText( @@ -654,7 +643,16 @@ function wrapText( } const chWidth = getWidth(ch, font); - const inWord = isBreakAll ? false : !isWordBreakChar(ch); + + if (ch === ' ') { + line += currentWord + ch; + accumWidth += chWidth; + currentWord = ''; + currentWordWidth = 0; + continue; + } + + const inWord = isBreakAll ? false : !canLineBreakBeforeChar(ch); if (!lines.length ? lastAccumWidth + accumWidth + chWidth > lineWidth