public RectangleF[] GetBoundingRectsForGlyphs(TFont font, TGlyph[] glyphs) { CTFont ctFont = font.CtFont; int nVariants = glyphs.Length; CGRect[] rects = new CGRect[nVariants]; ctFont.GetBoundingRects(CTFontOrientation.Horizontal, glyphs, rects, nVariants); RectangleF[] r = rects.Select(rect => (RectangleF)rect).ToArray(); return(r); }
public static CGImage GetImage(string glyphName, int widthUnit, int heightUnit , float fontSizeUnit , CGColor textColor , CGColor backColor , float?scaleOrNull = null , PointF?offsetOrNull = null) { float scale = scaleOrNull ?? ScaleOfMainScreen; float fontSize = fontSizeUnit * scale; var ctfont = new CTFont(FontAwesomeUtil.Font, fontSize, CGAffineTransform.MakeIdentity()); float fontMetric = ctfont.AscentMetric + ctfont.DescentMetric; ushort[] glyphs = new ushort[] { ctfont.GetGlyphWithName(glyphName) }; int width = (int)(widthUnit * scale); int height = (int)(heightUnit * scale); PointF offset = offsetOrNull ?? new PointF(0f, 0f); offset.X *= scale; offset.Y *= scale; CGImage cgImg = SetUpBitmapContextAndDraw(width, height, (cg, rect) => { if (backColor.Alpha > 0f) { cg.SetFillColor(backColor); cg.FillRect(cg.GetClipBoundingBox()); } var bounds = ctfont.GetBoundingRects(CTFontOrientation.Default, glyphs); var x = rect.GetMidX() - bounds.Width / 2f; x -= bounds.GetMinX(); var y = rect.GetMidY() - fontMetric / 2f; y += -ctfont.UnderlinePosition + ctfont.UnderlineThickness; x += offset.X; y += offset.Y; // draw bounds // cg.SetStrokeColor (1f, 1f); // bounds.X = x; // bounds.Y = y; // cg.StrokeRect (bounds); cg.SetFillColor(textColor); cg.SetTextDrawingMode(CGTextDrawingMode.Fill); ctfont.DrawGlyphs(cg, glyphs, new PointF[] { new PointF(x, y) }); }); return(cgImg); }
// Get square sized image // using CoreText public static CGImage GetImageForBarItem(string glyphName, int sizeUnit = 20 , float?scaleOrNull = null , PointF?offsetOrNull = null) { float scale = scaleOrNull ?? ScaleOfMainScreen; float fontSize = sizeUnit * scale * 0.9f; // 0.9f is a magic value for adjust var ctfont = new CTFont(FontAwesomeUtil.Font, fontSize, CGAffineTransform.MakeIdentity()); float fontMetric = ctfont.AscentMetric + ctfont.DescentMetric; ushort[] glyphs = new ushort[] { ctfont.GetGlyphWithName(glyphName) }; int size = (int)(sizeUnit * scale); PointF offset = offsetOrNull ?? new PointF(0f, 0f); offset.X *= scale; offset.Y *= scale; CGImage img = SetUpBitmapContextForBarItemAndDraw(size, size, (cg, rect) => { var bounds = ctfont.GetBoundingRects(CTFontOrientation.Default, glyphs); var x = rect.GetMidX() - bounds.Width / 2f; x -= bounds.GetMinX(); var y = rect.GetMidY() - fontMetric / 2f; y += -ctfont.UnderlinePosition + ctfont.UnderlineThickness; x += offset.X; y += offset.Y; #region for_debug // Console.Write ("Glyph geom {0} : {1}", glyphName, bounds); // if (rect.Width < bounds.Width || rect.Height < bounds.Height) { // Console.Write (" Glyph geom larger than bounds"); // } // Console.WriteLine (); // // // draw underline // cg.SaveState (); // cg.SetStrokeColor (1f, 0.4f); // cg.SetLineWidth (ctfont.UnderlineThickness); // cg.BeginPath (); // cg.MoveTo (rect.GetMinX (), rect.GetMidY ()+ctfont.UnderlinePosition); // cg.AddLineToPoint (rect.GetMaxX (), rect.GetMidY ()+ctfont.UnderlinePosition); // cg.StrokePath (); // // cg.SetLineWidth (1f); // cg.SetStrokeColor (1f, 0.5f); // cg.BeginPath (); // cg.StrokeLineSegments (new PointF[] { // new PointF (rect.GetMinX (), rect.GetMidY () - (fontMetric / 2f)) // , new PointF (rect.GetMaxX (), rect.GetMidY () - (fontMetric / 2f))}); // cg.StrokeLineSegments (new PointF[] { // new PointF (rect.GetMinX (), rect.GetMidY () + (fontMetric / 2f)) // , new PointF (rect.GetMaxX (), rect.GetMidY () + (fontMetric / 2f))}); // // cg.RestoreState (); // // draw diagonal lines // cg.SetStrokeColor (1f, 0.5f); // cg.BeginPath (); // cg.MoveTo (rect.GetMinX (), rect.GetMinY ()); // cg.AddLineToPoint (rect.GetMaxX (), rect.GetMaxY ()); // cg.StrokePath (); // cg.BeginPath (); // cg.MoveTo (rect.GetMinX (), rect.GetMaxY ()); // cg.AddLineToPoint (rect.GetMaxX (), rect.GetMinY ()); // cg.StrokePath (); // // draw bounds // cg.StrokeRect (new RectangleF (x + bounds.X, y + bounds.Y, bounds.Width, bounds.Height)); #endregion cg.SetFillColor(1f, 1f); cg.SetTextDrawingMode(CGTextDrawingMode.Fill); ctfont.DrawGlyphs(cg, glyphs, new PointF[] { new PointF(x, y) }); }); return(img); }