mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-09-19 10:47:56 +08:00
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:
@@ -45,7 +45,6 @@
|
||||
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
|
||||
// Write the file header details and read the number of tables out.
|
||||
CopyThroughBufferPreserveData(stream, buffer, fontBytes, SizeOfFraction + (SizeOfShort * 4));
|
||||
|
||||
@@ -86,7 +85,7 @@
|
||||
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.
|
||||
inputOffset = location.Offset + location.Length;
|
||||
@@ -111,9 +110,7 @@
|
||||
|
||||
inputOffset = fontBytes.CurrentOffset;
|
||||
}
|
||||
|
||||
inputTableHeaders.Remove("OS/2");
|
||||
|
||||
|
||||
// Create a new cmap table here.
|
||||
var table = GenerateWindowsSymbolTable(fontProgram, newEncoding);
|
||||
var cmapLocation = inputTableHeaders[CMapTag];
|
||||
@@ -159,19 +156,6 @@
|
||||
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);
|
||||
|
||||
// Overwrite checksum values per table.
|
||||
@@ -201,8 +185,6 @@
|
||||
// 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)));
|
||||
|
||||
File.WriteAllBytes(@"C:\temp\no-os2-2.ttf", result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -264,11 +246,6 @@
|
||||
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);
|
||||
}
|
||||
|
||||
|
@@ -13,6 +13,52 @@
|
||||
{
|
||||
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 numGlyphs = indexToLocationTable.GlyphOffsets.Length - 1;
|
||||
@@ -62,46 +108,7 @@
|
||||
}
|
||||
}
|
||||
|
||||
var newGlyphRecords = new GlyphRecord[mapping.Length];
|
||||
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);
|
||||
return glyphRecords;
|
||||
}
|
||||
|
||||
private static void ReadSimpleGlyph(TrueTypeDataBytes data, int numberOfContours)
|
||||
|
@@ -7,6 +7,7 @@
|
||||
using Core;
|
||||
using Filters;
|
||||
using Geometry;
|
||||
using IO;
|
||||
using Logging;
|
||||
using Tokens;
|
||||
using PdfPig.Fonts;
|
||||
|
Reference in New Issue
Block a user