PORTING_GUIDE 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. ...-----=======-----...
  2. Cairo 1.0 Porting Guide
  3. ...-----=======-----...
  4. Here are some notes on more easily porting cairo_code from cairo 0.4
  5. to cairo 1.0. It is sorted roughly in order of importance, (the items
  6. near the top are expected to affect the most people).
  7. Automated API renamings
  8. =======================
  9. There have been a lot of simple renamings where the functionality is
  10. the same but the name of the symbol is different. We have provided a
  11. script to automate the conversion of these symbols. It can be found
  12. within the cairo distribution in:
  13. util/cairo-api-update
  14. This script is used by installing it somewhere on your PATH, and the
  15. running it and providing the names of your source files on the command
  16. line. For example:
  17. cairo-api-update *.[ch]
  18. The script will first save backup copies of each file (renamed with a
  19. .bak extension) and then will perform all of the simple renamings.
  20. For your benefit, the script also produces messages giving filenames
  21. and line numbers for several of the manual API updates that you will
  22. need to perform as described below.
  23. Manual API changes
  24. ==================
  25. This section of the porting guide describes changes you will have to
  26. manually make to your source code. In addition to the information in
  27. this guide, the cairo-api-update script will notify you of some of
  28. these issues as described above.
  29. Cairo's deprecation warnings
  30. ----------------------------
  31. Also, if your compiler provides warnings for implicit declarations of
  32. functions, (eg. "gcc -Wall"), then simply attempting to compile your
  33. program will cause cairo to generate messages intended to guide you
  34. through the porting process.
  35. For example, if you neglect to update an old call to
  36. cairo_set_target_drawable, you might see an error message as follows:
  37. foo.c:10: warning: implicit declaration of function
  38. ‘cairo_set_target_drawable_DEPRECATED_BY_cairo_xlib_surface_create’
  39. This message is indicating to you that the deprecatd function
  40. cairo_set_target_drawable appears in your program foo.c on line 10,
  41. and you should rewrite your program to call cairo_xlib_surface_create
  42. instead.
  43. The remainder of this porting guide is arranged as a set of common
  44. code patterns that appear in old (cairo-0.4) code and how it should be
  45. transformed to new (cairo-0.5) code.
  46. cairo_create
  47. ------------
  48. Was: cr = cairo_create ();
  49. cairo_set_target_foo (cr, args);
  50. /* draw */
  51. cairo_destroy (cr);
  52. Now: cairo_surface_t *surface;
  53. surface = cairo_foo_surface_create (args);
  54. cr = cairo_create (surface);
  55. /* draw */
  56. cairo_destroy (cr);
  57. cairo_surface_destroy (surface);
  58. Or: cairo_surface_t *surface;
  59. surface = cairo_foo_surface_create (args);
  60. cr = cairo_create (surface);
  61. cairo_surface_destroy (surface);
  62. /* draw */
  63. cairo_destroy (cr);
  64. NOTE: Many of the cairo_foo_surface_create functions accept the
  65. identical arguments as the the old cairo_set_target_foo
  66. functions, (minus the cairo_t*), making this transformation
  67. quite easy. One notable exception is cairo_set_target_drawable
  68. which, when it becomes cairo_xlib_surface_create must pickup new
  69. arguments for the Visual*, the width, and the height.
  70. cairo_set_alpha (1)
  71. -------------------
  72. Was: cairo_set_rgb_color (cr, red, green, blue);
  73. cairo_set_alpha (cr, alpha);
  74. Now: cairo_set_source_rgba (cr, red, green, blue, alpha);
  75. cairo_show_surface
  76. ------------------
  77. Was: cairo_show_surface (cr, surface, width, height);
  78. Now: cairo_set_source_surface (cr, surface, x, y);
  79. cairo_paint (cr);
  80. NOTE: The type signatures of cairo_show_surface and cairo_set_source
  81. are the same, but pay attention that cairo_show_surface required
  82. the width and height, while cairo_set_source_surface requires
  83. the X,Y location to where the surface will be placed.
  84. cairo_set_alpha (2)
  85. -------------------
  86. Was: cairo_set_alpha (cr, alpha);
  87. cairo_show_surface (cr, surface, width, height);
  88. Now: cairo_set_source_surface (cr, surface, x, y);
  89. cairo_paint_with_alpha (cr, alpha);
  90. filling and stroking
  91. --------------------
  92. Was: cairo_save (cr);
  93. /* set fill color */
  94. cairo_fiill (cr);
  95. cairo_restore (cr);
  96. /* set stroke color */
  97. cairo_stroke (cr);
  98. Now: /* set fill color */
  99. cairo_fill_preserve (cr);
  100. /* set stroke color */
  101. cairo_stroke (cr);
  102. NOTE: The current path is no longer saved/restored by
  103. cairo_save/cairo_restore. This can lead to some subtle
  104. surprises, so look out.
  105. cairo_matrix_t
  106. --------------
  107. Was: cairo_matrix_t *matrix;
  108. matrix = cairo_matrix_create ();
  109. /* Do stuff with matrix */
  110. cairo_matrix_destroy (matrix);
  111. Now: cairo_matrix_t matrix;
  112. cairo_matrix_init_identity (&matrix);
  113. /* Do stuff with &matrix */
  114. NOTE: If you are really lazy, you can still use a cairo_matrix_t* and
  115. avoid putting the &matrix all over by just replacing
  116. cairo_matrix_create() with malloc() and cairo_matrix_destroy()
  117. with free(). That's not as nice, and you still need to be
  118. careful to see if you need to initialize it to an identity
  119. matrix as cairo_matrix_create() did for you.
  120. Rendering to a temporary surface
  121. --------------------------------
  122. Was: cairo_save (cr);
  123. {
  124. cairo_set_target_surface (cr, temporary);
  125. /* draw through cr onto temporary */
  126. }
  127. cairo_restore (cr);
  128. /* use temporary as source on cr */
  129. Now: {
  130. cr2 = cairo_create (temporary);
  131. /* draw through cr2 onto temporary */
  132. cairo_destory (cr2);
  133. }
  134. /* use temporary as source on cr */
  135. NOTE: Having to create another cairo_t is a bit annoying, but having
  136. to invent a new name for it is just awful, (imagine a deeply
  137. nested version of this code). Fortunately, the style above is
  138. just a stop-gap measure until the new group API comes along.
  139. Iterating over a path
  140. ---------------------
  141. Was: cairo_current_path (cr,
  142. my_move_to,
  143. my_line_to,
  144. my_curve_to,
  145. my_close_path,
  146. closure);
  147. Now: int i;
  148. cairo_path_t *path;
  149. cairo_path_data_t *data;
  150. path = cairo_copy_path (cr);
  151. for (i=0; i < path->num_data; i += path->data[i].header.length) {
  152. data = &path->data[i];
  153. switch (data->header.type) {
  154. case CAIRO_PATH_MOVE_TO:
  155. my_move_to (closure, data[1].point.x, data[1].point.y);
  156. break;
  157. case CAIRO_PATH_LINE_TO:
  158. my_line_to (closure, data[1].point.x, data[1].point.y);
  159. break;
  160. case CAIRO_PATH_CURVE_TO:
  161. my_curve_to (closure, data[1].point.x, data[1].point.y,
  162. data[2].point.x, data[2].point.y,
  163. data[3].point.x, data[3].point.y);
  164. break;
  165. case CAIRO_PATH_CLOSE_PATH:
  166. my_close_path (closure);
  167. break;
  168. }
  169. }
  170. cairo_path_destroy (path);
  171. NOTE: This version makes it looks like the new form is a _lot_ more
  172. verbose than the old version. But realize that the old version
  173. required the support of 4 additional functions. The new approach
  174. allows great flexibility including the ability to inline the
  175. entire operation within the switch statement when appropriate.
  176. Erasing a surface to transparent
  177. --------------------------------
  178. Was: cairo_set_rgb_color (cr, 0., 0., 0.);
  179. cairo_set_alpha (cr, 0.)
  180. cairo_set_operator (cr, CAIRO_OPERATOR_SRC);
  181. cairo_rectangle (cr, 0., 0., surface_width, surface_height);
  182. cairo_fill (cr);
  183. or: cairo_set_rgb_color (cr, 0., 0., 0.);
  184. cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
  185. cairo_rectangle (cr, 0., 0., surface_width, surface_height);
  186. cairo_fill (cr);
  187. Now: cairo_set_source_rgba (cr, 0., 0., 0., 0.);
  188. cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
  189. cairo_paint (cr);
  190. or: cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
  191. cairo_paint (cr);
  192. NOTE: Using cairo_rectangle and fill would still work just fine. It's
  193. just a lot more convenient to use cairo_paint now, (particularly
  194. as it doesn't require you to even know what the bounds of the
  195. target surface are).
  196. Drawing to a PNG file
  197. ---------------------
  198. Was: file = fopen (filename, "w");
  199. cr = cairo_create ();
  200. cairo_set_target_png (cr, file, format, width, height);
  201. /* draw image */
  202. cairo_destroy (cr);
  203. fclose (file);
  204. Now: surface = cairo_image_surface_create (format, width, height);
  205. cr = cairo_create (surface);
  206. /* draw image */
  207. cairo_surface_write_to_png (surface, filename);
  208. cairo_destroy (cr);
  209. cairo_surface_destroy (surface);
  210. NOTE: The png backend is gone. So there is no cairo_png_surface_create
  211. to take the place of cairo_set_target_png. And notice that we
  212. used an image surface here, but it is just as easy to use
  213. cairo_surface_write_to_png with an xlib or other surface, (but
  214. not PDF at the moment). This is one of the big advantages of
  215. this approach as opposed to a PNG surface.