Cairo-Chart/src/Text.vala

143 lines
2.7 KiB
Vala
Raw Normal View History

2018-01-08 23:33:13 +03:00
namespace CairoChart {
/**
* ``CairoChart`` Text.
*/
2017-08-28 14:47:31 +03:00
public class Text {
2018-01-08 23:33:13 +03:00
protected unowned Chart chart;
protected string _text;
protected Font _font;
protected Cairo.TextExtents? _ext;
/**
* ``Text`` string.
*/
public virtual string text {
get {
return _text;
}
set {
_text = value;
_ext = null;
}
2017-08-28 14:47:31 +03:00
}
2018-01-08 23:33:13 +03:00
/**
* ``Text`` font style.
*/
public virtual Font font {
get {
return _font;
}
set {
_font = value;
_font.notify.connect((s, p) => {
_ext = null;
});
_ext = null;
}
2017-08-28 14:47:31 +03:00
}
2018-01-08 23:33:13 +03:00
/**
* ``Text`` color.
*/
public Color color = Color();
/**
* Cairo ``Text`` extents.
*/
protected virtual Cairo.TextExtents ext {
get {
if (_ext == null) {
chart.ctx.select_font_face (font.family, font.slant, font.weight);
chart.ctx.set_font_size (font.size);
chart.ctx.text_extents (text, out _ext);
}
return _ext;
}
protected set {
}
2017-08-28 14:47:31 +03:00
}
2018-01-08 23:33:13 +03:00
/**
* ``Text`` width.
*/
public virtual double width {
get {
switch (font.orient) {
case Gtk.Orientation.HORIZONTAL: return ext.width + ext.x_bearing;
case Gtk.Orientation.VERTICAL: return ext.height;
default: return 0;
}
}
protected set {
}
}
2018-01-08 23:33:13 +03:00
/**
* ``Text`` height.
*/
public virtual double height {
get {
switch (font.orient) {
case Gtk.Orientation.HORIZONTAL: return ext.height; // + ext.x_bearing ?
case Gtk.Orientation.VERTICAL: return ext.width; // +- ext.y_bearing ?
default: return 0;
}
}
protected set {
}
2017-08-28 14:47:31 +03:00
}
2018-01-08 23:33:13 +03:00
/**
* Constructs a new ``Text``.
* @param chart ``Chart`` instance.
* @param text ``Text`` string.
* @param font ``Text`` font style.
* @param color ``Text`` color.
*/
public Text (Chart chart,
string text = "",
Font font = new Font(),
Color color = Color()
) {
this.chart = chart;
2017-08-28 14:47:31 +03:00
this.text = text;
2018-01-08 23:33:13 +03:00
this.font = font;
2017-08-28 14:47:31 +03:00
this.color = color;
}
2018-01-08 23:33:13 +03:00
/**
* Gets a copy of the ``Text``.
*/
public virtual Text copy () {
var text = new Text (chart);
text.chart = this.chart;
2017-08-28 18:16:48 +03:00
text.text = this.text;
2018-01-08 23:33:13 +03:00
text.font = this.font.copy();
text._ext = this._ext;
2017-08-28 18:16:48 +03:00
text.color = this.color;
return text;
2017-08-28 14:47:31 +03:00
}
2018-01-08 23:33:13 +03:00
/**
* Show ``Text``.
*/
public virtual void show () {
if (text == "") return;
chart.ctx.select_font_face(font.family,
font.slant,
font.weight);
chart.ctx.set_font_size(font.size);
if (font.orient == Gtk.Orientation.VERTICAL) {
chart.ctx.rotate(- GLib.Math.PI / 2);
chart.ctx.show_text(text);
chart.ctx.rotate(GLib.Math.PI / 2);
} else {
chart.ctx.show_text(text);
}
}
2017-08-28 14:47:31 +03:00
}
}