// Capture all characters of the font texture to seperate rectangles that can be used to display text // // Pre-conditions: // // - Must be complete ASCII set from ! to ~ // - There must be an empty horizontal line between character lines // - There must be an empty vertical line between characters // - All characters must consist of one connecting piece, except :;= they must consist of 2 pieces // - Vertical offset is based on the highest character of a line, so you might need to adjust some characters a little bit using Adjust() method (before calling Initialize) // public void Initialize(Texture2D textureFont_) { fontRect = new Rectangle[255]; fontYOffset = new int[255]; textureFont = textureFont_; bitmapData = new Color[textureFont.Width * textureFont.Height]; textureFont.GetData <Color>(bitmapData, 0, bitmapData.Length); int x = 0; int y = 0; CharacterNumber = 0; FirstNonemptyLine = -1; LastNonemptyLine = -1; LineNumber = 0; detectionPhase = DetectionPhase.FindObject; while (y < textureFont.Height) { if (detectionPhase.Equals(DetectionPhase.FindObject)) { if (!isBackgroundPixel(x + y * textureFont.Width)) // found object { FirstNonemptyLine = y; detectionPhase = DetectionPhase.FindEmptyLine; } x++; if (x >= textureFont.Width) { x = 0; y++; } } if (detectionPhase.Equals(DetectionPhase.FindEmptyLine)) { int xx; int yy = FirstNonemptyLine; { bool isEmptyLine = false; while (!isEmptyLine) { xx = 0; yy++; isEmptyLine = true; while (isEmptyLine && xx <= textureFont.Width) { if (!isBackgroundPixel(xx + yy * textureFont.Width)) { isEmptyLine = false; } xx++; } } LastNonemptyLine = yy; detectionPhase = DetectionPhase.VerticalScanning; } } if (detectionPhase.Equals(DetectionPhase.VerticalScanning)) { VerticalScanning(); y = LastNonemptyLine + 1; x = 0; LineNumber++; } } }
private void VerticalScanning() { Rectangle rect; int x = 0; int y = FirstNonemptyLine; while (y < LastNonemptyLine || x < textureFont.Width) { y++; if (y > LastNonemptyLine) { y = FirstNonemptyLine; x++; } if (!isBackgroundPixel(x + y * textureFont.Width)) // found object { rect = ExpandArea(bitmapData, new Rectangle(x - 1, y - 1, 3, 3)); // create rectangle around object if (CharacterNumber == 25 || CharacterNumber == 26 || CharacterNumber == 28) // find another object below this rectangle and add it (for characters ;:=) { int xx = rect.Left; // NOTE: we start looking from the left of the first object to find the second. That's ok for now, because the character-parts vertically overlap each other int yy = FirstNonemptyLine; while (isBackgroundPixel(xx + yy * textureFont.Width) || rect.Contains(xx, yy)) { yy++; if (yy > LastNonemptyLine) { yy = FirstNonemptyLine; xx++; } } Rectangle rect2 = new Rectangle(xx, yy, 3, 3); rect2 = ExpandArea(bitmapData, rect2); // create rectangle around object // now create a rectangle around both rectangles if (rect2.X < rect.X) { rect.Width += (rect.X - rect2.X); rect.X = rect2.X; } if (rect2.Y < rect.Y) { rect.Height += (rect.Y - rect2.Y); rect.Y = rect2.Y; } if (rect2.Right > rect.Right) { rect.Width += (rect2.Right - rect.Right); } if (rect2.Bottom > rect.Bottom) { rect.Height += (rect2.Bottom - rect.Bottom); } } fontRect[CharacterNumber] = rect; fontYOffset[CharacterNumber] = fontExtraYOffset[LineNumber] + rect.Top - FirstNonemptyLine; CharacterNumber++; x += rect.Width; } } detectionPhase = DetectionPhase.FindObject; }