Cairo-Chart/src/Text.vala

96 lines
2.4 KiB
Vala
Raw Normal View History

2018-01-08 23:33:13 +03:00
namespace CairoChart {
2017-08-28 14:47:31 +03:00
public class Text {
public string text = "";
2018-01-08 23:33:13 +03:00
public Font.Style style = Font.Style ();
2017-08-28 14:47:31 +03:00
public Color color = Color();
2018-01-19 11:09:33 +03:00
public double vspacing = 4;
public double hspacing = 4;
public double spacing {
protected get {
return 0;
}
set {
vspacing = hspacing = value;
}
default = 4;
}
2017-08-28 14:47:31 +03:00
2018-01-17 10:36:10 +03:00
public virtual Cairo.TextExtents get_extents (Cairo.Context ctx) {
ctx.select_font_face (style.family, style.slant, style.weight);
ctx.set_font_size (style.size);
2017-08-28 14:47:31 +03:00
Cairo.TextExtents extents;
2018-01-17 10:36:10 +03:00
ctx.text_extents (text, out extents);
2017-08-28 14:47:31 +03:00
return extents;
}
2018-01-17 10:36:10 +03:00
public virtual double get_width (Cairo.Context ctx) {
var extents = get_extents (ctx);
switch (style.orientation) {
2018-01-08 23:33:13 +03:00
case Font.Orientation.HORIZONTAL: return extents.width;
case Font.Orientation.VERTICAL: return extents.height;
default: return 0.0;
}
2017-08-28 14:47:31 +03:00
}
2018-01-17 10:36:10 +03:00
public virtual double get_height (Cairo.Context ctx) {
var extents = get_extents (ctx);
switch (style.orientation) {
2018-01-08 23:33:13 +03:00
case Font.Orientation.HORIZONTAL: return extents.height;
case Font.Orientation.VERTICAL: return extents.width;
default: return 0.0;
}
2017-08-28 14:47:31 +03:00
}
public struct Size {
double width;
double height;
}
2018-01-17 10:36:10 +03:00
public virtual Size get_size (Cairo.Context ctx) {
var sz = Size();
2018-01-17 10:36:10 +03:00
var extents = get_extents (ctx);
switch (style.orientation) {
2018-01-08 23:33:13 +03:00
case Font.Orientation.HORIZONTAL:
2017-12-30 09:16:28 +03:00
sz.width = extents.width + extents.x_bearing;
sz.height = extents.height;
break;
2018-01-08 23:33:13 +03:00
case Font.Orientation.VERTICAL:
2017-12-30 09:16:28 +03:00
sz.width = extents.height; // + extents.x_bearing ?
sz.height = extents.width; // +- extents.y_bearing ?
break;
}
return sz;
2017-08-28 14:47:31 +03:00
}
2018-01-17 10:36:10 +03:00
public virtual void show (Cairo.Context ctx) {
ctx.select_font_face(style.family,
style.slant,
style.weight);
2018-01-17 10:36:10 +03:00
ctx.set_font_size(style.size);
if (style.orientation == Font.Orientation.VERTICAL) {
2018-01-17 10:36:10 +03:00
ctx.rotate(- GLib.Math.PI / 2.0);
ctx.show_text(text);
ctx.rotate(GLib.Math.PI / 2.0);
} else {
2018-01-17 10:36:10 +03:00
ctx.show_text(text);
}
}
2017-08-28 14:47:31 +03:00
public Text (string text = "",
2018-01-08 23:33:13 +03:00
Font.Style style = Font.Style(),
2017-08-28 14:47:31 +03:00
Color color = Color()) {
this.text = text;
this.style = style;
this.color = color;
}
2018-01-15 12:05:21 +03:00
public virtual Text copy () {
2017-08-28 18:16:48 +03:00
var text = new Text ();
text.text = this.text;
text.style = this.style;
text.color = this.color;
return text;
2017-08-28 14:47:31 +03:00
}
}
}