Ejemplo n.º 1
0
        /// <summary>
        /// 计算文本显示宽、高
        /// </summary>
        public static MeasureSize MeasureText(string text,
                                              System.Windows.Media.FontFamily fontFamily,
                                              System.Windows.FontStyle fontStyle,
                                              System.Windows.FontWeight fontWeight,
                                              System.Windows.FontStretch fontStretch,
                                              double fontSize)
        {
            System.Windows.Media.Typeface      typeface = new System.Windows.Media.Typeface(fontFamily, fontStyle, fontWeight, fontStretch);
            System.Windows.Media.GlyphTypeface glyphTypeface;
            if (!typeface.TryGetGlyphTypeface(out glyphTypeface))
            {
                return(MeasureTextSize(text, fontFamily, fontStyle, fontWeight, fontStretch, fontSize));
            }
            double totalWidth = 0;
            double height     = 0;

            for (int n = 0; n < text.Length; n++)
            {
                ushort glyphIndex  = glyphTypeface.CharacterToGlyphMap[text[n]];
                double width       = glyphTypeface.AdvanceWidths[glyphIndex] * fontSize;
                double glyphHeight = glyphTypeface.AdvanceHeights[glyphIndex] * fontSize;
                if (glyphHeight > height)
                {
                    height = glyphHeight;
                }
                totalWidth += width;
            }
            return(new MeasureSize(totalWidth, height));
        }
Ejemplo n.º 2
0
 private static byte[] GetFontWithWPF(Font aFont, byte[] FFontData)
 {
     System.Windows.Media.Typeface      fm = new System.Windows.Media.Typeface(GetWpfFontFamily(aFont), GetWpfStyle(aFont), GetWpfFontWeight(aFont), GetWpfFontStretch(aFont));
     System.Windows.Media.GlyphTypeface gt;
     if (fm.TryGetGlyphTypeface(out gt))
     {
         using (Stream st = gt.GetFontStream())
         {
             FFontData = new byte[st.Length];
             st.Read(FFontData, 0, FFontData.Length);
         }
     }
     return(FFontData);
 }
