Esempio n. 1
0
        private static void BlitCharacter(int pixelSize, int maxWidth, FullDictionary <char, CharacterInfo> dict, ref int currentX, ref int currentY, Graphics g, FontFace typeface, char c)
        {
            if (c == ' ')
            {
                int width = pixelSize / 3;
                if (currentX + xInterval + width >= maxWidth)
                {
                    currentX  = 0;
                    currentY += yInterval + pixelSize;
                    if (currentY + yInterval + pixelSize >= maxWidth)
                    {
                        throw new Exception("Texture Size not big enough for reuqired characters.");
                    }
                }
                Bitmap glyphBitmap = new Bitmap(width + xInterval, pixelSize + yInterval);
                //float yoffset = pixelSize * 3 / 4 - glyph.HorizontalMetrics.Bearing.Y;
                g.DrawImage(glyphBitmap, currentX + xInterval, currentY + yInterval);
                CharacterInfo info = new CharacterInfo(currentX, currentY, width + xInterval, pixelSize + yInterval);
                dict.Add(c, info);
                glyphBitmap.Dispose();
                currentX += width;
            }
            else
            {
                Surface surface; Glyph glyph;
                if (RenderGlyph(typeface, c, pixelSize, out surface, out glyph))
                {
                    if (currentX + xInterval + surface.Width >= maxWidth)
                    {
                        currentX  = 0;
                        currentY += yInterval + pixelSize;
                        if (currentY + yInterval + pixelSize >= maxWidth)
                        {
                            throw new Exception("Texture Size not big enough for reuqired characters.");
                        }
                    }
                    Bitmap    glyphBitmap = GetGlyphBitmap(surface);
                    const int a           = 5;
                    const int b           = 8;
                    //float yoffset = pixelSize * a / b - glyph.HorizontalMetrics.Bearing.Y;
#if DEBUG
                    g.DrawRectangle(redPen, currentX + xInterval, currentY + yInterval + pixelSize * a / b - glyph.HorizontalMetrics.Bearing.Y, glyphBitmap.Width, glyphBitmap.Height);
                    g.DrawRectangle(greenPen, currentX, currentY, glyphBitmap.Width + xInterval, yInterval + pixelSize - 1);
#endif
                    g.DrawImage(glyphBitmap, currentX + xInterval, currentY + yInterval + pixelSize * a / b - glyph.HorizontalMetrics.Bearing.Y, glyphBitmap.Width, glyphBitmap.Height);
                    CharacterInfo info = new CharacterInfo(currentX, currentY, glyphBitmap.Width + xInterval, yInterval + pixelSize - 1);
                    dict.Add(c, info);
                    glyphBitmap.Dispose();
                    currentX += xInterval + surface.Width;
                }

                surface.Dispose();
            }
        }
Esempio n. 2
0
        private static FullDictionary <char, GlyphInfo> GetGlyphDict(Font font, string charSet)
        {
            var dict = new FullDictionary <char, GlyphInfo>(GlyphInfo.Default);

            using (var bmp = new Bitmap(1, 1))
            {
                using (var graphics = Graphics.FromImage(bmp))
                {
                    foreach (var c in charSet)
                    {
                        Size oneSize    = graphics.MeasureString(string.Format("{0}", c), font).ToSize();
                        Size doubleSize = graphics.MeasureString(string.Format("{0}{0}", c), font).ToSize();

                        // TODO: Log this.
                        if (oneSize.Height != doubleSize.Height)
                        {
                            continue;
                        }
                        if (oneSize.Width >= doubleSize.Width)
                        {
                            continue;
                        }

                        Size charSize = new Size(doubleSize.Width - oneSize.Width, oneSize.Height);
                        var  info     = new GlyphInfo(0, 0, charSize.Width, charSize.Height);
                        dict.Add(c, info);
                    }
                }
            }

            return(dict);
        }
Esempio n. 3
0
        public static FullDictionary <char, CharacterInfo> Parse(XElement xElement)
        {
            FullDictionary <char, CharacterInfo> result = new FullDictionary <char, CharacterInfo>(
                CharacterInfo.Default);

            foreach (var item in xElement.Elements(KeyValuePairHelper.strKeyValuePair))
            {
                KeyValuePair <char, CharacterInfo> pair = KeyValuePairHelper.Parse(item);
                result.Add(pair.Key, pair.Value);
            }

            return(result);
        }