desktop-style.cpp revision d539b73cdfe899f4791f8f07f926bbc476db190c
/*
* Desktop style management
*
* Authors:
* bulia byak
* verbalshadow
* Jon A. Cruz <jon@joncruz.org>
* Abhishek Sharma
*
* Copyright (C) 2004, 2006 authors
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#include <string>
#include <cstring>
#include "desktop.h"
#include "color-rgba.h"
#include "svg/css-ostringstream.h"
#include "svg/svg-color.h"
#include "selection.h"
#include "inkscape.h"
#include "style.h"
#include "preferences.h"
#include "sp-use.h"
#include "sp-filter.h"
#include "sp-filter-reference.h"
#include "filters/gaussian-blur.h"
#include "sp-flowtext.h"
#include "sp-flowregion.h"
#include "sp-flowdiv.h"
#include "sp-linear-gradient.h"
#include "sp-pattern.h"
#include "sp-radial-gradient.h"
#include "sp-textpath.h"
#include "sp-tref.h"
#include "sp-tspan.h"
#include "xml/sp-css-attr.h"
#include "libnrtype/font-style-to-pos.h"
#include "sp-path.h"
#include "event-context.h"
#include "desktop-style.h"
#include "svg/svg-icc-color.h"
#include "box3d-side.h"
/**
* Set color on selection on desktop.
*/
void
{
/// \todo relative color setting
if (is_relative) {
g_warning("FIXME: relative color setting not yet implemented");
return;
}
gchar b[64];
sp_svg_write_color(b, sizeof(b), rgba);
if (fill) {
} else {
}
}
/**
* Apply style on object and children, recursively.
*/
void
{
// non-items should not have style
if (!SP_IS_ITEM(o))
return;
// 1. tspans with role=line are not regular objects in that they are not supposed to have style of their own,
// but must always inherit from the parent text. Same for textPath.
// However, if the line tspan or textPath contains some style (old file?), we reluctantly set our style to it too.
// 2. Generally we allow setting style on clones, but when it's inside flowRegion, do not touch
// it, be it clone or not; it's just styleless shape (because that's how Inkscape does
// flowtext).
if (!(skip_lines
|| SP_IS_FLOWDIV(o)
|| SP_IS_FLOWPARA(o)
|| SP_IS_TEXTPATH(o))
&& !o->getAttribute("style"))
&&
!(SP_IS_FLOWREGION(o) ||
SP_IS_FLOWREGIONEXCLUDE(o) ||
(SP_IS_USE(o) &&
o->parent &&
(SP_IS_FLOWREGION(o->parent) ||
)
)
)
) {
// Scale the style by the inverse of the accumulated parent transform in the paste context.
{
if ( ( ex != 0. )
&& ( ex != 1. ) ) {
}
}
}
// setting style on child of clone spills into the clone original (via shared repr), don't do it!
if (SP_IS_USE(o))
return;
// Unset properties which are accumulating and thus should not be set recursively.
// For example, setting opacity 0.5 on a group recursively would result in the visible opacity of 0.25 for an item in the group.
} else {
}
}
}
/**
* Apply style on selection on desktop.
*/
void
{
if (write_current) {
// 1. Set internal value
// 1a. Write to prefs; make a copy and unset any URIs first
/* last used styles for 3D box faces are stored separately */
if (SP_IS_BOX3D_SIDE (i->data)) {
}
}
}
}
if (!change)
return;
// 2. Emit signal
/** \todo
* FIXME: in set_style, compensate pattern and gradient fills, stroke width,
* rect corners, font size for the object's own transform so that pasting
*/
// 3. If nobody has intercepted the signal, apply the style to the selection
if (!intercepted) {
// If we have an event context, update its cursor (TODO: it could be neater to do this with the signal sent above, but what if the signal gets intercepted?)
if (desktop->event_context) {
}
// Remove text attributes if not text...
// Do this once in case a zillion objects are selected.
// If not text, don't apply text attributes (can a group have text attributes?)
} else {
}
}
}
}
/**
* Return the desktop's current style.
*/
{
if (!css->attributeList()) {
return NULL;
} else {
if (!with_text) {
}
return css;
}
}
/**
* Return the desktop's current color.
*/
{
guint32 r = 0; // if there's no color, return black
"#000");
// read it
r = sp_svg_read_color(property, r);
}
}
return r;
}
double
sp_desktop_get_master_opacity_tool(SPDesktop *desktop, Glib::ustring const &tool, bool *has_opacity)
{
if (has_opacity)
*has_opacity = false;
} else {
}
if (css) {
} else {
if (has_opacity)
*has_opacity = true;
}
}
}
return value;
}
double
{
} else {
}
if (css) {
gchar const *property = css ? sp_repr_css_property(css, is_fill ? "fill-opacity": "stroke-opacity", "1.000") : 0;
}
}
}
return value;
}
sp_desktop_get_color_tool(SPDesktop *desktop, Glib::ustring const &tool, bool is_fill, bool *has_color)
{
guint32 r = 0; // if there's no color, return black
if (has_color)
*has_color = false;
} else {
}
if (css) {
// read it
r = sp_svg_read_color(property, r);
if (has_color)
*has_color = true;
}
}
}
return r | 0xff;
}
/**
* Apply the desktop's current style or the tool style to repr.
*/
void
sp_desktop_apply_style_tool(SPDesktop *desktop, Inkscape::XML::Node *repr, Glib::ustring const &tool_path, bool with_text)
{
} else {
}
if (css_current) {
}
}
/**
* Returns the font size (in SVG pixels) of the text tool style (if text
* tool uses its own style) or desktop style (otherwise).
*/
double
{
(void)desktop; // TODO cleanup
} else {
}
double ret = 12;
}
return ret;
}
/** Determine average stroke width, simple method */
// see TODO in dialogs/stroke-style.cpp on how to get rid of this eventually
{
bool notstroked = true;
int n_notstroked = 0;
if (!SP_IS_ITEM (l->data))
continue;
++n_notstroked; // do not count nonstroked objects
continue;
} else {
notstroked = false;
}
}
if (notstroked)
}
{
bool isClose = false;
static double epsilon = 1e-6;
isClose = true;
}
}
return isClose;
}
/**
* Write to style_res the average fill or stroke of list of objects, if applicable.
*/
int
{
if (g_slist_length(objects) == 0) {
/* No objects, set empty */
return QUERY_STYLE_NOTHING;
}
bool paintImpossible = true;
SVGICCColor* iccColor = 0;
bool iccSeen = false;
gfloat c[4];
c[0] = c[1] = c[2] = c[3] = 0.0;
bool same_color = true;
if (!style) {
continue;
}
// We consider paint "effectively set" for anything within text hierarchy
bool paint_effectively_set =
// 1. Bail out with QUERY_STYLE_MULTIPLE_DIFFERENT if necessary
if ((!paintImpossible) && (!paint->isSameType(*paint_res) || (paint_res->set != paint_effectively_set))) {
return QUERY_STYLE_MULTIPLE_DIFFERENT; // different types of paint
}
// both previous paint and this paint were a server, see if the servers are compatible
SPPaintServer *server_res = isfill? SP_STYLE_FILL_SERVER (style_res) : SP_STYLE_STROKE_SERVER (style_res);
if (SP_IS_LINEARGRADIENT (server_res)) {
if (!SP_IS_LINEARGRADIENT(server))
return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
if (vector_res != vector)
return QUERY_STYLE_MULTIPLE_DIFFERENT; // different gradient vectors
} else if (SP_IS_RADIALGRADIENT (server_res)) {
if (!SP_IS_RADIALGRADIENT(server))
return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
if (vector_res != vector)
return QUERY_STYLE_MULTIPLE_DIFFERENT; // different gradient vectors
} else if (SP_IS_PATTERN (server_res)) {
if (!SP_IS_PATTERN(server))
return QUERY_STYLE_MULTIPLE_DIFFERENT; // different kind of server
return QUERY_STYLE_MULTIPLE_DIFFERENT; // different pattern roots
}
}
// 2. Sum color, copy server from paint to paint_res
gfloat d[3];
// Check if this color is the same as previous
if (paintImpossible) {
prev[0] = d[0];
iccSeen = true;
} else {
same_color = false;
iccColor = 0;
}
same_color = false;
iccColor = 0;
}
}
}
// average color
c[0] += d[0];
c[1] += d[1];
c[2] += d[2];
num ++;
}
paintImpossible = false;
if (isfill) {
} else {
}
}
style_res->fill_rule.computed = style->fill_rule.computed; // no averaging on this, just use the last one
}
// After all objects processed, divide the color if necessary and return
c[0] /= num;
c[1] /= num;
c[2] /= num;
c[3] /= num;
if (isfill) {
} else {
}
// TODO check for existing
}
if (num > 1) {
if (same_color)
return QUERY_STYLE_MULTIPLE_SAME;
else
return QUERY_STYLE_MULTIPLE_AVERAGED;
} else {
return QUERY_STYLE_SINGLE;
}
}
// Not color
return QUERY_STYLE_MULTIPLE_SAME;
} else {
return QUERY_STYLE_SINGLE;
}
}
/**
* Write to style_res the average opacity of a list of objects.
*/
int
{
if (g_slist_length(objects) == 0) {
/* No objects, set empty */
return QUERY_STYLE_NOTHING;
}
gdouble opacity_sum = 0;
bool same_opacity = true;
guint opacity_items = 0;
if (!style) {
continue;
}
opacity_sum += opacity;
same_opacity = false;
}
opacity_items ++;
}
if (opacity_items > 1) {
}
if (opacity_items == 0) {
return QUERY_STYLE_NOTHING;
} else if (opacity_items == 1) {
return QUERY_STYLE_SINGLE;
} else {
if (same_opacity) {
return QUERY_STYLE_MULTIPLE_SAME;
} else {
return QUERY_STYLE_MULTIPLE_AVERAGED;
}
}
}
/**
* Write to style_res the average stroke width of a list of objects.
*/
int
{
if (g_slist_length(objects) == 0) {
/* No objects, set empty */
return QUERY_STYLE_NOTHING;
}
bool same_sw = true;
bool noneSet = true; // is stroke set to none?
int n_stroked = 0;
if (!SP_IS_ITEM(obj)) {
continue;
}
if (!style) {
continue;
}
style->marker[SP_MARKER_LOC].set || // stroke width affects markers, so if there's no stroke but only markers then we should
{
continue;
}
same_sw = false;
n_stroked ++;
}
}
if (n_stroked > 1)
style_res->stroke.noneSet = noneSet; // Will only be true if none of the selected objects has it's stroke set.
if (n_stroked == 0) {
return QUERY_STYLE_NOTHING;
} else if (n_stroked == 1) {
return QUERY_STYLE_SINGLE;
} else {
if (same_sw)
return QUERY_STYLE_MULTIPLE_SAME;
else
return QUERY_STYLE_MULTIPLE_AVERAGED;
}
}
/**
* Write to style_res the average miter limit of a list of objects.
*/
int
{
if (g_slist_length(objects) == 0) {
/* No objects, set empty */
return QUERY_STYLE_NOTHING;
}
int n_stroked = 0;
bool same_ml = true;
if (!SP_IS_ITEM(obj)) {
continue;
}
if (!style) {
continue;
}
continue;
}
n_stroked ++;
same_ml = false;
}
}
if (n_stroked > 1) {
}
if (n_stroked == 0) {
return QUERY_STYLE_NOTHING;
} else if (n_stroked == 1) {
return QUERY_STYLE_SINGLE;
} else {
if (same_ml)
return QUERY_STYLE_MULTIPLE_SAME;
else
return QUERY_STYLE_MULTIPLE_AVERAGED;
}
}
/**
* Write to style_res the stroke cap of a list of objects.
*/
int
{
if (g_slist_length(objects) == 0) {
/* No objects, set empty */
return QUERY_STYLE_NOTHING;
}
int cap = -1;
bool same_cap = true;
int n_stroked = 0;
if (!SP_IS_ITEM(obj)) {
continue;
}
if (!style) {
continue;
}
continue;
}
n_stroked ++;
same_cap = false;
}
if (n_stroked == 0) {
return QUERY_STYLE_NOTHING;
} else if (n_stroked == 1) {
return QUERY_STYLE_SINGLE;
} else {
if (same_cap)
return QUERY_STYLE_MULTIPLE_SAME;
else
return QUERY_STYLE_MULTIPLE_DIFFERENT;
}
}
/**
* Write to style_res the stroke join of a list of objects.
*/
int
{
if (g_slist_length(objects) == 0) {
/* No objects, set empty */
return QUERY_STYLE_NOTHING;
}
int join = -1;
bool same_join = true;
int n_stroked = 0;
if (!SP_IS_ITEM(obj)) {
continue;
}
if (!style) {
continue;
}
continue;
}
n_stroked ++;
same_join = false;
}
}
if (n_stroked == 0) {
return QUERY_STYLE_NOTHING;
} else if (n_stroked == 1) {
return QUERY_STYLE_SINGLE;
} else {
if (same_join)
return QUERY_STYLE_MULTIPLE_SAME;
else
return QUERY_STYLE_MULTIPLE_DIFFERENT;
}
}
/**
* Write to style_res the average font size and spacing of objects.
*/
int
{
bool different = false;
double size = 0;
double letterspacing = 0;
double wordspacing = 0;
double linespacing = 0;
bool letterspacing_normal = false;
bool wordspacing_normal = false;
bool linespacing_normal = false;
double size_prev = 0;
double letterspacing_prev = 0;
double wordspacing_prev = 0;
double linespacing_prev = 0;
int texts = 0;
int no_size = 0;
continue;
}
if (!style) {
continue;
}
texts ++;
} else {
no_size++;
}
letterspacing_normal = true;
}
} else {
letterspacing_normal = false;
}
wordspacing_normal = true;
}
} else {
wordspacing_normal = false;
}
double linespacing_current;
linespacing_normal = true;
linespacing_normal = false;
} else { // we need % here
linespacing_normal = false;
}
different = true;
}
// FIXME: we must detect MULTIPLE_DIFFERENT for these too
}
if (texts == 0)
return QUERY_STYLE_NOTHING;
if (texts > 1) {
}
letterspacing /= texts;
wordspacing /= texts;
linespacing /= texts;
}
if (texts > 1) {
if (different) {
return QUERY_STYLE_MULTIPLE_AVERAGED;
} else {
return QUERY_STYLE_MULTIPLE_SAME;
}
} else {
return QUERY_STYLE_SINGLE;
}
}
/**
* Write to style_res the average font style of objects.
*/
int
{
bool different = false;
bool set = false;
int texts = 0;
continue;
if (!style) {
continue;
}
texts ++;
if (set &&
different = true; // different styles
}
}
return QUERY_STYLE_NOTHING;
if (texts > 1) {
if (different) {
return QUERY_STYLE_MULTIPLE_DIFFERENT;
} else {
return QUERY_STYLE_MULTIPLE_SAME;
}
} else {
return QUERY_STYLE_SINGLE;
}
}
/**
* Write to style_res the baseline numbers.
*/
static int
{
bool different = false;
// Only baseline-shift at the moment
// We will return:
// If baseline-shift is same for all objects:
// The full baseline-shift data (used for subscripts and superscripts)
// If baseline-shift is different:
// The average baseline-shift (not implemented at the moment as this is complicated June 2010)
// double baselineshift = 0.0;
bool set = false;
int texts = 0;
continue;
}
if (!style) {
continue;
}
texts ++;
if( set ) {
// Maybe this needs to be better thought out.
different = true;
}
}
set = true;
}
}
} else {
}
return QUERY_STYLE_NOTHING;
if (texts > 1) {
if (different) {
return QUERY_STYLE_MULTIPLE_DIFFERENT;
} else {
return QUERY_STYLE_MULTIPLE_SAME;
}
} else {
return QUERY_STYLE_SINGLE;
}
}
/**
* Write to style_res the average font family of objects.
*/
int
{
bool different = false;
int texts = 0;
}
// std::cout << " " << reinterpret_cast<SPObject*>(i->data)->getId() << std::endl;
continue;
}
if (!style) {
continue;
}
texts ++;
different = true; // different fonts
}
}
}
return QUERY_STYLE_NOTHING;
}
if (texts > 1) {
if (different) {
return QUERY_STYLE_MULTIPLE_DIFFERENT;
} else {
return QUERY_STYLE_MULTIPLE_SAME;
}
} else {
return QUERY_STYLE_SINGLE;
}
}
static int
{
bool different = false;
int texts = 0;
}
// std::cout << " " << reinterpret_cast<SPObject*>(i->data)->getId() << std::endl;
continue;
}
if (!style) {
continue;
}
texts ++;
different = true; // different fonts
}
}
}
}
if (texts == 0) {
return QUERY_STYLE_NOTHING;
}
if (texts > 1) {
if (different) {
return QUERY_STYLE_MULTIPLE_DIFFERENT;
} else {
return QUERY_STYLE_MULTIPLE_SAME;
}
} else {
return QUERY_STYLE_SINGLE;
}
}
static int
{
const int empty_prev = -2;
const int complex_filter = 5;
int blend = 0;
float blend_prev = empty_prev;
bool same_blend = true;
continue;
}
items++;
//if object has a filter
int blurcount = 0;
int blendcount = 0;
if(SP_IS_FEBLEND(primitive))
++blendcount;
else if(SP_IS_GAUSSIANBLUR(primitive))
++blurcount;
else {
break;
}
}
// simple filter
if(SP_IS_FEBLEND(primitive_obj)) {
}
}
}
else {
}
}
// defaults to blend mode = "normal"
else {
blend = 0;
}
same_blend = false;
blend_prev = blend;
}
if (items > 0) {
}
if (items == 0) {
return QUERY_STYLE_NOTHING;
} else if (items == 1) {
return QUERY_STYLE_SINGLE;
} else {
if(same_blend)
return QUERY_STYLE_MULTIPLE_SAME;
else
return QUERY_STYLE_MULTIPLE_DIFFERENT;
}
}
/**
* Write to style_res the average blurring of a list of objects.
*/
int
{
if (g_slist_length(objects) == 0) {
/* No objects, set empty */
return QUERY_STYLE_NOTHING;
}
float blur_sum = 0;
float blur_prev = -1;
bool same_blur = true;
guint blur_items = 0;
if (!style) {
continue;
}
if (!SP_IS_ITEM(obj)) {
continue;
}
items ++;
//if object has a filter
//cycle through filter primitives
while (primitive_obj) {
if (SP_IS_FILTER_PRIMITIVE(primitive_obj)) {
//if primitive is gaussianblur
if(SP_IS_GAUSSIANBLUR(primitive)) {
if (blur_prev != -1 && fabs (num - blur_prev) > 1e-2) // rather low tolerance because difference in blur radii is much harder to notice than e.g. difference in sizes
same_blur = false;
//TODO: deal with opt number, for the moment it's not necessary to the ui.
blur_items ++;
}
}
}
}
}
}
if (items > 0) {
if (blur_items > 0)
blur_sum /= blur_items;
}
if (items == 0) {
return QUERY_STYLE_NOTHING;
} else if (items == 1) {
return QUERY_STYLE_SINGLE;
} else {
if (same_blur)
return QUERY_STYLE_MULTIPLE_SAME;
else
return QUERY_STYLE_MULTIPLE_AVERAGED;
}
}
/**
* Query the given list of objects for the given property, write
* the result to style, return appropriate flag.
*/
int
{
if (property == QUERY_STYLE_PROPERTY_FILL) {
} else if (property == QUERY_STYLE_PROPERTY_STROKE) {
} else if (property == QUERY_STYLE_PROPERTY_STROKEWIDTH) {
} else if (property == QUERY_STYLE_PROPERTY_STROKEMITERLIMIT) {
} else if (property == QUERY_STYLE_PROPERTY_STROKECAP) {
} else if (property == QUERY_STYLE_PROPERTY_STROKEJOIN) {
} else if (property == QUERY_STYLE_PROPERTY_MASTEROPACITY) {
} else if (property == QUERY_STYLE_PROPERTY_FONT_SPECIFICATION) {
} else if (property == QUERY_STYLE_PROPERTY_FONTFAMILY) {
} else if (property == QUERY_STYLE_PROPERTY_FONTSTYLE) {
} else if (property == QUERY_STYLE_PROPERTY_FONTNUMBERS) {
} else if (property == QUERY_STYLE_PROPERTY_BASELINES) {
} else if (property == QUERY_STYLE_PROPERTY_BLEND) {
} else if (property == QUERY_STYLE_PROPERTY_BLUR) {
}
return QUERY_STYLE_NOTHING;
}
/**
* Query the subselection (if any) or selection on the given desktop for the given property, write
* the result to style, return appropriate flag.
*/
int
{
if (ret != QUERY_STYLE_NOTHING)
return ret; // subselection returned a style, pass it on
// otherwise, do querying and averaging over selection
return sp_desktop_query_style_from_list ((GSList *) desktop->selection->itemList(), style, property);
}
return QUERY_STYLE_NOTHING;
}
/**
* Do the same as sp_desktop_query_style for all (defined) style properties, return true if at
* least one of the properties did not return QUERY_STYLE_NOTHING.
*/
bool
{
int result_strokemiterlimit = sp_desktop_query_style (desktop, query, QUERY_STYLE_PROPERTY_STROKEMITERLIMIT);
return (result_family != QUERY_STYLE_NOTHING ||
}
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :