float GetBaselineInWorldSpace(FontMetricsHolder fmh) { FontMetricsHolder.Metrics metrics; float fontScale; if (UniformStyle) { fontScale = CurrentFontSize; metrics = fmh.GlyphRunMetrics[0]; } else { // Baseline is decided from whichever is the larger font selection. if (GetGlyphSize(CurrentUppercaseFontSize, fmh.GlyphRunMetrics[0]) > GetGlyphSize(CurrentLowercaseFontSize, fmh.GlyphRunMetrics[fmh.GlyphRunMetrics.Count - 1])) { fontScale = CurrentUppercaseFontSize; metrics = fmh.GlyphRunMetrics[0]; } else { fontScale = CurrentLowercaseFontSize; metrics = fmh.GlyphRunMetrics[fmh.GlyphRunMetrics.Count - 1]; } } return((float)textLayout.LayoutBounds.Top + (fontScale * (metrics.Ascent + metrics.LineGap))); }
private void Canvas_Draw(CanvasControl sender, CanvasDrawEventArgs args) { args.DrawingSession.Transform = System.Numerics.Matrix3x2.CreateTranslation(0, 5); EnsureResources(sender, sender.Size); FontMetricsHolder fmh = new FontMetricsHolder(args.DrawingSession); textLayout.DrawToTextRenderer(fmh, new System.Numerics.Vector2(0, 0)); args.DrawingSession.DrawTextLayout(textLayout, 0, 0, Colors.White); if (ShowGlyphRunBounds) { foreach (var metrics in fmh.GlyphRunMetrics) { args.DrawingSession.DrawRectangle(metrics.Bounds, Colors.Cyan); } } float baselineInWorldSpace = (float)textLayout.LayoutBounds.Top + textLayout.LineMetrics[0].Baseline; Labeler.DrawBaseline(args.DrawingSession, sender.Size.ToVector2(), baselineInWorldSpace); if (UniformStyle) { Labeler l = new Labeler(CurrentFontSize, sizeDim, sender.Size.ToVector2(), textLayout.LayoutBounds, baselineInWorldSpace, fmh.GlyphRunMetrics[0], args.DrawingSession); l.DrawAscent(); l.DrawLineGap(); l.DrawDescent(); l.DrawCapitalHeight(); l.DrawLowercaseHeight(); } else { Labeler l1 = new Labeler(CurrentUppercaseFontSize, sizeDim, sender.Size.ToVector2(), textLayout.LayoutBounds, baselineInWorldSpace, fmh.GlyphRunMetrics[0], args.DrawingSession); l1.DrawLineGap(); l1.DrawCapitalHeight(); l1.DrawAscent(); Labeler l2 = new Labeler(CurrentLowercaseFontSize, sizeDim, sender.Size.ToVector2(), textLayout.LayoutBounds, baselineInWorldSpace, fmh.GlyphRunMetrics[fmh.GlyphRunMetrics.Count - 1], args.DrawingSession); l2.DrawLineGap(); l2.DrawLowercaseHeight(); l2.DrawDescent(); } }
public Labeler(float f, float sd, Vector2 l, Rect tlb, float b, FontMetricsHolder.Metrics m, CanvasDrawingSession ds) { fontSize = f; sizeDim = sd; layoutSize = l; textLayoutBounds = tlb; baselineInWorldSpace = b; glyphRunMetrics = m; drawingSession = ds; horizontalMidpoint = (layoutSize.X / 2); leftJustifiedTextFormat = new CanvasTextFormat() { VerticalAlignment = CanvasVerticalAlignment.Top, HorizontalAlignment = CanvasHorizontalAlignment.Left }; rightJustifiedTextFormat = new CanvasTextFormat() { VerticalAlignment = CanvasVerticalAlignment.Top, HorizontalAlignment = CanvasHorizontalAlignment.Right }; NextLabel(); }
float GetGlyphSize(float fontSize, FontMetricsHolder.Metrics metrics) { // // This isn't a universally reliable way of determining baseline height // within a layout, but it's fine for this demo- since we've got no inline objects, // horizontal text and no custom line spacing. // return fontSize * (metrics.Ascent + metrics.LineGap); }
float GetBaselineInWorldSpace(FontMetricsHolder fmh) { FontMetricsHolder.Metrics metrics; float fontScale; if (UniformStyle) { fontScale = CurrentFontSize; metrics = fmh.GlyphRunMetrics[0]; } else { // Baseline is decided from whichever is the larger font selection. if (GetGlyphSize(CurrentUppercaseFontSize, fmh.GlyphRunMetrics[0]) > GetGlyphSize(CurrentLowercaseFontSize, fmh.GlyphRunMetrics[fmh.GlyphRunMetrics.Count - 1])) { fontScale = CurrentUppercaseFontSize; metrics = fmh.GlyphRunMetrics[0]; } else { fontScale = CurrentLowercaseFontSize; metrics = fmh.GlyphRunMetrics[fmh.GlyphRunMetrics.Count - 1]; } } return (float)textLayout.LayoutBounds.Top + (fontScale * (metrics.Ascent + metrics.LineGap)); }