public Font(string font, uint size) { if (!File.Exists(font)) throw new FileNotFoundException("Failed to load font file:" + font); Size = size; if (Graphics.Instance.GraphicsDevice == null) throw new NullReferenceException("Graphics Device is null."); gd = Graphics.Instance.GraphicsDevice; IntPtr libptr; int ret = FT.FT_Init_FreeType(out libptr); if (ret != 0) throw new Exception("Failed to load FreeType library."); int retb = FT.FT_New_Face(libptr, font, 0, out faceptr); if (retb != 0) throw new Exception("Failed to create font face."); face = (FT_FaceRec)Marshal.PtrToStructure(faceptr, typeof(FT_FaceRec)); FT.FT_Set_Char_Size(faceptr, (int)size << 6, (int)size << 6, 96, 96); FT.FT_Set_Pixel_Sizes(faceptr, size, size); ascender = face.ascender >> 6; descender = face.descender >> 6; fontheight = ((face.height >> 6) + descender + ascender) / 4; yoffset = (int)(Size - ascender); baseCharacter = CreateChar('i'); }
private Character[] CreateStringTexture(string s) { var temp = new Character[s.Length]; for (int i = 0; i < s.Length; i++) { temp[i] = CreateChar(s[i]); } return temp; }
//TODO: overflow private void DrawText(SpriteBatch sb, Vector2 pos, Rectangle rect,Character[] text,Color color) { int presentx = (int)pos.X; int presenty = (int)pos.Y + yoffset; for (int i = 0; i < text.Length; i++) { bool draw = true; if (presentx > rect.Width +rect.X || presenty > rect.Height + rect.Y - Size || presentx < rect.X || presenty < yoffset - Size +rect.Y) draw = false; if (text[i].Char == '\n') { presenty += (int)Size + 2; presentx = (int)rect.X; continue; } if (text[i].Char == '\r') continue; if (draw) { if (EnableBorder) { if (draw) { } sb.Draw(text[i].Texture, new Rectangle(presentx + text[i].OffsetX - 1, presenty + text[i].OffserY - 1, text[i].Texture.Width, text[i].Texture.Height), BorderColor); sb.Draw(text[i].Texture, new Rectangle(presentx + text[i].OffsetX - 1, presenty + text[i].OffserY, text[i].Texture.Width, text[i].Texture.Height), BorderColor); sb.Draw(text[i].Texture, new Rectangle(presentx + text[i].OffsetX - 1, presenty + text[i].OffserY + 1, text[i].Texture.Width, text[i].Texture.Height), BorderColor); sb.Draw(text[i].Texture, new Rectangle(presentx + text[i].OffsetX, presenty + text[i].OffserY - 1, text[i].Texture.Width, text[i].Texture.Height), BorderColor); sb.Draw(text[i].Texture, new Rectangle(presentx + text[i].OffsetX, presenty + text[i].OffserY + 1, text[i].Texture.Width, text[i].Texture.Height), BorderColor); sb.Draw(text[i].Texture, new Rectangle(presentx + text[i].OffsetX + 1, presenty + text[i].OffserY - 1, text[i].Texture.Width, text[i].Texture.Height), BorderColor); sb.Draw(text[i].Texture, new Rectangle(presentx + text[i].OffsetX + 1, presenty + text[i].OffserY, text[i].Texture.Width, text[i].Texture.Height), BorderColor); sb.Draw(text[i].Texture, new Rectangle(presentx + text[i].OffsetX + 1, presenty + text[i].OffserY + 1, text[i].Texture.Width, text[i].Texture.Height), BorderColor); } if (EnableShadow) { sb.Draw(text[i].Texture, new Rectangle(presentx + text[i].OffsetX + ShadowXOffset, presenty + text[i].OffserY + ShadowYOffset, text[i].Texture.Width, text[i].Texture.Height), ShadowColor); } sb.Draw(text[i].Texture, new Rectangle(presentx + text[i].OffsetX, presenty + text[i].OffserY, text[i].Texture.Width, text[i].Texture.Height), color); } presentx += (1 + (int)text[i].Texture.Width + text[i].OffsetX); } }
private Character CreateChar(char c) { Character ch = new Character(); try { if (buffer.ContainsKey(c)) { return buffer[c]; } if (c == '\r' || c == '\n') { Texture2D texture = new Texture2D(gd, baseCharacter.Texture.Width, 1); ch.Texture = texture; ch.Char = c; buffer.Add(c, ch); return ch; } if (c != ' ' && c != ' ') { var tt = GetCharBitmap(Convert.ToUInt32(c)); var charoffsety = (ascender) - tt.bitmap_top; var charoffsetx = tt.bitmap_left; byte[] bmp = new byte[tt.bitmap.rows * tt.bitmap.width]; Marshal.Copy(tt.bitmap.buffer, bmp, 0, bmp.Length); Color[] colordata = new Color[tt.bitmap.rows * tt.bitmap.width]; for (int i = 0; i < colordata.Length; i++) { colordata[i] = new Color(255, 255, 255, bmp[i]); } Texture2D texture = new Texture2D(gd, tt.bitmap.width, tt.bitmap.rows); texture.SetData<Color>(colordata); PreMultiplyAlphas(texture); ch.Texture = texture; ch.OffserY = charoffsety; ch.OffsetX = charoffsetx; ch.Char = c; buffer.Add(c, ch); } else { Texture2D texture = new Texture2D(gd, baseCharacter.Texture.Width, 1); ch.Texture = texture; buffer.Add(c, ch); } } catch (Exception) { Texture2D texture = new Texture2D(gd, baseCharacter.Texture.Width, 1); ch.Texture = texture; buffer.Add(c, ch); } return ch; }