1 /** 2 Copyright: Copyright (c) 2013-2014 Andrey Penechko. 3 License: a$(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0). 4 Authors: Andrey Penechko. 5 */ 6 7 module anchovy.gui.utils.textline; 8 9 import anchovy.gui; 10 public import anchovy.graphics.texrectarray; 11 import anchovy.graphics.interfaces.irenderer; 12 import anchovy.graphics.font.fontmanager; 13 14 class TextLine 15 { 16 17 public: 18 19 this() 20 { 21 _text = ""; 22 } 23 24 void font(Font newFont) @property 25 { 26 if (newFont is null) return; 27 _font = newFont; 28 if (!_isInited || newFont != _font) 29 init(); 30 } 31 32 Font font() @property 33 { 34 return _font; 35 } 36 37 bool isInited() @property 38 { 39 return _isInited; 40 } 41 42 void update() 43 { 44 if (text.length == 0 || !_isInited) return; 45 if (_isDirty) 46 { 47 _geometry.load; 48 _isDirty = false; 49 } 50 } 51 52 uint width() @property 53 { 54 return _width; 55 } 56 57 uint height() @property 58 { 59 return _height; 60 } 61 62 ivec2 size() @property 63 { 64 return ivec2(_width, _height); 65 } 66 67 TexRectArray geometry() @property 68 { 69 return _geometry; 70 } 71 72 dstring text() @property 73 { 74 return _text; 75 } 76 77 dstring text(in dstring newText) @property 78 { 79 if (_text == newText) 80 { 81 return _text; 82 } 83 84 _text = newText; 85 if (!_isInited) return _text; 86 87 _geometry.vertieces = null; 88 _cursorX = 0; 89 90 appendGlyphs(_text, _font); 91 _isDirty = true; 92 93 return _text; 94 } 95 96 string fontName() @property 97 { 98 return _fontName; 99 } 100 101 void fontName(string newFontName) @property 102 { 103 _fontName = newFontName; 104 } 105 106 ///Supports chaining 107 TextLine append(in string text) 108 { 109 return append(to!dstring(text)); 110 } 111 112 TextLine append(in dstring text) 113 { 114 appendGlyphs(_text, _font); 115 _isDirty = true; 116 return this; 117 } 118 119 protected: 120 121 void init() 122 { 123 _geometry = new TexRectArray; 124 _height = _font.size; 125 appendGlyphs(_text, _font); 126 _isInited = true; 127 } 128 129 void appendGlyphs(in dstring text, Font font) 130 { 131 foreach(dchar chr; text) 132 { 133 Glyph* glyph = font.getGlyph(chr); 134 if (glyph !is null && chr == '\t') 135 { 136 _cursorX += glyph.metrics.advanceX * _tabSize; 137 continue; 138 } 139 if (glyph is null) glyph = font.getGlyph('?'); 140 141 int x = glyph.metrics.offsetX + _cursorX; 142 int y = font.verticalOffset - glyph.metrics.offsetY; 143 int w = glyph.metrics.width; 144 int h = glyph.metrics.height; 145 int tx = glyph.atlasPosition.x; 146 int ty = glyph.atlasPosition.y; 147 148 //whitespace 149 if (w == 0 || h == 0) 150 { 151 _cursorX += glyph.metrics.advanceX; 152 continue; 153 } 154 155 _geometry.appendQuad(Rect(x, y, w, h), Rect(tx, ty, w, h)); 156 _cursorX += glyph.metrics.advanceX; 157 } 158 159 _width = _cursorX; 160 } 161 162 protected: 163 uint _width; 164 uint _height; 165 dstring _text; 166 bool _isInited = false; 167 168 TexRectArray _geometry; 169 uint _cursorX; 170 bool _isDirty = true; 171 172 Font _font; 173 string _fontName; 174 175 ubyte _tabSize = 4; 176 }