public override void Draw(CGRect rect) { base.Draw(rect); var cgContext = UIGraphics.GetCurrentContext(); if (_mathList != null) { var appleContext = new AppleGraphicsContext() { CgContext = cgContext }; cgContext.SaveState(); cgContext.SetStrokeColor(TextColor.CGColor); cgContext.SetFillColor(TextColor.CGColor); _displayList.Draw(appleContext); cgContext.RestoreState(); } else if (ErrorMessage.IsNonEmpty()) { cgContext.SaveState(); float errorFontSize = 20; var attributes = new UIStringAttributes { ForegroundColor = NColor.Red, Font = UIFont.SystemFontOfSize(errorFontSize), }; var attributedString = new NSAttributedString(ErrorMessage, attributes); var ctLine = new CTLine(attributedString); cgContext.TextPosition = new CGPoint(0, Bounds.Size.Height - errorFontSize); ctLine.Draw(cgContext); cgContext.RestoreState(); } }
public void DrawText(FormattedText formattedText, Point point, Rect?clipRect) { context.SaveState(); context.ScaleCTM(1, -1); context.SetFillColor(formattedText.Brush.Color.ToiOS()); var sizeOfText = formattedText.DesiredSize; var ctFont = new CTFont(formattedText.FontName, formattedText.FontSize); var attributedString = new NSAttributedString(formattedText.Text, new CTStringAttributes { ForegroundColor = formattedText.Brush.Color.ToiOS(), Font = ctFont }); context.TextPosition = new CGPoint(point.X, -(point.Y + sizeOfText.Height - ctFont.DescentMetric)); using (var textLine = new CTLine(attributedString)) { textLine.Draw(context); } context.RestoreState(); }
public override void Draw(CGRect rect) { base.Draw(rect); var gctx = UIGraphics.GetCurrentContext(); gctx.TranslateCTM(10, 0.5f * Bounds.Height); gctx.ScaleCTM(1, -1); gctx.RotateCTM((float)Math.PI * 315 / 180); gctx.SetFillColor(UIColor.Green.CGColor); string text = "Alfatraining"; var attributedString = new NSAttributedString(text, new CoreText.CTStringAttributes { ForegroundColorFromContext = true, Font = new CTFont("Arial", 24) }); using (var textLine = new CTLine(attributedString)) { textLine.Draw(gctx); } }
public override void Draw(CGRect rect) { base.Draw(rect); using (CGContext context = UIGraphics.GetCurrentContext()) { var radius = this.Frame.Width / 2; CGPath path = new CGPath(); path.AddArc(this.Frame.Width / 2, this.Frame.Height / 2, radius - 1, 0, (nfloat)(360 * radians), true); ViewColor.SetColor(); context.AddPath(path); context.DrawPath(CGPathDrawingMode.FillStroke); UIColor.White.SetColor(); CTFont font = new CTFont("Arial", 20); NSAttributedString stringToBeDrawn = new NSAttributedString(text, new CTStringAttributes() { Font = font, ForegroundColorFromContext = true, }); context.TranslateCTM(this.Frame.Width / 2 - stringToBeDrawn.Size.Width / 2, this.Frame.Height / 2 + stringToBeDrawn.Size.Height / 4); context.ScaleCTM(1, -1); CTLine line = new CTLine(stringToBeDrawn); line.Draw(context); font = null; stringToBeDrawn = null; path = null; } }
public Task <NSImage> LoadImageAsync( ImageSource imagesource, CancellationToken cancelationToken = default(CancellationToken), float scale = 1f) { NSImage image = null; var fontsource = imagesource as FontImageSource; if (fontsource != null) { var font = NSFont.FromFontName(fontsource.FontFamily ?? string.Empty, (float)fontsource.Size) ?? NSFont.SystemFontOfSize((float)fontsource.Size); var iconcolor = fontsource.Color.IsDefault ? _defaultColor : fontsource.Color; var attString = new NSAttributedString(fontsource.Glyph, font: font, foregroundColor: iconcolor.ToNSColor()); var imagesize = ((NSString)fontsource.Glyph).StringSize(attString.GetAttributes(0, out _)); using (var context = new CGBitmapContext(IntPtr.Zero, (nint)imagesize.Width, (nint)imagesize.Height, 8, (nint)imagesize.Width * 4, NSColorSpace.GenericRGBColorSpace.ColorSpace, CGImageAlphaInfo.PremultipliedFirst)) { using (var ctline = new CTLine(attString)) { ctline.Draw(context); } using (var cgImage = context.ToImage()) { image = new NSImage(cgImage, imagesize); } } } return(Task.FromResult(image)); }
public void DrawText(string text, Rect frame, Font font, TextAlignment alignment = TextAlignment.Left, Pen pen = null, Brush brush = null) { if (string.IsNullOrEmpty(text)) { return; } if (font == null) { throw new ArgumentNullException("font"); } SetBrush(brush); var pt = frame.TopLeft; using (var atext = new NSMutableAttributedString(text)) { atext.AddAttributes(new CTStringAttributes { ForegroundColorFromContext = true, Font = font.GetCTFont(), }, new NSRange(0, text.Length)); using (var l = new CTLine(atext)) { context.SaveState(); context.TranslateCTM((nfloat)(pt.X), (nfloat)(pt.Y)); context.TextPosition = CGPoint.Empty; l.Draw(context); context.RestoreState(); } } }
public void SimpleValuesSet() { var sa = new CTStringAttributes(); sa.ForegroundColor = UIColor.Blue.CGColor; sa.Font = new CTFont("Georgia-BoldItalic", 24); sa.UnderlineStyle = CTUnderlineStyle.Double; // It does not seem to do anything sa.UnderlineColor = UIColor.Blue.CGColor; sa.UnderlineStyleModifiers = CTUnderlineStyleModifiers.PatternDashDotDot; Assert.IsNull(sa.BaselineClass, "#0"); sa.BaselineClass = CTBaselineClass.IdeographicHigh; Assert.AreEqual(CTBaselineClass.IdeographicHigh, sa.BaselineClass, "#1"); sa.SetBaselineInfo(CTBaselineClass.Roman, 13); sa.SetBaselineInfo(CTBaselineClass.IdeographicHigh, 3); sa.SetWritingDirection(CTWritingDirection.LeftToRight); var size = new SizeF(300, 300); UIGraphics.BeginImageContext(size); var gctx = UIGraphics.GetCurrentContext(); gctx.SetFillColor(UIColor.Green.CGColor); var attributedString = new NSAttributedString("Test_ME~`", sa); using (var textLine = new CTLine(attributedString)) { textLine.Draw(gctx); } UIGraphics.EndImageContext(); }
public void DrawGlyphRunWithOffset(AttributedGlyphRun <TFont, TGlyph> run, PointF offset, Color?color) { DebugWriteLine($"Text {run} {offset.X} {offset.Y}"); CgContext.TextPosition = new CGPoint(CgContext.TextPosition.X + offset.X, CgContext.TextPosition.Y + offset.Y); if (color.HasValue) { CgContext.SetFillColor(color.Value.ToCgColor()); } using (var textLine = new CTLine(run.ToNsAttributedString())) textLine.Draw(CgContext); }
public void DrawGlyphRunWithOffset(AttributedGlyphRun <TFont, TGlyph> run, PointF offset, Color?color) { CgContext.TextPosition = new CGPoint (CgContext.TextPosition.X + offset.X, CgContext.TextPosition.Y + offset.Y); if (color.HasValue) { CgContext.SetFillColor(color.GetValueOrDefault().ToCGColor()); } using var textLine = new CTLine(run.ToNsAttributedString()); textLine.Draw(CgContext); }
public void DrawString(string s, float x, float y) { var attributedString = new NSAttributedString(s, _ct_attr); _c.TextPosition = new PointF((float)x, (float)/*height -*/ y); // I think that by using CTLine() below, we are preventing multi-line text. :-( using (var textLine = new CTLine(attributedString)) { textLine.Draw(_c); } }
public void DrawText(string text, Rect frame, Font font, TextAlignment alignment = TextAlignment.Left, Pen pen = null, Brush brush = null) { if (string.IsNullOrEmpty(text)) { return; } if (font == null) { throw new ArgumentNullException("font"); } SetBrush(brush); using (var atext = new NSMutableAttributedString(text)) { atext.AddAttributes(new CTStringAttributes { ForegroundColorFromContext = true, Font = font.GetCTFont(), }, new NSRange(0, text.Length)); using (var l = new CTLine(atext)) { nfloat asc, desc, lead; var len = l.GetTypographicBounds(out asc, out desc, out lead); var pt = frame.TopLeft; switch (alignment) { case TextAlignment.Left: pt.X = frame.X; break; case TextAlignment.Center: pt.X = frame.X + (frame.Width - len) / 2; break; case TextAlignment.Right: pt.X = frame.Right - len; break; } context.SaveState(); context.TranslateCTM((nfloat)(pt.X), (nfloat)(pt.Y)); context.TextPosition = CGPoint.Empty; l.Draw(context); context.RestoreState(); } } }
public void DrawText(string text, float x, float y, float width, float height, Xamarin.Forms.TextAlignment hAlignment, Xamarin.Forms.TextAlignment vAlignment) { if (font == null || font.NativeFont == null) { return; } var attributedString = new NSAttributedString(text, new CTStringAttributes() { Font = font.NativeFont, ForegroundColorFromContext = true }); CTLine ctLine = new CTLine(attributedString); CGSize size = new CGSize((float)ctLine.GetTypographicBounds(), font.NativeFont.CapHeightMetric + font.NativeFont.DescentMetric); if ((hAlignment & Xamarin.Forms.TextAlignment.Center) != 0) { x += (width - (float)size.Width) / 2; } else if ((hAlignment & Xamarin.Forms.TextAlignment.End) != 0) { x += width - (float)size.Width; } if ((vAlignment & Xamarin.Forms.TextAlignment.Center) != 0) { y += (height - (float)size.Height) / 2; } else if ((vAlignment & Xamarin.Forms.TextAlignment.End) != 0) { y += height - (float)size.Height; } context.SaveState(); context.ScaleCTM(1f, -1f); context.TextPosition = new CGPoint(x, -y - font.NativeFont.CapHeightMetric); ctLine.Draw(context); context.RestoreState(); ctLine.Dispose(); attributedString.Dispose(); }
/// <summary> /// Draws the specified string to the backing store. /// </summary> /// <param name="text">The <see cref="System.String"/> to draw.</param> /// <param name="font">The <see cref="System.Drawing.Font"/> that will be used.</param> /// <param name="brush">The <see cref="System.Drawing.Brush"/> that will be used.</param> /// <param name="point">The location of the text on the backing store, in 2d pixel coordinates. /// The origin (0, 0) lies at the top-left corner of the backing store.</param> public void DrawString(CTLine text, Point point) { if (m_Bitmap != null) { m_Bitmap.TextPosition = new CGPoint(point.X, point.Y); text.Draw(m_Bitmap); // Fix ugly looking anti-alias unsafe { // Pointer to the current pixel uint* pPixel = (uint*)m_PixelData; // Pointer value at which we terminate the loop (end of pixel data) var pLastPixel = pPixel + (int)m_Bitmap.Width * (int)m_Bitmap.Height; while (pPixel < pLastPixel) { // Get pixel data uint pixelValue = *pPixel; // Average RGB uint brightness = (((pixelValue & 0xFF) + ((pixelValue >> 8) & 0xFF) + ((pixelValue >> 16) & 0xFF)) * 21845) >> 16; // Division by 3 // Use brightness for alpha value, set R, G, and B 0xff (white) pixelValue = brightness << 24 | 0xffffff; // Copy back to image *pPixel = pixelValue; // Next pixel pPixel++; } } OpenTK.LoadTextureInternal(m_Texture, m_Bitmap); // copy bitmap to gl texture // Freeze texture to save memory // Todo: Make optional m_Bitmap.Dispose(); m_Bitmap = null; Marshal.FreeHGlobal(m_PixelData); m_PixelData = IntPtr.Zero; } }
/// <summary> /// To the color of the ns image with. /// </summary> /// <param name="icon">The icon.</param> /// <param name="size">The size.</param> /// <param name="color">The color.</param> /// <returns></returns> public static NSImage ToNSImageWithColor(this IIcon icon, nfloat size, CGColor color) { var attributedString = new NSAttributedString($"{icon.Character}", new CTStringAttributes { Font = new CTFont(Iconize.FindModuleOf(icon).FontName, size), ForegroundColorFromContext = true }); using (var ctx = new CGBitmapContext(IntPtr.Zero, (nint)size, (nint)size, 8, 4 * (nint)(size), CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedFirst)) { ctx.SetFillColor(color); using (var textLine = new CTLine(attributedString)) { textLine.Draw(ctx); } return(new NSImage(ctx.ToImage(), new CGSize(size, size))); } }
protected override void DrawText(string text, UIntPtr fontId, ref web_color color, ref position pos) { var fontHolder = GetFont(fontId); var attrString = fontHolder.GetAttributedString(text, color); Context.SaveState(); Context.TranslateCTM(pos.x, pos.y + fontHolder.Attributes.Font.AscentMetric); Context.ScaleCTM(1, -1); Context.TextMatrix = CGAffineTransform.MakeIdentity(); /*Context.SetAllowsAntialiasing(true); * Context.SetShouldAntialias(true); * Context.SetAllowsFontSmoothing(true); * Context.SetShouldSmoothFonts(true);*/ using (var textLine = new CTLine(attrString)) { textLine.Draw(Context); } Context.RestoreState(); }
void DrawTexts(List<string> tagStrings, List<RectangleF> rects) { using (CGContext gctx = UIGraphics.GetCurrentContext ()) { gctx.SaveState (); gctx.ScaleCTM (1f, -1f); gctx.TranslateCTM (0, Frame.Height); gctx.SetFillColor (UIColor.Green.CGColor); for (int i = 0; i < tagStrings.Count; i++) { gctx.SaveState (); var attributedString = new NSAttributedString (tagStrings[i], new CTStringAttributes { ForegroundColorFromContext = true, Font = new CTFont ("ArialMT", 48) }); gctx.TextPosition = new PointF (rects[i].X, rects[i].Y); using (var textLine = new CTLine (attributedString)) { textLine.Draw (gctx); } gctx.RestoreState (); } gctx.RestoreState (); } }
public override void DrawRect(CGRect dirtyRect) { base.DrawRect(dirtyRect); // Use Core Graphic routines to draw our UI using (CGContext context = NSGraphicsContext.CurrentContext.GraphicsPort) { // Fill the screening color. CGPath path = new CGPath(); CGRect frame = new CGRect(0, 0, base.Frame.Width, base.Frame.Height); path.AddRect(frame); ColorView.SetScreeningColor(_screening, context); context.AddPath(path); context.DrawPath(CGPathDrawingMode.Fill); // Draw screening information. context.TranslateCTM(2, ScreeningsView.VerticalTextOffset); context.SetTextDrawingMode(CGTextDrawingMode.Fill); ColorView.SetScreeningColor(_screening, context, true); CTStringAttributes attrs = new CTStringAttributes(); attrs.ForegroundColorFromContext = true; attrs.Font = ScreeningControl.StandardFont; var textPosition = new CGPoint(0, _lineHeight); string[] lines = _screening.ToScreeningLabelString().Split('\n'); foreach (var line in lines) { context.TextPosition = textPosition; var attributedString = new NSAttributedString(line, attrs); using (var textLine = new CTLine(attributedString)) { textLine.Draw(context); } textPosition.Y -= _lineHeight; } } NeedsDisplay = true; }
public void DrawText(string text, Rect frame, Font font, TextAlignment alignment = TextAlignment.Left, Pen pen = null, Brush brush = null) { if (string.IsNullOrEmpty(text)) { return; } if (font == null) { throw new ArgumentNullException("font"); } SetBrush(brush); // string fontName = font.Name; // context.SelectFont (font.Name, (nfloat)font.Size, CGTextEncoding.MacRoman); // // context.ShowTextAtPoint ((nfloat)frame.X, (nfloat)frame.Y, text); var pt = frame.TopLeft; using (var atext = new NSMutableAttributedString(text)) { atext.AddAttributes(new CTStringAttributes { ForegroundColorFromContext = true, Font = font.GetCTFont(), }, new NSRange(0, text.Length)); using (var l = new CTLine(atext)) { nfloat asc, desc, lead; var width = l.GetTypographicBounds(out asc, out desc, out lead); context.SaveState(); context.TranslateCTM((nfloat)(pt.X - width / 2), (nfloat)(pt.Y + desc)); context.TextPosition = CGPoint.Empty; l.Draw(context); context.RestoreState(); } } }
public override void Draw(CGRect rect) { base.Draw(rect); using (var g = UIGraphics.GetCurrentContext()) { // scale and translate the CTM so the image appears upright g.ScaleCTM(1, -1); g.TranslateCTM(0, -Bounds.Height); if (image != null) { nfloat imageWidth = image.Size.Width; nfloat imageHeight = image.Size.Height; var centerX = Bounds.Width / 2 - (imageWidth / 2); var centerY = (Bounds.Height / 2 - (imageHeight / 2)); CGRect imageRect = new CGRect(centerX, centerY - (centerY / 3), imageWidth, imageHeight); g.DrawImage(imageRect, image.CGImage); } g.TranslateCTM(10, .8f * Bounds.Height); g.SetFillColor(UIColor.Black.CGColor); string someText = "Like this manufacturer?"; var attributedString = new NSAttributedString(someText, new CTStringAttributes { ForegroundColorFromContext = true, Font = new CTFont("Arial", 12) }); using (var textLine = new CTLine(attributedString)) { textLine.Draw(g); } } }
public override void Draw(CGRect rect) { base.Draw (rect); var gctx = UIGraphics.GetCurrentContext (); gctx.TranslateCTM (10, 0.5f * Bounds.Height); gctx.ScaleCTM (1, -1); gctx.RotateCTM ((float)Math.PI * 315 / 180); gctx.SetFillColor (UIColor.Green.CGColor); string someText = "你好世界"; var attributedString = new NSAttributedString (someText, new CTStringAttributes{ ForegroundColorFromContext = true, Font = new CTFont ("Arial", 24) }); using (var textLine = new CTLine (attributedString)) { textLine.Draw (gctx); } }
public void DrawText(string text, float x, float y) { if (font == null || font.NativeFont == null) { return; } var attributedString = new NSAttributedString(text, new CTStringAttributes() { Font = font.NativeFont, ForegroundColorFromContext = true }); CTLine ctLine = new CTLine(attributedString); context.SaveState(); context.ScaleCTM(1f, -1f); context.TextPosition = new CGPoint(x, -y - font.NativeFont.CapHeightMetric); ctLine.Draw(context); context.RestoreState(); ctLine.Dispose(); attributedString.Dispose(); }
/// <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> /// 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(); } } }
protected override void DrawParagraph(Paragraph paragraph) { int currentRegionLeft = SecondsToXPosition(paragraph.StartTime.TotalSeconds - StartPositionSeconds); int currentRegionRight = SecondsToXPosition(paragraph.EndTime.TotalSeconds - StartPositionSeconds); int currentRegionWidth = currentRegionRight - currentRegionLeft; // background _context.SetFillColor(new CGColor(1, 1, 1, (System.nfloat) 0.2)); _context.FillRect(new CGRect(currentRegionLeft, 0, currentRegionWidth, Height)); // using (var brush = new SolidBrush(Color.FromArgb(42, 255, 255, 255))) // graphics.FillRectangle(brush, currentRegionLeft, 0, currentRegionWidth, graphics.VisibleClipBounds.Height); // // left edge _context.SetStrokeColor(Color.FromArgb(175, 0, 100, 0).ToCGColor()); DrawLine(currentRegionLeft, 0, currentRegionLeft, Height); // using (var pen = new Pen(new SolidBrush(Color.FromArgb(175, 0, 100, 0))) { DashStyle = DashStyle.Solid, Width = 2 }) // graphics.DrawLine(pen, currentRegionLeft, 0, currentRegionLeft, graphics.VisibleClipBounds.Height); // // right edge _context.SetStrokeColor(Color.FromArgb(175, 110, 10, 10).ToCGColor()); DrawLine(currentRegionRight - 1, 0, currentRegionRight - 1, Height); // using (var pen = new Pen(new SolidBrush(Color.FromArgb(175, 110, 10, 10))) { DashStyle = DashStyle.Dash, Width = 2 }) // graphics.DrawLine(pen, currentRegionRight - 1, 0, currentRegionRight - 1, graphics.VisibleClipBounds.Height); // using (var font = new Font(Configuration.Settings.General.SubtitleFontName, TextSize, TextBold ? FontStyle.Bold : FontStyle.Regular)) // using (var textBrush = new SolidBrush(TextColor)) // using (var outlineBrush = new SolidBrush(Color.Black)) // { // Action<string, int, int> drawStringOutlined = (text, x, y) => // { // // poor mans outline + text // graphics.DrawString(text, font, outlineBrush, new PointF(x, y - 1)); // graphics.DrawString(text, font, outlineBrush, new PointF(x, y + 1)); // graphics.DrawString(text, font, outlineBrush, new PointF(x - 1, y)); // graphics.DrawString(text, font, outlineBrush, new PointF(x + 1, y)); // graphics.DrawString(text, font, textBrush, new PointF(x, y)); // }; // const int padding = 3; double n = _zoomFactor * _wavePeaks.SampleRate; _context.SelectFont("Arial", 12f, CGTextEncoding.MacRoman); _context.SetTextDrawingMode(CGTextDrawingMode.FillStroke); _context.SetStrokeColor(TextColor.ToCGColor()); // paragraph text if (n > 80) { string text = HtmlUtil.RemoveHtmlTags(paragraph.Text, true).Replace(Environment.NewLine, " "); // int removeLength = 1; // while (text.Length > removeLength && graphics.MeasureString(text, font).Width > currentRegionWidth - padding - 1) // { // text = text.Remove(text.Length - removeLength).TrimEnd() + "…"; // removeLength = 2; // } var attributedString = new NSAttributedString(text, new CTStringAttributes { ForegroundColorFromContext = true, Font = new CTFont("Arial", 12) }); _context.TextPosition = new CGPoint(currentRegionLeft + padding, padding); using (var textLine = new CTLine(attributedString)) { textLine.Draw(_context); } // drawStringOutlined(text, currentRegionLeft + padding, padding); } // // // paragraph number if (n > 25) { string text = "#" + paragraph.Number + " " + paragraph.Duration.ToShortDisplayString(); // if (n <= 51 || graphics.MeasureString(text, font).Width >= currentRegionWidth - padding - 1) // text = "#" + paragraph.Number; // drawStringOutlined(text, currentRegionLeft + padding, Height - 14 - (int)graphics.MeasureString("#", font).Height); _context.ShowTextAtPoint(currentRegionLeft + padding, Height - 14, text); } // } }
/// <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(); } } }
public override void DrawRect(CGRect dirtyRect) { if (!drawInit) { this.Window.Title = "PixelFarm"; drawInit = true; destImg = new ActualImage(destImgW, destImgH, PixelFarm.Agg.PixelFormat.ARGB32); imgGfx2d = new ImageGraphics2D(destImg); //no platform p = new AggCanvasPainter(imgGfx2d); stride = destImg.Stride; LoadGlyphs(); } //base.Draw(rect); base.DrawRect(dirtyRect); p.Clear(Color.Yellow); p.FillColor = Color.Black; p.Fill(vxs); var data = Foundation.NSData.FromArray(ActualImage.GetBuffer(destImg)); CGDataProvider provider = new CGDataProvider(data); using (var myImg2 = new CGImage( destImgW, destImgH, 8, 32, stride, CGColorSpace.CreateGenericRgb(), CGBitmapFlags.PremultipliedLast, provider, null, true, CGColorRenderingIntent.AbsoluteColorimetric)) using (var nsGraphics = AppKit.NSGraphicsContext.CurrentContext) { CGContext g = nsGraphics.CGContext; CGColor color0 = new CGColor(1, 1, 1, 1); g.SetFillColor(color0); //g.ClearRect(new CGRect(0, 0, 800, 600)); //---------- CGColor color1 = new CGColor(1, 0, 0, 1); g.SetFillColor(color1); CGRect s1 = CGRect.FromLTRB(0, 0, 50, 50); CGPath gpath = new CGPath(); gpath.AddRect(CGAffineTransform.MakeTranslation(20, 20), s1); g.AddPath(gpath); g.FillPath(); CGRect s2 = new CGRect(50, 50, destImgW, destImgH); g.DrawImage(s2, myImg2); // //g.FillRect(s1); CGColor color2 = new CGColor(0, 0, 1, 1); g.SetFillColor(color2); g.TranslateCTM(30, 30); var strAttr = new CTStringAttributes { ForegroundColorFromContext = true, Font = new CTFont("Arial", 24) }; g.ScaleCTM(1, -1); //flip NSAttributedString a_str = new NSAttributedString("abcd", strAttr); using (CTLine line = new CTLine(a_str)) { line.Draw(g); } ////if (chkBorder.Checked) ////{ //// //5.4 //// p.StrokeColor = PixelFarm.Drawing.Color.Green; //// //user can specific border width here... //// //p.StrokeWidth = 2; //// //5.5 //// p.Draw(vxs); ////} ////6. use this util to copy image from Agg actual image to System.Drawing.Bitmap //BitmapHelper.CopyToWindowsBitmap(destImg, winBmp, new RectInt(0, 0, 300, 300)); ////--------------- ////7. just render our bitmap //g.ClearRect(rect); //g.DrawImage(winBmp, new Point(10, 0)); } //// scale and translate the CTM so the image appears upright //g.ScaleCTM (1, -1); //g.TranslateCTM (0, -Bounds.Height); //g.DrawImage (rect, UIImage.FromFile ("MyImage.png").CGImage); //// translate the CTM by the font size so it displays on screen //float fontSize = 35f; //g.TranslateCTM (0, fontSize); //// set general-purpose graphics state //g.SetLineWidth (1.0f); //g.SetStrokeColor (UIColor.Yellow.CGColor); //g.SetFillColor (UIColor.Red.CGColor); //g.SetShadow (new CGSize (5, 5), 0, UIColor.Blue.CGColor); //// set text specific graphics state //g.SetTextDrawingMode (CGTextDrawingMode.FillStroke); //g.SelectFont ("Helvetica", fontSize, CGTextEncoding.MacRoman); //// show the text //g.ShowText ("Hello Core Graphics"); }
private static void DrawText(this CGContext context, string source, float x, float y, string fontName, float fontSize, bool isCenter) { var stringAttributes = new CTStringAttributes { ForegroundColorFromContext = true, Font = new CTFont(fontName, fontSize) }; var attributedString = new NSAttributedString(source, stringAttributes); context.TextPosition = new PointF(x, y); using (var textLine = new CTLine(attributedString)) { if (isCenter) context.TextPosition = new PointF((float)(x - (textLine.GetTypographicBounds() / 2)), y); textLine.Draw(context); } }
public void DrawText (string text, Rect frame, Font font, TextAlignment alignment = TextAlignment.Left, Pen pen = null, Brush brush = null) { if (string.IsNullOrEmpty (text)) return; if (font == null) throw new ArgumentNullException ("font"); SetBrush (brush); using (var atext = new NSMutableAttributedString (text)) { atext.AddAttributes (new CTStringAttributes { ForegroundColorFromContext = true, StrokeColor = pen != null ? pen.Color.GetCGColor () : null, Font = font.GetCTFont (), }, new NSRange (0, text.Length)); using (var l = new CTLine (atext)) { nfloat asc, desc, lead; var len = l.GetTypographicBounds (out asc, out desc, out lead); var pt = frame.TopLeft; switch (alignment) { case TextAlignment.Left: pt.X = frame.X; break; case TextAlignment.Center: pt.X = frame.X + (frame.Width - len) / 2; break; case TextAlignment.Right: pt.X = frame.Right - len; break; } context.SaveState (); context.TranslateCTM ((nfloat)(pt.X), (nfloat)(pt.Y)); context.TextPosition = CGPoint.Empty; l.Draw (context); context.RestoreState (); } } }
public void DrawText(string text, Rect frame, Font font, TextAlignment alignment = TextAlignment.Left, Pen pen = null, Brush brush = null) { if (string.IsNullOrEmpty (text)) return; if (font == null) throw new ArgumentNullException ("font"); SetBrush (brush); // string fontName = font.Name; // context.SelectFont (font.Name, (nfloat)font.Size, CGTextEncoding.MacRoman); // // context.ShowTextAtPoint ((nfloat)frame.X, (nfloat)frame.Y, text); var pt = frame.TopLeft; using (var atext = new NSMutableAttributedString (text)) { atext.AddAttributes (new CTStringAttributes { ForegroundColorFromContext = true, Font = font.GetCTFont (), }, new NSRange (0, text.Length)); using (var l = new CTLine (atext)) { nfloat asc, desc, lead; var width = l.GetTypographicBounds (out asc, out desc, out lead); context.SaveState (); context.TranslateCTM ((nfloat)(pt.X - width / 2), (nfloat)(pt.Y + desc)); context.TextPosition = CGPoint.Empty; l.Draw (context); context.RestoreState (); } } }
// TODO using Core Text here is going to be a huge problem because we have to // change the coordinate system for every call to DrawString rather than changing // it once for all the calls. Not even sure we have enough info (the height of // the coord system or UIView) to do the transform? // TODO if we know the text is one-line, we can skip the big transform and just use // the textmatrix thing. public void DrawString(string s, float box_x, float box_y, float box_width, float box_height, Xamarin.Forms.LineBreakMode lineBreak, Xamarin.Forms.TextAlignment horizontal_align, Xamarin.Forms.TextAlignment vertical_align ) { _c.SaveState(); // TODO _c.SetTextDrawingMode (CGTextDrawingMode.Fill); // Core Text uses a different coordinate system. _c.TranslateCTM(0, _overall_height); _c.ScaleCTM(1, -1); var attributedString = new NSAttributedString(s, _ct_attr); float text_width; float text_height; if ( (horizontal_align != TextAlignment.Start) || (vertical_align != TextAlignment.Start)) { // not all of the alignments need the bounding rect. don't // calculate it if not needed. var sizeAttr = attributedString.GetBoundingRect( new SizeF( (float)box_width, int.MaxValue), NSStringDrawingOptions.UsesLineFragmentOrigin | NSStringDrawingOptions.UsesFontLeading, null ); text_width = sizeAttr.Width; //text_height = sizeAttr.Height; //text_height = sizeAttr.Height + uif.Descender; // descender is negative text_height = _font_ui.CapHeight; } else { text_width = 0; text_height = 0; } //Console.WriteLine ("width: {0} height: {1}", text_width, text_height); float x; switch (horizontal_align) { case Xamarin.Forms.TextAlignment.End: x = (box_x + box_width) - text_width; break; case Xamarin.Forms.TextAlignment.Center: x = box_x + (box_width - text_width) / 2; break; case Xamarin.Forms.TextAlignment.Start: default: x = box_x; break; } float y; switch (vertical_align) { case Xamarin.Forms.TextAlignment.End: y = box_y + text_height; break; case Xamarin.Forms.TextAlignment.Center: y = (box_y + box_height) - (box_height - text_height) / 2; break; case Xamarin.Forms.TextAlignment.Start: default: y = (box_y + box_height); break; } _c.TextPosition = new PointF((float)x, (float)(_overall_height - y)); // I think that by using CTLine() below, we are preventing multi-line text. :-( using (var textLine = new CTLine(attributedString)) { textLine.Draw(_c); } //gctx.StrokeRect (new RectangleF(x, height - y, text_width, text_height)); _c.RestoreState(); }
public override void Draw (RectangleF rect) { var context = UIGraphics.GetCurrentContext (); context.TextMatrix = CGAffineTransform.MakeScale (1f, -1f); context.TextPosition = new PointF (10f, base.Center.Y); var attrString = new NSAttributedString ("This space intentionally blank. See stdout.", new CTStringAttributes () { Font = new CTFont ("Arial", 8f), }); using (var line = new CTLine (attrString)) { // line.Draw (context); // Get the offset needed to center the line: var flush = 0.5f; // centered var penOffset = line.GetPenOffsetForFlush (flush, Frame.Width); // Move the given text drawing position by the calculated offset and draw context.TextPosition = new PointF ((float) penOffset, base.Center.Y); line.Draw (context); } FontDescriptorChecks (); FontCollectionChecks (); ParagraphStyleChecks (); }
public override void Draw (RectangleF rect) { var context = UIGraphics.GetCurrentContext (); context.TextMatrix = CGAffineTransform.MakeScale (1f, -1f); context.TextPosition = new PointF (10f, base.Center.Y); var attrString = new NSAttributedString ("my Arial 20 label!", new CTStringAttributes () { Font = new CTFont ("Arial", 20f), }); using (var line = new CTLine (attrString)) line.Draw (context); }
protected override void DrawParagraph(Paragraph paragraph) { int currentRegionLeft = SecondsToXPosition(paragraph.StartTime.TotalSeconds - StartPositionSeconds); int currentRegionRight = SecondsToXPosition(paragraph.EndTime.TotalSeconds - StartPositionSeconds); int currentRegionWidth = currentRegionRight - currentRegionLeft; // background _context.SetFillColor(new CGColor(1, 1, 1, (System.nfloat)0.2)); _context.FillRect(new CGRect(currentRegionLeft, 0, currentRegionWidth, Height)); // using (var brush = new SolidBrush(Color.FromArgb(42, 255, 255, 255))) // graphics.FillRectangle(brush, currentRegionLeft, 0, currentRegionWidth, graphics.VisibleClipBounds.Height); // // left edge _context.SetStrokeColor(Color.FromArgb(175, 0, 100, 0).ToCGColor()); DrawLine(currentRegionLeft, 0, currentRegionLeft, Height); // using (var pen = new Pen(new SolidBrush(Color.FromArgb(175, 0, 100, 0))) { DashStyle = DashStyle.Solid, Width = 2 }) // graphics.DrawLine(pen, currentRegionLeft, 0, currentRegionLeft, graphics.VisibleClipBounds.Height); // // right edge _context.SetStrokeColor(Color.FromArgb(175, 110, 10, 10).ToCGColor()); DrawLine(currentRegionRight - 1, 0, currentRegionRight - 1, Height); // using (var pen = new Pen(new SolidBrush(Color.FromArgb(175, 110, 10, 10))) { DashStyle = DashStyle.Dash, Width = 2 }) // graphics.DrawLine(pen, currentRegionRight - 1, 0, currentRegionRight - 1, graphics.VisibleClipBounds.Height); // using (var font = new Font(Configuration.Settings.General.SubtitleFontName, TextSize, TextBold ? FontStyle.Bold : FontStyle.Regular)) // using (var textBrush = new SolidBrush(TextColor)) // using (var outlineBrush = new SolidBrush(Color.Black)) // { // Action<string, int, int> drawStringOutlined = (text, x, y) => // { // // poor mans outline + text // graphics.DrawString(text, font, outlineBrush, new PointF(x, y - 1)); // graphics.DrawString(text, font, outlineBrush, new PointF(x, y + 1)); // graphics.DrawString(text, font, outlineBrush, new PointF(x - 1, y)); // graphics.DrawString(text, font, outlineBrush, new PointF(x + 1, y)); // graphics.DrawString(text, font, textBrush, new PointF(x, y)); // }; // const int padding = 3; double n = _zoomFactor * _wavePeaks.SampleRate; _context.SelectFont("Arial", 12f, CGTextEncoding.MacRoman); _context.SetTextDrawingMode(CGTextDrawingMode.FillStroke); _context.SetStrokeColor(TextColor.ToCGColor()); // paragraph text if (n > 80) { string text = HtmlUtil.RemoveHtmlTags(paragraph.Text, true).Replace(Environment.NewLine, " "); // int removeLength = 1; // while (text.Length > removeLength && graphics.MeasureString(text, font).Width > currentRegionWidth - padding - 1) // { // text = text.Remove(text.Length - removeLength).TrimEnd() + "…"; // removeLength = 2; // } var attributedString = new NSAttributedString (text, new CTStringAttributes{ ForegroundColorFromContext = true, Font = new CTFont ("Arial", 12) }); _context.TextPosition = new CGPoint(currentRegionLeft + padding, padding); using (var textLine = new CTLine (attributedString)) { textLine.Draw (_context); } // drawStringOutlined(text, currentRegionLeft + padding, padding); } // // // paragraph number if (n > 25) { string text = "#" + paragraph.Number + " " + paragraph.Duration.ToShortDisplayString(); // if (n <= 51 || graphics.MeasureString(text, font).Width >= currentRegionWidth - padding - 1) // text = "#" + paragraph.Number; // drawStringOutlined(text, currentRegionLeft + padding, Height - 14 - (int)graphics.MeasureString("#", font).Height); _context.ShowTextAtPoint(currentRegionLeft + padding, Height - 14, text); } // } }
/// <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(); } } }
public void DrawText(string text, Rect frame, Font font, TextAlignment alignment = TextAlignment.Left, Pen pen = null, Brush brush = null) { if (string.IsNullOrEmpty (text)) return; if (font == null) throw new ArgumentNullException ("font"); SetBrush (brush); var pt = frame.TopLeft; using (var atext = new NSMutableAttributedString (text)) { atext.AddAttributes (new CTStringAttributes { ForegroundColorFromContext = true, Font = font.GetCTFont (), }, new NSRange (0, text.Length)); using (var l = new CTLine (atext)) { context.SaveState (); context.TranslateCTM ((nfloat)(pt.X), (nfloat)(pt.Y)); context.TextPosition = CGPoint.Empty; l.Draw (context); context.RestoreState (); } } }