Esempio n. 1
0
/*! \brief Returns how much room is required to draw a string in the font.
	\param inText The string to be examined.
	\param fromOffset The offset in the string where to begin the examination.
	\param lenght The amount of bytes to be examined.
	\param inStyle The font.
	\return The space (in pixels) required to draw the given string.
*/
float
WidthBuffer::StringWidth(const char *inText, int32 fromOffset,
	int32 length, const BFont *inStyle)
{
	if (inText == NULL || length == 0)
		return 0;

	BAutolock _(fLock);

	int32 index = 0;
	if (!FindTable(inStyle, &index))
		index = InsertTable(inStyle);

	char *text = NULL;
	int32 numChars = 0;
	int32 textLen = 0;

	char *sourceText = (char *)inText + fromOffset;
	const float fontSize = inStyle->Size();
	float stringWidth = 0;
	for (int32 charLen = 0;
			sourceText < inText + length;
			sourceText += charLen) {
		charLen = UTF8NextCharLen(sourceText);

		// End of string, bail out
		if (charLen <= 0)
			break;

		// Some magic, to uniquely identify this charachter
		const uint32 value = CharToCode(sourceText, charLen);

		float escapement;
		if (GetEscapement(value, index, &escapement)) {
			// Well, we've got a match for this charachter
			stringWidth += escapement;
		} else {
			// Store this charachter into an array, which we'll
			// pass to HashEscapements() later
			int32 offset = textLen;
			textLen += charLen;
			numChars++;
			text = (char *)realloc(text, textLen);
			for (int32 x = 0; x < charLen; x++)
				text[offset + x] = sourceText[x];
		}
	}

	if (text != NULL) {
		// We've found some charachters which aren't yet in the hash table.
		// Get their width via HashEscapements()
		stringWidth += HashEscapements(text, numChars, textLen, index, inStyle);
		free(text);
	}

	return stringWidth * fontSize;
}
Esempio n. 2
0
BOOL WriteText (LPCTSTR szText)
{
    DWORD			dwWritten;
    INPUT_RECORD	rec;
    char			upper, *sz;

    sz = (LPTSTR) szText;

    while (*sz)
    {
        // 13 is the code for a carriage return (\n) instead of 10.
        if (*sz == 10)
            *sz = 13;

        upper = toupper(*sz);

        rec.EventType = KEY_EVENT;
        rec.Event.KeyEvent.bKeyDown = TRUE;
        rec.Event.KeyEvent.wRepeatCount = 1;
        rec.Event.KeyEvent.wVirtualKeyCode = upper;
        rec.Event.KeyEvent.wVirtualScanCode = CharToCode (*sz);
        rec.Event.KeyEvent.uChar.AsciiChar = *sz;
        rec.Event.KeyEvent.uChar.UnicodeChar = *sz;
        rec.Event.KeyEvent.dwControlKeyState = isupper(*sz) ? 0x80 : 0x0;

        WriteConsoleInput(
            hStdin,
            &rec,
            1,
            &dwWritten);

        rec.Event.KeyEvent.bKeyDown = FALSE;

        WriteConsoleInput(
            hStdin,
            &rec,
            1,
            &dwWritten);

        sz++;
    }

    //ServerWindowProcCommandExecute ();

    return TRUE;
}