X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Ftext.cpp;h=1d824e0ddb9ca590f551748921503e626bcdd931;hb=7c1b1b44dc3726d6cdb4681f9d44189eb4102a5a;hp=8ef17392f353259c0fa83ec95b1f5b4e6a08a41c;hpb=bf4dbeb94ef7ce7fc7f9fb3e5f1d75cfe5cefff2;p=libs%2Fgltk.git diff --git a/source/text.cpp b/source/text.cpp index 8ef1739..1d824e0 100644 --- a/source/text.cpp +++ b/source/text.cpp @@ -115,6 +115,22 @@ void Text::insert(unsigned pos, const string &s) } } +unsigned Text::get_visible_lines(const Part &part, const Geometry &parent, unsigned *fit_height) const +{ + const GL::Font &font = style->get_font(); + float font_size = style->get_font_size(); + unsigned line_height = static_cast((font.get_ascent()-font.get_descent())*font_size); + unsigned line_spacing = static_cast(font_size*6/5); + + const Sides &margin = part.get_margin(); + unsigned vmargin = margin.top+margin.bottom+line_height-line_spacing; + unsigned n_lines = min(lines.size(), max((max(parent.h, vmargin)-vmargin)/line_spacing, 1U)); + if(fit_height) + *fit_height = line_height+(n_lines-1)*line_spacing; + + return n_lines; +} + unsigned Text::get_line_length(unsigned i) const { if(i>=lines.size()) @@ -160,7 +176,7 @@ Geometry Text::coords_to_geometry(const Part &part, const Geometry &parent, unsi data.row = row; data.col = col; - process_lines(part, parent, first_row, data); + process_lines(part, parent, first_row, &Text::coords_to_geom_line, data); return data.result; } @@ -182,7 +198,7 @@ void Text::build(const Part &part, State state, const Geometry &parent, unsigned RenderData data; data.bld = &bld; - process_lines(part, parent, first_row, data); + process_lines(part, parent, first_row, &Text::build_line, data); } Text &Text::operator=(const string &t) @@ -217,22 +233,19 @@ void Text::find_lines() } } -template -void Text::process_lines(const Part &part, const Geometry &parent, unsigned first_row, T &data) const +template +void Text::process_lines(const Part &part, const Geometry &parent, unsigned first_row, void (Text::*func)(unsigned, const Geometry &, T &) const, T &data) const { if(!style) return; const GL::Font &font = style->get_font(); float font_size = style->get_font_size(); - unsigned line_height = static_cast((font.get_ascent()-font.get_descent())*font_size); unsigned line_spacing = static_cast(font_size*6/5); - unsigned height = line_height+(lines.size()-1)*line_spacing; int y_offset = static_cast(-font.get_descent()*font_size); - const Sides &margin = part.get_margin(); - unsigned fit_height = parent.h-margin.top-margin.bottom+line_spacing-line_height; - unsigned n_lines = min(lines.size(), max(fit_height/line_spacing, 1U)); + unsigned fit_height; + unsigned n_lines = get_visible_lines(part, parent, &fit_height); first_row = min(first_row, lines.size()-n_lines); for(unsigned i=0; i