public static SpriteFont Compile(GraphicsDevice g, string file, out IDisposable gR) { FontDescriptionImporter ei = new FontDescriptionImporter(); FontDescription ec = ei.Import(file, new XNADynImporterContext()); FontDescriptionProcessor ep = new FontDescriptionProcessor(); var cec = ep.Process(ec, new XNADynProcessorContext()); // Get Private Texture Texture2DContent texC = sfcTexture.GetValue(cec) as Texture2DContent; MipmapChain o = txcMipmaps.GetValue(texC, null) as MipmapChain; BitmapContent texBMP = o[0]; SurfaceFormat sf; if (!texBMP.TryGetFormat(out sf)) { throw new InvalidContentException("Could Not Obtain The Surface Format Of The SpriteFont"); } Texture2D texture = new Texture2D(g, texBMP.Width, texBMP.Height, false, sf); texture.SetData(texBMP.GetPixelData()); // Get Private Glyph Data List <Rectangle> glyphs = sfcGlyphs.GetValue(cec) as List <Rectangle>; List <Rectangle> cropping = sfcCropping.GetValue(cec) as List <Rectangle>; List <char> charMap = sfcCharMap.GetValue(cec) as List <char>; int lineSpacing = (int)sfcLineSpacing.GetValue(cec); float spacing = (float)sfcSpacing.GetValue(cec); List <Vector3> kerning = sfcKerning.GetValue(cec) as List <Vector3>; char? defaultChar = sfcDefaultChar.GetValue(cec) as char?; // Invoke Private SpriteFont Constructor gR = texture; return(sfConstructor.Invoke(new object[] { texture, glyphs, cropping, charMap, lineSpacing, spacing, kerning, defaultChar }) as SpriteFont); }
unsafe private static BitmapContent ConvertToColor(BitmapContent input) { var width = input.Width; var height = input.Height; SurfaceFormat format; input.TryGetFormat(out format); var formatSize = DDSImporter.GetBitmapSize(format, width, height); var blocks = formatSize; var inData = input.GetPixelData(); var output = new PixelBitmapContent <Color>(width, height); fixed(byte *p = &inData[0]) { DXT1Block *block = (DXT1Block *)p; //convert DXT1 to color for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { int blockIdx = (x / 4) + (y / 4) * (width / 4); int colorIndex = x % 4 + (y % 4) * 4; Color color = block[blockIdx].GetColor(colorIndex); output.SetPixel(x, y, color); } } } return(output); }
public override SpriteFontContent Process(Texture2DContent input, ContentProcessorContext context) { var output = new SpriteFontContent(); // extract the glyphs from the texture and map them to a list of characters. // we need to call GtCharacterForIndex for each glyph in the Texture to // get the char for that glyph, by default we start at ' ' then '!' and then ASCII // after that. BitmapContent face = input.Faces[0][0]; SurfaceFormat faceFormat; face.TryGetFormat(out faceFormat); if (faceFormat != SurfaceFormat.Color) { var colorFace = new PixelBitmapContent <Color>(face.Width, face.Height); BitmapContent.Copy(face, colorFace); face = colorFace; } var glyphs = ExtractGlyphs((PixelBitmapContent <Color>)face); // Optimize. foreach (var glyph in glyphs) { GlyphCropper.Crop(glyph); output.VerticalLineSpacing = Math.Max(output.VerticalLineSpacing, glyph.Subrect.Height); } var format = GraphicsUtil.GetTextureFormatForPlatform(TextureFormat, context.TargetPlatform); var requiresPOT = GraphicsUtil.RequiresPowerOfTwo(format, context.TargetPlatform, context.TargetProfile); var requiresSquare = GraphicsUtil.RequiresSquare(format, context.TargetPlatform); face = GlyphPacker.ArrangeGlyphs(glyphs.ToArray(), requiresPOT, requiresSquare); foreach (var glyph in glyphs) { output.CharacterMap.Add(glyph.Character); output.Glyphs.Add(new Rectangle(glyph.Subrect.X, glyph.Subrect.Y, glyph.Subrect.Width, glyph.Subrect.Height)); output.Cropping.Add(new Rectangle((int)glyph.XOffset, (int)glyph.YOffset, glyph.Width, glyph.Height)); var abc = glyph.CharacterWidths; output.Kerning.Add(new Vector3(abc.A, abc.B, abc.C)); } output.Texture.Faces[0].Add(face); var bmp = output.Texture.Faces[0][0]; if (PremultiplyAlpha) { var data = bmp.GetPixelData(); var idx = 0; for (; idx < data.Length;) { var r = data[idx + 0]; var g = data[idx + 1]; var b = data[idx + 2]; var a = data[idx + 3]; var col = Color.FromNonPremultiplied(r, g, b, a); data[idx + 0] = col.R; data[idx + 1] = col.G; data[idx + 2] = col.B; data[idx + 3] = col.A; idx += 4; } bmp.SetPixelData(data); } if (GraphicsUtil.IsCompressedTextureFormat(format)) { try { GraphicsUtil.CompressTexture(context.TargetProfile, output.Texture, format, context, false, true); } catch (Exception ex) { context.Logger.LogImportantMessage("{0}", ex.ToString()); } } return(output); }
internal static void WriteUncompressed(string filename, BitmapContent bitmapContent) { using (var writer = new BinaryWriter(new FileStream(filename, FileMode.Create, FileAccess.Write))) { // Write signature ("DDS ") writer.Write((byte)0x44); writer.Write((byte)0x44); writer.Write((byte)0x53); writer.Write((byte)0x20); var header = new DdsHeader(); header.dwSize = 124; header.dwFlags = Ddsd.Caps | Ddsd.Width | Ddsd.Height | Ddsd.Pitch | Ddsd.PixelFormat; header.dwWidth = (uint)bitmapContent.Width; header.dwHeight = (uint)bitmapContent.Height; header.dwPitchOrLinearSize = (uint)(bitmapContent.Width * 4); header.dwDepth = (uint)0; header.dwMipMapCount = (uint)0; writer.Write((uint)header.dwSize); writer.Write((uint)header.dwFlags); writer.Write((uint)header.dwHeight); writer.Write((uint)header.dwWidth); writer.Write((uint)header.dwPitchOrLinearSize); writer.Write((uint)header.dwDepth); writer.Write((uint)header.dwMipMapCount); // 11 unsed and reserved DWORDS. writer.Write((uint)0); writer.Write((uint)0); writer.Write((uint)0); writer.Write((uint)0); writer.Write((uint)0); writer.Write((uint)0); writer.Write((uint)0); writer.Write((uint)0); writer.Write((uint)0); writer.Write((uint)0); writer.Write((uint)0); SurfaceFormat format; if (!bitmapContent.TryGetFormat(out format) || format != SurfaceFormat.Color) { throw new NotSupportedException("Unsupported bitmap content!"); } header.ddspf.dwSize = 32; header.ddspf.dwFlags = Ddpf.AlphaPixels | Ddpf.Rgb; header.ddspf.dwFourCC = 0; header.ddspf.dwRgbBitCount = 32; header.ddspf.dwRBitMask = 0x000000ff; header.ddspf.dwGBitMask = 0x0000ff00; header.ddspf.dwBBitMask = 0x00ff0000; header.ddspf.dwABitMask = 0xff000000; // Write the DDS_PIXELFORMAT writer.Write((uint)header.ddspf.dwSize); writer.Write((uint)header.ddspf.dwFlags); writer.Write((uint)header.ddspf.dwFourCC); writer.Write((uint)header.ddspf.dwRgbBitCount); writer.Write((uint)header.ddspf.dwRBitMask); writer.Write((uint)header.ddspf.dwGBitMask); writer.Write((uint)header.ddspf.dwBBitMask); writer.Write((uint)header.ddspf.dwABitMask); header.dwCaps = DdsCaps.Texture; header.dwCaps2 = 0; // Continue reading DDS_HEADER writer.Write((uint)header.dwCaps); writer.Write((uint)header.dwCaps2); // More reserved unused DWORDs. writer.Write((uint)0); writer.Write((uint)0); writer.Write((uint)0); // Write out the face data. writer.Write(bitmapContent.GetPixelData()); } }
public override SpriteFontContent Process(Texture2DContent input, ContentProcessorContext context) { var output = new SpriteFontContent(); // extract the glyphs from the texture and map them to a list of characters. // we need to call GtCharacterForIndex for each glyph in the Texture to // get the char for that glyph, by default we start at ' ' then '!' and then ASCII // after that. BitmapContent face = input.Faces[0][0]; SurfaceFormat faceFormat; face.TryGetFormat(out faceFormat); if (faceFormat != SurfaceFormat.Color) { var colorFace = new PixelBitmapContent <Color>(face.Width, face.Height); BitmapContent.Copy(face, colorFace); face = colorFace; } var glyphs = ExtractGlyphs((PixelBitmapContent <Color>)face); // Optimize. foreach (var glyph in glyphs) { GlyphCropper.Crop(glyph); output.VerticalLineSpacing = Math.Max(output.VerticalLineSpacing, glyph.Subrect.Height); } // Get the platform specific texture profile. var texProfile = TextureProfile.ForPlatform(context.TargetPlatform); // We need to know how to pack the glyphs. bool requiresPot, requiresSquare; texProfile.Requirements(context, TextureFormat, out requiresPot, out requiresSquare); face = GlyphPacker.ArrangeGlyphs(glyphs.ToArray(), requiresPot, requiresSquare); foreach (var glyph in glyphs) { output.CharacterMap.Add(glyph.Character); output.Glyphs.Add(new Rectangle(glyph.Subrect.X, glyph.Subrect.Y, glyph.Subrect.Width, glyph.Subrect.Height)); output.Cropping.Add(new Rectangle((int)glyph.XOffset, (int)glyph.YOffset, glyph.Width, glyph.Height)); var abc = glyph.CharacterWidths; output.Kerning.Add(new Vector3(abc.A, abc.B, abc.C)); } output.Texture.Faces[0].Add(face); var bmp = output.Texture.Faces[0][0]; if (PremultiplyAlpha) { var data = bmp.GetPixelData(); var idx = 0; for (; idx < data.Length;) { var r = data[idx + 0]; var g = data[idx + 1]; var b = data[idx + 2]; var a = data[idx + 3]; var col = Color.FromNonPremultiplied(r, g, b, a); data[idx + 0] = col.R; data[idx + 1] = col.G; data[idx + 2] = col.B; data[idx + 3] = col.A; idx += 4; } bmp.SetPixelData(data); } // Perform the final texture conversion. texProfile.ConvertTexture(context, output.Texture, TextureFormat, true); return(output); }