Esempio n. 1
0
        public static void ExportData(Mfe.Font font, string path)
        {
            if (font.VariableWidth)
            {
                throw new ArgumentException("Raw bitmap fonts must be fixed-width.");
            }
            if (font.Width < 1 || (font.Width & 0x7) > 0)
            {
                throw new ArgumentException("Font width must be multiple of 8.");
            }
            bool Inverted = font["Inverted"] == "true";
            int  firstChar;
            int  lastChar;

            if (!Int32.TryParse((font["FirstChar"] ?? "0"), out firstChar))
            {
                firstChar = 0;
            }
            if (!Int32.TryParse((font["LastChar"] ?? "255"), out lastChar))
            {
                firstChar = 255;
            }
            if (firstChar < 0)
            {
                firstChar = 0;
            }
            if (lastChar > 255)
            {
                lastChar = 255;
            }

            byte[] data  = new byte[(((font.Width - 1) >> 3) + 1) * font.Height * (lastChar - firstChar + 1)];
            int    index = 0;

            for (int i = firstChar; i <= lastChar; i++)
            {
                for (int row = 0; row < font[i].Height; row++)
                {
                    int col;
                    int b = 0;
                    for (col = 0; col < font[i].Width; col++)
                    {
                        if (font[i][row, col] ^ Inverted)
                        {
                            b |= 0x80 >> (0 + (col & 0x7));
                        }
                        if ((col & 0x7) == 7)
                        {
                            data[index++] = (byte)b;
                            b             = 0;
                        }
                    }
                    if ((col & 0x7) != 0)
                    {
                        data[index++] = (byte)b;
                    }
                }
            }
            System.IO.File.WriteAllBytes(path, data);
        }
Esempio n. 2
0
 public void ApplyToFont(Mfe.Font font)
 {
     for (int i = 0; i < Font.MaximumCodePoints; i++)
     {
         font[i].Codepoint = CodePoints[i];
         font[i].Name      = Names[i];
     }
     font["Code Page"] = Name;
 }
Esempio n. 3
0
        public static void ExportData(Mfe.Font font, string path)
        {
            // Check to make sure the font even looks approximately compatible
            if (font.VariableWidth)
            {
                throw new ArgumentException("Omnicalc does not support variable width Fonts.");
            }
            if (!(font.Width == 8 || font.Width == 6 || font.Width == 5))
            {
                throw new ArgumentException("Invalid font width for Omnicalc.");
            }
            if (font.Height != 7)
            {
                throw new ArgumentException("Omnicalc fonts must be 7 pixels high.");
            }
            // First, we're going to stuff all the data we want to export into
            // an array.  Then, we'll use the program class to package the data
            // into a valid program file and return it to the caller.
            byte[] data = new byte[256 * 7 + 8];
            // Master pointer
            int i = 0;
            // Create file header
            string headerData = "omnicalc";

            for (int a = 0; a < headerData.Length; a++)
            {
                data[i++] = (byte)headerData[a];
            }
            // Add font glyphs
            // Char: 01234567
            // Omni: 76543210
            int temp;

            for (int ch = 0; ch < 256; ch++)
            {
                for (int row = 0; row < font.Height; row++)
                {
                    temp = 0;
                    for (int col = 0; col < font.Width; col++)
                    {
                        if (font[ch][row, col])
                        {
                            temp = temp | (1 << (5 - col));
                        }
                    }
                    data[i++] = (byte)temp;
                }
            }
            // Now package the data into a program variable.
            Program prgm = new Program();

            prgm.Type = VariableType.ProtProgObj;
            prgm.Data = data;
            System.IO.File.WriteAllBytes(path, prgm.Export(archived: true));
        }
Esempio n. 4
0
        public static CodePageTable CopyFromFont(Mfe.Font font)
        {
            CodePageTable ret = new CodePageTable();

            for (int i = 0; i < Font.MaximumCodePoints; i++)
            {
                ret.CodePoints[i] = font[i].Codepoint;
            }
            ret.Name = font["Code Page"] ?? "<unspecified>";
            return(ret);
        }