Ejemplo n.º 3
0
        private void ConvertButton_Click(object sender, EventArgs e)
        {
            if (OutputFileName == null || OutputFileName == "")
            {
                MessageBox.Show("Invalid File Name!");
            }
            else
            {
                //FileStream str;
                FileStream final;
                dynamic    strm;

                #region Already Exists
                if (File.Exists(OutputFileName))
                {
                    if (MessageBox.Show("A file at '" + OutputFileName + "' already exists! Would you like to overwrite it?", "File Already Exists", MessageBoxButtons.YesNoCancel) == System.Windows.Forms.DialogResult.Yes)
                    {
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    File.Create(OutputFileName).Close();
                }
                #endregion

                #region Compression Config
                //if (CompressionMode == CompressionType.Deflate)
                //{
                //    str = new FileStream(OutputFileName, FileMode.Truncate);
                //    strm = new DeflateStream(str, System.IO.Compression.CompressionMode.Compress);
                //    final = null;
                //}
                //else if (CompressionMode == CompressionType.GZip)
                //{
                //    str = new FileStream(OutputFileName, FileMode.Truncate);
                //    strm = new GZipStream(str, System.IO.Compression.CompressionMode.Compress);
                //    final = null;
                //}
                if (CompressionMode == CompressionType.LZMA)
                {
                    if (File.Exists(OutputFileName + ".uncmpr"))
                    {
                        File.Delete(OutputFileName + ".uncmpr");
                    }
                    strm  = File.Create(OutputFileName + ".uncmpr");
                    final = new FileStream(OutputFileName, FileMode.Truncate);
                }
                else
                {
                    strm  = new FileStream(OutputFileName, FileMode.Truncate);
                    final = null;
                }
                #endregion

                BinaryWriter br = new BinaryWriter(strm);

                br.Write((byte)0);
                br.Write((byte)0);
                br.Write((byte)0);
                br.Write((byte)0);
                br.Write((byte)0); // Write the 8 empty bytes of the header
                br.Write((byte)0);
                br.Write((byte)0);
                br.Write((byte)0);

                byte[] buffer;
                br.Write((UInt16)47); // write the format version.


                string FontName = (string)FontComboBox.SelectedItem;

                buffer = ASCIIEncoding.ASCII.GetBytes(FontName);
                int i = 256 - buffer.Length;
                if (i < 0)
                {
                    throw new Exception("Font Name is to long!");
                }
                else
                {
                    br.Write(buffer);
                    for (int i2 = 0; i2 < i; i2++)
                    {
                        br.Write((byte)0); // Fill the rest of the 256 bytes.
                    }
                }

                System.Windows.Media.Typeface      t = new System.Windows.Media.Typeface(FontName);
                System.Windows.Media.GlyphTypeface glyph;
                if (t.TryGetGlyphTypeface(out glyph))
                {
                    t = null;
                    IDictionary <int, ushort> charKeyMap = (IDictionary <int, ushort>)glyph.CharacterToGlyphMap;
                    glyph = null;
                    SortedSet <int> chars = new SortedSet <int>();
                    foreach (KeyValuePair <int, ushort> c in charKeyMap)
                    {
                        chars.Add(c.Key);
                    }
                    charKeyMap = null;
                    Font f = new Font(FontName, 128, GraphicsUnit.Pixel);

                    UInt64 charsToWrite = (ulong)chars.Count; // *16;
                    br.Write(charsToWrite);                   // Write the number of chars to read.

                    int prevChar = 0;

                    for (byte style = 0; style < 1; style++)
                    {
                        f = new Font(FontName, 64, (FontStyle)style, GraphicsUnit.Pixel);
                        foreach (int ch in chars)
                        {
                            Bitmap   Backend = new Bitmap(1, 1);
                            Graphics g       = Graphics.FromImage(Backend);
                            SizeF    sz      = g.MeasureString(new String(new char[] { (char)ch }), f);
                            byte     height  = (byte)Math.Ceiling(sz.Height + 2);
                            byte     width   = (byte)Math.Ceiling(sz.Width + 4);
                            Backend = new Bitmap(width, height);
                            g       = Graphics.FromImage(Backend);
                            g.Clear(Color.White);

                            g.DrawString(new String(new char[] { (char)ch }), f, new SolidBrush(Color.Black), 2, 2);
                            g.Flush(System.Drawing.Drawing2D.FlushIntention.Flush);
                            if (prevChar + 1 == ch)
                            {
                                br.Write((byte)255); // write that it's incremented from the previous char.
                            }
                            else
                            {
                                br.Write((byte)0);    // write that it's not incremented from the previous char.
                                br.Write((UInt32)ch); // Write the char number.
                            }
                            pictureBox1.Image = Backend;
                            pictureBox1.Refresh();

                            br.Write((byte)style);  // write it's style
                            br.Write((byte)height); // write the height
                            br.Write((byte)width);  // write the width
                            buffer = ConvertToByteArray(Backend);
                            br.Write(buffer);
                            prevChar = ch;
                        }
                        strm.Flush();
                        System.GC.Collect();
                    }

                    if (CompressionMode == CompressionType.LZMA)
                    {
                        strm.Position = 0;
                        buffer        = new byte[strm.Length];
                        strm.Read(buffer, 0, (Int32)strm.Length);
                        buffer = Orvid.Compression.LZMACoder.Compress(buffer);
                        final.WriteByte(255);
                        final.Write(buffer, 0, buffer.Length);
                        final.Flush();
                        final.Close();
                        final.Dispose();
                    }

                    strm.Flush();
                    strm.Close();
                    strm.Dispose();
                    if (CompressionMode == CompressionType.LZMA)
                    {
                        File.Delete(OutputFileName + ".uncmpr");
                    }
                    //pictureBox1.Image = null;

                    MessageBox.Show("Conversion Completed Successfully!");
                }
                else
                {
                    throw new Exception("Unable to load the glyph typeface!");
                }
            }
        }
Ejemplo n.º 4
0
        private void ConvertButton_Click(object sender, EventArgs e)
        {
            if (OutputFileName == null || OutputFileName == "")
            {
                MessageBox.Show("Invalid File Name!");
            }
            else
            {
                //FileStream str;
                FileStream final;
                dynamic strm;

                #region Already Exists
                if (File.Exists(OutputFileName))
                {
                    if (MessageBox.Show("A file at '" + OutputFileName + "' already exists! Would you like to overwrite it?", "File Already Exists", MessageBoxButtons.YesNoCancel) == System.Windows.Forms.DialogResult.Yes)
                    {

                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    File.Create(OutputFileName).Close();
                }
                #endregion

                #region Compression Config
                //if (CompressionMode == CompressionType.Deflate)
                //{
                //    str = new FileStream(OutputFileName, FileMode.Truncate);
                //    strm = new DeflateStream(str, System.IO.Compression.CompressionMode.Compress);
                //    final = null;
                //}
                //else if (CompressionMode == CompressionType.GZip)
                //{
                //    str = new FileStream(OutputFileName, FileMode.Truncate);
                //    strm = new GZipStream(str, System.IO.Compression.CompressionMode.Compress);
                //    final = null;
                //}
                if (CompressionMode == CompressionType.LZMA)
                {
                    if (File.Exists(OutputFileName + ".uncmpr"))
                    {
                        File.Delete(OutputFileName + ".uncmpr");
                    }
                    strm = File.Create(OutputFileName + ".uncmpr");
                    final = new FileStream(OutputFileName, FileMode.Truncate);
                }
                else
                {
                    strm = new FileStream(OutputFileName, FileMode.Truncate);
                    final = null;
                }
                #endregion

                BinaryWriter br = new BinaryWriter(strm);

                br.Write((byte)0);
                br.Write((byte)0);
                br.Write((byte)0);
                br.Write((byte)0);
                br.Write((byte)0); // Write the 8 empty bytes of the header
                br.Write((byte)0);
                br.Write((byte)0);
                br.Write((byte)0);

                byte[] buffer;
                br.Write((UInt16)47); // write the format version.


                string FontName = (string)FontComboBox.SelectedItem;

                buffer = ASCIIEncoding.ASCII.GetBytes(FontName);
                int i = 256 - buffer.Length;
                if (i < 0)
                {
                    throw new Exception("Font Name is to long!");
                }
                else
                {
                    br.Write(buffer);
                    for (int i2 = 0; i2 < i; i2++)
                    {
                        br.Write((byte)0); // Fill the rest of the 256 bytes.
                    }
                }

                System.Windows.Media.Typeface t = new System.Windows.Media.Typeface(FontName);
                System.Windows.Media.GlyphTypeface glyph;
                if (t.TryGetGlyphTypeface(out glyph))
                {
                    t = null;
                    IDictionary<int, ushort> charKeyMap = (IDictionary<int, ushort>)glyph.CharacterToGlyphMap;
                    glyph = null;
                    SortedSet<int> chars = new SortedSet<int>();
                    foreach (KeyValuePair<int, ushort> c in charKeyMap)
                    {
                        chars.Add(c.Key);
                    }
                    charKeyMap = null;
                    Font f = new Font(FontName, 128, GraphicsUnit.Pixel);

                    UInt64 charsToWrite = (ulong)chars.Count;// *16;
                    br.Write(charsToWrite); // Write the number of chars to read.

                    int prevChar = 0;

                    for (byte style = 0; style < 1; style++)
                    {
                        f = new Font(FontName, 64, (FontStyle)style, GraphicsUnit.Pixel);
                        foreach (int ch in chars)
                        {
                            Bitmap Backend = new Bitmap(1, 1);
                            Graphics g = Graphics.FromImage(Backend);
                            SizeF sz = g.MeasureString(new String(new char[] { (char)ch }), f);
                            byte height = (byte)Math.Ceiling(sz.Height + 2);
                            byte width = (byte)Math.Ceiling(sz.Width + 4);
                            Backend = new Bitmap(width, height);
                            g = Graphics.FromImage(Backend);
                            g.Clear(Color.White);

                            g.DrawString(new String(new char[] { (char)ch }), f, new SolidBrush(Color.Black), 2, 2);
                            g.Flush(System.Drawing.Drawing2D.FlushIntention.Flush);
                            if (prevChar + 1 == ch)
                            {
                                br.Write((byte)255); // write that it's incremented from the previous char.
                            }
                            else
                            {
                                br.Write((byte)0); // write that it's not incremented from the previous char.
                                br.Write((UInt32)ch); // Write the char number.
                            }
                            pictureBox1.Image = Backend;
                            pictureBox1.Refresh();

                            br.Write((byte)style); // write it's style
                            br.Write((byte)height); // write the height
                            br.Write((byte)width); // write the width
                            buffer = ConvertToByteArray(Backend);
                            br.Write(buffer);
                            prevChar = ch;
                        }
                        strm.Flush();
                        System.GC.Collect();
                    }

                    if (CompressionMode == CompressionType.LZMA)
                    {
                        strm.Position = 0;
                        buffer = new byte[strm.Length];
                        strm.Read(buffer, 0, (Int32)strm.Length);
                        buffer = Orvid.Compression.LZMACoder.Compress(buffer);
                        final.WriteByte(255);
                        final.Write(buffer, 0, buffer.Length);
                        final.Flush();
                        final.Close();
                        final.Dispose();
                    }

                    strm.Flush();
                    strm.Close();
                    strm.Dispose();
                    if (CompressionMode == CompressionType.LZMA)
                    {
                        File.Delete(OutputFileName + ".uncmpr");
                    }
                    //pictureBox1.Image = null;

                    MessageBox.Show("Conversion Completed Successfully!");
                }
                else
                {
                    throw new Exception("Unable to load the glyph typeface!");
                }
            }
        }