/// <summary> /// 读取旧字库图片 /// </summary> private void LoadOldFontBmp() { if (this.isComText) { Bitmap bio1ComFont1 = new Bitmap(@"..\EncodeFenxi\TextEditTools\Bio1Edit\bio1ComFont1.bmp"); Bitmap bio1ComFont2 = new Bitmap(@"..\EncodeFenxi\TextEditTools\Bio1Edit\bio1ComFont2.bmp"); this.fontBmp = new Bitmap(bio1ComFont1.Width, bio1ComFont1.Height + bio1ComFont2.Height); for (int y = 0; y < bio1ComFont1.Height; y += 1) { for (int x = 0; x < bio1ComFont1.Width; x += 1) { this.fontBmp.SetPixel(x, y, bio1ComFont1.GetPixel(x, y)); } } for (int y = 0; y < bio1ComFont2.Height; y += 1) { for (int x = 0; x < bio1ComFont2.Width; x += 1) { this.fontBmp.SetPixel(x, y + bio1ComFont1.Height, bio1ComFont2.GetPixel(x, y)); } } } else { this.fontBmp = new Bitmap(@"..\\EncodeFenxi\TextEditTools\Bio1Edit\bio1FileFont.bmp"); } this.fontCharInfoLst.Clear(); FileStream fs = null; byte[] byData; try { if (this.isWii) { fs = new FileStream(this.baseFolder + @"\WiiJp\sys\main.dol", FileMode.Open); fs.Seek(0x2ee900, SeekOrigin.Begin); byData = new byte[0x2ef8d0 - 0x2ee900]; } else { fs = new FileStream(this.baseFolder + @"\&&systemdata\Start.dol", FileMode.Open); fs.Seek(0x17d9d0, SeekOrigin.Begin); byData = new byte[0x17e9a0 - 0x17d9d0]; } fs.Read(byData, 0, byData.Length); fs.Close(); for (int i = 0; i < byData.Length; i += 2) { FontCharInfo charInfo = new FontCharInfo(); charInfo.LeftPadding = byData[i]; charInfo.Width = byData[i + 1]; this.fontCharInfoLst.Add(charInfo); } } catch (Exception me) { MessageBox.Show(me.Message + "\n" + me.StackTrace); } finally { if (fs != null) { fs.Close(); } } }
/// <summary> /// 根据单个字符生成字库图片 /// </summary> /// <param name="fontChar"></param> /// <returns></returns> private Bitmap CreateCharBitMap(string fontChar) { Bitmap bmp = new Bitmap(32, 32); Graphics g = Graphics.FromImage(bmp); g.Clear(Color.FromArgb(0, 0, 0, 0)); g.SmoothingMode = SmoothingMode.HighQuality; g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; Pen blackPen = new Pen(Color.Black, 0.01F); if (this.useOldFont.Contains(fontChar)) { // 取得原有的字库 int fontW = 32; int fontH = 32; int charFontIndex = 0; string[] diffData = Bio1TextEditor.GetDiffData(fontChar, this.isComText).Split(' '); int index = Convert.ToInt32(diffData[0]); // 根据旧字符宽度信息,设置新字符宽度信息 if (this.isComText) { this.newFontCharInfoLst.Add(this.fontCharInfoLst[index]); } else { this.newFontCharInfoLst.Add(this.fontCharInfoLst[index + 32 * 34 + 2]); } // 取得旧字库图片 for (int y = 0; y < fontBmp.Height; y += fontH) { Image[] rowImage = new Image[this.fontBmp.Width / fontW]; for (int x = 0; x < this.fontBmp.Width; x += fontW) { if (charFontIndex == index) { for (int yPixel = 0; yPixel < fontH; yPixel++) { for (int xPixel = 0; xPixel < fontW; xPixel++) { bmp.SetPixel(xPixel, yPixel, this.fontBmp.GetPixel(x + xPixel, y + yPixel)); } } return(bmp); } charFontIndex++; } } } else { FontCharInfo charInfo = null; if (string.IsNullOrEmpty(fontChar)) { // 更新进度条 this.ProcessBarStep(); return(bmp); } else if (" " == fontChar) { charInfo = new FontCharInfo(); charInfo.LeftPadding = 32; charInfo.Width = 0; this.newFontCharInfoLst.Add(charInfo); // 更新进度条 this.ProcessBarStep(); return(bmp); } // 单个子对齐风格 StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Near; CharacterRange[] characterRanges = { new CharacterRange(0, 1) }; sf.SetMeasurableCharacterRanges(characterRanges); // 在指定的区域内写入特定汉字 RectangleF rectangle = new RectangleF(0, 0, 32, 30); GraphicsPath graphPath = new GraphicsPath(); graphPath.AddString(fontChar, new FontFamily(txtFontTest.Font.Name), (int)FontStyle.Regular, txtFontTest.Font.Size, rectangle, sf); g.FillPath(Brushes.White, graphPath); g.DrawPath(blackPen, graphPath); Region[] charRegions = g.MeasureCharacterRanges(fontChar, this.selectedFont, rectangle, sf); int charWidth = (int)charRegions[0].GetBounds(g).Width; int charLeftWidth = 32 + 1 - charWidth; if (charLeftWidth <= 0) { charLeftWidth = 0; } else if (charLeftWidth % 2 != 0) { charLeftWidth = charLeftWidth / 2 + 1; } else { charLeftWidth = charLeftWidth / 2; } charInfo = new FontCharInfo(); charInfo.LeftPadding = charLeftWidth; charInfo.Width = charLeftWidth + charWidth; this.newFontCharInfoLst.Add(charInfo); } // 更新进度条 this.ProcessBarStep(); return(bmp); }
/// <summary> /// 保存字库 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSave_Click(object sender, EventArgs e) { FileStream fs = null; try { Bitmap fntBmp = new Bitmap(1024, 1024); // 显示进度条 this.ResetProcessBar(this.grdFont.Rows.Count); // 生成字库图片文件 this.CreateImgFontFile(0, 32, fntBmp, this.fontImgName + ".png"); if (this.grdFont.Rows.Count > 32) { fntBmp = new Bitmap(1024, 128); // 生成字库图片文件 this.CreateImgFontFile(32, 36, fntBmp, this.fontImgName + "2.png"); } // 写入字符宽度信息 int charInfoStart = 0; string fontWidInfoFile = string.Empty; if (this.isWii) { charInfoStart = 0x2ee900; fontWidInfoFile = this.baseFolder + @"\WiiCn\sys\main.dol"; } else { charInfoStart = 0x17d9d0; fontWidInfoFile = this.baseFolder + @"\&&systemdata\Start.dol"; } int charInfoLen = 32 * 34 + 2; if (!this.isComText) { charInfoStart += (32 * 34 + 2) * 2; charInfoLen = 32 * 29 + 6; } byte[] byCharWidInfo = new byte[charInfoLen * 2]; for (int i = 0; i < byCharWidInfo.Length; i += 2) { if (i / 2 < this.newFontCharInfoLst.Count) { FontCharInfo charInfo = this.newFontCharInfoLst[i / 2]; byCharWidInfo[i] = (byte)charInfo.LeftPadding; byCharWidInfo[i + 1] = (byte)charInfo.Width; } } fs = new FileStream(fontWidInfoFile, FileMode.Open); fs.Seek(charInfoStart, SeekOrigin.Begin); fs.Write(byCharWidInfo, 0, byCharWidInfo.Length); fs.Close(); } catch (Exception me) { MessageBox.Show(me.Message + "\n" + me.StackTrace); } finally { // 隐藏进度条 this.CloseProcessBar(); if (fs != null) { fs.Close(); } } }