Ejemplo n.º 1
0
 public CodeFactory(FontFile fontFile)
 {
     this.FontFile = fontFile;
 }
Ejemplo n.º 2
0
        public async Task <FontFile> Import()
        {
            FontFile returnValue = new FontFile();

            returnValue.Items = new List <Glyph>();

            IList <string> lines = await FileIO.ReadLinesAsync(this.File);

            bool          readingBitmapBytes = false;
            bool          readingGlyphs      = false;
            StringBuilder hexString          = new StringBuilder();
            int           asc = 0x20;

            foreach (string line in lines)
            {
                if (!readingBitmapBytes && line.Contains("const uint8_t") && line.Contains("PROGMEM = {"))
                {
                    readingBitmapBytes = true;
                }
                else if (readingBitmapBytes)
                {
                    if (line.Contains("};"))
                    {
                        readingBitmapBytes = false;
                    }
                    else if (!line.StartsWith("#"))
                    {
                        if (line.Contains("/*"))
                        {
                            int pos = line.IndexOf("/*");
                            hexString.Append(line.Substring(0, pos).Trim());
                        }
                        else
                        {
                            hexString.Append(line.Trim());
                        }
                    }
                }
                else if (!readingGlyphs && line.Contains("const GFXglyph") && line.Contains("PROGMEM = {"))
                {
                    readingGlyphs = true;
                }
                else if (readingGlyphs)
                {
                    if (line.Contains("};"))
                    {
                        readingGlyphs = false;
                    }
                    else if (!String.IsNullOrEmpty(line) && !line.StartsWith("#"))
                    {
                        int startPos = line.IndexOf("{");
                        int endPos   = line.IndexOf("}");

                        string   stringValue = line.Substring(startPos + 1, endPos - startPos - 1);
                        string[] values      = stringValue.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        char     c           = Convert.ToChar(asc);

                        Glyph glyph = new Glyph()
                        {
                            Key          = Convert.ToString(c),
                            BitmapOffset = Convert.ToInt32(values[0]),
                            Width        = Convert.ToInt32(values[1]),
                            Height       = Convert.ToInt32(values[2]),
                            xAdvance     = Convert.ToInt32(values[3]),
                            xOffset      = Convert.ToInt32(values[4]),
                            yOffset      = Convert.ToInt32(values[5]),
                            AsciiCode    = asc
                        };

                        ((IList <Glyph>)returnValue.Items).Add(glyph);

                        asc++;
                    }
                }
            }

            // ***
            // *** Convert the bitmap array.
            // ***
            byte[] bitmapArray = this.ConvertHexString(hexString.ToString());

            // ***
            // *** Check the byte array count.
            // ***
            int expectedByteCount = returnValue.Items.Sum(t => t.Height);

            if (expectedByteCount == bitmapArray.Count())
            {
                foreach (Glyph glyph in returnValue.Items)
                {
                    glyph.FontBitmap.AddRange(bitmapArray.Skip(glyph.BitmapOffset).Take(glyph.Height));
                }
            }

            returnValue.FontWidth  = returnValue.Items.Max(t => t.Width);
            returnValue.FontHeight = returnValue.Items.Max(t => t.Height);

            return(returnValue);
        }