public void GetImageBounds() { using (var a = new NSAttributedString()) using (var l = new CTLine(a)) { Assert.True(l.GetImageBounds(null).IsEmpty, "GetImageBounds"); } }
/// <summary> /// Measures the text. /// </summary> /// <param name="text">The text.</param> /// <param name="fontFamily">The font family.</param> /// <param name="fontSize">Size of the font.</param> /// <param name="fontWeight">The font weight.</param> /// <returns> /// The size of the text. /// </returns> public override OxySize MeasureText(string text, string fontFamily, double fontSize, double fontWeight) { if (string.IsNullOrEmpty(text) || fontFamily == null) { return(OxySize.Empty); } var fontName = GetActualFontName(fontFamily, fontWeight); var font = this.GetCachedFont(fontName, (float)fontSize); using (var attributedString = new NSAttributedString(text, new CTStringAttributes { ForegroundColorFromContext = true, Font = font })) { using (var textLine = new CTLine(attributedString)) { float lineHeight, delta; this.GetFontMetrics(font, out lineHeight, out delta); // the text position must be set to get the correct bounds this.gctx.TextPosition = new CGPoint(0, 0); var bounds = textLine.GetImageBounds(this.gctx); var width = bounds.Left + bounds.Width; return(new OxySize(width, lineHeight)); } } }
/// <summary> /// Measures the text. /// </summary> /// <param name="text">The text.</param> /// <param name="fontFamily">The font family.</param> /// <param name="fontSize">Size of the font.</param> /// <param name="fontWeight">The font weight.</param> /// <returns> /// The size of the text. /// </returns> public override OxySize MeasureText(string text, string fontFamily, double fontSize, double fontWeight) { if (string.IsNullOrEmpty(text) || fontFamily == null) { return(OxySize.Empty); } var fontName = GetActualFontName(fontFamily, fontWeight); using (var attributedString = new NSAttributedString(text, new CTStringAttributes { ForegroundColorFromContext = true, Font = new CTFont(fontName, (float)fontSize) })) { using (var textLine = new CTLine(attributedString)) { var bounds = textLine.GetImageBounds(this.gctx); return(new OxySize(bounds.Width, bounds.Height)); } } }
public SizeF MeasureString(string textg, Font font, RectangleF rect) { // As per documentation // https://developer.apple.com/library/mac/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_text/dq_text.html#//apple_ref/doc/uid/TP30001066-CH213-TPXREF101 // // * Note * Not sure if we should save off the graphic state, set context transform to identity // and restore state to do the measurement. Something to be looked into. // context.TextPosition = rect.Location; // var startPos = context.TextPosition; // context.SelectFont ( font.nativeFont.PostScriptName, // font.SizeInPoints, // CGTextEncoding.MacRoman); // context.SetTextDrawingMode(CGTextDrawingMode.Invisible); // // context.SetCharacterSpacing(1); // var textMatrix = CGAffineTransform.MakeScale(1f,-1f); // context.TextMatrix = textMatrix; // // context.ShowTextAtPoint(rect.X, rect.Y, textg, textg.Length); // var endPos = context.TextPosition; // // var measure = new SizeF(endPos.X - startPos.X, font.nativeFont.CapHeightMetric); var atts = buildAttributedString(textg, font); // for now just a line not sure if this is going to work CTLine line = new CTLine(atts); var lineBounds = line.GetImageBounds(context); // Create and initialize some values from the bounds. float ascent; float descent; float leading; double lineWidth = line.GetTypographicBounds(out ascent, out descent, out leading); var measure = new SizeF((float)lineWidth, ascent + descent); return(measure); }
/// <summary> /// Draws the text. /// </summary> /// <param name="p">The position of the text.</param> /// <param name="text">The text.</param> /// <param name="fill">The fill color.</param> /// <param name="fontFamily">The font family.</param> /// <param name="fontSize">Size of the font.</param> /// <param name="fontWeight">The font weight.</param> /// <param name="rotate">The rotation angle.</param> /// <param name="halign">The horizontal alignment.</param> /// <param name="valign">The vertical alignment.</param> /// <param name="maxSize">The maximum size of the text.</param> public override void DrawText(ScreenPoint p, string text, OxyColor fill, string fontFamily, double fontSize, double fontWeight, double rotate, HorizontalAlignment halign, VerticalAlignment valign, OxySize?maxSize) { if (string.IsNullOrEmpty(text)) { return; } var fontName = GetActualFontName(fontFamily, fontWeight); var font = this.GetCachedFont(fontName, fontSize); using (var attributedString = new NSAttributedString(text, new CTStringAttributes { ForegroundColorFromContext = true, Font = font })) { using (var textLine = new CTLine(attributedString)) { float width; float height; this.gctx.TextPosition = new PointF(0, 0); float lineHeight, delta; this.GetFontMetrics(font, out lineHeight, out delta); var bounds = textLine.GetImageBounds(this.gctx); if (maxSize.HasValue || halign != HorizontalAlignment.Left || valign != VerticalAlignment.Bottom) { width = bounds.Left + bounds.Width; height = lineHeight; } else { width = height = 0f; } if (maxSize.HasValue) { if (width > maxSize.Value.Width) { width = (float)maxSize.Value.Width; } if (height > maxSize.Value.Height) { height = (float)maxSize.Value.Height; } } var dx = halign == HorizontalAlignment.Left ? 0d : (halign == HorizontalAlignment.Center ? -width * 0.5 : -width); var dy = valign == VerticalAlignment.Bottom ? 0d : (valign == VerticalAlignment.Middle ? height * 0.5 : height); var x0 = -bounds.Left; var y0 = delta; this.SetFill(fill); this.SetAlias(false); this.gctx.SaveState(); this.gctx.TranslateCTM((float)p.X, (float)p.Y); if (!rotate.Equals(0)) { this.gctx.RotateCTM((float)(rotate / 180 * Math.PI)); } this.gctx.TranslateCTM((float)dx + x0, (float)dy + y0); this.gctx.ScaleCTM(1f, -1f); if (maxSize.HasValue) { var clipRect = new RectangleF(-x0, y0, (float)Math.Ceiling(width), (float)Math.Ceiling(height)); this.gctx.ClipToRect(clipRect); } textLine.Draw(this.gctx); this.gctx.RestoreState(); } } }
/// <summary> /// Draws the text. /// </summary> /// <param name="p">The position of the text.</param> /// <param name="text">The text.</param> /// <param name="fill">The fill color.</param> /// <param name="fontFamily">The font family.</param> /// <param name="fontSize">Size of the font.</param> /// <param name="fontWeight">The font weight.</param> /// <param name="rotate">The rotation angle.</param> /// <param name="halign">The horizontal alignment.</param> /// <param name="valign">The vertical alignment.</param> /// <param name="maxSize">The maximum size of the text.</param> public override void DrawText(ScreenPoint p, string text, OxyColor fill, string fontFamily, double fontSize, double fontWeight, double rotate, HorizontalAlignment halign, VerticalAlignment valign, OxySize?maxSize) { if (string.IsNullOrEmpty(text) || fontFamily == null) { return; } var fontName = GetActualFontName(fontFamily, fontWeight); using (var attributedString = new NSAttributedString(text, new CTStringAttributes { ForegroundColorFromContext = true, Font = new CTFont(fontName, (float)fontSize) })) { using (var textLine = new CTLine(attributedString)) { float width; float height; if (maxSize.HasValue || halign != HorizontalAlignment.Left || valign != VerticalAlignment.Bottom) { var bounds = textLine.GetImageBounds(this.gctx); width = bounds.Width; height = bounds.Height; } else { width = height = 0f; } if (maxSize.HasValue) { if (width > maxSize.Value.Width) { width = (float)maxSize.Value.Width; } if (height > maxSize.Value.Height) { height = (float)maxSize.Value.Height; } } var x = p.X; var y = p.Y; this.gctx.SaveState(); this.SetFill(fill); this.SetAlias(false); this.gctx.SetTextDrawingMode(CGTextDrawingMode.Fill); this.gctx.TextPosition = new PointF(0, 0); var dx = halign == HorizontalAlignment.Left ? 0d : (halign == HorizontalAlignment.Center ? -width * 0.5 : -width); var dy = valign == VerticalAlignment.Bottom ? 0d : (valign == VerticalAlignment.Middle ? height * 0.5 : height); this.gctx.TranslateCTM((float)x, (float)y); this.gctx.RotateCTM((float)(rotate / 180 * Math.PI)); this.gctx.TranslateCTM((float)dx, (float)dy); this.gctx.ScaleCTM(1f, -1f); if (maxSize.HasValue) { this.gctx.ClipToRect(new RectangleF(0, 0, width, height)); } textLine.Draw(this.gctx); this.gctx.RestoreState(); } } }
/// <summary> /// Measures the text. /// </summary> /// <param name="text">The text.</param> /// <param name="fontFamily">The font family.</param> /// <param name="fontSize">Size of the font.</param> /// <param name="fontWeight">The font weight.</param> /// <returns> /// The size of the text. /// </returns> public override OxySize MeasureText(string text, string fontFamily, double fontSize, double fontWeight) { if (string.IsNullOrEmpty(text) || fontFamily == null) { return OxySize.Empty; } var fontName = GetActualFontName(fontFamily, fontWeight); var font = this.GetCachedFont(fontName, (float)fontSize); using (var attributedString = new NSAttributedString(text, new CTStringAttributes { ForegroundColorFromContext = true, Font = font })) { using (var textLine = new CTLine(attributedString)) { nfloat lineHeight, delta; this.GetFontMetrics(font, out lineHeight, out delta); this.gctx.TextPosition = new CGPoint(0, 0); var bounds = textLine.GetImageBounds(this.gctx); return new OxySize(bounds.Left + bounds.Width, lineHeight); } } }
/// <summary> /// Draws the text. /// </summary> /// <param name="p">The position of the text.</param> /// <param name="text">The text.</param> /// <param name="fill">The fill color.</param> /// <param name="fontFamily">The font family.</param> /// <param name="fontSize">Size of the font.</param> /// <param name="fontWeight">The font weight.</param> /// <param name="rotate">The rotation angle.</param> /// <param name="halign">The horizontal alignment.</param> /// <param name="valign">The vertical alignment.</param> /// <param name="maxSize">The maximum size of the text.</param> public override void DrawText(ScreenPoint p, string text, OxyColor fill, string fontFamily, double fontSize, double fontWeight, double rotate, HorizontalAlignment halign, VerticalAlignment valign, OxySize? maxSize) { if (string.IsNullOrEmpty(text)) { return; } var fontName = GetActualFontName(fontFamily, fontWeight); var font = this.GetCachedFont(fontName, fontSize); using (var attributedString = new NSAttributedString(text, new CTStringAttributes { ForegroundColorFromContext = true, Font = font })) { using (var textLine = new CTLine(attributedString)) { nfloat width; nfloat height; this.gctx.TextPosition = new CGPoint(0, 0); nfloat lineHeight, delta; this.GetFontMetrics(font, out lineHeight, out delta); var bounds = textLine.GetImageBounds(this.gctx); if (maxSize.HasValue || halign != HorizontalAlignment.Left || valign != VerticalAlignment.Bottom) { width = bounds.Left + bounds.Width; height = lineHeight; } else { width = height = 0f; } if (maxSize.HasValue) { if (width > maxSize.Value.Width) { width = (float)maxSize.Value.Width; } if (height > maxSize.Value.Height) { height = (float)maxSize.Value.Height; } } var dx = halign == HorizontalAlignment.Left ? 0d : (halign == HorizontalAlignment.Center ? -width * 0.5 : -width); var dy = valign == VerticalAlignment.Bottom ? 0d : (valign == VerticalAlignment.Middle ? height * 0.5 : height); var x0 = -bounds.Left; var y0 = delta; this.SetFill(fill); this.SetAlias(false); this.gctx.SaveState(); this.gctx.TranslateCTM((float)p.X, (float)p.Y); if (!rotate.Equals(0)) { this.gctx.RotateCTM((float)(rotate / 180 * Math.PI)); } this.gctx.TranslateCTM((float)dx + x0, (float)dy + y0); this.gctx.ScaleCTM(1f, -1f); if (maxSize.HasValue) { var clipRect = new CGRect (-x0, y0, (float)Math.Ceiling (width), (float)Math.Ceiling (height)); this.gctx.ClipToRect(clipRect); } textLine.Draw(this.gctx); this.gctx.RestoreState(); } } }
/// <summary> /// Measures the text. /// </summary> /// <param name="text">The text.</param> /// <param name="fontFamily">The font family.</param> /// <param name="fontSize">Size of the font.</param> /// <param name="fontWeight">The font weight.</param> /// <returns> /// The size of the text. /// </returns> public override OxySize MeasureText(string text, string fontFamily, double fontSize, double fontWeight) { if (string.IsNullOrEmpty(text) || fontFamily == null) { return OxySize.Empty; } var fontName = GetActualFontName(fontFamily, fontWeight); using (var attributedString = new NSAttributedString(text, new CTStringAttributes { ForegroundColorFromContext = true, Font = new CTFont(fontName, (float)fontSize) })) { using (var textLine = new CTLine(attributedString)) { var bounds = textLine.GetImageBounds(this.gctx); return new OxySize(bounds.Width, bounds.Height); } } }
/// <summary> /// Draws the text. /// </summary> /// <param name="p">The position of the text.</param> /// <param name="text">The text.</param> /// <param name="fill">The fill color.</param> /// <param name="fontFamily">The font family.</param> /// <param name="fontSize">Size of the font.</param> /// <param name="fontWeight">The font weight.</param> /// <param name="rotate">The rotation angle.</param> /// <param name="halign">The horizontal alignment.</param> /// <param name="valign">The vertical alignment.</param> /// <param name="maxSize">The maximum size of the text.</param> public override void DrawText(ScreenPoint p, string text, OxyColor fill, string fontFamily, double fontSize, double fontWeight, double rotate, HorizontalAlignment halign, VerticalAlignment valign, OxySize? maxSize) { if (string.IsNullOrEmpty(text) || fontFamily == null) { return; } var fontName = GetActualFontName(fontFamily, fontWeight); using (var attributedString = new NSAttributedString(text, new CTStringAttributes { ForegroundColorFromContext = true, Font = new CTFont(fontName, (float)fontSize) })) { using (var textLine = new CTLine(attributedString)) { float width; float height; if (maxSize.HasValue || halign != HorizontalAlignment.Left || valign != VerticalAlignment.Bottom) { var bounds = textLine.GetImageBounds(this.gctx); width = bounds.Width; height = bounds.Height; } else { width = height = 0f; } if (maxSize.HasValue) { if (width > maxSize.Value.Width) { width = (float)maxSize.Value.Width; } if (height > maxSize.Value.Height) { height = (float)maxSize.Value.Height; } } var x = p.X; var y = p.Y; this.gctx.SaveState(); this.SetFill(fill); this.SetAlias(false); this.gctx.SetTextDrawingMode(CGTextDrawingMode.Fill); this.gctx.TextPosition = new PointF(0, 0); var dx = halign == HorizontalAlignment.Left ? 0d : (halign == HorizontalAlignment.Center ? -width * 0.5 : -width); var dy = valign == VerticalAlignment.Bottom ? 0d : (valign == VerticalAlignment.Middle ? height * 0.5 : height); this.gctx.TranslateCTM((float)x, (float)y); this.gctx.RotateCTM((float)(rotate / 180 * Math.PI)); this.gctx.TranslateCTM((float)dx, (float)dy); this.gctx.ScaleCTM(1f, -1f); if (maxSize.HasValue) { this.gctx.ClipToRect(new RectangleF(0, 0, width, height)); } textLine.Draw(this.gctx); this.gctx.RestoreState(); } } }