diff --git a/src/UglyToad.PdfPig/Writer/Fonts/TrueTypeCMapReplacer.cs b/src/UglyToad.PdfPig/Writer/Fonts/TrueTypeCMapReplacer.cs index 49584f0f..8d6033dc 100644 --- a/src/UglyToad.PdfPig/Writer/Fonts/TrueTypeCMapReplacer.cs +++ b/src/UglyToad.PdfPig/Writer/Fonts/TrueTypeCMapReplacer.cs @@ -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); } diff --git a/src/UglyToad.PdfPig/Writer/Fonts/TrueTypeGlyphTableSubsetter.cs b/src/UglyToad.PdfPig/Writer/Fonts/TrueTypeGlyphTableSubsetter.cs index 8408d4f8..2e1a5420 100644 --- a/src/UglyToad.PdfPig/Writer/Fonts/TrueTypeGlyphTableSubsetter.cs +++ b/src/UglyToad.PdfPig/Writer/Fonts/TrueTypeGlyphTableSubsetter.cs @@ -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) diff --git a/src/UglyToad.PdfPig/Writer/Fonts/TrueTypeWritingFont.cs b/src/UglyToad.PdfPig/Writer/Fonts/TrueTypeWritingFont.cs index 0f71024e..fddc9865 100644 --- a/src/UglyToad.PdfPig/Writer/Fonts/TrueTypeWritingFont.cs +++ b/src/UglyToad.PdfPig/Writer/Fonts/TrueTypeWritingFont.cs @@ -7,6 +7,7 @@ using Core; using Filters; using Geometry; + using IO; using Logging; using Tokens; using PdfPig.Fonts;