Paint.breakText 是否包括 maxWidth 处的字符?

Does Paint.breakText include the character at the maxWidth?

Paint.breakTextdocumentation

breakText

int breakText (CharSequence text,  
               int start,  
               int end,  
               boolean measureForwards,  
               float maxWidth,  
               float[] measuredWidth)  

Measure the text, stopping early if the measured width exceeds maxWidth. Return the number of chars that were measured, and if measuredWidth is not null, return in it the actual width measured.

Returns
int The number of chars that were measured. Will always be <= abs(end - start).

不清楚返回的字符数是否包括超过 maxWidth 的字符数,因为该字符可能是在确定总测量宽度超过 maxWidth 之前测量的。

也就是说,如果我的红线代表maxWidth,它会包括下图中Worldo吗?

我在下面回答我的问题作为自我回答。

否,breakText不包括使它超过maxWidth的字符。

我们可以通过下面的代码看到这一点。

String text = "Hello World";
Paint paint = new Paint();
paint.setTextSize(100);

// Measure the substrings individually
int length = text.length();
for (int i = 1; i <= length; i++) {
    float totalWidth = paint.measureText(text, 0, i);
    Log.i("TAG", i + ", totalWidth of " + text.substring(0, i) + ": " + totalWidth);
}

// compare these to breakText
float[] measuredWidth = new float[1];
float maxWidth = 360; // halfway through the "o" of "World"
int countedChars = paint.breakText(text, 0, length, true, maxWidth, measuredWidth);
Log.i("TAG", "countedChars: " + countedChars + " (\"" + text.substring(0, countedChars) + "\")");
Log.i("TAG", "measuredWidth: " + measuredWidth[0]);

// 1, totalWidth of H: 70.0
// 2, totalWidth of He: 123.0
// 3, totalWidth of Hel: 148.0
// 4, totalWidth of Hell: 173.0
// 5, totalWidth of Hello: 230.0
// 6, totalWidth of Hello : 255.0
// 7, totalWidth of Hello W: 344.0
// 8, totalWidth of Hello Wo: 399.0
// 9, totalWidth of Hello Wor: 433.0
//10, totalWidth of Hello Worl: 458.0
//11, totalWidth of Hello World: 515.0

// countedChars: 7 ("Hello W")
// measuredWidth: 342.0

为什么 measureText 给出的值 (344) 与 breakText (342) 给出的值略有不同是另一个问题。我的猜测是它可能与 Wo 之间的字距调整有关。