head 1.6; access; symbols RELEASE_1_0_2:1.5 BRANCH_1_0:1.5.0.2 RELEASE_1_0_0:1.5 BRANCH_SCALED_GLYPH:1.4.0.2 RELEASE_0_9_2:1.4 RELEASE_0_9_0:1.4 SNAPSHOT_0_6_0:1.4 SNAPSHOT_0_5_2:1.4 SNAPSHOT_0_5_1:1.3; locks; strict; comment @# @; 1.6 date 2005.12.16.11.02.35; author biesi; state Exp; branches; next 1.5; commitid 605343a29e244567; 1.5 date 2005.08.23.19.48.17; author cworth; state Exp; branches; next 1.4; commitid 4c07430b7d7e4567; 1.4 date 2005.07.07.08.15.21; author cworth; state Exp; branches; next 1.3; commitid 20c442cce4974567; 1.3 date 2005.06.03.22.56.52; author cworth; state Exp; branches; next 1.2; commitid 217c42a0e0324567; 1.2 date 2005.06.03.22.54.40; author cworth; state Exp; branches; next 1.1; commitid 211b42a0dfab4567; 1.1 date 2005.06.03.21.51.57; author cworth; state Exp; branches; next ; commitid 10b242a0d0f94567; desc @@ 1.6 log @2005-12-16 Christian Biesinger reviewed by: cworth * CODING_STYLE: * src/cairo-atsui-font.c: (_cairo_atsui_font_old_show_glyphs): * src/cairo-clip-private.h: * src/cairo-clip.c: (_cairo_clip_combine_to_surface): * src/cairo-ft-font.c: (_cairo_ft_show_glyphs): * src/cairo-gstate-private.h: * src/cairo-gstate.c: (_cairo_gstate_init), (_cairo_gstate_init_copy), (_cairo_gstate_set_operator), (_cairo_gstate_get_operator), (_cairo_gstate_paint), (_cairo_operator_bounded_by_mask), (_cairo_operator_bounded_by_source), (_cairo_gstate_clip_and_composite_with_mask), (_cairo_gstate_clip_and_composite_combine), (_cairo_gstate_clip_and_composite), (_cairo_gstate_mask), (_cairo_gstate_stroke), (_composite_trap_region), (_composite_traps_draw_func), (_cairo_surface_clip_and_composite_trapezoids), (_cairo_gstate_fill), (_cairo_gstate_show_glyphs): * src/cairo-image-surface.c: (_pixman_operator), (_cairo_image_surface_composite), (_cairo_image_surface_fill_rectangles), (_cairo_image_surface_composite_trapezoids): * src/cairo-meta-surface-private.h: * src/cairo-meta-surface.c: (_cairo_meta_surface_paint), (_cairo_meta_surface_mask), (_cairo_meta_surface_stroke), (_cairo_meta_surface_fill), (_cairo_meta_surface_show_glyphs), (_cairo_meta_surface_replay): * src/cairo-pdf-surface.c: (_cairo_pdf_surface_composite), (_cairo_pdf_surface_fill_rectangles), (_cairo_pdf_surface_fill), (_cairo_pdf_surface_composite_trapezoids), (_cairo_pdf_surface_old_show_glyphs): * src/cairo-ps-surface.c: (_cairo_ps_surface_composite), (_cairo_ps_surface_fill_rectangles), (_cairo_ps_surface_composite_trapezoids), (_cairo_ps_surface_old_show_glyphs), (_cairo_ps_surface_paint), (_cairo_ps_surface_mask), (_cairo_ps_surface_stroke), (_cairo_ps_surface_fill), (_cairo_ps_surface_show_glyphs), (operator_always_opaque), (operator_always_translucent), (color_operation_needs_fallback), (pattern_operation_needs_fallback), (_ps_output_composite), (_ps_output_fill_rectangles), (_ps_output_composite_trapezoids), (_ps_output_old_show_glyphs), (_ps_output_paint), (_ps_output_mask), (_ps_output_stroke), (_ps_output_fill), (_ps_output_show_glyphs): * src/cairo-scaled-font.c: (_cairo_scaled_font_show_glyphs): * src/cairo-surface.c: (_fallback_composite), (_cairo_surface_composite), (_cairo_surface_fill_rectangle), (_cairo_surface_fill_region), (_fallback_fill_rectangles), (_cairo_surface_fill_rectangles), (_fallback_paint), (_cairo_surface_paint), (_cairo_surface_mask_draw_func), (_fallback_mask), (_cairo_surface_mask), (_fallback_stroke), (_cairo_surface_stroke), (_fallback_fill), (_cairo_surface_fill), (_fallback_composite_trapezoids), (_cairo_surface_composite_trapezoids), (_cairo_surface_old_show_glyphs_draw_func), (_fallback_show_glyphs), (_cairo_surface_show_glyphs), (_cairo_surface_old_show_glyphs): * src/cairo-svg-surface.c: (_cairo_svg_surface_composite), (emit_operator), (_cairo_svg_surface_fill), (_cairo_svg_surface_composite_trapezoids), (_cairo_svg_surface_stroke), (_cairo_svg_surface_old_show_glyphs): * src/cairo-win32-font.c: (_cairo_win32_scaled_font_show_glyphs): * src/cairo-win32-surface.c: (_cairo_win32_surface_composite), (categorize_solid_dest_operator), (_cairo_win32_surface_fill_rectangles): * src/cairo-xcb-surface.c: (_render_operator), (_cairo_xcb_surface_composite), (_cairo_xcb_surface_fill_rectangles), (_cairo_xcb_surface_composite_trapezoids): * src/cairo-xlib-surface.c: (_operator_needs_alpha_composite), (_categorize_composite_operation), (_recategorize_composite_operation), (_render_operator), (_cairo_xlib_surface_composite), (_cairo_xlib_surface_fill_rectangles), (_cairo_xlib_surface_composite_trapezoids), (_cairo_xlib_surface_old_show_glyphs8), (_cairo_xlib_surface_old_show_glyphs16), (_cairo_xlib_surface_old_show_glyphs32), (_cairo_xlib_surface_old_show_glyphs): * src/cairoint.h: * test/get-and-set.c: (settings_set), (settings_get), (settings_equal): Globally rename "operator" to "op", to allow writing backends in C++ @ text @Cairo coding style. This document is intended to be a short description of the preferred coding style for the cairo source code. Good style requires good taste, which means this can't all be reduced to automated rules, and there are exceptions. We want the code to be easy to understand and maintain, and consistent style plays an important part in that, even if some of the specific details seem trivial. If nothing else, this document gives a place to put consistent answers for issues that would otherwise be arbitrary. Most of the guidelines here are demonstrated by examples, (which means this document is quicker to read than it might appear given its length). Most of the examples are positive examples that you should imitate. The few negative examples are clearly marked with a comment of /* Yuck! */. Please don't submit code to cairo that looks like any of these. Indentation ----------- Each new level is indented 4 more spaces than the previous level: if (condition) do_something (); This may be achieved with space characters or a combination of tab characters and space characters. It may not be achieved with tab characters exclusively (see below). Tab characters -------------- The tab character must always be interepreted according to its traditional meaning: Advance to the next column which is a multiple of 8. With this definition, even levels of indentation can be achieved with a sequence of tab characters, while odd levels of indentation may begin with a sequence of tab character but must end with 4 space characters. Some programmers have been misled by certain text editors into thinking that 4-space indentation can be achieved with tab characters exclusively by changing the meaning of tab character to be "advance to the next column which is a multiple of 4". Code formatted in this way, making an assumption of a fictitious 4-character-tab will not be accepted into cairo. The rationale here is that tabs are used in the code for lining things up other than indentation, (see the Whitespace section below), and changing the interpretation of tab from its traditional meaning will break this alignment. Braces ------ Most of the code in cairo uses bracing in the style of K&R: if (condition) { do_this (); do_that (); } else { do_the_other (); } but some of the code uses an alternate style: if (condition) { do_this (); do_that (); } else { do_the_other (); } and that seems just fine. We won't lay down any strict rule on this point, (though there should be some local consistency). If you came here hoping to find some guidance, then use the first form above. If all of the substatements of an if statement are single statements, the optional braces should not usually appear: if (condition) do_this (); else do_that (); But the braces are mandatory when mixing single statement and compound statements in the various clauses. For example, do not do this: if (condition) { do_this (); do_that (); } else /* Yuck! */ do_the_other (); And of course, there are exceptions for when the code just looks better with the braces: if (condition) { /* Note that we have to be careful here. */ do_something_dangerous (with_care); } if (condition && other_condition && yet_another) { do_something (); } And note that this last example also shows a situation in which the opening brace really needs to be on its own line. The following looks awful: if (condition && other_condition && yet_another) { /* Yuck! */ do_something (); } As we said above, legible code that is easy to understand and maintain is the goal, not adherence to strict rules. Whitespace ---------- Separate logically distinct chunks with a single newline. This obviously applies between functions, but also applies within a function or block and can even be used to good effect within a structure definition: struct _cairo_gstate { cairo_operator_t op; double tolerance; /* stroke style */ double line_width; cairo_line_cap_t line_cap; cairo_line_join_t line_join; double miter_limit; cairo_fill_rule_t fill_rule; double *dash; int num_dashes; double dash_offset; ... } Use a single space before a left parenthesis, except where the standard will not allow it, (eg. when defining a parameterized macro). Don't eliminate whitespace just because things would still fit on one line. This breaks the expected visual structure of the code making it much harder to read and understand: if (condition) foo (); else bar (); /* Yuck! */ As a special case of the bracing and whitespace guidelines, function definitions should always take the following form: void my_function (argument) { do_my_things (); } And function prototypes should similarly have the return type (and associated specifiers and qualifiers) on a line above the function, so that the function name is flush left. Break up long lines (> ~80 characters) and use whitespace to align things nicely. For example the arguments in a long list to a function call should all be aligned with each other: align_function_arguments (argument_the_first, argument_the_second, argument_the_third); And as a special rule, in a function prototype, (as well as in the definition), whitespace should be inserted between the parameter types and names so that the names are aligned: void align_parameter_names_in_prototypes (const char *char_star_arg, int int_arg, double *double_star_arg, double double_arg); Note that parameters with a * prefix are aligned one character to the left so that the actual names are aligned. Managing nested blocks ---------------------- Long blocks that are deeply nested make the code very hard to read. Fortunately such blocks often indicate logically distinct chunks of functionality that are begging to be split into their own functions. Please listen to the blocks when they beg. In other cases, gratuitous nesting comes about because the primary functionality gets buried in a nested block rather than living at the primary level where it belongs. Consider the following: foo = malloc (sizeof (foo_t)); if (foo) { /* Yuck! */ ... /* lots of code to initialize foo */ ... return SUCCESS; } return FAILURE; This kind of gratuitous nesting can be avoided by following a pattern of handling exceptional cases early and returning: foo = malloc (sizeof (foo_t)); if (foo == NULL) return FAILURE; ... /* lots of code to initialize foo */ ... return SUCCESS; The return statement is often the best thing to use in a pattern like this. If it's not available due to additional nesting above which require some cleanup after the current block, then consider splitting the current block into a new function before using goto. @ 1.5 log @ 2005-08-23 Carl Worth * CODING_STYLE: Revise the description of indentation and tabs to be clear (I hope). @ text @d134 1 a134 1 cairo_operator_t operator; @ 1.4 log @ * CODING_STYLE: Add a missing word. * ROADMAP: Note that the BadMatch bug has been resolved. @ text @d27 22 a48 4 Spaces or tabs (or a combination) can be used in indentation, but tabs must always be interpreted as 8 spaces. Code using single tabs for all indentation (expecting people to interpret tabs as 4 spaces) will not be accepted in cairo. d52 2 a53 1 changing the interpretation of tab from 8 characters will break this. @ 1.3 log @ * CODING_STYLE: Fix spelling errors. @ text @d60 2 a61 2 point, (though there should be some local). If you came here hoping to find some guidance, then use the first form above. @ 1.2 log @ * CODING_STYLE: Fix Freudian unwrapped line in paragraph describing why long lines should be wrapped. @ text @d71 1 a71 1 But the braces are mandatory when mixing single statement and compund d95 1 a95 1 And note that this last example also shows a situtation in which the d172 1 a172 1 double duble_arg); @ 1.1 log @ * CODING_STYLE: Add CODING_STYLE document to standardize on some style issues. * src/cairo-atsui-font.c: * src/cairo-cache.c: * src/cairo-ft-font.c: * src/cairo-glitz-surface.c: * src/cairo-gstate.c: * src/cairo-matrix.c: * src/cairo-pattern.c: * src/cairo-pdf-surface.c: * src/cairo-spline.c: * src/cairo-wideint.c: * src/cairo-win32-font.c: * src/cairo-xlib-surface.c: Standardize brace handling around all else clauses according to new CODING_STYLE guidelines. @ text @d156 3 a158 2 Break up long line (> ~80 characters) and use whitespace to align things nicely. For example the arguments in a long list to a function call should all be aligned with each other: @