[ease/text: 1/4] Text rendering with Pango sort of works.
- From: Nate Stedman <natesm src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [ease/text: 1/4] Text rendering with Pango sort of works.
- Date: Mon, 29 Nov 2010 04:53:36 +0000 (UTC)
commit 0921a127229d8b0e3a6d343fa3ff43efdd53922f
Author: Nate Stedman <natesm gmail com>
Date: Sun Nov 28 03:28:53 2010 -0500
Text rendering with Pango sort of works.
ease-core/Makefile.am | 2 +
ease-core/ease-actor.vala | 4 +-
ease-core/ease-element.vala | 9 +-
ease-core/ease-slide.vala | 6 +-
ease-core/ease-text-actor.vala | 138 ++---------------
ease-core/ease-text-element.vala | 314 ++------------------------------------
ease-core/ease-text.vala | 53 +++++++
ease-core/ease-theme.vala | 8 +-
ease/ease-editor-embed.vala | 4 +-
ease/ease-editor-window.vala | 9 +-
ease/ease-welcome-actor.vala | 4 +-
11 files changed, 109 insertions(+), 442 deletions(-)
---
diff --git a/ease-core/Makefile.am b/ease-core/Makefile.am
index 648cea2..b51af3c 100644
--- a/ease-core/Makefile.am
+++ b/ease-core/Makefile.am
@@ -31,7 +31,9 @@ libease_core_ EASE_CORE_VERSION@_la_SOURCES = \
ease-scrolled-embed-window.vala \
ease-shape-element.vala \
ease-slide.vala \
+ ease-string.vala \
ease-temp.vala \
+ ease-text.vala \
ease-text-actor.vala \
ease-text-element.vala \
ease-theme.vala \
diff --git a/ease-core/ease-actor.vala b/ease-core/ease-actor.vala
index c7f5224..1fb3fd0 100644
--- a/ease-core/ease-actor.vala
+++ b/ease-core/ease-actor.vala
@@ -208,8 +208,10 @@ public abstract class Ease.Actor : Clutter.Group
* Called when the actor should be edited. Subclasses should override this.
*
* @param sender The widget this Actor is on.
+ * @param mouse_x The x position of the mouse, relative to the actor.
+ * @param mouse_y The y position of the mouse, relative to the actor.
*/
- public virtual void edit(Gtk.Widget sender) {}
+ public virtual void edit(Gtk.Widget sender, float mouse_x, float mouse_y) {}
/**
* Called when the actor end editing. Subclasses with editing that is not
diff --git a/ease-core/ease-element.vala b/ease-core/ease-element.vala
index cc28080..a3f191f 100644
--- a/ease-core/ease-element.vala
+++ b/ease-core/ease-element.vala
@@ -80,7 +80,6 @@ public abstract class Ease.Element : GLib.Object, UndoSource
*/
public Element.from_json(Json.Object obj)
{
- signals();
identifier = obj.get_string_member(Theme.E_IDENTIFIER);
x = (float)obj.get_string_member(Theme.X).to_double();
y = (float)obj.get_string_member(Theme.Y).to_double();
@@ -91,6 +90,14 @@ public abstract class Ease.Element : GLib.Object, UndoSource
}
/**
+ * Connect signals when an Element is created.
+ */
+ construct
+ {
+ signals();
+ }
+
+ /**
* Connects base Element signals.
*/
public virtual void signals()
diff --git a/ease-core/ease-slide.vala b/ease-core/ease-slide.vala
index b9ba72e..77c3be1 100644
--- a/ease-core/ease-slide.vala
+++ b/ease-core/ease-slide.vala
@@ -651,7 +651,7 @@ public class Ease.Slide : GLib.Object, UndoSource
if ((object as TextElement).identifier == Theme.TITLE_TEXT ||
(object as TextElement).identifier == Theme.HEADER_TEXT)
{
- title_changed(this, (object as TextElement).text);
+ //title_changed(this, (object as TextElement).text);
}
}
}
@@ -669,8 +669,8 @@ public class Ease.Slide : GLib.Object, UndoSource
if ((element as TextElement).identifier == Theme.TITLE_TEXT ||
(element as TextElement).identifier == Theme.HEADER_TEXT)
{
- var ret = (element as TextElement).text;
- return ret.length > 0 ? ret : null;
+ //var ret = (element as TextElement).text;
+ //return ret.length > 0 ? ret : null;
}
}
}
diff --git a/ease-core/ease-string.vala b/ease-core/ease-string.vala
new file mode 100644
index 0000000..e69de29
diff --git a/ease-core/ease-text-actor.vala b/ease-core/ease-text-actor.vala
index 407d5df..77e513d 100644
--- a/ease-core/ease-text-actor.vala
+++ b/ease-core/ease-text-actor.vala
@@ -33,9 +33,9 @@ public class Ease.TextActor : Actor
private UndoAction undo_action;
/**
- * Text at the start of an edit.
+ * The CairoTexture that is rendered onto.
*/
- private string original_text;
+ private Clutter.CairoTexture texture;
/**
* Instantiates a new TextActor from an Element.
@@ -49,140 +49,36 @@ public class Ease.TextActor : Actor
{
base(e, c);
- var text = new Clutter.Text();
- contents = text;
-
- // set actor properties
- text.use_markup = true;
- text.line_wrap = true;
- text.line_wrap_mode = Pango.WrapMode.WORD_CHAR;
- text.color = e.color.clutter;
- text.line_alignment = e.text_align;
- text.font_name = e.font_description.to_string();
- text.set_markup(e.display_text);
+ texture = new Clutter.CairoTexture((uint)e.width, (uint)e.height);
+ contents = texture;
+
+ // automatically render when the size of the texture changes
+ texture.allocation_changed.connect(() => {
+ texture.clear();
+ render_text();
+ });
add_actor(contents);
contents.width = e.width;
contents.height = e.height;
x = e.x;
y = e.y;
-
- // add notify event handlers to update when changes to the element occur
- e.notify["color"].connect((sender, spec) => {
- text.color = (sender as TextElement).color.clutter;
- });
-
- e.notify["font-description"].connect((sender, spec) => {
- text.font_name = e.font_description.to_string();
- });
-
- e.notify["text-align"].connect((sender, spec) => {
- text.line_alignment = e.text_align;
- });
-
- e.notify["color"].connect((sender, spec) => {
- text.color = e.color.clutter;
- });
-
- e.notify["text"].connect((sender, spec) => {
- if (!text.editable)
- {
- text.set_markup((element as TextElement).display_text);
- }
- });
- }
-
- /**
- * { inheritDoc}
- */
- public override void edit(Gtk.Widget sender)
- {
- // set text to editable
- var text = contents as Clutter.Text;
- text.editable = true;
- text.reactive = true;
- text.activatable = true;
- text.text_changed.connect(text_changed);
- text.activate.connect(text_activate);
-
- // grab key focus
- ((Clutter.Stage)get_stage()).set_key_focus(text);
- sender.grab_focus();
-
- // set the selection color
- text.selection_color = { 255 - text.color.red,
- 255 - text.color.green,
- 255 - text.color.blue,
- SELECTION_ALPHA };
-
- // if the element hasn't been edited, empty it
- if (!element.has_been_edited)
- {
- text.text = "";
- }
-
- // create an UndoAction for the element
- undo_action = new UndoAction(element, "has-been-edited");
-
- // order is IMPORTANT here because of notify lambda
- undo_action.add(element, "text");
- original_text = (element as TextElement).text;
-
- // if the text is being edited when the action is applied, stop editing
- undo_action.pre_apply.connect((action) => {
- if (text.editable) end_edit(sender);
- });
}
- /**
- * { inheritDoc}
- */
- public override void end_edit(Gtk.Widget sender)
+ public override void edit(Gtk.Widget sender, float mouse_x, float mouse_y)
{
- // release key focus
- ((Clutter.Stage)get_stage()).set_key_focus(null);
-
- // disable text editing
- var text = contents as Clutter.Text;
- text.editable = false;
- text.reactive = false;
- text.activatable = false;
- text.text_changed.disconnect(text_changed);
- text.activate.disconnect(text_activate);
- // if the text has not been edited, restore default text
- if (text.text == "" && !element.has_been_edited)
- {
- text.text = (element as TextElement).display_text;
- }
- else // otherwise, the element has been edited
- {
- element.has_been_edited = true;
- }
-
- // if changes were made to the text, report an UndoAction
- if (original_text != (element as TextElement).text)
- {
- element.undo(undo_action);
- }
- }
-
- /**
- * Signal handler for text editing. Updates the "text" property on the
- * linked { link Element}.
- */
- private void text_changed(Clutter.Text sender)
- {
- (element as TextElement).text = sender.text;
- element.parent.changed(element.parent);
}
/**
- * Signal handler for text "activation", inserts a newline character.
+ * Renders the TextElement's text to the CairoTexture.
*/
- private void text_activate(Clutter.Text sender)
+ private void render_text()
{
- (contents as Clutter.Text).insert_unichar('\n');
+ texture.set_surface_size((uint)texture.width, (uint)texture.height);
+ var cr = texture.create();
+ (element as TextElement).text.render(cr, (int)texture.width,
+ (int)texture.height);
}
}
diff --git a/ease-core/ease-text-element.vala b/ease-core/ease-text-element.vala
index f138403..3076fc8 100644
--- a/ease-core/ease-text-element.vala
+++ b/ease-core/ease-text-element.vala
@@ -25,11 +25,11 @@ public class Ease.TextElement : Element
private const string DEFAULT_TEXT = _("Double click to edit");
/**
- * Creates a new TextElement.
+ * Creates a default text element with an empty block of text.
*/
public TextElement()
{
- signals();
+ text = new Text.with_text("Hello World! This is a sample paragraph.\nThis has a newline before it!", Pango.FontDescription.from_string("Sans 20"));
}
/**
@@ -38,30 +38,11 @@ public class Ease.TextElement : Element
internal TextElement.from_json(Json.Object obj)
{
base.from_json(obj);
-
- text = obj.get_string_member(Theme.TEXT_TEXT);
- color = new Color.from_string(obj.get_string_member(Theme.TEXT_COLOR));
- text_font = obj.get_string_member(Theme.TEXT_FONT);
- text_style_from_string(obj.get_string_member(Theme.TEXT_STYLE));
- text_variant_from_string(obj.get_string_member(Theme.TEXT_VARIANT));
- text_weight_from_string(obj.get_string_member(Theme.TEXT_WEIGHT));
- text_align_from_string(obj.get_string_member(Theme.TEXT_ALIGN));
- text_size_from_string(obj.get_string_member(Theme.TEXT_SIZE));
}
internal override Json.Object to_json()
{
var obj = base.to_json();
-
- obj.set_string_member(Theme.TEXT_COLOR, color.to_string());
- obj.set_string_member(Theme.TEXT_TEXT, text);
- obj.set_string_member(Theme.TEXT_FONT, text_font);
- obj.set_string_member(Theme.TEXT_STYLE, text_style_to_string());
- obj.set_string_member(Theme.TEXT_VARIANT, text_variant_to_string());
- obj.set_string_member(Theme.TEXT_WEIGHT, text_weight_to_string());
- obj.set_string_member(Theme.TEXT_ALIGN, text_align_to_string());
- obj.set_string_member(Theme.TEXT_SIZE, text_size_to_string());
-
return obj;
}
@@ -70,25 +51,6 @@ public class Ease.TextElement : Element
return new TextActor(this, c);
}
- /**
- * This method sets the color of this TextElement, then returns "true".
- *
- * @param c The color to set the element to.
- */
- public override bool set_color(Clutter.Color c)
- {
- color = new Color.from_clutter(c);
- return true;
- }
-
- /**
- * This method returns the color of the TextElement.
- */
- public override Clutter.Color? get_color()
- {
- return color.clutter;
- }
-
public override Gtk.Widget inspector_widget()
{
var builder = new Gtk.Builder();
@@ -98,7 +60,7 @@ public class Ease.TextElement : Element
UI_FILE_PATH)));
}
catch (Error e) { error("Error loading UI: %s", e.message); }
-
+ /*
// connect signals
builder.connect_signals(this);
@@ -170,7 +132,7 @@ public class Ease.TextElement : Element
notify["color"].connect((obj, spec) => {
color_b.color = color.gdk;
- });
+ });*/
// return the root
return builder.get_object("root") as Gtk.Widget;
@@ -179,7 +141,7 @@ public class Ease.TextElement : Element
[CCode (instance_pos = -1)]
public void on_inspector_alignment(Gtk.Widget sender)
{
- (sender.get_parent() as Gtk.Container).foreach((widget) => {
+ /*(sender.get_parent() as Gtk.Container).foreach((widget) => {
(widget as Gtk.Button).relief = Gtk.ReliefStyle.NONE;
});
@@ -194,41 +156,13 @@ public class Ease.TextElement : Element
if (text_align != old)
{
undo(action);
- }
+ }*/
}
protected override string html_render(HTMLExporter exporter)
{
- // open the tag
- string html = "<div class=\"text element\" ";
-
- // set the size and position of the element
- html += "style=\"";
- html += "left:" + x.to_string() + "px;";
- html += " top:" + y.to_string() + "px;";
- html += " width:" + width.to_string() + "px;";
- html += " height:" + width.to_string() + "px;";
- html += " position: absolute;";
-
- // set the text-specific properties of the element
- html += " color:" +
- @"rgb($(color.red8),$(color.green8),$(color.blue8));";
-
- html += " font-family:'" + text_font + "', sans-serif;";
-
- html += " font-size:" + text_size_to_string() + "pt;";
-
- html += " font-weight:" + text_weight_to_string() + ";";
- html += " font-style:" + text_style_to_string().down() +
- ";";
-
- html += " text-align:" + text_align_to_string() + ";\"";
-
- // write the actual content
- html += ">" + text.replace("\n", "<br />") +
- "</div>";
-
- return html;
+ critical("Fix HTML export for text please...");
+ return "";
}
/**
@@ -237,7 +171,7 @@ public class Ease.TextElement : Element
public override void cairo_render(Cairo.Context context,
bool use_small) throws Error
{
- var t = display_text;
+ /*var t = display_text;
// create the layout
var layout = Pango.cairo_create_layout(context);
@@ -250,237 +184,11 @@ public class Ease.TextElement : Element
// render
color.set_cairo(context);
Pango.cairo_update_layout(context, layout);
- Pango.cairo_show_layout(context, layout);
+ Pango.cairo_show_layout(context, layout);*/
}
/**
* The text value of this Element.
*/
- public string text { get; set; }
-
- /**
- * Gets the text this Element should display. This might not be the same as
- * { link text}.
- */
- public string display_text
- {
- get
- {
- return has_been_edited || text.length > 0 ? text : DEFAULT_TEXT;
- }
- }
-
- /**
- * The color of the text.
- */
- public Color color { get; set; }
-
- /**
- * The name of the text's font family.
- */
- public string text_font
- {
- get { return text_font_priv; }
- set
- {
- text_font_priv = value;
- if (!freeze) notify_property("font-description");
- }
- }
- private string text_font_priv;
-
- /**
- * The PangoStyle for this Element.
- */
- public Pango.Style text_style
- {
- get { return text_style_priv; }
- set
- {
- text_style_priv = value;
- if (!freeze) notify_property("font-description");
- }
- }
- private Pango.Style text_style_priv;
-
- public string text_style_to_string()
- {
- switch (text_style)
- {
- case Pango.Style.OBLIQUE:
- return "oblique";
- case Pango.Style.ITALIC:
- return "italic";
- case Pango.Style.NORMAL:
- return "normal";
- default:
- critical("Invalid text style");
- return "normal";
- }
- }
-
- public void text_style_from_string(string str)
- {
- switch (str)
- {
- case "oblique":
- text_style = Pango.Style.OBLIQUE;
- break;
- case "italic":
- text_style = Pango.Style.ITALIC;
- break;
- default:
- text_style = Pango.Style.NORMAL;
- break;
- }
- }
-
- /**
- * The PangoVariant for this Element.
- */
- public Pango.Variant text_variant
- {
- get { return text_variant_priv; }
- set
- {
- text_variant_priv = value;
- if (!freeze) notify_property("font-description");
- }
- }
- private Pango.Variant text_variant_priv;
-
- public void text_variant_from_string(string str)
- {
- text_variant = str == "normal" ?
- Pango.Variant.NORMAL : Pango.Variant.SMALL_CAPS;
- }
-
- public string text_variant_to_string()
- {
- return text_variant == Pango.Variant.NORMAL ? "Normal" : "Small Caps";
- }
-
- /**
- * The font's weight.
- */
- public int text_weight
- {
- get { return text_weight_priv; }
- set
- {
- text_weight_priv = value;
- if (!freeze) notify_property("font-description");
- }
- }
- private int text_weight_priv;
-
-
- public void text_weight_from_string(string str)
- {
- text_weight = (Pango.Weight)(str.to_int());
- }
-
- public string text_weight_to_string()
- {
- return ((int)text_weight).to_string();
- }
-
- /**
- * A full PangoFontDescription for this Element.
- *
- * This property creates a new FontDescription when retrieved, and
- * sets all appropriate properties (text_weight, etc.) when set.
- */
- public Pango.FontDescription font_description
- {
- owned get
- {
- var desc = new Pango.FontDescription();
- desc.set_family(text_font);
- desc.set_style(text_style);
- desc.set_weight((Pango.Weight)text_weight);
- desc.set_variant(text_variant);
- desc.set_size(text_size * Pango.SCALE);
-
- return desc;
- }
- set
- {
- freeze = true;
- text_font = value.get_family();
- text_style = value.get_style();
- text_weight = (int)value.get_weight();
- text_variant = value.get_variant();
- text_size = value.get_size() / Pango.SCALE;
- freeze = false;
- }
- }
-
- /**
- * The alignment of the text.
- */
- public Pango.Alignment text_align { get; set; }
-
- public void text_align_from_string(string str)
- {
- switch (str)
- {
- case "right":
- case "gtk-justify-right":
- text_align = Pango.Alignment.RIGHT;
- break;
- case "center":
- case "gtk-justify-center":
- text_align = Pango.Alignment.CENTER;
- break;
- case "left":
- case "gtk-justify-left":
- text_align = Pango.Alignment.LEFT;
- break;
- default:
- critical("Illegal alignment: %s", str);
- text_align = Pango.Alignment.LEFT;
- break;
- }
- }
-
- public string text_align_to_string()
- {
- switch (text_align)
- {
- case Pango.Alignment.RIGHT:
- return "right";
- case Pango.Alignment.CENTER:
- return "center";
- default:
- return "left";
- }
- }
-
- /**
- * The size of the font.
- *
- * This value should be multiplied by Pango.SCALE for rendering, otherwise
- * the text will be far too small to be visible.
- */
- public int text_size
- {
- get { return text_size_priv; }
- set
- {
- text_size_priv = value;
- if (!freeze) notify_property("font-description");
- }
- }
- private int text_size_priv;
-
- public void text_size_from_string(string str)
- {
- text_size = str.to_int();
- }
-
- public string text_size_to_string()
- {
- return text_size.to_string();
- }
+ public Text text { get; set; }
}
diff --git a/ease-core/ease-text.vala b/ease-core/ease-text.vala
new file mode 100644
index 0000000..d7c416e
--- /dev/null
+++ b/ease-core/ease-text.vala
@@ -0,0 +1,53 @@
+/* Ease, a GTK presentation application
+ Copyright (C) 2010 Nate Stedman
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+/**
+ * A string with various types of Pango formatting applied
+ */
+public class Ease.Text : GLib.Object
+{
+ private Pango.Layout layout;
+ private Pango.Context context;
+ private Pango.AttrList attrs;
+
+ construct
+ {
+ // create context and layout
+ context = Gdk.pango_context_get_for_screen(Gdk.Screen.get_default());
+ layout = new Pango.Layout(context);
+
+ // set layout properties
+ layout.set_ellipsize(Pango.EllipsizeMode.END);
+
+ // create attribute list
+ attrs = new Pango.AttrList();
+ layout.set_attributes(attrs);
+ }
+
+ public Text.with_text(string text, Pango.FontDescription font_description)
+ {
+ layout.set_text(text, (int)text.length);
+ layout.set_font_description(font_description);
+ }
+
+ public void render(Cairo.Context cr, int width, int height)
+ {
+ layout.set_width(width * Pango.SCALE);
+ layout.set_height(height * Pango.SCALE);
+ Pango.cairo_show_layout(cr, layout);
+ }
+}
diff --git a/ease-core/ease-theme.vala b/ease-core/ease-theme.vala
index c30e6ee..b794fe1 100644
--- a/ease-core/ease-theme.vala
+++ b/ease-core/ease-theme.vala
@@ -476,15 +476,15 @@ public class Ease.Theme : GLib.Object
var text = new TextElement();
// set text properties
- text.text_font = element_get(type, TEXT_FONT);
+ /*text.text_font = element_get(type, TEXT_FONT);
text.text_size_from_string(element_get(type, TEXT_SIZE));
text.text_style_from_string(element_get(type, TEXT_STYLE));
text.text_variant_from_string(element_get(type, TEXT_VARIANT));
text.text_weight_from_string(element_get(type, TEXT_WEIGHT));
- text.text_align_from_string(element_get(type, TEXT_ALIGN));
+ text.text_align_from_string(element_get(type, TEXT_ALIGN));*/
// set the color property
- text.color = new Color.from_string(element_get(type, TEXT_COLOR));
+ //text.color = new Color.from_string(element_get(type, TEXT_COLOR));
// set size properties
text.x = x;
@@ -494,8 +494,6 @@ public class Ease.Theme : GLib.Object
// set base properties
text.identifier = type;
- text.has_been_edited = false;
- text.text = "";
return text;
}
diff --git a/ease/ease-editor-embed.vala b/ease/ease-editor-embed.vala
index 61b62fe..a8bd548 100644
--- a/ease/ease-editor-embed.vala
+++ b/ease/ease-editor-embed.vala
@@ -464,7 +464,9 @@ internal class Ease.EditorEmbed : ScrolledEmbedWindow, UndoSource
if (event.click_count == 2)
{
disconnect_keys();
- (sender as Actor).edit(this);
+ (sender as Actor).edit(this,
+ event.x - sender.x,
+ event.y - sender.y);
is_editing = true;
return true;
}
diff --git a/ease/ease-editor-window.vala b/ease/ease-editor-window.vala
index fa35b49..408617b 100644
--- a/ease/ease-editor-window.vala
+++ b/ease/ease-editor-window.vala
@@ -887,11 +887,10 @@ internal class Ease.EditorWindow : Gtk.Window
var text = embed.selected.element as TextElement;
// set the preview text to the element's text, if none, use preview text
- font_selection.set_preview_text(text.text != "" ?
- text.text : FONT_TEXT);
+ font_selection.set_preview_text("Lorem Ipsum Dolor");
// set the dialog's font to the current font
- font_selection.set_font_name(text.font_description.to_string());
+ //font_selection.set_font_name(text.font_description.to_string());
// run the dialog
switch (font_selection.run())
@@ -902,9 +901,9 @@ internal class Ease.EditorWindow : Gtk.Window
new UndoAction(embed.selected.element, "font-description"));
// set the font description to the new font
- text.font_description =
+ /*text.font_description =
Pango.FontDescription.from_string(
- font_selection.get_font_name());
+ font_selection.get_font_name());*/
// emit the "changed" signal on the element's slide
text.changed();
diff --git a/ease/ease-welcome-actor.vala b/ease/ease-welcome-actor.vala
index a54899e..156841d 100644
--- a/ease/ease-welcome-actor.vala
+++ b/ease/ease-welcome-actor.vala
@@ -231,10 +231,10 @@ internal class Ease.WelcomeActor : Clutter.Group
switch (element.identifier)
{
case Theme.TITLE_TEXT:
- (element as TextElement).text = theme.title;
+ //(element as TextElement).text = theme.title;
break;
case Theme.AUTHOR_TEXT:
- (element as TextElement).text = Environment.get_real_name();
+ //(element as TextElement).text = Environment.get_real_name();
break;
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]