C++使用Uniscribe进行文字自动换行的计算和渲染(四)

2014-11-24 09:13:54 · 作者: · 浏览: 3
false;}

bool operator==(const GOFFSET&, const GOFFSET&){return false;}
bool operator!=(const GOFFSET&, const GOFFSET&){return false;}

bool operator==(const SCRIPT_LOGATTR&, const SCRIPT_LOGATTR&){return false;}
bool operator!=(const SCRIPT_LOGATTR&, const SCRIPT_LOGATTR&){return false;}

namespace test
{

/***********************************************************************
DocumentFragment
***********************************************************************/

class DocumentFragment : public Object
{
public:
bool paragraph;
WString font;
bool bold;
Color color;
int size;
WString text;
Ptr fontObject;

DocumentFragment()
:paragraph(false)
,bold(false)
,size(0)
{
}

DocumentFragment(Ptr prototype, const WString& _text)
:paragraph(prototype->paragraph)
,font(prototype->font)
,bold(prototype->bold)
,color(prototype->color)
,size(prototype->size)
,fontObject(prototype->fontObject)
,text(_text)
{
}

WString GetFingerPrint()
{
return font+L":"+(bold L"B":L"N")+L":"+itow(size);
}
};

int ConvertHex(wchar_t c)
{
if(L'a'<=c && c<=L'f') return c-L'a'+10;
if(L'A'<=c && c<=L'F') return c-L'A'+10;
if(L'0'<=c && c<=L'9') return c-L'0';
return 0;
}

Color ConvertColor(const WString& colorString)
{
return Color(
ConvertHex(colorString[1])*16+ConvertHex(colorString[2]),
ConvertHex(colorString[3])*16+ConvertHex(colorString[4]),
ConvertHex(colorString[5])*16+ConvertHex(colorString[6])
);
}

void BuildDocumentFragments(const WString& fileName, List>& fragments)
{
fragments.Clear();
WString rawDocument;
{
FileStream fileStream(fileName, FileStream::ReadOnly);
Utf8Decoder decoder;
DecoderStream decoderStream(fileStream, decoder);
StreamReader reader(decoderStream);
rawDocument=reader.ReadToEnd();
}

Regex regex(L"<(s)>([^:]+):([^:]+):([^:]+):([^:]+):(/.* )|<(p)//>");
RegexMatch::List matches;
regex.Search(rawDocument, matches);

for(int i=0;i {
Ptr match=matches[i];
Ptr fragment=new DocumentFragment;
fragments.Add(fragment);
if(match->Groups()[L"tag"][0].Value()==L"p")
{
fragment->paragraph=true;
}
else
{
WString font=match->Groups()[L"tag"][0].Value();
WString bold=match->Groups()[L"bold"][0].Value();
WString color=match->Groups()[L"color"][0].Value();
WString size=match->Groups()[L"size"][0].Value();
WString text=match->Groups()[L"text"][0].Value();

fragment->font=font;
fragment->bold=bold==L"true";
fragment->size=wtoi(size);
fragment->color=ConvertColor(color);
fragment->text=text;
}
}