I’m making a TextField and drawing it to BitmapData because scaling/tweening the latter looks way smoother. But I make calculations based on textWidth and the results often appear wrong. The value of textWidth isn’t the same any two times I run this code and the result is the text is getting clipped. Am I doing something wrong here?
var textFormat:TextFormat = new TextFormat();
textFormat.align = TextFormatAlign.LEFT;
textFormat.size = 50;
textFormat.color = 0xffffff;
textFormat.font = Assets.getFont('fonts/' + textStyle.font).fontName;
textField = new TextField();
textField.text = text;
textField.defaultTextFormat = textFormat;
textField.autoSize = TextFieldAutoSize.LEFT;
textField.selectable = false;
textField.width = textField.textWidth;
textField.height = textField.textHeight;
// Max the size
var wScale = stage.stageWidth / (textField.textWidth);
var hScale = stage.stageHeight / (textField.textHeight);
multiplier = Math.min(wScale, hScale);
// Draw the text to the bitmap
var bitmapData:BitmapData = new BitmapData(Std.int(stage.stageWidth), Std.int(stage.stageHeight));
bitmapData.fillRect(new Rectangle(0, 0, bitmapData.width, bitmapData.height), 0x000000);
bitmapData.draw(textField, new Matrix(multiplier, 0, 0, multiplier));
bitmap = new Bitmap(bitmapData);
addChild(bitmap);
bitmap.x = bitmap.y = 0;
I think the problem here (I’m not sure if it’s our problem, or just how the API is supposed to work) is that you are resizing the TextField when you set width and height
textField.width and height are not the same as textWidth and textHeight … it is usually 4 pixels larger, or more if you size the TextField up. This is probably the cause of your TextField shrinking
I switched it back to just directly displaying the TextField and found that it was smooth enough but had the same problem. I removed the part where I’m setting width and height (I only set scaleX and scaleY) and still having the problem. It does seem to vary with the font. Some are fine and some are off-center or clipped. Are there known issues with OpenFL’s font handling?
That’s what I mean. I changed to code to no longer use bitmap and just add directly to the displaylist (addChild). So there’s no longer a matrix property. Only me setting scaleX and scaleY of the TextField.
public function resize():Void
{
var textWidth = textField.textWidth;
var textHeight = textField.textHeight;
var parentWidth = stage.stageWidth;
var parentHeight = stage.stageHeight;
// Max the size
var wScale = parentWidth / (textWidth);
var hScale = parentHeight / (textHeight);
multiplier = Math.min(wScale, hScale);
//textField.width = textWidth;
//textField.height = textHeight;
textField.scaleX = textField.scaleY = multiplier;
}
function drawText():Void
{
var textFormat:TextFormat = new TextFormat();
textFormat.align = TextFormatAlign.LEFT;
textFormat.size = 50;
textFormat.color = 0xffffff;
textFormat.font = Assets.getFont('fonts/' + textStyle.font).fontName;
textField = new TextField();
textField.htmlText = text;
textField.defaultTextFormat = textFormat;
textField.selectable = false;
textField.autoSize = TextFieldAutoSize.LEFT;
addChild(textField);
}
How bad is the clipping? My team has had minor issues with TextField bound calculations since Flash days (we continue to use work arounds). But I am taking pretty small margins. tf.border = true; may be helpful while debugging in that case.
Quick question : why are you using autoSize but also setting the width and height manually?