public static double GetLineHeight(string fontName) { var editorFont = Xwt.Drawing.Font.FromName(fontName); using (var nsFont = NSFont.FromFontName(editorFont.Family, (nfloat)editorFont.Size)) using (var lm = new NSLayoutManager()) return(lm.DefaultLineHeightForFont(nsFont)); }
public static double GetLineHeight(Xwt.Drawing.Font font) { if (font is null) { throw new ArgumentNullException(nameof(font)); } using (var lm = new NSLayoutManager()) return(lm.DefaultLineHeightForFont(font.ToNSFont())); }
public static float LineHeight(this NSFont font) { return(manager.DefaultLineHeightForFont(font)); /* * var leading = Math.Floor (Math.Max (0, font.Leading) + 0.5f); * var lineHeight = (float)(Math.Floor(font.Ascender + 0.5f) - Math.Floor (font.Descender + 0.5f) + leading); * * if (leading > 0) * return lineHeight; * else * return (float)(lineHeight + Math.Floor(0.2 * lineHeight + 0.5));*/ }
public override CGRect DrawingRectForBounds(CGRect theRect) { var lineHeight = lm.DefaultLineHeightForFont(Font); // Vertical text position compensation for the case when the text field is too narrow. // 1px is not enough, but 2px would cause artifacts. var dr = base.DrawingRectForBounds(theRect); if (lineHeight > dr.Height) { dr.Y -= 1; } return(dr); }
private SCNNode NodeWithText(string message, TextType type, int level) { var textNode = SCNNode.Create(); // Bullet if (type == TextType.Bullet) { if (level == 0) { message = "• " + message; } else { var bullet = SCNNode.Create(); bullet.Geometry = SCNPlane.Create(10.0f, 10.0f); bullet.Geometry.FirstMaterial.Diffuse.Contents = NSColor.FromDeviceRgba((float)(160 / 255.0), (float)(182 / 255.0), (float)(203 / 255.0), 1); bullet.Position = new SCNVector3(80, 30, 0); bullet.Geometry.FirstMaterial.LightingModelName = SCNLightingModel.Constant; bullet.Geometry.FirstMaterial.WritesToDepthBuffer = false; bullet.RenderingOrder = 1; textNode.AddChildNode(bullet); message = "\t\t\t\t" + message; } } // Text attributes var extrusion = ExtrusionDepthForTextType(type); var text = SCNText.Create(message, extrusion); textNode.Geometry = text; text.Flatness = TEXT_FLATNESS; text.ChamferRadius = (extrusion == 0 ? 0 : TEXT_CHAMFER); text.Font = FontForTextType(type, level); // Layout var layoutManager = new NSLayoutManager(); var leading = layoutManager.DefaultLineHeightForFont(text.Font); var descender = text.Font.Descender; int newlineCount = ((string[])(((string)text.String.ToString()).Split('\n'))).Length; textNode.Pivot = SCNMatrix4.CreateTranslation(0, -descender + newlineCount * leading, 0); if (type == TextType.Chapter) { var min = new SCNVector3(); var max = new SCNVector3(); textNode.GetBoundingBox(ref min, ref max); textNode.Position = new SCNVector3(-11, (-min.Y + textNode.Pivot.M42) * TEXT_SCALE, 7); textNode.Scale = new SCNVector3(TEXT_SCALE, TEXT_SCALE, TEXT_SCALE); textNode.Rotation = new SCNVector4(0, 1, 0, (float)(Math.PI / 270.0)); } else { textNode.Position = new SCNVector3(-16, CurrentBaseline, 0); textNode.Scale = new SCNVector3(TEXT_SCALE, TEXT_SCALE, TEXT_SCALE); } // Material if (type == TextType.Chapter) { var frontMaterial = SCNMaterial.Create(); var sideMaterial = SCNMaterial.Create(); frontMaterial.Emission.Contents = NSColor.DarkGray; frontMaterial.Diffuse.Contents = ColorForTextType(type, level); sideMaterial.Diffuse.Contents = NSColor.LightGray; textNode.Geometry.Materials = new SCNMaterial[] { frontMaterial, frontMaterial, sideMaterial, frontMaterial, frontMaterial }; } else { // Full white emissive material (visible even when there is no light) textNode.Geometry.FirstMaterial = SCNMaterial.Create(); textNode.Geometry.FirstMaterial.Diffuse.Contents = NSColor.Black; textNode.Geometry.FirstMaterial.Emission.Contents = ColorForTextType(type, level); // Don't write to the depth buffer because we don't want the text to be reflected textNode.Geometry.FirstMaterial.WritesToDepthBuffer = false; // Render last textNode.RenderingOrder = 1; } return(textNode); }
private SCNNode NodeWithText (string message, TextType type, int level) { var textNode = SCNNode.Create (); // Bullet if (type == TextType.Bullet) { if (level == 0) message = "• " + message; else { var bullet = SCNNode.Create (); bullet.Geometry = SCNPlane.Create (10.0f, 10.0f); bullet.Geometry.FirstMaterial.Diffuse.Contents = NSColor.FromDeviceRgba ((float)(160 / 255.0), (float)(182 / 255.0), (float)(203 / 255.0), 1); bullet.Position = new SCNVector3 (80, 30, 0); bullet.Geometry.FirstMaterial.LightingModelName = SCNLightingModel.Constant; bullet.Geometry.FirstMaterial.WritesToDepthBuffer = false; bullet.RenderingOrder = 1; textNode.AddChildNode (bullet); message = "\t\t\t\t" + message; } } // Text attributes var extrusion = ExtrusionDepthForTextType (type); var text = SCNText.Create (message, extrusion); textNode.Geometry = text; text.Flatness = TEXT_FLATNESS; text.ChamferRadius = (extrusion == 0 ? 0 : TEXT_CHAMFER); text.Font = FontForTextType (type, level); // Layout var layoutManager = new NSLayoutManager (); var leading = layoutManager.DefaultLineHeightForFont (text.Font); var descender = text.Font.Descender; int newlineCount = ((string[])(((string)text.String.ToString ()).Split ('\n'))).Length; textNode.Pivot = SCNMatrix4.CreateTranslation (0, -descender + newlineCount * leading, 0); if (type == TextType.Chapter) { var min = new SCNVector3 (); var max = new SCNVector3 (); textNode.GetBoundingBox (ref min, ref max); textNode.Position = new SCNVector3 (-11, (-min.Y + textNode.Pivot.M42) * TEXT_SCALE, 7); textNode.Scale = new SCNVector3 (TEXT_SCALE, TEXT_SCALE, TEXT_SCALE); textNode.Rotation = new SCNVector4 (0, 1, 0, (float)(Math.PI / 270.0)); } else { textNode.Position = new SCNVector3 (-16, CurrentBaseline, 0); textNode.Scale = new SCNVector3 (TEXT_SCALE, TEXT_SCALE, TEXT_SCALE); } // Material if (type == TextType.Chapter) { var frontMaterial = SCNMaterial.Create (); var sideMaterial = SCNMaterial.Create (); frontMaterial.Emission.Contents = NSColor.DarkGray; frontMaterial.Diffuse.Contents = ColorForTextType (type, level); sideMaterial.Diffuse.Contents = NSColor.LightGray; textNode.Geometry.Materials = new SCNMaterial[] { frontMaterial, frontMaterial, sideMaterial, frontMaterial, frontMaterial }; } else { // Full white emissive material (visible even when there is no light) textNode.Geometry.FirstMaterial = SCNMaterial.Create (); textNode.Geometry.FirstMaterial.Diffuse.Contents = NSColor.Black; textNode.Geometry.FirstMaterial.Emission.Contents = ColorForTextType (type, level); // Don't write to the depth buffer because we don't want the text to be reflected textNode.Geometry.FirstMaterial.WritesToDepthBuffer = false; // Render last textNode.RenderingOrder = 1; } return textNode; }