Skip to content

Commit

Permalink
Represent characters with Unicode codepoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Jjagg committed Jun 9, 2018
1 parent c78b1c9 commit 4c058ec
Show file tree
Hide file tree
Showing 16 changed files with 123 additions and 111 deletions.
4 changes: 2 additions & 2 deletions src/SixLabors.Fonts/FileFontInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public FileFontInstance(string path)

public int LineHeight => this.font.Value.LineHeight;

public GlyphInstance GetGlyph(char character)
=> this.font.Value.GetGlyph(character);
public GlyphInstance GetGlyph(int codePoint)
=> this.font.Value.GetGlyph(codePoint);

public Vector2 GetOffset(GlyphInstance glyph, GlyphInstance previousGlyph)
=> this.font.Value.GetOffset(glyph, previousGlyph);
Expand Down
6 changes: 3 additions & 3 deletions src/SixLabors.Fonts/Font.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ public Font(Font prototype, float size)
/// <summary>
/// Gets the glyph.
/// </summary>
/// <param name="character">The character.</param>
/// <param name="codePoint">The code point of the character.</param>
/// <returns>Returns the glyph</returns>
internal Glyph GetGlyph(char character)
internal Glyph GetGlyph(int codePoint)
{
return new Glyph(this.instance.Value.GetGlyph(character), this.Size);
return new Glyph(this.instance.Value.GetGlyph(codePoint), this.Size);
}

private IFontInstance LoadInstanceInternal()
Expand Down
16 changes: 11 additions & 5 deletions src/SixLabors.Fonts/FontInstance.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.

using System;
using System.IO;
using System.Numerics;
using SixLabors.Fonts.Tables.General;
Expand Down Expand Up @@ -82,19 +83,24 @@ internal FontInstance(NameTable nameTable, CMapTable cmap, GlyphTable glyphs, OS

public FontDescription Description { get; }

internal ushort GetGlyphIndex(char character)
internal ushort GetGlyphIndex(int codePoint)
{
return this.cmap.GetGlyphId(character);
if (codePoint > short.MaxValue)
{
throw new NotImplementedException("cmap table doesn't support 32-bit characters yet.");
}

return this.cmap.GetGlyphId(codePoint);
}

/// <summary>
/// Gets the glyph.
/// </summary>
/// <param name="character">The character.</param>
/// <param name="codePoint">The code point of the character.</param>
/// <returns>the glyph for a known character.</returns>
public GlyphInstance GetGlyph(char character)
public GlyphInstance GetGlyph(int codePoint)
{
ushort idx = this.GetGlyphIndex(character);
ushort idx = this.GetGlyphIndex(codePoint);
if (this.glyphCache[idx] == null)
{
ushort advanceWidth = this.horizontalMetrics.GetAdvancedWidth(idx);
Expand Down
18 changes: 12 additions & 6 deletions src/SixLabors.Fonts/GlyphMetric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,26 @@ namespace SixLabors.Fonts
/// <summary>
/// Initializes a new instance of the <see cref="GlyphMetric"/> struct.
/// </summary>
/// <param name="character">The character.</param>
/// <param name="codePoint">Unicode codepoint of the character.</param>
/// <param name="bounds">The bounds.</param>
/// <param name="isControlCharacter">Whether the character is a control character.</param>
public GlyphMetric(char character, RectangleF bounds, bool isControlCharacter)
public GlyphMetric(int codePoint, RectangleF bounds, bool isControlCharacter)
{
this.Character = character;
this.Codepoint = codePoint;
this.Character = char.ConvertFromUtf32(codePoint);
this.Bounds = bounds;
this.IsControlCharacter = isControlCharacter;
}

/// <summary>
/// Gets the character.
/// Gets the Unicode codepoint of the character.
/// </summary>
public char Character { get; }
public int Codepoint { get; }

/// <summary>
/// Gets the UTF-16 encoded character.
/// </summary>
public string Character { get; }

/// <summary>
/// Gets the character bounds.
Expand All @@ -42,7 +48,7 @@ public GlyphMetric(char character, RectangleF bounds, bool isControlCharacter)
/// <inheritdoc/>
public override string ToString()
{
return $"Char: {this.Character}, bounds: {this.Bounds}, is control char: {this.IsControlCharacter}";
return $"Character: {this.Character}, bounds: {this.Bounds}, is control char: {this.IsControlCharacter}";
}
}
}
2 changes: 1 addition & 1 deletion src/SixLabors.Fonts/IFontInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal interface IFontInstance

short LineGap { get; }

GlyphInstance GetGlyph(char character);
GlyphInstance GetGlyph(int codePoint);

Vector2 GetOffset(GlyphInstance glyph, GlyphInstance previousGlyph);
}
Expand Down
2 changes: 1 addition & 1 deletion src/SixLabors.Fonts/Tables/General/CMap/CMapSubTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ public CMapSubTable(PlatformIDs platform, ushort encoding, ushort format)

public ushort Encoding { get; }

public abstract ushort GetGlyphId(char character);
public abstract ushort GetGlyphId(int codePoint);
}
}
4 changes: 2 additions & 2 deletions src/SixLabors.Fonts/Tables/General/CMap/Format0SubTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public Format0SubTable(ushort language, PlatformIDs platform, ushort encoding, b

public ushort Language { get; }

public override ushort GetGlyphId(char character)
public override ushort GetGlyphId(int codePoint)
{
uint b = character;
uint b = (uint)codePoint;
if (b >= this.GlyphIds.Length)
{
return 0;
Expand Down
4 changes: 2 additions & 2 deletions src/SixLabors.Fonts/Tables/General/CMap/Format4SubTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public Format4SubTable(ushort language, PlatformIDs platform, ushort encoding, S

public ushort Language { get; }

public override ushort GetGlyphId(char character)
public override ushort GetGlyphId(int codePoint)
{
uint charAsInt = character;
uint charAsInt = (uint)codePoint;

foreach (Segment seg in this.Segments)
{
Expand Down
6 changes: 3 additions & 3 deletions src/SixLabors.Fonts/Tables/General/CMapTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ public CMapTable(CMapSubTable[] tables)
this.table = table;
}

public ushort GetGlyphId(char character)
public ushort GetGlyphId(int codePoint)
{
// use the best match only
if (this.table != null)
{
return this.table.GetGlyphId(character);
return this.table.GetGlyphId(codePoint);
}

// didn't have a windows match just use any and hope for the best
foreach (CMapSubTable t in this.Tables)
{
// keep looking until we have an index thats not the fallback.
ushort index = t.GetGlyphId(character);
ushort index = t.GetGlyphId(codePoint);
if (index > 0)
{
return index;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public ushort GetAdvancedWidth(int glyphIndex)
return this.advancedWidths[glyphIndex];
}

internal short GetLeftSideBearing(ushort glyphIndex)
internal short GetLeftSideBearing(int glyphIndex)
{
if (glyphIndex >= this.leftSideBearings.Length)
{
Expand Down

0 comments on commit 4c058ec

Please sign in to comment.