Skip to content

Commit

Permalink
Do not set the baselineOrigin of ShapedTextRuns to (0,0) and rework t…
Browse files Browse the repository at this point in the history
…he BaselineOffset calculation (#16545)

Co-authored-by: Jumar Macato <16554748+jmacato@users.noreply.github.com>
  • Loading branch information
Gillibald and jmacato authored Aug 1, 2024
1 parent a562c4e commit e2ec814
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
3 changes: 1 addition & 2 deletions src/Avalonia.Base/Media/TextFormatting/ShapedTextRun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,7 @@ internal GlyphRun CreateGlyphRun()
ShapedBuffer.FontRenderingEmSize,
Text,
ShapedBuffer,
biDiLevel: BidiLevel,
baselineOrigin: new Point());
biDiLevel: BidiLevel);
}

public void Dispose()
Expand Down
23 changes: 16 additions & 7 deletions src/Avalonia.Base/Media/TextFormatting/TextLineImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public override void Draw(DrawingContext drawingContext, Point lineOrigin)
{
case DrawableTextRun drawableTextRun:
{
var offsetY = GetBaselineOffset(this, drawableTextRun);
var offsetY = GetBaselineOffset(drawableTextRun);

drawableTextRun.Draw(drawingContext, new Point(currentX, currentY + offsetY));

Expand All @@ -108,29 +108,38 @@ public override void Draw(DrawingContext drawingContext, Point lineOrigin)
}
}

private static double GetBaselineOffset(TextLine textLine, DrawableTextRun textRun)
private double GetBaselineOffset(DrawableTextRun textRun)
{
var baseline = textRun.Baseline;
var baselineAlignment = textRun.Properties?.BaselineAlignment;

var baselineOffset = -baseline;

switch (baselineAlignment)
{
case BaselineAlignment.Baseline:
return textLine.Baseline;
baselineOffset += Baseline;
break;
case BaselineAlignment.Top:
case BaselineAlignment.TextTop:
return textLine.Baseline - textLine.Extent + textRun.Size.Height / 2;
baselineOffset += Height - Extent + textRun.Size.Height / 2;
break;
case BaselineAlignment.Center:
return textLine.Height / 2 + baseline - textRun.Size.Height / 2;
baselineOffset += Height / 2 + baseline - textRun.Size.Height / 2;
break;
case BaselineAlignment.Subscript:
case BaselineAlignment.Bottom:
case BaselineAlignment.TextBottom:
return textLine.Height - textRun.Size.Height + baseline;
baselineOffset += Height - textRun.Size.Height + baseline;
break;
case BaselineAlignment.Superscript:
return baseline;
baselineOffset += baseline;
break;
default:
throw new ArgumentOutOfRangeException(nameof(baselineAlignment), baselineAlignment, null);
}

return baselineOffset;
}

/// <inheritdoc/>
Expand Down

0 comments on commit e2ec814

Please sign in to comment.