Debian Codegraph @

fontforgeexe/cvtranstools.c

raw symbols SCIP
1 /* Copyright (C) 2000-2012 by George Williams */
2 /*
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <fontforge-config.h>
29
30 #include "cvundoes.h"
31 #include "fontforgeui.h"
32 #include "nonlineartrans.h"
33 #include <math.h>
34
35 /* Cache the fixed point and original drag vector for the scale gesture. */
36 static void ScaleSetOppositeOrigin(CharView *cv) {
37 BasePoint center;
38
39 CVFindCenter(cv, &center, !CVAnySel(cv,NULL,NULL,NULL,NULL));
40 cv->expandorigin.x = 2*center.x - cv->p.cx;
41 cv->expandorigin.y = 2*center.y - cv->p.cy;
42 cv->expandwidth = cv->p.cx - cv->expandorigin.x;
43 cv->expandheight = cv->p.cy - cv->expandorigin.y;
44 }
45
46 void CVMouseDownTransform(CharView *cv) {
47 CVPreserveTState(cv);
48 if ( cv->active_tool==cvt_scale )
49 ScaleSetOppositeOrigin(cv);
50 }
51
52 void CVMouseMoveTransform(CharView *cv) {
53 CharViewTab* tab = CVGetActiveTab(cv);
54 real transform[6]; // PostScript-style 6-value transform
55
56 CVRestoreTOriginalState(cv);
57 if ( cv->info.x != cv->p.cx || cv->info.y != cv->p.cy ) {
58 transform[0] = transform[3] = 1;
59 transform[1] = transform[2] = 0;
60 switch ( cv->active_tool ) {
61 case cvt_rotate: {
62 real angle = atan2(cv->info.y-cv->p.cy,cv->info.x-cv->p.cx);
63 transform[0] = transform[3] = cos(angle);
64 transform[2] = -(transform[1] = sin(angle));
65 } break;
66 case cvt_flip: {
67 real dx,dy;
68 if (( dx = cv->info.x-cv->p.cx)<0 ) dx=-dx;
69 if (( dy = cv->info.y-cv->p.cy)<0 ) dy=-dy;
70 if ( dy>2*dx )
71 transform[0] = -1;
72 else if ( dx>2*dy )
73 transform[3] = -1;
74 else if ( (cv->info.x-cv->p.cx)*(cv->info.y-cv->p.cy)>0 ) {
75 transform[0] = transform[3] = 0;
76 transform[1] = transform[2] = -1;
77 } else {
78 transform[0] = transform[3] = 0;
79 transform[1] = transform[2] = 1;
80 }
81 } break;
82 case cvt_scale: {
83 /* Scale from cached opposite side/corner so dragged point tracks the mouse. */
84 transform[0] = cv->expandwidth==0 ? 1 : (cv->info.x-cv->expandorigin.x)/cv->expandwidth;
85 transform[3] = cv->expandheight==0 ? 1 : (cv->info.y-cv->expandorigin.y)/cv->expandheight;
86
87 /* Edge handles constrain scaling to the dragged axis. */
88 if ( cv->expandedge==ee_left || cv->expandedge==ee_right )
89 transform[3] = 1; // lt/rt? lock yscale=1
90 else if ( cv->expandedge==ee_up || cv->expandedge==ee_down )
91 transform[0] = 1; // up/dn? lock xscale=1
92 } break;
93 case cvt_skew: {
94 real angle = atan2(cv->info.y-cv->p.cy,cv->info.x-cv->p.cx);
95 transform[2] = sin(angle);
96 } break;
97 case cvt_3d_rotate: {
98 real angle = atan2(cv->info.y-cv->p.cy,cv->info.x-cv->p.cx);
99 /* Allow one pixel per degree */
100 real zangle = sqrt( (cv->info.x-cv->p.cx)*(cv->info.x-cv->p.cx) +
101 (cv->info.y-cv->p.cy)*(cv->info.y-cv->p.cy) ) * tab->scale *
102 FF_PI/180;
103 real s = sin(angle), c = cos(angle);
104 real cz = cos(zangle);
105 transform[0] = c*c + s*s*cz;
106 transform[3] = s*s + c*c*cz;
107 transform[2] = transform[1] = c*s * (cz-1);
108 } break;
109 /* Perspective takes three points: origin, start point, cur point */
110 /* first rotate so that orig/start are the x axis */
111 /* then define perspective so that: */
112 /* y' = y */
113 /* x' = cur.x + (cur.y - y)/cur.y * (x - cur.x) */
114 /* then rotate back */
115 case cvt_perspective: {
116 real angle = atan2(cv->p.cy,cv->p.cx);
117 real s = sin(angle), c = cos(angle);
118 transform[0] = transform[3] = c;
119 transform[2] = -(transform[1] = -s);
120 transform[4] = transform[5] = 0;
121 CVTransFunc(cv,transform,false);
122 CVYPerspective((CharViewBase *) cv,
123 c*cv->info.x + s*cv->info.y,
124 -s*cv->info.x + c*cv->info.y);
125 transform[2] = -(transform[1] = s);
126 } break;
127 default:
128 break;
129 }
130 if ( cv->active_tool==cvt_scale ) {
131 /* Translate scaled outline back so cached origin remains fixed */
132 transform[4] = (1-transform[0])*cv->expandorigin.x;
133 transform[5] = (1-transform[3])*cv->expandorigin.y;
134 } else if ( cv->active_tool!=cvt_perspective ) {
135 /* Other transform tools: pressed point is center of the transformation */
136 transform[4] = -cv->p.cx*transform[0] -
137 cv->p.cy*transform[2] +
138 cv->p.cx;
139 transform[5] = -cv->p.cy*transform[3] -
140 cv->p.cx*transform[1] +
141 cv->p.cy;
142 }
143 CVSetCharChanged(cv,true);
144 /* Don't move 'advance width' when scale-tool 'no selection' means 'all points' */
145 CVTransFunc(cv, transform, cv->active_tool==cvt_scale && !CVAnySel(cv,NULL,NULL,NULL,NULL)
146 ? fvt_dontmovewidth : false);
147 }
148 SCUpdateAll(cv->b.sc);
149 CVGridHandlePossibleFitChar(cv);
150 }
151
152 void CVMouseUpTransform(CharView *cv) {
153 if ( cv->info.x == cv->p.cx && cv->info.y == cv->p.cy )
154 {
155 /* Nothing happened */
156 cv->needsrasterize = cv->recentchange = false;
157 CVRemoveTopUndo(&cv->b);
158 SCUpdateAll(cv->b.sc);
159 CVGridHandlePossibleFitChar(cv);
160 }
161 else
162 {
163 CVUndoCleanup(cv);
164 }
165 }