Skip to content

Commit

Permalink
Don't upscale already scaled ppem
Browse files Browse the repository at this point in the history
  • Loading branch information
JimBobSquarePants committed Mar 26, 2022
1 parent 6c3d00b commit 7b3ec08
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
22 changes: 8 additions & 14 deletions src/SixLabors.Fonts/GlyphMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,15 +239,15 @@ internal void RenderTo(IGlyphRenderer surface, float pointSize, Vector2 location
{
float dpi = options.Dpi;
location *= dpi;
float scaledPoint = dpi * pointSize;
float scaledPPEM = dpi * pointSize;
bool forcePPEMToInt = (this.FontMetrics.HeadFlags & HeadTable.HeadFlags.ForcePPEMToInt) != 0;

if (forcePPEMToInt)
{
scaledPoint = MathF.Round(scaledPoint);
scaledPPEM = MathF.Round(scaledPPEM);
}

FontRectangle box = this.GetBoundingBox(location, scaledPoint);
FontRectangle box = this.GetBoundingBox(location, scaledPPEM);

// TextRun is never null here as rendering is only accessable via a Glyph which
// uses the cloned metrics instance.
Expand All @@ -262,26 +262,20 @@ internal void RenderTo(IGlyphRenderer surface, float pointSize, Vector2 location
colorSurface.SetColor(this.GlyphColor.Value);
}

if (!this.scaledVector.TryGetValue(scaledPoint, out GlyphVector scaledVector))
if (!this.scaledVector.TryGetValue(scaledPPEM, out GlyphVector scaledVector))
{
// Scale and translate the glyph
Vector2 scale = new Vector2(scaledPoint) / this.ScaleFactor;
Vector2 scale = new Vector2(scaledPPEM) / this.ScaleFactor;
var transform = Matrix3x2.CreateScale(scale);
transform.Translation = this.offset * scale * MirrorScale;
scaledVector = GlyphVector.Transform(this.vector, transform);

if (options.ApplyHinting)
{
float scaledPixel = scaledPoint * (dpi / 72F);
if (forcePPEMToInt)
{
scaledPixel = MathF.Round(scaledPixel);
}

this.FontMetrics.ApplyHinting(this, ref scaledVector, scale, scaledPixel);
this.FontMetrics.ApplyHinting(this, ref scaledVector, scale, scaledPPEM);
}

this.scaledVector[scaledPoint] = scaledVector;
this.scaledVector[scaledPPEM] = scaledVector;
}

GlyphOutline outline = scaledVector.GetOutline();
Expand Down Expand Up @@ -360,7 +354,7 @@ internal void RenderTo(IGlyphRenderer surface, float pointSize, Vector2 location

(Vector2 Start, Vector2 End, float Thickness) GetEnds(float thickness, float position)
{
Vector2 scale = new Vector2(scaledPoint) / this.ScaleFactor * MirrorScale;
Vector2 scale = new Vector2(scaledPPEM) / this.ScaleFactor * MirrorScale;
Vector2 offset = location + (this.offset * scale * MirrorScale);

// Calculate the correct advance for the line.
Expand Down
6 changes: 3 additions & 3 deletions src/SixLabors.Fonts/StreamFontMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ public static StreamFontMetrics[] LoadFontCollection(Stream stream)
return fonts;
}

internal void ApplyHinting(GlyphMetrics metrics, ref GlyphVector glyphVector, Vector2 scaleXY, float scaledPixel)
internal void ApplyHinting(GlyphMetrics metrics, ref GlyphVector glyphVector, Vector2 scaleXY, float scaledPPEM)
{
if (this.interpreter == null)
{
Expand All @@ -495,8 +495,8 @@ internal void ApplyHinting(GlyphMetrics metrics, ref GlyphVector glyphVector, Ve
}
}

float scaleFactor = scaledPixel / this.UnitsPerEm;
this.interpreter.SetControlValueTable(this.cvt?.ControlValues, scaleFactor, scaledPixel, this.prep?.Instructions);
float scaleFactor = scaledPPEM / this.UnitsPerEm;
this.interpreter.SetControlValueTable(this.cvt?.ControlValues, scaleFactor, scaledPPEM, this.prep?.Instructions);

Bounds bounds = glyphVector.GetBounds();

Expand Down
2 changes: 2 additions & 0 deletions src/SixLabors.Fonts/Tables/General/Glyphs/GlyphVector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ public static void Hint(ref GlyphVector glyph, Interpreter interpreter, Vector2
for (int i = 0; i < glyph.entries.Count; i++)
{
GlyphTableEntry entry = glyph.entries[i];

// TODO: Figure out a way to pool this.
var controlPoints = new Vector2[entry.ControlPoints.Length + 4];
controlPoints[controlPoints.Length - 4] = pp1;
controlPoints[controlPoints.Length - 3] = pp2;
Expand Down

0 comments on commit 7b3ec08

Please sign in to comment.