PageRenderTime 1313ms CodeModel.GetById 825ms app.highlight 278ms RepoModel.GetById 205ms app.codeStats 1ms

/src/test/canvas_test.c

http://ftk.googlecode.com/
C | 538 lines | 426 code | 83 blank | 29 comment | 34 complexity | 9e34bbeca4d6ecc8191808486d4dd062 MD5 | raw file
  1/*
  2 * File: canvas_test.c    
  3 * Author:  Li XianJing <xianjimli@hotmail.com>
  4 * Brief:   
  5 *
  6 * Copyright (c) 2009 - 2010  Li XianJing <xianjimli@hotmail.com>
  7 *
  8 * Licensed under the Academic Free License version 2.1
  9 *
 10 * This program is free software; you can redistribute it and/or modify
 11 * it under the terms of the GNU General Public License as published by
 12 * the Free Software Foundation; either version 2 of the License, or
 13 * (at your option) any later version.
 14 *
 15 * This program is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18 * GNU General Public License for more details.
 19 *
 20 * You should have received a copy of the GNU General Public License
 21 * along with this program; if not, write to the Free Software
 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 23 */
 24
 25/*
 26 * History:
 27 * ================================================================
 28 * 2009-10-03 Li XianJing <xianjimli@hotmail.com> created
 29 *
 30 */
 31#include "ftk.h"
 32#include "ftk_canvas.h"
 33
 34static void show_canvas(FtkDisplay* display, FtkCanvas* canvas)
 35{
 36	FtkBitmap* bitmap = NULL;
 37	FtkRect rect = {.x = 0, .y=0, .width=0, .height=0};
 38	rect.width = ftk_display_width(display);
 39	rect.height = ftk_display_height(display);
 40
 41	ftk_canvas_lock_buffer(canvas, &bitmap);
 42	ftk_display_update(display, bitmap, &rect, 0, 0);
 43	ftk_canvas_unlock_buffer(canvas);
 44
 45	return;
 46}
 47#if 1
 48void test_misc(FtkDisplay* display, FtkFontDesc* font)
 49{
 50	if(display != NULL)
 51	{
 52		int i = 0;
 53		FtkGc gc = {0};
 54		int extent = 0;
 55		FtkColor color = {0x0, 0, 0, 0x0};
 56		FtkRect rect = {.x = 0, .y=0, .width=0, .height=0};
 57		rect.width = ftk_display_width(display);
 58		rect.height = ftk_display_height(display);
 59		gc.mask = FTK_GC_FG | FTK_GC_FONT;
 60		gc.fg.a = 0xff;
 61		gc.fg.r = 0x00;
 62		gc.fg.g = 0x00;
 63		gc.fg.b = 0xff;
 64		gc.font = font;
 65
 66		FtkCanvas* thiz = ftk_canvas_create(rect.width, rect.height, &color);
 67		show_canvas(display, thiz);
 68		for(i = 0; i < ftk_display_height(display); i++)
 69		{
 70			if(gc.fg.r < 0xff)
 71			{
 72				gc.fg.r++;
 73			}
 74			else
 75			{
 76				gc.fg.g++;
 77			}
 78			ftk_canvas_set_gc(thiz, &gc);
 79			ftk_canvas_draw_hline(thiz, 0, i, 320);
 80		}
 81		FtkBitmap* bitmap = ftk_bitmap_create(100, 100, color);
 82		ftk_canvas_draw_bitmap_simple(thiz, bitmap, 0, 0, 100, 100, 100, 100);
 83		ftk_canvas_draw_string(thiz, 0, 240, " Jim is a Programmer.", -1, 0);
 84		gc.fg.b = 0xff;
 85		ftk_canvas_set_gc(thiz, &gc);
 86		ftk_canvas_draw_string(thiz, 0, 220, "?????????", -1, 0);
 87	
 88		unsigned int line_mask = 0xaaaaaaaa;
 89		gc.line_mask = line_mask;
 90		gc.mask = FTK_GC_LINE_MASK;
 91		ftk_canvas_set_gc(thiz, &gc);
 92		show_canvas(display, thiz);
 93
 94		extent = ftk_canvas_get_str_extent(thiz, "???", -1);
 95		printf("extent=%d\n", ftk_canvas_get_str_extent(thiz, "???", -1));
 96
 97
 98		ftk_bitmap_unref(bitmap);
 99		ftk_canvas_destroy(thiz);
100	}
101
102	sleep(3);
103
104	return;
105}
106
107#if 1
108void test_draw_point(FtkDisplay* display)
109{
110	int i = 0;
111	FtkGc gc = {.mask = FTK_GC_FG};
112	FtkPoint p = {0};
113	FtkColor color = {.a = 0xff};
114	int width = ftk_display_width(display);
115	int height = ftk_display_height(display);
116	FtkCanvas* thiz = ftk_canvas_create(width, height, &color);
117
118	color.r = 0xff;
119	color.a = 0xff;
120	gc.fg = color;
121	for(i = 0; i < width; i++)
122	{
123		p.x = i;
124		p.y = 10;
125		ftk_canvas_reset_gc(thiz, &gc);
126		ftk_canvas_draw_pixels(thiz, &p, 1);
127	}
128	
129	color.g = 0xff;
130	color.r = 0;
131	for(i = 0; i < width; i++)
132	{
133		color.a = 0xff - (0xff & i);
134		gc.fg = color;
135		p.x = i;
136		p.y = 20;
137		ftk_canvas_reset_gc(thiz, &gc);
138		ftk_canvas_draw_pixels(thiz, &p, 1);
139	}
140	
141	color.r = 0;
142	color.g = 0;
143	color.b = 0xff;
144	color.a = 0xff;
145	gc.fg = color;
146	gc.mask |= FTK_GC_ALPHA;
147	for(i = 0; i < width; i++)
148	{
149		gc.alpha = 0xff - (0xff & i);
150		ftk_canvas_reset_gc(thiz, &gc);
151		p.x = i;
152		p.y = 20;
153		ftk_canvas_draw_pixels(thiz, &p, 1);
154	}
155
156	show_canvas(display, thiz);
157	
158	ftk_canvas_destroy(thiz);
159
160	sleep(3);
161
162	return;
163}
164#endif
165void test_draw_vline(FtkDisplay* display)
166{
167	int i = 0;
168	FtkGc gc = {.mask = FTK_GC_FG};
169	FtkColor color = {.a = 0xff};
170	int width = ftk_display_width(display);
171	int height = ftk_display_height(display);
172	FtkCanvas* thiz = ftk_canvas_create(width, height, &color);
173
174	color.r = 0xff;
175	color.a = 0xff;
176	gc.fg = color;
177	for(i = 0; i < width; i++)
178	{
179		ftk_canvas_reset_gc(thiz, &gc);
180		ftk_canvas_draw_vline(thiz, i, 0, 20);
181	}
182	
183	color.g = 0xff;
184	color.r = 0;
185	for(i = 0; i < width; i++)
186	{
187		color.a = 0xff - (0xff & i);
188		gc.fg = color;
189		ftk_canvas_reset_gc(thiz, &gc);
190		ftk_canvas_draw_vline(thiz, i, 30, 20);
191	}
192	
193	color.r = 0;
194	color.g = 0;
195	color.b = 0xff;
196	color.a = 0xff;
197	gc.fg = color;
198	gc.mask |= FTK_GC_ALPHA;
199	for(i = 0; i < width; i++)
200	{
201		gc.alpha = 0xff - (0xff & i);
202		ftk_canvas_reset_gc(thiz, &gc);
203		ftk_canvas_draw_vline(thiz, i, 60, 20);
204	}
205
206	show_canvas(display, thiz);
207	
208	ftk_canvas_destroy(thiz);
209	sleep(3);
210
211	return;
212}
213
214void test_draw_hline(FtkDisplay* display)
215{
216	int i = 0;
217	FtkGc gc = {.mask = FTK_GC_FG};
218	FtkColor color = {.a = 0xff};
219	int width = ftk_display_width(display);
220	int height = ftk_display_height(display);
221	FtkCanvas* thiz = ftk_canvas_create(width, height, &color);
222
223	color.r = 0xff;
224	color.a = 0xff;
225	gc.fg = color;
226	for(i = 0; i < height; i++)
227	{
228		ftk_canvas_reset_gc(thiz, &gc);
229		ftk_canvas_draw_hline(thiz, 0, i, 20);
230	}
231	
232	color.g = 0xff;
233	color.r = 0;
234	for(i = 0; i < height; i++)
235	{
236		color.a = 0xff - (0xff & i);
237		gc.fg = color;
238		ftk_canvas_reset_gc(thiz, &gc);
239		ftk_canvas_draw_hline(thiz, 30, i, 20);
240	}
241	
242	color.r = 0;
243	color.g = 0;
244	color.b = 0xff;
245	color.a = 0xff;
246	gc.fg = color;
247	gc.mask |= FTK_GC_ALPHA;
248	for(i = 0; i < height; i++)
249	{
250		gc.alpha = 0xff - (0xff & i);
251		ftk_canvas_reset_gc(thiz, &gc);
252		ftk_canvas_draw_hline(thiz, 60, i, 20);
253	}
254
255	show_canvas(display, thiz);
256	
257	ftk_canvas_destroy(thiz);
258
259	sleep(3);
260
261	return;
262}
263
264#if 1
265void test_draw_line(FtkDisplay* display)
266{
267	int i = 0;
268	FtkGc gc = {.mask = FTK_GC_FG};
269	FtkRect rect = {0};
270	FtkColor color = {.a = 0xff};
271	int width = ftk_display_width(display);
272	int height = ftk_display_height(display);
273	FtkCanvas* thiz = ftk_canvas_create(width, height, &color);
274
275	rect.width = width;
276	rect.height = height;
277
278	color.r = 0xff;
279	color.a = 0xff;
280	gc.fg = color;
281	for(i = 0; i < height/2; i++)
282	{
283		ftk_canvas_reset_gc(thiz, &gc);
284		ftk_canvas_draw_line(thiz, 0, i, 20, i+10);
285	}
286	
287	color.g = 0xff;
288	color.r = 0;
289	for(i = 0; i < height/2; i++)
290	{
291		color.a = 0xff - (0xff & i);
292		gc.fg = color;
293		ftk_canvas_reset_gc(thiz, &gc);
294		ftk_canvas_draw_line(thiz, 30, i, 50, i+10);
295	}
296	
297	color.r = 0;
298	color.g = 0;
299	color.b = 0xff;
300	color.a = 0xff;
301	gc.fg = color;
302	gc.mask |= FTK_GC_ALPHA;
303	for(i = 0; i < height/2; i++)
304	{
305		gc.alpha = 0xff - (0xff & i);
306		ftk_canvas_reset_gc(thiz, &gc);
307		ftk_canvas_draw_line(thiz, 60, i, 80, i+10);
308	}
309
310	ftk_canvas_show(thiz, display, &rect, 0, 0);
311	
312	ftk_canvas_destroy(thiz);
313
314	sleep(3);
315
316	return;
317}
318#endif
319void test_alpha(FtkDisplay* display)
320{
321	int i = 0;
322	FtkGc gc = {.mask = FTK_GC_FG};
323	FtkColor color = {.a = 0xff};
324	int width = ftk_display_width(display);
325	int height = ftk_display_height(display);
326	FtkCanvas* thiz = ftk_canvas_create(width, height, &color);
327
328	color.g = 0xff;
329	color.r = 0;
330	for(i = 0; i < 0xff; i += 4)
331	{
332		color.a = 0xff;
333		color.g = 0;
334		gc.fg = color;
335		ftk_canvas_reset_gc(thiz, &gc);
336		ftk_canvas_draw_rect(thiz, 0, 0, width, height, 0, 1);
337		
338		color.a = 0xff - i;
339		color.g = 0xff;
340		gc.fg = color;
341		ftk_canvas_reset_gc(thiz, &gc);
342		ftk_canvas_draw_rect(thiz, 0, 0, width, height, 0, 1);
343		show_canvas(display, thiz);
344		usleep(200000);
345	}
346	
347	ftk_canvas_destroy(thiz);
348
349	sleep(3);
350
351	return;
352}
353
354void test_put_get_pixel(FtkDisplay* display)
355{
356	int i = 0;
357	int j = 0;
358	FtkColor color = {.a=0xff, .r=0xef, .g=0xdf, .b=0xcf};
359	int width = ftk_display_width(display);
360	int height = ftk_display_height(display);
361	FtkCanvas* thiz = ftk_canvas_create(width, height, &color);
362
363	for(i = 0; i < height; i++)
364	{
365		for(j = 0; j < width; j++)
366		{
367			FtkColor c = {0};
368			FtkColor* colorp = NULL;
369			colorp = &c;
370			assert(colorp->r == color.r);
371			assert(colorp->g == color.g);
372			assert(colorp->b == color.b);
373			assert(colorp->a == color.a);
374		}
375	}
376
377	ftk_canvas_destroy(thiz);
378
379	return;
380}
381
382void test_font(FtkDisplay* display, FtkFontDesc* font)
383{
384	int extent2 = 0;
385	FtkGc gc = {.mask = FTK_GC_FONT};
386	FtkColor color = {.a=0xff, .r=0xef, .g=0xdf, .b=0xcf};
387	int width = ftk_display_width(display);
388	int height = ftk_display_height(display);
389	FtkCanvas* thiz = ftk_canvas_create(width, height, &color);
390	const char* str = "?????????";
391	const char* other_side = NULL;
392	
393	gc.font = font;
394	ftk_canvas_set_gc(thiz, &gc);
395	
396	other_side = ftk_canvas_calc_str_visible_range(thiz, str, 0, -1, 60, NULL);
397	assert(strcmp(other_side, "??????") == 0);
398
399	other_side = ftk_canvas_calc_str_visible_range(thiz, str, other_side-str, -1, 60, NULL);
400	assert(strcmp(other_side, "???") == 0);
401	
402	other_side = ftk_canvas_calc_str_visible_range(thiz, str, other_side-str, -1, 60, NULL);
403	assert(strcmp(other_side, "") == 0);
404
405	other_side = ftk_canvas_calc_str_visible_range(thiz, str, -1, other_side-str, 60, NULL);
406	assert(strcmp(other_side, "???") == 0);
407	
408	other_side = ftk_canvas_calc_str_visible_range(thiz, str, -1, other_side-str, 60, NULL);
409	assert(strcmp(other_side, "??????") == 0);
410	
411	other_side = ftk_canvas_calc_str_visible_range(thiz, str, -1, other_side-str, 60, NULL);
412	assert(strcmp(other_side, str) == 0);
413	
414	other_side = ftk_canvas_calc_str_visible_range(thiz, str, -1, other_side-str, 60, NULL);
415	assert(strcmp(other_side, str) == 0);
416
417	printf("other_side = %s\n", other_side);
418	
419
420	str = "Single line editor, that means you can input a one line only.";
421	
422	extent2 = ftk_canvas_get_str_extent(thiz, str, -1);
423
424	ftk_canvas_destroy(thiz);
425	sleep(3);
426
427	return;
428}
429
430static void test_fill_bg(FtkDisplay* display)
431{
432	FtkColor color = {.a=0xff, .r=0xef, .g=0xdf, .b=0xcf};
433	int width = ftk_display_width(display);
434	int height = ftk_display_height(display);
435	FtkCanvas* thiz = ftk_canvas_create(width, height, &color);
436	FtkBitmap* bitmap = ftk_theme_load_image(ftk_default_theme(), "btn_default_pressed.9.png");
437	ftk_canvas_draw_bg_image(thiz, bitmap, FTK_BG_FOUR_CORNER, 10, 10, 100, 60);
438	ftk_canvas_draw_bg_image(thiz, bitmap, FTK_BG_FOUR_CORNER, 120, 10, 40, 60);
439	
440	ftk_canvas_draw_bg_image(thiz, bitmap, FTK_BG_FOUR_CORNER, 10, 80, 20, 20);
441	ftk_canvas_draw_bg_image(thiz, bitmap, FTK_BG_FOUR_CORNER, 30, 80, 40, 20);
442	ftk_canvas_draw_bg_image(thiz, bitmap, FTK_BG_FOUR_CORNER, 80, 80, 60, 20);
443	show_canvas(display, thiz);
444	ftk_canvas_destroy(thiz);
445
446	sleep(3);
447	
448	return;
449}
450
451static void test_draw_rect(FtkDisplay* display)
452{
453	int i = 0;
454	FtkColor color = {.a = 0xff};
455	int width = ftk_display_width(display);
456	int height = ftk_display_height(display);
457	FtkGc gc = {.mask = FTK_GC_FG};
458	FtkCanvas* thiz = ftk_canvas_create(width, height, &color);
459	gc.fg.a = 0xff;
460
461	gc.fg.r = 0xff;
462	for(i = 0; i < width/8; i++)
463	{
464		gc.fg.r -= 0x10;
465		ftk_canvas_set_gc(thiz, &gc);
466		ftk_canvas_draw_rect(thiz, width * i/8, 0, width/8 - 1, height/8 - 1, 0, 1);
467	}
468	
469	gc.fg.r = 0xff;
470	for(i = 0; i < width/8; i++)
471	{
472		gc.fg.r -= 0x10;
473		gc.fg.b += 0x10;
474		ftk_canvas_set_gc(thiz, &gc);
475		ftk_canvas_draw_rect(thiz, width * i/8, height/8, width/8 - 1, height/8 - 1, 0, 0);
476	}
477	
478	gc.fg.r = 0xff;
479	for(i = 0; i < width/8; i++)
480	{
481		gc.fg.r -= 0x10;
482		ftk_canvas_set_gc(thiz, &gc);
483		ftk_canvas_draw_rect(thiz, width * i/8, height/4, width/8 - 1, height/8 - 1, 1, 1);
484	}
485	
486	gc.fg.r = 0xff;
487	for(i = 0; i < width/8; i++)
488	{
489		gc.fg.r -= 0x10;
490		gc.fg.b += 0x10;
491		ftk_canvas_set_gc(thiz, &gc);
492		ftk_canvas_draw_rect(thiz, width * i/8, 3*height/8, width/8 - 1, height/8 - 1, 1, 0);
493	}
494	show_canvas(display, thiz);
495	ftk_canvas_destroy(thiz);
496
497	sleep(3);
498
499	return;
500}
501
502int main(int argc, char* argv[])
503{
504	ftk_init(argc, argv);
505
506	FtkRect rect = {0};
507	FtkColor bg = {.a = 0xff};
508	FtkBitmap* bitmap = NULL;
509	FtkFontDesc* font = ftk_default_font();
510	FtkDisplay* display = ftk_default_display();
511
512	rect.width = ftk_display_width(display);
513	rect.height = ftk_display_height(display);
514	
515	test_draw_rect(display);
516	test_alpha(display);
517	test_draw_vline(display);
518	bitmap = ftk_bitmap_create(ftk_display_width(display), ftk_display_height(display), bg);
519	ftk_display_snap(display, &rect, bitmap);
520	test_draw_hline(display);
521	ftk_display_update(display, bitmap, &rect, 0, 0);
522	test_fill_bg(display);
523	test_font(display, font);
524	test_put_get_pixel(display);
525	test_draw_hline(display);
526	test_draw_vline(display);
527	ftk_bitmap_unref(bitmap);
528	
529	ftk_run();
530
531	return 0;
532}
533#else
534int main(int argc, char* argv[])
535{
536	return 0;
537}
538#endif