internal virtual void Invalidate() { charWidths.Clear(); Paint.FontMetrics fm = textPaint.GetFontMetrics(); charHeight = fm.Bottom - fm.Top; charBaseline = -fm.Top; }
public override FontMetrics GetFontMetrics(Font font) { Paint paint = font.RendererData as Paint; if (paint == null || Math.Abs(font.RealSize - font.Size * Scale) > 2) { FreeFont(font); LoadFont(font); paint = font.RendererData as Paint; } Paint.FontMetrics metrics = paint.GetFontMetrics(); FontMetrics fm = new FontMetrics ( -metrics.Top - metrics.Leading, -metrics.Ascent, metrics.Descent, -metrics.Top + metrics.Bottom, metrics.Leading, paint.FontSpacing, 0 ); return(fm); }
void Initialize() { Clickable = true; Click += HandleClick; textSize = TypedValue.ApplyDimension(ComplexUnitType.Sp, 14, Resources.DisplayMetrics); spacing = TypedValue.ApplyDimension(ComplexUnitType.Dip, 24, Resources.DisplayMetrics); knobWidth = TypedValue.ApplyDimension(ComplexUnitType.Dip, 6, Resources.DisplayMetrics); knobPadding = TypedValue.ApplyDimension(ComplexUnitType.Dip, 2, Resources.DisplayMetrics); cornerRadius = TypedValue.ApplyDimension(ComplexUnitType.Dip, 2, Resources.DisplayMetrics); normalColor = Resources.GetColor(Resource.Color.dimmed_text_color); selectedColor = Resources.GetColor(Resource.Color.highlight_text_color); bgColor = Resources.GetColor(Resource.Color.primary_background); textPaint = new Paint { AntiAlias = true }; textPaint.SetTypeface(Typeface.Create("sans-serif-condensed", TypefaceStyle.Normal)); textPaint.TextSize = textSize; textPaint.TextAlign = Paint.Align.Center; textPaint.Color = normalColor; fontMetrics = textPaint.GetFontMetrics(); letterSpacing = fontMetrics.Bottom; ComputeCharacterSizes(); }
public void Text(string text) { if (mBitmap != null) { int width = mBitmap.Width; int height = mBitmap.Height; mPaint.Color = new Color(COLORS[mColorIndex]); mPaint.Alpha = 255; int size = height; mPaint.TextSize = size; var bounds = new Rect(); mPaint.GetTextBounds(text, 0, text.Length, bounds); int twidth = bounds.Width(); twidth += (twidth / 4); if (twidth > width) { size = (size * width) / twidth; mPaint.TextSize = size; mPaint.GetTextBounds(text, 0, text.Length, bounds); } Paint.FontMetrics fm = mPaint.GetFontMetrics(); mCanvas.DrawText(text, (width - bounds.Width()) / 2, ((height - size) / 2) - fm.Ascent, mPaint); mFadeSteps = 0; Invalidate(); } }
public override void DrawString(string s, Font font, Brush brush, float x, float y) { UpdateAndroidPaint(font, brush, null); var text = s ?? ""; var tmp = MeasureString(text, font); var tmp2 = paint.GetFontMetrics().Bottom / density; canvas.DrawText(text, x * density, (y + tmp.Height) * density - tmp2, paint); }
public double GetHeight(string fontFamily, float fontSize) { var paint = new Paint(); paint.TextSize = fontSize; var fontMetrics = paint.GetFontMetrics(null); return(fontMetrics); }
public override void Draw(Canvas canvas, ICharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) { canvas.Save(); var cachedDrawable = getCachedDrawable(); var transY = bottom - cachedDrawable.Bounds.Bottom - (paint.GetFontMetricsInt().Descent - (int)(paint.GetFontMetrics().Ascent / 2f)); canvas.Translate(x, (-paint.GetFontMetrics().Ascent) / 2f + transY / 2f); cachedDrawable.Draw(canvas); canvas.Restore(); }
// @Override protected override void OnDraw(Canvas canvas) { var text = "This is a custom view with setTypeface support"; var fm = paint.GetFontMetrics(); paint.GetTextBounds(text, 0, text.Length, textBounds); width = textBounds.Left + textBounds.Right + PaddingLeft + PaddingRight; height = (int)(Math.Abs(fm.Top) + fm.Bottom); canvas.DrawText(text, 0, -fm.Top + PaddingTop, paint); }
/// <summary> /// Gets font metrics for the font in the specified paint. /// </summary> /// <param name="paint">The paint.</param> /// <param name="defaultLineHeight">Default line height.</param> /// <param name="delta">The vertical delta.</param> private void GetFontMetrics(Paint paint, out float defaultLineHeight, out float delta) { var metrics = paint.GetFontMetrics(); var ascent = -metrics.Ascent; var descent = metrics.Descent; var leading = metrics.Leading; //// http://stackoverflow.com/questions/5511830/how-does-line-spacing-work-in-core-text-and-why-is-it-different-from-nslayoutm leading = leading < 0 ? 0 : (float)Math.Floor(leading + 0.5f); var lineHeight = (float)Math.Floor(ascent + 0.5f) + (float)Math.Floor(descent + 0.5) + leading; var ascenderDelta = leading >= 0 ? 0 : (float)Math.Floor((0.2 * lineHeight) + 0.5); defaultLineHeight = lineHeight + ascenderDelta; delta = ascenderDelta - descent; }
private void DrawIntoBitmap(Bitmap bm) { float x = bm.Width; float y = bm.Height; Canvas c = new Canvas(bm); Paint p = new Paint(); p.AntiAlias = true; p.Alpha = 0x80; c.DrawCircle(x / 2, y / 2, x / 2, p); p.Alpha = 0x30; p.SetXfermode(new PorterDuffXfermode(PorterDuff.Mode.Src)); p.TextSize = 60; p.TextAlign = Paint.Align.Center; Paint.FontMetrics fm = p.GetFontMetrics(); c.DrawText("Alpha", x / 2, (y - fm.Ascent) / 2, p); }
private void CreateFont(string fontName, float fontSize, CCRawList <char> charset) { if (_paint == null) { var contex = (Activity)CCApplication.SharedApplication.Game.Window.Context; var display = contex.WindowManager.DefaultDisplay; var metrics = new DisplayMetrics(); display.GetMetrics(metrics); _fontScaleFactor = metrics.ScaledDensity; _paint = new Paint(PaintFlags.AntiAlias); _paint.Color = Android.Graphics.Color.White; _paint.TextAlign = Paint.Align.Left; // _paint.LinearText = true; } _paint.TextSize = fontSize; var ext = System.IO.Path.GetExtension(fontName); if (!String.IsNullOrEmpty(ext) && ext.ToLower() == ".ttf") { var path = System.IO.Path.Combine(CCApplication.SharedApplication.Game.Content.RootDirectory, fontName); var activity = (Activity)CCApplication.SharedApplication.Game.Window.Context; try { var typeface = Typeface.CreateFromAsset(activity.Assets, path); _paint.SetTypeface(typeface); } catch (Exception) { _paint.SetTypeface(Typeface.Create(fontName, TypefaceStyle.Normal)); } } else { _paint.SetTypeface(Typeface.Create(fontName, TypefaceStyle.Normal)); } _fontMetrix = _paint.GetFontMetrics(); }
public static float LineHeight(this Paint self) { var matrics = self.GetFontMetrics(); return(matrics.Descent - matrics.Ascent); }
protected override void OnDraw(Canvas canvas) { float centerX = Width / 2F; float centerY = Height / 2F; float uniformWidth = Math.Min(Math.Min(centerX, centerY), RenderSize); // Path pathClip = new Path(); switch (shape) { case AvatarShape.Circle: pathClip.AddCircle(centerX, centerY, uniformWidth - (BorderRadius * 2), Path.Direction.Cw); break; case AvatarShape.RoundedRect: pathClip.AddRoundRect(0, 0, Width, Height, 5, 5, Path.Direction.Ccw); break; } canvas.Save(SaveFlags.Clip); canvas.ClipPath(pathClip); // canvas.DrawColor(backgroundColor); if (_fetchImage && placeHolderImage == null) { canvas.DrawColor(Color.LightGray); } else { if (image != null) { float subX = imageSize != null ? imageSize.Width / 2F : image.Width / 2F; float subY = imageSize != null ? imageSize.Height / 2F : image.Height / 2F; canvas.DrawBitmap(image, new Rect(0, 0, image.Width, image.Height), new Rect((int)(centerX - subX), (int)(centerY - subY), (int)(centerX - subX + subX * 2F), (int)(centerY - subY + subY * 2F)), null); } else if (placeHolderImage != null) { float subX = placeHolderImageSize != null ? placeHolderImageSize.Width / 2F : placeHolderImage.Width / 2F; float subY = placeHolderImageSize != null ? placeHolderImageSize.Height / 2F : placeHolderImage.Height / 2F; canvas.DrawBitmap(placeHolderImage, new Rect(0, 0, placeHolderImage.Width, placeHolderImage.Height), new Rect((int)(centerX - subX), (int)(centerY - subY), (int)((centerX - subX) + subX * 2F), (int)((centerY - subY) + subY * 2F)), null); if (_fetchImage) { canvas.DrawColor(OverlayColor); } } else if (Name != null) { string avatarName = AvatarName; Paint p = new Paint() { Color = GetAvatarBackgroundColor(avatarName), }; p.Flags |= PaintFlags.AntiAlias; // p.SetStyle(Paint.Style.Fill); canvas.DrawPaint(p); // Set p.Color = Color.White; p.SetTypeface(Typeface.SansSerif); p.TextSize = 60; // var metrics = p.GetFontMetrics(); float width = p.MeasureText(avatarName); canvas.DrawText(avatarName, centerX - width / 2F, centerY + metrics.Bottom, p); } } if (BorderRadius > 0) { canvas.Restore(); // var cPaint = new Paint(); cPaint.Flags |= PaintFlags.AntiAlias; cPaint.Color = BorderColor; cPaint.StrokeWidth = BorderRadius; cPaint.SetStyle(Paint.Style.Stroke); if (DrawShadow && !canvas.IsHardwareAccelerated) { cPaint.SetShadowLayer(ShadowRadius, ShadowOffsetX, ShadowOffsetY, ShadowColor); } // switch (shape) { case AvatarShape.Circle: canvas.DrawCircle(centerX, centerY, uniformWidth - (BorderRadius * 2F), cPaint); break; case AvatarShape.RoundedRect: { canvas.DrawRoundRect(0, 0, Width, Height, 5, 5, cPaint); } break; } } }
public static HopeFont CreateHopeFont(Typeface typeface) { int bitmapWH = 1024; int textSize = 160; // double scale = DeviceDisplay.MainDisplayInfo.Density; Paint paint = new Paint(PaintFlags.AntiAlias | PaintFlags.Dither) { Color = Color.White, TextSize = textSize }; paint.SetTypeface(typeface); paint.SetShadowLayer(0.4f, 0f, 1f, Color.DarkGray); // First create the canvas 1024x1024 with alpha Bitmap.Config bitmapConfig = Bitmap.Config.Argb8888; Bitmap bitmap = Bitmap.CreateBitmap(bitmapWH, bitmapWH, bitmapConfig); Canvas canvas = new Canvas(bitmap); Dictionary <char, Glyph> glyphs = new Dictionary <char, Glyph>(); FontMetrics metrics = paint.GetFontMetrics(); float lineHeight = metrics.Bottom - metrics.Top; float ascent = metrics.Ascent; List <char> missingChars = new List <char>(); missingChars.AddRange(chars.ToCharArray()); int currentLineIndex = 1; while (missingChars.Count > 0) { float currentLineHeight = lineHeight * currentLineIndex; int currentCharsCount; int index = -1; string currentChars; do { index++; currentCharsCount = missingChars.Count - index; currentChars = string.Join("", missingChars.GetRange(0, currentCharsCount)); } while (paint.MeasureText(currentChars) > bitmapWH); // Now we have the index of chars to write on this line missingChars.RemoveRange(0, currentCharsCount); canvas.DrawText(currentChars, 0, currentLineHeight, paint); float lastX = 0; foreach (char c in currentChars.ToCharArray()) { float[] widths = new float[1]; paint.GetTextWidths(c.ToString(), widths); float width = widths[0]; float relativeAscent = lineHeight + ascent + 5; float uvX = lastX / bitmapWH; float uvY = (currentLineHeight + relativeAscent) / bitmapWH; float uvW = width / bitmapWH; float uvH = lineHeight / bitmapWH; Glyph glyph = new Glyph(c, uvX, 1.0f - uvY, uvW, uvH); glyphs.Add(c, glyph); lastX += width; } currentLineIndex++; } return(new HopeFont(glyphs, new Texture(bitmap), lineHeight / bitmapWH)); }
public Xamarin.Forms.Size MeasureText(string text) { Paint.FontMetrics fm = paintFill.GetFontMetrics(); return(new Xamarin.Forms.Size(paintFill.MeasureText(text), fm.Bottom - fm.Ascent)); }
// --------------------------------------------------------------------- /* refitText =========================================================== * * Scales the size of the field's text until it fits within the field. * * -------------------------------------------------------------------- */ private void refitText(String text, int textWidth, int textHeight) { //Only do stuff if width != 0. if (textWidth > 0) { //Width and height of text field minus any padding. int targetWidth = textWidth - this.PaddingLeft - this.PaddingRight; int targetHeight = textHeight - this.PaddingTop - this.PaddingBottom; //Vers to get width and height of rendered text in new font size. float myMeasuredWidth; float myMeasuredHeight; //Var to calculate new font size. float size; //Max and min font sizes. float hi = 10000; float lo = 2; //Threshold for acceptable font size. float threshold = 0.5f; //Flag to check for good font size. bool tooBig; //For measuring the font height. Paint.FontMetrics myFontMetric; //Until we have zeroed in on a good font size... while ((hi - lo) > threshold) { //Split the difference for the new font size. size = (hi + lo) / 2; //Set size in test paint. mTestPaint.TextSize = size; //Get text width. myMeasuredWidth = mTestPaint.MeasureText(text); //Get text height. myFontMetric = mTestPaint.GetFontMetrics(); myMeasuredHeight = myFontMetric.Bottom - myFontMetric.Top; //Check size. tooBig = ( (myMeasuredWidth >= targetWidth) || (myMeasuredHeight >= targetHeight) ); //If width or height is too big... if (tooBig) { //Update hi size. hi = size; } //If too small... else { //Update lo size. lo = size; } } //Set new text size. this.SetTextSize(ComplexUnitType.Px, lo); } }
public static int GetDisplayHeight(this Paint paint) { var fm = paint.GetFontMetrics(); return((int)JavaMath.Ceil(fm.Descent - fm.Ascent)); }
public static float LineSpacing(this Paint self) { var matrics = self.GetFontMetrics(); return(matrics.Descent - matrics.Top + matrics.Bottom); }