tidy up code in subsetting classes #98

removes some debugging code in the cmap replacer and moves the main glyph parsing logic in the glyph table subsetter to a method.

in the next commit we will delete the cmap replacer since it doesn't work and is no longer needed but we want a clean version of it in the commit history for reference.
This commit is contained in:
Eliot Jones
2020-01-02 17:05:11 +00:00
parent 1dd46bace2
commit f2f33dc5cf
3 changed files with 50 additions and 65 deletions

View File

@@ -45,7 +45,6 @@
using (var stream = new MemoryStream()) using (var stream = new MemoryStream())
{ {
// Write the file header details and read the number of tables out. // Write the file header details and read the number of tables out.
CopyThroughBufferPreserveData(stream, buffer, fontBytes, SizeOfFraction + (SizeOfShort * 4)); CopyThroughBufferPreserveData(stream, buffer, fontBytes, SizeOfFraction + (SizeOfShort * 4));
@@ -86,7 +85,7 @@
CopyThroughBufferDiscardData(stream, buffer, fontBytes, gapFromPrevious); CopyThroughBufferDiscardData(stream, buffer, fontBytes, gapFromPrevious);
} }
if (inputHeader.Value.IsTable(CMapTag) || inputHeader.Value.IsTable(TrueTypeHeaderTable.Os2)) if (inputHeader.Value.IsTable(CMapTag))
{ {
// Skip the CMap table for now, move it to the end in the output so we can resize it dynamically. // Skip the CMap table for now, move it to the end in the output so we can resize it dynamically.
inputOffset = location.Offset + location.Length; inputOffset = location.Offset + location.Length;
@@ -112,8 +111,6 @@
inputOffset = fontBytes.CurrentOffset; inputOffset = fontBytes.CurrentOffset;
} }
inputTableHeaders.Remove("OS/2");
// Create a new cmap table here. // Create a new cmap table here.
var table = GenerateWindowsSymbolTable(fontProgram, newEncoding); var table = GenerateWindowsSymbolTable(fontProgram, newEncoding);
var cmapLocation = inputTableHeaders[CMapTag]; var cmapLocation = inputTableHeaders[CMapTag];
@@ -159,19 +156,6 @@
result = stream.ToArray(); result = stream.ToArray();
} }
// num tables
result[4] = 0;
result[5] = 16;
// search range
result[6] = 1;
result[7] = 0;
// entry selector
result[8] = 0;
result[9] = 4;
// range shift
result[10] = 0;
result[11] = 0;
var inputBytes = new ByteArrayInputBytes(result); var inputBytes = new ByteArrayInputBytes(result);
// Overwrite checksum values per table. // Overwrite checksum values per table.
@@ -201,8 +185,6 @@
// TODO: take andada regular with no modifications but removing the os/2 table and validate. // TODO: take andada regular with no modifications but removing the os/2 table and validate.
var canParse = new TrueTypeFontParser().Parse(new TrueTypeDataBytes(new ByteArrayInputBytes(result))); var canParse = new TrueTypeFontParser().Parse(new TrueTypeDataBytes(new ByteArrayInputBytes(result)));
File.WriteAllBytes(@"C:\temp\no-os2-2.ttf", result);
return result; return result;
} }
@@ -264,11 +246,6 @@
throw new InvalidOperationException($"Failed to read {size} bytes starting at offset {input.CurrentOffset - read}."); throw new InvalidOperationException($"Failed to read {size} bytes starting at offset {input.CurrentOffset - read}.");
} }
if (buffer[0] == 'O' && buffer[1] == 'S')
{
return;
}
destination.Write(buffer, 0, read); destination.Write(buffer, 0, read);
} }

View File

@@ -13,6 +13,52 @@
{ {
var data = new TrueTypeDataBytes(fontBytes); var data = new TrueTypeDataBytes(fontBytes);
var existingGlyphs = GetGlyphRecordsInFont(font, data);
var newGlyphRecords = new GlyphRecord[mapping.Length];
var newGlyphTableLength = 0;
for (var i = 0; i < mapping.Length; i++)
{
var map = mapping[i];
var record = existingGlyphs[map.OldIndex];
newGlyphRecords[i] = record;
newGlyphTableLength += record.DataLength;
}
var newIndexToLoca = new uint[newGlyphRecords.Length + 1];
var outputIndex = 0u;
var output = new byte[newGlyphTableLength];
for (var i = 0; i < newGlyphRecords.Length; i++)
{
var newRecord = newGlyphRecords[i];
if (newRecord.Type == GlyphType.Composite)
{
throw new NotSupportedException("TODO");
}
newIndexToLoca[i] = outputIndex;
if (newRecord.Type == GlyphType.Empty)
{
continue;
}
data.Seek(newRecord.Offset);
for (var j = 0; j < newRecord.DataLength; j++)
{
output[outputIndex++] = data.ReadByte();
}
}
newIndexToLoca[newIndexToLoca.Length - 1] = (uint)output.Length;
return new NewGlyphTable(output, newIndexToLoca);
}
private static GlyphRecord[] GetGlyphRecordsInFont(TrueTypeFontProgram font, TrueTypeDataBytes data)
{
var indexToLocationTable = font.TableRegister.IndexToLocationTable; var indexToLocationTable = font.TableRegister.IndexToLocationTable;
var numGlyphs = indexToLocationTable.GlyphOffsets.Length - 1; var numGlyphs = indexToLocationTable.GlyphOffsets.Length - 1;
@@ -62,46 +108,7 @@
} }
} }
var newGlyphRecords = new GlyphRecord[mapping.Length]; return glyphRecords;
var newGlyphTableLength = 0;
for (var i = 0; i < mapping.Length; i++)
{
var map = mapping[i];
var record = glyphRecords[map.OldIndex];
newGlyphRecords[i] = record;
newGlyphTableLength += record.DataLength;
}
var newIndexToLoca = new uint[newGlyphRecords.Length + 1];
var outputIndex = 0u;
var output = new byte[newGlyphTableLength];
for (var i = 0; i < newGlyphRecords.Length; i++)
{
var newRecord = newGlyphRecords[i];
if (newRecord.Type == GlyphType.Composite)
{
throw new NotSupportedException("TODO");
}
newIndexToLoca[i] = outputIndex;
if (newRecord.Type == GlyphType.Empty)
{
continue;
}
data.Seek(newRecord.Offset);
for (var j = 0; j < newRecord.DataLength; j++)
{
output[outputIndex++] = data.ReadByte();
}
}
newIndexToLoca[newIndexToLoca.Length - 1] = (uint)output.Length;
return new NewGlyphTable(output, newIndexToLoca);
} }
private static void ReadSimpleGlyph(TrueTypeDataBytes data, int numberOfContours) private static void ReadSimpleGlyph(TrueTypeDataBytes data, int numberOfContours)

View File

@@ -7,6 +7,7 @@
using Core; using Core;
using Filters; using Filters;
using Geometry; using Geometry;
using IO;
using Logging; using Logging;
using Tokens; using Tokens;
using PdfPig.Fonts; using PdfPig.Fonts;