private FontWrapper(int hash, IntPtr font, FontKind kind) { if (!lastSetSizes.ContainsKey(hash)) { lastSetSizes [hash] = 0; } this.kind = kind; this.Hash = hash; this.disp = kind != FontKind.Texture; this.font = font; }
public void SpriteBatch_CanRenderSimpleStrings(FontKind fontKind, ColorEncoding encoding) { var spriteBatch = default(SpriteBatch); var spriteFont = default(UltravioletFont); var result = GivenAnUltravioletApplication() .WithConfiguration(config => { if (encoding == ColorEncoding.Srgb) { config.SrgbBuffersEnabled = true; config.SrgbDefaultForTexture2D = true; config.SrgbDefaultForRenderBuffer2D = true; } }) .WithPlugin(fontKind == FontKind.FreeType2 ? new FreeTypeFontPlugin() : null) .WithContent(content => { spriteBatch = SpriteBatch.Create(); spriteFont = content.Load <UltravioletFont>(fontKind == FontKind.SpriteFont ? "Fonts/SegoeUI" : "Fonts/FiraSans"); }) .Render(uv => { spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend); spriteBatch.DrawString(spriteFont.Regular, "Hello, world!", Vector2.Zero, Color.White); spriteBatch.End(); }); if (fontKind == FontKind.SpriteFont) { TheResultingImage(result) .ShouldMatch(@"Resources/Expected/Graphics/Graphics2D/SpriteBatch_CanRenderSimpleStrings.png"); } else { if (encoding == ColorEncoding.Linear) { TheResultingImage(result) .ShouldMatch(@"Resources/Expected/Graphics/Graphics2D/SpriteBatch_CanRenderSimpleStrings(FreeType2).png"); } else { TheResultingImage(result) .ShouldMatch(@"Resources/Expected/Graphics/Graphics2D/SpriteBatch_CanRenderSimpleStrings(FreeType2_sRGB).png"); } } }
public void Load(ContentManager content, FontKind kind) { switch (kind) { case FontKind.SpriteFont: Load(content); break; case FontKind.FreeType2: LoadFreeType(content); break; default: throw new ArgumentException(nameof(kind)); } }
public void SpriteBatch_CorrectlyRendersEastAsianCharacters(FontKind fontKind) { var spriteBatch = default(SpriteBatch); var spriteFont = default(UltravioletFont); var result = GivenAnUltravioletApplication() .WithPlugin(fontKind == FontKind.FreeType2 ? new FreeTypeFontPlugin() : null) .WithContent(content => { spriteBatch = SpriteBatch.Create(); spriteFont = content.Load <UltravioletFont>(fontKind == FontKind.SpriteFont ? "Fonts/MSGothic16" : "Fonts/NotoSansCJKjp-Regular"); }) .Render(uv => { spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend); // From the Japanese Wikipedia article "日本" spriteBatch.DrawString(spriteFont.Regular, "日本国(にっぽんこく、にほんこく)、または日本\n" + "(にっぽん、にほん)は、東アジアに位置する日本\n" + "列島(北海道・本州・四国・九州の主要四島および\n" + "それに付随する島々)及び、南西諸島・小笠原諸島\n" + "などの諸島嶼から成る島国である。日本語が事実上\n" + "の公用語として使用されている。首都は事実上東京\n" + "都とされている。", Vector2.Zero, Color.White); spriteBatch.End(); }); if (fontKind == FontKind.SpriteFont) { TheResultingImage(result) .ShouldMatch(@"Resources/Expected/Graphics/Graphics2D/SpriteBatch_CorrectlyRendersEastAsianCharacters.png"); } else { TheResultingImage(result) .ShouldMatch(@"Resources/Expected/Graphics/Graphics2D/SpriteBatch_CorrectlyRendersEastAsianCharacters(FreeType2).png"); } }
public static FontWrapper LoadFile(string pathToFont, FontKind kind = FontKind.Texture) { if (!File.Exists(pathToFont)) { throw new FileNotFoundException(pathToFont + " could not be found!"); } var hash = CalculateHashCode(pathToFont, kind); FontEntry fe; if (fontCache.TryGetValue(hash, out fe)) { fe.ReferenceCount++; return(new FontWrapper(hash, fe.Handle, kind)); } IntPtr font; switch (kind) { case FontKind.Pixmap: font = Fonts.CreatePixmapFont(pathToFont); break; default: case FontKind.Texture: font = Fonts.CreateTextureFont(pathToFont); break; } if (font == IntPtr.Zero) { throw new FTGLException(IntPtr.Zero); } fontCache [hash] = new FontEntry(font); return(new FontWrapper(hash, font, kind)); }
public static int CalculateHashCode(string fontFace, FontKind kind) { return(fontFace.GetHashCode() + ((byte)kind << 24)); }