public void Draw(RenderContext renderContext, float opacity, Color color) { if (renderContext.gl == null) { Vector3d viewPoint = Vector3d.TransformCoordinate(renderContext.ViewPoint, ViewTransform); double drawHeight = (Height / renderContext.FovAngle) * renderContext.Height / 180; foreach (Text3d t3d in Items) { Vector3d screenSpacePnt = renderContext.WVP.Transform(t3d.center); if (screenSpacePnt.Z < 0) { continue; } if (Vector3d.Dot((Vector3d)viewPoint, (Vector3d)t3d.center) < .55) { continue; } Vector3d screenSpaceTop = renderContext.WVP.Transform(t3d.top); double rotation = Math.Atan2(screenSpacePnt.X - screenSpaceTop.X, screenSpacePnt.Y - screenSpaceTop.Y); CanvasContext2D ctx = renderContext.Device; ctx.Save(); ctx.Translate(screenSpacePnt.X, screenSpacePnt.Y); ctx.Rotate(-rotation); // todo update with up vector ctx.Alpha = opacity; ctx.FillStyle = color.ToString(); ctx.Font = "normal" + " " + (false ? "bold" : "normal") + " " + Math.Round(drawHeight * 1.2).ToString() + "px " + "Arial"; ctx.TextBaseline = TextBaseline.Top; TextMetrics tm = ctx.MeasureText(t3d.Text); ctx.FillText(t3d.Text, -tm.Width / 2, -drawHeight / 2); ctx.Restore(); } } else { // gl version if (glyphCache == null || glyphCache.Version > glyphVersion) { PrepareBatch(); } if (glyphCache.Ready == false) { return; } TextShader.Use(renderContext, vertexBuffer.VertexBuffer, glyphCache.Texture.Texture2d); renderContext.gl.drawArrays(GL.TRIANGLES, 0, vertexBuffer.Count); } }
public static List <string> GetWrappedText(CanvasContext2D ctx, string text, double width) { List <string> lines = new List <string>(); string[] words = text.Split(" "); string currentLine = ""; for (int i = 0; i < words.Length; i++) { if (!string.IsNullOrEmpty(words[i])) { if (currentLine == "" || ctx.MeasureText(currentLine + " " + words[i]).Width < width) { currentLine += " " + words[i]; } else { lines.Add(currentLine); currentLine = words[i]; } } } if (currentLine != "") { lines.Add(currentLine); } return(lines); }