1 /*
2  *   Copyright (C) 2005, 2006, 2007, 2009, 2010, 2011 Free Software Foundation, Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16  *
17  *
18  */
19 
20 /*
21  * Uses both "embedded" font and device fonts.
22  * The text written is 'Hello world' in both cases.
23  * Text at the bottom is the one with embedded fonts.
24  *
25  * TODO: add a testrunner for pixel checking.
26  * TODO: test autoSize and wordWrap interaction (what takes precedence?)
27  *
28  * run as ./DefineEditTextTest
29  */
30 
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <ming.h>
34 
35 #include "ming_utils.h"
36 
37 #define OUTPUT_VERSION 8
38 #define OUTPUT_FILENAME "TextSnapshotTest.swf"
39 
40 SWFDisplayItem
41 add_text_field(SWFMovie mo, SWFFont font, const char* text);
42 
43 SWFDisplayItem
add_text_field(SWFMovie mo,SWFFont font,const char * text)44 add_text_field(SWFMovie mo, SWFFont font, const char* text)
45 {
46   SWFText tf;
47 
48   tf = newSWFText();
49 
50   SWFText_setFont(tf, font);
51 
52 
53   SWFText_addString(tf, text, NULL);
54 
55   return SWFMovie_add(mo, (SWFBlock)tf);
56 }
57 
58 int
main(int argc,char ** argv)59 main(int argc, char** argv)
60 {
61   SWFMovie mo;
62   const char *srcdir=".";
63   char fdbfont[256];
64   SWFMovieClip  dejagnuclip;
65 
66   /*********************************************
67    *
68    * Initialization
69    *
70    *********************************************/
71 
72   if ( argc>1 ) srcdir=argv[1];
73   else
74   {
75     fprintf(stderr, "Usage: %s <mediadir>\n", argv[0]);
76     return 1;
77   }
78 
79   sprintf(fdbfont, "%s/Bitstream-Vera-Sans.fdb", srcdir);
80 
81   puts("Setting things up");
82 
83   Ming_init();
84   Ming_useSWFVersion (OUTPUT_VERSION);
85   //Ming_setScale(20.0); /* so we talk twips */
86 
87   mo = newSWFMovie();
88   SWFMovie_setRate(mo, 1.0);
89   SWFMovie_setDimension(mo, 800, 600);
90 
91   dejagnuclip = get_dejagnu_clip((SWFBlock)get_default_font(srcdir), 10, 0, 0, 800, 600);
92   SWFMovie_add(mo, (SWFBlock)dejagnuclip);
93   SWFMovie_nextFrame(mo); // 1st frame
94 
95 
96   /*********************************************
97    *
98    * Add some textfields
99    *
100    *********************************************/
101   {
102     SWFDisplayItem it;
103     FILE *font_file = fopen(fdbfont, "r");
104     if ( font_file == NULL )
105     {
106       perror(fdbfont);
107       exit(1);
108     }
109     SWFFont efont = loadSWFFontFromFile(font_file);
110 
111     it = add_text_field(mo, efont, "First text");
112     SWFDisplayItem_setName(it, "dtext1");
113     SWFDisplayItem_moveTo(it, 0, 200);
114     it = add_text_field(mo, efont, "Zweites Textfeld");
115     SWFDisplayItem_setName(it, "etext1");
116     SWFDisplayItem_moveTo(it, 0, 300);
117 
118     it = add_text_field(mo, efont, "Some more static text here... abcdefgh");
119     SWFDisplayItem_setName(it, "dtext2");
120     SWFDisplayItem_moveTo(it, 0, 400);
121   }
122   SWFMovie_nextFrame(mo);
123 
124   add_actions(mo, "createTextField('dynamictext1', 99, 10, 10, 10, 10);"
125 		  "this.dynamictext1.text = 'Dynamic Text';");
126 
127   add_actions(mo, "ts = this.getTextSnapshot();");
128   check(mo, "ts instanceof TextSnapshot");
129   check_equals(mo, "ts.getCount()", "64");
130 
131   check_equals(mo, "ts.getText(0, 1)", "'F'");
132   check_equals(mo, "ts.getText(3, 3)", "'s'");
133   check_equals(mo, "ts.getText(-5, 5)", "'First'");
134   check_equals(mo, "ts.getText(10, 6)", "'Z'");
135   check_equals(mo, "ts.getText(0, 100)", "'First textZweites TextfeldSome more "
136 		  "static text here... abcdefgh'");
137   add_actions(mo, "ss = ts.getText(100, 110);");
138   check_equals(mo, "typeof(ss)", "'string'");
139   check_equals(mo, "ss", "'h'");
140   check_equals(mo, "ss.length", "1");
141 
142   check_equals(mo, "ts.getText(0, 100, true)",
143           "'First text\nZweites Textfeld\nSome more "
144 		  "static text here... abcdefgh'");
145 
146   check_equals(mo, "ts.getText(0, 14, true)", "'First text\nZwei'");
147 
148   check_equals(mo, "ts.findText(0, '', false)", "-1");
149   check_equals(mo, "ts.findText(0, 'f', false)", "0");
150   check_equals(mo, "ts.findText(0, 'f', true)", "22");
151   check_equals(mo, "ts.findText(1, 'Rst', false)", "2");
152   check_equals(mo, "ts.findText(3, 'RSt', false)", "-1");
153   check_equals(mo, "ts.findText(100, 'h', false)", "-1");
154   check_equals(mo, "ts.findText(64, 'h', false)", "-1");
155   check_equals(mo, "ts.findText(-5, 'Zwei', true)", "-1");
156   check_equals(mo, "ts.findText(-5, 'gh', true)", "-1");
157 
158   add_actions(mo, "ts.setSelected(5, 30, true);");
159   check_equals(mo, "ts.getSelectedText()", "' textZweites TextfeldSome'");
160   check_equals(mo, "ts.getSelectedText(true)", "' text\nZweites "
161           "Textfeld\nSome'");
162   check_equals(mo, "ts.getSelectedText(false)", "' textZweites TextfeldSome'");
163   check_equals(mo, "ts.getSelected(0, 4)", "false");
164   check_equals(mo, "ts.getSelected(1, 9)", "true");
165   check_equals(mo, "ts.getSelected(-4, 10)", "true");
166   check_equals(mo, "ts.getSelected(-4, 6)", "true");
167   check_equals(mo, "ts.getSelected(28, 23)", "true");
168   check_equals(mo, "ts.getSelected(20, 20)", "true");
169   check_equals(mo, "ts.getSelected(20, 27)", "true");
170   check_equals(mo, "ts.getSelected(-3, -1)", "false");
171   check_equals(mo, "ts.getSelected(30, 31)", "false");
172   check_equals(mo, "ts.getSelected(0, 5)", "false");
173   check_equals(mo, "ts.getSelected(40, 45)", "false");
174   check_equals(mo, "ts.getSelected(31, 34)", "false");
175 
176   check_equals(mo, "ts.getSelected(0)", "undefined");
177   check_equals(mo, "ts.getSelected(1)", "undefined");
178   check_equals(mo, "ts.getSelected(-4)", "undefined");
179   check_equals(mo, "ts.getSelected(-4)", "undefined");
180   check_equals(mo, "ts.getSelected(28)", "undefined");
181   check_equals(mo, "ts.getSelected(20)", "undefined");
182 
183   // Selected text is stored in the textfield and reset when a new
184   // snapshot is taken.
185   add_actions(mo, "ts2 = new TextSnapshot(this);");
186   check_equals(mo, "ts.getSelectedText(false)", "''");
187   check_equals(mo, "ts2.getCount()", "64");
188   check_equals(mo, "ts2.getSelectedText()", "''");
189   add_actions(mo, "ts2 = this.getTextSnapshot();");
190   check_equals(mo, "ts2.getCount()", "64");
191   check_equals(mo, "ts2.getSelectedText()", "''");
192 
193   add_actions(mo, "ts2.setSelected(3, 10, true);");
194   check_equals(mo, "ts2.getSelectedText(false).length", "7");
195   check_equals(mo, "ts.getSelectedText(false).length", "7");
196 
197   add_actions(mo, "ts.setSelectedColor(0xffff00);");
198   add_actions(mo, "ts2.setSelectedColor(0x0000ff);");
199 
200   check_equals(mo, "ts.getSelectedText(false)", "'st text'");
201   add_actions(mo, "ri = ts.getTextRunInfo(4, 10);");
202   check_equals(mo, "typeof(ri)", "'object'");
203   check(mo, "ri instanceof Array");
204   check_equals(mo, "ri.length", "7");
205 
206   add_actions(mo, "el = ri[1];");
207   check_equals(mo, "typeof(el)", "'object'");
208   check(mo, "!el.hasOwnProperty('indexInRun')");
209   check_equals(mo, "el.indexInRun", "5");
210   check_equals(mo, "el.selected", "true");
211   check_equals(mo, "el.font", "'Bitstream Vera Sans'");
212   check_equals(mo, "el.color", "0");
213   check_equals(mo, "el.height", "12");
214   check_equals(mo, "el.matrix_a", "1");
215   check_equals(mo, "el.matrix_b", "0");
216   check_equals(mo, "el.matrix_c", "0");
217   check_equals(mo, "el.matrix_d", "1");
218   check_equals(mo, "el.matrix_tx", "25.95");
219   check_equals(mo, "el.matrix_ty", "200");
220   xcheck_equals(mo, "el.corner0x", "25.95");
221   xcheck_equals(mo, "el.corner0y", "202.8");
222   xcheck_equals(mo, "el.corner1x", "29.75");
223   xcheck_equals(mo, "el.corner1y", "202.8");
224   xcheck_equals(mo, "el.corner2x", "29.75");
225   xcheck_equals(mo, "el.corner2y", "188.85");
226   xcheck_equals(mo, "el.corner3x", "25.95");
227   xcheck_equals(mo, "el.corner3y", "188.85");
228 
229   // Check properties individually
230   check_equals(mo, "ri[2].height", "12");
231   check_equals(mo, "ri[3].height", "12");
232   check_equals(mo, "ri[4].height", "12");
233   check_equals(mo, "ri[5].height", "12");
234   check_equals(mo, "ri[6].height", "12");
235 
236   check_equals(mo, "ri[2].selected", "true");
237   check_equals(mo, "ri[3].selected", "true");
238   check_equals(mo, "ri[4].selected", "true");
239   check_equals(mo, "ri[5].selected", "true");
240   check_equals(mo, "ri[6].selected", "false");
241 
242   check_equals(mo, "ri[2].matrix_tx", "29.75");
243   check_equals(mo, "ri[2].matrix_ty", "200");
244   check_equals(mo, "ri[3].matrix_tx", "34.4");
245   check_equals(mo, "ri[3].matrix_ty", "200");
246   check_equals(mo, "ri[4].matrix_tx", "41.75");
247   check_equals(mo, "ri[4].matrix_ty", "200");
248 
249   xcheck_equals(mo, "ri[2].corner0x", "29.75");
250   xcheck_equals(mo, "ri[2].corner0y", "202.8");
251   xcheck_equals(mo, "ri[3].corner0x", "34.4");
252   xcheck_equals(mo, "ri[3].corner0y", "202.8");
253   xcheck_equals(mo, "ri[4].corner0x", "41.75");
254   xcheck_equals(mo, "ri[4].corner0y", "202.8");
255 
256   xcheck_equals(mo, "ri[2].corner2y", "188.85");
257   xcheck_equals(mo, "ri[3].corner2y", "188.85");
258   xcheck_equals(mo, "ri[4].corner2y", "188.85");
259 
260   add_actions(mo, "ts.setSelected(0, 10, true);");
261   add_actions(mo, "ts.setSelected(15, 20, false);");
262   check_equals(mo, "ts2.getSelectedText().length", "10");
263 
264   add_actions(mo, "ri2 = ts.getTextRunInfo(0, 100);");
265 
266   check_equals(mo, "ri2[0].selected", "true");
267   check_equals(mo, "ri2[1].selected", "true");
268   check_equals(mo, "ri2[2].selected", "true");
269   check_equals(mo, "ri2[3].selected", "true");
270   check_equals(mo, "ri2[4].selected", "true");
271   check_equals(mo, "ri2[5].selected", "true");
272   check_equals(mo, "ri2[6].selected", "true");
273   check_equals(mo, "ri2[15].selected", "false");
274   check_equals(mo, "ri2[16].selected", "false");
275   check_equals(mo, "ri2[17].selected", "false");
276   check_equals(mo, "ri2[18].selected", "false");
277 
278   xcheck_equals(mo, "ri2[50].corner2y", "388.85");
279   xcheck_equals(mo, "ri2[50].corner2x", "156.6");
280   xcheck_equals(mo, "ri2[51].corner2y", "388.85");
281   xcheck_equals(mo, "ri2[51].corner2x", "163.95");
282 
283   check_equals(mo, "ri2[50].matrix_tx", "151.65");
284   check_equals(mo, "ri2[51].matrix_tx", "156.55");
285 
286   add_actions(mo, "ts = this.getTextSnapshot();");
287   check_equals(mo, "typeof(ts)", "'object'");
288   add_actions(mo, "backup = TextSnapshot;");
289   add_actions(mo, "TextSnapshot = undefined;");
290   add_actions(mo, "t = new TextSnapshot();");
291   check_equals(mo, "typeof(t)", "'undefined'");
292 
293   check_equals(mo, "typeof(TextSnapshot)", "'undefined'");
294   add_actions(mo, "ts = this.getTextSnapshot();");
295   xcheck_equals(mo, "typeof(ts)", "'undefined'");
296   add_actions(mo, "TextSnapshot = backup;");
297   add_actions(mo, "ts = this.getTextSnapshot();");
298   check_equals(mo, "typeof(ts)", "'object'");
299 
300   add_actions(mo, "backup = TextSnapshot.prototype;");
301   add_actions(mo, "TextSnapshot.prototype = undefined;");
302   add_actions(mo, "ts = this.getTextSnapshot();");
303   check_equals(mo, "typeof(ts)", "'object'");
304   add_actions(mo, "TextSnapshot.prototype = backup;");
305   add_actions(mo, "ts = this.getTextSnapshot();");
306   check_equals(mo, "typeof(ts)", "'object'");
307 
308   add_actions(mo, "totals(); stop();");
309 
310   SWFMovie_nextFrame(mo);
311 
312   /*****************************************************
313    *
314    * Output movie
315    *
316    *****************************************************/
317   puts("Saving " OUTPUT_FILENAME );
318 
319   SWFMovie_save(mo, OUTPUT_FILENAME);
320 
321   return 0;
322 }
323