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();
}
Example #2
0
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();
}
Example #4
0
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);
    }
}
Example #5
0
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;
}
Example #6
0
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);
	}
}