mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-10-14 19:05:01 +08:00
implement glyph index getter for the 3 implemented truetype cmap sub tables
This commit is contained in:
@@ -6,29 +6,40 @@
|
||||
/// </summary>
|
||||
internal class ByteEncodingCMapTable : ICMapSubTable
|
||||
{
|
||||
private const int GlyphMappingLength = 256;
|
||||
private readonly byte[] glyphMapping;
|
||||
|
||||
public int PlatformId { get; }
|
||||
|
||||
public int EncodingId { get; }
|
||||
|
||||
private ByteEncodingCMapTable(int platformId, int encodingId)
|
||||
private ByteEncodingCMapTable(int platformId, int encodingId, byte[] glyphMapping)
|
||||
{
|
||||
this.glyphMapping = glyphMapping;
|
||||
PlatformId = platformId;
|
||||
EncodingId = encodingId;
|
||||
}
|
||||
|
||||
public static ByteEncodingCMapTable Load(TrueTypeDataBytes data, int platformId, int encodingId)
|
||||
{
|
||||
// ReSharper disable UnusedVariable
|
||||
var length = data.ReadUnsignedShort();
|
||||
var version = data.ReadUnsignedShort();
|
||||
// ReSharper restore UnusedVariable
|
||||
|
||||
var glyphMapping = data.ReadByteArray(256);
|
||||
var glyphMapping = data.ReadByteArray(GlyphMappingLength);
|
||||
|
||||
return new ByteEncodingCMapTable(platformId, encodingId);
|
||||
return new ByteEncodingCMapTable(platformId, encodingId, glyphMapping);
|
||||
}
|
||||
|
||||
public int CharacterCodeToGlyphIndex(int characterCode)
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
if (characterCode < GlyphMappingLength || characterCode >= GlyphMappingLength)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return glyphMapping[characterCode];
|
||||
}
|
||||
}
|
||||
}
|
@@ -34,7 +34,26 @@ namespace UglyToad.PdfPig.Fonts.TrueType.Tables.CMapSubTables
|
||||
|
||||
public int CharacterCodeToGlyphIndex(int characterCode)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
for (var i = 0; i < Segments.Count; i++)
|
||||
{
|
||||
var segment = Segments[i];
|
||||
|
||||
if (segment.EndCode < characterCode || segment.StartCode > characterCode)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (segment.IdRangeOffset == 0)
|
||||
{
|
||||
return (characterCode + segment.IdDelta) % ushort.MaxValue;
|
||||
}
|
||||
|
||||
var offset = segment.IdRangeOffset / 2 + (characterCode - segment.StartCode);
|
||||
|
||||
return GlyphIds[offset - Segments.Count + i];
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static Format4CMapTable Load(TrueTypeDataBytes data, int platformId, int encodingId)
|
||||
|
@@ -10,25 +10,35 @@
|
||||
/// </summary>
|
||||
internal class HighByteMappingCMapTable : ICMapSubTable
|
||||
{
|
||||
private readonly IReadOnlyDictionary<int, int> characterCodesToGlyphIndices;
|
||||
|
||||
public int PlatformId { get; }
|
||||
|
||||
public int EncodingId { get; }
|
||||
|
||||
public HighByteMappingCMapTable(int platformId, int encodingId)
|
||||
private HighByteMappingCMapTable(int platformId, int encodingId, IReadOnlyDictionary<int, int> characterCodesToGlyphIndices)
|
||||
{
|
||||
this.characterCodesToGlyphIndices = characterCodesToGlyphIndices ?? throw new ArgumentNullException(nameof(characterCodesToGlyphIndices));
|
||||
PlatformId = platformId;
|
||||
EncodingId = encodingId;
|
||||
}
|
||||
|
||||
public int CharacterCodeToGlyphIndex(int characterCode)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
if (!characterCodesToGlyphIndices.TryGetValue(characterCode, out var index))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
public static HighByteMappingCMapTable Load(TrueTypeDataBytes data, int numberOfGlyphs, int platformId, int encodingId)
|
||||
{
|
||||
// ReSharper disable UnusedVariable
|
||||
var length = data.ReadUnsignedShort();
|
||||
var version = data.ReadUnsignedShort();
|
||||
// ReSharper restore UnusedVariable
|
||||
|
||||
var subHeaderKeys = new int[256];
|
||||
var maximumSubHeaderIndex = 0;
|
||||
@@ -83,7 +93,7 @@
|
||||
}
|
||||
}
|
||||
|
||||
return new HighByteMappingCMapTable(platformId, encodingId);
|
||||
return new HighByteMappingCMapTable(platformId, encodingId, characterCodeToGlyphId);
|
||||
}
|
||||
|
||||
public struct SubHeader
|
||||
|
Reference in New Issue
Block a user