private byte[] BuildOS2(TableDirectoryEntry oldOS2) { MemoryStream outStream = new MemoryStream(); IOUtils.WriteShort(outStream, Os2version); short xAvgCharWidth = CalculateAverageWeight(); IOUtils.WriteShort(outStream,xAvgCharWidth); IOUtils.WriteShort(outStream,Weight); IOUtils.WriteShort(outStream, Width); IOUtils.WriteShort(outStream, FsType); IOUtils.WriteShort(outStream, SubscriptXSize); IOUtils.WriteShort(outStream, SubscriptYSize); IOUtils.WriteShort(outStream, SubscriptXOffset); IOUtils.WriteShort(outStream, SubscriptYOffset); IOUtils.WriteShort(outStream, SuperscriptXSize); IOUtils.WriteShort(outStream, SuperscriptYSize); IOUtils.WriteShort(outStream, SuperscriptXOffset); IOUtils.WriteShort(outStream, SuperscriptYOffset); IOUtils.WriteShort(outStream, StrikeoutSize); IOUtils.WriteShort(outStream, StrikeoutPosition); IOUtils.WriteShort(outStream, FamilyClass); outStream.Write(panos,0,10); if (Os2version == 0) { IOUtils.WriteInt(outStream, CharRange); } else { // TODO: calculate and adjust the ranges that really present in the font according to version 4 of the table IOUtils.WriteInt(outStream, UnicodeRange1); IOUtils.WriteInt(outStream, UnicodeRange2); IOUtils.WriteInt(outStream, UnicodeRange3); IOUtils.WriteInt(outStream, UnicodeRange4); } Encoding isoEncoding = Encoding.GetEncoding("iso-8859-1"); outStream.Write(isoEncoding.GetBytes(achVendID,0,4),0,4); IOUtils.WriteShort(outStream, fsSelection); int firstCharIndex = GetNewFirstIndex(); IOUtils.WriteShort(outStream,firstCharIndex); int lastCharIndex = GetNewLastIndex(); IOUtils.WriteShort(outStream,lastCharIndex); IOUtils.WriteShort(outStream, TypoAscender); IOUtils.WriteShort(outStream, TypoDescender); IOUtils.WriteShort(outStream, TypoLineGap); IOUtils.WriteShort(outStream, WinAscent); IOUtils.WriteShort(outStream, WinDescent); if (Os2version == 0) { return outStream.ToArray(); } // TODO: recalculate codepages in case version 0 or wrong IOUtils.WriteInt(outStream, CodePageRange1); IOUtils.WriteInt(outStream, CodePageRange2); if (Os2version == 1) { return outStream.ToArray(); } // TODO: add calculations in case default not set IOUtils.WriteShort(outStream, Height); IOUtils.WriteShort(outStream, CapHeight); IOUtils.WriteShort(outStream, DefaultChar); IOUtils.WriteShort(outStream, BreakChar); IOUtils.WriteShort(outStream, MaxContext); return outStream.ToArray(); }
private byte[] BuildMaxP(TableDirectoryEntry maxp) { byte[] buf = new byte[maxp.Length]; // expect at least 6 byte long inputStream.Seek(maxp.Offset, SeekOrigin.Begin); inputStream.Read(buf, 0, maxp.Length); buf[4] = (byte)(NewGlyphCount >> 8); buf[5] = (byte)(NewGlyphCount); return buf; }
private byte[] BuildHead(TableDirectoryEntry head, bool shortOffsets) { byte[] buf = new byte[head.Length]; // expect 54 bytes long inputStream.Seek(head.Offset, SeekOrigin.Begin); inputStream.Read(buf, 0, head.Length); buf[8] = 0; // zero out checkSumAdjustment buf[9] = 0; buf[10] = 0; buf[11] = 0; IOUtils.StuffLong(buf, 28, DateTime.Now.Ticks / 1000 + baseTime); buf[50] = 0; buf[51] = (byte)(shortOffsets ? 0 : 1); return buf; }
private byte[] BuildHHea(TableDirectoryEntry hhea) { byte[] buf = new byte[hhea.Length]; // expect 36 bytes long inputStream.Seek(hhea.Offset, SeekOrigin.Begin); inputStream.Read(buf, 0, hhea.Length); advanceWidthMax = CalculateAdvanceWidthMax(); buf[10] = (byte)(advanceWidthMax>>8); buf[11] = (byte) (advanceWidthMax); buf[34] = (byte)(NewVariableWidthCount >> 8); buf[35] = (byte)(NewVariableWidthCount); return buf; }
public void Load(Stream stream) { byte[] buffer = new byte[256]; int readResult = stream.Read(buffer, 0, 12); if ( readResult != 12) { throw new Exception(string.Format("could not read {0} bytes",12)); } int version = IOUtils.GetInt(buffer, 0); switch (version) { case 0x74746366: // TrueType collection file // we can only read the first font in the collection stream.Read(buffer, 0, 4); stream.Seek(IOUtils.GetInt(buffer, 0), SeekOrigin.Begin); stream.Read(buffer, 0, 12); version = IOUtils.GetInt(buffer, 0); break; case 0x00010000: // regular TrueType break; case 0x4f54544f: // CFF based break; default: throw new Exception("Invalid OpenType file"); } Version = version; NumTables = IOUtils.GetShort(buffer, 4); SearchRange = IOUtils.GetShort(buffer, 6); EntrySelector = IOUtils.GetShort(buffer, 8); RangeShift = IOUtils.GetShort(buffer, 10); tableDirectory.Clear(); for (int i = 0; i < NumTables; i++) { TableDirectoryEntry entry = new TableDirectoryEntry(); stream.Read(buffer, 0, 16); char[] arr = { (char)(buffer[0] & 0xFF), (char)(buffer[1] & 0xFF), (char)(buffer[2] & 0xFF), (char)(buffer[3] & 0xFF) }; int len = buffer[0] == 0 ? 0 : (buffer[1] == 0 ? 1 : (buffer[2] == 0 ? 2 : (buffer[3] == 0 ? 3 : 4))); entry.Identifier = new string(arr,0,len); entry.CheckSum = IOUtils.GetInt(buffer, 4); entry.Offset = IOUtils.GetInt(buffer, 8); entry.Length = IOUtils.GetInt(buffer, 12); tableDirectory.Add(entry); tableMap.Add(entry.Identifier,entry); } }