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 close() 25 { 26 _geometry.close(); 27 } 28 29 void font(Font newFont) @property 30 { 31 if (newFont is null) return; 32 _font = newFont; 33 if (!_isInited || newFont != _font) 34 init(); 35 } 36 37 Font font() @property 38 { 39 return _font; 40 } 41 42 bool isInited() @property 43 { 44 return _isInited; 45 } 46 47 void update() 48 { 49 if (text.length == 0 || !_isInited) return; 50 if (_isDirty) 51 { 52 _geometry.load; 53 _isDirty = false; 54 } 55 } 56 57 uint width() @property 58 { 59 return _width; 60 } 61 62 uint height() @property 63 { 64 return _height; 65 } 66 67 ivec2 size() @property 68 { 69 return ivec2(_width, _height); 70 } 71 72 TexRectArray geometry() @property 73 { 74 return _geometry; 75 } 76 77 dstring text() @property 78 { 79 return _text; 80 } 81 82 dstring text(in dstring newText) @property 83 { 84 if (_text == newText) 85 { 86 return _text; 87 } 88 89 _text = newText; 90 if (!_isInited) return _text; 91 92 _geometry.vertieces = null; 93 _cursorX = 0; 94 95 appendGlyphs(_text, _font); 96 _isDirty = true; 97 98 return _text; 99 } 100 101 string fontName() @property 102 { 103 return _fontName; 104 } 105 106 void fontName(string newFontName) @property 107 { 108 _fontName = newFontName; 109 } 110 111 ///Supports chaining 112 TextLine append(in string text) 113 { 114 return append(to!dstring(text)); 115 } 116 117 TextLine append(in dstring text) 118 { 119 appendGlyphs(_text, _font); 120 _isDirty = true; 121 return this; 122 } 123 124 protected: 125 126 void init() 127 { 128 _geometry = new TexRectArray; 129 _height = _font.size; 130 appendGlyphs(_text, _font); 131 _isInited = true; 132 } 133 134 void appendGlyphs(in dstring text, Font font) 135 { 136 foreach(dchar chr; text) 137 { 138 Glyph* glyph = font.getGlyph(chr); 139 if (glyph !is null && chr == '\t') 140 { 141 _cursorX += glyph.metrics.advanceX * _tabSize; 142 continue; 143 } 144 if (glyph is null) glyph = font.getGlyph('?'); 145 146 int x = glyph.metrics.offsetX + _cursorX; 147 int y = font.verticalOffset - glyph.metrics.offsetY; 148 int w = glyph.metrics.width; 149 int h = glyph.metrics.height; 150 int tx = glyph.atlasPosition.x; 151 int ty = glyph.atlasPosition.y; 152 153 //whitespace 154 if (w == 0 || h == 0) 155 { 156 _cursorX += glyph.metrics.advanceX; 157 continue; 158 } 159 160 _geometry.appendQuad(Rect(x, y, w, h), Rect(tx, ty, w, h)); 161 _cursorX += glyph.metrics.advanceX; 162 } 163 164 _width = _cursorX; 165 } 166 167 protected: 168 uint _width; 169 uint _height; 170 dstring _text; 171 bool _isInited = false; 172 173 TexRectArray _geometry; 174 uint _cursorX; 175 bool _isDirty = true; 176 177 Font _font; 178 string _fontName; 179 180 ubyte _tabSize = 4; 181 }