Esempio n. 5
0
 private void loadCpButton_Click(object sender, EventArgs e)
 {
     if (loadCodepageDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
     {
         try
         {
             ImportedFont = CurrentFont.MakeMfeFont(
                 CodePageTable.FromFile(loadCodepageDialog.FileName)
                 );
             destCharPreviewer.Char = ImportedFont[(int)destSelectUpDown.Value];
             Master.Chart.RefreshData();
         }
         catch (Exception f)
         {
             MessageBox.Show("Error: " + f.ToString(), "Error Loading Codepage File", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
     }
 }
Esempio n. 6
0
        public static void ExportData(Mfe.Font font, string path)
        {
            StringBuilder data = new StringBuilder(65536);
            int           row, col;
            bool          Inverted = font["Inverted"] == "true";
            //string baseName = font["AsmName"] ?? "font";
            string baseName;

            if (font.AboutData.TryGetValue("AsmName", out baseName))
            {
                data.Append(baseName);
            }
            else
            {
                baseName = "font";
            }
            data.AppendLine(".header:");
            data.AppendLine("; FONT METADATA");
            foreach (KeyValuePair <string, string> s in font.AboutData)
            {
                data.Append("; ");
                data.Append(s.Key);
                data.Append(" = ");
                data.Append(s.Value);
                data.AppendLine();
            }
            data.AppendLine("\x0009db\x00090 ; font format version");
            data.Append("\x0009db\x0009"); data.Append(font.Height); data.AppendLine(" ; height");
            int glyphCount = (font.LastCodePoint - font.FirstCodePoint + 1) & 255;

            data.Append("\x0009db\x0009"); data.Append(glyphCount); data.AppendLine(" ; glyph count");
            //data.Append("\x0009db\x0009"); data.Append(); data.AppendLine(" ; ");
            data.Append("\x0009db\x0009"); data.Append(font.FirstCodePoint); data.AppendLine(" ; first codepoint");

            /*int locationOfWidthsTable = 18;
             * data.Append("\x0009dl\x0009"); data.Append(locationOfWidthsTable); data.AppendLine(" ; offset to widths table");
             * int locationOfBitmapsTable = locationOfWidthsTable + glyphCount;
             * data.Append("\x0009dl\x0009"); data.Append(locationOfBitmapsTable); data.AppendLine(" ; offset to bitmaps table");
             * int locationOfNextBitmap = locationOfBitmapsTable + glyphCount * 3;*/
            data.Append("\x0009dl\x0009"); /*data.Append(baseName); */ data.Append(".widthsTable - .header"); /*data.Append(baseName);*/ data.AppendLine(" ; offset to widths table");
            data.Append("\x0009dl\x0009"); /*data.Append(baseName); */ data.Append(".bitmapsTable - .header"); /*data.Append(baseName);*/ data.AppendLine(" ; offset to bitmaps offsets table");
            data.Append("\x0009db\x0009"); data.Append(font.ItalicSpaceAdjust); data.AppendLine(" ; italics space adjust");
            data.Append("\x0009db\x0009"); data.Append(font.SpaceAbove); data.AppendLine(" ; suggested blank space above");
            data.Append("\x0009db\x0009"); data.Append(font.SpaceBelow); data.AppendLine(" ; suggested blank space below");
            data.Append("\x0009db\x0009"); data.Append(font.Weight); data.AppendLine(" ; weight (boldness/thinness)");
            data.Append("\x0009db\x0009"); data.Append(font.Style); data.AppendLine(" ; style field");
            data.Append("\x0009db\x0009"); data.Append(font.CapHeight); data.AppendLine(" ; capital height");
            data.Append("\x0009db\x0009"); data.Append(font.XHeight); data.AppendLine(" ; lowercase x height");
            data.Append("\x0009db\x0009"); data.Append(font.BaseLine); data.AppendLine(" ; baseline height");

            /*data.Append(baseName); */ data.AppendLine(".widthsTable: ; start of widths table");
            for (int i = font.FirstCodePoint; i <= font.LastCodePoint; i++)
            {
                data.Append("\x0009db\x0009"); data.Append(font[i].Width);
                data.Append("\x0009; Code point ");
                data.Append(i.ToString("X2"));
                data.Append(" ");
                data.Append(font[i].Codepoint);
                data.Append(" ");
                data.Append(font[i].Name);
                data.AppendLine();
            }

            /*data.Append(baseName); */ data.AppendLine(".bitmapsTable: ; start of table of offsets to bitmaps");
            for (int i = font.FirstCodePoint; i <= font.LastCodePoint; i++)
            {
                data.Append("\x0009dw\x0009");
                /*data.Append(baseName); */ data.Append(".glyph_"); data.Append(i.ToString("X2"));
                data.Append(" - .header");// data.Append(baseName);
                if (font[i].Width <= 16)
                {
                    data.Append(" - ");
                    data.Append((byte)(2 - ((font[i].Width > 0 ? font[i].Width : 1) - 1) / 8));
                }
                data.AppendLine();
            }

            for (int i = font.FirstCodePoint; i <= font.LastCodePoint; i++)
            {
                // Header
                /*data.Append(baseName); */ data.Append(".glyph_"); data.Append(i.ToString("X2"));
                data.Append(":\x0009; Code point "); data.Append(i.ToString("X2")); data.Append(" ");
                data.Append(font[i].Codepoint); data.Append(" ");
                data.AppendLine(font[i].Name);
                // Data
                Char ch = font[i];
                if (ch.Width > 24)
                {
                    throw new ArgumentException("This format does not support glyphs wider than 24 pixels.");
                }
                for (row = 0; row < font.Height; row++)
                {
                    data.Append("\x0009db\x0009");
                    // Output distinct bytes, but in little-endian order
                    // The format also requires omitting the least-significant byte(s) if they're unused,
                    // so we have to output the bytes from right-to-left, and the bits from left-to-right.
                    for (int bit = ((ch.Width > 1 ? ch.Width : 1) - 1) & ~7; bit >= 0; bit -= 8) // Initializer gives 0 for 0-8, 8 for 9-16, 16 for 17-24
                    {
                        for (col = 0; col < 8; col++)
                        {
                            if (bit + col >= ch.Width)
                            {
                                data.Append('0');
                            }
                            else if (ch[row, bit + col] ^ Inverted)
                            {
                                data.Append('1');
                            }
                            else
                            {
                                data.Append('0');
                            }
                        }
                        data.Append("b");
                        if (bit > 0)
                        {
                            data.Append(", ");
                        }
                    }
                    data.AppendLine();
                }
            }

            System.IO.File.WriteAllText(path, data.ToString());
        }
Esempio n. 7
0
        public static void ExportData(Mfe.Font font, string path)
        {
            StringBuilder data = new StringBuilder(65536);
            int           row, col, remainder;
            bool          Inverted = font["Inverted"] == "true";
            string        baseName = font["AsmName"] ?? "font";
            int           firstChar;
            int           lastChar;

            if (!Int32.TryParse((font["FirstChar"] ?? "0"), out firstChar))
            {
                firstChar = 0;
            }
            if (!Int32.TryParse((font["LastChar"] ?? "255"), out lastChar))
            {
                firstChar = 255;
            }
            if (firstChar < 0)
            {
                firstChar = 0;
            }
            if (lastChar > 255)
            {
                lastChar = 255;
            }
            data.Append(baseName);
            data.AppendLine(":");
            data.AppendLine("; FONT METADATA");
            foreach (KeyValuePair <string, string> s in font.AboutData)
            {
                data.Append("; ");
                data.Append(s.Key);
                data.Append(" = ");
                data.Append(s.Value);
                data.AppendLine();
            }
            data.Append("\x9.db\x9");
            data.Append(font.Height);
            data.Append("\x9; font height");
            data.AppendLine();
            data.AppendLine("; ");
            data.AppendLine("; GLYPH WIDTH TABLE");
            data.Append(baseName);
            data.Append("WidthTable:");
            data.AppendLine();
            for (int i = firstChar; i <= lastChar; i++)
            {
                data.Append("\x9.db\x9");
                data.Append(font[i].Width);
                data.Append("\x9; ");
                data.Append(font[i].Codepoint);
                data.Append(" ");
                data.Append(font[i].Name);
                data.AppendLine();
            }
            data.AppendLine("; ");
            data.AppendLine("; GLYPH DATA TABLE");
            data.Append(baseName);
            data.Append("DataTable:");
            data.AppendLine();


            /*
             * byte[] bytes = new byte[65536];
             * int[] ptrs = new int[256];
             * int ptr = (lastChar - firstChar) * 3 + 3;
             *
             * for (int i = firstChar; i <= lastChar; i++)
             * {
             *  // Header
             *  bytes[(i - firstChar) * 3] = (byte)(ptr & 255);
             *  bytes[(i - firstChar) * 3 + 1] = (byte)((ptr >> 8) & 255);
             *  bytes[(i - firstChar) * 3 + 2] = (byte)((ptr >> 16) & 255);
             *  col = 0;
             *  row = 0;
             *  remainder = font[i].Width * font[i].Height;
             *  // Produce bitmap
             *  for (row = 0; row < font[i].Height; row++)
             *  {
             *      for (col = 0; col < font[i].Width; col += 8)
             *      {
             *          int dataByte = 0;
             *          for (int j = 0; j < 8; j++)
             *              if (j + col < font[i].Width ^ Inverted)
             *                  if (font[i][row, col + j])
             *                      dataByte = (dataByte << 1) | 1;
             *                  else
             *                      dataByte <<= 1;
             *          bytes[ptr++] = (byte)dataByte;
             *      }
             *  }
             * }
             *
             * int blah = 0;
             * for (int i = 0; i < ptr; i++)
             * {
             *  if (blah-- <= 1)
             *  {
             *      data.AppendLine();
             *      data.Append("\x9.db\x9");
             *      blah = 8;
             *  }
             *  else
             *      data.Append(", ");
             *  data.Append("0");
             *  data.Append(bytes[i].ToString("X2"));
             *  data.Append("h");
             * }
             */


            for (int i = firstChar; i <= lastChar; i++)
            {
                data.Append("\x9.dl\x9");
                data.Append(baseName);
                data.Append("Char");
                data.Append(i.ToString("X2"));
                data.Append(" - ");
                data.Append(baseName);
                data.Append("DataTable");
                data.Append("\x9; ");
                data.Append(font[i].Codepoint);
                data.Append(" ");
                data.Append(font[i].Name);
                data.AppendLine();
            }
            data.AppendLine("; ");
            data.AppendLine("; GLYPH DATA");
            for (int i = firstChar; i <= lastChar; i++)
            {
                // Header
                data.Append(baseName);
                data.Append("Char");
                data.Append(i.ToString("X2"));
                data.Append(": ; ");
                data.Append(font[i].Codepoint);
                data.Append(" ");
                data.Append(font[i].Name);
                data.AppendLine();
                col       = 0;
                row       = 0;
                remainder = font[i].Width * font[i].Height;
                // Produce bitmap
                for (row = 0; row < font[i].Height; row++)
                {
                    data.Append("\x9.db\x9");
                    for (col = 0; col < font[i].Width; col += 8)
                    {
                        if (col != 0)
                        {
                            data.Append(", ");
                        }
                        for (int j = 0; j < 8; j++)
                        {
                            if (j + col >= font[i].Width ^ Inverted)
                            {
                                data.Append("0");
                            }
                            else
                            if (font[i][row, col + j])
                            {
                                data.Append("1");
                            }
                            else
                            {
                                data.Append("0");
                            }
                        }
                        data.Append("b");
                    }
                    data.AppendLine();
                }
            }
            System.IO.File.WriteAllText(path, data.ToString());
        }
Esempio n. 8
0
        public static void ExportData(Mfe.Font font, string path)
        {
            // Glyphs must be orded by code point, so sort them once now.
            List <Char> glyphs = new List <Mfe.Char>();

            for (int i = font.FirstCodePoint; i <= font.LastCodePoint; i++)
            {
                if (!font[i].Name.ToUpper().EndsWith("#NOEXPORT"))
                {
                    glyphs.Add(font[i]);
                }
            }
            glyphs.Sort((a, b) => a.Codepoint.CompareTo(b.Codepoint));

            int location;

            byte[] data = new byte[65536];
            for (location = 0; location < 7; location++)
            {
                data[location] = (byte)"binfont"[location];
            }
            data[location++] = 0;

            WriteDWord(data, ref location, glyphs.Count);
            int metricsOffsetLocation = location;

            location += 4;
            int unicodeConversionTableOffsetLocation = location;

            location += 4;
            int widthsOffsetLocation = location;

            location += 4;
            int bitmapsTableOffsetLocation = location;

            WriteDWord(data, ref location, 0);

            // Write metrics struct
            // Offset to metrics
            WriteDWord(data, ref metricsOffsetLocation, location);
            // Version
            WriteDWord(data, ref location, 0x10001);
            // Flags
            WriteDWord(data, ref location, font.VariableWidth ? 0 : 1);
            // Height
            data[location++] = font.Height;
            // Base line
            data[location++] = font.BaseLine;
            // x Height
            data[location++] = font.XHeight;
            // Caps height
            data[location++] = font.CapHeight;
            // Default glyph
            int missingGlyph = 0;

            //if (!Int32.TryParse((font["MissingGlyph"] ?? "0"), out missingGlyph))
            //    missingGlyph = 0;
            if (font.AboutData.ContainsKey("MissingGlyph"))
            {
                if (font["MissingGlyph"].Length > 0)
                {
                    missingGlyph = (int)font["MissingGlyph"][0];
                }
            }
            WriteDWord(data, ref location, missingGlyph);


            // Unicode conversion table
            WriteDWord(data, ref unicodeConversionTableOffsetLocation, location);
            for (int i = 0; i < glyphs.Count; i++)
            {
                WriteDWord(data, ref location, glyphs[i].Codepoint);
            }

            // Widths table
            WriteDWord(data, ref widthsOffsetLocation, location);
            for (int i = 0; i < glyphs.Count; i++)
            {
                data[location++] = glyphs[i].Width;
            }
            if ((location & 3) > 0)
            {
                location = (location | 3) + 1; //location += 4 - location % 4; // Ensure alignment
            }
            // Bitmaps offsets table
            WriteDWord(data, ref bitmapsTableOffsetLocation, location);
            int bitmapsTableOffset = location;

            location += 4 * (glyphs.Count);

            // Bitmaps
            for (int i = 0; i < glyphs.Count; i++)
            {
                WriteDWord(data, ref bitmapsTableOffset, location);
                for (int y = 0; y < font.Height; y++)
                {
                    for (int xx = 0; xx < glyphs[i].Width; xx += 32)
                    {
                        uint b = 0;
                        for (int x = 0; x < glyphs[i].Width; x++)
                        {
                            b = ((glyphs[i][y, x + xx] ? 0x80000000 : 0) >> x) | b;
                        }
                        WriteDWord(data, ref location, (int)b);
                    }
                }
            }

            // Write data to disk
            byte[] finalData = new byte[location];
            for (int i = 0; i < location; i++)
            {
                finalData[i] = data[i];
            }
            System.IO.File.WriteAllBytes(path, finalData);
        }
Esempio n. 9
0
        public static void ExportData(Mfe.Font font, string path)
        {
            StringBuilder data = new StringBuilder(65536);
            int           row, col, remainder;
            bool          Inverted = font["Inverted"] == "true";
            string        baseName = font["AsmName"] ?? "font";
            int           firstChar;
            int           lastChar;

            if (!Int32.TryParse((font["FirstChar"] ?? "0"), out firstChar))
            {
                firstChar = 0;
            }
            if (!Int32.TryParse((font["LastChar"] ?? "255"), out lastChar))
            {
                firstChar = 255;
            }
            if (firstChar < 0)
            {
                firstChar = 0;
            }
            if (lastChar > 255)
            {
                lastChar = 255;
            }
            data.Append(baseName);
            data.AppendLine(":");
            data.AppendLine("; FONT METADATA");
            foreach (KeyValuePair <string, string> s in font.AboutData)
            {
                data.Append("; ");
                data.Append(s.Key);
                data.Append(" = ");
                data.Append(s.Value);
                data.AppendLine();
            }
            data.Append("\x9.db\x9");
            data.Append(font.Width);
            data.Append("\x9; font width");
            data.AppendLine();
            data.Append("\x9.db\x9");
            data.Append(font.Height);
            data.Append("\x9; font height");
            data.AppendLine();
            data.Append("\x9.db\x9((");
            data.Append(font.Width);
            data.Append(" + 7) >> 3) * ");
            data.Append(font.Height);
            data.Append("\x9; Bytes per character bitmap");
            data.AppendLine();
            data.AppendLine("; ");
            data.AppendLine("; GLYPH DATA TABLE");
            data.Append(baseName);
            data.Append("DataTable:");
            data.AppendLine();
            for (int i = firstChar; i <= lastChar; i++)
            {
                // Header
                data.Append("\x9; Code point ");
                data.Append(i.ToString("X2"));
                data.Append(" ");
                data.Append(font[i].Codepoint);
                data.Append(" ");
                data.Append(font[i].Name);
                data.AppendLine();
                col       = 0;
                row       = 0;
                remainder = font[i].Width * font[i].Height;
                // Produce bitmap
                for (row = 0; row < font[i].Height; row++)
                {
                    data.Append("\x9.db\x9");
                    for (col = 0; col < font[i].Width; col += 8)
                    {
                        if (col != 0)
                        {
                            data.Append(", ");
                        }
                        for (int j = 0; j < 8; j++)
                        {
                            if (j + col >= font[i].Width ^ Inverted)
                            {
                                data.Append("0");
                            }
                            else
                            if (font[i][row, col + j])
                            {
                                data.Append("1");
                            }
                            else
                            {
                                data.Append("0");
                            }
                        }
                        data.Append("b");
                    }
                    data.AppendLine();
                }
            }
            System.IO.File.WriteAllText(path, data.ToString());
        }
Esempio n. 10
0
 private void loadCpButton_Click(object sender, EventArgs e)
 {
     if (loadCodepageDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
         try
         {
             ImportedFont = CurrentFont.MakeMfeFont(
                 CodePageTable.FromFile(loadCodepageDialog.FileName)
                 );
             destCharPreviewer.Char = ImportedFont[(int)destSelectUpDown.Value];
             Master.Chart.RefreshData();
         }
         catch (Exception f)
         {
             MessageBox.Show("Error: " + f.ToString(), "Error Loading Codepage File", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
 }
Esempio n. 11
0
        public static void ExportData(Mfe.Font font, string path)
        {
            StringBuilder data = new StringBuilder(65536);
            // I dunno, maybe you want to invert the font?
            bool inverted = font["Inverted"] == "true";

            // Font header
            data.AppendLine("; FONT METADATA");
            foreach (KeyValuePair <string, string> s in font.AboutData)
            {
                data.Append("; ");
                data.Append(s.Key);
                data.Append(" = ");
                data.Append(s.Value);
                data.AppendLine();
            }
            data.AppendLine("; ");
            data.AppendLine("; FONT DATA");
            data.AppendLine("; Height byte");
            data.Append("\x9.db\x9");
            data.Append(font.Height);
            data.AppendLine();
            Mfe.Char ch;
            int      bytesPerLine;
            int      temp;

            // Iterate over codepoints
            for (int i = 0; i < 256; i++)
            {
                // Header for each character
                ch = font[i];
                data.Append("; Char ");
                data.Append(i.ToString("X2"));
                data.Append(" ");
                data.Append(ch.Codepoint.ToString());
                data.Append(" ");
                data.Append(ch.Name);
                data.Append(" ");
                data.AppendLine();
                bytesPerLine = (ch.Width + 7) / 8;
                data.Append("\x9.db\x9");
                data.Append(ch.LogicalWidth);
                data.Append(" ; width");
                data.AppendLine();
                // Produce bytes
                if (bytesPerLine != 0)
                {
                    for (int row = 0; row < ch.Height; row++)
                    {
                        data.Append("\x9.db\x9");
                        for (int bite = 0; bite < bytesPerLine; bite++)
                        {
                            temp = 0;
                            for (int bit = 0; bit < (bite != bytesPerLine - 1 ? 8 : ch.Width - bite * 8); bit++)
                            {
                                temp |= (ch[row, bite * 8 + bit] ^ inverted ? 1 : 0) << (7 - bit);
                            }
                            data.Append("$");
                            data.Append(temp.ToString("X2"));
                            if (bite != bytesPerLine - 1)
                            {
                                data.Append(", ");
                            }
                        }
                        data.AppendLine();
                    }
                }
            }

            System.IO.File.WriteAllText(path, data.ToString());
        }
Esempio n. 12
0
        public static void ExportData(Mfe.Font font, string path)
        {
            StringBuilder data = new StringBuilder(8192);
            int           shift, curByte;
            bool          Inverted = font["Inverted"] == "true";

            data.Append("; FONT METADATA");
            foreach (KeyValuePair <string, string> s in font.AboutData)
            {
                data.Append("; ");
                data.Append(s.Key);
                data.Append(" = ");
                data.Append(s.Value);
                data.AppendLine();
            }
            data.AppendLine("; ");
            data.AppendLine("; FONT DATA");
            for (int i = 0; i < 256; i++)
            {
                shift   = 0;
                curByte = 0;
                data.Append("; Char ");
                data.Append(i.ToString("X2"));
                data.Append(" ");
                if (i > 31)
                {
                    data.Append((char)i);
                }
                data.AppendLine();
                // Produce bytes
                for (int col = 0; col < font[i].Width; col++)
                {
                    for (int row = 0; row < font[i].Height; row++)
                    {
                        if (font[i][row, col] ^ Inverted)
                        {
                            curByte |= 1 << shift++;
                        }
                        else
                        {
                            shift++;
                        }
                        if (shift > 7)
                        {
                            data.Append("\x9.db\x9");
                            data.Append(ToBinaryByte(curByte));
                            data.Append("b");
                            data.AppendLine();
                            shift   = 0;
                            curByte = 0;
                        }
                    }
                }
                if (shift != 0)
                {
                    data.Append("\x9.db\x9");
                    data.Append(ToBinaryByte(curByte));
                    data.Append("b");
                    data.AppendLine();
                }
            }

            System.IO.File.WriteAllText(path, data.ToString());
        }
Esempio n. 13
0
        public static void ExportData(Mfe.Font font, string path)
        {
            List <byte> bytes = new List <byte>();

            // Version
            appendWord(bytes, 0x0200);
            // Location
            int dfSizeLocation = bytes.Count;

            appendDWord(bytes, 0);
            // Copyright
            for (int i = 0; i < 60; i++)
            {
                appendByte(bytes, 0);
            }
            // dfType
            appendWord(bytes, 0);
            // dfPoints
            appendWord(bytes, (short)(font.BaseLine - font.CapHeight));
            // dfVertRes, dfHorizRes
            appendWord(bytes, 96);
            appendWord(bytes, 96);
            // dfAscent
            appendWord(bytes, font.BaseLine);
            // dfInternalLeading
            appendWord(bytes, 0);
            // dfExternalLeading
            appendWord(bytes, 0);
            // dfItalic
            appendByte(bytes, font.StyleObliqueness != Font.Obliqueness.Upright ? (byte)1 : (byte)0);
            // dfUnderline
            appendByte(bytes, 0);
            // dfStrikeOut
            appendByte(bytes, 0);
            // dfWeight
            appendWord(bytes, 0);
            // dfCharSet
            appendByte(bytes, 0);
            // dfPixWidth
            appendWord(bytes, 0);
            // dfPixHeight
            appendWord(bytes, font.Height);
            // dfPitchAndFamily
            appendByte(bytes, 0);
            // dfAvgWidth
            appendWord(bytes, font.Width);
            // dfMaxWidth
            appendWord(bytes, font.Glyphs.Max(x => x.Width));
            // dfFirstChar
            appendByte(bytes, (byte)font.FirstCodePoint);
            // dfLastChar
            appendByte(bytes, (byte)font.LastCodePoint);
            // dfDefaultChar
            appendByte(bytes, font.FirstCodePoint <= (byte)' ' && (byte)' ' <= font.LastCodePoint ? (byte)' ' : (byte)font.FirstCodePoint);
            // dfBreakChar
            appendByte(bytes, 0);
            // dfWidthBytes, I think this is the right way to compute this. . . .
            appendWord(bytes, (short)(((font.Glyphs.Sum(x => x.Width) - 1) >> 3) + 1));
            // dfDevice
            appendDWord(bytes, 0);
            // dfFace
            int dfFaceLocation = bytes.Count;

            appendDWord(bytes, 0);
            // dfBitsPointer
            appendDWord(bytes, 0);
            // dfBitsOffset
            appendDWord(bytes, 0);
            // dfReserved
            appendByte(bytes, 0);
            // Serialize widths table
            ushort[] bitmapLocations = new ushort[font.LastCodePoint - font.FirstCodePoint + 1];
            for (int i = font.FirstCodePoint; i <= font.LastCodePoint; i++)
            {
                appendWord(bytes, font[i].Width);
                bitmapLocations[i] = (ushort)bytes.Count;
                appendWord(bytes, 0);
            }
            // Serialize bitmaps
            for (int i = font.FirstCodePoint; i <= font.LastCodePoint; i++)
            {
                Char ch = font[i];
                writeWord(bytes, bitmapLocations[i], (short)bytes.Count);
                int byteColumns = ((ch.Width - 1) >> 3) + 1;
                if (byteColumns > 0)
                {
                    for (int col = 0; col < byteColumns; col++)
                    {
                        for (int y = 0; y < ch.Height; y++)
                        {
                            int data = 0;
                            for (int b = 0; b <= (col == byteColumns - 1 ? ((ch.Width - 1) & 7) + 1: 7); b++)
                            {
                                data |= (ch[y, col * 8 + b] ? 1 : 0) << (7 - b);
                            }
                            appendByte(bytes, (byte)data);
                        }
                    }
                }
            }
            // Serialize name
            writeDWord(bytes, dfFaceLocation, bytes.Count);
            string name = font["Name"] ?? "MFE Font";

            for (int i = 0; i < name.Length; i++)
            {
                appendByte(bytes, (byte)name[i]);
            }
            appendByte(bytes, 0);
            writeDWord(bytes, dfSizeLocation, bytes.Count);
            System.IO.File.WriteAllBytes(path, bytes.ToArray());
        }
Esempio n. 14
0
        public static void ExportData(Mfe.Font font, string path)
        {
            byte[] data = new byte[65536];
            int    dataLength = 0;
            int    row, col;
            bool   Inverted = font["Inverted"] == "true";

            //string baseName = font["AsmName"] ?? "font";
            // Font format version
            data[dataLength++] = 0;
            // Height
            data[dataLength++] = font.Height;
            // Glyph count
            data[dataLength++] = (byte)(font.LastCodePoint - font.FirstCodePoint + 1);
            // First codepoint
            data[dataLength++] = (byte)font.FirstCodePoint;
            int locationOfWidthsTable = dataLength;

            dataLength += 3;
            int locationOfBitmapsTable = dataLength;

            dataLength        += 3;
            data[dataLength++] = font.ItalicSpaceAdjust;
            data[dataLength++] = font.SpaceAbove;
            data[dataLength++] = font.SpaceBelow;
            data[dataLength++] = font.Weight;
            data[dataLength++] = font.Style;
            data[dataLength++] = font.CapHeight;
            data[dataLength++] = font.XHeight;
            data[dataLength++] = font.BaseLine;
            // Widths table
            data[locationOfWidthsTable++] = (byte)(dataLength & 0xFF);
            data[locationOfWidthsTable++] = 0;
            data[locationOfWidthsTable++] = 0;
            for (int i = font.FirstCodePoint; i <= font.LastCodePoint; i++)
            {
                data[dataLength++] = font[i].Width;
            }
            // Bitmaps locations table
            data[locationOfBitmapsTable++] = (byte)(dataLength & 0xFF);
            data[locationOfBitmapsTable++] = (byte)((dataLength >> 8) & 0xFF);
            data[locationOfBitmapsTable++] = 0;
            int locationOfNextBitmapOffset = dataLength;

            dataLength += (font.LastCodePoint - font.FirstCodePoint + 1) * 2;
            // Bitmaps
            for (int i = font.FirstCodePoint; i <= font.LastCodePoint; i++)
            {
                Char ch           = font[i];
                int  bitmapoffset = -2 + ((ch.Width > 0 ? ch.Width : 1) - 1) / 8;
                bitmapoffset += dataLength;
                data[locationOfNextBitmapOffset++] = (byte)(bitmapoffset & 0xFF);
                data[locationOfNextBitmapOffset++] = (byte)((bitmapoffset >> 8) & 0xFF);
                //data[locationOfNextBitmapOffset++] = (byte)(bitmapoffset >> 16);
                if (ch.Width > 24)
                {
                    throw new ArgumentException("This format does not support glyphs wider than 24 pixels.");
                }
                for (row = 0; row < font.Height; row++)
                {
                    // Output distinct bytes, but in little-endian order
                    // The format also requires omitting the least-significant byte(s) if they're unused,
                    // so we have to output the bytes from right-to-left, and the bits from left-to-right.
                    for (int bit = ((ch.Width > 0 ? ch.Width : 1) - 1) & ~7; bit >= 0; bit -= 8) // Initializer gives 0 for 0-8, 8 for 9-16, 16 for 17-24
                    {
                        int b = 0;
                        for (col = 0; col < 8; col++)
                        {
                            if (bit + col >= ch.Width)
                            {
                                b = (b << 1) | 0;
                            }
                            else if (ch[row, bit + col] ^ Inverted)
                            {
                                b = (b << 1) | 1;
                            }
                            else
                            {
                                b = (b << 1) | 0;
                            }
                        }
                        data[dataLength++] = (byte)b;
                    }
                }
            }
            // Convert binary data into string
            StringBuilder str = new StringBuilder();

            for (int i = 0; i < dataLength; i++)
            {
                str.Append("0x");
                str.Append(data[i].ToString("X2"));
                str.Append(", ");
                if (i % 16 == 15)
                {
                    str.AppendLine();
                }
            }
            System.IO.File.WriteAllText(path, str.ToString());
        }
Esempio n. 15
0
        public static void ExportData(Mfe.Font font, string path)
        {
            // Check to make sure that it is an exportable font.
            if (font.VariableWidth)
            {
                throw new ArgumentException("Font must fixed-width.");
            }
            if (font.Height != 6 || font.Width != 4)
            {
                throw new ArgumentException("Font size must be 4x6.");
            }
            // Strings in C# are immutable, so doing "xxx" + "yyy" + "zzz" will end up creating five strings.
            // StringBuilder solves this problem of excessive string allocation.
            // For best performance, this number should be a little larger than the estimated font size for.
            StringBuilder data = new StringBuilder(65536);
            // I dunno, maybe you want to invert the font?
            bool inverted = font["Inverted"] == "true";

            // Font header
            data.Append("; FONT METADATA");
            foreach (KeyValuePair <string, string> s in font.AboutData)
            {
                data.Append("; ");
                data.Append(s.Key);
                data.Append(" = ");
                data.Append(s.Value);
                data.AppendLine();
            }
            data.AppendLine("; ");
            data.AppendLine("; FONT DATA");
            // Iterate over codepoints
            for (int i = 0; i < 256; i++)
            {
                // Header for each character
                data.Append("; Char ");
                data.Append(i.ToString("X2"));
                data.Append(" ");
                data.Append(font[i].Codepoint.ToString());
                data.Append(" ");
                data.Append(font[i].Name);
                data.Append(" ");
                data.AppendLine();
                // Produce bytes
                for (int row = 0; row < font[i].Height; row += 2)
                {
                    Char ch = font[i];
                    // \x9 creates a tab character
                    data.Append("\x9.db\x9$");
                    // I could have used a loop, but this required less thinking.
                    data.Append((
                                    (
                                        (ch[row, 0] ? 1 << 7 : 0)
                                        | (ch[row, 1] ? 1 << 6 : 0)
                                        | (ch[row, 2] ? 1 << 5 : 0)
                                        | (ch[row, 3] ? 1 << 4 : 0)
                                        | (ch[row + 1, 0] ? 1 << 3 : 0)
                                        | (ch[row + 1, 1] ? 1 << 2 : 0)
                                        | (ch[row + 1, 2] ? 1 << 1 : 0)
                                        | (ch[row + 1, 3] ? 1 : 0)
                                    )
                                    ^
                                    (
                                        inverted ? 0xFF : 0
                                    )
                                    ).ToString("X2")
                                );
                    data.AppendLine();
                }
            }

            System.IO.File.WriteAllText(path, data.ToString());
        }
Esempio n. 16
0
        public static void ExportData(Mfe.Font font, string path)
        {
            StringBuilder data = new StringBuilder(65536);
            int           row, col;
            bool          Inverted = font["Inverted"] == "true";
            string        baseName = font["AsmName"] ?? "font";
            int           firstChar;
            int           lastChar;

            if (!Int32.TryParse((font["FirstChar"] ?? "0"), out firstChar))
            {
                firstChar = 0;
            }
            if (!Int32.TryParse((font["LastChar"] ?? "255"), out lastChar))
            {
                firstChar = 255;
            }
            if (firstChar < 0)
            {
                firstChar = 0;
            }
            if (lastChar > 255)
            {
                lastChar = 255;
            }
            data.Append(baseName);
            data.AppendLine(":");
            data.AppendLine("' FONT METADATA");
            foreach (KeyValuePair <string, string> s in font.AboutData)
            {
                data.Append("' ");
                data.Append(s.Key);
                data.Append(" = ");
                data.Append(s.Value);
                data.AppendLine();
            }
            data.AppendLine("' font height");
            data.Append("DATA ");
            data.Append(font.Height);
            data.AppendLine();
            data.AppendLine("' ");
            data.AppendLine("' GLYPH DATA TABLE");
            for (int i = firstChar; i <= lastChar; i++)
            {
                // Header
                data.Append("' ");
                data.Append(i.ToString("X2"));
                if (i > 32 && i < 128)
                {
                    data.Append(" ");
                    data.Append((char)i);
                }
                // data.Append(" ");
                //data.Append(font[i].Name);
                data.AppendLine();
                data.Append("DATA ");
                data.Append(font[i].Width);
                data.Append(",");
                col = 0;
                row = 0;
                // Produce bitmap
                for (row = 0; row < font[i].Height; row++)
                {
                    //data.Append("&H");
                    short b = 0;
                    for (col = 0; col < font[i].Width; col++)
                    {
                        b |= (short)(font[i][row, col] ?  1 << (15 - col) : 0);
                    }
                    data.Append(b.ToString(""));
                    if (row != font[i].Height - 1)
                    {
                        data.Append(",");
                    }
                    else
                    {
                        data.AppendLine();
                    }
                }
            }
            System.IO.File.WriteAllText(path, data.ToString());
        }
Esempio n. 17
0
        public static void ExportData(Mfe.Font font, string path)
        {
            StringBuilder data = new StringBuilder(65536);
            int           row, col, remainder;
            bool          Inverted = font["Inverted"] == "true";
            string        baseName = font["AsmName"] ?? "font";

            data.Append(baseName);
            data.AppendLine(":");
            data.AppendLine("; FONT METADATA");
            foreach (KeyValuePair <string, string> s in font.AboutData)
            {
                data.Append("; ");
                data.Append(s.Key);
                data.Append(" = ");
                data.Append(s.Value);
                data.AppendLine();
            }
            data.Append("\x9.db\x9");
            data.Append(font.Height);
            data.Append("\x9; font height");
            data.AppendLine();
            data.AppendLine("; ");
            data.AppendLine("; GLYPH WIDTH TABLE");
            data.Append(baseName);
            data.Append("WidthTable:");
            data.AppendLine();
            for (int i = 0; i < 256; i++)
            {
                data.Append("\x9.db\x9");
                data.Append(font[i].Width);
                data.Append("\x9; ");
                data.Append(font[i].Codepoint);
                data.Append(" ");
                data.Append(font[i].Name);
                data.AppendLine();
            }
            data.AppendLine("; ");
            data.AppendLine("; GLYPH DATA TABLE");
            data.Append(baseName);
            data.Append("DataTable:");
            data.AppendLine();
            for (int i = 0; i < 256; i++)
            {
                data.Append("\x9.dw\x9");
                data.Append(baseName);
                data.Append("Char");
                data.Append(i.ToString("X2"));
                data.Append("\x9; ");
                data.Append(font[i].Codepoint);
                data.Append(" ");
                data.Append(font[i].Name);
                data.AppendLine();
            }
            data.AppendLine("; ");
            data.AppendLine("; GLYPH DATA");
            for (int i = 0; i < 256; i++)
            {
                // Header
                data.Append(baseName);
                data.Append("Char");
                data.Append(i.ToString("X2"));
                data.Append(": ; ");
                data.Append(font[i].Codepoint);
                data.Append(" ");
                data.Append(font[i].Name);
                data.AppendLine();
                col       = 0;
                row       = 0;
                remainder = font[i].Width * font[i].Height;
                // Number of bytes in glyph body
                data.Append("\x9.db\x9");
                if (remainder % 8 == 0)
                {
                    data.Append(remainder / 8 - 1);
                }
                else
                {
                    data.Append(remainder / 8);
                }
                data.Append(" ; body byte count");
                data.AppendLine();
                // Producy body bytes
                while (remainder > 8)
                {
                    data.Append("\x9.db\x9");
                    data.Append(ToBinaryByte(getNextByte(font[i], Inverted, ref row, ref col, out remainder)));
                    data.Append("b");
                    data.AppendLine();
                }
                // Number of bits in final byte
                data.Append("\x9.db\x9");
                data.Append(remainder);
                data.Append(" ; remaining bits in final byte");
                data.AppendLine();
                // Final byte
                data.Append("\x9.db\x9");
                data.Append(ToBinaryByte(getNextByte(font[i], Inverted, ref row, ref col, out remainder)));
                data.Append("b");
                data.AppendLine();
                if (remainder != 0)
                {
                    //System.IO.File.WriteAllText(path, data.ToString());
                    throw new Exception("Internal consistency error: expected remaining bits should have been zero, was " + remainder + " on char #" + i.ToString()
                                        + " with col=" + col.ToString() + " and row=" + row.ToString());
                }
            }
            System.IO.File.WriteAllText(path, data.ToString());
        }