⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 字库生成工具源代码
💻 CS
📖 第 1 页 / 共 3 页
字号:
			this.cmbImgLarge.Location = new System.Drawing.Point(496, 16);
			this.cmbImgLarge.Name = "cmbImgLarge";
			this.cmbImgLarge.Size = new System.Drawing.Size(32, 20);
			this.cmbImgLarge.TabIndex = 13;
			this.cmbImgLarge.Text = "1";
			this.cmbImgLarge.SelectedIndexChanged += new System.EventHandler(this.cmbImgLarge_SelectedIndexChanged);
			// 
			// hScrollBar
			// 
			this.hScrollBar.Cursor = System.Windows.Forms.Cursors.Hand;
			this.hScrollBar.Location = new System.Drawing.Point(8, 136);
			this.hScrollBar.Name = "hScrollBar";
			this.hScrollBar.Size = new System.Drawing.Size(72, 16);
			this.hScrollBar.TabIndex = 12;
			this.hScrollBar.Visible = false;
			this.hScrollBar.ValueChanged += new System.EventHandler(this.hScrollBar_ValueChanged);
			// 
			// vScrollBar
			// 
			this.vScrollBar.AllowDrop = true;
			this.vScrollBar.Cursor = System.Windows.Forms.Cursors.Hand;
			this.vScrollBar.Location = new System.Drawing.Point(8, 56);
			this.vScrollBar.Name = "vScrollBar";
			this.vScrollBar.Size = new System.Drawing.Size(16, 72);
			this.vScrollBar.TabIndex = 11;
			this.vScrollBar.Visible = false;
			this.vScrollBar.ValueChanged += new System.EventHandler(this.vScrollBar_ValueChanged);
			// 
			// btnAdjust
			// 
			this.btnAdjust.Location = new System.Drawing.Point(128, 152);
			this.btnAdjust.Name = "btnAdjust";
			this.btnAdjust.Size = new System.Drawing.Size(56, 23);
			this.btnAdjust.TabIndex = 10;
			this.btnAdjust.Text = "调整";
			this.btnAdjust.Click += new System.EventHandler(this.btnAdjust_Click);
			// 
			// btnAdd
			// 
			this.btnAdd.Location = new System.Drawing.Point(648, 16);
			this.btnAdd.Name = "btnAdd";
			this.btnAdd.TabIndex = 2;
			this.btnAdd.Text = "添加";
			this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
			// 
			// txtFonts
			// 
			this.txtFonts.Location = new System.Drawing.Point(8, 14);
			this.txtFonts.Name = "txtFonts";
			this.txtFonts.Size = new System.Drawing.Size(632, 21);
			this.txtFonts.TabIndex = 1;
			this.txtFonts.Text = "ABC";
			// 
			// listFont
			// 
			this.listFont.HorizontalScrollbar = true;
			this.listFont.ItemHeight = 12;
			this.listFont.Location = new System.Drawing.Point(8, 48);
			this.listFont.Name = "listFont";
			this.listFont.SelectionMode = System.Windows.Forms.SelectionMode.MultiSimple;
			this.listFont.Size = new System.Drawing.Size(112, 280);
			this.listFont.Sorted = true;
			this.listFont.TabIndex = 3;
			// 
			// btnCreateXML
			// 
			this.btnCreateXML.Location = new System.Drawing.Point(624, 328);
			this.btnCreateXML.Name = "btnCreateXML";
			this.btnCreateXML.Size = new System.Drawing.Size(64, 24);
			this.btnCreateXML.TabIndex = 16;
			this.btnCreateXML.Text = "生成xml";
			this.btnCreateXML.Click += new System.EventHandler(this.btnCreate_Click);
			// 
			// btnFont
			// 
			this.btnFont.Location = new System.Drawing.Point(128, 72);
			this.btnFont.Name = "btnFont";
			this.btnFont.Size = new System.Drawing.Size(56, 23);
			this.btnFont.TabIndex = 8;
			this.btnFont.Text = "字库>>";
			this.btnFont.Click += new System.EventHandler(this.btnFont_Click);
			// 
			// btnDelete
			// 
			this.btnDelete.Location = new System.Drawing.Point(128, 112);
			this.btnDelete.Name = "btnDelete";
			this.btnDelete.Size = new System.Drawing.Size(56, 23);
			this.btnDelete.TabIndex = 9;
			this.btnDelete.Text = "删除";
			this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
			// 
			// btnAdjustSure
			// 
			this.btnAdjustSure.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.btnAdjustSure.Location = new System.Drawing.Point(128, 184);
			this.btnAdjustSure.Name = "btnAdjustSure";
			this.btnAdjustSure.Size = new System.Drawing.Size(56, 24);
			this.btnAdjustSure.TabIndex = 13;
			this.btnAdjustSure.Text = "确定";
			this.btnAdjustSure.Visible = false;
			this.btnAdjustSure.Click += new System.EventHandler(this.btnAdjustSure_Click);
			// 
			// btnInportXML
			// 
			this.btnInportXML.Location = new System.Drawing.Point(536, 328);
			this.btnInportXML.Name = "btnInportXML";
			this.btnInportXML.Size = new System.Drawing.Size(64, 24);
			this.btnInportXML.TabIndex = 19;
			this.btnInportXML.Text = "导入xml";
			this.btnInportXML.Click += new System.EventHandler(this.btnInportXML_Click);
			// 
			// btnCreateLattice
			// 
			this.btnCreateLattice.Location = new System.Drawing.Point(448, 328);
			this.btnCreateLattice.Name = "btnCreateLattice";
			this.btnCreateLattice.Size = new System.Drawing.Size(64, 24);
			this.btnCreateLattice.TabIndex = 20;
			this.btnCreateLattice.Text = "生成字库";
			this.btnCreateLattice.Click += new System.EventHandler(this.btnCreateLattice_Click);
			// 
			// btnInportXSL
			// 
			this.btnInportXSL.Location = new System.Drawing.Point(352, 328);
			this.btnInportXSL.Name = "btnInportXSL";
			this.btnInportXSL.Size = new System.Drawing.Size(64, 24);
			this.btnInportXSL.TabIndex = 21;
			this.btnInportXSL.Text = "导入xsl";
			this.btnInportXSL.Click += new System.EventHandler(this.btnInportXSL_Click);
			// 
			// FontMaker
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(736, 349);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.btnAdd);
			this.Controls.Add(this.txtFonts);
			this.Controls.Add(this.listFont);
			this.Controls.Add(this.btnCreateXML);
			this.Controls.Add(this.btnFont);
			this.Controls.Add(this.btnDelete);
			this.Controls.Add(this.btnAdjust);
			this.Controls.Add(this.btnAdjustSure);
			this.Controls.Add(this.btnInportXML);
			this.Controls.Add(this.btnCreateLattice);
			this.Controls.Add(this.btnInportXSL);
			this.Name = "FontMaker";
			this.Text = "FontMaker";
			this.groupBox2.ResumeLayout(false);
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new FontMaker());
		}
		
		
		private void GetMaxSize(XmlDocument doc,out int maxX,out int maxY)
		{
			maxX=maxY=0;
			foreach(XmlNode node in doc.DocumentElement.ChildNodes)
			{
				if(maxX<Convert.ToUInt16(node.Attributes.GetNamedItem("imgSizeW").Value))
					maxX=Convert.ToUInt16(node.Attributes.GetNamedItem("imgSizeW").Value);
				if(maxY<Convert.ToUInt16(node.Attributes.GetNamedItem("imgSizeH").Value))
					maxY=Convert.ToUInt16(node.Attributes.GetNamedItem("imgSizeH").Value);
			}
		}
		public void DrawDotString(string str,int x0,int y0,int lineWidth,int d,XmlDocument doc,System.Drawing.Graphics g)
		{
			XmlNode node;
			int x=x0;
			int y=y0;
			foreach(char ch in str.ToCharArray())
			{
				node=doc.DocumentElement.SelectSingleNode("Font[@unicode='"+String.Format("0x{0}", (Convert.ToUInt16(ch)).ToString("X4"))+"']");
				if(node!=null)
				{
					DrawDotOneFont(ch,x,y,d,node,g);
					x+=Convert.ToInt32(node.Attributes.GetNamedItem("imgSizeW").Value)*d;
					if(x>lineWidth)
					{
						y+=Convert.ToInt32(node.Attributes.GetNamedItem("imgSizeH").Value)*d+5;
						x=x0;
					}
				}
			}
		}
		public void DrawDotOneFont(char ch,int x0,int y0,int d,XmlNode node,System.Drawing.Graphics g)
		{
			if(d<=0 || d>5)d=1;
			ushort imgSizeW=Convert.ToUInt16(node.Attributes.GetNamedItem("imgSizeW").Value);
			ushort imgSizeH=Convert.ToUInt16(node.Attributes.GetNamedItem("imgSizeH").Value);
			ushort offsetX=Convert.ToUInt16(node.Attributes.GetNamedItem("offsetX").Value);
			ushort offsetY=Convert.ToUInt16(node.Attributes.GetNamedItem("offsetY").Value);
			ushort cellSizeW=Convert.ToUInt16(node.Attributes.GetNamedItem("cellSizeW").Value);
			ushort cellSizeH=Convert.ToUInt16(node.Attributes.GetNamedItem("cellSizeH").Value);
			//g.FillRectangle(new SolidBrush(Color.White),x0,y0,imgSizeW*d,imgSizeH*d);
			ushort len=0;
			int x,y;
			foreach(string str in node.FirstChild.InnerText.Split(",".ToCharArray()))
			{
				ushort v=Convert.ToUInt16(str.Substring(2,str.Length-2),16);
				for(ushort i=0;i<16 && (((int)(len/cellSizeH))*16+i)<cellSizeW;i++)
					if( (v&( 1<<(16-i-1)))!=0)
					{
						x=x0+(((int)(len/cellSizeH))*16+i)*d;
						y=y0+(len % cellSizeH)*d;
						g.FillRegion(new SolidBrush(Color.Black),new Region(new Rectangle(x,y,d,d)));
					}	
				len++;
			};
		}

		public Image CreateFontImag(System.Xml.XmlNode node,int d,int maxX,int maxY)
		{
            if(d<=0 || d>5)d=1;
			maxX=maxX*d;
			maxY=maxY*d;
			short imgSizeW=Convert.ToInt16(node.Attributes.GetNamedItem("imgSizeW").Value);
			short imgSizeH=Convert.ToInt16(node.Attributes.GetNamedItem("imgSizeH").Value);
			short offsetX=Convert.ToInt16(node.Attributes.GetNamedItem("offsetX").Value);
			short offsetY=Convert.ToInt16(node.Attributes.GetNamedItem("offsetY").Value);
			short cellSizeW=Convert.ToInt16(node.Attributes.GetNamedItem("cellSizeW").Value);
			short cellSizeH=Convert.ToInt16(node.Attributes.GetNamedItem("cellSizeH").Value);
			
			// add for imagelist size64*64
			maxX=64;maxY=64;
			if(imgSizeW*d>64 || imgSizeW*d>64)d=1;
			
			System.Drawing.Bitmap img=new Bitmap(maxX,maxY,System.Drawing.Imaging.PixelFormat.Format48bppRgb  );
			System.Drawing.Graphics g=System.Drawing.Graphics.FromImage(img);
			g.FillRectangle(SystemBrushes.ScrollBar ,0,0,maxX,maxY);
			if(imgSizeW*d>64 || imgSizeW*d>64)return img;
			g.FillRectangle(new SolidBrush(Color.White),maxX-imgSizeW*d,maxY-imgSizeH*d,imgSizeW*d,imgSizeH*d);
			ushort len=0;
			int x0=maxX-imgSizeW*d+offsetX*d;
			int y0=maxX-imgSizeH*d+offsetY*d;
			int x,y;
			foreach(string str in node.FirstChild.InnerText.Split(",".ToCharArray()))
			{
				ushort v=Convert.ToUInt16(str.Substring(2,str.Length-2),16);
				for(ushort i=0;i<16 && (((int)(len/cellSizeH))*16+i)<cellSizeW;i++)
					if( (v&( 1<<(16-i-1)))!=0)
					{
						x=x0+(((int)(len/cellSizeH))*16+i)*d;
						y=y0+(len % cellSizeH)*d;
						g.FillRegion(new SolidBrush(Color.Black),new Region(new Rectangle(x,y,d,d)));
					}	
				len++;
			};
			return img;
		}
		public void MoveFontImage(ref XmlNode node,int offsetW,int offsetH)
		{
			int width=System.Convert.ToInt32(node.Attributes.GetNamedItem("offsetX").Value)+offsetW;
			int height=System.Convert.ToInt32(node.Attributes.GetNamedItem("offsetX").Value)+offsetH;
			//if(width<0)width=0;
			//if(width>System.Convert.ToInt32(node.Attributes.GetNamedItem("imgSizeW").Value ))width=System.Convert.ToInt32(node.Attributes.GetNamedItem("imgSizeW"));
			//if(height<0)width=0;
			//if(height>System.Convert.ToInt32(node.Attributes.GetNamedItem("imgSizeH").Value ))height=System.Convert.ToInt32(node.Attributes.GetNamedItem("imgSizeH"));

			node.Attributes.GetNamedItem("offsetX").Value=System.Convert.ToString(width);
			node.Attributes.GetNamedItem("offsetY").Value=System.Convert.ToString(height);
		}
		public void SetFontImageSize(ref XmlNode node,int width,int height)
		{
			if(width<0)width=0;
			if(height<0)height=0;
			node.Attributes.GetNamedItem("imgSizeW").Value=System.Convert.ToString(width);
			node.Attributes.GetNamedItem("imgSizeH").Value=System.Convert.ToString(height);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="code">unicode</param>
		/// <param name="fontName"></param>
		/// <param name="fontSize"></param>
		/// <returns>xml about the font arry information</returns>
		public XmlNode CreateFontPixel(char code,string fontName,float fontSize,short imgSizeW,short imgSizeH)
		{
			

			ushort imgW=(ushort)imgSizeW;
			ushort imgH=(ushort)imgSizeH;
			ushort offsetX=0;
			ushort offsetY=0;
			Font drawFont =new Font(fontName,fontSize,GraphicsUnit.Pixel);
			if(0==imgH)imgH=(ushort)drawFont.Height; 
			if(0==imgW)imgW=(ushort)(imgH*2);
			System.Drawing.Bitmap img=new Bitmap(imgW,imgH,System.Drawing.Imaging.PixelFormat.Format48bppRgb  );
			System.Drawing.Graphics g=System.Drawing.Graphics.FromImage(img);
			g.FillRectangle(new SolidBrush(Color.White  ),0,0,imgW,imgH);
			g.DrawString(new string(code,1),drawFont,new SolidBrush(Color.Black),0,0);
			
			//get the xml information string
			ushort x1,x2;
			ushort y1,y2;
			x2=y2=0;
			x1=imgW;y1=imgH;
			ushort[] data=new ushort[imgH*((int)(imgW/16+1))] ;
			for(ushort i=0;i<imgH;i++)
			{
				for(ushort k=0;k<((int)(imgW/16+1));k++)
				{
					data[i+k*imgH]=0;
					for(ushort j=0;j<16 && (j+k*16)<imgW;j++)
						if(img.GetPixel(j+k*16,i).ToArgb()==Color.Black.ToArgb())
						{
							data[i+k*imgH]|=(ushort)(1<<(16-j-1));
							if(j+k*16>x2)x2=(ushort)(j+k*16);		
							if(j+k*16<x1)x1=(ushort)(j+k*16);
							if(i>y2)y2=i;
							if(i<y1)y1=i;
						}
				}
			}
			//create the string 
			string strValue="";
			ushort v;
			for(int i=x1/16;i<=x2/16-1;i++)
				for(int j=y1;j<=y2;j++)
				{
					v=((ushort)((data[j+i*imgH]<<x1)|(data[j+(i+1)*imgH]>>(16-x1))));
					strValue+=String.Format("0x{0},", v.ToString("X4"));
				}
			for(int j=y1;j<=y2;j++)
			{
				v=((ushort)(data[j+imgH*((ushort)(x2/16))]<<x1));
				strValue+=String.Format("0x{0},", v.ToString("X4"));
			}
			strValue=strValue.Substring(0,strValue.Length-1);

			if(0==imgSizeW)imgSizeW=(short)(x2-x1+1);
			else offsetX=x1;
			if(0==imgSizeH)imgSizeH=(short)(y2-y1+1);
			else offsetY=y1;

			//create the xml string 
			System.Xml.XmlDocument xmlFile=new XmlDocument();
			xmlFile.LoadXml("<?xml version=\"1.0\" ?><Fonts><Font unicode=\"\" fontName=\"\" fontSize=\"\" imgSizeW=\"\" imgSizeH=\"\" offsetX=\"\" offsetY=\"\" cellSizeW=\"\"  cellSizeH=\"\" >	<Lattice len=\"\" type=\"ushort\"></Lattice></Font></Fonts>");
			XmlNode node=xmlFile.DocumentElement.FirstChild ;
			node.Attributes.GetNamedItem("unicode").Value =String.Format("0x{0}", Convert.ToUInt16(code).ToString("X4"));
			node.Attributes.GetNamedItem("fontName").Value=fontName;
			node.Attributes.GetNamedItem("fontSize").Value=Convert.ToString(fontSize);
			node.Attributes.GetNamedItem("imgSizeW").Value=Convert.ToString(imgSizeW);
			node.Attributes.GetNamedItem("imgSizeH").Value=Convert.ToString(imgSizeH);
			node.Attributes.GetNamedItem("offsetX").Value=Convert.ToString(offsetX);
			node.Attributes.GetNamedItem("offsetY").Value=Convert.ToString(offsetY);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -