/*
* Handwriting-like drawing mode
*
* Authors:
* Mitsuru Oka <oka326@parkcity.ne.jp>
* Lauris Kaplinski <lauris@kaplinski.com>
* bulia byak <buliabyak@users.sf.net>
* MenTaLguY <mental@rydia.net>
* Abhishek Sharma
* Jon A. Cruz <jon@joncruz.org>
*
* The original dynadraw code:
* Paul Haeberli <paul@sgi.com>
*
* Copyright (C) 1998 The Free Software Foundation
* Copyright (C) 1999-2005 authors
* Copyright (C) 2001-2002 Ximian, Inc.
* Copyright (C) 2005-2007 bulia byak
* Copyright (C) 2006 MenTaLguY
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#define noDYNA_DRAW_VERBOSE
#include "config.h"
#include <gdk/gdkkeysyms.h>
#include <string>
#include <cstring>
#include <numeric>
#include "display/canvas-bpath.h"
#include "display/cairo-utils.h"
#include <glib.h>
#include "macros.h"
#include "document.h"
#include "document-undo.h"
#include "selection.h"
#include "desktop.h"
#include "desktop-events.h"
#include "desktop-style.h"
#include "message-context.h"
#include "preferences.h"
#include "pixmaps/cursor-calligraphy.xpm"
#include "context-fns.h"
#include "sp-item.h"
#include "inkscape.h"
#include "color.h"
#include "splivarot.h"
#include "sp-item-group.h"
#include "sp-shape.h"
#include "sp-path.h"
#include "sp-text.h"
#include "display/sp-canvas.h"
#include "display/canvas-bpath.h"
#include "display/canvas-arena.h"
#include "verbs.h"
#include "ui/tools/calligraphic-tool.h"
using Inkscape::DocumentUndo;
namespace Inkscape {
namespace UI {
namespace Tools {
static void add_cap(SPCurve *curve, Geom::Point const &from, Geom::Point const &to, double rounding);
return CalligraphicTool::prefsPath;
}
, keep_selected(true)
, hatch_spacing(0)
, hatch_spacing_step(0)
, hatch_item(NULL)
, hatch_escaped(false)
, hatch_area(NULL)
, just_started_drawing(false)
, trace_bg(false)
{
this->vel_thin = 0.1;
this->flatness = 0.9;
this->cap_rounding = 0.0;
this->abs_width = false;
}
if (this->hatch_area) {
sp_canvas_item_destroy(this->hatch_area);
this->hatch_area = NULL;
}
}
DynamicBase::setup();
this->accumulated = new SPCurve();
this->currentcurve = new SPCurve();
sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(this->currentshape), 0x00000000, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
/* fixme: Cannot we cascade it to root more clearly? */
g_signal_connect(G_OBJECT(this->currentshape), "event", G_CALLBACK(sp_desktop_root_handler), this->desktop);
{
/* TODO: have a look at DropperTool::setup where the same is done.. generalize? */
c->unref();
sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(this->hatch_area), 0x0000007f, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
sp_canvas_item_hide(this->hatch_area);
}
sp_event_context_read(this, "mass");
sp_event_context_read(this, "wiggle");
sp_event_context_read(this, "angle");
sp_event_context_read(this, "width");
sp_event_context_read(this, "thinning");
sp_event_context_read(this, "tremor");
sp_event_context_read(this, "flatness");
sp_event_context_read(this, "tracebackground");
sp_event_context_read(this, "usepressure");
sp_event_context_read(this, "usetilt");
sp_event_context_read(this, "abs_width");
sp_event_context_read(this, "keep_selected");
sp_event_context_read(this, "cap_rounding");
this->is_drawing = false;
this->enableSelectionCue();
}
}
if (path == "tracebackground") {
} else if (path == "keep_selected") {
} else {
//pass on up to parent class to handle common attributes.
}
//g_print("DDC: %g %g %g %g\n", ddc->mass, ddc->drag, ddc->angle, ddc->width);
}
static double
{
}
///* Get normalized point */
//Geom::Point CalligraphicTool::getNormalizedPoint(Geom::Point v) const {
// Geom::Rect drect = desktop->get_display_area();
//
// double const max = MAX ( drect.dimensions()[Geom::X], drect.dimensions()[Geom::Y] );
//
// return Geom::Point(( v[Geom::X] - drect.min()[Geom::X] ) / max, ( v[Geom::Y] - drect.min()[Geom::Y] ) / max);
//}
//
///* Get view point */
//Geom::Point CalligraphicTool::getViewPoint(Geom::Point n) const {
// Geom::Rect drect = desktop->get_display_area();
//
// double const max = MAX ( drect.dimensions()[Geom::X], drect.dimensions()[Geom::Y] );
//
// return Geom::Point(n[Geom::X] * max + drect.min()[Geom::X], n[Geom::Y] * max + drect.min()[Geom::Y]);
//}
this->vel_max = 0;
}
} else {
this->pressure = DDC_DEFAULT_PRESSURE;
}
} else {
this->xtilt = DDC_DEFAULT_TILT;
}
} else {
this->ytilt = DDC_DEFAULT_TILT;
}
}
/* Calculate mass and drag */
/* Calculate force and acceleration */
// If force is below the absolute threshold DYNA_EPSILON,
// or we haven't yet reached DYNA_VEL_START (i.e. at the beginning of stroke)
// _and_ the force is below the (higher) DYNA_EPSILON_START threshold,
// discard this move.
// This prevents flips, blobs, and jerks caused by microscopic tremor of the tablet pen,
// especially bothersome at the start of the stroke where we don't yet have the inertia to
// smooth them out.
if ( Geom::L2(force) < DYNA_EPSILON || (this->vel_max < DYNA_VEL_START && Geom::L2(force) < DYNA_EPSILON_START)) {
return FALSE;
}
/* Calculate new velocity */
/* Calculate angle of drawing tool */
double a1;
if (this->usetilt) {
// 1a. calculate nib angle from input device tilt:
if (length > 0) {
}
else
a1 = 0.0;
}
else {
// 1b. fixed dc->angle (absolutely flat nib):
}
// 2. perpendicular to dc->vel (absolutely non-flat nib):
if ( mag_vel < DYNA_EPSILON ) {
return FALSE;
}
// 3. Average them using flatness parameter:
// calculate angles
// flip a2 to force it to be in the same half-circle as a1
bool flipped = false;
flipped = true;
}
// normalize a2
// find the flatness-weighted bisector angle, unflip if a2 was flipped
// FIXME: when dc->vel is oscillating around the fixed angle, the new_ang flips back and forth. How to avoid this?
// Try to detect a sudden flip when the new angle differs too much from the previous for the
// current velocity; in that case discard this move
return FALSE;
}
// convert to point
// g_print ("force %g acc %g vel_max %g vel %g a1 %g a2 %g new_ang %g\n", Geom::L2(force), Geom::L2(dc->acc), dc->vel_max, Geom::L2(dc->vel), a1, a2, new_ang);
/* Apply drag */
/* Update position */
return TRUE;
}
// How much velocity thins strokestyle
// Influence of pressure on thickness
// drag)
if (this->trace_bg) {
// pick single pixel
double R, G, B, A;
ink_cairo_surface_average_color_premul(s, R, G, B, A);
trace_thick = 1 - L;
//g_print ("L %g thick %g\n", L, trace_thick);
}
if (this->tremor > 0) {
// obtain two normally distributed random variables, using polar Box-Muller transform
do {
} while ( w >= 1.0 );
// deflect both left and right edges randomly and independently, so that:
// (1) dc->tremor=1 corresponds to sigma=1, decreasing dc->tremor narrows the bell curve;
// (2) deflection depends on width, but is upped for small widths for better visual uniformity across widths;
// (3) deflection somewhat depends on speed, to prevent fast strokes looking
// comparatively smooth and slow ones excessively jittery
}
}
if (!this->abs_width) {
}
this->npoints++;
}
static void
{
}
this->dragging = false;
this->is_drawing = false;
/* Remove all temporary line segments */
while (this->segments) {
}
/* reset accumulated curve */
this->accumulated->reset();
this->clear_current();
if (this->repr) {
}
}
case GDK_BUTTON_PRESS:
return TRUE;
}
this->accumulated->reset();
if (this->repr) {
}
/* initialize first point */
this->npoints = 0;
NULL,
this->is_drawing = true;
this->just_started_drawing = true;
}
break;
case GDK_MOTION_NOTIFY:
{
this->message_context->clear();
// for hatching:
double hatch_dist = 0;
// One item selected, and it's a path;
// let's try to track it as a guide
if (selected != this->hatch_item) {
this->hatch_item = selected;
if (this->hatch_livarot_path)
delete this->hatch_livarot_path;
}
// calculate pointer point in the guide item's coords
// calculate the nearest point on the guide path
boost::optional<Path::cut_position> position = get_nearest_position_on_Path(this->hatch_livarot_path, pointer);
// distance from pointer to nearest
// unit-length vector
this->message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Guide path selected</b>; start drawing along the guide with <b>Ctrl</b>"));
} else {
this->message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Select a guide path</b> to track with <b>Ctrl</b>"));
}
}
// speed is the movement of the nearest point along the guide path, divided by
// the movement of the pointer at the same period; it is averaged for the last
// SPEED_ELEMENTS motion events. Normally, as you track the guide path, speed
// is about 1, i.e. the nearest point on the path is moved by about the same
// distance as the pointer. If the speed starts to decrease, we are losing
// contact with the guide; if it drops below SPEED_MIN, we are on our own and
// not attracted to guide anymore. Most often this happens when you have
// tracked to the end of a guide calligraphic stroke and keep moving
// further. We try to handle this situation gracefully: not stick with the
// guide forever but let go of it smoothly and without sharp jerks (non-zero
// mass recommended; with zero mass, jerks are still quite noticeable).
// the distance nearest moved since the last motion event
// the distance pointer moved since the last motion event
// store them in stacks limited to SPEED_ELEMENTS
this->hatch_nearest_past.pop_back();
this->hatch_pointer_past.pop_back();
// If the stacks are full,
// calculate the sums of all stored movements
double nearest_sum = std::accumulate (this->hatch_nearest_past.begin(), this->hatch_nearest_past.end(), 0.0);
double pointer_sum = std::accumulate (this->hatch_pointer_past.begin(), this->hatch_pointer_past.end(), 0.0);
// and divide to get the speed
//g_print ("nearest sum %g pointer_sum %g speed %g\n", nearest_sum, pointer_sum, speed);
}
}
if ( this->hatch_escaped // already escaped, do not reattach
|| (this->hatch_spacing > 0 && hatch_dist > 50 * this->hatch_spacing) // went too far from the guide
) {
// We are NOT attracted to the guide!
//g_print ("\nlast_nearest %g %g nearest %g %g pointer %g %g pos %d %g\n", dc->last_nearest[Geom::X], dc->last_nearest[Geom::Y], nearest[Geom::X], nearest[Geom::Y], pointer[Geom::X], pointer[Geom::Y], position->piece, position->t);
// Remember hatch_escaped so we don't get
// attracted again until the end of this stroke
this->hatch_escaped = true;
if (dot > 0) { // mouse is still moving in approx the same direction
}
}
} else {
// Calculate angle cosine of this vector-to-guide and all past vectors
// summed, to detect if we accidentally flipped to the other side of the
// guide
if (this->hatch_spacing != 0) { // spacing was already set
double target;
if (speed > SPEED_NORMAL) {
// all ok, strictly obey the spacing
target = this->hatch_spacing;
} else {
// looks like we're starting to lose speed,
// so _gradually_ let go attraction to prevent jerks
}
}
// This is the track pointer that we will use instead of the real one
// some limited feedback: allow persistent pulling to slightly change
// the spacing
// return it to the desktop coords
if (speed >= SPEED_NORMAL) {
this->inertia_vectors.pop_back();
}
} else {
// this is the first motion event, set the dist
this->hatch_spacing = hatch_dist;
}
// remember last points
this->hatch_last_pointer = pointer;
this->hatch_last_nearest = nearest;
this->hatch_vectors.pop_back();
}
this->message_context->set(Inkscape::NORMAL_MESSAGE, this->hatch_escaped? _("Tracking: <b>connection to guide path lost!</b>") : _("<b>Tracking</b> a guide path"));
} else {
}
if (this->just_started_drawing) {
this->just_started_drawing = false;
}
break;
}
this->brush();
this->fit_and_split(false);
}
}
// Draw the hatching circle if necessary
if (this->hatch_spacing == 0 && hatch_dist != 0) {
// Haven't set spacing yet: gray, center free, update radius live
sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(this->hatch_area), 0x7f7f7fff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
sp_canvas_item_show(this->hatch_area);
} else if (this->dragging && !this->hatch_escaped) {
// Tracking: green, center snapped, fixed radius
sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(this->hatch_area), 0x00FF00ff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
sp_canvas_item_show(this->hatch_area);
} else if (this->dragging && this->hatch_escaped) {
// Tracking escaped: red, center free, fixed radius
sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(this->hatch_area), 0xFF0000ff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
sp_canvas_item_show(this->hatch_area);
} else {
// Not drawing but spacing set: gray, center snapped, fixed radius
sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(this->hatch_area), 0x7f7f7fff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
sp_canvas_item_show(this->hatch_area);
}
}
} else {
sp_canvas_item_hide(this->hatch_area);
}
}
break;
case GDK_BUTTON_RELEASE:
{
this->is_drawing = false;
/* Remove all temporary line segments */
while (this->segments) {
}
/* Create object */
this->fit_and_split(true);
if (this->accumulate())
this->set_to_accumulated(event->button.state & GDK_SHIFT_MASK, event->button.state & GDK_MOD1_MASK); // performs document_done
else
g_warning ("Failed to create path: invalid data in dc->cal1 or dc->cal2");
/* reset accumulated curve */
this->accumulated->reset();
this->clear_current();
if (this->repr) {
}
this->hatch_escaped = false;
this->hatch_item = NULL;
this->hatch_livarot_path = NULL;
this->just_started_drawing = false;
if (this->hatch_spacing != 0 && !this->keep_selected) {
// we do not select the newly drawn path, so increase spacing by step
if (this->hatch_spacing_step == 0) {
this->hatch_spacing_step = this->hatch_spacing;
}
this->hatch_spacing += this->hatch_spacing_step;
}
this->message_context->clear();
}
break;
}
case GDK_KEY_PRESS:
case GDK_KEY_Up:
case GDK_KEY_KP_Up:
if (!MOD__CTRL_ONLY(event)) {
this->angle += 5.0;
if (this->angle > 90.0)
this->angle = 90.0;
}
break;
case GDK_KEY_Down:
case GDK_KEY_KP_Down:
if (!MOD__CTRL_ONLY(event)) {
this->angle -= 5.0;
if (this->angle < -90.0)
this->angle = -90.0;
}
break;
case GDK_KEY_Right:
case GDK_KEY_KP_Right:
if (!MOD__CTRL_ONLY(event)) {
this->width += 0.01;
if (this->width > 1.0)
this->width = 1.0;
sp_ddc_update_toolbox (desktop, "altx-calligraphy", this->width * 100); // the same spinbutton is for alt+x
}
break;
case GDK_KEY_Left:
case GDK_KEY_KP_Left:
if (!MOD__CTRL_ONLY(event)) {
this->width -= 0.01;
if (this->width < 0.01)
this->width = 0.01;
}
break;
case GDK_KEY_Home:
case GDK_KEY_KP_Home:
this->width = 0.01;
break;
case GDK_KEY_End:
case GDK_KEY_KP_End:
this->width = 1.0;
break;
case GDK_KEY_x:
case GDK_KEY_X:
if (MOD__ALT_ONLY(event)) {
}
break;
case GDK_KEY_Escape:
if (this->is_drawing) {
// if drawing, cancel, otherwise pass it up for deselecting
this->cancel();
}
break;
case GDK_KEY_z:
case GDK_KEY_Z:
// if drawing, cancel, otherwise pass it up for undo
this->cancel();
}
break;
default:
break;
}
break;
case GDK_KEY_RELEASE:
case GDK_KEY_Control_L:
case GDK_KEY_Control_R:
this->message_context->clear();
this->hatch_spacing = 0;
this->hatch_spacing_step = 0;
break;
default:
break;
}
break;
default:
break;
}
if (!ret) {
// if ((SP_EVENT_CONTEXT_CLASS(sp_dyna_draw_context_parent_class))->root_handler) {
// ret = (SP_EVENT_CONTEXT_CLASS(sp_dyna_draw_context_parent_class))->root_handler(event_context, event);
// }
}
return ret;
}
/* reset bpath */
/* reset curve */
this->currentcurve->reset();
/* reset points */
this->npoints = 0;
}
if (!this->accumulated->is_empty()) {
if (!this->repr) {
/* Create object */
/* Set style */
item->updateRepr();
}
if (unionize) {
} else if (subtract) {
} else {
if (this->keep_selected) {
}
}
// Now we need to write the transform information.
// First, find out whether our repr is still linked to a valid object. In this case,
// we need to write the transform data only for this element.
// Either there was no boolean op or it failed.
// The boolean operation succeeded.
// Now we fetch the single item, that has been set as selected by the boolean op.
// This is its result.
}
} else {
if (this->repr) {
sp_repr_unparent(this->repr);
}
}
_("Draw calligraphic stroke"));
}
static void
double rounding)
{
}
}
if (
(this->cal1->get_segment_count() <= 0) ||
) {
return false; // failure
}
return false; // failure
}
add_cap(this->accumulated, dc_cal1_lastseg->finalPoint(), rev_cal2_firstseg->initialPoint(), this->cap_rounding);
add_cap(this->accumulated, rev_cal2_lastseg->finalPoint(), dc_cal1_firstseg->initialPoint(), this->cap_rounding);
this->accumulated->closepath();
return true; // success
}
static double square(double const x)
{
return x * x;
}
#ifdef DYNA_DRAW_VERBOSE
#endif
return; // just clicked
}
#ifdef DYNA_DRAW_VERBOSE
g_print("[F&S:#] dc->npoints:%d, release:%s\n",
#endif
/* Current calligraphic */
/* dc->npoints > 0 */
/* g_print("calligraphics(1|2) reset\n"); */
}
/* Fit and draw and reset state */
#ifdef DYNA_DRAW_VERBOSE
#endif
/* CanvasShape */
if (! release) {
this->currentcurve->reset();
}
}
// FIXME: dc->segments is always NULL at this point??
if (!this->segments) { // first segment
}
this->currentcurve->closepath();
}
/* Current calligraphic */
}
}
} else {
/* fixme: ??? */
#ifdef DYNA_DRAW_VERBOSE
g_print("[fit_and_split] failed to fit-cubic.\n");
#endif
this->draw_temporary_box();
}
}
}
/* Fit and draw and copy last point */
#ifdef DYNA_DRAW_VERBOSE
#endif
if (!release) {
NULL);
//guint32 strokeColor = sp_desktop_get_color_tool (desktop, "/tools/calligraphic", false);
//double strokeOpacity = sp_desktop_get_opacity_tool (desktop, "/tools/calligraphic", false);
sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cbp), ((fillColor & 0xffffff00) | SP_COLOR_F_TO_U(opacity*fillOpacity)), SP_WIND_RULE_EVENODD);
//on second thougtht don't do stroke yet because we don't have stoke-width yet and because stoke appears between segments while drawing
//sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cbp), ((strokeColor & 0xffffff00) | SP_COLOR_F_TO_U(opacity*strokeOpacity)), 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cbp), 0x00000000, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
/* fixme: Cannot we cascade it to root more clearly? */
}
this->npoints = 1;
} else {
this->draw_temporary_box();
}
}
this->currentcurve->reset();
}
}
if (this->npoints >= 2) {
add_cap(this->currentcurve, this->point1[this->npoints-1], this->point2[this->npoints-1], this->cap_rounding);
}
this->currentcurve->closepath();
}
}
}
}
/*
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:fileencoding=utf-8:textwidth=99 :