std::string DocBookReader::parseLink(ZLUnicodeUtil::Ucs2String s, bool urlencode) { //TODO add support for HYPERLINK like that: // [0x13] HYPERLINK "http://site.ru/some text" \t "_blank" [0x14] text [0x15] //Current implementation search for last QUOTE, so, it reads \t and _blank as part of link //Last quote searching is need to handle link like that: // [0x13] HYPERLINK "http://yandex.ru/yandsearch?text='some text' и "some text2"" [0x14] link text [0x15] static const ZLUnicodeUtil::Ucs2Char QUOTE = 0x22; size_t i, first = 0; //TODO maybe functions findFirstOf and findLastOf should be in ZLUnicodeUtil class for (i = 0; i < s.size(); ++i) { if (s.at(i) == QUOTE) { first = i; break; } } if (i == s.size()) { return std::string(); } size_t j, last = 0; for (j = s.size(); j > 0 ; --j) { if (s.at(j - 1) == QUOTE) { last = j - 1; break; } } if (j == 0 || last == first) { return std::string(); } ZLUnicodeUtil::Ucs2String link; for (size_t k = first + 1; k < last; ++k) { ZLUnicodeUtil::Ucs2Char ch = s.at(k); if (urlencode && ZLUnicodeUtil::isSpace(ch)) { //TODO maybe implement function for encoding all signs in url, not only spaces and quotes //TODO maybe add backslash support link.push_back('%'); link.push_back('2'); link.push_back('0'); } else if (urlencode && ch == QUOTE) { link.push_back('%'); link.push_back('2'); link.push_back('2'); } else { link.push_back(ch); } } std::string utf8String; ZLUnicodeUtil::ucs2ToUtf8(utf8String, link); return utf8String; }
void ZLWin32Utf16EncodingConverter::convert(std::string &dst, const char *srcStart, const char *srcEnd) { if (srcStart == srcEnd) { return; } const unsigned char *ptr = (const unsigned char*)srcStart; ZLUnicodeUtil::Ucs2Char ch; myBuffer.clear(); if (myUseStoredCharacter) { if (myLittleEndian) { ch = *ptr; ch <<= 8; ch += myStoredCharacter; } else { ch = myStoredCharacter; ch <<= 8; ch += *ptr; } myBuffer.push_back(ch); ++ptr; } const unsigned char *end = (const unsigned char*)srcEnd; if ((end - ptr) % 2 == 1) { myStoredCharacter = *end; myUseStoredCharacter = true; --end; } else { myUseStoredCharacter = false; } if (myLittleEndian) { for (; ptr < end; ptr += 2) { myBuffer.push_back((((ZLUnicodeUtil::Ucs2Char)*(ptr + 1)) << 8) + *ptr); } } else { for (; ptr < end; ptr += 2) { myBuffer.push_back((((ZLUnicodeUtil::Ucs2Char)*ptr) << 8) + *(ptr + 1)); } } std::string toAppend; ZLUnicodeUtil::ucs2ToUtf8(toAppend, myBuffer); dst += toAppend; myBuffer.clear(); }
void ZLWin32PaintContext::setFont(const std::string &family, int size, bool bold, bool italic) { if (size < 4) { size = 4; } if (myDisplayContext == 0) { return; } // TODO: optimize LOGFONT logicalFont; memset(&logicalFont, 0, sizeof(LOGFONT)); logicalFont.lfHeight = size; logicalFont.lfWeight = bold ? FW_BOLD : FW_REGULAR; logicalFont.lfItalic = italic; logicalFont.lfQuality = 5 /*CLEARTYPE_QUALITY*/; const int len = std::min((int)family.length(), LF_FACESIZE - 1); ZLUnicodeUtil::Ucs2String str; ZLUnicodeUtil::utf8ToUcs2(str, family.data(), len); str.push_back(0); memcpy(logicalFont.lfFaceName, ::wchar(str), 2 * str.size()); HFONT font = CreateFontIndirect(&logicalFont); DeleteObject(SelectObject(myDisplayContext, font)); GetTextMetrics(myDisplayContext, &myTextMetric); mySpaceWidth = -1; }
void ZLWin32FSDir::collectFiles(std::vector<std::string> &names, bool /*includeSymlinks*/) { std::string pattern = path(); if (pattern[pattern.length() - 1] != '\\') { pattern += '\\'; } pattern += '*'; ZLUnicodeUtil::Ucs2String wPattern; createNTWCHARString(wPattern, pattern); WIN32_FIND_DATAW findData; HANDLE handle = FindFirstFileW(::wchar(wPattern), &findData); ZLUnicodeUtil::Ucs2String wFileName; std::string fileName; if (handle != 0) { do { if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { continue; } wFileName.clear(); fileName.erase(); for (int i = 0; i < MAX_PATH; ++i) { if (findData.cFileName[i] != 0) { wFileName.push_back(findData.cFileName[i]); } else { break; } } ZLUnicodeUtil::ucs2ToUtf8(fileName, wFileName); names.push_back(fileName); } while (FindNextFileW(handle, &findData)); } }
void ZLWin32EncodingConverter::convert(std::string &dst, const char *srcStart, const char *srcEnd) { if ((srcStart == srcEnd) || (myCode == 0)) { return; } myBuffer.clear(); bool hasFirstChar = false; if (myUseStoredCharacter) { WCHAR symbol; char buf[2] = { myStoredCharacter, *srcStart }; if (MultiByteToWideChar(myCode, 0, buf, 2, &symbol, 1) == 1) { hasFirstChar = true; myBuffer.push_back(symbol); srcStart++; } myUseStoredCharacter = false; } int len = srcEnd - srcStart; if (len > 0) { myBuffer.insert(myBuffer.end(), len, 0); WCHAR *bufferStart = (WCHAR*)&myBuffer.front(); if (hasFirstChar) { bufferStart++; } int ucs2Len = MultiByteToWideChar(myCode, 0, srcStart, len, bufferStart, len); myBuffer.erase(myBuffer.begin() + ucs2Len + (hasFirstChar ? 1 : 0), myBuffer.end()); if (ucs2Len != len) { myRBuffer.append(len, '\0'); char defaultChar = 'X'; BOOL usedDefaultChar = false; int len1 = WideCharToMultiByte(myCode, 0, bufferStart, ucs2Len, (char*)myRBuffer.data(), len, &defaultChar, &usedDefaultChar); if (len1 == len - 1) { myUseStoredCharacter = true; myStoredCharacter = *(srcEnd - 1); } myRBuffer.erase(); } } std::string toAppend; ZLUnicodeUtil::ucs2ToUtf8(toAppend, myBuffer); dst += toAppend; myBuffer.clear(); }
bool OleMainStream::readBookmarks(const char *headerBuffer, const OleEntry &tableEntry) { //SttbfBkmk structure is a table of bookmark name strings unsigned int beginNamesInfo = OleUtil::getU4Bytes(headerBuffer, 0x142); // address of SttbfBkmk structure std::size_t namesInfoLength = (std::size_t)OleUtil::getU4Bytes(headerBuffer, 0x146); // length of SttbfBkmk structure if (namesInfoLength == 0) { return true; //there's no bookmarks } OleStream tableStream(myStorage, tableEntry, myBaseStream); std::string buffer; if (!readToBuffer(buffer, beginNamesInfo, namesInfoLength, tableStream)) { return false; } unsigned int recordsNumber = OleUtil::getU2Bytes(buffer.c_str(), 0x2); //count of records std::vector<std::string> names; unsigned int offset = 0x6; //initial offset for (unsigned int i = 0; i < recordsNumber; ++i) { if (buffer.size() < offset + 2) { ZLLogger::Instance().println("DocPlugin", "problmes with reading bookmarks names"); break; } unsigned int length = OleUtil::getU2Bytes(buffer.c_str(), offset) * 2; //length of string in bytes ZLUnicodeUtil::Ucs2String name; for (unsigned int j = 0; j < length; j+=2) { char ch1 = buffer.at(offset + 2 + j); char ch2 = buffer.at(offset + 2 + j + 1); ZLUnicodeUtil::Ucs2Char ucs2Char = (unsigned int)ch1 | ((unsigned int)ch2 << 8); name.push_back(ucs2Char); } std::string utf8Name; ZLUnicodeUtil::ucs2ToUtf8(utf8Name, name); names.push_back(utf8Name); offset += length + 2; } //plcfBkmkf structure is table recording beginning CPs of bookmarks unsigned int beginCharPosInfo = OleUtil::getU4Bytes(headerBuffer, 0x14A); // address of plcfBkmkf structure std::size_t charPosInfoLen = (std::size_t)OleUtil::getU4Bytes(headerBuffer, 0x14E); // length of plcfBkmkf structure if (charPosInfoLen == 0) { return true; //there's no bookmarks } if (!readToBuffer(buffer, beginCharPosInfo, charPosInfoLen, tableStream)) { return false; } static const unsigned int BKF_SIZE = 4; std::size_t size = calcCountOfPLC(charPosInfoLen, BKF_SIZE); std::vector<unsigned int> charPage; for (std::size_t index = 0, offset = 0; index < size; ++index, offset += 4) { charPage.push_back(OleUtil::getU4Bytes(buffer.c_str(), offset)); } for (std::size_t i = 0; i < names.size(); ++i) { if (i >= charPage.size()) { break; //for the case if something in these structures goes wrong, to not to lose all bookmarks } Bookmark bookmark; bookmark.CharPosition = charPage.at(i); bookmark.Name = names.at(i); myBookmarks.push_back(bookmark); } return true; }