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)); } }
static std::string getTextFromBuffer(const ZLUnicodeUtil::Ucs2String &buffer) { ZLUnicodeUtil::Ucs2String copy = buffer; copy.pop_back(); std::string txt; ZLUnicodeUtil::ucs2ToUtf8(txt, copy); return txt; }
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; }
LRESULT CALLBACK ZLWin32ApplicationWindow::TextEditParameter::ComboBoxCallback(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { TextEditParameter ¶meter = *(TextEditParameter*)GetWindowLong(hWnd, GWL_USERDATA); if ((uMsg == WM_COMMAND) && (HIWORD(wParam) == CBN_SELCHANGE)) { HWND comboBox = parameter.myComboBox; const int index = SendMessage(comboBox, CB_GETCURSEL, 0, 0); const int length = SendMessage(comboBox, CB_GETLBTEXTLEN, index, 0); ZLUnicodeUtil::Ucs2String buffer; if (length > 0) { buffer.assign(length + 1, '\0'); SendMessage(comboBox, CB_GETLBTEXT, index, (LPARAM)&buffer.front()); buffer.pop_back(); } std::string value; ZLUnicodeUtil::ucs2ToUtf8(value, buffer); parameter.setValue(value); parameter.myApplication.doAction(parameter.myParameterItem.actionId()); SetFocus(parameter.myMainWindow); } WndProc orig = parameter.myOriginalComboBoxCallback; return orig(hWnd, uMsg, wParam, lParam); }
std::string ZLWin32ApplicationWindow::TextEditParameter::internalValue() const { int len = GetWindowTextLengthW(myComboBox); if (len == 0) { return ""; } static ZLUnicodeUtil::Ucs2String buffer; buffer.assign(len + 1, '\0'); GetWindowTextW(myComboBox, (WCHAR*)::wchar(buffer), len + 1); buffer.pop_back(); std::string text; ZLUnicodeUtil::ucs2ToUtf8(text, buffer); return text; }
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 ZLWin32ApplicationWindow::setTooltip(TOOLTIPTEXT &tooltip) { Toolbar &tb = toolbar(isFullscreen() ? FULLSCREEN_TOOLBAR : WINDOW_TOOLBAR); if (tb.hwnd == 0) { return; } ZLToolbar::ItemPtr item = tb.TBItemByActionCode[tooltip.hdr.idFrom]; if (!item.isNull()) { const ZLToolbar::AbstractButtonItem &button = (const ZLToolbar::AbstractButtonItem&)*item; ZLUnicodeUtil::Ucs2String tooltipBuffer; ::createNTWCHARString(tooltipBuffer, button.tooltip()); size_t length = std::max(tooltipBuffer.size(), (size_t)80); memcpy((char*)tooltip.szText, (char*)::wchar(tooltipBuffer), 2 * length); tooltip.uFlags = TTF_DI_SETITEM; } }
void ZLWin32PaintContext::drawString(int x, int y, const char *str, int len) { if (myDisplayContext == 0) { return; } y -= stringHeight(); y += myTextMetric.tmDescent; int utf8len = ZLUnicodeUtil::utf8Length(str, len); if (utf8len == len) { TextOutA(myDisplayContext, x, y, str, len); } else { static ZLUnicodeUtil::Ucs2String ucs2Str; ucs2Str.clear(); ZLUnicodeUtil::utf8ToUcs2(ucs2Str, str, len, utf8len); TextOutW(myDisplayContext, x, y, ::wchar(ucs2Str), utf8len); } }
int ZLWin32PaintContext::stringWidth(const char *str, int len) const { if (myDisplayContext == 0) { return 0; } SIZE size; int utf8len = ZLUnicodeUtil::utf8Length(str, len); if (utf8len == len) { GetTextExtentPointA(myDisplayContext, str, len, &size); } else { static ZLUnicodeUtil::Ucs2String ucs2Str; ucs2Str.clear(); ZLUnicodeUtil::utf8ToUcs2(ucs2Str, str, len, utf8len); GetTextExtentPointW(myDisplayContext, ::wchar(ucs2Str), utf8len, &size); } return size.cx; }
void ZLQtPaintContext::drawString(int x, int y, const char *str, int len, bool rtl) { if (rtl) { static ZLUnicodeUtil::Ucs2String ucs2Str; ucs2Str.clear(); ZLUnicodeUtil::utf8ToUcs2(ucs2Str, str, len); QString qStr; FriBidiChar ch; for (int i = ucs2Str.size() - 1; i >= 0; --i) { fribidi_get_mirror_char(ucs2Str[i], &ch); qStr.append(QChar(ch)); } myPainter->drawText(x, y, qStr); } else { QString qStr = QString::fromUtf8(str, len); myPainter->drawText(x, y, qStr); } }
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(); }
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 ZLTextModel::addStyleEntry(const ZLTextStyleEntry &entry) { // +++ calculating entry size std::size_t len = 4; // entry type + feature mask for (int i = 0; i < ZLTextStyleEntry::NUMBER_OF_LENGTHS; ++i) { if (entry.isFeatureSupported((ZLTextStyleEntry::Feature)i)) { len += 4; // each supported length } } if (entry.isFeatureSupported(ZLTextStyleEntry::ALIGNMENT_TYPE)) { len += 2; } ZLUnicodeUtil::Ucs2String fontFamily; if (entry.isFeatureSupported(ZLTextStyleEntry::FONT_FAMILY)) { ZLUnicodeUtil::utf8ToUcs2(fontFamily, entry.fontFamily()); len += 2 + fontFamily.size() * 2; } if (entry.isFeatureSupported(ZLTextStyleEntry::FONT_STYLE_MODIFIER)) { len += 2; } // --- calculating entry size /* EntryCount += 1; EntryLen += len; std::string debug = "style entry counter: "; ZLStringUtil::appendNumber(debug, EntryCount); debug += "/"; ZLStringUtil::appendNumber(debug, EntryLen); ZLLogger::Instance().println(ZLLogger::DEFAULT_CLASS, debug); */ // +++ writing entry myLastEntryStart = myAllocator->allocate(len); char *address = myLastEntryStart; *address++ = entry.entryKind(); *address++ = 0; address = ZLCachedMemoryAllocator::writeUInt16(address, entry.myFeatureMask); for (int i = 0; i < ZLTextStyleEntry::NUMBER_OF_LENGTHS; ++i) { if (entry.isFeatureSupported((ZLTextStyleEntry::Feature)i)) { const ZLTextStyleEntry::LengthType &len = entry.myLengths[i]; address = ZLCachedMemoryAllocator::writeUInt16(address, len.Size); *address++ = len.Unit; *address++ = 0; } } if (entry.isFeatureSupported(ZLTextStyleEntry::ALIGNMENT_TYPE)) { *address++ = entry.myAlignmentType; *address++ = 0; } if (entry.isFeatureSupported(ZLTextStyleEntry::FONT_FAMILY)) { address = ZLCachedMemoryAllocator::writeString(address, fontFamily); } if (entry.isFeatureSupported(ZLTextStyleEntry::FONT_STYLE_MODIFIER)) { *address++ = entry.mySupportedFontModifier; *address++ = entry.myFontModifier; } // --- writing entry myParagraphs.back()->addEntry(myLastEntryStart); ++myParagraphLengths.back(); }
std::string ZLQtKeyUtil::keyName(QKeyEvent *keyEvent) { ZLUnicodeUtil::Ucs2String s; ZLUnicodeUtil::utf8ToUcs2(s, (const char*)keyEvent->text().toUtf8()); int unicode = s.empty() ? 0 : s[0]; return ZLKeyUtil::keyName(unicode, keyEvent->key(), keyEvent->modifiers()); }
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; }