Exemple #1
0
        private List <Glyph> ExtractGlyphs(PixelBitmapContent <Color> bitmap)
        {
            var glyphs  = new List <Glyph>();
            var regions = new List <Rectangle>();

            int width  = bitmap.Width;
            var pixels = bitmap.GetPixelSpan();

            for (int y = 0; y < bitmap.Height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    if (pixels[x + y * width] != _transparentPixel)
                    {
                        // if we don't have a region that has this pixel already
                        var re = regions.Find(r =>
                        {
                            return(r.Contains(x, y));
                        });

                        if (re == Rectangle.Empty)
                        {
                            // we have found the top, left of a image.
                            // we now need to scan for the 'bounds'
                            int top    = y;
                            int bottom = y;
                            int left   = x;
                            int right  = x;

                            while (pixels[right + bottom * width] != _transparentPixel)
                            {
                                right++;
                            }

                            while (pixels[left + bottom * width] != _transparentPixel)
                            {
                                bottom++;
                            }

                            // we got a glyph :)
                            regions.Add(new Rectangle(left, top, right - left, bottom - top));
                            x = right;
                        }
                        else
                        {
                            x += re.Width;
                        }
                    }
                }
            }

            for (int i = 0; i < regions.Count; i++)
            {
                var rect      = regions[i];
                var newBitmap = new PixelBitmapContent <Color>(rect.Width, rect.Height);
                BitmapContent.Copy(bitmap, rect, newBitmap, new Rectangle(0, 0, rect.Width, rect.Height));

                var glyph = new Glyph(GetCharacterForIndex(i), newBitmap);
                glyph.CharacterWidths.B = glyph.Bitmap.Width;
                glyphs.Add(glyph);
            }
            return(glyphs);
        }