Debian Codegraph @

fontforgeexe/sfundo.c

raw symbols SCIP
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 <fontforge-config.h>
30
31 #include "sfundo.h"
32
33 #include "fvfonts.h"
34 #include "gfile.h"
35 #include "sfd.h"
36 #include "uiinterface.h"
37 #include "views.h"
38
39 #include <string.h>
40
41 void SFUndoFreeAssociated( struct sfundoes *undo )
42 {
43 if( undo->sfdchunk )
44 free( undo->sfdchunk );
45 }
46
47 void SFUndoFree( struct sfundoes *undo )
48 {
49 SFUndoFreeAssociated( undo );
50 free( undo );
51 }
52
53 void SFUndoRemoveAndFree( SplineFont *sf, struct sfundoes *undo )
54 {
55 SFUndoFreeAssociated( undo );
56 dlist_erase( (struct dlistnode **)&sf->undoes, (struct dlistnode *)undo );
57 free(undo);
58 }
59
60
61 SFUndoes* SFUndoCreateSFD( enum sfundotype t, char* staticmsg, char* sfdfrag )
62 {
63 SFUndoes* undo = chunkalloc(sizeof(SFUndoes));
64 undo->ln.next = 0;
65 undo->ln.prev = 0;
66 undo->msg = staticmsg;
67 undo->type = t;
68 undo->sfdchunk = sfdfrag;
69 return undo;
70 }
71
72 char* SFUndoToString( SFUndoes* undo )
73 {
74 FILE* sfd;
75 if( (sfd = GFileTmpfile()) )
76 {
77 fprintf(sfd,"BeginFontLevelUndo\n");
78 fprintf(sfd,"FontLevelUndoType:%d\n",undo->type);
79 fprintf(sfd,"FontLevelUndoMessage:%s\n",undo->msg);
80 if( undo->sfdchunk )
81 fprintf(sfd,"%s\n", undo->sfdchunk );
82 fprintf(sfd,"EndFontLevelUndo\n");
83
84 char* str = FileToAllocatedString( sfd );
85 fclose( sfd );
86 return str;
87 }
88 return 0;
89 }
90
91 static char* findterm( char** str, char* term )
92 {
93 char* p;
94 if( (p = strstr( *str, term )) )
95 {
96 p += strlen( term );
97 char* e = p;
98 while( *e && *e!='\n')
99 e++;
100 if( *e )
101 {
102 *e = '\0';
103 (*str) = e+1;
104 return p;
105 }
106 }
107 return NULL;
108 }
109
110 SFUndoes* SFUndoFromString( char* str )
111 {
112 enum sfundotype t = sfut_fontinfo;
113 char* staticmsg = "fixme";
114 char* sfdfrag = str;
115
116 if( !strncmp( str, "BeginFontLevelUndo", strlen("BeginFontLevelUndo")))
117 {
118 char* p;
119 if( (p = findterm( &str, "FontLevelUndoType:" )) )
120 t = atoi(p);
121 if( (p = findterm( &str, "FontLevelUndoMessage:" )) )
122 staticmsg = p;
123 }
124
125 SFUndoes* ret = SFUndoCreateSFD( t, staticmsg, sfdfrag );
126 return ret;
127 }
128
129 SFUndoes* SFUndoCreateRedo( SFUndoes* undo, SplineFont* sf )
130 {
131 SFUndoes* ret = 0;
132 char* sfdfrag = 0;
133
134 switch(undo->type) {
135 case sfut_fontinfo:
136
137 sfdfrag = DumpSplineFontMetadata( sf );
138 ret = SFUndoCreateSFD( sfut_fontinfo,
139 _("Font Information Dialog"),
140 sfdfrag );
141
142 break;
143 case sfut_none: case sfut_lookups: case sfut_lookups_kerns: case sfut_noop: break;
144 }
145
146 return ret;
147 }
148
149 void SFUndoPerform( SFUndoes* undo, SplineFont* sf )
150 {
151 char* sfdchunk = 0;
152 FILE* sfd = 0;
153
154 switch(undo->type) {
155 case sfut_fontinfo:
156 sfdchunk = undo->sfdchunk;
157 // printf("font level undo, font info sfd:%s\n", sfdchunk );
158 sfd = GFileTmpfile();
159 fwrite( sfdchunk, strlen(sfdchunk), 1, sfd );
160 fseek( sfd, 0, SEEK_SET );
161
162 SFD_GetFontMetaDataData d;
163 SFD_GetFontMetaDataData_Init( &d );
164 visitSFDFragment( sfd, sf, SFD_GetFontMetaDataVoid, &d );
165 break;
166 case sfut_lookups_kerns:
167 case sfut_lookups:
168 sfdchunk = undo->sfdchunk;
169 if( !sfdchunk ) {
170 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."));
171 SFUndoRemoveAndFree( sf, undo );
172 return;
173 }
174
175 // Roll it on back!
176 sfd = GFileTmpfile();
177 fwrite( sfdchunk, strlen(sfdchunk), 1, sfd );
178 fseek( sfd, 0, SEEK_SET );
179
180 while( 1 ) {
181 char* name = SFDMoveToNextStartChar( sfd );
182 if( !name )
183 break;
184
185 int unienc = 0;
186 SplineChar *sc;
187 sc = SFGetChar( sf, unienc, name );
188 if( !sc ) {
189 ff_post_error(_("Bad undo"),_("couldn't find the character %s"), name );
190 break;
191 }
192 if( sc ) {
193 // Free the stuff in sc->psts so we don't leak it.
194 if( undo->type == sfut_lookups ) {
195 PSTFree(sc->possub);
196 sc->possub = 0;
197 }
198 char tok[2000];
199 getname( sfd, tok );
200 SFDGetPSTs( sfd, sc, tok );
201 SFDGetKerns( sfd, sc, tok );
202 }
203 free(name);
204 }
205
206 if( undo->type == sfut_lookups_kerns ) {
207 SFDFixupRefs( sf );
208 }
209 break;
210 default:
211 break;
212 }
213 }
214
215 void SFUndoPushFront( struct sfundoes ** undoes, SFUndoes* undo )
216 {
217 dlist_pushfront( (struct dlistnode **)undoes, (struct dlistnode *)undo );
218 }