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 {
2018-01-21 17:27:46 +03:00
/**
* ``CairoChart`` Text.
*/
2017-08-28 14:47:31 +03:00
public class Text {
2018-01-21 17:27:46 +03:00
2018-01-23 12:27:02 +03:00
protected unowned Chart chart;
2018-01-22 18:21:29 +03:00
protected string _text;
protected Font _font;
protected Cairo.TextExtents? _ext;
2018-01-21 17:27:46 +03:00
/**
* ``Text`` string.
*/
2018-01-22 13:22:09 +03:00
public virtual string text {
2018-01-21 18:41:41 +03:00
get {
return _text;
}
set {
_text = value;
2018-01-21 18:56:27 +03:00
_ext = null;
2018-01-21 18:41:41 +03:00
}
}
2018-01-21 17:27:46 +03:00
/**
* ``Text`` font style.
*/
2018-01-22 13:22:09 +03:00
public virtual Font font {
2018-01-21 18:41:41 +03:00
get {
return _font;
}
set {
_font = value;
2018-01-21 18:56:27 +03:00
_font.notify.connect((s, p) => {
_ext = null;
});
_ext = null;
2018-01-21 18:41:41 +03:00
}
}
2018-01-21 17:27:46 +03:00
/**
* ``Text`` color.
*/
2017-08-28 14:47:31 +03:00
public Color color = Color();
2018-01-21 17:27:46 +03:00
/**
* Cairo ``Text`` extents.
*/
2018-01-22 13:22:09 +03:00
protected virtual Cairo.TextExtents ext {
2018-01-21 17:27:46 +03:00
get {
2018-01-21 18:41:41 +03:00
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;
2018-01-21 17:27:46 +03:00
}
protected set {
}
2017-08-28 14:47:31 +03:00
}
2018-01-21 17:27:46 +03:00
/**
* ``Text`` width.
*/
public virtual double width {
get {
2018-01-21 18:41:41 +03:00
switch (font.orient) {
2018-01-22 13:22:09 +03:00
case Gtk.Orientation.HORIZONTAL: return ext.width + ext.x_bearing;
2018-01-21 17:27:46 +03:00
case Gtk.Orientation.VERTICAL: return ext.height;
2018-01-22 17:47:00 +03:00
default: return 0;
2018-01-21 17:27:46 +03:00
}
}
protected set {
}
2017-08-28 14:47:31 +03:00
}
2018-01-21 17:27:46 +03:00
/**
* ``Text`` height.
*/
public virtual double height {
get {
2018-01-21 18:41:41 +03:00
switch (font.orient) {
2018-01-22 13:22:09 +03:00
case Gtk.Orientation.HORIZONTAL: return ext.height; // + ext.x_bearing ?
case Gtk.Orientation.VERTICAL: return ext.width; // +- ext.y_bearing ?
2018-01-22 17:47:00 +03:00
default: return 0;
2018-01-21 17:27:46 +03:00
}
}
protected set {
}
2017-08-28 14:47:31 +03:00
}
2018-01-21 17:27:46 +03:00
/**
* Constructs a new ``Text``.
* @param chart ``Chart`` instance.
* @param text ``Text`` string.
2018-01-21 18:41:41 +03:00
* @param font ``Text`` font style.
2018-01-21 17:27:46 +03:00
* @param color ``Text`` color.
*/
public Text (Chart chart,
string text = "",
2018-01-21 18:41:41 +03:00
Font font = new Font(),
2018-01-19 20:35:22 +03:00
Color color = Color()
) {
2018-01-21 17:27:46 +03:00
this.chart = chart;
2017-08-28 14:47:31 +03:00
this.text = text;
2018-01-21 18:41:41 +03:00
this.font = font;
2017-08-28 14:47:31 +03:00
this.color = color;
}
2018-01-21 17:27:46 +03:00
/**
* Gets a copy of the ``Text``.
*/
2018-01-15 12:05:21 +03:00
public virtual Text copy () {
2018-01-21 17:27:46 +03:00
var text = new Text (chart);
text.chart = this.chart;
2018-01-21 18:56:27 +03:00
text.text = this.text;
2018-01-22 15:49:49 +03:00
text.font = this.font.copy();
2018-01-21 18:41:41 +03:00
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
}
/**
* 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
}
}