Cairo Summary 1

Source: Internet
Author: User
Tags cairo vector graphics library

Cairo will become the future of Linux 2D plotting
Note: currently, "theory is not practiced" ^_^
======================================
1. What is Cairo.
======================================
Cairo is a vector graphics library that supports multiple outputs. That is to say, Cairo is a tool library for drawing pictures. It can draw pictures from multiple devices.

For example, Cairo can output to PNG, PDF, PS, xlib, xcb, and

Output to Win32, and later output to SVG

======================================
2. Understanding Cairo:
======================================
If you simply understand Cairo in code, it may be confusing. Therefore, we need to understand Cairo as follows:
Cairo is a paint brush. You can set the color, Font, and alpha for this paint brush, or use this paint brush to draw any image.

. Just like a painter painting, you can use canvas, Xuan paper, or other materials. Cairo, which can be used in

PNG, PS, PDF, or xlib.

Example:
/* Use Cairo to draw a PNG Image */
# Include <Cairo. h>
# Include <cairo-png.h>
# Include
# Include
Int main ()
{
File * file;
File = fopen ("a.png", "W"); // open a file with the name a.png.

Cairo_t * Cr; // declare a paint brush
Cr = cairo_create (); // creates a paint brush.
Cairo_set_target_png (Cr, file, cairo_format_argb32, 400,400); // sets the layout, hosts file, a.png

Cairo_set_rgb_color (Cr, 0); // set the paint brush color, that is, red, green, and blue. Set it to green here.
Cairo_rectangle (Cr, 10, 10, 200,200); // draw a square with the position starting from the coordinate (10, 10), width 200, height 200
Cairo_fill (CR); // fill, the color used is of course the color set above.
Cairo_move_to (Cr, 250,200); // move the paint brush to (250,200)
Cairo_select_font (Cr, "dongwen -- song ",
Cairo_font_slant_normal,
Cairo_font_weight_normal); // set a font for Cairo. The font name is dongwen -- song, which is not italic or bold.
Cairo_scale_font (Cr, 60); // scale the font to 60 times
Cairo_show_text (Cr, "Hello World"); // draw a string
Cairo_destroy (CR); // destroy the paint brush
Fclose (File); // close the file
}

Save it as t1.c, and then compile it using the following command:
Gcc-O T1 t1.c-lcairo-I/usr/include/Cairo

Run./T1 to generate a PNG image file. You can open it with a tool.

======================================
3. Common functions:
======================================
Operation paint brush:
Create a paint brush.
Cairo_create ();

Add reference count 1.
Cairo_reference ();

Reduce the reference count by 1. If 0 is returned, all resources are released.
Cairo_destroy ();

Save the current Cairo paint brush.
Cairo_save ();

Restore the last saved Cairo paint brush, that is, it must be paired with cairo_save.
Cairo_restore ();
------------
Descriptive functions:
Draw a path by line (this function draws a "Hollow" graph ).
Cairo_stoke (CR );

Fill the items (a path) You have drawn in the specified color (this function draws a "solid" graph ).
Cairo_fill (CR );
--------------
Drawing:
Square:
Cairo_rectangle (Cr, 100,100 );

Define an arc through three points:
Cairo_curve_to (Cr, X1, Y1, X2, Y2, X3, Y3)

Draw an arc clockwise, circle X, Y, radius, start radians from, end radians.
For example, from 0, m_pi (in math. h), it can be a semi-circle (opening will not be filled with a straight line ). From 0 to 2 * m_pi is an integral circle.
Cairo_arc (Cr, X, Y, radius, from, );

Draw an arc counterclockwise.
Cairo_arc_negative (Cr, X, Y, radius, from, );
----------------
Font related:
/* Temporarily unavailable, so it is skipped */
Create a font and return cairo_font_t *.
Cairo_ft_font_create ();
Set the font and accept the cairo_t * And cairo_font_t * parameters *.
Cairo_set_font ();
Select Nimbus sans L. Select a font based on your system's FC-list.
Cairo_select_font (Cr, "Nimbus sans L", 0, 0 );
Scale the font. The font size is set to 60. Although the font size is scale, the font size is used inside Cairo.
Cairo_scale_font (Cr, 60 );
Show Hehe, Blue, 60, nimbus sans l font
Cairo_show_text (Cr, "hehe ");
----------------
Draw path:
It seems that if path is used, you do not need to use restore to prevent additional straight lines from being drawn when the paint brush moves. See the example below.
Cairo_new_path (CR );
Cairo_close_path (CR );

