bool VisualTextureContainer::initWithStyledString(VisualStyledString& styledString) {
	
	this->releaseTextureData();
	
    bool success = true;
	
	const VisualString* aVisualString = &(styledString);
	VisualStringStyle& stringStyle = styledString.getStyle();
	if (aVisualString->getNumberOfCharacters() == 0) {
		return false;
	}
	
	VisualGraphics* theVisualGraphics = VisualGraphics::getInstance();
	uint32 maxPixelWidth = theVisualGraphics->getCanvasPixelWidth();
	uint32 maxPixelHeight = theVisualGraphics->getCanvasPixelHeight();
	
	this->textureName = theVisualGraphics->getNextFreeTextureName();
	this->aTextureIsSet = true;
	VisualTextureContainer::textureRefCountMap[this->textureName] = 1;
	this->useRectExtension = theVisualGraphics->canUseTextureRectExtension();
	
#if TARGET_OS_MAC
	char alignmentStr[32];	
	switch (stringStyle.horizontalAlignment) {
		case (kLeftAligned):
			strcpy(alignmentStr, "left");
			break;
		case(kCenterAligned):
			strcpy(alignmentStr, "center");
			break;
		case(kRightAligned):
			strcpy(alignmentStr, "right");
			break;
		default:
			break;
	}
	
	success = getDimensionsOfCocoaStringBitmap((void*)aVisualString, (void*)&stringStyle, &(this->imageRect.width), &(this->imageRect.height), maxPixelWidth, maxPixelHeight, alignmentStr);
	if (!success) {
		return success;
	}
	
	if (this->useRectExtension == false) {
		this->textureRect.width = theVisualGraphics->power2Ceiling(this->imageRect.width);
		this->textureRect.height = theVisualGraphics->power2Ceiling(this->imageRect.height);
	} else {
		this->textureRect.width = this->imageRect.width;
		this->textureRect.height = this->imageRect.height;
	}
	
	PixelColor* pixelBuffer = (uint32*)calloc(this->textureRect.width * this->textureRect.height, sizeof(uint32));
	
	success = getCocoaStringBitmapData((void*)&styledString, this->textureRect.width, this->textureRect.height, alignmentStr, &(pixelBuffer));
	
	success = theVisualGraphics->copyARGBBitmapDataToTexture(this->textureName, this->textureRect.width, this->textureRect.height, this->useRectExtension, const_cast<const uint32**>(&(pixelBuffer)));
	
#endif
	
#if TARGET_OS_WIN
	
	wchar_t* stringValueRef = (wchar_t*)(styledString.getCharactersPointer());
	uint8 red = (uint8)(stringStyle.fontColor.r * 255.0f);
	uint8 green = (uint8)(stringStyle.fontColor.g * 255.0f);
	uint8 blue = (uint8)(stringStyle.fontColor.b * 255.0f);

	if (!stringValueRef) {
		success = false;
		return success;
	}
	
	success = theVisualGraphics->makeTextureOfStringWin(stringValueRef, styledString.getNumberOfCharacters(), this->textureName, this->textureRect.width, this->textureRect.height, this->imageRect.width, this->imageRect.height, stringStyle.fontNameStr, (uint16)stringStyle.fontSize, red, green, blue, stringStyle.horizontalAlignment, maxPixelWidth, maxPixelHeight);
	
#endif
	
	if (this->useRectExtension == false) {
		this->logicalSize.width = (double)this->imageRect.width / (double)this->textureRect.width;
		this->logicalSize.height = (double)this->imageRect.height / (double)this->textureRect.height;
	} else {
		this->logicalSize.width = (double)this->textureRect.width;
		this->logicalSize.height = (double)this->textureRect.height;
	}
	
	return success;
}
OSStatus VisualTextureContainer::initWithString(const VisualString& stringValue, VisualStringStyle& stringStyle) {
	
	this->releaseTextureData();
	if (this->pixelBuffer != NULL) {
		free(this->pixelBuffer);
		this->pixelBuffer = NULL;
	}

    OSStatus status = noErr;

	VisualGraphics* theVisualGraphics = VisualGraphics::getInstance();
	UInt16 maxPixelWidth = theVisualGraphics->getCanvasPixelWidth();
	UInt16 maxPixelHeight = theVisualGraphics->getCanvasPixelHeight();
	
	this->textureName = theVisualGraphics->getNextFreeTextureName();
	this->textureIsSet = true;
	VisualTextureContainer::textureRefCountMap[this->textureName] = 1;
	this->useRectExtension = theVisualGraphics->canUseTextureRectExtension();

#if TARGET_OS_MAC
	char alignmentStr[32];	
	switch (stringStyle.horizontalAlignment) {
		case (kLeftAligned):
			strcpy(alignmentStr, "left");
			break;
		case(kCenterAligned):
			strcpy(alignmentStr, "center");
			break;
		case(kRightAligned):
			strcpy(alignmentStr, "right");
			break;
		default:
			break;
	}
	
	status = getDimensionsOfCocoaStringBitmap((void*)&stringValue, &(this->imageWidth), &(this->imageHeight), const_cast<char*>(stringStyle.fontNameStr), &(stringStyle.fontSize), stringStyle.fontColor.r, stringStyle.fontColor.g, stringStyle.fontColor.b, maxPixelWidth, maxPixelHeight, alignmentStr);
	
	if (this->useRectExtension == false) {
		this->textureWidth = theVisualGraphics->power2Ceiling(this->imageWidth);
		this->textureHeight = theVisualGraphics->power2Ceiling(this->imageHeight);
	} else {
		this->textureWidth = this->imageWidth;
		this->textureHeight = this->imageHeight;
	}
	
	this->pixelBuffer = (UInt32*)calloc(this->textureWidth * this->textureHeight, sizeof(UInt32));

	status = getCocoaStringBitmapData((void*)&stringValue, this->textureWidth, this->textureHeight, const_cast<char*>(stringStyle.fontNameStr), stringStyle.fontSize, stringStyle.fontColor.r, stringStyle.fontColor.g, stringStyle.fontColor.b, alignmentStr, &(this->pixelBuffer));
	
	theVisualGraphics->copyARGBBitmapDataToTexture(this->textureName, this->textureWidth, this->textureHeight, this->useRectExtension, this->pixelFormat, this->dataType, const_cast<const UInt32**>(&(this->pixelBuffer)));

#endif

#if TARGET_OS_WIN

	wchar_t* stringValueRef = (wchar_t*)stringValue.getCharactersPointer();
	UInt8 red = (UInt8)(stringStyle.fontColor.r * 255.0f);
	UInt8 green = (UInt8)(stringStyle.fontColor.g * 255.0f);
	UInt8 blue = (UInt8)(stringStyle.fontColor.b * 255.0f);
	
	status = theVisualGraphics->makeTextureOfStringWin(stringValueRef, stringValue.getNumberOfCharacters(), this->textureName, this->textureWidth, this->textureHeight, this->imageWidth, this->imageHeight, stringStyle.fontNameStr, (UInt16)stringStyle.fontSize, red, green, blue, stringStyle.horizontalAlignment, maxPixelWidth, maxPixelHeight);

#endif

	if (this->useRectExtension == false) {
		this->textureLogicalWidth = (double)this->imageWidth / (double)this->textureWidth;
		this->textureLogicalHeight = (double)this->imageHeight / (double)this->textureHeight;
	} else {
		this->textureLogicalWidth = (double)this->textureWidth;
		this->textureLogicalHeight = (double)this->textureHeight;
	}

	return status;
}