If you draw text on a canvas, a common first wrapping function is: measure the line so far, and when the next character no longer fits, start a new line. On plain Chinese it looks fine. On one punctuation-heavy sample it put a closing quote, a closing bracket and an enumeration comma at the start of lines, 3 of the 7 lines at a 200px width. This post is what I learned about why that is wrong, and which characters are not allowed to start a line.

ImgIng https://imging.ai/ has an online image editor whose text layer breaks lines where you press Enter, so where a line ends is up to the person using it. That raises the question of how a program would decide the same thing. The numbers below come from one run on one Apple-silicon Mac, in three desktop browser engines (the code output shown is from the Chromium one), with the system Chinese font (PingFang) at 20px. The text samples are self-made, and the last sentence of the punctuation sample quotes the Book of Songs, a public-domain poem. It is one font, one size and five texts at three widths (200, 280 and 360px), each run once.

This is the whole function. It walks the string, measures the current line plus the next character, and breaks when the width goes past the limit.

const ctx = document.createElement('canvas').getContext('2d');
ctx.font = '20px "PingFang SC", "Hiragino Sans GB", sans-serif';

function wrapNaive(text, maxW) {
  const lines = []; let cur = '';
  for (const ch of text) {
    if (cur && ctx.measureText(cur + ch).width > maxW) { lines.push(cur); cur = ch; }
    else cur += ch;
  }
  if (cur) lines.push(cur);
  return lines;
}
console.log(wrapNaive(T3, 200));

T3 is the 66-character sample: 她问:“这是真的吗?”他答:“当然——千真万确!”(众人笑。)于是,大家都说:好、好、好……《诗经》里也有:“关关雎鸠,在河之洲。” The output at 200px, complete and unedited:

[
 "她问:“这是真的吗?",
 "”他答:“当然——千",
 "真万确!”(众人笑。",
 ")于是,大家都说:好",
 "、好、好……《诗经》",
 "里也有:“关关雎鸠,",
 "在河之洲。”"
]

Lines 2, 4 and 5 start with ” and ) and 、. A reader would call that a typesetting mistake, even though every line fits the width.

The browser's default layout avoids this, and the idea is simple. Closing punctuation belongs with the word before it, so it should never be pushed to the start of the next line. There is a mirror rule: an opening bracket or opening quote should not be left alone at the end of a line.

The table I used for this run is my own, not a standard. Not allowed at the start: ,。、;:?!)》」』】〉…—~ plus the closing quotes and ASCII equivalents. Not allowed at the end: (《「『【〈“‘. It is deliberately stricter than the browser, which matters below.

The fix has two parts. First split the text into tokens, where an English word or number string stays together and each Chinese or Japanese character is its own token. Then break greedily as before, but when a line would start with a forbidden character, pull the last token of the previous line down with it. Opening brackets get the same treatment in the other direction.

Against the browser's own layout for the same font and width, this version produced identical lines in 11 of the 15 cases in Chromium and WebKit, and in 7 of 15 in Firefox. The naive loop matched in only 2 of 15. A row count can look right while the breaks are wrong: on the punctuation-heavy sample at 200px the naive loop and the browser both give 7 rows, yet the lines are different.

One gap stands out: on this same sample at 200px, the token version used 8 lines and the browser used 7. One reason is visible in the lines. My table also forbids the em dash at a line start, and the browser's default does not, so it let —— start line 3. Whether anything else adds to the gap was not chased here. On both sides, 吗?” moved down together as a block of three.

line-break: loose goes further than the default and also lets ? and : start a line. That reading needs the page or element to carry a Chinese or Japanese lang; without one, loose matched the default line for line in Chromium. On the Japanese side, the long-vowel mark ー may start a line by default in Chromium and WebKit, and strict forbids it. Firefox does not allow it even by default.

For a canvas, a character loop is not enough once the text has closing punctuation. Tokenize, add the two prohibition rules, and compare against a DOM block of the same width, because the browser is the reference your readers are used to. Keep the table in one place, since "which characters are forbidden" is a decision, not a fact. And do not copy breaks from one browser engine to another: Firefox's Latin letters came out about 2% wider in the same font stack, so mixed text can break differently.

This run did not test other fonts, Windows or phones, vertical text, or right-to-left scripts. The Firefox and WebKit numbers are one run each and say nothing about Safari or other versions.

原文来源:dev.to