Example #1
0
        /// <summary>
        /// Generates characters from the given dictionary and font.
        /// </summary>
        /// <param name="dictionary">Dictionary containing max 2 chars per string.</param>
        /// <param name="font">.NET font</param>
        /// <returns></returns>
        public bool Generate(string[] dictionary, Font font)
        {
            foreach (FontCharacter fontCharacter in Characters)
            {
                fontCharacter.Data = new byte[32];
            }
            for (int i = 0; i < dictionary.Length; i++)
            {
                FontCharacter fontCharacter = Characters[i];
                fontCharacter.Symbol = dictionary[i];

                Bitmap bitmap = fontCharacter.GetBitmap();
                using (Graphics graphic = Graphics.FromImage(bitmap))
                {
                    RectangleF firstChar  = new RectangleF(-2, 0, 16, 16);
                    RectangleF secondChar = new RectangleF(6, 0, 16, 16);

                    graphic.SmoothingMode     = SmoothingMode.None;
                    graphic.InterpolationMode = InterpolationMode.NearestNeighbor;
                    graphic.PixelOffsetMode   = PixelOffsetMode.None;
                    graphic.DrawString("" + fontCharacter.Symbol[0], font, Brushes.White, firstChar);
                    if (fontCharacter.Symbol.Length == 1)
                    {
                        continue;                                   //should not appear actually
                    }
                    graphic.DrawString("" + fontCharacter.Symbol[1], font, Brushes.White, secondChar);
                }
                fontCharacter.SetBitmap(bitmap);
            }
            return(true);
        }
Example #2
0
        private void UpdatePreview()
        {
            _scriptFile.FontName = comboBox_Font.Text;
            _scriptFile.FontSize = (int)numericUpDown_FontSize.Value;
            Font font = _scriptFile.Font;

            if (String.IsNullOrWhiteSpace(textBox_PreviewText.Text))
            {
                List <Bitmap> allowedSymbolsBitmap = new List <Bitmap>();
                foreach (string line in textBox_AllowedSymbols.Lines)
                {
                    foreach (char character in line)
                    {
                        FontCharacter fontCharacter = new FontCharacter(new FontSymbol(character), font);
                        allowedSymbolsBitmap.Add(fontCharacter.GetBitmap());
                    }
                }
                foreach (string line in textBox_AllowedSplittedSymbols.Lines)
                {
                    foreach (char character in line)
                    {
                        FontCharacter fontCharacter = new FontCharacter(new FontSymbol(character, character), font);
                        allowedSymbolsBitmap.Add(fontCharacter.GetBitmap());
                    }
                }
                pictureBox_Preview.Image = Graphic.CombineBitmaps(allowedSymbolsBitmap, 16);
            }
            else
            {
                List <Bitmap> previewBitmap = new List <Bitmap>();
                foreach (string line in textBox_PreviewText.Lines)
                {
                    for (int i = 0; i < line.Length; i += 2)
                    {
                        FontCharacter fontCharacter;
                        char          firstChar  = line[i];
                        char          secondChar = ' ';
                        if (i + 1 < line.Length)
                        {
                            secondChar = line[i + 1];
                        }

                        if (!_scriptFile.AllowedSplittedSymbols.Contains(firstChar))
                        {
                            firstChar = ' ';
                        }

                        if (!_scriptFile.AllowedSplittedSymbols.Contains(secondChar))
                        {
                            secondChar = ' ';
                        }

                        fontCharacter = new FontCharacter(new FontSymbol(firstChar, secondChar), font);
                        previewBitmap.Add(fontCharacter.GetBitmap());
                    }
                }
                pictureBox_Preview.Image = Graphic.CombineBitmaps(previewBitmap, 16);
            }
        }
Example #3
0
        public Glyph?GetGlyph(uint Unicode)
        {
            if (GlyphTexture.ContainsKey(Unicode))
            {
                return(GlyphTexture[Unicode]);
            }

            FontCharacter FChar = new FontCharacter();

            if (Msdfgen.LoadGlyphNormal(Fnt, Unicode, ref FChar))
            {
                if (FChar.Width == 0 || FChar.Height == 0)
                {
                    if (!GlyphTexture.ContainsKey(Unicode))
                    {
                        GlyphTexture.Add(Unicode, new Glyph(FChar, null));
                    }

                    return(GetGlyph(Unicode));
                }

                Glyph G = new Glyph(FChar, FChar.GetBitmap());
                if (!Packer.Pack(G.Bitmap.Width, G.Bitmap.Height, out G.X, out G.Y))
                {
                    throw new NotImplementedException("Cannot pack glyph");
                }

                G.Bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
                TextureAtlas.SubRect2D(G.Bitmap, G.X, G.Y);
                G.Bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);

                GlyphTexture.Add(Unicode, G);
                return(GetGlyph(Unicode));
            }

            return(null);
        }