From 1c9e7ee09d40b8e06b80d0a09eca17d1b0fb8357 Mon Sep 17 00:00:00 2001 From: Martin Owens Date: Sat, 12 Mar 2016 18:50:04 -0500 Subject: Add a units box to line height and wire in the style units, plus some cleanup (bzr r14701) --- src/widgets/text-toolbar.cpp | 81 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 67 insertions(+), 14 deletions(-) (limited to 'src/widgets/text-toolbar.cpp') diff --git a/src/widgets/text-toolbar.cpp b/src/widgets/text-toolbar.cpp index 5ca92b4c0..c49f0bc05 100644 --- a/src/widgets/text-toolbar.cpp +++ b/src/widgets/text-toolbar.cpp @@ -54,12 +54,17 @@ #include "ui/icon-names.h" #include "ui/tools/text-tool.h" #include "ui/tools/tool-base.h" +#include "ui/widget/unit-tracker.h" +#include "util/units.h" #include "verbs.h" #include "xml/repr.h" using Inkscape::DocumentUndo; using Inkscape::UI::ToolboxFactory; using Inkscape::UI::PrefPusher; +using Inkscape::UI::Widget::UnitTracker; +using Inkscape::Util::Unit; +using Inkscape::Util::unit_table; //#define DEBUG_TEXT @@ -504,8 +509,14 @@ static void sp_text_align_mode_changed( EgeSelectOneAction *act, GObject *tbl ) static void sp_text_lineheight_value_changed( GtkAdjustment *adj, GObject *tbl ) { - // quit if run by the _changed callbacks - if (g_object_get_data(G_OBJECT(tbl), "freeze")) { + UnitTracker *tracker = reinterpret_cast(g_object_get_data(tbl, "tracker")); + + if ( !tracker || tracker->isUpdating() || g_object_get_data(tbl, "freeze")) { + /* + * When only units are being changed, don't treat changes + * to adjuster values as object changes. + * or quit if run by the _changed callbacks + */ return; } g_object_set_data( tbl, "freeze", GINT_TO_POINTER(TRUE) ); @@ -514,7 +525,18 @@ static void sp_text_lineheight_value_changed( GtkAdjustment *adj, GObject *tbl ) // Set css line height. SPCSSAttr *css = sp_repr_css_attr_new (); Inkscape::CSSOStringStream osfs; - osfs << gtk_adjustment_get_value(adj)*100 << "%"; + + gdouble value = gtk_adjustment_get_value(adj); + + Unit const *unit = tracker->getActiveUnit(); + + // Value can only be in px or percent or naked pc (e.g. 0.7 for 70%) + if (unit->abbr != "%") { + value = unit->convert(value, "px"); + unit = unit_table.getUnit("px"); + } + + osfs << value << unit->abbr; sp_repr_css_set_property (css, "line-height", osfs.str().c_str()); // Apply line-height to selected objects. @@ -1073,21 +1095,31 @@ static void sp_text_toolbox_selection_changed(Inkscape::Selection */*selection*/ // Line height (spacing) double height; + + Unit const *lh_unit; + UnitTracker* tracker = reinterpret_cast( g_object_get_data( tbl, "tracker" ) ); + if (query.line_height.normal) { - height = Inkscape::Text::Layout::LINE_HEIGHT_NORMAL; + lh_unit = unit_table.getUnit("%"); + height = Inkscape::Text::Layout::LINE_HEIGHT_NORMAL * 100; + } else if (query.line_height.unit == SP_CSS_UNIT_PERCENT) { + lh_unit = unit_table.getUnit("%"); + height = query.line_height.value * 100; } else { - if (query.line_height.unit == SP_CSS_UNIT_PERCENT) { - height = query.line_height.value; - } else { - height = query.line_height.computed; - } + lh_unit = tracker->getActiveUnit(); + // Can get unit like this: unit_table.getUnit(query.line_height.unit); + height = Inkscape::Util::Quantity::convert(query.line_height.computed, "px", lh_unit); } + // Set before value is set + tracker->setActiveUnit(lh_unit); + GtkAction* lineHeightAction = GTK_ACTION( g_object_get_data( tbl, "TextLineHeightAction" ) ); GtkAdjustment *lineHeightAdjustment = ege_adjustment_action_get_adjustment(EGE_ADJUSTMENT_ACTION( lineHeightAction )); gtk_adjustment_set_value( lineHeightAdjustment, height ); + height = gtk_adjustment_get_value( lineHeightAdjustment ); // Word spacing double wordSpacing; @@ -1290,6 +1322,15 @@ static void sp_text_toolbox_select_cb( GtkEntry* entry, GtkEntryIconPosition /*p static void text_toolbox_watch_ec(SPDesktop* dt, Inkscape::UI::Tools::ToolBase* ec, GObject* holder); +static void destroy_tracker( GObject* obj, gpointer /*user_data*/ ) +{ + UnitTracker *tracker = reinterpret_cast(g_object_get_data(obj, "tracker")); + if ( tracker ) { + delete tracker; + g_object_set_data( obj, "tracker", 0 ); + } +} + // Define all the "widgets" in the toolbar. void sp_text_toolbox_prep(SPDesktop *desktop, GtkActionGroup* mainActions, GObject* holder) { @@ -1588,22 +1629,29 @@ void sp_text_toolbox_prep(SPDesktop *desktop, GtkActionGroup* mainActions, GObje gchar const* labels[] = {_("Smaller spacing"), 0, 0, 0, 0, C_("Text tool", "Normal"), 0, 0, 0, 0, 0, _("Larger spacing")}; gdouble values[] = { 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1,2, 1.3, 1.4, 1.5, 2.0}; + // Add the units menu. + UnitTracker* tracker = new UnitTracker(Inkscape::Util::UNIT_TYPE_LINEAR); + tracker->prependUnit(unit_table.getUnit("%")); + + g_object_set_data( holder, "tracker", tracker ); + g_signal_connect( holder, "destroy", G_CALLBACK(destroy_tracker), holder ); + EgeAdjustmentAction *eact = create_adjustment_action( "TextLineHeightAction", /* name */ _("Line Height"), /* label */ _("Line:"), /* short label */ - _("Spacing between baselines (times font size)"), /* tooltip */ + _("Spacing between baselines"), /* tooltip */ "/tools/text/lineheight", /* preferences path */ - 0.0, /* default */ + 125, /* default */ GTK_WIDGET(desktop->canvas), /* focusTarget */ holder, /* dataKludge */ FALSE, /* set alt-x keyboard shortcut? */ NULL, /* altx_mark */ - 0.0, 10.0, 0.01, 0.10, /* lower, upper, step (arrow up/down), page up/down */ + 0.0, 1e6, 1.0, 10.0, /* lower, upper, step (arrow up/down), page up/down */ labels, values, G_N_ELEMENTS(labels), /* drop down menu */ sp_text_lineheight_value_changed, /* callback */ - NULL, /* unit tracker */ - 0.1, /* step (used?) */ + tracker, /* unit tracker */ + 1.0, /* step (used?) */ 2, /* digits to show */ 1.0 /* factor (multiplies default) */ ); @@ -1611,6 +1659,11 @@ void sp_text_toolbox_prep(SPDesktop *desktop, GtkActionGroup* mainActions, GObje gtk_action_set_sensitive( GTK_ACTION(eact), TRUE ); g_object_set_data( holder, "TextLineHeightAction", eact ); g_object_set( G_OBJECT(eact), "iconId", "text_line_spacing", NULL ); + + GtkAction* act = tracker->createAction( "TextLineHeightUnitAction", _("Units"), ("") ); + gtk_action_group_add_action( mainActions, act ); + g_object_set_data( holder, "TextLineHeightUnitAction", act ); + } /* Word spacing */ -- cgit v1.2.3 From d832c91bf21f6649c1ca7b35bfa7ac67b7832cc3 Mon Sep 17 00:00:00 2001 From: Martin Owens Date: Mon, 14 Mar 2016 23:37:29 -0400 Subject: Commit to using our stored units for now. (bzr r14707) --- src/widgets/text-toolbar.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'src/widgets/text-toolbar.cpp') diff --git a/src/widgets/text-toolbar.cpp b/src/widgets/text-toolbar.cpp index c49f0bc05..60e932338 100644 --- a/src/widgets/text-toolbar.cpp +++ b/src/widgets/text-toolbar.cpp @@ -1106,9 +1106,11 @@ static void sp_text_toolbox_selection_changed(Inkscape::Selection */*selection*/ lh_unit = unit_table.getUnit("%"); height = query.line_height.value * 100; } else { - lh_unit = tracker->getActiveUnit(); - // Can get unit like this: unit_table.getUnit(query.line_height.unit); - height = Inkscape::Util::Quantity::convert(query.line_height.computed, "px", lh_unit); + //Unit const *active = tracker->getActiveUnit(); + // This allows us to show the unit stored to the user, but right now + // it's always px (because Tav said other units are broken/2016) + lh_unit = unit_table.getUnit(query.line_height.unit); + height = query.line_height.computed; } // Set before value is set -- cgit v1.2.3 From 5051da96adc06f2d05c508c2b0ec6ba236d3fdc9 Mon Sep 17 00:00:00 2001 From: Tavmjong Bah Date: Fri, 18 Mar 2016 11:22:38 +0100 Subject: Revert 14707 (bzr r14716.1.1) --- src/widgets/text-toolbar.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'src/widgets/text-toolbar.cpp') diff --git a/src/widgets/text-toolbar.cpp b/src/widgets/text-toolbar.cpp index 60e932338..c49f0bc05 100644 --- a/src/widgets/text-toolbar.cpp +++ b/src/widgets/text-toolbar.cpp @@ -1106,11 +1106,9 @@ static void sp_text_toolbox_selection_changed(Inkscape::Selection */*selection*/ lh_unit = unit_table.getUnit("%"); height = query.line_height.value * 100; } else { - //Unit const *active = tracker->getActiveUnit(); - // This allows us to show the unit stored to the user, but right now - // it's always px (because Tav said other units are broken/2016) - lh_unit = unit_table.getUnit(query.line_height.unit); - height = query.line_height.computed; + lh_unit = tracker->getActiveUnit(); + // Can get unit like this: unit_table.getUnit(query.line_height.unit); + height = Inkscape::Util::Quantity::convert(query.line_height.computed, "px", lh_unit); } // Set before value is set -- cgit v1.2.3 From a714c688d60a3a62114591984f57f26cd41dbed9 Mon Sep 17 00:00:00 2001 From: Tavmjong Bah Date: Fri, 18 Mar 2016 11:23:29 +0100 Subject: Reverting 14701. (bzr r14716.1.2) --- src/widgets/text-toolbar.cpp | 81 ++++++++------------------------------------ 1 file changed, 14 insertions(+), 67 deletions(-) (limited to 'src/widgets/text-toolbar.cpp') diff --git a/src/widgets/text-toolbar.cpp b/src/widgets/text-toolbar.cpp index c49f0bc05..5ca92b4c0 100644 --- a/src/widgets/text-toolbar.cpp +++ b/src/widgets/text-toolbar.cpp @@ -54,17 +54,12 @@ #include "ui/icon-names.h" #include "ui/tools/text-tool.h" #include "ui/tools/tool-base.h" -#include "ui/widget/unit-tracker.h" -#include "util/units.h" #include "verbs.h" #include "xml/repr.h" using Inkscape::DocumentUndo; using Inkscape::UI::ToolboxFactory; using Inkscape::UI::PrefPusher; -using Inkscape::UI::Widget::UnitTracker; -using Inkscape::Util::Unit; -using Inkscape::Util::unit_table; //#define DEBUG_TEXT @@ -509,14 +504,8 @@ static void sp_text_align_mode_changed( EgeSelectOneAction *act, GObject *tbl ) static void sp_text_lineheight_value_changed( GtkAdjustment *adj, GObject *tbl ) { - UnitTracker *tracker = reinterpret_cast(g_object_get_data(tbl, "tracker")); - - if ( !tracker || tracker->isUpdating() || g_object_get_data(tbl, "freeze")) { - /* - * When only units are being changed, don't treat changes - * to adjuster values as object changes. - * or quit if run by the _changed callbacks - */ + // quit if run by the _changed callbacks + if (g_object_get_data(G_OBJECT(tbl), "freeze")) { return; } g_object_set_data( tbl, "freeze", GINT_TO_POINTER(TRUE) ); @@ -525,18 +514,7 @@ static void sp_text_lineheight_value_changed( GtkAdjustment *adj, GObject *tbl ) // Set css line height. SPCSSAttr *css = sp_repr_css_attr_new (); Inkscape::CSSOStringStream osfs; - - gdouble value = gtk_adjustment_get_value(adj); - - Unit const *unit = tracker->getActiveUnit(); - - // Value can only be in px or percent or naked pc (e.g. 0.7 for 70%) - if (unit->abbr != "%") { - value = unit->convert(value, "px"); - unit = unit_table.getUnit("px"); - } - - osfs << value << unit->abbr; + osfs << gtk_adjustment_get_value(adj)*100 << "%"; sp_repr_css_set_property (css, "line-height", osfs.str().c_str()); // Apply line-height to selected objects. @@ -1095,31 +1073,21 @@ static void sp_text_toolbox_selection_changed(Inkscape::Selection */*selection*/ // Line height (spacing) double height; - - Unit const *lh_unit; - UnitTracker* tracker = reinterpret_cast( g_object_get_data( tbl, "tracker" ) ); - if (query.line_height.normal) { - lh_unit = unit_table.getUnit("%"); - height = Inkscape::Text::Layout::LINE_HEIGHT_NORMAL * 100; - } else if (query.line_height.unit == SP_CSS_UNIT_PERCENT) { - lh_unit = unit_table.getUnit("%"); - height = query.line_height.value * 100; + height = Inkscape::Text::Layout::LINE_HEIGHT_NORMAL; } else { - lh_unit = tracker->getActiveUnit(); - // Can get unit like this: unit_table.getUnit(query.line_height.unit); - height = Inkscape::Util::Quantity::convert(query.line_height.computed, "px", lh_unit); + if (query.line_height.unit == SP_CSS_UNIT_PERCENT) { + height = query.line_height.value; + } else { + height = query.line_height.computed; + } } - // Set before value is set - tracker->setActiveUnit(lh_unit); - GtkAction* lineHeightAction = GTK_ACTION( g_object_get_data( tbl, "TextLineHeightAction" ) ); GtkAdjustment *lineHeightAdjustment = ege_adjustment_action_get_adjustment(EGE_ADJUSTMENT_ACTION( lineHeightAction )); gtk_adjustment_set_value( lineHeightAdjustment, height ); - height = gtk_adjustment_get_value( lineHeightAdjustment ); // Word spacing double wordSpacing; @@ -1322,15 +1290,6 @@ static void sp_text_toolbox_select_cb( GtkEntry* entry, GtkEntryIconPosition /*p static void text_toolbox_watch_ec(SPDesktop* dt, Inkscape::UI::Tools::ToolBase* ec, GObject* holder); -static void destroy_tracker( GObject* obj, gpointer /*user_data*/ ) -{ - UnitTracker *tracker = reinterpret_cast(g_object_get_data(obj, "tracker")); - if ( tracker ) { - delete tracker; - g_object_set_data( obj, "tracker", 0 ); - } -} - // Define all the "widgets" in the toolbar. void sp_text_toolbox_prep(SPDesktop *desktop, GtkActionGroup* mainActions, GObject* holder) { @@ -1629,29 +1588,22 @@ void sp_text_toolbox_prep(SPDesktop *desktop, GtkActionGroup* mainActions, GObje gchar const* labels[] = {_("Smaller spacing"), 0, 0, 0, 0, C_("Text tool", "Normal"), 0, 0, 0, 0, 0, _("Larger spacing")}; gdouble values[] = { 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1,2, 1.3, 1.4, 1.5, 2.0}; - // Add the units menu. - UnitTracker* tracker = new UnitTracker(Inkscape::Util::UNIT_TYPE_LINEAR); - tracker->prependUnit(unit_table.getUnit("%")); - - g_object_set_data( holder, "tracker", tracker ); - g_signal_connect( holder, "destroy", G_CALLBACK(destroy_tracker), holder ); - EgeAdjustmentAction *eact = create_adjustment_action( "TextLineHeightAction", /* name */ _("Line Height"), /* label */ _("Line:"), /* short label */ - _("Spacing between baselines"), /* tooltip */ + _("Spacing between baselines (times font size)"), /* tooltip */ "/tools/text/lineheight", /* preferences path */ - 125, /* default */ + 0.0, /* default */ GTK_WIDGET(desktop->canvas), /* focusTarget */ holder, /* dataKludge */ FALSE, /* set alt-x keyboard shortcut? */ NULL, /* altx_mark */ - 0.0, 1e6, 1.0, 10.0, /* lower, upper, step (arrow up/down), page up/down */ + 0.0, 10.0, 0.01, 0.10, /* lower, upper, step (arrow up/down), page up/down */ labels, values, G_N_ELEMENTS(labels), /* drop down menu */ sp_text_lineheight_value_changed, /* callback */ - tracker, /* unit tracker */ - 1.0, /* step (used?) */ + NULL, /* unit tracker */ + 0.1, /* step (used?) */ 2, /* digits to show */ 1.0 /* factor (multiplies default) */ ); @@ -1659,11 +1611,6 @@ void sp_text_toolbox_prep(SPDesktop *desktop, GtkActionGroup* mainActions, GObje gtk_action_set_sensitive( GTK_ACTION(eact), TRUE ); g_object_set_data( holder, "TextLineHeightAction", eact ); g_object_set( G_OBJECT(eact), "iconId", "text_line_spacing", NULL ); - - GtkAction* act = tracker->createAction( "TextLineHeightUnitAction", _("Units"), ("") ); - gtk_action_group_add_action( mainActions, act ); - g_object_set_data( holder, "TextLineHeightUnitAction", act ); - } /* Word spacing */ -- cgit v1.2.3 From 4cce40a79ab973a3609cd5ee7666673bc2337fef Mon Sep 17 00:00:00 2001 From: Tavmjong Bah Date: Tue, 22 Mar 2016 14:56:36 +0100 Subject: Add line-height unit selector to text toolbar. Remove 'm' and 'ft' non-CSS lengths. (bzr r14716.1.3) --- src/widgets/text-toolbar.cpp | 256 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 242 insertions(+), 14 deletions(-) (limited to 'src/widgets/text-toolbar.cpp') diff --git a/src/widgets/text-toolbar.cpp b/src/widgets/text-toolbar.cpp index 5ca92b4c0..8df80d2b6 100644 --- a/src/widgets/text-toolbar.cpp +++ b/src/widgets/text-toolbar.cpp @@ -54,12 +54,18 @@ #include "ui/icon-names.h" #include "ui/tools/text-tool.h" #include "ui/tools/tool-base.h" +#include "ui/widget/unit-tracker.h" +#include "util/units.h" #include "verbs.h" #include "xml/repr.h" using Inkscape::DocumentUndo; using Inkscape::UI::ToolboxFactory; using Inkscape::UI::PrefPusher; +using Inkscape::Util::Unit; +using Inkscape::Util::Quantity; +using Inkscape::Util::unit_table; +using Inkscape::UI::Widget::UnitTracker; //#define DEBUG_TEXT @@ -510,25 +516,48 @@ static void sp_text_lineheight_value_changed( GtkAdjustment *adj, GObject *tbl ) } g_object_set_data( tbl, "freeze", GINT_TO_POINTER(TRUE) ); - // At the moment this handles only numerical values (i.e. no percent). + + // Get user selected unit and save as preference + UnitTracker *tracker = reinterpret_cast(g_object_get_data(tbl, "tracker")); + Unit const *unit = tracker->getActiveUnit(); + g_return_if_fail(unit != NULL); + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); + + + // This nonsense is to get SP_CSS_UNIT_xx value corresponding to unit so + // we can save it (allows us to adjust line height value when unit changes). + SPILength temp_length; + Inkscape::CSSOStringStream temp_stream; + temp_stream << 1 << unit->abbr; + temp_length.read(temp_stream.str().c_str()); + prefs->setInt("/tools/text/lineheight/display_unit", temp_length.unit); + g_object_set_data( tbl, "lineheight_unit", GINT_TO_POINTER(temp_length.unit)); + + // Set css line height. SPCSSAttr *css = sp_repr_css_attr_new (); Inkscape::CSSOStringStream osfs; - osfs << gtk_adjustment_get_value(adj)*100 << "%"; + // We should handle unitless values as well as 'em' and 'ex' + if ((unit->abbr) == "em" || unit->abbr == "ex" || unit->abbr == "%") { + osfs << gtk_adjustment_get_value(adj) << unit->abbr; + } else { + // Inside SVG file, always use "px" for absolute units. + osfs << Quantity::convert(gtk_adjustment_get_value(adj), unit, "px") << "px"; + } sp_repr_css_set_property (css, "line-height", osfs.str().c_str()); + // Apply line-height to selected objects. SPDesktop *desktop = SP_ACTIVE_DESKTOP; sp_desktop_set_style (desktop, css, true, false); - // Until deprecated sodipodi:linespacing purged: + // Only need to save for undo if a text item has been changed. Inkscape::Selection *selection = desktop->getSelection(); bool modmade = false; std::vector itemlist=selection->itemList(); for(std::vector::const_iterator i=itemlist.begin();i!=itemlist.end(); ++i){ if (SP_IS_TEXT (*i)) { - (*i)->getRepr()->setAttribute("sodipodi:linespacing", sp_repr_css_property (css, "line-height", NULL)); modmade = true; } } @@ -554,6 +583,153 @@ static void sp_text_lineheight_value_changed( GtkAdjustment *adj, GObject *tbl ) g_object_set_data( tbl, "freeze", GINT_TO_POINTER(FALSE) ); } + +static void sp_text_lineheight_unit_changed( gpointer /* */, GObject *tbl ) +{ + // quit if run by the _changed callbacks + if (g_object_get_data(G_OBJECT(tbl), "freeze")) { + return; + } + g_object_set_data( tbl, "freeze", GINT_TO_POINTER(TRUE) ); + + // Get old saved unit + int old_unit = GPOINTER_TO_INT( g_object_get_data(tbl, "lineheight_unit")); + + // Get user selected unit and save as preference + UnitTracker *tracker = reinterpret_cast(g_object_get_data(tbl, "tracker")); + Unit const *unit = tracker->getActiveUnit(); + g_return_if_fail(unit != NULL); + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); + + // This nonsense is to get SP_CSS_UNIT_xx value corresponding to unit. + SPILength temp_length; + Inkscape::CSSOStringStream temp_stream; + temp_stream << 1 << unit->abbr; + temp_length.read(temp_stream.str().c_str()); + prefs->setInt("/tools/text/lineheight/display_unit", temp_length.unit); + g_object_set_data( tbl, "lineheight_unit", GINT_TO_POINTER(temp_length.unit)); + + // Read current line height value + EgeAdjustmentAction *line_height_act = + reinterpret_cast(g_object_get_data(tbl, "TextLineHeightAction")); + GtkAdjustment *line_height_adj = ege_adjustment_action_get_adjustment( line_height_act ); + double line_height = gtk_adjustment_get_value(line_height_adj); + + SPDesktop *desktop = SP_ACTIVE_DESKTOP; + Inkscape::Selection *selection = desktop->getSelection(); + std::vector itemlist=selection->itemList(); + + // Convert between units + if ((unit->abbr) == "em" && old_unit == SP_CSS_UNIT_EX) { + line_height *= 0.5; + } else if ((unit->abbr) == "ex" && old_unit == SP_CSS_UNIT_EM) { + line_height *= 2.0; + } else if ((unit->abbr) == "em" && old_unit == SP_CSS_UNIT_PERCENT) { + line_height /= 100.0; + } else if ((unit->abbr) == "%" && old_unit == SP_CSS_UNIT_EM) { + line_height *= 100; + } else if ((unit->abbr) == "ex" && old_unit == SP_CSS_UNIT_PERCENT) { + line_height /= 50.0; + } else if ((unit->abbr) == "%" && old_unit == SP_CSS_UNIT_EX) { + line_height *= 50; + } else if ((unit->abbr) == "%" || (unit->abbr) == "em" || (unit->abbr) == "ex") { + // Convert absolute to relative... for the moment use average font-size + double font_size = 0; + int count = 0; + for(std::vector::const_iterator i=itemlist.begin();i!=itemlist.end(); ++i){ + if (SP_IS_TEXT (*i)) { + font_size += (*i)->style->font_size.computed; + ++count; + } + } + if (count > 0) { + font_size /= count; + } else { + font_size = 20; + } + line_height = Quantity::convert(line_height, sp_style_get_css_unit_string(old_unit), "px"); + if (font_size > 0) { + line_height /= font_size; + } + if ((unit->abbr) == "%") { + line_height *= 100; + } else if ((unit->abbr) == "ex") { + line_height *= 2; + } + } else if (old_unit==SP_CSS_UNIT_PERCENT || old_unit==SP_CSS_UNIT_EM || old_unit==SP_CSS_UNIT_EX) { + // Convert relative to absolute... for the moment use average font-size + double font_size = 0; + int count = 0; + for(std::vector::const_iterator i=itemlist.begin();i!=itemlist.end(); ++i){ + if (SP_IS_TEXT (*i)) { + font_size += (*i)->style->font_size.computed; + ++count; + } + } + if (count > 0) { + font_size /= count; + } else { + font_size = 20; + } + line_height *= font_size; + if (old_unit == SP_CSS_UNIT_PERCENT) { + line_height /= 100.0; + } else if (old_unit == SP_CSS_UNIT_EX) { + line_height /= 2.0; + } + line_height = Quantity::convert(line_height, "px", unit); + } else { + // Convert between different absolute units (only used in GUI) + line_height = Quantity::convert(line_height, sp_style_get_css_unit_string(old_unit), unit); + } + + // Set css line height. + SPCSSAttr *css = sp_repr_css_attr_new (); + Inkscape::CSSOStringStream osfs; + // We should handle unitless values as well as 'em' and 'ex' + if ((unit->abbr) == "em" || unit->abbr == "ex" || unit->abbr == "%") { + osfs << line_height << unit->abbr; + } else { + osfs << Quantity::convert(line_height, unit, "px") << "px"; + } + sp_repr_css_set_property (css, "line-height", osfs.str().c_str()); + + // Update GUI with line_height value. + gtk_adjustment_set_value(line_height_adj, line_height); + + // Apply line-height to selected objects. + sp_desktop_set_style (desktop, css, true, false); + + // Only need to save for undo if a text item has been changed. + bool modmade = false; + for(std::vector::const_iterator i=itemlist.begin();i!=itemlist.end(); ++i){ + if (SP_IS_TEXT (*i)) { + modmade = true; + } + } + + // Save for undo + if(modmade) { + DocumentUndo::maybeDone(SP_ACTIVE_DESKTOP->getDocument(), "ttb:line-height", SP_VERB_NONE, + _("Text: Change line-height unit")); + } + + // If no selected objects, set default. + SPStyle query(SP_ACTIVE_DOCUMENT); + int result_numbers = + sp_desktop_query_style (SP_ACTIVE_DESKTOP, &query, QUERY_STYLE_PROPERTY_FONTNUMBERS); + if (result_numbers == QUERY_STYLE_NOTHING) + { + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); + prefs->mergeStyle("/tools/text/style", css); + } + + sp_repr_css_attr_unref (css); + + g_object_set_data( tbl, "freeze", GINT_TO_POINTER(FALSE) ); +} + + static void sp_text_wordspacing_value_changed( GtkAdjustment *adj, GObject *tbl ) { // quit if run by the _changed callbacks @@ -1064,6 +1240,7 @@ static void sp_text_toolbox_selection_changed(Inkscape::Selection */*selection*/ { activeButton = 3; } else { + // This should take 'direction' into account if (query.text_anchor.computed == SP_CSS_TEXT_ANCHOR_START) activeButton = 0; if (query.text_anchor.computed == SP_CSS_TEXT_ANCHOR_MIDDLE) activeButton = 1; if (query.text_anchor.computed == SP_CSS_TEXT_ANCHOR_END) activeButton = 2; @@ -1071,16 +1248,47 @@ static void sp_text_toolbox_selection_changed(Inkscape::Selection */*selection*/ ege_select_one_action_set_active( textAlignAction, activeButton ); - // Line height (spacing) + // Line height (spacing) and line height unit double height; + int line_height_unit = -1; if (query.line_height.normal) { + std::cout << " normal" << std::endl; height = Inkscape::Text::Layout::LINE_HEIGHT_NORMAL; + line_height_unit = SP_CSS_UNIT_NONE; } else { - if (query.line_height.unit == SP_CSS_UNIT_PERCENT) { - height = query.line_height.value; - } else { - height = query.line_height.computed; - } + height = query.line_height.value; + line_height_unit = query.line_height.unit; + } + + switch (line_height_unit) { + case SP_CSS_UNIT_NONE: + // tracker can't show no unit... use 'em' + line_height_unit = SP_CSS_UNIT_EM; + case SP_CSS_UNIT_EM: + case SP_CSS_UNIT_EX: + break; + case SP_CSS_UNIT_PERCENT: + height *= 100.0; // Inkscape store % as fraction in .value + break; + case SP_CSS_UNIT_PX: + // If unit is set to 'px', use the preferred display unit (if absolute). + line_height_unit = + prefs->getInt("/tools/text/lineheight/display_unit", SP_CSS_UNIT_PT); + if (line_height_unit != SP_CSS_UNIT_EM && + line_height_unit != SP_CSS_UNIT_EX && + line_height_unit != SP_CSS_UNIT_PERCENT) { + height = + Quantity::convert(height, "px", sp_style_get_css_unit_string(line_height_unit)); + } else { + line_height_unit = SP_CSS_UNIT_PX; + } + break; + default: + // If unit has been set by an external program to something other than 'px', use + // that unit. But height is average of computed values (px) so we need to convert + // back. + height = + Quantity::convert(height, "px", sp_style_get_css_unit_string(line_height_unit)); } GtkAction* lineHeightAction = GTK_ACTION( g_object_get_data( tbl, "TextLineHeightAction" ) ); @@ -1088,7 +1296,11 @@ static void sp_text_toolbox_selection_changed(Inkscape::Selection */*selection*/ ege_adjustment_action_get_adjustment(EGE_ADJUSTMENT_ACTION( lineHeightAction )); gtk_adjustment_set_value( lineHeightAdjustment, height ); - + UnitTracker* tracker = reinterpret_cast( g_object_get_data( tbl, "tracker" ) ); + tracker->setActiveUnitByAbbr(sp_style_get_css_unit_string(line_height_unit)); + // Save unit so we can do convertions between new/old units. + g_object_set_data( tbl, "lineheight_unit", GINT_TO_POINTER(line_height_unit)); + // Word spacing double wordSpacing; if (query.word_spacing.normal) wordSpacing = 0.0; @@ -1231,7 +1443,6 @@ static void sp_text_toolbox_selection_changed(Inkscape::Selection */*selection*/ #endif g_object_set_data( tbl, "freeze", GINT_TO_POINTER(FALSE) ); - } static void sp_text_toolbox_selection_modified(Inkscape::Selection *selection, guint /*flags*/, GObject *tbl) @@ -1582,6 +1793,15 @@ void sp_text_toolbox_prep(SPDesktop *desktop, GtkActionGroup* mainActions, GObje g_signal_connect_after( G_OBJECT(act), "changed", G_CALLBACK(sp_text_orientation_changed), holder ); } + /* Line height unit tracker */ + UnitTracker* tracker = new UnitTracker(Inkscape::Util::UNIT_TYPE_LINEAR); + tracker->addUnit(unit_table.getUnit("%")); + tracker->addUnit(unit_table.getUnit("em")); + tracker->addUnit(unit_table.getUnit("ex")); + // tracker->addUnit(unit_table.getUnit("None")); + tracker->setActiveUnit(unit_table.getUnit("%")); + g_object_set_data( holder, "tracker", tracker ); + /* Line height */ { // Drop down menu @@ -1599,20 +1819,28 @@ void sp_text_toolbox_prep(SPDesktop *desktop, GtkActionGroup* mainActions, GObje holder, /* dataKludge */ FALSE, /* set alt-x keyboard shortcut? */ NULL, /* altx_mark */ - 0.0, 10.0, 0.01, 0.10, /* lower, upper, step (arrow up/down), page up/down */ + 0.0, 1000.0, 1.0, 10.0, /* lower, upper, step (arrow up/down), page up/down */ labels, values, G_N_ELEMENTS(labels), /* drop down menu */ sp_text_lineheight_value_changed, /* callback */ - NULL, /* unit tracker */ + NULL, // tracker, /* unit tracker */ 0.1, /* step (used?) */ 2, /* digits to show */ 1.0 /* factor (multiplies default) */ ); + //tracker->addAdjustment( ege_adjustment_action_get_adjustment(eact) ); gtk_action_group_add_action( mainActions, GTK_ACTION(eact) ); gtk_action_set_sensitive( GTK_ACTION(eact), TRUE ); g_object_set_data( holder, "TextLineHeightAction", eact ); g_object_set( G_OBJECT(eact), "iconId", "text_line_spacing", NULL ); } + /* Line height units */ + { + GtkAction* act = tracker->createAction( "TextLineHeightUnitsAction", _("Units"), ("") ); + gtk_action_group_add_action( mainActions, act ); + g_signal_connect_after( G_OBJECT(act), "changed", G_CALLBACK(sp_text_lineheight_unit_changed), holder ); + } + /* Word spacing */ { // Drop down menu -- cgit v1.2.3 From a18efaedfcc01ec5bd1b9c60ca55b8be4445c5cb Mon Sep 17 00:00:00 2001 From: Tavmjong Bah Date: Wed, 30 Mar 2016 19:26:19 +0200 Subject: Partial fix for bug 156221 ('line-height' with non-pixel scaled drawing). (bzr r14752) --- src/widgets/text-toolbar.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src/widgets/text-toolbar.cpp') diff --git a/src/widgets/text-toolbar.cpp b/src/widgets/text-toolbar.cpp index 8df80d2b6..e71c911bd 100644 --- a/src/widgets/text-toolbar.cpp +++ b/src/widgets/text-toolbar.cpp @@ -638,7 +638,8 @@ static void sp_text_lineheight_unit_changed( gpointer /* */, GObject *tbl ) int count = 0; for(std::vector::const_iterator i=itemlist.begin();i!=itemlist.end(); ++i){ if (SP_IS_TEXT (*i)) { - font_size += (*i)->style->font_size.computed; + double doc_scale = Geom::Affine((*i)->i2dt_affine()).descrim(); + font_size += (*i)->style->font_size.computed * doc_scale; ++count; } } @@ -662,7 +663,8 @@ static void sp_text_lineheight_unit_changed( gpointer /* */, GObject *tbl ) int count = 0; for(std::vector::const_iterator i=itemlist.begin();i!=itemlist.end(); ++i){ if (SP_IS_TEXT (*i)) { - font_size += (*i)->style->font_size.computed; + double doc_scale = Geom::Affine((*i)->i2dt_affine()).descrim(); + font_size += (*i)->style->font_size.computed * doc_scale; ++count; } } @@ -671,12 +673,13 @@ static void sp_text_lineheight_unit_changed( gpointer /* */, GObject *tbl ) } else { font_size = 20; } - line_height *= font_size; + if (old_unit == SP_CSS_UNIT_PERCENT) { line_height /= 100.0; } else if (old_unit == SP_CSS_UNIT_EX) { line_height /= 2.0; } + line_height *= font_size; line_height = Quantity::convert(line_height, "px", unit); } else { // Convert between different absolute units (only used in GUI) @@ -1252,7 +1255,6 @@ static void sp_text_toolbox_selection_changed(Inkscape::Selection */*selection*/ double height; int line_height_unit = -1; if (query.line_height.normal) { - std::cout << " normal" << std::endl; height = Inkscape::Text::Layout::LINE_HEIGHT_NORMAL; line_height_unit = SP_CSS_UNIT_NONE; } else { -- cgit v1.2.3 From 0ed6ffcc46be15caa8974c2778ec2b86e6139ab2 Mon Sep 17 00:00:00 2001 From: Tavmjong Bah Date: Wed, 13 Apr 2016 11:11:33 +0200 Subject: Restore ability to set unitless 'line-height'. Hackfest 2016 Note: Rendering with 'line-height' with units is buggy. (bzr r14814) --- src/widgets/text-toolbar.cpp | 79 +++++++++++++++++++++++++++----------------- 1 file changed, 48 insertions(+), 31 deletions(-) (limited to 'src/widgets/text-toolbar.cpp') diff --git a/src/widgets/text-toolbar.cpp b/src/widgets/text-toolbar.cpp index e71c911bd..661fc6fa9 100644 --- a/src/widgets/text-toolbar.cpp +++ b/src/widgets/text-toolbar.cpp @@ -508,6 +508,10 @@ static void sp_text_align_mode_changed( EgeSelectOneAction *act, GObject *tbl ) g_object_set_data( tbl, "freeze", GINT_TO_POINTER(FALSE) ); } +static bool is_relative( Unit const *unit ) { + return (unit->abbr == "" || unit->abbr == "em" || unit->abbr == "ex" || unit->abbr == "%"); +} + static void sp_text_lineheight_value_changed( GtkAdjustment *adj, GObject *tbl ) { // quit if run by the _changed callbacks @@ -524,21 +528,22 @@ static void sp_text_lineheight_value_changed( GtkAdjustment *adj, GObject *tbl ) Inkscape::Preferences *prefs = Inkscape::Preferences::get(); - // This nonsense is to get SP_CSS_UNIT_xx value corresponding to unit so - // we can save it (allows us to adjust line height value when unit changes). - SPILength temp_length; - Inkscape::CSSOStringStream temp_stream; - temp_stream << 1 << unit->abbr; - temp_length.read(temp_stream.str().c_str()); - prefs->setInt("/tools/text/lineheight/display_unit", temp_length.unit); - g_object_set_data( tbl, "lineheight_unit", GINT_TO_POINTER(temp_length.unit)); - + // Only save if not relative unit + if ( !is_relative(unit) ) { + // This nonsense is to get SP_CSS_UNIT_xx value corresponding to unit so + // we can save it (allows us to adjust line height value when unit changes). + SPILength temp_length; + Inkscape::CSSOStringStream temp_stream; + temp_stream << 1 << unit->abbr; + temp_length.read(temp_stream.str().c_str()); + prefs->setInt("/tools/text/lineheight/display_unit", temp_length.unit); + g_object_set_data( tbl, "lineheight_unit", GINT_TO_POINTER(temp_length.unit)); + } // Set css line height. SPCSSAttr *css = sp_repr_css_attr_new (); Inkscape::CSSOStringStream osfs; - // We should handle unitless values as well as 'em' and 'ex' - if ((unit->abbr) == "em" || unit->abbr == "ex" || unit->abbr == "%") { + if ( is_relative(unit) ) { osfs << gtk_adjustment_get_value(adj) << unit->abbr; } else { // Inside SVG file, always use "px" for absolute units. @@ -601,13 +606,16 @@ static void sp_text_lineheight_unit_changed( gpointer /* */, GObject *tbl ) g_return_if_fail(unit != NULL); Inkscape::Preferences *prefs = Inkscape::Preferences::get(); - // This nonsense is to get SP_CSS_UNIT_xx value corresponding to unit. - SPILength temp_length; - Inkscape::CSSOStringStream temp_stream; - temp_stream << 1 << unit->abbr; - temp_length.read(temp_stream.str().c_str()); - prefs->setInt("/tools/text/lineheight/display_unit", temp_length.unit); - g_object_set_data( tbl, "lineheight_unit", GINT_TO_POINTER(temp_length.unit)); + // Only save if not relative unit + if ( !is_relative(unit) ) { + // This nonsense is to get SP_CSS_UNIT_xx value corresponding to unit. + SPILength temp_length; + Inkscape::CSSOStringStream temp_stream; + temp_stream << 1 << unit->abbr; + temp_length.read(temp_stream.str().c_str()); + prefs->setInt("/tools/text/lineheight/display_unit", temp_length.unit); + g_object_set_data( tbl, "lineheight_unit", GINT_TO_POINTER(temp_length.unit)); + } // Read current line height value EgeAdjustmentAction *line_height_act = @@ -620,19 +628,19 @@ static void sp_text_lineheight_unit_changed( gpointer /* */, GObject *tbl ) std::vector itemlist=selection->itemList(); // Convert between units - if ((unit->abbr) == "em" && old_unit == SP_CSS_UNIT_EX) { + if ((unit->abbr == "" || unit->abbr == "em") && old_unit == SP_CSS_UNIT_EX) { line_height *= 0.5; - } else if ((unit->abbr) == "ex" && old_unit == SP_CSS_UNIT_EM) { + } else if ((unit->abbr) == "ex" && (old_unit == SP_CSS_UNIT_EM || old_unit == SP_CSS_UNIT_NONE) ) { line_height *= 2.0; - } else if ((unit->abbr) == "em" && old_unit == SP_CSS_UNIT_PERCENT) { + } else if ((unit->abbr == "" || unit->abbr == "em") && old_unit == SP_CSS_UNIT_PERCENT) { line_height /= 100.0; - } else if ((unit->abbr) == "%" && old_unit == SP_CSS_UNIT_EM) { + } else if ((unit->abbr) == "%" && (old_unit == SP_CSS_UNIT_EM || old_unit == SP_CSS_UNIT_NONE) ) { line_height *= 100; } else if ((unit->abbr) == "ex" && old_unit == SP_CSS_UNIT_PERCENT) { line_height /= 50.0; } else if ((unit->abbr) == "%" && old_unit == SP_CSS_UNIT_EX) { line_height *= 50; - } else if ((unit->abbr) == "%" || (unit->abbr) == "em" || (unit->abbr) == "ex") { + } else if (is_relative(unit)) { // Convert absolute to relative... for the moment use average font-size double font_size = 0; int count = 0; @@ -648,7 +656,10 @@ static void sp_text_lineheight_unit_changed( gpointer /* */, GObject *tbl ) } else { font_size = 20; } + + if (old_unit == SP_CSS_UNIT_NONE) old_unit = SP_CSS_UNIT_EM; line_height = Quantity::convert(line_height, sp_style_get_css_unit_string(old_unit), "px"); + if (font_size > 0) { line_height /= font_size; } @@ -657,7 +668,8 @@ static void sp_text_lineheight_unit_changed( gpointer /* */, GObject *tbl ) } else if ((unit->abbr) == "ex") { line_height *= 2; } - } else if (old_unit==SP_CSS_UNIT_PERCENT || old_unit==SP_CSS_UNIT_EM || old_unit==SP_CSS_UNIT_EX) { + } else if (old_unit==SP_CSS_UNIT_NONE || old_unit==SP_CSS_UNIT_PERCENT || + old_unit==SP_CSS_UNIT_EM || old_unit==SP_CSS_UNIT_EX) { // Convert relative to absolute... for the moment use average font-size double font_size = 0; int count = 0; @@ -689,8 +701,7 @@ static void sp_text_lineheight_unit_changed( gpointer /* */, GObject *tbl ) // Set css line height. SPCSSAttr *css = sp_repr_css_attr_new (); Inkscape::CSSOStringStream osfs; - // We should handle unitless values as well as 'em' and 'ex' - if ((unit->abbr) == "em" || unit->abbr == "ex" || unit->abbr == "%") { + if ( is_relative(unit) ) { osfs << line_height << unit->abbr; } else { osfs << Quantity::convert(line_height, unit, "px") << "px"; @@ -1264,8 +1275,6 @@ static void sp_text_toolbox_selection_changed(Inkscape::Selection */*selection*/ switch (line_height_unit) { case SP_CSS_UNIT_NONE: - // tracker can't show no unit... use 'em' - line_height_unit = SP_CSS_UNIT_EM; case SP_CSS_UNIT_EM: case SP_CSS_UNIT_EX: break; @@ -1276,7 +1285,9 @@ static void sp_text_toolbox_selection_changed(Inkscape::Selection */*selection*/ // If unit is set to 'px', use the preferred display unit (if absolute). line_height_unit = prefs->getInt("/tools/text/lineheight/display_unit", SP_CSS_UNIT_PT); - if (line_height_unit != SP_CSS_UNIT_EM && + // But not if prefered unit is relative + if (line_height_unit != SP_CSS_UNIT_NONE && + line_height_unit != SP_CSS_UNIT_EM && line_height_unit != SP_CSS_UNIT_EX && line_height_unit != SP_CSS_UNIT_PERCENT) { height = @@ -1299,7 +1310,13 @@ static void sp_text_toolbox_selection_changed(Inkscape::Selection */*selection*/ gtk_adjustment_set_value( lineHeightAdjustment, height ); UnitTracker* tracker = reinterpret_cast( g_object_get_data( tbl, "tracker" ) ); - tracker->setActiveUnitByAbbr(sp_style_get_css_unit_string(line_height_unit)); + if( line_height_unit == SP_CSS_UNIT_NONE ) { + // Function 'sp_style_get_css_unit_string' returns 'px' for unit none. + // We need to avoid this. + tracker->setActiveUnitByAbbr(""); + } else { + tracker->setActiveUnitByAbbr(sp_style_get_css_unit_string(line_height_unit)); + } // Save unit so we can do convertions between new/old units. g_object_set_data( tbl, "lineheight_unit", GINT_TO_POINTER(line_height_unit)); @@ -1797,10 +1814,10 @@ void sp_text_toolbox_prep(SPDesktop *desktop, GtkActionGroup* mainActions, GObje /* Line height unit tracker */ UnitTracker* tracker = new UnitTracker(Inkscape::Util::UNIT_TYPE_LINEAR); + tracker->prependUnit(unit_table.getUnit("")); // No unit tracker->addUnit(unit_table.getUnit("%")); tracker->addUnit(unit_table.getUnit("em")); tracker->addUnit(unit_table.getUnit("ex")); - // tracker->addUnit(unit_table.getUnit("None")); tracker->setActiveUnit(unit_table.getUnit("%")); g_object_set_data( holder, "tracker", tracker ); -- cgit v1.2.3 From 1853340f66e25dbece6666c7ebf71f93ed203ba7 Mon Sep 17 00:00:00 2001 From: Alex Valavanis Date: Wed, 13 Apr 2016 14:45:53 +0100 Subject: text-toolbar: Gtk+ 3 theming #Hackfest2016 (bzr r14827) --- src/widgets/text-toolbar.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src/widgets/text-toolbar.cpp') diff --git a/src/widgets/text-toolbar.cpp b/src/widgets/text-toolbar.cpp index 661fc6fa9..23acb74af 100644 --- a/src/widgets/text-toolbar.cpp +++ b/src/widgets/text-toolbar.cpp @@ -1562,6 +1562,23 @@ void sp_text_toolbox_prep(SPDesktop *desktop, GtkActionGroup* mainActions, GObje g_object_set_data( holder, "TextFontFamilyAction", act ); // Change style of drop-down from menu to list +#if GTK_CHECK_VERSION(3,0,0) + GtkCssProvider *css_provider = gtk_css_provider_new(); + gtk_css_provider_load_from_data(css_provider, + "#TextFontFamilyAction_combobox {\n" + " -GtkComboBox-appears-as-list: true;\n" + "}\n" + "combobox window.popup scrolledwindow treeview separator {\n" + " -GtkWidget-wide-separators: true;\n" + " -GtkWidget-separator-height: 6;\n" + "}\n", + -1, NULL); + + GdkScreen *screen = gdk_screen_get_default(); + gtk_style_context_add_provider_for_screen(screen, + GTK_STYLE_PROVIDER(css_provider), + GTK_STYLE_PROVIDER_PRIORITY_USER); +#else gtk_rc_parse_string ( "style \"dropdown-as-list-style\"\n" "{\n" @@ -1574,6 +1591,7 @@ void sp_text_toolbox_prep(SPDesktop *desktop, GtkActionGroup* mainActions, GObje " GtkWidget::separator-height = 6\n" "}\n" "widget \"*gtk-combobox-popup-window.GtkScrolledWindow.GtkTreeView\" style \"fontfamily-separator-style\""); +#endif } /* Font size */ -- cgit v1.2.3