Display font, similar to cairo_show_text, but the font can be treated as path, so any special effects and transformations can be made.
Cairo_text_path (char * utf8 );
---------------
Special Drawing:
Rotate the function and accept radians as parameters (removed using m_pi)
Note that when used, it affects the global, that is, after the rotation, everything is rotated.
Therefore, make cairo_save before rotation, and then perform cairo_restore after the required rotation.
Cairo_rotate (Cr, angle );

Zoom in and out the image with caution. Use cairo_save and cairo_restore. This function can be used on different surfaces.
X refers to the width scaling factor, and Y refers to the height scaling factor.
Cairo_scale (Cr, x, y );

Relative coordinates refer to the origin point where your current point is taken as the coordinate, and then move or draw at the specified position.
Cairo_rel_move_to ();
Cairo_rel_line_to ();
Cairo_rel_curve_to ();

======================================
4. Examples of common code snippets:
======================================
Drawing:
 
Draw a square (stroke ):
Cairo_rectangle (Cr, 100,100 );
Cairo_set_line_width (Cr, 10)
Cairo_set_rgb_color (Cr, 0, 0, 1 );
Cairo_stroke (CR );

Draw a diagonal line (from 100,100 to) (stroke ):
Cairo_move_to (Cr, 0, 0 );
Cairo_line_to (Cr, 100,100 );
Cairo_set_rgb_color (Cr, 1, 0, 0 );
Cairo_stroke (CR );

Draw a hollow circle (stroke ):
Cairo_arc (Cr, 100,100, 0, 2 * m_pi );
Cairo_set_line_width (Cr, 3 );
Cairo_set_rgb_color (Cr, 1, 0, 0 );
Cairo_stroke (CR );
 
A red solid circle (fill ):
Cairo_arc (Cr, 100,100, 100,100, 2 * m_pi); // draw a circle with a radius of 40 (an integral circle) at the center of the coordinate ).
Cairo_set_rgb_color (Cr, 1, 0, 0); // red
Cairo_fill (CR); // a red solid circle is obtained at this time.
----------------
Writing:
/* Move the paint brush to the 60x60 coordinate.
Note: When you draw a text, the coordinates are the coordinates in the lower left corner of the text,
If you move to the 0, 0 coordinates at this time, your text will actually be painted out of the canvas of X. */
Cairo_move_to (Cr, 60, 60 );
// Select the positive Nimbus sans L. Select a font based on your system's FC-list.
Cairo_select_font (Cr, "Nimbus sans L", 0, 0 );
// Set it to 60. Although it is scale, Cairo uses the font size internally.
Cairo_scale_font (Cr, 60 );
// Display Hehe, Blue, 60, nimbus sans l font
Cairo_show_text (Cr, "hehe ");

Make a hollow word:
// Textpath is used, which cannot be done through cairo_show_text.
Cairo_text_path ("hello ");
Cairo_set_line_width (Cr, 1 );
Cairo_stroke (CR );
 

----------------
Relative coordinates:
Cairo_move_to (Cr, 10, 10 );
// At this time, it will be at 20 or 20.
Cairo_rel_move_to (Cr, 10, 10 );

----------------
Save paint brush:
/* General process:
1) Save the paint brush cairo_save (CR );
2) use a paint brush... causes the paint brush to change...
3) restore the paint brush cairo_restore (CR );
To maintain integrity, the Code is a bit large but does not affect the essence.
*/
Cairo_t * Cr;
Cr = cairo_create (); // declare and create a paint brush
File * file;
File = fopen ("c.png", "W ");
// Set the target to a PNG file. The size is 400x400 and the color is 32 bits.
Cairo_set_target_png (Cr, file, cairo_format_argb32, 400,400 );

// $ Save_1 Save the paint brush. At this time, the paint brush is clean without any operation.
Cairo_save (CR );

