public static List <PFontGlyph> GetGlyphs(string charlist, string ttfName, int width, int height, float scaleRatio) { StreamWriter sw = new StreamWriter(File.Create("ch.tbl"), System.Text.Encoding.Unicode); List <PFontGlyph> glyphs = new List <PFontGlyph>(); int base_id = 0x8080; string currentChar = ""; Bitmap glyphBitmap; PFontGlyph pfontGlyph = null; for (int i = 0; i < charlist.Length; i++) { currentChar = charlist[i].ToString(); bool hasGlyph = DrawBitmap(currentChar, width, height, ttfName, scaleRatio, out glyphBitmap); if (hasGlyph == false) { DrawBitmapFallback(currentChar, width, height, scaleRatio, out glyphBitmap); } int left = 0; int right = 0; if ((base_id + i) % 0x100 == 0) { base_id += 0x80; } int char_id = base_id + i; CheckPoints(glyphBitmap, out left, out right); //如果是逗号和句号,强制设定字符宽度为18 if (currentChar.Equals(",") || currentChar.Equals("。")) { left = 1; right = (int)((float)height * 0.75f); } pfontGlyph = new PFontGlyph(i, base_id, left, right, glyphBitmap); glyphs.Add(pfontGlyph); sw.Write(string.Format("{0:x4}={1}\r\n", char_id, charlist[i].ToString())); } sw.Close(); return(glyphs); }
public void buildCompress(List <PFontGlyph> pglyphs, FontInfo fInfo, out byte[] dst) { dst = null; int wndLen = 0; int decLen = 0; int compressedsize = 0; TextureEncoder encoder = new TextureEncoder(); using (MemoryStream ms = new MemoryStream()) { using (BinaryWriter bw = new BinaryWriter(ms)) { bw.Write(new byte[0x20]); bw.Write(fInfo.dictData); int seek_ori = (int)bw.BaseStream.Position; List <int> ptr = new List <int>(); //bw.Write(new byte[(pglyphs.Count + 1) * 4]); MemoryStream mem = new MemoryStream(); for (int i = pglyphs.Count - 1; i >= 0; i--) { PFontGlyph glyph = pglyphs[i]; byte[] input = encoder.create4Bpp(glyph.bitmap, fInfo.t_width, fInfo.t_height, encoder.getPS2PaletteData(fInfo.palette_data), EndianType.LITTLE); mem.Write(input, 0, input.Length); decLen = (int)mem.Position; } mem.Position = 0; int[,] Dictionary = Compression.ReadDict(fInfo.dictData); int DictPart = Compression.FindDictPart(Dictionary); MemoryStream FONT_COMPRESS = new MemoryStream(); bool boolean = true; while (boolean) { if (mem.Position == mem.Length) { boolean = false; } else { int s4 = mem.ReadByte(); int i = 1; while (Dictionary[i, 1] != s4) { i++; if (Dictionary[i - 1, 1] == 0) { if ((s4 >> 4) > ((s4 << 4) >> 4)) { s4 = s4 - (1 << 4); } else { s4 = s4 - 1; } i = 1; } } int v0 = i; while (v0 != 0) { v0 = Compression.FindDictIndex(v0, Dictionary, DictPart, ref FONT_COMPRESS); } } } int GlyphSize = 0; MemoryStream FONT_COMPRESS_BUFFER = new MemoryStream(); do { int i = 0; string str = ""; while ((i < 8) & (FONT_COMPRESS.Position != 0)) { FONT_COMPRESS.Position--; str = Convert.ToString(FONT_COMPRESS.ReadByte()) + str; FONT_COMPRESS.Position--; i++; } str = str.PadLeft(8, '0'); FONT_COMPRESS_BUFFER.WriteByte(Convert.ToByte(str, 2)); GlyphSize++; } while (FONT_COMPRESS.Position != 0); FONT_COMPRESS_BUFFER.WriteByte(0); GlyphSize++; byte[] positions; wndLen = Compression.WriteGlyphPosition(FONT_COMPRESS_BUFFER, Dictionary, fInfo, out positions); byte[] comp = FONT_COMPRESS_BUFFER.ToArray(); compressedsize = comp.Length; bw.Write(positions); bw.Write(comp); bw.Seek(0, SeekOrigin.Begin); bw.Write((Int32)0x20); bw.Write((Int32)fInfo.dictData.Length); bw.Write((Int32)compressedsize); bw.Write((Int32)wndLen); bw.Write((Int32)fInfo.t_length); bw.Write((Int32)positions.Length / 4); bw.Write((Int32)positions.Length); bw.Write((Int32)decLen); } dst = ms.ToArray(); } }