private bool LoadGlyphDataCached() { string glyphPath = Path.Combine(Program.cachePath, GetCacheFilename() + ".bin"); if (!File.Exists(glyphPath)) { return(false); } using (Stream stream = File.Open(glyphPath, FileMode.Open)) { using (BinaryReader reader = new BinaryReader(stream)) { int fileVersion = reader.ReadInt32(); if (fileVersion != version) { return(false); } int length = reader.ReadInt32(); glyphs = new FontGlyph[length]; for (int i = 0; i < glyphs.Length; ++i) { glyphs[i].BinaryRead(reader); } } } return(true); }
private byte[] GetGlyphData(IntPtr surfacePtr, FontGlyph glyph, out int width, out int height) { SDL.SDL_Surface surface = Marshal.PtrToStructure <SDL.SDL_Surface>(surfacePtr); SDL.SDL_PixelFormat surfaceFormat = Marshal.PtrToStructure <SDL.SDL_PixelFormat>(surface.format); IntPtr surfaceData = surface.pixels; int surfacePitch = surface.pitch; int ascent = GetAscent(); int descent = GetDescent(); byte[] data = new byte[glyph.width * glyph.height * surfaceFormat.BytesPerPixel]; unsafe { byte *src = (byte *)surfaceData; int rowBytes = glyph.width * surfaceFormat.BytesPerPixel; int offsetX = glyph.minX; int offsetY = -glyph.minY + (ascent - glyph.height); // bug in SDL_ttf?: // some glyphs have parts rendered outside the surface area if (offsetY < 0) { offsetY = 0; } if (offsetX < 0) { offsetX = 0; } src += offsetY * surfacePitch; src += offsetX * surfaceFormat.BytesPerPixel; for (int i = 0; i < glyph.height; i++) { Marshal.Copy((IntPtr)src, data, i * rowBytes, rowBytes); src += surfacePitch; } } width = glyph.width; height = glyph.height; return(data); }
private void GenerateGlyphData(ushort characterCount) { Log.Info("Font: Generating glyph data for {0} characters", characterCount); glyphs = new FontGlyph[characterCount]; for (ushort i = 0; i < characterCount; ++i) { if (SDL_ttf.TTF_GlyphIsProvided(handle, i) == 0) { continue; } if (SDL_ttf.TTF_GlyphMetrics(handle, i, out glyphs[i].minX, out glyphs[i].maxX, out glyphs[i].minY, out glyphs[i].maxY, out glyphs[i].advance) != 0) { throw new ApplicationException("Failed to get glyph metrics for glyph " + i + " : " + SDL.SDL_GetError()); } } }