Added cropping of image

There is still a bug with reflection
This commit is contained in:
Flamefire committed 2013-04-30 16:23:39 +02:00
1 parent e602f55bb4
commit 8ffff107e2
1 file changed
+33 -19
+33 -19
View File
@@ -32,7 +32,7 @@ namespace Vocaluxe.Base.Font
{ {
private STexture _Texture; private STexture _Texture;
private readonly SizeF _BoundingBox; private readonly SizeF _BoundingBox;
private readonly Point _Offset; private readonly RectangleF _DrawBounding;
public float Width public float Width
{ {
@@ -54,30 +54,35 @@ namespace Vocaluxe.Base.Font
CFonts.Height = maxHigh; // *factor; CFonts.Height = maxHigh; // *factor;
//float factor = _GetFactor(chr, flags); //float factor = _GetFactor(chr, flags);
System.Drawing.Font fo = CFonts.GetFont(); System.Drawing.Font fo = CFonts.GetFont();
SizeF fullSize; SizeF fullSize, boundingSize;
using (Graphics g = Graphics.FromHwnd(IntPtr.Zero)) using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
{ {
fullSize = g.MeasureString(chrString, fo); fullSize = g.MeasureString(chrString, fo);
fullSize.Width += outlineSize; fullSize.Width += outlineSize;
if (chr != ' ') if (chr != ' ')
{ {
SizeF boundingSize = g.MeasureString(chrString, fo, -1, new StringFormat(StringFormat.GenericTypographic)); boundingSize = g.MeasureString(chrString, fo, -1, new StringFormat(StringFormat.GenericTypographic));
boundingSize.Width += outlineSize; boundingSize.Width += outlineSize;
_BoundingBox = boundingSize;
} }
else else
{ {
_BoundingBox = fullSize; boundingSize = fullSize;
fullSize.Width = 1; fullSize.Width = 1;
fullSize.Height = 1; fullSize.Height = 1;
} }
_BoundingBox = boundingSize;
} }
using (Bitmap bmp = new Bitmap((int)(fullSize.Width), (int)(fullSize.Height), PixelFormat.Format32bppArgb)) using (Bitmap bmp = new Bitmap((int)(fullSize.Width), (int)(fullSize.Height), PixelFormat.Format32bppArgb))
using (Graphics g = Graphics.FromImage(bmp))
{ {
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.Transparent); g.Clear(Color.Transparent);
if (chr != ' ') if (chr == ' ')
{
_Texture = CDraw.AddTexture(bmp);
_DrawBounding = new RectangleF(0, 0, 1, 1);
}
else
{ {
g.SmoothingMode = SmoothingMode.AntiAlias; g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.InterpolationMode = InterpolationMode.HighQualityBicubic;
@@ -96,18 +101,22 @@ namespace Vocaluxe.Base.Font
g.FillPath(Brushes.White, path); g.FillPath(Brushes.White, path);
} }
} }
} Rectangle realBounds = _GetRealBounds(bmp);
_Texture = CDraw.AddTexture(bmp); _DrawBounding = realBounds;
float dx = (fullSize.Width - boundingSize.Width - 1) / 2;
_DrawBounding.X -= dx;
using (Bitmap bmpCropped = bmp.Clone(realBounds, PixelFormat.Format32bppArgb))
{
_Texture = CDraw.AddTexture(bmpCropped);
if (false) if (false)
{ {
if (outline > 0) if (outline > 0)
bmp.Save("font/" + chr + "o" + CFonts.Style + "2.png", ImageFormat.Png); bmpCropped.Save("font/" + chr + "o" + CFonts.Style + "2.png", ImageFormat.Png);
else else
bmp.Save("font/" + chr + CFonts.Style + "2.png", ImageFormat.Png); bmpCropped.Save("font/" + chr + CFonts.Style + "2.png", ImageFormat.Png);
}
}
} }
g.Dispose();
} }
fo.Dispose(); fo.Dispose();
CFonts.Height = oldHeight; CFonts.Height = oldHeight;
@@ -126,12 +135,17 @@ namespace Vocaluxe.Base.Font
public void GetTextureAndRect(float x, float y, float z, out STexture texture, out SRectF rect) public void GetTextureAndRect(float x, float y, float z, out STexture texture, out SRectF rect)
{ {
texture = _Texture; texture = _Texture;
rect = new SRectF(x, y, Width, Height, z); float factor = _GetFactor();
x += _DrawBounding.X * factor;
y += _DrawBounding.Y * factor;
float w = _DrawBounding.Width * factor;
float h = _DrawBounding.Height * factor;
rect = new SRectF(x, y, w, h, z);
} }
private Point _TrimBmp(ref Bitmap bmp) private static Rectangle _GetRealBounds(Bitmap bmp)
{ {
int minX = 0, minY = 0, maxX = bmp.Width - 1, maxY = bmp.Width - 1; int minX = 0, minY = 0, maxX = bmp.Width - 1, maxY = bmp.Height - 1;
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
int values = bmpData.Width * bmp.Height; int values = bmpData.Width * bmp.Height;
Int32[] rgbValues = new Int32[values]; Int32[] rgbValues = new Int32[values];
@@ -158,6 +172,7 @@ namespace Vocaluxe.Base.Font
//find from bottom //find from bottom
for (int y = bmp.Height - 1; y >= minY && !found; y--) for (int y = bmp.Height - 1; y >= minY && !found; y--)
{ {
index = y * bmp.Width;
for (int x = 0; x < bmp.Width; x++) for (int x = 0; x < bmp.Width; x++)
{ {
if (rgbValues[index] != 0) if (rgbValues[index] != 0)
@@ -209,8 +224,7 @@ namespace Vocaluxe.Base.Font
if (!found) if (!found)
break; break;
} }
bmp = bmp.Clone(new Rectangle(minX, minY, maxX - minX, maxY - minY), PixelFormat.Format32bppArgb); return new Rectangle(minX, minY, maxX - minX + 1, maxY - minY + 1);
return new Point(minX, minY);
} }
private float _GetStyleFactor(char chr, TextFormatFlags flags) private float _GetStyleFactor(char chr, TextFormatFlags flags)