public static void CreateBitmapPerGlyph(GLFontGlyph[] sourceGlyphs, GLFontBitmap[] sourceBitmaps, out GLFontGlyph[] destGlyphs, out GLFontBitmap[] destBitmaps) { destBitmaps = new GLFontBitmap[sourceGlyphs.Length]; destGlyphs = new GLFontGlyph[sourceGlyphs.Length]; for (int i = 0; i < sourceGlyphs.Length; i++) { var sg = sourceGlyphs[i]; destGlyphs[i] = new GLFontGlyph(i, new Rectangle(0, 0, sg.Rect.Width, sg.Rect.Height), sg.YOffset, sg.Character); destBitmaps[i] = new GLFontBitmap(new Bitmap(sg.Rect.Width, sg.Rect.Height, PixelFormat.Format32bppArgb)); GLFontBitmap.Blit(sourceBitmaps[sg.Page].bitmapData, destBitmaps[i].bitmapData, sg.Rect, 0, 0); } }
//The initial bitmap is simply a long thin strip of all glyphs in a row private Bitmap CreateInitialBitmap(Font font, SizeF maxSize, int initialMargin, out GLFontGlyph[] glyphs, GLFontRenderHint renderHint) { glyphs = new GLFontGlyph[charSet.Length]; int spacing = (int)Math.Ceiling(maxSize.Width) + 2 * initialMargin; Bitmap bmp = new Bitmap(spacing * charSet.Length, (int)Math.Ceiling(maxSize.Height) + 2 * initialMargin + 1, PixelFormat.Format24bppRgb); Graphics graph = Graphics.FromImage(bmp); switch (renderHint) { case GLFontRenderHint.SizeDependent: graph.TextRenderingHint = font.Size <= 12.0f ? TextRenderingHint.ClearTypeGridFit : TextRenderingHint.AntiAlias; break; case GLFontRenderHint.AntiAlias: graph.TextRenderingHint = TextRenderingHint.AntiAlias; break; case GLFontRenderHint.AntiAliasGridFit: graph.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; break; case GLFontRenderHint.ClearTypeGridFit: graph.TextRenderingHint = TextRenderingHint.ClearTypeGridFit; break; case GLFontRenderHint.SystemDefault: graph.TextRenderingHint = TextRenderingHint.SystemDefault; break; } int xOffset = initialMargin; for (int i = 0; i < charSet.Length; i++) { graph.DrawString("" + charSet[i], font, Brushes.White, xOffset, initialMargin); var charSize = graph.MeasureString("" + charSet[i], font); glyphs[i] = new GLFontGlyph(0, new Rectangle(xOffset - initialMargin, 0, (int)charSize.Width + initialMargin * 2, (int)charSize.Height + initialMargin * 2), 0, charSet[i]); xOffset += (int)charSize.Width + initialMargin * 2; } graph.Flush(); graph.Dispose(); return(bmp); }
private static int Kerning(GLFontGlyph g1, GLFontGlyph g2, XLimits[] lim1, XLimits[] lim2, GLFontKerningConfiguration config) { int yOffset1 = g1.YOffset; int yOffset2 = g2.YOffset; int startY = Math.Max(yOffset1, yOffset2); int endY = Math.Min(g1.Rect.Height + yOffset1, g2.Rect.Height + yOffset2); int w1 = g1.Rect.Width; int worstCase = w1; //TODO - offset startY, endY by yOffset1 so that lim1[j-yOffset1] can be written as lim1[j], will need another var for yOffset2 for (int j = startY; j < endY; j++) { worstCase = Math.Min(worstCase, w1 - lim1[j - yOffset1].Max + lim2[j - yOffset2].Min); } worstCase = Math.Min(worstCase, g1.Rect.Width); worstCase = Math.Min(worstCase, g2.Rect.Width); //modify by character kerning rules GLFontCharacterKerningRule kerningRule = config.GetOverridingCharacterKerningRuleForPair("" + g1.Character + g2.Character); if (kerningRule == GLFontCharacterKerningRule.Zero) { return(0); } else if (kerningRule == GLFontCharacterKerningRule.NotMoreThanHalf) { return((int)Math.Min(Math.Min(g1.Rect.Width, g2.Rect.Width) * 0.5f, worstCase)); } return(worstCase); }
private static List <GLFontBitmap> GenerateBitmapSheetsAndRepack(GLFontGlyph[] sourceGlyphs, BitmapData[] sourceBitmaps, int destSheetWidth, int destSheetHeight, out GLFontGlyph[] destGlyphs, int destMargin, bool usePowerOfTwo) { var pages = new List <GLFontBitmap>(); destGlyphs = new GLFontGlyph[sourceGlyphs.Length]; GLFontBitmap currentPage = null; int maxY = sourceGlyphs.Max(g => g.Rect.Height); int finalPageIndex = 0; int finalPageRequiredWidth = 0; int finalPageRequiredHeight = 0; for (int k = 0; k < 2; k++) { bool pre = k == 0; //first iteration is simply to determine the required size of the final page, so that we can crop it in advance int xPos = 0; int yPos = 0; int maxYInRow = 0; int totalTries = 0; for (int i = 0; i < sourceGlyphs.Length; i++) { if (!pre && currentPage == null) { if (finalPageIndex == pages.Count) { int width = Math.Min(destSheetWidth, usePowerOfTwo ? PowerOfTwo(finalPageRequiredWidth) : finalPageRequiredWidth); int height = Math.Min(destSheetHeight, usePowerOfTwo ? PowerOfTwo(finalPageRequiredHeight) : finalPageRequiredHeight); currentPage = new GLFontBitmap(new Bitmap(width, height, PixelFormat.Format32bppArgb)); currentPage.Clear32(255, 255, 255, 0); // clear to white, but totally transparent } else { currentPage = new GLFontBitmap(new Bitmap(destSheetWidth, destSheetHeight, PixelFormat.Format32bppArgb)); currentPage.Clear32(255, 255, 255, 0); // clear to white, but totally transparent } pages.Add(currentPage); } totalTries++; if (totalTries > 10 * sourceGlyphs.Length) { throw new Exception("Failed to fit font into texture pages"); } var rect = sourceGlyphs[i].Rect; if (xPos + rect.Width + 2 * destMargin <= destSheetWidth && yPos + rect.Height + 2 * destMargin <= destSheetHeight) { if (!pre) { //add to page if (sourceBitmaps[sourceGlyphs[i].Page].PixelFormat == PixelFormat.Format32bppArgb) { GLFontBitmap.Blit(sourceBitmaps[sourceGlyphs[i].Page], currentPage.bitmapData, rect.X, rect.Y, rect.Width, rect.Height, xPos + destMargin, yPos + destMargin); } else { GLFontBitmap.BlitMask(sourceBitmaps[sourceGlyphs[i].Page], currentPage.bitmapData, rect.X, rect.Y, rect.Width, rect.Height, xPos + destMargin, yPos + destMargin); } destGlyphs[i] = new GLFontGlyph(pages.Count - 1, new Rectangle(xPos + destMargin, yPos + destMargin, rect.Width, rect.Height), sourceGlyphs[i].YOffset, sourceGlyphs[i].Character); } else { finalPageRequiredWidth = Math.Max(finalPageRequiredWidth, xPos + rect.Width + 2 * destMargin); finalPageRequiredHeight = Math.Max(finalPageRequiredHeight, yPos + rect.Height + 2 * destMargin); } xPos += rect.Width + 2 * destMargin; maxYInRow = Math.Max(maxYInRow, rect.Height); continue; } if (xPos + rect.Width + 2 * destMargin > destSheetWidth) { i--; yPos += maxYInRow + 2 * destMargin; xPos = 0; if (yPos + maxY + 2 * destMargin > destSheetHeight) { yPos = 0; if (!pre) { currentPage = null; } else { finalPageRequiredWidth = 0; finalPageRequiredHeight = 0; finalPageIndex++; } } continue; } } } return(pages); }
private static void RetargetGlyphRectangleOutwards(BitmapData bitmapData, GLFontGlyph glyph, bool setYOffset, byte alphaTolerance) { int startX, endX; int startY, endY; var rect = glyph.Rect; EmptyDel emptyPix; if (bitmapData.PixelFormat == PixelFormat.Format32bppArgb) { emptyPix = delegate(BitmapData data, int x, int y) { return(GLFontBitmap.EmptyAlphaPixel(data, x, y, alphaTolerance)); } } ; else { emptyPix = delegate(BitmapData data, int x, int y) { return(GLFontBitmap.EmptyPixel(data, x, y)); } }; unsafe { for (startX = rect.X; startX >= 0; startX--) { bool foundPix = false; for (int j = rect.Y; j <= rect.Y + rect.Height; j++) { if (!emptyPix(bitmapData, startX, j)) { foundPix = true; break; } } if (foundPix) { startX++; break; } } for (endX = rect.X + rect.Width; endX < bitmapData.Width; endX++) { bool foundPix = false; for (int j = rect.Y; j <= rect.Y + rect.Height; j++) { if (!emptyPix(bitmapData, endX, j)) { foundPix = true; break; } } if (foundPix) { endX--; break; } } for (startY = rect.Y; startY >= 0; startY--) { bool foundPix = false; for (int i = startX; i <= endX; i++) { if (!emptyPix(bitmapData, i, startY)) { foundPix = true; break; } } if (foundPix) { startY++; break; } } for (endY = rect.Y + rect.Height; endY < bitmapData.Height; endY++) { bool foundPix = false; for (int i = startX; i <= endX; i++) { if (!emptyPix(bitmapData, i, endY)) { foundPix = true; break; } } if (foundPix) { endY--; break; } } } glyph.Rect = new Rectangle(startX, startY, endX - startX + 1, endY - startY + 1); if (setYOffset) { glyph.YOffset = glyph.Rect.Y; } }
private static void RetargetGlyphRectangleInwards(BitmapData bitmapData, GLFontGlyph glyph, bool setYOffset, byte alphaTolerance) { int startX, endX; int startY, endY; var rect = glyph.Rect; EmptyDel emptyPix; if (bitmapData.PixelFormat == PixelFormat.Format32bppArgb) { emptyPix = delegate(BitmapData data, int x, int y) { return(GLFontBitmap.EmptyAlphaPixel(data, x, y, alphaTolerance)); } } ; else { emptyPix = delegate(BitmapData data, int x, int y) { return(GLFontBitmap.EmptyPixel(data, x, y)); } }; unsafe { for (startX = rect.X; startX < bitmapData.Width; startX++) { for (int j = rect.Y; j < rect.Y + rect.Height; j++) { if (!emptyPix(bitmapData, startX, j)) { goto Done1; } } } Done1: for (endX = rect.X + rect.Width - 1; endX >= 0; endX--) { for (int j = rect.Y; j < rect.Y + rect.Height; j++) { if (!emptyPix(bitmapData, endX, j)) { goto Done2; } } } Done2: for (startY = rect.Y; startY < bitmapData.Height; startY++) { for (int i = startX; i < endX; i++) { if (!emptyPix(bitmapData, i, startY)) { goto Done3; } } } Done3: for (endY = rect.Y + rect.Height - 1; endY >= 0; endY--) { for (int i = startX; i < endX; i++) { if (!emptyPix(bitmapData, i, endY)) { goto Done4; } } } Done4 :; } if (endY < startY) { startY = endY = rect.Y; } if (endX < startX) { startX = endX = rect.X; } glyph.Rect = new Rectangle(startX, startY, endX - startX + 1, endY - startY + 1); if (setYOffset) { glyph.YOffset = glyph.Rect.Y; } }