int TypewriterText::GetWidthOfNextWord(char* String, int CurrentPos) { int Len = 0; Font* Ft = GetFontBySizeIndex(mFontNumber); if(!Ft) return 0; for(;;CurrentPos++) { Len += Ft->GetCharWidth(String[CurrentPos]); if(String[CurrentPos] == '\n' || String[CurrentPos] == ' ' || String[CurrentPos] == '\0') { break; } } return Len; }
void TextSegment::Wrap(TextArea *theTextArea, TextLine *theLine) { mLine = NULL; // set mLine to NULL then reset it to aLine on the first line to which we add a segment piece int aCurPos = 0; GUIString &aStr = mText; Font *aFM = mFont; int aTextBoxWidth = theTextArea->GetScrollArea().Width(); TextLine *aLine = theLine; while(aCurPos < aStr.length()) { int aWidth = aLine->mWidth; int aBreakWidth = aLine->mWidth; int aStartPos = aCurPos; int aBreakPos = aStartPos-1; while(aCurPos < aStr.length()) { int aChar = aStr.at(aCurPos); aWidth += aFM->GetCharWidth(aChar); if(aChar==' ') { aBreakPos = aCurPos; aBreakWidth = aWidth; } if(aWidth > aTextBoxWidth) break; aCurPos++; } bool forceBreak = false; if(aBreakPos < aStartPos) { if(aWidth < aTextBoxWidth) aBreakPos = aStr.length(); else if(aLine->mSegmentList.empty()) // force the break in the middle of the word if we're at the beginning of the line { aBreakPos = aCurPos; aBreakWidth = aTextBoxWidth; if(aBreakPos==aStartPos) // need at least one character per line aBreakPos++; forceBreak = true; } else // otherwise try again on new line aCurPos = 0; } if(aBreakPos >= aStartPos) { if(aBreakPos!=aStartPos) { theTextArea->AddSeg(new TextSegmentPiece(this, aStartPos, aBreakPos, aBreakWidth/*aTextBoxWidth-aLine->mWidth*/)); if(mLine==NULL) mLine = aLine; } aCurPos = aBreakPos; if(!forceBreak) // Skip the space where we wrapped aCurPos++; } if(aCurPos>=aStr.length()) break; aLine = theTextArea->AddLine(mHeight, mAscent); if(theTextArea->mLineAlign==HorzAlign_Left) aLine->mWidth = theTextArea->mWrapIndentSize; aWidth = aFM->GetStringWidth(aStr, aCurPos, aStr.length()-aCurPos); if(aWidth <= aTextBoxWidth - aLine->mWidth) { theTextArea->AddSeg(new TextSegmentPiece(this, aCurPos, aStr.length(), aWidth)); if(mLine==NULL) mLine = aLine; break; } } }