1 /** 2 * Rendering for simple bitmap fonts. 3 * 4 * License: 5 * This Source Code Form is subject to the terms of 6 * the Mozilla Public License, v. 2.0. If a copy of 7 * the MPL was not distributed with this file, You 8 * can obtain one at http://mozilla.org/MPL/2.0/. 9 * 10 * Authors: 11 * Vladimir Panteleev <ae@cy.md> 12 */ 13 14 module ae.ui.video.bmfont; 15 16 import ae.ui.video.renderer; 17 import ae.utils.array; 18 import ae.utils.graphics.fonts.draw; 19 20 /// Adapter from a font (as in `ae.utils.graphics.fonts`) 21 /// and `ProceduralTextureSource` . 22 final class FontTextureSource(Font) : ProceduralTextureSource 23 { 24 Font font; 25 Renderer.COLOR color; 26 27 this(Font font, Renderer.COLOR color) 28 { 29 this.font = font; 30 this.color = color; 31 } /// 32 33 override void getSize(out int width, out int height) 34 { 35 width = font.maxWidth; 36 height = font.maxGlyph * font.height; 37 } 38 39 override void drawTo(TextureCanvas dest) 40 { 41 foreach (g; 0..font.maxGlyph) 42 { 43 dchar c = g; 44 dest.drawText(0, g * font.height, c.toArray, font, color); 45 } 46 } 47 48 void drawText(S)(Renderer r, int x, int y, S s) 49 { 50 foreach (c; s) 51 { 52 if (font.hasGlyph(c)) 53 { 54 auto g = font.getGlyph(c); 55 auto v = c * font.height; 56 r.draw(x, y, this, 0, v, g.width, v + font.height); 57 x += g.width; 58 } 59 } 60 } 61 } 62 63 unittest 64 { 65 // Test instantiation 66 if (false) 67 { 68 Renderer r; 69 import ae.utils.graphics.fonts.font8x8; 70 FontTextureSource!Font8x8 f; 71 f.drawText(r, 0, 0, "foo"); 72 } 73 }