Line data Source code
1 : /******************************************************************************
2 : *******************************************************************************
3 : *******************************************************************************
4 :
5 : Copyright (C) 2013 Ben Martin
6 :
7 : This file is part of FontForge.
8 :
9 : FontForge is free software: you can redistribute it and/or modify
10 : it under the terms of the GNU General Public License as published by
11 : the Free Software Foundation,either version 3 of the License, or
12 : (at your option) any later version.
13 :
14 : FontForge is distributed in the hope that it will be useful,
15 : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : GNU General Public License for more details.
18 :
19 : You should have received a copy of the GNU General Public License
20 : along with FontForge. If not, see <http://www.gnu.org/licenses/>.
21 :
22 : For more details see the COPYING.gplv3 file in the root directory of this
23 : distribution.
24 :
25 : *******************************************************************************
26 : *******************************************************************************
27 : ******************************************************************************/
28 :
29 : #include "sfundo.h"
30 :
31 : #include "fvfonts.h"
32 : #include "sfd.h"
33 : #include "views.h"
34 : #include <string.h>
35 : #include "uiinterface.h"
36 :
37 0 : void SFUndoFreeAssociated( struct sfundoes *undo )
38 : {
39 0 : if( undo->sfdchunk )
40 0 : free( undo->sfdchunk );
41 0 : }
42 :
43 0 : void SFUndoFree( struct sfundoes *undo )
44 : {
45 0 : SFUndoFreeAssociated( undo );
46 0 : free( undo );
47 0 : }
48 :
49 0 : void SFUndoRemoveAndFree( SplineFont *sf, struct sfundoes *undo )
50 : {
51 0 : SFUndoFreeAssociated( undo );
52 0 : dlist_erase( (struct dlistnode **)&sf->undoes, (struct dlistnode *)undo );
53 0 : free(undo);
54 0 : }
55 :
56 :
57 0 : SFUndoes* SFUndoCreateSFD( enum sfundotype t, char* staticmsg, char* sfdfrag )
58 : {
59 0 : SFUndoes* undo = chunkalloc(sizeof(SFUndoes));
60 0 : undo->ln.next = 0;
61 0 : undo->ln.prev = 0;
62 0 : undo->msg = staticmsg;
63 0 : undo->type = t;
64 0 : undo->sfdchunk = sfdfrag;
65 0 : return undo;
66 : }
67 :
68 0 : char* SFUndoToString( SFUndoes* undo )
69 : {
70 : FILE* sfd;
71 0 : if( (sfd = MakeTemporaryFile()) )
72 : {
73 0 : fprintf(sfd,"BeginFontLevelUndo\n");
74 0 : fprintf(sfd,"FontLevelUndoType:%d\n",undo->type);
75 0 : fprintf(sfd,"FontLevelUndoMessage:%s\n",undo->msg);
76 0 : if( undo->sfdchunk )
77 0 : fprintf(sfd,"%s\n", undo->sfdchunk );
78 0 : fprintf(sfd,"EndFontLevelUndo\n");
79 :
80 0 : char* str = FileToAllocatedString( sfd );
81 0 : fclose( sfd );
82 0 : return str;
83 : }
84 0 : return 0;
85 : }
86 :
87 0 : static char* findterm( char** str, char* term )
88 : {
89 : char* p;
90 0 : if( (p = strstr( *str, term )) )
91 : {
92 0 : p += strlen( term );
93 0 : char* e = p;
94 0 : while( *e && *e!='\n')
95 0 : e++;
96 0 : if( *e )
97 : {
98 0 : *e = '\0';
99 0 : (*str) = e+1;
100 0 : return p;
101 : }
102 : }
103 0 : return NULL;
104 : }
105 :
106 0 : SFUndoes* SFUndoFromString( char* str )
107 : {
108 0 : enum sfundotype t = sfut_fontinfo;
109 0 : char* staticmsg = "fixme";
110 0 : char* sfdfrag = str;
111 :
112 0 : if( !strncmp( str, "BeginFontLevelUndo", strlen("BeginFontLevelUndo")))
113 : {
114 : char* p;
115 0 : if( (p = findterm( &str, "FontLevelUndoType:" )) )
116 0 : t = atoi(p);
117 0 : if( (p = findterm( &str, "FontLevelUndoMessage:" )) )
118 0 : staticmsg = p;
119 : }
120 :
121 0 : SFUndoes* ret = SFUndoCreateSFD( t, staticmsg, sfdfrag );
122 0 : return ret;
123 : }
124 :
125 0 : SFUndoes* SFUndoCreateRedo( SFUndoes* undo, SplineFont* sf )
126 : {
127 0 : SFUndoes* ret = 0;
128 0 : char* sfdfrag = 0;
129 :
130 0 : switch(undo->type) {
131 : case sfut_fontinfo:
132 :
133 0 : sfdfrag = DumpSplineFontMetadata( sf );
134 0 : ret = SFUndoCreateSFD( sfut_fontinfo,
135 : _("Font Information Dialog"),
136 : sfdfrag );
137 :
138 0 : break;
139 : }
140 :
141 0 : return ret;
142 : }
143 :
144 0 : void SFUndoPerform( SFUndoes* undo, SplineFont* sf )
145 : {
146 0 : char* sfdchunk = 0;
147 0 : FILE* sfd = 0;
148 :
149 0 : switch(undo->type) {
150 : case sfut_fontinfo:
151 0 : sfdchunk = undo->sfdchunk;
152 0 : printf("font level undo, font info sfd:%s\n", sfdchunk );
153 0 : sfd = MakeTemporaryFile();
154 0 : fwrite( sfdchunk, strlen(sfdchunk), 1, sfd );
155 0 : fseek( sfd, 0, SEEK_SET );
156 :
157 : SFD_GetFontMetaDataData d;
158 0 : SFD_GetFontMetaDataData_Init( &d );
159 0 : visitSFDFragment( sfd, sf, SFD_GetFontMetaDataVoid, &d );
160 0 : break;
161 : case sfut_lookups_kerns:
162 : case sfut_lookups:
163 0 : sfdchunk = undo->sfdchunk;
164 0 : if( !sfdchunk ) {
165 0 : ff_post_error(_("Undo information incomplete"),_("There is a splinefont level undo, but it does not contain any information to perform the undo. This is an application error, please report what you last did to the lookup tables so the developers can try to reproduce the issue and fix it."));
166 0 : SFUndoRemoveAndFree( sf, undo );
167 0 : return;
168 : }
169 :
170 : // Roll it on back!
171 0 : sfd = MakeTemporaryFile();
172 0 : fwrite( sfdchunk, strlen(sfdchunk), 1, sfd );
173 0 : fseek( sfd, 0, SEEK_SET );
174 :
175 : while( 1 ) {
176 0 : char* name = SFDMoveToNextStartChar( sfd );
177 0 : if( !name )
178 0 : break;
179 :
180 0 : int unienc = 0;
181 : SplineChar *sc;
182 0 : sc = SFGetChar( sf, unienc, name );
183 0 : if( !sc ) {
184 0 : ff_post_error(_("Bad undo"),_("couldn't find the character %s"), name );
185 0 : break;
186 : }
187 0 : if( sc ) {
188 : // Free the stuff in sc->psts so we don't leak it.
189 0 : if( undo->type == sfut_lookups ) {
190 0 : PSTFree(sc->possub);
191 0 : sc->possub = 0;
192 : }
193 : char tok[2000];
194 0 : getname( sfd, tok );
195 0 : SFDGetPSTs( sfd, sc, tok );
196 0 : SFDGetKerns( sfd, sc, tok );
197 : }
198 0 : free(name);
199 0 : }
200 :
201 0 : if( undo->type == sfut_lookups_kerns ) {
202 0 : SFDFixupRefs( sf );
203 : }
204 0 : break;
205 : default:
206 0 : break;
207 : }
208 : }
209 :
210 0 : void SFUndoPushFront( struct sfundoes ** undoes, SFUndoes* undo )
211 : {
212 0 : dlist_pushfront( (struct dlistnode **)undoes, (struct dlistnode *)undo );
213 0 : }
|