// Draw a rectangle, blue
Cairo_rectangle (Cr, 200,200 );
Cairo_set_rgb_color (Cr, 0, 0, 1 );
Cairo_set_line_width (Cr, 4 );
Cairo_stroke (CR );

// Write
Cairo_move_to (Cr, 60, 60 );
Cairo_select_font (Cr, "Nimbus sans L", 0, 0 );
Cairo_scale_font (Cr, 60 );
Cairo_show_text (Cr, "hehe ");

// $ Restore to save_1. At this time, the paint brush is clean.
Cairo_restore (CR );
// Save the save_2 and use this clean brush later.
Cairo_save (CR );.

// Write
Cairo_move_to (Cr, 70, 70 );
Cairo_set_rgb_color (Cr, 1, 0, 0 );
Cairo_scale_font (Cr, 60 );
// If the restore is not performed and the scale_font is performed, the font is 60x60.
Cairo_show_text (Cr, "hehe ");

// Resume to save_2. At this time, the paint brush is still in the initial state.
Cairo_restore (CR );

/* Draws a big semi-circular curve.
If you have not recovered to the initial state of the paint brush, then the Cairo paint brush is located at 70, 70, and will draw a straight line with one

This is the direction of the big semi-circle curve, from the current coordinate (70, 70) to the beginning of the big semi-circle, and then to the end of the big semi-circle, that is, Shun

Clockwise.
*/
Cairo_set_rgb_color (Cr, 0, 0 );
Cairo_arc (Cr, 100,100, 60, 0, 1.5 * m_pi );
Cairo_set_line_width (Cr, 3 );
Cairo_set_line_cap (Cr, cairo_line_cap_round );
Cairo_stroke (CR );

Cairo_destroy (CR );
Cairo_destroy (CR );
Fclose (File );
--------------------
Path:
/* If the current point is (100,100), the circle at () will first move to the dot, resulting in a straight line.
* The path can solve this problem as follows:
*/
Cairo_move_to (70, 70 );
Cairo_new_path (CR );
Cairo_set_rgb_color (Cr, 0, 0 );
Cairo_arc (Cr, 100,100, 60, 0, 1.5 * m_pi );
Cairo_set_line_width (Cr, 3 );
Cairo_set_line_cap (Cr, cairo_line_cap_round );
Cairo_stroke (CR );
Cairo_close_path (CR );
----------------
Rotation:
Cairo_t * Cr;
Cr = cairo_create ();
Cairo_set_target _.....;
// Rotate for 30 degrees clockwise. If it is-m_pi/6, it is 30 degrees clockwise.
Cairo_rotate (Cr, m_pi/6 );
Cairo_rectangle (Cr, 100,100,); // Square
Cairo_set_rgb_color (Cr, 1, 0, 0 );
Cairo_set_line_width (Cr, 10 );
Cairo_stroke (CR );

Scaling: Draw a Parallelogram
Cairo_t * Cr;
Cr = cairo_create ();
Cairo_set_target _.....;
// The horizontal direction remains unchanged, and the vertical direction is reduced to half.
Cairo_scale (Cr, 1, 0.5 );
// Rotate for 30 degrees clockwise. If it is-m_pi/6, it is 30 degrees clockwise.
Cairo_rotate (Cr, m_pi/6 );
Cairo_rectangle (Cr, 10, 10, 100,100); // a rectangle after scale.
Cairo_set_rgb_color (Cr, 1, 0, 0 );
Cairo_set_line_width (Cr, 10 );
/* First, the width of the upper and lower sides is half the original width. At the same time, the image is not a right-angle rectangle, but a parallelogram.
Because:
After scale, all the transformations that affect the vertical direction become half of the original one. Therefore, each side should have been rotated by 30

Degrees, and now both the upper and lower sides have become the degree of rotation half, that is, 15 degrees.
Of course, if you want to rotate a rectangle, just use cairo_rectangle to draw a rectangle.
Note that cairo_rotate only affects the post content. Therefore, you cannot finish the post content in rotate.
*/
Cairo_stroke (CR );
======================================
Refer:
Http://hi.baidu.com/litaosmile/blog/item/68a89a8b092f4f1ac9fc7a66.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.