private void prepareBitmapDraw() { string originalText = _text ?? ""; string text = _text; var config = AGSTextConfig.ScaleConfig(_config, _scaleUp.X); int maxWidth = _maxWidth == int.MaxValue ? _maxWidth : (int)(_maxWidth * _scaleUp.X - config.PaddingLeft - config.PaddingRight); string textToMeasure = text; if (_renderCaret && textToMeasure == "") { textToMeasure = "|"; } SizeF originalTextSize = config.Font.MeasureString(textToMeasure, config.Alignment, _cropText ? int.MaxValue : maxWidth); SizeF textSize = originalTextSize; if (_cropText && textSize.Width > maxWidth) { textSize = cropText(maxWidth, config, ref text); } float widthOffset = Math.Max(config.OutlineWidth, Math.Abs(config.ShadowOffsetX)); float heightOffset = Math.Max(config.OutlineWidth, Math.Abs(config.ShadowOffsetY)); float widthF = textSize.Width + widthOffset + config.PaddingLeft + config.PaddingRight; float heightF = textSize.Height + heightOffset + config.PaddingTop + config.PaddingBottom; // ReSharper disable CompareOfFloatsByEqualityOperator SizeF baseSize = new SizeF(_baseSize.Width == EmptySize.Width ? widthF : _baseSize.Width * _scaleUp.X, _baseSize.Height == EmptySize.Height ? heightF : _baseSize.Height * _scaleUp.Y); // ReSharper restore CompareOfFloatsByEqualityOperator Width = (widthF / _scaleUp.X); Height = (heightF / _scaleUp.Y); int bitmapWidth = MathUtils.GetNextPowerOf2(Math.Max((int)(widthF) + 1, (int)(baseSize.Width) + 1)); int bitmapHeight = MathUtils.GetNextPowerOf2(Math.Max((int)(heightF) + 1, (int)(baseSize.Height) + 1)); BitmapWidth = (int)(bitmapWidth / _scaleDown.X); BitmapHeight = (int)(bitmapHeight / _scaleDown.Y); if (_measureOnly || _alwaysMeasureOnly) { return; } _draw.BitmapWidth = bitmapWidth; _draw.BitmapHeight = bitmapHeight; _draw.BaseSize = baseSize; _draw.TextSize = textSize; _draw.HeightF = heightF; _draw.Text = text; _draw.OriginalText = originalText; _draw.MaxWidth = maxWidth; _draw.Config = config; _renderChanged = true; }
private void drawToBitmap() { string originalText = _text ?? ""; string text = _text; var config = AGSTextConfig.ScaleConfig(_config, TextResolutionFactorX); int maxWidth = _maxWidth == int.MaxValue ? _maxWidth : _maxWidth * TextResolutionFactorX; SizeF originalTextSize = config.Font.MeasureString(text, _cropText ? int.MaxValue : maxWidth); SizeF textSize = originalTextSize; if (_cropText && textSize.Width > maxWidth) { textSize = cropText(maxWidth, config, ref text); } float widthOffset = Math.Max(config.OutlineWidth, Math.Abs(config.ShadowOffsetX)); float heightOffset = Math.Max(config.OutlineWidth, Math.Abs(config.ShadowOffsetY)); float widthF = textSize.Width + widthOffset + config.PaddingLeft + config.PaddingRight; float heightF = textSize.Height + heightOffset + config.PaddingTop + config.PaddingBottom; SizeF baseSize = new SizeF(_baseSize.Width == EmptySize.Width ? widthF : _baseSize.Width * TextResolutionFactorX, _baseSize.Height == EmptySize.Height ? heightF : _baseSize.Height * TextResolutionFactorY); Width = (widthF / GLText.TextResolutionFactorX); Height = (heightF / GLText.TextResolutionFactorY); int bitmapWidth = MathUtils.GetNextPowerOf2(Math.Max((int)widthF + 1, (int)baseSize.Width + 1)); int bitmapHeight = MathUtils.GetNextPowerOf2(Math.Max((int)heightF + 1, (int)baseSize.Height + 1)); IBitmap bitmap = _bitmap; if (bitmap == null || bitmap.Width != bitmapWidth || bitmap.Height != bitmapHeight) { if (bitmap != null) { _bitmapPool.Release(bitmap); } _bitmap = _bitmapPool.Acquire(bitmapWidth, bitmapHeight); bitmap = _bitmap; } IBitmapTextDraw textDraw = bitmap.GetTextDraw(); using (var context = textDraw.CreateContext()) { textDraw.DrawText(text, config, textSize, baseSize, maxWidth, (int)heightF, 0f); drawCaret(originalText, textSize, heightF, baseSize, textDraw, config, maxWidth); } _renderChanged = true; }
public static SizeF GetTextSize(this ITextConfig config, string text, SizeF labelSize) { config = AGSTextConfig.ScaleConfig(config, GLText.TextResolutionFactorX); labelSize = labelSize.Scale(GLText.TextResolutionFactorX, GLText.TextResolutionFactorY); float scaleBackX = 1f / GLText.TextResolutionFactorX; float scaleBackY = 1f / GLText.TextResolutionFactorY; switch (config.AutoFit) { case AutoFit.TextShouldFitLabel: var textSize = config.Font.MeasureString(text); return(new SizeF(Math.Min(textSize.Width, labelSize.Width), Math.Min(textSize.Height, labelSize.Height)).Scale(scaleBackX, scaleBackY)); case AutoFit.TextShouldWrapAndLabelShouldFitHeight: return(config.Font.MeasureString(text, (int)labelSize.Width).Scale(scaleBackX, scaleBackY)); default: return(config.Font.MeasureString(text).Scale(scaleBackX, scaleBackY)); } }