Posted By


xxtjaxx on 11/05/09

Tagged


Statistics


Viewed 1428 times
Favorited by 0 user(s)

Cairo "Hello World!" examble


/ Published in: C++
Save to your folder(s)

A small examble on how to make a hello world image from cairo.


Copy this code and paste it in your HTML
  1. #include<cairo.h>
  2. #include<iostream>
  3. #include<string>
  4.  
  5. using std::string;
  6. using namespace std;
  7.  
  8.  
  9. int main(int argc, char* argv[])
  10. {
  11. int x,y,width,height;
  12. float size_font;
  13.  
  14. x = 20;
  15. y = 20;
  16. width = 200;
  17. height = 40;
  18. size_font=20.0;
  19. string file_path(argv[1]);
  20.  
  21.  
  22. cairo_surface_t *surface;
  23. cairo_t *cr;
  24.  
  25.  
  26. surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
  27. cr = cairo_create (surface);
  28.  
  29. cairo_text_extents_t te;
  30. cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
  31. cairo_select_font_face (cr, "Georgia",
  32. CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
  33. cairo_set_font_size (cr, size_font);
  34. cairo_text_extents (cr, "hello world!", &te);
  35. cairo_move_to (cr, x ,y);
  36. cairo_show_text (cr, "hello world!");
  37. cairo_fill(cr);
  38. cairo_surface_write_to_png(surface , file_path.c_str());
  39.  
  40. return 0;
  41. }
  42. /*
  43. Compile it like this:
  44.  
  45. Makefile:
  46. ------------------------------------------------------
  47. CC=g++
  48. #Compiler Optionen
  49. C_Flags_Wall= -Wall
  50. C_Flags_DEBUG= -DEBUG
  51. #C_Flags += -g
  52. #Macros describing realtive filepaths for
  53. #the main function file
  54. MAIN=./main.cpp
  55. #the projects header directory
  56. PROJECT_HEADER=./header
  57. #the cairo header directory
  58. CAIRO_CFLAGS=$(shell pkg-config --cflags cairo)
  59. CAIRO_LIBS=$(shell pkg-config --libs cairo)
  60. #actual compiling
  61. main: $(MAIN)
  62.   $(CC) $(C_Flags_Wall) $(C_FLAGS_DEBUG) -o ../bin/main $(MAIN) $(CAIRO_CFLAGS) $(CAIRO_LIBS)
  63. ------------------------------------------------------
  64. */

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.