dyna-draw-context.cpp revision 597de4a7a6a70f44324f3ddf984c80210b6c268a
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#define __SP_DYNA_DRAW_CONTEXT_C__
0dc2366f7b9f9f36e10909b1e95edbf2a261c2acVenugopal Iyer
c533a883a71cff9ff32df1c53c31201e1cbf371fhx/*
c533a883a71cff9ff32df1c53c31201e1cbf371fhx * Handwriting-like drawing mode
c533a883a71cff9ff32df1c53c31201e1cbf371fhx *
c533a883a71cff9ff32df1c53c31201e1cbf371fhx * Authors:
c533a883a71cff9ff32df1c53c31201e1cbf371fhx * Mitsuru Oka <oka326@parkcity.ne.jp>
c533a883a71cff9ff32df1c53c31201e1cbf371fhx * Lauris Kaplinski <lauris@kaplinski.com>
c533a883a71cff9ff32df1c53c31201e1cbf371fhx * bulia byak <buliabyak@users.sf.net>
c533a883a71cff9ff32df1c53c31201e1cbf371fhx *
c533a883a71cff9ff32df1c53c31201e1cbf371fhx * The original dynadraw code:
c533a883a71cff9ff32df1c53c31201e1cbf371fhx * Paul Haeberli <paul@sgi.com>
c533a883a71cff9ff32df1c53c31201e1cbf371fhx *
c533a883a71cff9ff32df1c53c31201e1cbf371fhx * Copyright (C) 1998 The Free Software Foundation
c533a883a71cff9ff32df1c53c31201e1cbf371fhx * Copyright (C) 1999-2005 authors
c533a883a71cff9ff32df1c53c31201e1cbf371fhx * Copyright (C) 2001-2002 Ximian, Inc.
c533a883a71cff9ff32df1c53c31201e1cbf371fhx *
c533a883a71cff9ff32df1c53c31201e1cbf371fhx * Released under GNU GPL, read the file 'COPYING' for more information
c533a883a71cff9ff32df1c53c31201e1cbf371fhx */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx/*
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.
c533a883a71cff9ff32df1c53c31201e1cbf371fhx */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#define noDYNA_DRAW_VERBOSE
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#include "config.h"
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#include <gtk/gtk.h>
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#include <gdk/gdkkeysyms.h>
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#include "svg/svg.h"
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#include "display/canvas-bpath.h"
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#include "display/bezier-utils.h"
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#include "macros.h"
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#include "document.h"
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#include "selection.h"
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#include "desktop.h"
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#include "desktop-events.h"
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#include "desktop-handles.h"
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#include "desktop-affine.h"
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#include "desktop-style.h"
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#include "message-context.h"
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#include "pixmaps/cursor-calligraphy.xpm"
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#include "dyna-draw-context.h"
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#include "libnr/n-art-bpath.h"
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng#include "libnr/nr-path.h"
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#include "xml/repr.h"
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#include "context-fns.h"
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#include "sp-item.h"
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#define DDC_RED_RGBA 0xff0000ff
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#define DDC_GREEN_RGBA 0x000000ff
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China#define SAMPLE_TIMEOUT 10
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#define TOLERANCE_LINE 1.0
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#define TOLERANCE_CALLIGRAPHIC 3.0
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#define DYNA_EPSILON 1.0e-6
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#define DYNA_MIN_WIDTH 1.0e-6
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#define DRAG_MIN 0.0
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#define DRAG_DEFAULT 1.0
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#define DRAG_MAX 1.0
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
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);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
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);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void clear_current(SPDynaDrawContext *dc);
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void set_to_accumulated(SPDynaDrawContext *dc);
43439c96b8398c01c375889c79bed72d78fb4c39hxstatic void concat_current_line(SPDynaDrawContext *dc);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing Chinastatic void accumulate_calligraphic(SPDynaDrawContext *dc);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
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);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
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);
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void draw_temporary_box(SPDynaDrawContext *dc);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic SPEventContextClass *parent_class;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhxGtkType
c533a883a71cff9ff32df1c53c31201e1cbf371fhxsp_dyna_draw_context_get_type(void)
c533a883a71cff9ff32df1c53c31201e1cbf371fhx{
c533a883a71cff9ff32df1c53c31201e1cbf371fhx static GType type = 0;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (!type) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx GTypeInfo info = {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sizeof(SPDynaDrawContextClass),
c533a883a71cff9ff32df1c53c31201e1cbf371fhx NULL, NULL,
c533a883a71cff9ff32df1c53c31201e1cbf371fhx (GClassInitFunc) sp_dyna_draw_context_class_init,
c533a883a71cff9ff32df1c53c31201e1cbf371fhx NULL, NULL,
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sizeof(SPDynaDrawContext),
c533a883a71cff9ff32df1c53c31201e1cbf371fhx 4,
c533a883a71cff9ff32df1c53c31201e1cbf371fhx (GInstanceInitFunc) sp_dyna_draw_context_init,
c533a883a71cff9ff32df1c53c31201e1cbf371fhx NULL, /* value_table */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx };
c533a883a71cff9ff32df1c53c31201e1cbf371fhx type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPDynaDrawContext", &info, (GTypeFlags)0);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx return type;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx}
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void
c533a883a71cff9ff32df1c53c31201e1cbf371fhxsp_dyna_draw_context_class_init(SPDynaDrawContextClass *klass)
c533a883a71cff9ff32df1c53c31201e1cbf371fhx{
c533a883a71cff9ff32df1c53c31201e1cbf371fhx GObjectClass *object_class = (GObjectClass *) klass;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx object_class->dispose = sp_dyna_draw_context_dispose;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx event_context_class->setup = sp_dyna_draw_context_setup;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx event_context_class->set = sp_dyna_draw_context_set;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx event_context_class->root_handler = sp_dyna_draw_context_root_handler;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx}
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void
c533a883a71cff9ff32df1c53c31201e1cbf371fhxsp_dyna_draw_context_init(SPDynaDrawContext *ddc)
c533a883a71cff9ff32df1c53c31201e1cbf371fhx{
c533a883a71cff9ff32df1c53c31201e1cbf371fhx SPEventContext *event_context = SP_EVENT_CONTEXT(ddc);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx event_context->cursor_shape = cursor_calligraphy_xpm;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx event_context->hot_x = 4;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx event_context->hot_y = 4;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->accumulated = NULL;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->segments = NULL;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->currentcurve = NULL;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->currentshape = NULL;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->npoints = 0;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->cal1 = NULL;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->cal2 = NULL;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->repr = NULL;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* DynaDraw values */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->cur = NR::Point(0,0);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->last = NR::Point(0,0);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->vel = NR::Point(0,0);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->acc = NR::Point(0,0);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->ang = NR::Point(0,0);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->del = NR::Point(0,0);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* attributes */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->use_timeout = FALSE;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->use_calligraphic = TRUE;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->timer_id = 0;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->dragging = FALSE;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->dynahand = FALSE;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->mass = 0.3;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->drag = DRAG_DEFAULT;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->angle = 30.0;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->width = 0.2;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->vel_thin = 0.1;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->flatness = 0.9;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx}
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void
c533a883a71cff9ff32df1c53c31201e1cbf371fhxsp_dyna_draw_context_dispose(GObject *object)
c533a883a71cff9ff32df1c53c31201e1cbf371fhx{
c533a883a71cff9ff32df1c53c31201e1cbf371fhx SPDynaDrawContext *ddc = SP_DYNA_DRAW_CONTEXT(object);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (ddc->accumulated) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->accumulated = sp_curve_unref(ddc->accumulated);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx while (ddc->segments) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx gtk_object_destroy(GTK_OBJECT(ddc->segments->data));
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->segments = g_slist_remove(ddc->segments, ddc->segments->data);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (ddc->currentcurve) ddc->currentcurve = sp_curve_unref(ddc->currentcurve);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (ddc->cal1) ddc->cal1 = sp_curve_unref(ddc->cal1);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (ddc->cal2) ddc->cal2 = sp_curve_unref(ddc->cal2);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (ddc->currentshape) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx gtk_object_destroy(GTK_OBJECT(ddc->currentshape));
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->currentshape = NULL;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (ddc->_message_context) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx delete ddc->_message_context;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx G_OBJECT_CLASS(parent_class)->dispose(object);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx}
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void
c533a883a71cff9ff32df1c53c31201e1cbf371fhxsp_dyna_draw_context_setup(SPEventContext *ec)
c533a883a71cff9ff32df1c53c31201e1cbf371fhx{
c533a883a71cff9ff32df1c53c31201e1cbf371fhx SPDynaDrawContext *ddc = SP_DYNA_DRAW_CONTEXT(ec);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (((SPEventContextClass *) parent_class)->setup)
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ((SPEventContextClass *) parent_class)->setup(ec);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->accumulated = sp_curve_new_sized(32);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->currentcurve = sp_curve_new_sized(4);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->cal1 = sp_curve_new_sized(32);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->cal2 = sp_curve_new_sized(32);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
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
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 sp_event_context_read(ec, "usepressure");
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_event_context_read(ec, "usetilt");
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->is_drawing = false;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
c533a883a71cff9ff32df1c53c31201e1cbf371fhx}
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void
c533a883a71cff9ff32df1c53c31201e1cbf371fhxsp_dyna_draw_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
c533a883a71cff9ff32df1c53c31201e1cbf371fhx{
c533a883a71cff9ff32df1c53c31201e1cbf371fhx SPDynaDrawContext *ddc = SP_DYNA_DRAW_CONTEXT(ec);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (!strcmp(key, "mass")) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.2 );
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->mass = CLAMP(dval, -1000.0, 1000.0);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx } else if (!strcmp(key, "drag")) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx double const dval = ( val ? g_ascii_strtod (val, NULL) : DRAG_DEFAULT );
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->drag = CLAMP(dval, DRAG_MIN, DRAG_MAX);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx } else if (!strcmp(key, "angle")) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.0);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->angle = CLAMP (dval, -90, 90);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx } else if (!strcmp(key, "width")) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.1 );
43439c96b8398c01c375889c79bed72d78fb4c39hx ddc->width = CLAMP(dval, -1000.0, 1000.0);
43439c96b8398c01c375889c79bed72d78fb4c39hx } else if (!strcmp(key, "thinning")) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.1 );
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->vel_thin = CLAMP(dval, -1.0, 1.0);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx } else if (!strcmp(key, "tremor")) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.0 );
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->tremor = CLAMP(dval, 0.0, 1.0);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx } else if (!strcmp(key, "flatness")) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx double const dval = ( val ? g_ascii_strtod (val, NULL) : 1.0 );
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->flatness = CLAMP(dval, 0, 1.0);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx } else if (!strcmp(key, "usepressure")) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->usepressure = (val && strcmp(val, "0"));
c533a883a71cff9ff32df1c53c31201e1cbf371fhx } else if (!strcmp(key, "usetilt")) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ddc->usetilt = (val && strcmp(val, "0"));
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx //g_print("DDC: %g %g %g %g\n", ddc->mass, ddc->drag, ddc->angle, ddc->width);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China}
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic double
c533a883a71cff9ff32df1c53c31201e1cbf371fhxflerp(double f0, double f1, double p)
c533a883a71cff9ff32df1c53c31201e1cbf371fhx{
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China return f0 + ( f1 - f0 ) * p;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx}
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx/* Get normalized point */
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic NR::Point
c533a883a71cff9ff32df1c53c31201e1cbf371fhxsp_dyna_draw_get_npoint(SPDynaDrawContext const *dc, NR::Point v)
c533a883a71cff9ff32df1c53c31201e1cbf371fhx{
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);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx}
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
43439c96b8398c01c375889c79bed72d78fb4c39hx/* Get view point */
43439c96b8398c01c375889c79bed72d78fb4c39hxstatic NR::Point
43439c96b8398c01c375889c79bed72d78fb4c39hxsp_dyna_draw_get_vpoint(SPDynaDrawContext const *dc, NR::Point n)
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China{
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}
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China
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{
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 China}
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing Chinastatic void
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing Chinasp_dyna_draw_reset(SPDynaDrawContext *dc, NR::Point p)
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China{
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 China}
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing Chinastatic void
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing Chinasp_dyna_draw_extinput(SPDynaDrawContext *dc, GdkEvent *event)
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China{
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 else
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China dc->pressure = DDC_DEFAULT_PRESSURE;
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China
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);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China else
d2a61391af480de12cf4264080d7254a6de96e2apengcheng chen - Sun Microsystems - Beijing China dc->xtilt = DDC_DEFAULT_TILT;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (gdk_event_get_axis (event, GDK_AXIS_YTILT, &dc->ytilt))
c533a883a71cff9ff32df1c53c31201e1cbf371fhx dc->ytilt = CLAMP (dc->ytilt, DDC_MIN_TILT, DDC_MAX_TILT);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx else
c533a883a71cff9ff32df1c53c31201e1cbf371fhx dc->ytilt = DDC_DEFAULT_TILT;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx}
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic gboolean
c533a883a71cff9ff32df1c53c31201e1cbf371fhxsp_dyna_draw_apply(SPDynaDrawContext *dc, NR::Point p)
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China{
c533a883a71cff9ff32df1c53c31201e1cbf371fhx NR::Point n = sp_dyna_draw_get_npoint(dc, p);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
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
0dc2366f7b9f9f36e10909b1e95edbf2a261c2acVenugopal Iyer /* Calculate force and acceleration */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx NR::Point force = n - dc->cur;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if ( NR::L2(force) < DYNA_EPSILON ) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx return FALSE;
f3c4902c7f91725a7b538a3d82f1993bff02944fpengcheng chen - Sun Microsystems - Beijing China }
19d332fefbc61327bb6187d0eb818629f3b52c6ffei feng - Sun Microsystems - Beijing China
19d332fefbc61327bb6187d0eb818629f3b52c6ffei feng - Sun Microsystems - Beijing China dc->acc = force / mass;
6f12def440a1ce798ab128210a43414d173669f0pengcheng chen - Sun Microsystems - Beijing China
19d332fefbc61327bb6187d0eb818629f3b52c6ffei feng - Sun Microsystems - Beijing China /* Calculate new velocity */
19d332fefbc61327bb6187d0eb818629f3b52c6ffei feng - Sun Microsystems - Beijing China dc->vel += dc->acc;
19d332fefbc61327bb6187d0eb818629f3b52c6ffei feng - Sun Microsystems - Beijing China
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* Calculate angle of drawing tool */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx double a1;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (dc->usetilt) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx // 1a. calculate nib angle from input device tilt:
c533a883a71cff9ff32df1c53c31201e1cbf371fhx gdouble length = std::sqrt(dc->xtilt*dc->xtilt + dc->ytilt*dc->ytilt);;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (length > 0) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx NR::Point ang1 = NR::Point(dc->ytilt/length, dc->xtilt/length);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx a1 = atan2(ang1);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx else
c533a883a71cff9ff32df1c53c31201e1cbf371fhx a1 = 0.0;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx else {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx // 1b. fixed dc->angle (absolutely flat nib):
c533a883a71cff9ff32df1c53c31201e1cbf371fhx double const radians = ( (dc->angle - 90) / 180.0 ) * M_PI;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx NR::Point ang1 = NR::Point(-sin(radians), cos(radians));
c533a883a71cff9ff32df1c53c31201e1cbf371fhx a1 = atan2(ang1);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
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 if ( mag_vel < DYNA_EPSILON ) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx return FALSE;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx NR::Point ang2 = NR::rot90(dc->vel) / mag_vel;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx // 3. Average them using flatness parameter:
c533a883a71cff9ff32df1c53c31201e1cbf371fhx // calculate angles
c533a883a71cff9ff32df1c53c31201e1cbf371fhx double a2 = atan2(ang2);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx // flip a2 to force it to be in the same half-circle as a1
c533a883a71cff9ff32df1c53c31201e1cbf371fhx bool flipped = false;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (fabs (a2-a1) > 0.5*M_PI) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx a2 += M_PI;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx flipped = true;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx // normalize a2
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (a2 > M_PI)
c533a883a71cff9ff32df1c53c31201e1cbf371fhx a2 -= 2*M_PI;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (a2 < -M_PI)
c533a883a71cff9ff32df1c53c31201e1cbf371fhx a2 += 2*M_PI;
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 dc->ang = NR::Point (cos (new_ang), sin (new_ang));
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* Apply drag */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx dc->vel *= 1.0 - drag;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* Update position */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx dc->last = dc->cur;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx dc->cur += dc->vel;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx return TRUE;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx}
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void
c533a883a71cff9ff32df1c53c31201e1cbf371fhxsp_dyna_draw_brush(SPDynaDrawContext *dc)
c533a883a71cff9ff32df1c53c31201e1cbf371fhx{
c533a883a71cff9ff32df1c53c31201e1cbf371fhx g_assert( dc->npoints >= 0 && dc->npoints < SAMPLING_SIZE );
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (dc->use_calligraphic) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* calligraphics */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx // How much velocity thins strokestyle
c533a883a71cff9ff32df1c53c31201e1cbf371fhx double vel_thin = flerp (0, 160, dc->vel_thin);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx // Influence of pressure on thickness
c533a883a71cff9ff32df1c53c31201e1cbf371fhx double pressure_thick = (dc->usepressure ? dc->pressure : 1.0);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx double width = ( pressure_thick - vel_thin * NR::L2(dc->vel) ) * dc->width;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx double tremble_left = 0, tremble_right = 0;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (dc->tremor > 0) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx // obtain two normally distributed random variables, using polar Box-Muller transform
c533a883a71cff9ff32df1c53c31201e1cbf371fhx double x1, x2, w, y1, y2;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx do {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx x1 = 2.0 * g_random_double_range(0,1) - 1.0;
0dc2366f7b9f9f36e10909b1e95edbf2a261c2acVenugopal Iyer x2 = 2.0 * g_random_double_range(0,1) - 1.0;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx w = x1 * x1 + x2 * x2;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx } while ( w >= 1.0 );
c533a883a71cff9ff32df1c53c31201e1cbf371fhx w = sqrt( (-2.0 * log( w ) ) / w );
c533a883a71cff9ff32df1c53c31201e1cbf371fhx y1 = x1 * w;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx y2 = x2 * w;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
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));
0dc2366f7b9f9f36e10909b1e95edbf2a261c2acVenugopal Iyer }
0dc2366f7b9f9f36e10909b1e95edbf2a261c2acVenugopal Iyer
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if ( width < 0.02 * dc->width ) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx width = 0.02 * dc->width;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
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
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
c533a883a71cff9ff32df1c53c31201e1cbf371fhx dc->del = 0.5*(del_left + del_right);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx } else {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx dc->point1[dc->npoints] = sp_dyna_draw_get_curr_vpoint(dc);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx dc->npoints++;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx}
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic gint
c533a883a71cff9ff32df1c53c31201e1cbf371fhxsp_dyna_draw_timeout_handler(gpointer data)
c533a883a71cff9ff32df1c53c31201e1cbf371fhx{
c533a883a71cff9ff32df1c53c31201e1cbf371fhx SPDynaDrawContext *dc = SP_DYNA_DRAW_CONTEXT(data);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx SPDesktop *desktop = SP_EVENT_CONTEXT(dc)->desktop;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx SPCanvas *canvas = SP_CANVAS(SP_DT_CANVAS(desktop));
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx dc->dragging = TRUE;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx dc->dynahand = TRUE;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx int x, y;
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)) {
d62cb7fff63265824a8e6725a3bcef698f6134c9hx return TRUE;
d62cb7fff63265824a8e6725a3bcef698f6134c9hx }
d62cb7fff63265824a8e6725a3bcef698f6134c9hx
d62cb7fff63265824a8e6725a3bcef698f6134c9hx if ( dc->cur != dc->last ) {
d62cb7fff63265824a8e6725a3bcef698f6134c9hx sp_dyna_draw_brush(dc);
d62cb7fff63265824a8e6725a3bcef698f6134c9hx g_assert( dc->npoints > 0 );
d62cb7fff63265824a8e6725a3bcef698f6134c9hx fit_and_split(dc, FALSE);
09539a3c2da6fef054f5306232ef0480de261eabpengcheng chen - Sun Microsystems - Beijing China }
09539a3c2da6fef054f5306232ef0480de261eabpengcheng chen - Sun Microsystems - Beijing China
09539a3c2da6fef054f5306232ef0480de261eabpengcheng chen - Sun Microsystems - Beijing China return TRUE;
09539a3c2da6fef054f5306232ef0480de261eabpengcheng chen - Sun Microsystems - Beijing China}
09539a3c2da6fef054f5306232ef0480de261eabpengcheng chen - Sun Microsystems - Beijing China
d40f4da491abdcae192eb797766f5f44772a832fpengcheng chen - Sun Microsystems - Beijing Chinavoid
d40f4da491abdcae192eb797766f5f44772a832fpengcheng chen - Sun Microsystems - Beijing Chinasp_ddc_update_toolbox (SPDesktop *desktop, const gchar *id, double value)
d40f4da491abdcae192eb797766f5f44772a832fpengcheng chen - Sun Microsystems - Beijing China{
d62cb7fff63265824a8e6725a3bcef698f6134c9hx desktop->setToolboxAdjustmentValue (id, value);
d40f4da491abdcae192eb797766f5f44772a832fpengcheng chen - Sun Microsystems - Beijing China}
d62cb7fff63265824a8e6725a3bcef698f6134c9hx
d40f4da491abdcae192eb797766f5f44772a832fpengcheng chen - Sun Microsystems - Beijing Chinagint
d62cb7fff63265824a8e6725a3bcef698f6134c9hxsp_dyna_draw_context_root_handler(SPEventContext *event_context,
d62cb7fff63265824a8e6725a3bcef698f6134c9hx GdkEvent *event)
d62cb7fff63265824a8e6725a3bcef698f6134c9hx{
c533a883a71cff9ff32df1c53c31201e1cbf371fhx SPDynaDrawContext *dc = SP_DYNA_DRAW_CONTEXT(event_context);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx SPDesktop *desktop = event_context->desktop;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx gint ret = FALSE;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx switch (event->type) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx case GDK_BUTTON_PRESS:
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if ( event->button.button == 1 ) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx return TRUE;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx NR::Point const button_w(event->button.x,
c533a883a71cff9ff32df1c53c31201e1cbf371fhx event->button.y);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx NR::Point const button_dt(desktop->w2d(button_w));
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_dyna_draw_reset(dc, button_dt);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_dyna_draw_extinput(dc, event);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_dyna_draw_apply(dc, button_dt);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_curve_reset(dc->accumulated);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (dc->repr) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx dc->repr = NULL;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* initialize first point */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx dc->npoints = 0;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ( dc->use_timeout
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ? ( GDK_KEY_PRESS_MASK |
c533a883a71cff9ff32df1c53c31201e1cbf371fhx GDK_BUTTON_RELEASE_MASK |
c533a883a71cff9ff32df1c53c31201e1cbf371fhx GDK_BUTTON_PRESS_MASK )
c533a883a71cff9ff32df1c53c31201e1cbf371fhx : ( GDK_KEY_PRESS_MASK |
c533a883a71cff9ff32df1c53c31201e1cbf371fhx GDK_BUTTON_RELEASE_MASK |
c533a883a71cff9ff32df1c53c31201e1cbf371fhx GDK_POINTER_MOTION_MASK |
c533a883a71cff9ff32df1c53c31201e1cbf371fhx GDK_BUTTON_PRESS_MASK ) ),
c533a883a71cff9ff32df1c53c31201e1cbf371fhx NULL,
c533a883a71cff9ff32df1c53c31201e1cbf371fhx event->button.time);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if ( dc->use_timeout && !dc->timer_id ) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx dc->timer_id = gtk_timeout_add(SAMPLE_TIMEOUT, sp_dyna_draw_timeout_handler, dc);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China ret = TRUE;
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China dc->is_drawing = true;
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China }
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China break;
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
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
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 ret = TRUE;
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China break;
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China }
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China
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 }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ret = TRUE;
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China }
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China break;
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China
c533a883a71cff9ff32df1c53c31201e1cbf371fhx case GDK_BUTTON_RELEASE:
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 {
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China gtk_timeout_remove(dc->timer_id);
19d332fefbc61327bb6187d0eb818629f3b52c6ffei feng - Sun Microsystems - Beijing China dc->timer_id = 0;
19d332fefbc61327bb6187d0eb818629f3b52c6ffei feng - Sun Microsystems - Beijing China }
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China if ( dc->dragging && event->button.button == 1 ) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx dc->dragging = FALSE;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* release */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (dc->dynahand) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx dc->dynahand = FALSE;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* Remove all temporary line segments */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx while (dc->segments) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx gtk_object_destroy(GTK_OBJECT(dc->segments->data));
c533a883a71cff9ff32df1c53c31201e1cbf371fhx dc->segments = g_slist_remove(dc->segments, dc->segments->data);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* Create object */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx fit_and_split(dc, TRUE);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (dc->use_calligraphic) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx accumulate_calligraphic(dc);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx } else {
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China concat_current_line(dc);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx set_to_accumulated(dc); /* temporal implementation */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (dc->use_calligraphic /* || dc->cinside*/) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* reset accumulated curve */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_curve_reset(dc->accumulated);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx clear_current(dc);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (dc->repr) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx dc->repr = NULL;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China }
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ret = TRUE;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx break;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx case GDK_KEY_PRESS:
c533a883a71cff9ff32df1c53c31201e1cbf371fhx switch (get_group0_keyval (&event->key)) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx case GDK_Up:
c533a883a71cff9ff32df1c53c31201e1cbf371fhx case GDK_KP_Up:
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (!MOD__CTRL_ONLY) {
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China dc->angle += 5.0;
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China if (dc->angle > 90.0)
c533a883a71cff9ff32df1c53c31201e1cbf371fhx dc->angle = 90.0;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_ddc_update_toolbox (desktop, "calligraphy-angle", dc->angle);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ret = TRUE;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx break;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx case GDK_Down:
c533a883a71cff9ff32df1c53c31201e1cbf371fhx case GDK_KP_Down:
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (!MOD__CTRL_ONLY) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx dc->angle -= 5.0;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (dc->angle < -90.0)
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 ret = TRUE;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx break;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx case GDK_Right:
c533a883a71cff9ff32df1c53c31201e1cbf371fhx case GDK_KP_Right:
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (!MOD__CTRL_ONLY) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx dc->width += 0.01;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (dc->width > 1.0)
c533a883a71cff9ff32df1c53c31201e1cbf371fhx dc->width = 1.0;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_ddc_update_toolbox (desktop, "altx-calligraphy", dc->width); // the same spinbutton is for alt+x
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ret = TRUE;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx break;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx case GDK_Left:
c533a883a71cff9ff32df1c53c31201e1cbf371fhx case GDK_KP_Left:
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (!MOD__CTRL_ONLY) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx dc->width -= 0.01;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (dc->width < 0.01)
c533a883a71cff9ff32df1c53c31201e1cbf371fhx dc->width = 0.01;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_ddc_update_toolbox (desktop, "altx-calligraphy", dc->width);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ret = TRUE;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx break;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx case GDK_x:
c533a883a71cff9ff32df1c53c31201e1cbf371fhx case GDK_X:
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (MOD__ALT_ONLY) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx desktop->setToolboxFocusTo ("altx-calligraphy");
c533a883a71cff9ff32df1c53c31201e1cbf371fhx ret = TRUE;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx break;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx case GDK_Escape:
c533a883a71cff9ff32df1c53c31201e1cbf371fhx SP_DT_SELECTION(desktop)->clear();
c533a883a71cff9ff32df1c53c31201e1cbf371fhx break;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx default:
c533a883a71cff9ff32df1c53c31201e1cbf371fhx break;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx default:
c533a883a71cff9ff32df1c53c31201e1cbf371fhx break;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
19d332fefbc61327bb6187d0eb818629f3b52c6ffei feng - Sun Microsystems - Beijing China
19d332fefbc61327bb6187d0eb818629f3b52c6ffei feng - Sun Microsystems - Beijing China if (!ret) {
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);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx return ret;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx}
c50ced99bb75273dc8c4c36876d2e8f59a64f4b1pengcheng chen - Sun Microsystems - Beijing China
c50ced99bb75273dc8c4c36876d2e8f59a64f4b1pengcheng chen - Sun Microsystems - Beijing China
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void
c533a883a71cff9ff32df1c53c31201e1cbf371fhxclear_current(SPDynaDrawContext *dc)
c533a883a71cff9ff32df1c53c31201e1cbf371fhx{
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 */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_curve_reset(dc->currentcurve);
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 */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx dc->npoints = 0;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx}
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void
c533a883a71cff9ff32df1c53c31201e1cbf371fhxset_to_accumulated(SPDynaDrawContext *dc)
c533a883a71cff9ff32df1c53c31201e1cbf371fhx{
c533a883a71cff9ff32df1c53c31201e1cbf371fhx SPDesktop *desktop = SP_EVENT_CONTEXT(dc)->desktop;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (!sp_curve_empty(dc->accumulated)) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx NArtBpath *abp;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx gchar *str;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (!dc->repr) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* Create object */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx Inkscape::XML::Node *repr = sp_repr_new("svg:path");
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* Set style */
f3c4902c7f91725a7b538a3d82f1993bff02944fpengcheng chen - Sun Microsystems - Beijing China sp_desktop_apply_style_tool (desktop, repr, "tools.calligraphic", false);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China
19d332fefbc61327bb6187d0eb818629f3b52c6ffei feng - Sun Microsystems - Beijing China dc->repr = repr;
43439c96b8398c01c375889c79bed72d78fb4c39hx
43439c96b8398c01c375889c79bed72d78fb4c39hx SPItem *item=SP_ITEM(desktop->currentLayer()->appendChildRepr(dc->repr));
c533a883a71cff9ff32df1c53c31201e1cbf371fhx Inkscape::GC::release(dc->repr);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
c533a883a71cff9ff32df1c53c31201e1cbf371fhx item->updateRepr();
c533a883a71cff9ff32df1c53c31201e1cbf371fhx SP_DT_SELECTION(desktop)->set(dc->repr);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
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 );
c533a883a71cff9ff32df1c53c31201e1cbf371fhx nr_free(abp);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China dc->repr->setAttribute("d", str);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China g_free(str);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx } else {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (dc->repr) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_repr_unparent(dc->repr);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx dc->repr = NULL;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China sp_document_done(SP_DT_DOCUMENT(desktop));
c533a883a71cff9ff32df1c53c31201e1cbf371fhx}
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing Chinastatic void
c533a883a71cff9ff32df1c53c31201e1cbf371fhxconcat_current_line(SPDynaDrawContext *dc)
c533a883a71cff9ff32df1c53c31201e1cbf371fhx{
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (!sp_curve_empty(dc->currentcurve)) {
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 }
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China bpath = sp_curve_last_bpath(dc->currentcurve);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if ( bpath->code == NR_CURVETO ) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_curve_curveto(dc->accumulated,
c533a883a71cff9ff32df1c53c31201e1cbf371fhx bpath->x1, bpath->y1,
c533a883a71cff9ff32df1c53c31201e1cbf371fhx bpath->x2, bpath->y2,
c533a883a71cff9ff32df1c53c31201e1cbf371fhx bpath->x3, bpath->y3);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx } else if ( bpath->code == NR_LINETO ) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_curve_lineto(dc->accumulated, bpath->x3, bpath->y3);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx } else {
19d332fefbc61327bb6187d0eb818629f3b52c6ffei feng - Sun Microsystems - Beijing China g_assert_not_reached();
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx}
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void
c533a883a71cff9ff32df1c53c31201e1cbf371fhxaccumulate_calligraphic(SPDynaDrawContext *dc)
c533a883a71cff9ff32df1c53c31201e1cbf371fhx{
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if ( !sp_curve_empty(dc->cal1) && !sp_curve_empty(dc->cal2) ) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_curve_reset(dc->accumulated); /* Is this required ?? */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx SPCurve *rev_cal2 = sp_curve_reverse(dc->cal2);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_curve_append(dc->accumulated, dc->cal1, FALSE);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_curve_append(dc->accumulated, rev_cal2, TRUE);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_curve_closepath(dc->accumulated);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_curve_unref(rev_cal2);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_curve_reset(dc->cal1);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_curve_reset(dc->cal2);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx}
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void
c533a883a71cff9ff32df1c53c31201e1cbf371fhxfit_and_split(SPDynaDrawContext *dc,
c533a883a71cff9ff32df1c53c31201e1cbf371fhx gboolean release)
c533a883a71cff9ff32df1c53c31201e1cbf371fhx{
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (dc->use_calligraphic) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx fit_and_split_calligraphics(dc, release);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx } else {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx fit_and_split_line(dc, release);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx}
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic double square(double const x)
c533a883a71cff9ff32df1c53c31201e1cbf371fhx{
c533a883a71cff9ff32df1c53c31201e1cbf371fhx return x * x;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx}
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void
c533a883a71cff9ff32df1c53c31201e1cbf371fhxfit_and_split_line(SPDynaDrawContext *dc,
c533a883a71cff9ff32df1c53c31201e1cbf371fhx gboolean release)
c533a883a71cff9ff32df1c53c31201e1cbf371fhx{
c533a883a71cff9ff32df1c53c31201e1cbf371fhx double const tolerance_sq = square( NR::expansion(SP_EVENT_CONTEXT(dc)->desktop->w2d()) * TOLERANCE_LINE );
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx NR::Point b[4];
c533a883a71cff9ff32df1c53c31201e1cbf371fhx double const n_segs = sp_bezier_fit_cubic(b, dc->point1, dc->npoints, tolerance_sq);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if ( n_segs > 0
c533a883a71cff9ff32df1c53c31201e1cbf371fhx && dc->npoints < SAMPLING_SIZE )
c533a883a71cff9ff32df1c53c31201e1cbf371fhx {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* Fit and draw and reset state */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#ifdef DYNA_DRAW_VERBOSE
c533a883a71cff9ff32df1c53c31201e1cbf371fhx g_print("%d", dc->npoints);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#endif
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_curve_reset(dc->currentcurve);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_curve_moveto(dc->currentcurve, b[0]);
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 } else {
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China /* Fit and draw and copy last point */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#ifdef DYNA_DRAW_VERBOSE
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China g_print("[%d]Yup\n", dc->npoints);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China#endif
c533a883a71cff9ff32df1c53c31201e1cbf371fhx g_assert(!sp_curve_empty(dc->currentcurve));
c533a883a71cff9ff32df1c53c31201e1cbf371fhx concat_current_line(dc);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx SPCanvasItem *cbp = sp_canvas_item_new(SP_DT_SKETCH(SP_EVENT_CONTEXT(dc)->desktop),
c533a883a71cff9ff32df1c53c31201e1cbf371fhx SP_TYPE_CANVAS_BPATH,
c533a883a71cff9ff32df1c53c31201e1cbf371fhx NULL);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx SPCurve *curve = sp_curve_copy(dc->currentcurve);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cbp), curve);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_curve_unref(curve);
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
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];
c533a883a71cff9ff32df1c53c31201e1cbf371fhx dc->npoints = 1;
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx}
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void
c533a883a71cff9ff32df1c53c31201e1cbf371fhxfit_and_split_calligraphics(SPDynaDrawContext *dc, gboolean release)
c533a883a71cff9ff32df1c53c31201e1cbf371fhx{
c533a883a71cff9ff32df1c53c31201e1cbf371fhx double const tolerance_sq = square( NR::expansion(SP_EVENT_CONTEXT(dc)->desktop->w2d()) * TOLERANCE_CALLIGRAPHIC );
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#ifdef DYNA_DRAW_VERBOSE
c533a883a71cff9ff32df1c53c31201e1cbf371fhx g_print("[F&S:R=%c]", release?'T':'F');
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#endif
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (!( dc->npoints > 0 && dc->npoints < SAMPLING_SIZE ))
c533a883a71cff9ff32df1c53c31201e1cbf371fhx return; // just clicked
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if ( dc->npoints == SAMPLING_SIZE - 1 || release ) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#define BEZIER_SIZE 4
d62cb7fff63265824a8e6725a3bcef698f6134c9hx#define BEZIER_MAX_BEZIERS 8
d62cb7fff63265824a8e6725a3bcef698f6134c9hx#define BEZIER_MAX_LENGTH ( BEZIER_SIZE * BEZIER_MAX_BEZIERS )
d62cb7fff63265824a8e6725a3bcef698f6134c9hx
d62cb7fff63265824a8e6725a3bcef698f6134c9hx#ifdef DYNA_DRAW_VERBOSE
d62cb7fff63265824a8e6725a3bcef698f6134c9hx g_print("[F&S:#] dc->npoints:%d, release:%s\n",
d62cb7fff63265824a8e6725a3bcef698f6134c9hx dc->npoints, release ? "TRUE" : "FALSE");
d62cb7fff63265824a8e6725a3bcef698f6134c9hx#endif
d40f4da491abdcae192eb797766f5f44772a832fpengcheng chen - Sun Microsystems - Beijing China
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"); */
d62cb7fff63265824a8e6725a3bcef698f6134c9hx sp_curve_reset(dc->cal1);
d62cb7fff63265824a8e6725a3bcef698f6134c9hx sp_curve_reset(dc->cal2);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
d62cb7fff63265824a8e6725a3bcef698f6134c9hx sp_curve_moveto(dc->cal1, dc->point1[0]);
43439c96b8398c01c375889c79bed72d78fb4c39hx sp_curve_moveto(dc->cal2, dc->point2[0]);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx NR::Point b1[BEZIER_MAX_LENGTH];
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)) );
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China
c533a883a71cff9ff32df1c53c31201e1cbf371fhx NR::Point b2[BEZIER_MAX_LENGTH];
c533a883a71cff9ff32df1c53c31201e1cbf371fhx gint const nb2 = sp_bezier_fit_cubic_r(b2, dc->point2, dc->npoints,
c533a883a71cff9ff32df1c53c31201e1cbf371fhx tolerance_sq, BEZIER_MAX_BEZIERS);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx g_assert( nb2 * BEZIER_SIZE <= gint(G_N_ELEMENTS(b2)) );
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if ( nb1 != -1 && nb2 != -1 ) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* Fit and draw and reset state */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#ifdef DYNA_DRAW_VERBOSE
c533a883a71cff9ff32df1c53c31201e1cbf371fhx g_print("nb1:%d nb2:%d\n", nb1, nb2);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#endif
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* CanvasShape */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx if (! release) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_curve_reset(dc->currentcurve);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_curve_moveto(dc->currentcurve, b1[0]);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx for (NR::Point *bp1 = b1; bp1 < b1 + BEZIER_SIZE * nb1; bp1 += BEZIER_SIZE) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_curve_curveto(dc->currentcurve, bp1[1],
c533a883a71cff9ff32df1c53c31201e1cbf371fhx bp1[2], bp1[3]);
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_curve_lineto(dc->currentcurve,
c533a883a71cff9ff32df1c53c31201e1cbf371fhx b2[BEZIER_SIZE*(nb2-1) + 3]);
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 }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_curve_closepath(dc->currentcurve);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->currentshape), dc->currentcurve);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
cdc64593cc1046229f4ac4daf5ead688b5efe6ebxinghua wen - Sun Microsystems - Beijing China
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 }
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 }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx } else {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx /* fixme: ??? */
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#ifdef DYNA_DRAW_VERBOSE
c533a883a71cff9ff32df1c53c31201e1cbf371fhx g_print("[fit_and_split_calligraphics] failed to fit-cubic.\n");
c533a883a71cff9ff32df1c53c31201e1cbf371fhx#endif
c533a883a71cff9ff32df1c53c31201e1cbf371fhx draw_temporary_box(dc);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx for (gint i = 1; i < dc->npoints; i++) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_curve_lineto(dc->cal1, dc->point1[i]);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx for (gint i = 1; i < dc->npoints; i++) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_curve_lineto(dc->cal2, dc->point2[i]);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
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#endif
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
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 NULL);
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
d2a61391af480de12cf4264080d7254a6de96e2apengcheng chen - Sun Microsystems - Beijing China dc->segments = g_slist_prepend(dc->segments, cbp);
d2a61391af480de12cf4264080d7254a6de96e2apengcheng chen - Sun Microsystems - Beijing China }
d2a61391af480de12cf4264080d7254a6de96e2apengcheng chen - Sun Microsystems - Beijing China
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 } else {
d2a61391af480de12cf4264080d7254a6de96e2apengcheng chen - Sun Microsystems - Beijing China draw_temporary_box(dc);
d2a61391af480de12cf4264080d7254a6de96e2apengcheng chen - Sun Microsystems - Beijing China }
d2a61391af480de12cf4264080d7254a6de96e2apengcheng chen - Sun Microsystems - Beijing China}
d2a61391af480de12cf4264080d7254a6de96e2apengcheng chen - Sun Microsystems - Beijing China
c533a883a71cff9ff32df1c53c31201e1cbf371fhxstatic void
c533a883a71cff9ff32df1c53c31201e1cbf371fhxdraw_temporary_box(SPDynaDrawContext *dc)
c533a883a71cff9ff32df1c53c31201e1cbf371fhx{
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_curve_reset(dc->currentcurve);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_curve_moveto(dc->currentcurve, dc->point1[0]);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx for (gint i = 1; i < dc->npoints; i++) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_curve_lineto(dc->currentcurve, dc->point1[i]);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx for (gint i = dc->npoints-1; i >= 0; i--) {
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_curve_lineto(dc->currentcurve, dc->point2[i]);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx }
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_curve_closepath(dc->currentcurve);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->currentshape), dc->currentcurve);
c533a883a71cff9ff32df1c53c31201e1cbf371fhx}
c533a883a71cff9ff32df1c53c31201e1cbf371fhx
c533a883a71cff9ff32df1c53c31201e1cbf371fhx/*
c533a883a71cff9ff32df1c53c31201e1cbf371fhx Local Variables:
c533a883a71cff9ff32df1c53c31201e1cbf371fhx mode:c++
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 End:
c533a883a71cff9ff32df1c53c31201e1cbf371fhx*/
c533a883a71cff9ff32df1c53c31201e1cbf371fhx// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
c533a883a71cff9ff32df1c53c31201e1cbf371fhx