/// <summary> /// Function to write the font data to the stream. /// </summary> /// <param name="fontData">The font data to write.</param> /// <param name="stream">The stream to write into.</param> /// <remarks> /// <para> /// Implementors must override this method to write out the font data in the expected format. /// </para> /// </remarks> protected override void OnWriteFontData(GorgonFont fontData, Stream stream) { var fontFile = new GorgonChunkFileWriter(stream, FileHeader.ChunkID()); IGorgonFontInfo fontInfo = fontData.Info; try { fontFile.Open(); GorgonBinaryWriter writer = fontFile.OpenChunk(FontInfoChunk); writer.Write(fontInfo.FontFamilyName); writer.Write(fontInfo.Size); writer.WriteValue(fontInfo.FontHeightMode); writer.WriteValue(fontInfo.FontStyle); writer.Write(fontInfo.DefaultCharacter); writer.Write(string.Join(string.Empty, fontInfo.Characters)); writer.WriteValue(fontInfo.AntiAliasingMode); writer.Write(fontInfo.OutlineColor1.ToARGB()); writer.Write(fontInfo.OutlineColor2.ToARGB()); writer.Write(fontInfo.OutlineSize); writer.Write(fontInfo.PackingSpacing); writer.Write(fontInfo.TextureWidth); writer.Write(fontInfo.TextureHeight); writer.Write(fontInfo.UsePremultipliedTextures); writer.Write(fontInfo.UseKerningPairs); fontFile.CloseChunk(); writer = fontFile.OpenChunk(FontHeightChunk); writer.Write(fontData.FontHeight); writer.Write(fontData.LineHeight); writer.Write(fontData.Ascent); writer.Write(fontData.Descent); fontFile.CloseChunk(); if (fontInfo.Brush != null) { writer = fontFile.OpenChunk(BrushChunk); writer.Write((int)fontInfo.Brush.BrushType); fontInfo.Brush.WriteBrushData(writer); fontFile.CloseChunk(); } if (fontInfo.UseKerningPairs) { WriteKerningValues(fontData, fontFile); } } finally { fontFile.Close(); } }
public void ChunkReaderWriter() { const string stringChunk = "STRSCHNK"; const string intChunk = "INTSCHNK"; const string header = "TheHeader"; string[] strs = { "Cow", "Dog", "Cat", "Rabbit", "Duck" }; int[] ints = { 32, 11, 89, 64, 87, 77, 16, 2, 42 }; int expectedStrLength = strs.Length; int expectedIntsLength = ints.Length; using (MemoryStream stream = new MemoryStream()) { var fileWriter = new GorgonChunkFileWriter(stream, header.ChunkID()); fileWriter.Open(); GorgonBinaryWriter writer = fileWriter.OpenChunk(stringChunk.ChunkID()); writer.Write(strs.Length); foreach (string str in strs) { writer.Write(str); } fileWriter.CloseChunk(); writer = fileWriter.OpenChunk(intChunk.ChunkID()); writer.Write(ints.Length); foreach (int intVal in ints) { writer.Write(intVal); } fileWriter.CloseChunk(); fileWriter.Close(); stream.Position = 0; var fileReader = new GorgonChunkFileReader(stream, new[] { header.ChunkID() }); fileReader.Open(); GorgonBinaryReader reader = fileReader.OpenChunk(intChunk.ChunkID()); int numInts = reader.ReadInt32(); Assert.AreEqual(expectedIntsLength, numInts); int[] intVals = new int[numInts]; for (int i = 0; i < numInts; ++i) { intVals[i] = reader.ReadInt32(); } Assert.IsTrue(ints.SequenceEqual(intVals)); fileReader.CloseChunk(); reader = fileReader.OpenChunk(stringChunk.ChunkID()); int numStrs = reader.ReadInt32(); Assert.AreEqual(expectedStrLength, numStrs); string[] strVals = new string[numStrs]; for (int i = 0; i < numStrs; ++i) { strVals[i] = reader.ReadString(); } Assert.IsTrue(strs.SequenceEqual(strVals)); fileReader.CloseChunk(); fileReader.Close(); } }