Example #1
0
const char*
BString::CharAt(int32 charIndex, int32* bytes) const
{
	int32 offset = UTF8CountBytes(fPrivateData, charIndex);
	if (bytes != NULL)
		*bytes = UTF8NextCharLen(fPrivateData + offset);
	return fPrivateData + offset;
}
Example #2
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;
}
Example #3
0
void
BTextView::TypingUndoBuffer::ForwardErase()
{
	// TODO: Cleanup
	int32 start, end;

	fTextView->GetSelection(&start, &end);
	
	int32 charLen = UTF8NextCharLen(fTextView->Text() + start);	
	
	if (start != fTypedEnd || end != fTypedEnd || fUndone > 0) {
		_Reset();
		// if we've got a selection, we're already done
		if (fStart == fEnd) {
			free(fTextData);
			fTextLength = charLen;
			fTextData = (char*)malloc(fTextLength);
			
			// store the erased character
			for (int32 x = 0; x < charLen; x++)
				fTextData[x] = fTextView->ByteAt(start + x);
		}
	} else {	
		// Here we need to store the erased text, so we get the text that it's 
		// already in the buffer, and we add the erased character.
		// a realloc + memmove would maybe be cleaner, but that way we spare a
		// copy (malloc + memcpy vs realloc + memmove).
		
		int32 newLength = fTextLength + charLen;
		char* buffer = (char*)malloc(newLength);
		
		// copy the already stored data
		memcpy(buffer, fTextData, fTextLength);
		
		if (fTextLength < newLength) {
			// store the erased character
			for (int32 x = 0; x < charLen; x++)
				buffer[fTextLength + x] = fTextView->ByteAt(start + x);
		}

		fTextLength = newLength;
		free(fTextData);
		fTextData = buffer;
	}
}