Max 5 API Reference
00001 /** 00002 @file 00003 uitester - demonstrate the drawing of various objects using jgraphics 00004 00005 @ingroup examples 00006 */ 00007 00008 #include "ext.h" 00009 #include "ext_obex.h" 00010 #include "jpatcher_api.h" 00011 #include "jgraphics.h" 00012 00013 00014 /**********************************************************************/ 00015 // Data Structures 00016 00017 typedef struct _color_stop { 00018 double offset; 00019 t_jrgba color; 00020 } t_color_stop; 00021 00022 00023 typedef struct _uitester { 00024 t_jbox j_box; 00025 long j_whichmouse; 00026 long j_mode; 00027 double j_param1; // generic params to hook to various tests 00028 double j_param2; 00029 double j_param3; 00030 double j_param4; 00031 t_jmatrix j_matrix; 00032 t_jpattern *j_bgpattern; // solid, gradient, or image 00033 t_jmatrix j_pattern_matrix; 00034 t_jsurface *j_image; // for testing image drawing 00035 t_jsvg *j_svg; // svg, of course 00036 double j_x0; 00037 double j_y0; 00038 double j_r0; 00039 double j_x1; 00040 double j_y1; 00041 double j_r1; 00042 long j_isradial; 00043 long j_num_color_stops; 00044 t_color_stop *j_color_stops; 00045 long j_autoscale; // set scale so box goes from 0 to 1 on each dim 00046 } t_uitester; 00047 00048 00049 /**********************************************************************/ 00050 // Prototypes 00051 00052 void uitester_initclass(); 00053 t_uitester* uitester_new(t_symbol *s, short argc, t_atom *argv); 00054 void uitester_free(t_uitester *x); 00055 00056 void uitester_setmouse(t_uitester *x, long which); 00057 void uitester_mouseenter(t_uitester *x, t_object *patcherview, t_pt pt, long modifiers); 00058 00059 void uitester_paint(t_uitester *x, t_object *view); 00060 void uitester_read(t_uitester *x, t_symbol *s, short argc, t_atom *argv); 00061 void uitester_doread(t_uitester *x, t_symbol *s, short argc, t_atom *argv); 00062 void uitester_identity(t_uitester *x); 00063 void uitester_translate(t_uitester *x, double tx, double ty); 00064 void uitester_rotate(t_uitester *x, t_symbol *s, short argc, t_atom *argv); 00065 void uitester_scale(t_uitester *x, double sx, double sy); 00066 void uitester_pattern_identity(t_uitester *x); 00067 void uitester_pattern_translate(t_uitester *x, double tx, double ty); 00068 void uitester_pattern_rotate(t_uitester *x, double degrees); 00069 void uitester_pattern_scale(t_uitester *x, double sx, double sy); 00070 void uitester_setpattern(t_uitester *x, t_symbol *s, short argc, t_atom *argv); 00071 void uitester_addcolorstop(t_uitester *x, t_symbol *s, short argc, t_atom *argv); 00072 void uitester_clearcolorstops(t_uitester *x); 00073 void uitester_setupgradient(t_uitester *x); 00074 void uitester_autoscale(t_uitester *x, long way); 00075 void uitester_size(t_uitester *x, long width, long height); 00076 void uitester_param1(t_uitester *x, double value); 00077 void uitester_param2(t_uitester *x, double value); 00078 void uitester_param3(t_uitester *x, double value); 00079 void uitester_param4(t_uitester *x, double value); 00080 00081 // various painting tests 00082 void uitester_int(t_uitester *x, long which); 00083 void uitester_rectangle(t_jgraphics *g, double width, double height); 00084 void uitester_image(t_uitester *x, t_jgraphics *g, double width, double height); 00085 void uitester_svg(t_uitester *x, t_jgraphics *g, double width, double height); 00086 00087 void snippet_normalize(t_jgraphics *g, double width, double height); 00088 void snippet_arc(t_jgraphics *cr, double width, double height); 00089 void snippet_arc_negative(t_jgraphics *cr, double width, double height); 00090 void snippet_curve_rectangle(t_jgraphics *cr, double width, double height); 00091 void snippet_curve_to(t_jgraphics *cr, double width, double height); 00092 void snippet_fill_and_stroke2(t_jgraphics *cr, double width, double height); 00093 void snippet_fill_and_stroke(t_jgraphics *cr, double width, double height); 00094 void snippet_gradient(t_jgraphics *cr, double width, double height); 00095 void snippet_path(t_jgraphics *cr, double width, double height); 00096 void snippet_set_line_cap(t_jgraphics *cr, double width, double height); 00097 void snippet_set_line_join(t_jgraphics *cr, double width, double height); 00098 void snippet_dash(t_jgraphics *cr, double width, double height); 00099 void snippet_long_lines(t_jgraphics *cr, double width, double height); 00100 void snippet_multi_segment_caps(t_jgraphics *cr, double width, double height); 00101 void snippet_self_intersect(t_jgraphics *cr, double width, double height); 00102 void snippet_clock(t_jgraphics *cr, double width, double height); 00103 void snippet_offscreen(t_jgraphics *cr, double width, double height); 00104 void snippet_doughnut(t_jgraphics *cr, double width, double height); 00105 00106 void uitester_fill_path_with_surface(t_jgraphics *g, double width, double height); 00107 void uitester_antialiased_lines(t_jgraphics *g, double width, double height); 00108 00109 void uitester_image_surface_draw(t_jgraphics *g, double width, double height); 00110 00111 void uitester_textlayout(t_jgraphics *g, double width, double height); 00112 void uitester_scroll(t_jgraphics *g, double width, double height); 00113 void uitester_showtext(t_jgraphics *g, double width, double height); 00114 00115 void uitester_strokeroundedrect(t_jgraphics *g, double width, double height); 00116 00117 void uitester_textlayouttransformed(t_jgraphics *g, double width, double height); 00118 00119 00120 /**********************************************************************/ 00121 // Globals and Statics 00122 00123 static t_class *s_uitester_class = NULL; 00124 00125 00126 /**********************************************************************/ 00127 // Class Definition and Life Cycle 00128 00129 int main(void) 00130 { 00131 t_class *c; 00132 00133 c = class_new("uitester", 00134 (method)uitester_new, 00135 (method)uitester_free, 00136 sizeof(t_uitester), 00137 (method)NULL, 00138 A_GIMME, 00139 0L); 00140 00141 jbox_initclass(c, 0); 00142 00143 class_addmethod(c, (method) uitester_setmouse, "setmouse", A_LONG, 0); 00144 class_addmethod(c, (method) uitester_mouseenter, "mouseenter", A_CANT, 0); 00145 class_addmethod(c, (method) uitester_read, "read", A_GIMME, 0); 00146 class_addmethod(c, (method) uitester_paint, "paint", A_CANT, 0); 00147 class_addmethod(c, (method) uitester_identity, "identity", 0); 00148 class_addmethod(c, (method) uitester_translate, "translate", A_FLOAT, A_FLOAT, 0); 00149 class_addmethod(c, (method) uitester_rotate, "rotate", A_GIMME, 0); 00150 class_addmethod(c, (method) uitester_scale, "scale", A_FLOAT, A_FLOAT, 0); 00151 class_addmethod(c, (method) uitester_pattern_identity, "patternidentity", 0); 00152 class_addmethod(c, (method) uitester_pattern_translate, "patterntranslate", A_FLOAT, A_FLOAT, 0); 00153 class_addmethod(c, (method) uitester_pattern_rotate, "patternrotate", A_FLOAT, 0); 00154 class_addmethod(c, (method) uitester_pattern_scale, "patternscale", A_FLOAT, A_FLOAT, 0); 00155 class_addmethod(c, (method) uitester_setpattern, "setpattern", A_GIMME, 0); 00156 class_addmethod(c, (method) uitester_addcolorstop, "addcolorstop", A_GIMME, 0); 00157 class_addmethod(c, (method) uitester_autoscale, "autoscale", A_LONG, 0); 00158 class_addmethod(c, (method) uitester_size, "size", A_LONG, A_LONG, 0); 00159 class_addmethod(c, (method) uitester_clearcolorstops, "clearcolorstops", 0); 00160 class_addmethod(c, (method) uitester_param1, "param1", A_FLOAT, 0); 00161 class_addmethod(c, (method) uitester_param2, "param2", A_FLOAT, 0); 00162 class_addmethod(c, (method) uitester_param3, "param3", A_FLOAT, 0); 00163 class_addmethod(c, (method) uitester_param4, "param4", A_FLOAT, 0); 00164 class_addmethod(c, (method) uitester_int, "int", A_LONG, 0); 00165 00166 CLASS_ATTR_DEFAULT(c, "rect", 0, "0. 0. 128. 128."); 00167 00168 class_register(CLASS_BOX, c); 00169 s_uitester_class = c; 00170 return 0; 00171 } 00172 00173 00174 t_uitester* uitester_new(t_symbol *s, short argc, t_atom *argv) 00175 { 00176 t_uitester* x = (t_uitester*)object_alloc(s_uitester_class); 00177 00178 if (x) { 00179 long flags; 00180 flags = 0 00181 | JBOX_DRAWFIRSTIN 00182 // | JBOX_NODRAWBOX 00183 // | JBOX_DRAWINLAST 00184 | JBOX_TRANSPARENT 00185 // | JBOX_NOGROW 00186 // | JBOX_GROWY 00187 | JBOX_GROWBOTH 00188 // | JBOX_IGNORELOCKCLICK 00189 | JBOX_HILITE 00190 // | JBOX_BACKGROUND 00191 // | JBOX_NOFLOATINSPECTOR 00192 // | JBOX_TEXTFIELD 00193 ; 00194 00195 jbox_new(&x->j_box, flags, argc, argv); 00196 x->j_box.b_firstin = (t_object*) x; 00197 00198 x->j_whichmouse = -1; // none set 00199 x->j_param1 = 0; 00200 x->j_param2 = 0; 00201 x->j_param3 = 0; 00202 x->j_param4 = 0; 00203 jgraphics_matrix_init_identity(&x->j_matrix); 00204 x->j_bgpattern = NULL; 00205 x->j_image = NULL; 00206 x->j_svg = NULL; 00207 jgraphics_matrix_init_identity(&x->j_pattern_matrix); 00208 x->j_num_color_stops = 0; 00209 x->j_color_stops = NULL; 00210 x->j_x0 = x->j_y0 = x->j_r0 = 0; 00211 x->j_x1 = x->j_y1 = x->j_r1 = 0; 00212 x->j_isradial = 0; 00213 x->j_autoscale = 0; 00214 00215 jbox_ready(&x->j_box); 00216 } 00217 return x; 00218 } 00219 00220 00221 void uitester_free(t_uitester *x) 00222 { 00223 jbox_free(&x->j_box); 00224 if (x->j_bgpattern) { 00225 jgraphics_pattern_destroy(x->j_bgpattern); 00226 x->j_bgpattern = NULL; 00227 } 00228 if (x->j_image) { 00229 jgraphics_surface_destroy(x->j_image); 00230 x->j_image = NULL; 00231 } 00232 if (x->j_svg) { 00233 jsvg_destroy(x->j_svg); 00234 x->j_svg = NULL; 00235 } 00236 } 00237 00238 00239 /**********************************************************************/ 00240 // Methods 00241 00242 void uitester_identity(t_uitester *x) 00243 { 00244 jgraphics_matrix_init_identity(&x->j_matrix); 00245 jbox_redraw(&x->j_box); 00246 } 00247 00248 00249 void uitester_translate(t_uitester *x, double tx, double ty) 00250 { 00251 jgraphics_matrix_translate(&x->j_matrix, tx, ty); 00252 jbox_redraw(&x->j_box); 00253 } 00254 00255 00256 void uitester_rotate(t_uitester *x, t_symbol *s, short argc, t_atom *argv) 00257 { 00258 double degrees = 0; 00259 double cx=0, cy=0; 00260 if (argc--) 00261 degrees = atom_getfloat(argv++); 00262 if (argc--) 00263 cx = atom_getfloat(argv++); 00264 if (argc--) 00265 cy = atom_getfloat(argv++); 00266 jgraphics_matrix_translate(&x->j_matrix, -cx, -cy); 00267 jgraphics_matrix_rotate(&x->j_matrix, degrees * JGRAPHICS_PI * (1./180.)); 00268 jgraphics_matrix_translate(&x->j_matrix, cx, cy); 00269 jbox_redraw(&x->j_box); 00270 } 00271 00272 00273 void uitester_scale(t_uitester *x, double sx, double sy) 00274 { 00275 jgraphics_matrix_scale(&x->j_matrix, sx, sy); 00276 jbox_redraw(&x->j_box); 00277 } 00278 00279 00280 void uitester_pattern_identity(t_uitester *x) 00281 { 00282 jgraphics_matrix_init_identity(&x->j_pattern_matrix); 00283 jbox_redraw(&x->j_box); 00284 } 00285 00286 00287 void uitester_pattern_translate(t_uitester *x, double tx, double ty) 00288 { 00289 jgraphics_matrix_translate(&x->j_pattern_matrix, tx, ty); 00290 jbox_redraw(&x->j_box); 00291 } 00292 00293 00294 void uitester_pattern_rotate(t_uitester *x, double degrees) // degrees are more convenient from a max message 00295 { 00296 jgraphics_matrix_rotate(&x->j_pattern_matrix, degrees * JGRAPHICS_PI * (1./180.)); 00297 jbox_redraw(&x->j_box); 00298 } 00299 00300 00301 void uitester_pattern_scale(t_uitester *x, double sx, double sy) 00302 { 00303 jgraphics_matrix_scale(&x->j_pattern_matrix, sx, sy); 00304 jbox_redraw(&x->j_box); 00305 } 00306 00307 00308 void uitester_setpattern(t_uitester *x, t_symbol *s, short argc, t_atom *argv) 00309 { 00310 if (!argc) 00311 return; 00312 if (x->j_bgpattern) { 00313 jgraphics_pattern_destroy(x->j_bgpattern); 00314 x->j_bgpattern = NULL; 00315 } 00316 if (argv->a_type == A_FLOAT) { 00317 double red=0, green=0, blue=0, alpha=1; 00318 if (argc--) 00319 red = atom_getfloat(argv++); 00320 if (argc--) 00321 green = atom_getfloat(argv++); 00322 if (argc--) 00323 blue = atom_getfloat(argv++); 00324 if (argc--) 00325 alpha = atom_getfloat(argv++); 00326 x->j_bgpattern = jgraphics_pattern_create_rgba(red, green, blue, alpha); 00327 } 00328 else if (argv->a_type == A_SYM) { 00329 t_symbol *s = argv->a_w.w_sym; 00330 argc--, argv++; 00331 if (s == gensym("image")) { 00332 00333 } 00334 else if (s == gensym("linear")) { 00335 double x0 = 0, y0 = 0, x1 = 0, y1 = 0; 00336 if (argc--) 00337 x0 = atom_getfloat(argv++); 00338 if (argc--) 00339 y0 = atom_getfloat(argv++); 00340 if (argc--) 00341 x1 = atom_getfloat(argv++); 00342 if (argc--) 00343 y1 = atom_getfloat(argv++); 00344 x->j_x0 = x0; 00345 x->j_y0 = y0; 00346 x->j_x1 = x1; 00347 x->j_y1 = y1; 00348 x->j_isradial = 0; 00349 uitester_setupgradient(x); 00350 } 00351 else if (s == gensym("radial")) { 00352 double cx0=0, cy0=0, r0=1, cx1=0, cy1=0, r1=1; 00353 if (argc--) 00354 cx0 = atom_getfloat(argv++); 00355 if (argc--) 00356 cy0 = atom_getfloat(argv++); 00357 if (argc--) 00358 r0 = atom_getfloat(argv++); 00359 if (argc--) 00360 cx1 = atom_getfloat(argv++); 00361 if (argc--) 00362 cy1 = atom_getfloat(argv++); 00363 if (argc--) 00364 r1 = atom_getfloat(argv++); 00365 x->j_x0 = cx0; 00366 x->j_y0 = cy0; 00367 x->j_r0 = r0; 00368 x->j_x1 = cx1; 00369 x->j_y1 = cy1; 00370 x->j_r1 = r1; 00371 x->j_isradial = 1; 00372 uitester_setupgradient(x); 00373 } 00374 } 00375 jbox_redraw(&x->j_box); 00376 } 00377 00378 00379 void uitester_addcolorstop(t_uitester *x, t_symbol *s, short argc, t_atom *argv) 00380 { 00381 double offset=0, r=0, g=0, b=0, a=1; 00382 if (!x->j_color_stops) { 00383 x->j_color_stops = (t_color_stop*) sysmem_newptr(sizeof(t_color_stop)); 00384 } 00385 else { 00386 x->j_color_stops = (t_color_stop*) sysmem_resizeptr(x->j_color_stops, sizeof(t_color_stop) * (x->j_num_color_stops+1)); 00387 } 00388 x->j_num_color_stops++; 00389 if (argc--) 00390 offset = atom_getfloat(argv++); 00391 if (argc--) 00392 r = atom_getfloat(argv++); 00393 if (argc--) 00394 g = atom_getfloat(argv++); 00395 if (argc--) 00396 b = atom_getfloat(argv++); 00397 if (argc--) 00398 a = atom_getfloat(argv++); 00399 x->j_color_stops[x->j_num_color_stops-1].offset = offset; 00400 x->j_color_stops[x->j_num_color_stops-1].color.red = r; 00401 x->j_color_stops[x->j_num_color_stops-1].color.green = g; 00402 x->j_color_stops[x->j_num_color_stops-1].color.blue = b; 00403 x->j_color_stops[x->j_num_color_stops-1].color.alpha = a; 00404 uitester_setupgradient(x); 00405 } 00406 00407 00408 void uitester_clearcolorstops(t_uitester *x) 00409 { 00410 if (x->j_color_stops) { 00411 sysmem_freeptr(x->j_color_stops); 00412 x->j_color_stops = NULL; 00413 } 00414 x->j_num_color_stops = 0; 00415 if (x->j_bgpattern) { 00416 t_jgraphics_pattern_type type; 00417 type = jgraphics_pattern_get_type(x->j_bgpattern); 00418 if (type == JGRAPHICS_PATTERN_TYPE_LINEAR || type == JGRAPHICS_PATTERN_TYPE_RADIAL) { 00419 uitester_setupgradient(x); 00420 } 00421 } 00422 } 00423 00424 00425 void uitester_setupgradient(t_uitester *x) 00426 { 00427 if (x->j_bgpattern) { 00428 jgraphics_pattern_destroy(x->j_bgpattern); 00429 x->j_bgpattern = NULL; 00430 } 00431 if (x->j_num_color_stops < 2) 00432 return; // can't make it yet 00433 if (!x->j_isradial) { 00434 x->j_bgpattern = jgraphics_pattern_create_linear(x->j_x0, x->j_y0, x->j_x1, x->j_y1); 00435 } 00436 else { 00437 x->j_bgpattern = jgraphics_pattern_create_radial(x->j_x0, x->j_y0, x->j_r0, x->j_x1, x->j_y1, x->j_r1); 00438 } 00439 if (x->j_bgpattern) { 00440 int i; 00441 for (i=0; i < x->j_num_color_stops; i++) { 00442 t_color_stop *cs; 00443 cs = &x->j_color_stops[i]; 00444 jgraphics_pattern_add_color_stop_rgba(x->j_bgpattern, cs->offset, 00445 cs->color.red, cs->color.green, cs->color.blue, cs->color.alpha); 00446 } 00447 } 00448 jbox_redraw(&x->j_box); 00449 } 00450 00451 00452 void uitester_autoscale(t_uitester *x, long way) 00453 { 00454 x->j_autoscale = way; 00455 jbox_redraw(&x->j_box); 00456 } 00457 00458 00459 void uitester_size(t_uitester *x, long width, long height) 00460 { 00461 t_size size; 00462 size.width = (double) width; 00463 size.height = (double) height; 00464 jbox_set_size((t_object*) x, &size); 00465 } 00466 00467 00468 void uitester_param1(t_uitester *x, double value) 00469 { 00470 x->j_param1 = value; 00471 jbox_redraw(&x->j_box); 00472 } 00473 00474 00475 void uitester_param2(t_uitester *x, double value) 00476 { 00477 x->j_param2 = value; 00478 jbox_redraw(&x->j_box); 00479 } 00480 00481 00482 void uitester_param3(t_uitester *x, double value) 00483 { 00484 x->j_param3 = value; 00485 jbox_redraw(&x->j_box); 00486 } 00487 00488 00489 void uitester_param4(t_uitester *x, double value) 00490 { 00491 x->j_param4 = value; 00492 jbox_redraw(&x->j_box); 00493 } 00494 00495 00496 void uitester_setmouse(t_uitester *x, long which) 00497 { 00498 x->j_whichmouse = which; 00499 } 00500 00501 00502 void uitester_mouseenter(t_uitester *x, t_object *patcherview, t_pt pt, long modifiers) 00503 { 00504 // to set the mouse you need to have a view, thus we do it in the mouseenter 00505 if (x->j_whichmouse >= 0) { 00506 jmouse_setcursor(patcherview, (t_object*) x, (t_jmouse_cursortype) x->j_whichmouse); 00507 } 00508 else if (x->j_whichmouse == -2) { 00509 // let's try drawing a cursor 00510 t_jgraphics *g; 00511 t_jsurface *s; 00512 s = jgraphics_image_surface_create(JGRAPHICS_FORMAT_ARGB32, 32, 32); 00513 g = jgraphics_create(s); 00514 00515 jgraphics_arc(g, 16., 16., 15., 0, JGRAPHICS_PI * 2); 00516 jgraphics_close_path(g); 00517 jgraphics_set_line_width(g, 1.); 00518 jgraphics_set_source_rgba(g, .8, .1, .1, .5); 00519 jgraphics_fill_preserve(g); 00520 jgraphics_set_source_rgba(g, 0, 0, 0, 1); 00521 jgraphics_stroke(g); 00522 00523 jmouse_setcursor_surface(patcherview, (t_object*) x, s, 16, 16); 00524 00525 jgraphics_destroy(g); 00526 jgraphics_surface_destroy(s); 00527 } 00528 } 00529 00530 00531 void uitester_paint(t_uitester *x, t_object *view) 00532 { 00533 // paint the box grey 00534 t_rect rect; 00535 t_jgraphics *g; 00536 t_jmatrix autoscalematrix; 00537 double width, height; 00538 00539 g = (t_jgraphics*) patcherview_get_jgraphics(view); 00540 00541 // set up matrix 00542 jbox_get_rect_for_view((t_object*) x, view, &rect); 00543 if (x->j_autoscale) { 00544 jgraphics_matrix_init_scale(&autoscalematrix, rect.width, rect.height); 00545 jgraphics_matrix_multiply(&autoscalematrix, &autoscalematrix, &x->j_matrix); 00546 jgraphics_set_matrix(g, &autoscalematrix); 00547 } 00548 else { 00549 jgraphics_set_matrix(g, &x->j_matrix); 00550 } 00551 00552 // set up pattern 00553 if (x->j_bgpattern) { 00554 jgraphics_pattern_set_matrix(x->j_bgpattern, &x->j_pattern_matrix); 00555 jgraphics_set_source(g, x->j_bgpattern); 00556 jgraphics_paint(g); // fill bg with pattern 00557 } 00558 00559 // call paint method, if non-null 00560 if (x->j_autoscale) { 00561 width = 1; 00562 height = 1; 00563 } 00564 else { 00565 width = rect.width; 00566 height = rect.height; 00567 } 00568 jgraphics_set_source_rgba(g, 0, 0, 0, 1); 00569 00570 switch (x->j_mode) { 00571 case 0: uitester_rectangle(g, width, height); break; 00572 case 1: snippet_arc(g, width, height); break; 00573 case 2: snippet_arc_negative(g, width, height); break; 00574 case 3: snippet_curve_rectangle(g, width, height); break; 00575 case 4: snippet_curve_to(g, width, height); break; 00576 case 5: snippet_fill_and_stroke2(g, width, height); break; 00577 case 6: snippet_fill_and_stroke(g, width, height); break; 00578 case 7: snippet_gradient(g, width, height); break; 00579 case 8: snippet_path(g, width, height); break; 00580 case 9: snippet_set_line_cap(g, width, height); break; 00581 case 10: snippet_set_line_join(g, width, height); break; 00582 case 11: snippet_dash(g, width, height); break; 00583 case 12: snippet_long_lines(g, width, height); break; 00584 case 13: snippet_multi_segment_caps(g, width, height); break; 00585 case 14: snippet_self_intersect(g, width, height); break; 00586 case 15: snippet_clock(g, width, height); break; 00587 case 16: snippet_offscreen(g, width, height); break; 00588 case 17: snippet_doughnut(g, width, height); break; 00589 case 18: uitester_fill_path_with_surface(g, width, height); break; 00590 case 19: uitester_antialiased_lines(g, width, height); break; 00591 case 20: uitester_image_surface_draw(g, width, height); break; 00592 case 21: uitester_textlayout(g, width, height); break; 00593 case 22: uitester_scroll(g, width, height); break; 00594 case 23: uitester_showtext(g, width, height); break; 00595 case 24: uitester_strokeroundedrect(g, width, height); break; 00596 case 25: uitester_textlayouttransformed(g, width, height); break; 00597 case 26: uitester_svg(x, g, width, height); break; 00598 case 27: uitester_image(x, g, width, height); break; 00599 } 00600 } 00601 00602 00603 void uitester_read(t_uitester *x, t_symbol *s, short argc, t_atom *argv) 00604 { 00605 defer(x,(method)uitester_doread,s,argc,argv); 00606 } 00607 00608 00609 void uitester_doread(t_uitester *x, t_symbol *s, short argc, t_atom *argv) 00610 { 00611 char filename[MAX_PATH_CHARS]; 00612 short vol = 0; 00613 long *type,ntype,outtype; 00614 00615 ntype = 0; 00616 type = 0L; 00617 if (!argc) { 00618 if (open_dialog(filename, &vol, &outtype, type, ntype)) 00619 goto out; 00620 } else { 00621 strcpy(filename,argv->a_w.w_sym->s_name); 00622 if (!locatefile_extended(filename,&vol,&outtype,type,ntype)) 00623 { 00624 //continue 00625 } else { 00626 goto out; 00627 } 00628 } 00629 00630 if (filename && filename[0]) { 00631 long len; 00632 len = strlen(filename); 00633 if ((len > 4) && (strcmp_case_insensitive(filename+len-4, ".svg") == 0)) { 00634 // if it ends in .svg we try to load an svg file 00635 t_jsvg *svg; 00636 svg = jsvg_create_from_file(filename, vol); 00637 if (svg) { 00638 if (x->j_svg) { 00639 jsvg_destroy(x->j_svg); 00640 } 00641 x->j_svg = svg; 00642 x->j_mode = 30; 00643 } 00644 } 00645 else { 00646 // otherwise we try to load an image 00647 t_jsurface *surface; 00648 surface = jgraphics_image_surface_create_from_file(filename, vol); 00649 if (surface) { 00650 if (x->j_image) { 00651 jgraphics_surface_destroy(x->j_image); 00652 } 00653 x->j_image = surface; 00654 x->j_mode = 31; 00655 } 00656 } 00657 } 00658 00659 jbox_redraw(&x->j_box); 00660 00661 out: 00662 return; 00663 } 00664 00665 00666 void uitester_int(t_uitester *x, long which) 00667 { 00668 x->j_mode = which; 00669 jbox_redraw(&x->j_box); 00670 } 00671 00672 00673 void uitester_image(t_uitester *x, t_jgraphics *g, double width, double height) 00674 { 00675 if (x && x->j_image) { 00676 int iw, ih; 00677 snippet_normalize (g, width, height); 00678 00679 iw = jgraphics_image_surface_get_width(x->j_image); 00680 ih = jgraphics_image_surface_get_height(x->j_image); 00681 jgraphics_scale(g, 1./iw, 1./ih); 00682 jgraphics_set_source_surface(g, x->j_image, 0, 0); 00683 jgraphics_paint(g); 00684 } 00685 } 00686 00687 void uitester_svg(t_uitester *x, t_jgraphics *g, double width, double height) 00688 { 00689 if (x && x->j_svg) { 00690 double sw, sh; 00691 int count, i; 00692 snippet_normalize(g, height, height); 00693 jsvg_get_size(x->j_svg, &sw, &sh); 00694 jgraphics_scale(g, 1./sw, 1./sh); 00695 count = width / (height+5) + 1; 00696 for (i=0; i < count; i++) { 00697 jsvg_render(x->j_svg, g); 00698 jgraphics_translate(g, sw+5, 0); 00699 } 00700 } 00701 } 00702 00703 void uitester_rectangle(t_jgraphics *g, double width, double height) 00704 { 00705 snippet_normalize (g, width, height); 00706 jgraphics_rectangle(g, -.5, -.5, 1., 1.); 00707 jgraphics_set_source_rgba(g, 0, 0, 0, 1); 00708 jgraphics_stroke(g); 00709 } 00710 00711 void snippet_normalize(t_jgraphics *g, double width, double height) 00712 { 00713 jgraphics_scale(g, width, height); 00714 jgraphics_set_line_width(g, .04); 00715 } 00716 00717 void snippet_arc(t_jgraphics *cr, double width, double height) 00718 { 00719 double xc = 0.5; 00720 double yc = 0.5; 00721 double radius = 0.4; 00722 double angle1 = 45.0 * (JGRAPHICS_PI/180.0); /* angles are specified */ 00723 double angle2 = 180.0 * (JGRAPHICS_PI/180.0); /* in radians */ 00724 00725 snippet_normalize (cr, width, height); 00726 00727 jgraphics_arc (cr, xc, yc, radius, angle1, angle2); 00728 jgraphics_stroke (cr); 00729 00730 /* draw helping lines */ 00731 jgraphics_set_source_rgba (cr, 1,0.2,0.2,0.6); 00732 jgraphics_arc (cr, xc, yc, 0.05, 0, 2*JGRAPHICS_PI); 00733 jgraphics_fill (cr); 00734 jgraphics_set_line_width (cr, 0.03); 00735 jgraphics_arc (cr, xc, yc, radius, angle1, angle1); 00736 jgraphics_line_to (cr, xc, yc); 00737 jgraphics_arc (cr, xc, yc, radius, angle2, angle2); 00738 jgraphics_line_to (cr, xc, yc); 00739 jgraphics_stroke (cr); 00740 } 00741 00742 double xc = 0.5; 00743 double yc = 0.5; 00744 double radius = 0.4; 00745 double angle1 = 45.0 * (JGRAPHICS_PI/180.0); /* angles are specified */ 00746 double angle2 = 180.0 * (JGRAPHICS_PI/180.0); /* in radians */ 00747 00748 void snippet_arc_negative(t_jgraphics *cr, double width, double height) 00749 { 00750 snippet_normalize (cr, width, height); 00751 00752 jgraphics_arc_negative (cr, xc, yc, radius, angle1, angle2); 00753 jgraphics_stroke (cr); 00754 00755 /* draw helping lines */ 00756 jgraphics_set_source_rgba (cr, 1,0.2,0.2,0.6); 00757 jgraphics_arc (cr, xc, yc, 0.05, 0, 2*JGRAPHICS_PI); 00758 jgraphics_fill (cr); 00759 jgraphics_set_line_width (cr, 0.03); 00760 jgraphics_arc (cr, xc, yc, radius, angle1, angle1); 00761 jgraphics_line_to (cr, xc, yc); 00762 jgraphics_arc (cr, xc, yc, radius, angle2, angle2); 00763 jgraphics_line_to (cr, xc, yc); 00764 jgraphics_stroke (cr); 00765 } 00766 00767 void snippet_curve_rectangle(t_jgraphics *cr, double width, double height) 00768 { 00769 /* a custom shape, that could be wrapped in a function */ 00770 double x0 = 0.1, /*< parameters like jgraphics_rectangle */ 00771 y0 = 0.1, 00772 rect_width = 0.8, 00773 rect_height = 0.8, 00774 radius = 0.4; /*< and an approximate curvature radius */ 00775 00776 double x1,y1; 00777 00778 snippet_normalize (cr, width, height); 00779 00780 x1=x0+rect_width; 00781 y1=y0+rect_height; 00782 if (!rect_width || !rect_height) 00783 return; 00784 if (rect_width/2<radius) { 00785 if (rect_height/2<radius) { 00786 jgraphics_move_to (cr, x0, (y0 + y1)/2); 00787 jgraphics_curve_to (cr, x0 ,y0, x0, y0, (x0 + x1)/2, y0); 00788 jgraphics_curve_to (cr, x1, y0, x1, y0, x1, (y0 + y1)/2); 00789 jgraphics_curve_to (cr, x1, y1, x1, y1, (x1 + x0)/2, y1); 00790 jgraphics_curve_to (cr, x0, y1, x0, y1, x0, (y0 + y1)/2); 00791 } else { 00792 jgraphics_move_to (cr, x0, y0 + radius); 00793 jgraphics_curve_to (cr, x0 ,y0, x0, y0, (x0 + x1)/2, y0); 00794 jgraphics_curve_to (cr, x1, y0, x1, y0, x1, y0 + radius); 00795 jgraphics_line_to (cr, x1 , y1 - radius); 00796 jgraphics_curve_to (cr, x1, y1, x1, y1, (x1 + x0)/2, y1); 00797 jgraphics_curve_to (cr, x0, y1, x0, y1, x0, y1- radius); 00798 } 00799 } else { 00800 if (rect_height/2<radius) { 00801 jgraphics_move_to (cr, x0, (y0 + y1)/2); 00802 jgraphics_curve_to (cr, x0 , y0, x0 , y0, x0 + radius, y0); 00803 jgraphics_line_to (cr, x1 - radius, y0); 00804 jgraphics_curve_to (cr, x1, y0, x1, y0, x1, (y0 + y1)/2); 00805 jgraphics_curve_to (cr, x1, y1, x1, y1, x1 - radius, y1); 00806 jgraphics_line_to (cr, x0 + radius, y1); 00807 jgraphics_curve_to (cr, x0, y1, x0, y1, x0, (y0 + y1)/2); 00808 } else { 00809 jgraphics_move_to (cr, x0, y0 + radius); 00810 jgraphics_curve_to (cr, x0 , y0, x0 , y0, x0 + radius, y0); 00811 jgraphics_line_to (cr, x1 - radius, y0); 00812 jgraphics_curve_to (cr, x1, y0, x1, y0, x1, y0 + radius); 00813 jgraphics_line_to (cr, x1 , y1 - radius); 00814 jgraphics_curve_to (cr, x1, y1, x1, y1, x1 - radius, y1); 00815 jgraphics_line_to (cr, x0 + radius, y1); 00816 jgraphics_curve_to (cr, x0, y1, x0, y1, x0, y1- radius); 00817 } 00818 } 00819 jgraphics_close_path (cr); 00820 00821 jgraphics_set_source_rgb (cr, 0.5,0.5,1); 00822 jgraphics_fill_preserve (cr); 00823 jgraphics_set_source_rgba (cr, 0.5, 0, 0, 0.5); 00824 jgraphics_stroke (cr); 00825 } 00826 00827 void snippet_curve_to(t_jgraphics *cr, double width, double height) 00828 { 00829 double x=0.1, y=0.5; 00830 double x1=0.4, y1=0.9, 00831 x2=0.6, y2=0.1, 00832 x3=0.9, y3=0.5; 00833 snippet_normalize (cr, width, height); 00834 00835 jgraphics_move_to (cr, x, y); 00836 jgraphics_curve_to (cr, x1, y1, x2, y2, x3, y3); 00837 00838 jgraphics_stroke (cr); 00839 00840 jgraphics_set_source_rgba (cr, 1,0.2,0.2,0.6); 00841 jgraphics_set_line_width (cr, 0.03); 00842 jgraphics_move_to (cr,x,y); jgraphics_line_to (cr,x1,y1); 00843 jgraphics_move_to (cr,x2,y2); jgraphics_line_to (cr,x3,y3); 00844 jgraphics_stroke (cr); 00845 } 00846 00847 void snippet_fill_and_stroke2(t_jgraphics *cr, double width, double height) 00848 { 00849 snippet_normalize (cr, width, height); 00850 00851 jgraphics_move_to (cr, 0.5, 0.1); 00852 jgraphics_line_to (cr, 0.9, 0.9); 00853 jgraphics_rel_line_to (cr, -0.4, 0.0); 00854 jgraphics_curve_to (cr, 0.2, 0.9, 0.2, 0.5, 0.5, 0.5); 00855 jgraphics_close_path (cr); 00856 00857 jgraphics_move_to (cr, 0.25, 0.1); 00858 jgraphics_rel_line_to (cr, 0.2, 0.2); 00859 jgraphics_rel_line_to (cr, -0.2, 0.2); 00860 jgraphics_rel_line_to (cr, -0.2, -0.2); 00861 jgraphics_close_path (cr); 00862 00863 jgraphics_set_source_rgb (cr, 0, 0, 1); 00864 jgraphics_fill_preserve (cr); 00865 jgraphics_set_source_rgb (cr, 0, 0, 0); 00866 jgraphics_stroke (cr); 00867 } 00868 00869 void snippet_fill_and_stroke(t_jgraphics *cr, double width, double height) 00870 { 00871 snippet_normalize (cr, width, height); 00872 00873 jgraphics_move_to (cr, 0.5, 0.1); 00874 jgraphics_line_to (cr, 0.9, 0.9); 00875 jgraphics_rel_line_to (cr, -0.4, 0.0); 00876 jgraphics_curve_to (cr, 0.2, 0.9, 0.2, 0.5, 0.5, 0.5); 00877 jgraphics_close_path (cr); 00878 00879 jgraphics_set_source_rgb (cr, 0, 0, 1); 00880 jgraphics_fill_preserve (cr); 00881 jgraphics_set_source_rgb (cr, 0, 0, 0); 00882 jgraphics_stroke (cr); 00883 } 00884 00885 void snippet_gradient(t_jgraphics *cr, double width, double height) 00886 { 00887 t_jpattern *pat; 00888 00889 snippet_normalize (cr, width, height); 00890 00891 pat = jgraphics_pattern_create_linear (0.0, 0.0, 0.0, 1.0); 00892 jgraphics_pattern_add_color_stop_rgba (pat, 1, 0, 0, 0, 1); 00893 jgraphics_pattern_add_color_stop_rgba (pat, 0, 1, 1, 1, 1); 00894 jgraphics_rectangle (cr, 0, 0, 1, 1); 00895 jgraphics_set_source (cr, pat); 00896 jgraphics_fill (cr); 00897 jgraphics_pattern_destroy (pat); 00898 00899 pat = jgraphics_pattern_create_radial (0.45, 0.4, 0.1, 00900 0.4, 0.4, 0.5); 00901 jgraphics_pattern_add_color_stop_rgba (pat, 0, 1, 1, 1, 1); 00902 jgraphics_pattern_add_color_stop_rgba (pat, 1, 0, 0, 0, 1); 00903 jgraphics_set_source (cr, pat); 00904 jgraphics_arc (cr, 0.5, 0.5, 0.3, 0, 2 * JGRAPHICS_PI); 00905 jgraphics_fill (cr); 00906 jgraphics_pattern_destroy (pat); 00907 } 00908 00909 void snippet_path(t_jgraphics *cr, double width, double height) 00910 { 00911 snippet_normalize (cr, width, height); 00912 jgraphics_move_to (cr, 0.5, 0.1); 00913 jgraphics_line_to (cr, 0.9, 0.9); 00914 jgraphics_rel_line_to (cr, -0.4, 0.0); 00915 jgraphics_curve_to (cr, 0.2, 0.9, 0.2, 0.5, 0.5, 0.5); 00916 00917 jgraphics_stroke (cr); 00918 } 00919 00920 void snippet_set_line_cap(t_jgraphics *cr, double width, double height) 00921 { 00922 snippet_normalize (cr, width, height); 00923 jgraphics_set_line_width (cr, 0.12); 00924 jgraphics_set_line_cap (cr, JGRAPHICS_LINE_CAP_BUTT); /* default */ 00925 jgraphics_move_to (cr, 0.25, 0.2); jgraphics_line_to (cr, 0.25, 0.8); 00926 jgraphics_stroke (cr); 00927 jgraphics_set_line_cap (cr, JGRAPHICS_LINE_CAP_ROUND); 00928 jgraphics_move_to (cr, 0.5, 0.2); jgraphics_line_to (cr, 0.5, 0.8); 00929 jgraphics_stroke (cr); 00930 jgraphics_set_line_cap (cr, JGRAPHICS_LINE_CAP_SQUARE); 00931 jgraphics_move_to (cr, 0.75, 0.2); jgraphics_line_to (cr, 0.75, 0.8); 00932 jgraphics_stroke (cr); 00933 00934 /* draw helping lines */ 00935 jgraphics_set_source_rgb (cr, 1,0.2,0.2); 00936 jgraphics_set_line_width (cr, 0.01); 00937 jgraphics_move_to (cr, 0.25, 0.2); jgraphics_line_to (cr, 0.25, 0.8); 00938 jgraphics_move_to (cr, 0.5, 0.2); jgraphics_line_to (cr, 0.5, 0.8); 00939 jgraphics_move_to (cr, 0.75, 0.2); jgraphics_line_to (cr, 0.75, 0.8); 00940 jgraphics_stroke (cr); 00941 } 00942 00943 void snippet_set_line_join(t_jgraphics *cr, double width, double height) 00944 { 00945 snippet_normalize (cr, width, height); 00946 jgraphics_set_line_width (cr, 0.16); 00947 jgraphics_move_to (cr, 0.3, 0.33); 00948 jgraphics_rel_line_to (cr, 0.2, -0.2); 00949 jgraphics_rel_line_to (cr, 0.2, 0.2); 00950 jgraphics_set_line_join (cr, JGRAPHICS_LINE_JOIN_MITER); /* default */ 00951 jgraphics_stroke (cr); 00952 00953 jgraphics_move_to (cr, 0.3, 0.63); 00954 jgraphics_rel_line_to (cr, 0.2, -0.2); 00955 jgraphics_rel_line_to (cr, 0.2, 0.2); 00956 jgraphics_set_line_join (cr, JGRAPHICS_LINE_JOIN_BEVEL); 00957 jgraphics_stroke (cr); 00958 00959 jgraphics_move_to (cr, 0.3, 0.93); 00960 jgraphics_rel_line_to (cr, 0.2, -0.2); 00961 jgraphics_rel_line_to (cr, 0.2, 0.2); 00962 jgraphics_set_line_join (cr, JGRAPHICS_LINE_JOIN_ROUND); 00963 jgraphics_stroke (cr); 00964 } 00965 00966 void snippet_dash(t_jgraphics *cr, double width, double height) 00967 { 00968 double dashes[] = {0.20, /* ink */ 00969 0.05, /* skip */ 00970 0.05, /* ink */ 00971 0.05 /* skip*/ 00972 }; 00973 int ndash = sizeof (dashes)/sizeof(dashes[0]); 00974 // double offset = -0.2; 00975 double offset = 0; 00976 00977 snippet_normalize (cr, width, height); 00978 00979 jgraphics_set_dash (cr, dashes, ndash, offset); 00980 00981 jgraphics_move_to (cr, 0.5, 0.1); 00982 jgraphics_line_to (cr, 0.9, 0.9); 00983 jgraphics_rel_line_to (cr, -0.4, 0.0); 00984 jgraphics_curve_to (cr, 0.2, 0.9, 0.2, 0.5, 0.5, 0.5); 00985 00986 jgraphics_stroke (cr); 00987 } 00988 00989 void snippet_long_lines(t_jgraphics *cr, double width, double height) 00990 { 00991 snippet_normalize (cr, width, height); 00992 00993 jgraphics_move_to (cr, 0.1, -50); 00994 jgraphics_line_to (cr, 0.1, 50); 00995 jgraphics_set_source_rgb (cr, 1, 0 ,0); 00996 jgraphics_stroke (cr); 00997 00998 jgraphics_move_to (cr, 0.2, -60); 00999 jgraphics_line_to (cr, 0.2, 60); 01000 jgraphics_set_source_rgb (cr, 1, 1 ,0); 01001 jgraphics_stroke (cr); 01002 01003 jgraphics_move_to (cr, 0.3, -70); 01004 jgraphics_line_to (cr, 0.3, 70); 01005 jgraphics_set_source_rgb (cr, 0, 1 ,0); 01006 jgraphics_stroke (cr); 01007 01008 jgraphics_move_to (cr, 0.4, -80); 01009 jgraphics_line_to (cr, 0.4, 80); 01010 jgraphics_set_source_rgb (cr, 0, 0 ,1); 01011 jgraphics_stroke (cr); 01012 } 01013 01014 void snippet_multi_segment_caps(t_jgraphics *cr, double width, double height) 01015 { 01016 snippet_normalize (cr, width, height); 01017 01018 jgraphics_move_to (cr, 0.2, 0.3); 01019 jgraphics_line_to (cr, 0.8, 0.3); 01020 01021 jgraphics_move_to (cr, 0.2, 0.5); 01022 jgraphics_line_to (cr, 0.8, 0.5); 01023 01024 jgraphics_move_to (cr, 0.2, 0.7); 01025 jgraphics_line_to (cr, 0.8, 0.7); 01026 01027 jgraphics_set_line_width (cr, 0.12); 01028 jgraphics_set_line_cap (cr, JGRAPHICS_LINE_CAP_ROUND); 01029 jgraphics_stroke (cr); 01030 } 01031 01032 void snippet_self_intersect(t_jgraphics *cr, double width, double height) 01033 { 01034 snippet_normalize (cr, width, height); 01035 01036 jgraphics_move_to (cr, 0.3, 0.3); 01037 jgraphics_line_to (cr, 0.7, 0.3); 01038 01039 jgraphics_line_to (cr, 0.5, 0.3); 01040 jgraphics_line_to (cr, 0.5, 0.7); 01041 01042 jgraphics_set_line_width (cr, 0.22); 01043 jgraphics_set_line_cap (cr, JGRAPHICS_LINE_CAP_ROUND); 01044 jgraphics_set_line_join (cr, JGRAPHICS_LINE_JOIN_ROUND); 01045 jgraphics_stroke (cr); 01046 } 01047 01048 void snippet_clock(t_jgraphics *cr, double width, double height) 01049 { 01050 /* store the current time */ 01051 // time_t rawtime; 01052 // time(&rawtime); 01053 // struct tm * timeinfo; 01054 /* In newer versions of Visual Studio localtime(..) is deprecated. Use localtime_s instead. See MSDN. */ 01055 // timeinfo = localtime(&rawtime); 01056 01057 /* compute the angles of the indicators of our clock */ 01058 // double minutes = timeinfo->tm_min * M_PI / 30; 01059 // double hours = timeinfo->tm_hour * M_PI / 6; 01060 // double seconds= timeinfo->tm_sec * M_PI / 30; 01061 double minutes = 50. * JGRAPHICS_PI / 30; 01062 double hours = 2.8 * JGRAPHICS_PI / 6; 01063 double seconds = 30. * JGRAPHICS_PI / 30; 01064 01065 /* Normalize our context to a (width, height) of (1.0, 1.0) by scaling by our window width and height. */ 01066 jgraphics_scale (cr, width, height); 01067 01068 /* draw the entire context white. */ 01069 jgraphics_set_source_rgba (cr, 1, 1, 1, 1); 01070 jgraphics_paint(cr); 01071 01072 /* who doesn't want all those nice line settings :) */ 01073 jgraphics_set_line_cap (cr, JGRAPHICS_LINE_CAP_ROUND); 01074 jgraphics_set_line_width(cr, 0.1); 01075 01076 /* translate to the center of the rendering context and draw a black clock outline */ 01077 jgraphics_set_source_rgba (cr, 0, 0, 0, 1); 01078 jgraphics_translate(cr, 0.5, 0.5); 01079 jgraphics_arc(cr, 0, 0, 0.4, 0, JGRAPHICS_PI * 2); 01080 jgraphics_stroke(cr); 01081 01082 /* draw a white dot on the current second. */ 01083 jgraphics_set_source_rgba (cr, 1, 1, 1, 0.6); 01084 jgraphics_arc(cr, sin(seconds) * 0.4, -cos(seconds) * 0.4, 0.05, 0, JGRAPHICS_PI * 2); 01085 jgraphics_fill(cr); 01086 01087 /* draw the minutes indicator */ 01088 jgraphics_set_source_rgba (cr, 0.2, 0.2, 1, 0.6); 01089 jgraphics_move_to(cr, 0, 0); 01090 jgraphics_line_to(cr, sin(minutes) * 0.4, -cos(minutes) * 0.4); 01091 jgraphics_stroke (cr); 01092 01093 /* draw the hours indicator */ 01094 jgraphics_move_to(cr, 0, 0); 01095 jgraphics_line_to(cr, sin(hours) * 0.2, -cos(hours) * 0.2); 01096 jgraphics_stroke (cr); 01097 } 01098 01099 void snippet_offscreen(t_jgraphics *cr, double width, double height) 01100 { 01101 t_jsurface *surface; 01102 t_jpattern *pattern; 01103 t_jmatrix matrix; 01104 t_jgraphics *g; 01105 01106 snippet_normalize(cr, width, height); 01107 01108 jgraphics_set_source_rgb(cr, 1,0,0); 01109 jgraphics_paint(cr); 01110 01111 surface = jgraphics_image_surface_create(JGRAPHICS_FORMAT_ARGB32, width, height); 01112 g = jgraphics_create(surface); 01113 snippet_normalize(g, width, height); 01114 01115 jgraphics_set_source_rgb(g, 0, 1, 0); 01116 jgraphics_rectangle(g, .2, .2, .6, .6); 01117 jgraphics_fill_preserve(g); 01118 jgraphics_set_source_rgb(g, 0, 0, 1); 01119 jgraphics_stroke(g); 01120 01121 pattern = jgraphics_pattern_create_for_surface(surface); 01122 jgraphics_get_matrix(cr, &matrix); 01123 jgraphics_pattern_set_matrix(pattern, &matrix); 01124 jgraphics_set_source(cr, pattern); 01125 01126 jgraphics_paint_with_alpha(cr, .5); 01127 01128 jgraphics_pattern_destroy(pattern); 01129 jgraphics_surface_destroy(surface); 01130 jgraphics_destroy(g); 01131 } 01132 01133 01134 void snippet_doughnut(t_jgraphics *cr, double width, double height) 01135 { 01136 // How do you draw a doughnut (shape with a hole in it)? 01137 // Here is one way. 01138 jgraphics_set_source_rgb(cr, .2, .8, 0); 01139 #if 0 01140 // Here's one way 01141 jgraphics_arc(cr, width*.5, height*.5, width*.5, 0, JGRAPHICS_2PI); 01142 jgraphics_arc_negative(cr, width*.5, height*.5, width*.25, JGRAPHICS_2PI, 0); 01143 jgraphics_close_path(cr); 01144 #elif 0 01145 // Or, setting the winding to even/odd seems to work with this 01146 jgraphics_set_fill_rule(cr, JGRAPHICS_FILL_RULE_EVEN_ODD); 01147 jgraphics_arc(cr, width*.5, height*.5, width*.5, 0, JGRAPHICS_2PI); 01148 jgraphics_arc(cr, width*.5, height*.5, width*.25, 0, JGRAPHICS_2PI); 01149 jgraphics_close_path(cr); 01150 #elif 0 01151 // Or, setting the winding to even/odd seems to work with this 01152 jgraphics_set_fill_rule(cr, JGRAPHICS_FILL_RULE_EVEN_ODD); 01153 jgraphics_ovalarc(cr, width*.5, height*.5, width*.5, height*.5, 0, JGRAPHICS_2PI); 01154 jgraphics_arc(cr, width*.666, height*.6, width*.25, 0, JGRAPHICS_2PI); 01155 jgraphics_close_path(cr); 01156 #elif 0 01157 // Ah, but this works too, and is much simpler. The key is the EVEN_ODD winding rule. 01158 jgraphics_set_fill_rule(cr, JGRAPHICS_FILL_RULE_EVEN_ODD); 01159 jgraphics_rectangle(cr, 0, 0, width, height); 01160 jgraphics_rectangle(cr, width*.25, height*.25, width*.5, height*.5); 01161 #elif 0 01162 jgraphics_set_fill_rule(cr, JGRAPHICS_FILL_RULE_EVEN_ODD); 01163 jgraphics_ellipse(cr, 0, 0, width, height); 01164 jgraphics_ellipse(cr, width*.25, height*.5, height*.4, height*.4); 01165 #else // this demonstrates it better 01166 jgraphics_set_fill_rule(cr, JGRAPHICS_FILL_RULE_EVEN_ODD); 01167 jgraphics_rectangle(cr, 0., 0., 100., 100.); 01168 jgraphics_rectangle(cr, width-100., height-100., 100., 100.); 01169 #endif 01170 jgraphics_fill(cr); 01171 } 01172 01173 void uitester_fill_path_with_surface(t_jgraphics *g, double width, double height) 01174 { 01175 t_jgraphics *gs; 01176 t_jsurface *s; 01177 double i,ratio,r2,r3,inc; 01178 double pixels = 5; 01179 double y=0; 01180 01181 s = jgraphics_image_surface_create(JGRAPHICS_FORMAT_ARGB32, width, height); 01182 gs = jgraphics_create(s); 01183 01184 jgraphics_scale(gs,width,width); 01185 inc = pixels / width; 01186 jgraphics_set_line_width(gs, inc); 01187 i = 0.; 01188 ratio = height / width; 01189 r2 = ratio * 2; 01190 r3 = (width / height) * 2; 01191 if (r3 > r2) 01192 r2 = r3; 01193 while (i < r2) { 01194 jgraphics_move_to(gs,i,y); 01195 jgraphics_rel_line_to(gs, -ratio, ratio); 01196 01197 jgraphics_stroke(gs); 01198 i += (inc * 2.5); 01199 } 01200 01201 jgraphics_set_source_surface(g, s, 0, 0); 01202 jgraphics_rectangle_rounded(g, 0, 0, width, height, width, height); 01203 jgraphics_fill_preserve(g); 01204 jgraphics_set_source_rgba(g, 0, 0, 0, 1.); 01205 jgraphics_set_line_width(g, 2.); 01206 jgraphics_stroke(g); 01207 01208 jgraphics_destroy(gs); 01209 jgraphics_surface_destroy(s); 01210 } 01211 01212 void uitester_antialiased_lines(t_jgraphics *g, double width, double height) 01213 { 01214 // snippet_normalize(g, width, height); 01215 jgraphics_set_source_rgb(g, 0, 0, 0); 01216 jgraphics_set_line_width(g, 1.); 01217 jgraphics_move_to(g, width * .5 + .5, 0); 01218 jgraphics_line_to(g, width * .5 + .5, height); 01219 jgraphics_stroke(g); 01220 jgraphics_move_to(g, width * .5 + 10, 0); 01221 jgraphics_line_to(g, width * .5 + 10, height); 01222 jgraphics_stroke(g); 01223 jgraphics_move_to(g, width * .5 + 10, 0); 01224 jgraphics_line_to(g, width * .5 + 100, height); 01225 jgraphics_stroke(g); 01226 jgraphics_move_to(g, width * .5 - .5 - 10, 0); 01227 jgraphics_line_to(g, width * .5 - .5 - 100, height); 01228 jgraphics_stroke(g); 01229 } 01230 01231 void uitester_image_surface_draw(t_jgraphics *g, double width, double height) 01232 { 01233 t_jsurface *surf; 01234 t_jgraphics *og; 01235 t_rect srcRect; 01236 t_rect destRect; 01237 int i,j; 01238 01239 surf = jgraphics_image_surface_create(JGRAPHICS_FORMAT_ARGB32, 256, 256); 01240 og = jgraphics_create(surf); 01241 01242 for (i=0; i<256; i++) { 01243 for (j=0; j<256; j++) { 01244 t_jrgba myjrgba; 01245 myjrgba.red = myjrgba.green = 0.1; 01246 myjrgba.blue = myjrgba.alpha = 1.; 01247 01248 jgraphics_image_surface_set_pixel(surf, j, i, myjrgba); 01249 } 01250 } 01251 01252 jgraphics_destroy(og); 01253 01254 srcRect.x = destRect.x = srcRect.y = destRect.y = 0; 01255 srcRect.width = srcRect.height = 256; 01256 destRect.width = width; 01257 destRect.height = height; 01258 jgraphics_image_surface_draw(g, surf, srcRect, destRect); 01259 } 01260 01261 void uitester_textlayout(t_jgraphics *g, double width, double height) 01262 { 01263 t_jtextlayout* mytxt; 01264 t_jfont* myfont; 01265 t_jrgba rgba; 01266 01267 const char *text = "testing the textlayout stuff"; 01268 double boxwidth = width / 3.; 01269 double boxheight = height / 3.; 01270 01271 double xpos[9] = { 0., 0., 0., 1./3., 1./3., 1./3., 2./3., 2./3., 2./3. }; 01272 double ypos[9] = { 0., 1./3., 2./3., 0., 1./3., 2./3., 0., 1./3., 2./3. }; 01273 int i; 01274 01275 t_jgraphics_text_justification justification[9] = 01276 { 01277 (t_jgraphics_text_justification) (JGRAPHICS_TEXT_JUSTIFICATION_LEFT | JGRAPHICS_TEXT_JUSTIFICATION_TOP), 01278 (t_jgraphics_text_justification) (JGRAPHICS_TEXT_JUSTIFICATION_LEFT | JGRAPHICS_TEXT_JUSTIFICATION_VCENTERED), 01279 (t_jgraphics_text_justification) (JGRAPHICS_TEXT_JUSTIFICATION_LEFT | JGRAPHICS_TEXT_JUSTIFICATION_BOTTOM), 01280 (t_jgraphics_text_justification) (JGRAPHICS_TEXT_JUSTIFICATION_HCENTERED | JGRAPHICS_TEXT_JUSTIFICATION_TOP), 01281 (t_jgraphics_text_justification) (JGRAPHICS_TEXT_JUSTIFICATION_HCENTERED | JGRAPHICS_TEXT_JUSTIFICATION_VCENTERED), 01282 (t_jgraphics_text_justification) (JGRAPHICS_TEXT_JUSTIFICATION_HCENTERED | JGRAPHICS_TEXT_JUSTIFICATION_BOTTOM), 01283 (t_jgraphics_text_justification) (JGRAPHICS_TEXT_JUSTIFICATION_RIGHT | JGRAPHICS_TEXT_JUSTIFICATION_TOP), 01284 (t_jgraphics_text_justification) (JGRAPHICS_TEXT_JUSTIFICATION_RIGHT | JGRAPHICS_TEXT_JUSTIFICATION_VCENTERED), 01285 (t_jgraphics_text_justification) (JGRAPHICS_TEXT_JUSTIFICATION_RIGHT | JGRAPHICS_TEXT_JUSTIFICATION_BOTTOM) 01286 }; 01287 01288 t_jgraphics_textlayout_flags flags = (t_jgraphics_textlayout_flags) (0); 01289 01290 mytxt = jtextlayout_create(); 01291 myfont = jfont_create("Arial", JGRAPHICS_FONT_SLANT_NORMAL, JGRAPHICS_FONT_WEIGHT_NORMAL, 16.); 01292 01293 for (i=0; i < 9; i++) 01294 { 01295 jtextlayout_set(mytxt, text, myfont, xpos[i] * width, ypos[i] * height, boxwidth, boxheight, justification[i], flags); 01296 rgba.red = 0.; 01297 rgba.green = 0.; 01298 rgba.blue = 0.; 01299 rgba.alpha = 1.; 01300 01301 jgraphics_rectangle(g, xpos[i] * width, ypos[i] * height, boxwidth, boxheight); 01302 jgraphics_set_source_rgb(g, 0., 0., 0.); 01303 jgraphics_stroke(g); 01304 01305 jtextlayout_settextcolor(mytxt, &rgba); 01306 jtextlayout_draw(mytxt, g); 01307 } 01308 01309 jtextlayout_destroy(mytxt); 01310 jfont_destroy(myfont); 01311 } 01312 01313 void uitester_scroll(t_jgraphics *g, double width, double height) 01314 { 01315 static t_jsurface *surf = NULL,*surf2; 01316 t_jgraphics *og; 01317 static long begin = 1; 01318 static long count = 0; 01319 t_rect src, dst; 01320 01321 if (!surf) { 01322 surf = jgraphics_image_surface_create(JGRAPHICS_FORMAT_ARGB32, width, height); 01323 surf2 = jgraphics_image_surface_create(JGRAPHICS_FORMAT_ARGB32, width, height); 01324 } 01325 og = jgraphics_create(surf); 01326 jgraphics_set_source_rgba(og,0.7,0.7,0.7,1.); 01327 if (begin) { 01328 begin = 0; 01329 jgraphics_rectangle_fill_fast(og,0,0,width,height); 01330 } 01331 jgraphics_image_surface_scroll(surf,0, 0, width, height, 1, 0, NULL); 01332 01333 jgraphics_set_source_rgba(og, 0.9, 0.9, 0.9, 1.); 01334 jgraphics_rectangle_fill_fast(og, 0, 0, 1, height); 01335 jgraphics_set_source_rgba(og, 0.3, 0.3, 0.3, 1.); 01336 jgraphics_rectangle_fill_fast(og, 0, count++, 1, 1); 01337 01338 src.x = src.y = 0; 01339 src.width = width; 01340 src.height = height; 01341 dst = src; 01342 jgraphics_image_surface_draw(g, surf, src, dst); 01343 jgraphics_destroy(og); 01344 } 01345 01346 void uitester_showtext(t_jgraphics *g, double width, double height) 01347 { 01348 jgraphics_set_source_rgb(g, 0.7, 0.7, 0.7); 01349 jgraphics_set_line_width(g, 2.0); 01350 jgraphics_move_to(g, 5.0, 13.0); 01351 jgraphics_select_font_face(g, "Arial", JGRAPHICS_FONT_SLANT_NORMAL, JGRAPHICS_FONT_WEIGHT_BOLD); 01352 jgraphics_set_font_size(g, 8.0); 01353 jgraphics_show_text(g, "f"); 01354 } 01355 01356 void uitester_strokeroundedrect(t_jgraphics *g, double width, double height) 01357 { 01358 const int roundedness = 10; 01359 const int linewidth = 50; 01360 01361 jgraphics_set_source_rgb(g, 0., 0., 0.); 01362 jgraphics_rectangle_rounded(g, linewidth, linewidth, width - linewidth*2, height - linewidth*2, roundedness, roundedness); 01363 jgraphics_set_line_width(g, linewidth); 01364 jgraphics_stroke(g); 01365 } 01366 01367 void uitester_textlayouttransformed(t_jgraphics *g, double width, double height) 01368 { 01369 t_jtextlayout* mytxt; 01370 t_jfont* myfont; 01371 t_jrgba rgba; 01372 01373 const char *text = "herding cats"; 01374 double boxwidth = width; 01375 double boxheight = height / 2.; 01376 t_jgraphics_text_justification justification = JGRAPHICS_TEXT_JUSTIFICATION_CENTERED; 01377 mytxt = jtextlayout_create(); 01378 myfont = jfont_create("Arial", JGRAPHICS_FONT_SLANT_NORMAL, JGRAPHICS_FONT_WEIGHT_NORMAL, 16.); 01379 01380 jtextlayout_set(mytxt, text, myfont, 0, 0, boxwidth, boxheight, justification, (t_jgraphics_textlayout_flags) 0); 01381 rgba.red = 0.; 01382 rgba.green = 0.; 01383 rgba.blue = 0.; 01384 rgba.alpha = 1.; 01385 01386 jtextlayout_settextcolor(mytxt, &rgba); 01387 jtextlayout_draw(mytxt, g); 01388 01389 //jgraphics_scale(g, 0, boxheight); 01390 jgraphics_translate(g, 0, boxheight); 01391 jtextlayout_draw(mytxt, g); 01392 01393 jtextlayout_destroy(mytxt); 01394 jfont_destroy(myfont); 01395 }
Copyright © 2008, Cycling '74