dyna-draw-context.cpp revision 597de4a7a6a70f44324f3ddf984c80210b6c268a
c533a883a71cff9ff32df1c53c31201e1cbf371fhx * Handwriting-like drawing mode
c533a883a71cff9ff32df1c53c31201e1cbf371fhx * Authors:
c533a883a71cff9ff32df1c53c31201e1cbf371fhx * Mitsuru Oka <oka326@parkcity.ne.jp>
c533a883a71cff9ff32df1c53c31201e1cbf371fhx * Lauris Kaplinski <lauris@kaplinski.com>
c533a883a71cff9ff32df1c53c31201e1cbf371fhx * bulia byak <buliabyak@users.sf.net>
c533a883a71cff9ff32df1c53c31201e1cbf371fhx * The original dynadraw code:
c533a883a71cff9ff32df1c53c31201e1cbf371fhx * Paul Haeberli <paul@sgi.com>
c533a883a71cff9ff32df1c53c31201e1cbf371fhx * Copyright (C) 1998 The Free Software Foundation
c533a883a71cff9ff32df1c53c31201e1cbf371fhx * Copyright (C) 1999-2005 authors
c533a883a71cff9ff32df1c53c31201e1cbf371fhx * Copyright (C) 2001-2002 Ximian, Inc.
c533a883a71cff9ff32df1c53c31201e1cbf371fhx * Released under GNU GPL, read the file 'COPYING' for more information
c533a883a71cff9ff32df1c53c31201e1cbf371fhx * TODO: Tue Oct 2 22:57:15 2001
c533a883a71cff9ff32df1c53c31201e1cbf371fhx * - Decide control point behavior when use_calligraphic==1.
c533a883a71cff9ff32df1c53c31201e1cbf371fhx * - Decide to use NORMALIZED_COORDINATE or not.
c533a883a71cff9ff32df1c53c31201e1cbf371fhx * - Bug fix.
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China#define SAMPLE_TIMEOUT 10
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void sp_dyna_draw_context_class_init(SPDynaDrawContextClass *klass);
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void sp_dyna_draw_context_init(SPDynaDrawContext *ddc);
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void sp_dyna_draw_context_dispose(GObject *object);
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void sp_dyna_draw_context_setup(SPEventContext *ec);
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void sp_dyna_draw_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic gint sp_dyna_draw_context_root_handler(SPEventContext *ec, GdkEvent *event);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing Chinastatic void accumulate_calligraphic(SPDynaDrawContext *dc);
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void fit_and_split(SPDynaDrawContext *ddc, gboolean release);
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void fit_and_split_line(SPDynaDrawContext *ddc, gboolean release);
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void fit_and_split_calligraphics(SPDynaDrawContext *ddc, gboolean release);
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void sp_dyna_draw_reset(SPDynaDrawContext *ddc, NR::Point p);
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic NR::Point sp_dyna_draw_get_npoint(SPDynaDrawContext const *ddc, NR::Point v);
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic NR::Point sp_dyna_draw_get_vpoint(SPDynaDrawContext const *ddc, NR::Point n);
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic NR::Point sp_dyna_draw_get_curr_vpoint(SPDynaDrawContext const *ddc);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPDynaDrawContext", &info, (GTypeFlags)0);
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void
c533a883a71cff9ff32df1c53c31201e1cbf371fhxsp_dyna_draw_context_class_init(SPDynaDrawContextClass *klass)
c533a883a71cff9ff32df1c53c31201e1cbf371fhx SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx event_context_class->root_handler = sp_dyna_draw_context_root_handler;
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* DynaDraw values */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* attributes */
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->segments = g_slist_remove(ddc->segments, ddc->segments->data);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (ddc->currentcurve) ddc->currentcurve = sp_curve_unref(ddc->currentcurve);
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* style should be changed when dc->use_calligraphc is touched */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->currentshape = sp_canvas_item_new(SP_DT_SKETCH(ec->desktop), SP_TYPE_CANVAS_BPATH, NULL);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(ddc->currentshape), DDC_RED_RGBA, SP_WIND_RULE_EVENODD);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(ddc->currentshape), 0x00000000, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* fixme: Cannot we cascade it to root more clearly? */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx g_signal_connect(G_OBJECT(ddc->currentshape), "event", G_CALLBACK(sp_desktop_root_handler), ec->desktop);
b510adae7e8895b2bf58eda3537fd56df35302e4fei feng - Sun Microsystems - Beijing China sp_event_context_read(ec, "mass");
b510adae7e8895b2bf58eda3537fd56df35302e4fei feng - Sun Microsystems - Beijing China sp_event_context_read(ec, "drag");
b510adae7e8895b2bf58eda3537fd56df35302e4fei feng - Sun Microsystems - Beijing China sp_event_context_read(ec, "angle");
b510adae7e8895b2bf58eda3537fd56df35302e4fei feng - Sun Microsystems - Beijing China sp_event_context_read(ec, "width");
b510adae7e8895b2bf58eda3537fd56df35302e4fei feng - Sun Microsystems - Beijing China sp_event_context_read(ec, "thinning");
b510adae7e8895b2bf58eda3537fd56df35302e4fei feng - Sun Microsystems - Beijing China sp_event_context_read(ec, "tremor");
b510adae7e8895b2bf58eda3537fd56df35302e4fei feng - Sun Microsystems - Beijing China sp_event_context_read(ec, "flatness");
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void
c533a883a71cff9ff32df1c53c31201e1cbf371fhxsp_dyna_draw_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
c533a883a71cff9ff32df1c53c31201e1cbf371fhx double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.2 );
c533a883a71cff9ff32df1c53c31201e1cbf371fhx double const dval = ( val ? g_ascii_strtod (val, NULL) : DRAG_DEFAULT );
c533a883a71cff9ff32df1c53c31201e1cbf371fhx double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.0);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.1 );
c533a883a71cff9ff32df1c53c31201e1cbf371fhx double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.1 );
c533a883a71cff9ff32df1c53c31201e1cbf371fhx double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.0 );
c533a883a71cff9ff32df1c53c31201e1cbf371fhx double const dval = ( val ? g_ascii_strtod (val, NULL) : 1.0 );
c533a883a71cff9ff32df1c53c31201e1cbf371fhx //g_print("DDC: %g %g %g %g\n", ddc->mass, ddc->drag, ddc->angle, ddc->width);
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic double
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China return f0 + ( f1 - f0 ) * p;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx/* Get normalized point */
c533a883a71cff9ff32df1c53c31201e1cbf371fhxsp_dyna_draw_get_npoint(SPDynaDrawContext const *dc, NR::Point v)
c533a883a71cff9ff32df1c53c31201e1cbf371fhx NR::Rect drect = SP_EVENT_CONTEXT(dc)->desktop->get_display_area();
c533a883a71cff9ff32df1c53c31201e1cbf371fhx double const max = MAX ( drect.dimensions()[NR::X], drect.dimensions()[NR::Y] );
c533a883a71cff9ff32df1c53c31201e1cbf371fhx return NR::Point(( v[NR::X] - drect.min()[NR::X] ) / max, ( v[NR::Y] - drect.min()[NR::Y] ) / max);
43439c96b8398c01c375889c79bed72d78fb4c39hx/* Get view point */
43439c96b8398c01c375889c79bed72d78fb4c39hxsp_dyna_draw_get_vpoint(SPDynaDrawContext const *dc, NR::Point n)
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China NR::Rect drect = SP_EVENT_CONTEXT(dc)->desktop->get_display_area();
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China double const max = MAX ( drect.dimensions()[NR::X], drect.dimensions()[NR::Y] );
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China return NR::Point(n[NR::X] * max + drect.min()[NR::X], n[NR::Y] * max + drect.min()[NR::Y]);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China/* Get current view point */
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing Chinastatic NR::Point sp_dyna_draw_get_curr_vpoint(SPDynaDrawContext const *dc)
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China NR::Rect drect = SP_EVENT_CONTEXT(dc)->desktop->get_display_area();
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China double const max = MAX ( drect.dimensions()[NR::X], drect.dimensions()[NR::Y] );
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China return NR::Point(dc->cur[NR::X] * max + drect.min()[NR::X], dc->cur[NR::Y] * max + drect.min()[NR::Y]);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing Chinasp_dyna_draw_reset(SPDynaDrawContext *dc, NR::Point p)
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China dc->last = dc->cur = sp_dyna_draw_get_npoint(dc, p);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China dc->vel = NR::Point(0,0);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China dc->acc = NR::Point(0,0);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China dc->ang = NR::Point(0,0);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China dc->del = NR::Point(0,0);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing Chinasp_dyna_draw_extinput(SPDynaDrawContext *dc, GdkEvent *event)
19d332fefbc61327bb6187d0eb818629f3b52c6ffei feng - Sun Microsystems - Beijing China if (gdk_event_get_axis (event, GDK_AXIS_PRESSURE, &dc->pressure))
19d332fefbc61327bb6187d0eb818629f3b52c6ffei feng - Sun Microsystems - Beijing China dc->pressure = CLAMP (dc->pressure, DDC_MIN_PRESSURE, DDC_MAX_PRESSURE);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China dc->pressure = DDC_DEFAULT_PRESSURE;
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China if (gdk_event_get_axis (event, GDK_AXIS_XTILT, &dc->xtilt))
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China dc->xtilt = CLAMP (dc->xtilt, DDC_MIN_TILT, DDC_MAX_TILT);
d2a61391af480de12cf4264080d7254a6de96e2apengcheng chen - Sun Microsystems - Beijing China dc->xtilt = DDC_DEFAULT_TILT;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (gdk_event_get_axis (event, GDK_AXIS_YTILT, &dc->ytilt))
c533a883a71cff9ff32df1c53c31201e1cbf371fhx dc->ytilt = CLAMP (dc->ytilt, DDC_MIN_TILT, DDC_MAX_TILT);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan /* Calculate mass and drag */
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan double const mass = flerp(1.0, 160.0, dc->mass);
0dc2366f7b9f9f36e10909b1e95edbf2a261c2acVenugopal Iyer double const drag = flerp(0.0, 0.5, dc->drag * dc->drag);
0dc2366f7b9f9f36e10909b1e95edbf2a261c2acVenugopal Iyer /* Calculate force and acceleration */
19d332fefbc61327bb6187d0eb818629f3b52c6ffei feng - Sun Microsystems - Beijing China dc->acc = force / mass;
19d332fefbc61327bb6187d0eb818629f3b52c6ffei feng - Sun Microsystems - Beijing China /* Calculate new velocity */
19d332fefbc61327bb6187d0eb818629f3b52c6ffei feng - Sun Microsystems - Beijing China dc->vel += dc->acc;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* Calculate angle of drawing tool */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx // 1a. calculate nib angle from input device tilt:
c533a883a71cff9ff32df1c53c31201e1cbf371fhx gdouble length = std::sqrt(dc->xtilt*dc->xtilt + dc->ytilt*dc->ytilt);;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (length > 0) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx NR::Point ang1 = NR::Point(dc->ytilt/length, dc->xtilt/length);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx // 1b. fixed dc->angle (absolutely flat nib):
c533a883a71cff9ff32df1c53c31201e1cbf371fhx double const radians = ( (dc->angle - 90) / 180.0 ) * M_PI;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx // 2. perpendicular to dc->vel (absolutely non-flat nib):
d2a61391af480de12cf4264080d7254a6de96e2apengcheng chen - Sun Microsystems - Beijing China gdouble const mag_vel = NR::L2(dc->vel);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx // 3. Average them using flatness parameter:
c533a883a71cff9ff32df1c53c31201e1cbf371fhx // calculate angles
c533a883a71cff9ff32df1c53c31201e1cbf371fhx // flip a2 to force it to be in the same half-circle as a1
c533a883a71cff9ff32df1c53c31201e1cbf371fhx bool flipped = false;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx // normalize a2
c533a883a71cff9ff32df1c53c31201e1cbf371fhx // find the flatness-weighted bisector angle, unflip if a2 was flipped
c533a883a71cff9ff32df1c53c31201e1cbf371fhx // FIXME: when dc->vel is oscillating around the fixed angle, the new_ang flips back and forth. How to avoid this?
c533a883a71cff9ff32df1c53c31201e1cbf371fhx double new_ang = a1 + (1 - dc->flatness) * (a2 - a1) - (flipped? M_PI : 0);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx // convert to point
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* Apply drag */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* Update position */
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void
c533a883a71cff9ff32df1c53c31201e1cbf371fhx g_assert( dc->npoints >= 0 && dc->npoints < SAMPLING_SIZE );
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* calligraphics */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx // How much velocity thins strokestyle
c533a883a71cff9ff32df1c53c31201e1cbf371fhx // Influence of pressure on thickness
c533a883a71cff9ff32df1c53c31201e1cbf371fhx double pressure_thick = (dc->usepressure ? dc->pressure : 1.0);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx double width = ( pressure_thick - vel_thin * NR::L2(dc->vel) ) * dc->width;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx // obtain two normally distributed random variables, using polar Box-Muller transform
c533a883a71cff9ff32df1c53c31201e1cbf371fhx } while ( w >= 1.0 );
c533a883a71cff9ff32df1c53c31201e1cbf371fhx // deflect both left and right edges randomly and independently, so that:
0dc2366f7b9f9f36e10909b1e95edbf2a261c2acVenugopal Iyer // (1) dc->tremor=1 corresponds to sigma=1, decreasing dc->tremor narrows the bell curve;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan // (2) deflection depends on width, but is upped for small widths for better visual uniformity across widths;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan // (3) deflection somewhat depends on speed, to prevent fast strokes looking
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan // comparatively smooth and slow ones excessively jittery
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan tremble_left = (y1)*dc->tremor * (0.15 + 0.8*width) * (0.35 + 14*NR::L2(dc->vel));
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan tremble_right = (y2)*dc->tremor * (0.15 + 0.8*width) * (0.35 + 14*NR::L2(dc->vel));
c533a883a71cff9ff32df1c53c31201e1cbf371fhx NR::Point del_left = 0.05 * (width + tremble_left) * dc->ang;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx NR::Point del_right = 0.05 * (width + tremble_right) * dc->ang;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx dc->point1[dc->npoints] = sp_dyna_draw_get_vpoint(dc, dc->cur + del_left);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx dc->point2[dc->npoints] = sp_dyna_draw_get_vpoint(dc, dc->cur - del_right);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx dc->point1[dc->npoints] = sp_dyna_draw_get_curr_vpoint(dc);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China gtk_widget_get_pointer(GTK_WIDGET(canvas), &x, &y);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China NR::Point p = sp_canvas_window_to_world(canvas, NR::Point(x, y));
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China p = desktop->w2d(p);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China if (! sp_dyna_draw_apply(dc, p)) {
09539a3c2da6fef054f5306232ef0480de261eabpengcheng chen - Sun Microsystems - Beijing China return TRUE;
d40f4da491abdcae192eb797766f5f44772a832fpengcheng chen - Sun Microsystems - Beijing Chinasp_ddc_update_toolbox (SPDesktop *desktop, const gchar *id, double value)
d62cb7fff63265824a8e6725a3bcef698f6134c9hxsp_dyna_draw_context_root_handler(SPEventContext *event_context,
c533a883a71cff9ff32df1c53c31201e1cbf371fhx SPDynaDrawContext *dc = SP_DYNA_DRAW_CONTEXT(event_context);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* initialize first point */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx dc->timer_id = gtk_timeout_add(SAMPLE_TIMEOUT, sp_dyna_draw_timeout_handler, dc);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China dc->is_drawing = true;
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China case GDK_MOTION_NOTIFY:
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China if ( dc->is_drawing && !dc->use_timeout && ( event->motion.state & GDK_BUTTON1_MASK ) ) {
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China dc->dragging = TRUE;
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China dc->dynahand = TRUE;
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China NR::Point const motion_w(event->motion.x,
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China event->motion.y);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China NR::Point const motion_dt(desktop->w2d(motion_w));
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China sp_dyna_draw_extinput(dc, event);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China if (!sp_dyna_draw_apply(dc, motion_dt)) {
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China if ( dc->cur != dc->last ) {
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China sp_dyna_draw_brush(dc);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China g_assert( dc->npoints > 0 );
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China fit_and_split(dc, FALSE);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China dc->is_drawing = false;
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China if ( event->button.button == 1
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China && dc->use_timeout
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China && dc->timer_id != 0 )
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China gtk_timeout_remove(dc->timer_id);
19d332fefbc61327bb6187d0eb818629f3b52c6ffei feng - Sun Microsystems - Beijing China dc->timer_id = 0;
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China if ( dc->dragging && event->button.button == 1 ) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* release */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* Remove all temporary line segments */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx dc->segments = g_slist_remove(dc->segments, dc->segments->data);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* Create object */
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China concat_current_line(dc);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* reset accumulated curve */
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China dc->angle += 5.0;
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China if (dc->angle > 90.0)
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_ddc_update_toolbox (desktop, "calligraphy-angle", dc->angle);
b510adae7e8895b2bf58eda3537fd56df35302e4fei feng - Sun Microsystems - Beijing China dc->angle = -90.0;
c50ced99bb75273dc8c4c36876d2e8f59a64f4b1pengcheng chen - Sun Microsystems - Beijing China sp_ddc_update_toolbox (desktop, "calligraphy-angle", dc->angle);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_ddc_update_toolbox (desktop, "altx-calligraphy", dc->width); // the same spinbutton is for alt+x
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_ddc_update_toolbox (desktop, "altx-calligraphy", dc->width);
19d332fefbc61327bb6187d0eb818629f3b52c6ffei feng - Sun Microsystems - Beijing China if (((SPEventContextClass *) parent_class)->root_handler) {
19d332fefbc61327bb6187d0eb818629f3b52c6ffei feng - Sun Microsystems - Beijing China ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* reset bpath */
c50ced99bb75273dc8c4c36876d2e8f59a64f4b1pengcheng chen - Sun Microsystems - Beijing China sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->currentshape), NULL);
c50ced99bb75273dc8c4c36876d2e8f59a64f4b1pengcheng chen - Sun Microsystems - Beijing China /* reset curve */
19d332fefbc61327bb6187d0eb818629f3b52c6ffei feng - Sun Microsystems - Beijing China sp_curve_reset(dc->cal1);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China sp_curve_reset(dc->cal2);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* reset points */
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* Create object */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* Set style */
f3c4902c7f91725a7b538a3d82f1993bff02944fpengcheng chen - Sun Microsystems - Beijing China sp_desktop_apply_style_tool (desktop, repr, "tools.calligraphic", false);
19d332fefbc61327bb6187d0eb818629f3b52c6ffei feng - Sun Microsystems - Beijing China dc->repr = repr;
43439c96b8398c01c375889c79bed72d78fb4c39hx SPItem *item=SP_ITEM(desktop->currentLayer()->appendChildRepr(dc->repr));
c533a883a71cff9ff32df1c53c31201e1cbf371fhx item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
c533a883a71cff9ff32df1c53c31201e1cbf371fhx abp = nr_artpath_affine(sp_curve_first_bpath(dc->accumulated), sp_desktop_dt2root_affine(desktop));
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China str = sp_svg_write_path(abp);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China g_assert( str != NULL );
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China dc->repr->setAttribute("d", str);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China sp_document_done(SP_DT_DOCUMENT(desktop));
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China NArtBpath *bpath;
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China if (sp_curve_empty(dc->accumulated)) {
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China bpath = sp_curve_first_bpath(dc->currentcurve);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China g_assert( bpath->code == NR_MOVETO_OPEN );
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China sp_curve_moveto(dc->accumulated, bpath->x3, bpath->y3);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China bpath = sp_curve_last_bpath(dc->currentcurve);
19d332fefbc61327bb6187d0eb818629f3b52c6ffei feng - Sun Microsystems - Beijing China g_assert_not_reached();
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if ( !sp_curve_empty(dc->cal1) && !sp_curve_empty(dc->cal2) ) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_curve_reset(dc->accumulated); /* Is this required ?? */
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic double square(double const x)
c533a883a71cff9ff32df1c53c31201e1cbf371fhx return x * x;
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void
c533a883a71cff9ff32df1c53c31201e1cbf371fhx double const tolerance_sq = square( NR::expansion(SP_EVENT_CONTEXT(dc)->desktop->w2d()) * TOLERANCE_LINE );
c533a883a71cff9ff32df1c53c31201e1cbf371fhx double const n_segs = sp_bezier_fit_cubic(b, dc->point1, dc->npoints, tolerance_sq);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* Fit and draw and reset state */
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China sp_curve_curveto(dc->currentcurve, b[1], b[2], b[3]);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->currentshape), dc->currentcurve);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China /* Fit and draw and copy last point */
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China g_print("[%d]Yup\n", dc->npoints);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx SPCanvasItem *cbp = sp_canvas_item_new(SP_DT_SKETCH(SP_EVENT_CONTEXT(dc)->desktop),
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* fixme: We have to parse style color somehow */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cbp), DDC_GREEN_RGBA, SP_WIND_RULE_EVENODD);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cbp), 0x000000ff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China /* fixme: Cannot we cascade it to root more clearly? */
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China g_signal_connect(G_OBJECT(cbp), "event", G_CALLBACK(sp_desktop_root_handler), SP_EVENT_CONTEXT(dc)->desktop);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China dc->segments = g_slist_prepend(dc->segments, cbp);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China dc->point1[0] = dc->point1[dc->npoints - 2];
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void
c533a883a71cff9ff32df1c53c31201e1cbf371fhxfit_and_split_calligraphics(SPDynaDrawContext *dc, gboolean release)
c533a883a71cff9ff32df1c53c31201e1cbf371fhx double const tolerance_sq = square( NR::expansion(SP_EVENT_CONTEXT(dc)->desktop->w2d()) * TOLERANCE_CALLIGRAPHIC );
c533a883a71cff9ff32df1c53c31201e1cbf371fhx return; // just clicked
d62cb7fff63265824a8e6725a3bcef698f6134c9hx#define BEZIER_MAX_LENGTH ( BEZIER_SIZE * BEZIER_MAX_BEZIERS )
d40f4da491abdcae192eb797766f5f44772a832fpengcheng chen - Sun Microsystems - Beijing China /* Current calligraphic */
d40f4da491abdcae192eb797766f5f44772a832fpengcheng chen - Sun Microsystems - Beijing China if ( dc->cal1->end == 0 || dc->cal2->end == 0 ) {
d40f4da491abdcae192eb797766f5f44772a832fpengcheng chen - Sun Microsystems - Beijing China /* dc->npoints > 0 */
d62cb7fff63265824a8e6725a3bcef698f6134c9hx /* g_print("calligraphics(1|2) reset\n"); */
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China gint const nb1 = sp_bezier_fit_cubic_r(b1, dc->point1, dc->npoints,
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China tolerance_sq, BEZIER_MAX_BEZIERS);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China g_assert( nb1 * BEZIER_SIZE <= gint(G_N_ELEMENTS(b1)) );
c533a883a71cff9ff32df1c53c31201e1cbf371fhx gint const nb2 = sp_bezier_fit_cubic_r(b2, dc->point2, dc->npoints,
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* Fit and draw and reset state */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* CanvasShape */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx for (NR::Point *bp1 = b1; bp1 < b1 + BEZIER_SIZE * nb1; bp1 += BEZIER_SIZE) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx for (NR::Point *bp2 = b2 + BEZIER_SIZE * ( nb2 - 1 ); bp2 >= b2; bp2 -= BEZIER_SIZE) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_curve_curveto(dc->currentcurve, bp2[2], bp2[1], bp2[0]);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->currentshape), dc->currentcurve);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China /* Current calligraphic */
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China for (NR::Point *bp1 = b1; bp1 < b1 + BEZIER_SIZE * nb1; bp1 += BEZIER_SIZE) {
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China sp_curve_curveto(dc->cal1, bp1[1], bp1[2], bp1[3]);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China for (NR::Point *bp2 = b2; bp2 < b2 + BEZIER_SIZE * nb2; bp2 += BEZIER_SIZE) {
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China sp_curve_curveto(dc->cal2, bp2[1], bp2[2], bp2[3]);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* fixme: ??? */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx g_print("[fit_and_split_calligraphics] failed to fit-cubic.\n");
d2a61391af480de12cf4264080d7254a6de96e2apengcheng chen - Sun Microsystems - Beijing China /* Fit and draw and copy last point */
d2a61391af480de12cf4264080d7254a6de96e2apengcheng chen - Sun Microsystems - Beijing China#ifdef DYNA_DRAW_VERBOSE
d2a61391af480de12cf4264080d7254a6de96e2apengcheng chen - Sun Microsystems - Beijing China g_print("[%d]Yup\n", dc->npoints);
d2a61391af480de12cf4264080d7254a6de96e2apengcheng chen - Sun Microsystems - Beijing China if (!release) {
d2a61391af480de12cf4264080d7254a6de96e2apengcheng chen - Sun Microsystems - Beijing China g_assert(!sp_curve_empty(dc->currentcurve));
d2a61391af480de12cf4264080d7254a6de96e2apengcheng chen - Sun Microsystems - Beijing China SPCanvasItem *cbp = sp_canvas_item_new(SP_DT_SKETCH(SP_EVENT_CONTEXT(dc)->desktop),
d2a61391af480de12cf4264080d7254a6de96e2apengcheng chen - Sun Microsystems - Beijing China SP_TYPE_CANVAS_BPATH,
d2a61391af480de12cf4264080d7254a6de96e2apengcheng chen - Sun Microsystems - Beijing China SPCurve *curve = sp_curve_copy(dc->currentcurve);
d2a61391af480de12cf4264080d7254a6de96e2apengcheng chen - Sun Microsystems - Beijing China sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH (cbp), curve);
d2a61391af480de12cf4264080d7254a6de96e2apengcheng chen - Sun Microsystems - Beijing China sp_curve_unref(curve);
d2a61391af480de12cf4264080d7254a6de96e2apengcheng chen - Sun Microsystems - Beijing China sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cbp), 0x000000ff, SP_WIND_RULE_EVENODD);
d2a61391af480de12cf4264080d7254a6de96e2apengcheng chen - Sun Microsystems - Beijing China sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cbp), 0x00000000, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
d2a61391af480de12cf4264080d7254a6de96e2apengcheng chen - Sun Microsystems - Beijing China /* fixme: Cannot we cascade it to root more clearly? */
d2a61391af480de12cf4264080d7254a6de96e2apengcheng chen - Sun Microsystems - Beijing China g_signal_connect(G_OBJECT(cbp), "event", G_CALLBACK(sp_desktop_root_handler), SP_EVENT_CONTEXT(dc)->desktop);
d2a61391af480de12cf4264080d7254a6de96e2apengcheng chen - Sun Microsystems - Beijing China dc->segments = g_slist_prepend(dc->segments, cbp);
d2a61391af480de12cf4264080d7254a6de96e2apengcheng chen - Sun Microsystems - Beijing China dc->point1[0] = dc->point1[dc->npoints - 1];
d2a61391af480de12cf4264080d7254a6de96e2apengcheng chen - Sun Microsystems - Beijing China dc->point2[0] = dc->point2[dc->npoints - 1];
d2a61391af480de12cf4264080d7254a6de96e2apengcheng chen - Sun Microsystems - Beijing China dc->npoints = 1;
d2a61391af480de12cf4264080d7254a6de96e2apengcheng chen - Sun Microsystems - Beijing China draw_temporary_box(dc);
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->currentshape), dc->currentcurve);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx Local Variables:
c533a883a71cff9ff32df1c53c31201e1cbf371fhx c-file-style:"stroustrup"
c533a883a71cff9ff32df1c53c31201e1cbf371fhx c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
c533a883a71cff9ff32df1c53c31201e1cbf371fhx indent-tabs-mode:nil
c533a883a71cff9ff32df1c53c31201e1cbf371fhx fill-column:99
c533a883a71cff9ff32df1c53c31201e1cbf371fhx// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :