fix layout's word break strategy

This commit is contained in:
侯歌 2024-05-17 10:53:44 +08:00
parent 21746a11d7
commit adae810ca7

View File

@ -500,7 +500,7 @@ void Layout::pushLineData(int start,int ytop,int descent,int width){
void Layout::relayout(bool force){
TextExtents extents;
FontExtents fontextents;
double total_width = 0;
double total_width = 0,word_width = 0;
int start=0,ytop=0;
std::wstring word;
if(!(force||mLayout)) return;
@ -520,32 +520,33 @@ void Layout::relayout(bool force){
case WORDBREAK_NOBREAK:
word.append(1,mText[i]);
measureSize(wch,extents);
if(total_width + extents.x_advance > mWidth){
word_width += extents.x_advance;
if(total_width + word_width > mWidth){
pushLineData(start,ytop,fontextents.descent,ceil(total_width));
ytop += mLineHeight;
mLineCount++;
total_width = 0 ;
start = mBreakStrategy?(i - word.length()):i;
start = i-word.length();
word.erase();
total_width = word_width;
word_width =0;
}
total_width += extents.x_advance;
//if(mBreakStrategy==0)word.erase();
break;
case WORDBREAK_BREAK:{
word.append(1,mText[i]);
measureSize(wch,extents);
if(mText[i]==10)extents.x_advance=0;
const int outofwidth = (total_width + extents.x_advance >mWidth);
word_width += extents.x_advance;
const int outofwidth = (total_width + word_width >mWidth);
if( (outofwidth && mBreakStrategy) || (linebreak==LINEBREAK_MUSTBREAK) ){
pushLineData(start,ytop,fontextents.descent,ceil(total_width));
ytop += mLineHeight;
mLineCount ++;
start = i+1;
if(outofwidth)//char[i] is wordbreak char must be in old lines
start = i - (word.length() - 1);
total_width = 0;
//char[i] is wordbreak char must be in old lines
start = outofwidth ? (i - word.length()) : (i+1);
total_width = 0;
}
total_width += extents.x_advance;
total_width += word_width;
word_width = 0;
word.erase();
}
break;
@ -594,7 +595,7 @@ void Layout::drawText(Canvas&canvas,int firstLine,int lastLine){
LOGD("%playoutWidth=%d fontSize=%.f alignment=%x breakStrategy=%d",this,mWidth,mFontSize,mAlignment,mBreakStrategy);
for (int lineNum = firstLine; lineNum < lastLine; lineNum++) {
int x = 0,lw = getLineWidth(lineNum,true);
TextExtents te;
TextExtents te,te2;
int y = getLineBaseline(lineNum);
int lineStart = getLineStart(lineNum);
int lineEnd = getLineEnd(lineNum);
@ -611,8 +612,9 @@ void Layout::drawText(Canvas&canvas,int firstLine,int lastLine){
case ALIGN_OPPOSITE:
case ALIGN_RIGHT : x = mWidth - lw ; break;
}
LOGV("line[%d/%d](%d,%d) [%s](%d).width=%d abearing=%f",
lineNum,mLineCount,x,y,TextUtils::unicode2utf8(line).c_str(),line.size(),lw,te.x_bearing);
measureSize(line,te2);
LOGV("line[%d/%d](%d,%d) [%s](%d).width=%d/%d abearing=%f",
lineNum,mLineCount,x,y,TextUtils::unicode2utf8(line).c_str(),line.size(),lw,int(te2.x_advance),te.x_bearing);
canvas.move_to(x - te.x_bearing,y);
canvas.show_text(processBidi(line));
if(mCaretPos>=lineStart&&mCaretPos<lineEnd){