/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. * History:
  26. * ================================================================
  27. * 2009-10-03 Li XianJing <xianjimli@hotmail.com> created
  28. *
  29. */
  30. #include "ftk.h"
  31. #include "ftk_canvas.h"
  32. static void show_canvas(FtkDisplay* display, FtkCanvas* canvas)
  33. {
  34. FtkBitmap* bitmap = NULL;
  35. FtkRect rect = {.x = 0, .y=0, .width=0, .height=0};
  36. rect.width = ftk_display_width(display);
  37. rect.height = ftk_display_height(display);
  38. ftk_canvas_lock_buffer(canvas, &bitmap);
  39. ftk_display_update(display, bitmap, &rect, 0, 0);
  40. ftk_canvas_unlock_buffer(canvas);
  41. return;
  42. }
  43. #if 1
  44. void test_misc(FtkDisplay* display, FtkFontDesc* font)
  45. {
  46. if(display != NULL)
  47. {
  48. int i = 0;
  49. FtkGc gc = {0};
  50. int extent = 0;
  51. FtkColor color = {0x0, 0, 0, 0x0};
  52. FtkRect rect = {.x = 0, .y=0, .width=0, .height=0};
  53. rect.width = ftk_display_width(display);
  54. rect.height = ftk_display_height(display);
  55. gc.mask = FTK_GC_FG | FTK_GC_FONT;
  56. gc.fg.a = 0xff;
  57. gc.fg.r = 0x00;
  58. gc.fg.g = 0x00;
  59. gc.fg.b = 0xff;
  60. gc.font = font;
  61. FtkCanvas* thiz = ftk_canvas_create(rect.width, rect.height, &color);
  62. show_canvas(display, thiz);
  63. for(i = 0; i < ftk_display_height(display); i++)
  64. {
  65. if(gc.fg.r < 0xff)
  66. {
  67. gc.fg.r++;
  68. }
  69. else
  70. {
  71. gc.fg.g++;
  72. }
  73. ftk_canvas_set_gc(thiz, &gc);
  74. ftk_canvas_draw_hline(thiz, 0, i, 320);
  75. }
  76. FtkBitmap* bitmap = ftk_bitmap_create(100, 100, color);
  77. ftk_canvas_draw_bitmap_simple(thiz, bitmap, 0, 0, 100, 100, 100, 100);
  78. ftk_canvas_draw_string(thiz, 0, 240, " Jim is a Programmer.", -1, 0);
  79. gc.fg.b = 0xff;
  80. ftk_canvas_set_gc(thiz, &gc);
  81. ftk_canvas_draw_string(thiz, 0, 220, "?????????", -1, 0);
  82. unsigned int line_mask = 0xaaaaaaaa;
  83. gc.line_mask = line_mask;
  84. gc.mask = FTK_GC_LINE_MASK;
  85. ftk_canvas_set_gc(thiz, &gc);
  86. show_canvas(display, thiz);
  87. extent = ftk_canvas_get_str_extent(thiz, "???", -1);
  88. printf("extent=%d\n", ftk_canvas_get_str_extent(thiz, "???", -1));
  89. ftk_bitmap_unref(bitmap);
  90. ftk_canvas_destroy(thiz);
  91. }
  92. sleep(3);
  93. return;
  94. }
  95. #if 1
  96. void test_draw_point(FtkDisplay* display)
  97. {
  98. int i = 0;
  99. FtkGc gc = {.mask = FTK_GC_FG};
  100. FtkPoint p = {0};
  101. FtkColor color = {.a = 0xff};
  102. int width = ftk_display_width(display);
  103. int height = ftk_display_height(display);
  104. FtkCanvas* thiz = ftk_canvas_create(width, height, &color);
  105. color.r = 0xff;
  106. color.a = 0xff;
  107. gc.fg = color;
  108. for(i = 0; i < width; i++)
  109. {
  110. p.x = i;
  111. p.y = 10;
  112. ftk_canvas_reset_gc(thiz, &gc);
  113. ftk_canvas_draw_pixels(thiz, &p, 1);
  114. }
  115. color.g = 0xff;
  116. color.r = 0;
  117. for(i = 0; i < width; i++)
  118. {
  119. color.a = 0xff - (0xff & i);
  120. gc.fg = color;
  121. p.x = i;
  122. p.y = 20;
  123. ftk_canvas_reset_gc(thiz, &gc);
  124. ftk_canvas_draw_pixels(thiz, &p, 1);
  125. }
  126. color.r = 0;
  127. color.g = 0;
  128. color.b = 0xff;
  129. color.a = 0xff;
  130. gc.fg = color;
  131. gc.mask |= FTK_GC_ALPHA;
  132. for(i = 0; i < width; i++)
  133. {
  134. gc.alpha = 0xff - (0xff & i);
  135. ftk_canvas_reset_gc(thiz, &gc);
  136. p.x = i;
  137. p.y = 20;
  138. ftk_canvas_draw_pixels(thiz, &p, 1);
  139. }
  140. show_canvas(display, thiz);
  141. ftk_canvas_destroy(thiz);
  142. sleep(3);
  143. return;
  144. }
  145. #endif
  146. void test_draw_vline(FtkDisplay* display)
  147. {
  148. int i = 0;
  149. FtkGc gc = {.mask = FTK_GC_FG};
  150. FtkColor color = {.a = 0xff};
  151. int width = ftk_display_width(display);
  152. int height = ftk_display_height(display);
  153. FtkCanvas* thiz = ftk_canvas_create(width, height, &color);
  154. color.r = 0xff;
  155. color.a = 0xff;
  156. gc.fg = color;
  157. for(i = 0; i < width; i++)
  158. {
  159. ftk_canvas_reset_gc(thiz, &gc);
  160. ftk_canvas_draw_vline(thiz, i, 0, 20);
  161. }
  162. color.g = 0xff;
  163. color.r = 0;
  164. for(i = 0; i < width; i++)
  165. {
  166. color.a = 0xff - (0xff & i);
  167. gc.fg = color;
  168. ftk_canvas_reset_gc(thiz, &gc);
  169. ftk_canvas_draw_vline(thiz, i, 30, 20);
  170. }
  171. color.r = 0;
  172. color.g = 0;
  173. color.b = 0xff;
  174. color.a = 0xff;
  175. gc.fg = color;
  176. gc.mask |= FTK_GC_ALPHA;
  177. for(i = 0; i < width; i++)
  178. {
  179. gc.alpha = 0xff - (0xff & i);
  180. ftk_canvas_reset_gc(thiz, &gc);
  181. ftk_canvas_draw_vline(thiz, i, 60, 20);
  182. }
  183. show_canvas(display, thiz);
  184. ftk_canvas_destroy(thiz);
  185. sleep(3);
  186. return;
  187. }
  188. void test_draw_hline(FtkDisplay* display)
  189. {
  190. int i = 0;
  191. FtkGc gc = {.mask = FTK_GC_FG};
  192. FtkColor color = {.a = 0xff};
  193. int width = ftk_display_width(display);
  194. int height = ftk_display_height(display);
  195. FtkCanvas* thiz = ftk_canvas_create(width, height, &color);
  196. color.r = 0xff;
  197. color.a = 0xff;
  198. gc.fg = color;
  199. for(i = 0; i < height; i++)
  200. {
  201. ftk_canvas_reset_gc(thiz, &gc);
  202. ftk_canvas_draw_hline(thiz, 0, i, 20);
  203. }
  204. color.g = 0xff;
  205. color.r = 0;
  206. for(i = 0; i < height; i++)
  207. {
  208. color.a = 0xff - (0xff & i);
  209. gc.fg = color;
  210. ftk_canvas_reset_gc(thiz, &gc);
  211. ftk_canvas_draw_hline(thiz, 30, i, 20);
  212. }
  213. color.r = 0;
  214. color.g = 0;
  215. color.b = 0xff;
  216. color.a = 0xff;
  217. gc.fg = color;
  218. gc.mask |= FTK_GC_ALPHA;
  219. for(i = 0; i < height; i++)
  220. {
  221. gc.alpha = 0xff - (0xff & i);
  222. ftk_canvas_reset_gc(thiz, &gc);
  223. ftk_canvas_draw_hline(thiz, 60, i, 20);
  224. }
  225. show_canvas(display, thiz);
  226. ftk_canvas_destroy(thiz);
  227. sleep(3);
  228. return;
  229. }
  230. #if 1
  231. void test_draw_line(FtkDisplay* display)
  232. {
  233. int i = 0;
  234. FtkGc gc = {.mask = FTK_GC_FG};
  235. FtkRect rect = {0};
  236. FtkColor color = {.a = 0xff};
  237. int width = ftk_display_width(display);
  238. int height = ftk_display_height(display);
  239. FtkCanvas* thiz = ftk_canvas_create(width, height, &color);
  240. rect.width = width;
  241. rect.height = height;
  242. color.r = 0xff;
  243. color.a = 0xff;
  244. gc.fg = color;
  245. for(i = 0; i < height/2; i++)
  246. {
  247. ftk_canvas_reset_gc(thiz, &gc);
  248. ftk_canvas_draw_line(thiz, 0, i, 20, i+10);
  249. }
  250. color.g = 0xff;
  251. color.r = 0;
  252. for(i = 0; i < height/2; i++)
  253. {
  254. color.a = 0xff - (0xff & i);
  255. gc.fg = color;
  256. ftk_canvas_reset_gc(thiz, &gc);
  257. ftk_canvas_draw_line(thiz, 30, i, 50, i+10);
  258. }
  259. color.r = 0;
  260. color.g = 0;
  261. color.b = 0xff;
  262. color.a = 0xff;
  263. gc.fg = color;
  264. gc.mask |= FTK_GC_ALPHA;
  265. for(i = 0; i < height/2; i++)
  266. {
  267. gc.alpha = 0xff - (0xff & i);
  268. ftk_canvas_reset_gc(thiz, &gc);
  269. ftk_canvas_draw_line(thiz, 60, i, 80, i+10);
  270. }
  271. ftk_canvas_show(thiz, display, &rect, 0, 0);
  272. ftk_canvas_destroy(thiz);
  273. sleep(3);
  274. return;
  275. }
  276. #endif
  277. void test_alpha(FtkDisplay* display)
  278. {
  279. int i = 0;
  280. FtkGc gc = {.mask = FTK_GC_FG};
  281. FtkColor color = {.a = 0xff};
  282. int width = ftk_display_width(display);
  283. int height = ftk_display_height(display);
  284. FtkCanvas* thiz = ftk_canvas_create(width, height, &color);
  285. color.g = 0xff;
  286. color.r = 0;
  287. for(i = 0; i < 0xff; i += 4)
  288. {
  289. color.a = 0xff;
  290. color.g = 0;
  291. gc.fg = color;
  292. ftk_canvas_reset_gc(thiz, &gc);
  293. ftk_canvas_draw_rect(thiz, 0, 0, width, height, 0, 1);
  294. color.a = 0xff - i;
  295. color.g = 0xff;
  296. gc.fg = color;
  297. ftk_canvas_reset_gc(thiz, &gc);
  298. ftk_canvas_draw_rect(thiz, 0, 0, width, height, 0, 1);
  299. show_canvas(display, thiz);
  300. usleep(200000);
  301. }
  302. ftk_canvas_destroy(thiz);
  303. sleep(3);
  304. return;
  305. }
  306. void test_put_get_pixel(FtkDisplay* display)
  307. {
  308. int i = 0;
  309. int j = 0;
  310. FtkColor color = {.a=0xff, .r=0xef, .g=0xdf, .b=0xcf};
  311. int width = ftk_display_width(display);
  312. int height = ftk_display_height(display);
  313. FtkCanvas* thiz = ftk_canvas_create(width, height, &color);
  314. for(i = 0; i < height; i++)
  315. {
  316. for(j = 0; j < width; j++)
  317. {
  318. FtkColor c = {0};
  319. FtkColor* colorp = NULL;
  320. colorp = &c;
  321. assert(colorp->r == color.r);
  322. assert(colorp->g == color.g);
  323. assert(colorp->b == color.b);
  324. assert(colorp->a == color.a);
  325. }
  326. }
  327. ftk_canvas_destroy(thiz);
  328. return;
  329. }
  330. void test_font(FtkDisplay* display, FtkFontDesc* font)
  331. {
  332. int extent2 = 0;
  333. FtkGc gc = {.mask = FTK_GC_FONT};
  334. FtkColor color = {.a=0xff, .r=0xef, .g=0xdf, .b=0xcf};
  335. int width = ftk_display_width(display);
  336. int height = ftk_display_height(display);
  337. FtkCanvas* thiz = ftk_canvas_create(width, height, &color);
  338. const char* str = "?????????";
  339. const char* other_side = NULL;
  340. gc.font = font;
  341. ftk_canvas_set_gc(thiz, &gc);
  342. other_side = ftk_canvas_calc_str_visible_range(thiz, str, 0, -1, 60, NULL);
  343. assert(strcmp(other_side, "??????") == 0);
  344. other_side = ftk_canvas_calc_str_visible_range(thiz, str, other_side-str, -1, 60, NULL);
  345. assert(strcmp(other_side, "???") == 0);
  346. other_side = ftk_canvas_calc_str_visible_range(thiz, str, other_side-str, -1, 60, NULL);
  347. assert(strcmp(other_side, "") == 0);
  348. other_side = ftk_canvas_calc_str_visible_range(thiz, str, -1, other_side-str, 60, NULL);
  349. assert(strcmp(other_side, "???") == 0);
  350. other_side = ftk_canvas_calc_str_visible_range(thiz, str, -1, other_side-str, 60, NULL);
  351. assert(strcmp(other_side, "??????") == 0);
  352. other_side = ftk_canvas_calc_str_visible_range(thiz, str, -1, other_side-str, 60, NULL);
  353. assert(strcmp(other_side, str) == 0);
  354. other_side = ftk_canvas_calc_str_visible_range(thiz, str, -1, other_side-str, 60, NULL);
  355. assert(strcmp(other_side, str) == 0);
  356. printf("other_side = %s\n", other_side);
  357. str = "Single line editor, that means you can input a one line only.";
  358. extent2 = ftk_canvas_get_str_extent(thiz, str, -1);
  359. ftk_canvas_destroy(thiz);
  360. sleep(3);
  361. return;
  362. }
  363. static void test_fill_bg(FtkDisplay* display)
  364. {
  365. FtkColor color = {.a=0xff, .r=0xef, .g=0xdf, .b=0xcf};
  366. int width = ftk_display_width(display);
  367. int height = ftk_display_height(display);
  368. FtkCanvas* thiz = ftk_canvas_create(width, height, &color);
  369. FtkBitmap* bitmap = ftk_theme_load_image(ftk_default_theme(), "btn_default_pressed.9.png");
  370. ftk_canvas_draw_bg_image(thiz, bitmap, FTK_BG_FOUR_CORNER, 10, 10, 100, 60);
  371. ftk_canvas_draw_bg_image(thiz, bitmap, FTK_BG_FOUR_CORNER, 120, 10, 40, 60);
  372. ftk_canvas_draw_bg_image(thiz, bitmap, FTK_BG_FOUR_CORNER, 10, 80, 20, 20);
  373. ftk_canvas_draw_bg_image(thiz, bitmap, FTK_BG_FOUR_CORNER, 30, 80, 40, 20);
  374. ftk_canvas_draw_bg_image(thiz, bitmap, FTK_BG_FOUR_CORNER, 80, 80, 60, 20);
  375. show_canvas(display, thiz);
  376. ftk_canvas_destroy(thiz);
  377. sleep(3);
  378. return;
  379. }
  380. static void test_draw_rect(FtkDisplay* display)
  381. {
  382. int i = 0;
  383. FtkColor color = {.a = 0xff};
  384. int width = ftk_display_width(display);
  385. int height = ftk_display_height(display);
  386. FtkGc gc = {.mask = FTK_GC_FG};
  387. FtkCanvas* thiz = ftk_canvas_create(width, height, &color);
  388. gc.fg.a = 0xff;
  389. gc.fg.r = 0xff;
  390. for(i = 0; i < width/8; i++)
  391. {
  392. gc.fg.r -= 0x10;
  393. ftk_canvas_set_gc(thiz, &gc);
  394. ftk_canvas_draw_rect(thiz, width * i/8, 0, width/8 - 1, height/8 - 1, 0, 1);
  395. }
  396. gc.fg.r = 0xff;
  397. for(i = 0; i < width/8; i++)
  398. {
  399. gc.fg.r -= 0x10;
  400. gc.fg.b += 0x10;
  401. ftk_canvas_set_gc(thiz, &gc);
  402. ftk_canvas_draw_rect(thiz, width * i/8, height/8, width/8 - 1, height/8 - 1, 0, 0);
  403. }
  404. gc.fg.r = 0xff;
  405. for(i = 0; i < width/8; i++)
  406. {
  407. gc.fg.r -= 0x10;
  408. ftk_canvas_set_gc(thiz, &gc);
  409. ftk_canvas_draw_rect(thiz, width * i/8, height/4, width/8 - 1, height/8 - 1, 1, 1);
  410. }
  411. gc.fg.r = 0xff;
  412. for(i = 0; i < width/8; i++)
  413. {
  414. gc.fg.r -= 0x10;
  415. gc.fg.b += 0x10;
  416. ftk_canvas_set_gc(thiz, &gc);
  417. ftk_canvas_draw_rect(thiz, width * i/8, 3*height/8, width/8 - 1, height/8 - 1, 1, 0);
  418. }
  419. show_canvas(display, thiz);
  420. ftk_canvas_destroy(thiz);
  421. sleep(3);
  422. return;
  423. }
  424. int main(int argc, char* argv[])
  425. {
  426. ftk_init(argc, argv);
  427. FtkRect rect = {0};
  428. FtkColor bg = {.a = 0xff};
  429. FtkBitmap* bitmap = NULL;
  430. FtkFontDesc* font = ftk_default_font();
  431. FtkDisplay* display = ftk_default_display();
  432. rect.width = ftk_display_width(display);
  433. rect.height = ftk_display_height(display);
  434. test_draw_rect(display);
  435. test_alpha(display);
  436. test_draw_vline(display);
  437. bitmap = ftk_bitmap_create(ftk_display_width(display), ftk_display_height(display), bg);
  438. ftk_display_snap(display, &rect, bitmap);
  439. test_draw_hline(display);
  440. ftk_display_update(display, bitmap, &rect, 0, 0);
  441. test_fill_bg(display);
  442. test_font(display, font);
  443. test_put_get_pixel(display);
  444. test_draw_hline(display);
  445. test_draw_vline(display);
  446. ftk_bitmap_unref(bitmap);
  447. ftk_run();
  448. return 0;
  449. }
  450. #else
  451. int main(int argc, char* argv[])
  452. {
  453. return 0;
  454. }
  455. #endif