My approach in pseudocode:
- invoke analyse sth like that:
HDC hDC = mFont->font->GetDC();
HRESULT hr = ScriptStringAnalyse(hDC,
mBuffer,
lstrlenW(mBuffer) + 1, // NULL is also analyzed
lstrlenW(mBuffer) * 3/2 + 16, //size of buffer for glyph, recommended value 1.5 * length + 16
-1, //for unicode set -1
SSA_BREAK | SSA_GLYPHS | SSA_FALLBACK | SSA_LINK, // | SSA_FIT, SSA_CLIP
0, //127 this is piece of shit in the msdn, it doesn't divide the text into width = 127 px I have to do it on my own:/
&ScriptControl,
&ScriptState,
NULL,
NULL,
NULL,
&mAnalysis);
- then go trough each buffer character and collect the full words which are able to fit into the Width of editBox and catch all the specific ' ' which then You have to change into '\n' character.
This approach was improved, after each modification ' ' into '\n' I have to invoke again analyse(), because the X coordinates of chars change when You replace ' ' into '\n'.
However this approach doesn't solve the problem of alignment the text. Here I don't have idea. So in other words I would like to have the divided text and alignment to both borders in the buffer! That is why any setTextAlign function cannot be used.
This is my code which is trying to modify the mBuffer.
void Buffer::transformText(i32 width)
{
if(width < 0)
return;
if(mAnalyseRequired)
if(analyse() != OK)
return;
const SCRIPT_LOGATTR* pLogAttr = ScriptString_pLogAttr(mAnalysis);
if(!pLogAttr)
return;
if(!ScriptString_pcOutChars(mAnalysis))
return;
i32 limit = *ScriptString_pcOutChars(mAnalysis);
//calculate number of words
u32 numWords = 1;
for(i32 i=0; i<limit - 1; ++i) //without '\0'
if(pLogAttr.fWordStop && mBuffer != '\n') //'\n' is also softBreak, charStop and wordStop
++numWords;
//vector<i32> znaczniki;
mBeginMarks.clear();
mBeginMarks.swap(vector<i32>(mBeginMarks));
mBeginMarks.push_back(0);
mLines = 1;
i32 fromBegX = 0;
i32 charNr = 0;
for(i32 i=0; i<limit; ++i)
{
(void)mBuffer;
if((pLogAttr.fWhiteSpace && !pLogAttr[i+1].fWhiteSpace) || mBuffer == '\n' || mBuffer == '\0')
{
i32 X;
CPtoX(i, FALSE, &X);
if(width >= X-fromBegX && mBuffer != '\n')
charNr = i; //remember the last succeed char number
else
{
if(mBuffer == '\n')
charNr = i;
else
mBuffer[charNr] = '\n';
mAnalyseRequired = true;
analyse();
pLogAttr = ScriptString_pLogAttr(mAnalysis);
if(!pLogAttr)
return;
if(!ScriptString_pcOutChars(mAnalysis))
return;
limit = *ScriptString_pcOutChars(mAnalysis);
CPtoX(charNr, TRUE, &X);
fromBegX = X; //+= width;
++mLines;
//znaczniki.push_back(charNr);
mBeginMarks.push_back(X); //remember the X coordinate of the lines first chars
}
}
}
/*i32 size = static_cast<i32>(znaczniki.size());
for(i32 i = 0; i<size; ++i)
mBuffer[znaczniki] = '\n';*/
}