From 145f991920a6850c4720c7f9b8c3ed52a2f39a80 Mon Sep 17 00:00:00 2001 From: Nicola Fontana Date: Sat, 13 Feb 2010 18:43:03 +0100 Subject: [PATCH] [CpmlPrimitive] Reordered functions Retained the order of the "virtual table" while declaring and defining the functions. --- cpml/cpml-primitive.c | 317 +++++++++++++++++++++++++------------------------- cpml/cpml-primitive.h | 25 ++-- 2 files changed, 169 insertions(+), 173 deletions(-) diff --git a/cpml/cpml-primitive.c b/cpml/cpml-primitive.c index e38558dd..a0654c04 100644 --- a/cpml/cpml-primitive.c +++ b/cpml/cpml-primitive.c @@ -85,6 +85,25 @@ static void dump_cairo_point (const cairo_path_data_t *path_data); /** + * cpml_primitive_type_get_n_points: + * @type: a primitive type + * + * Gets the number of points required to identify the @type primitive. + * + * Returns: the number of points or %0 on errors + **/ +size_t +cpml_primitive_type_get_n_points(CpmlPrimitiveType type) +{ + const _CpmlPrimitiveClass *class_data = get_class_from_type(type); + + if (class_data == NULL) + return 0; + + return class_data->n_points; +} + +/** * cpml_primitive_copy: * @primitive: the destination #CpmlPrimitive * @src: the source #CpmlPrimitive @@ -239,165 +258,6 @@ cpml_primitive_get_point(const CpmlPrimitive *primitive, int n_point) } /** - * cpml_primitive_to_cairo: - * @primitive: a #CpmlPrimitive - * @cr: the destination cairo context - * - * Renders a single @primitive to the @cr cairo context. - * As a special case, if the primitive is a #CPML_CLOSE, an - * equivalent line is rendered, because a close path left alone - * is not renderable. - * - * Also a #CPML_ARC primitive is treated specially, as it is not - * natively supported by cairo and has its own rendering API. - **/ -void -cpml_primitive_to_cairo(const CpmlPrimitive *primitive, cairo_t *cr) -{ - cairo_path_t path; - cairo_path_data_t *path_data; - - cairo_move_to(cr, primitive->org->point.x, primitive->org->point.y); - - switch (primitive->data->header.type) { - - case CPML_CLOSE: - path_data = cpml_primitive_get_point(primitive, -1); - cairo_line_to(cr, path_data->point.x, path_data->point.y); - break; - - case CPML_ARC: - cpml_arc_to_cairo(primitive, cr); - break; - - default: - path.status = CAIRO_STATUS_SUCCESS; - path.data = primitive->data; - path.num_data = primitive->data->header.length; - cairo_append_path(cr, &path); - break; - } -} - -/** - * cpml_primitive_dump: - * @primitive: a #CpmlPrimitive - * @org_also: whether to output also the origin coordinates - * - * Dumps info on the specified @primitive to stdout: useful for - * debugging purposes. If @org_also is 1, a #CPML_MOVE to the - * origin is prepended to the data otherwise the - * org field is not used. - **/ -void -cpml_primitive_dump(const CpmlPrimitive *primitive, cairo_bool_t org_also) -{ - const cairo_path_data_t *data; - int type; - size_t n, n_points; - - data = primitive->data; - type = data->header.type; - n_points = cpml_primitive_get_n_points(primitive); - if (n_points == 0) { - printf("Unhandled primitive type (%d)\n", type); - return; - } - - /* Dump the origin movement, if requested */ - if (org_also) { - printf("Move to "); - dump_cairo_point(primitive->org); - printf("\n"); - } - - switch (type) { - - case CPML_LINE: - printf("Line to "); - break; - - case CPML_ARC: - printf("Arc to "); - break; - - case CPML_CURVE: - printf("Curve to "); - break; - - case CPML_CLOSE: - printf("Path close"); - break; - - default: - printf("Unknown primitive (type = %d)", type); - break; - } - - for (n = 1; n < n_points; ++n) - dump_cairo_point(cpml_primitive_get_point(primitive, n)); - - printf("\n"); -} - -/** - * cpml_primitive_put_intersections_with_segment: - * @primitive: a #CpmlPrimitive - * @segment: a #CpmlSegment - * @n_dest: maximum number of intersection pairs to return - * @dest: the destination buffer of #CpmlPair - * - * Computes the intersections between @segment and @primitive by - * sequentially scanning the primitives in @segment and looking - * for their intersections with @primitive. - * - * If the intersections are more than @n_dest, only the first - * @n_dest pairs are stored. - * - * Returns: the number of intersections found - **/ -size_t -cpml_primitive_put_intersections_with_segment(const CpmlPrimitive *primitive, - const CpmlSegment *segment, - size_t n_dest, CpmlPair *dest) -{ - CpmlPrimitive portion; - size_t found; - - cpml_primitive_from_segment(&portion, (CpmlSegment *) segment); - found = 0; - - while (found < n_dest) { - found += cpml_primitive_put_intersections(&portion, primitive, - n_dest-found, dest+found); - if (!cpml_primitive_next(&portion)) - break; - } - - return found; -} - - -/** - * cpml_primitive_type_get_n_points: - * @type: a primitive type - * - * Gets the number of points required to identify the @type primitive. - * - * Returns: the number of points or %0 on errors - **/ -size_t -cpml_primitive_type_get_n_points(CpmlPrimitiveType type) -{ - const _CpmlPrimitiveClass *class_data = get_class_from_type(type); - - if (class_data == NULL) - return 0; - - return class_data->n_points; -} - -/** * cpml_primitive_get_length: * @primitive: a #CpmlPrimitive * @@ -586,6 +446,43 @@ cpml_primitive_put_intersections(const CpmlPrimitive *primitive, } /** + * cpml_primitive_put_intersections_with_segment: + * @primitive: a #CpmlPrimitive + * @segment: a #CpmlSegment + * @n_dest: maximum number of intersection pairs to return + * @dest: the destination buffer of #CpmlPair + * + * Computes the intersections between @segment and @primitive by + * sequentially scanning the primitives in @segment and looking + * for their intersections with @primitive. + * + * If the intersections are more than @n_dest, only the first + * @n_dest pairs are stored. + * + * Returns: the number of intersections found + **/ +size_t +cpml_primitive_put_intersections_with_segment(const CpmlPrimitive *primitive, + const CpmlSegment *segment, + size_t n_dest, CpmlPair *dest) +{ + CpmlPrimitive portion; + size_t found; + + cpml_primitive_from_segment(&portion, (CpmlSegment *) segment); + found = 0; + + while (found < n_dest) { + found += cpml_primitive_put_intersections(&portion, primitive, + n_dest-found, dest+found); + if (!cpml_primitive_next(&portion)) + break; + } + + return found; +} + +/** * cpml_primitive_offset: * @primitive: a #CpmlPrimitive * @offset: distance for the computed offset primitive @@ -665,6 +562,108 @@ cpml_primitive_join(CpmlPrimitive *primitive, CpmlPrimitive *primitive2) return 1; } +/** + * cpml_primitive_to_cairo: + * @primitive: a #CpmlPrimitive + * @cr: the destination cairo context + * + * Renders a single @primitive to the @cr cairo context. + * As a special case, if the primitive is a #CPML_CLOSE, an + * equivalent line is rendered, because a close path left alone + * is not renderable. + * + * Also a #CPML_ARC primitive is treated specially, as it is not + * natively supported by cairo and has its own rendering API. + **/ +void +cpml_primitive_to_cairo(const CpmlPrimitive *primitive, cairo_t *cr) +{ + cairo_path_t path; + cairo_path_data_t *path_data; + + cairo_move_to(cr, primitive->org->point.x, primitive->org->point.y); + + switch (primitive->data->header.type) { + + case CPML_CLOSE: + path_data = cpml_primitive_get_point(primitive, -1); + cairo_line_to(cr, path_data->point.x, path_data->point.y); + break; + + case CPML_ARC: + cpml_arc_to_cairo(primitive, cr); + break; + + default: + path.status = CAIRO_STATUS_SUCCESS; + path.data = primitive->data; + path.num_data = primitive->data->header.length; + cairo_append_path(cr, &path); + break; + } +} + +/** + * cpml_primitive_dump: + * @primitive: a #CpmlPrimitive + * @org_also: whether to output also the origin coordinates + * + * Dumps info on the specified @primitive to stdout: useful for + * debugging purposes. If @org_also is 1, a #CPML_MOVE to the + * origin is prepended to the data otherwise the + * org field is not used. + **/ +void +cpml_primitive_dump(const CpmlPrimitive *primitive, cairo_bool_t org_also) +{ + const cairo_path_data_t *data; + int type; + size_t n, n_points; + + data = primitive->data; + type = data->header.type; + n_points = cpml_primitive_get_n_points(primitive); + if (n_points == 0) { + printf("Unhandled primitive type (%d)\n", type); + return; + } + + /* Dump the origin movement, if requested */ + if (org_also) { + printf("Move to "); + dump_cairo_point(primitive->org); + printf("\n"); + } + + switch (type) { + + case CPML_LINE: + printf("Line to "); + break; + + case CPML_ARC: + printf("Arc to "); + break; + + case CPML_CURVE: + printf("Curve to "); + break; + + case CPML_CLOSE: + printf("Path close"); + break; + + default: + printf("Unknown primitive (type = %d)", type); + break; + } + + for (n = 1; n < n_points; ++n) + dump_cairo_point(cpml_primitive_get_point(primitive, n)); + + printf("\n"); +} + static const _CpmlPrimitiveClass * get_class_from_type(CpmlPrimitiveType type) diff --git a/cpml/cpml-primitive.h b/cpml/cpml-primitive.h index f96a0958..c1f3d7f1 100644 --- a/cpml/cpml-primitive.h +++ b/cpml/cpml-primitive.h @@ -41,7 +41,8 @@ struct _CpmlPrimitive { cairo_path_data_t *data; }; - +size_t cpml_primitive_type_get_n_points + (CpmlPrimitiveType type); CpmlPrimitive * cpml_primitive_copy (CpmlPrimitive *primitive, const CpmlPrimitive *src); CpmlPrimitive * cpml_primitive_from_segment(CpmlPrimitive *primitive, @@ -52,19 +53,6 @@ size_t cpml_primitive_get_n_points(const CpmlPrimitive *primitive); cairo_path_data_t * cpml_primitive_get_point (const CpmlPrimitive *primitive, int n_point); -void cpml_primitive_to_cairo (const CpmlPrimitive *primitive, - cairo_t *cr); -void cpml_primitive_dump (const CpmlPrimitive *primitive, - cairo_bool_t org_also); -size_t cpml_primitive_put_intersections_with_segment - (const CpmlPrimitive *primitive, - const CpmlSegment *segment, - size_t n_dest, - CpmlPair *dest); - -/* To be implemented by the primitives */ -size_t cpml_primitive_type_get_n_points - (CpmlPrimitiveType type); double cpml_primitive_get_length (const CpmlPrimitive *primitive); void cpml_primitive_put_extents (const CpmlPrimitive *primitive, CpmlExtents *extents); @@ -83,10 +71,19 @@ size_t cpml_primitive_put_intersections const CpmlPrimitive *primitive2, size_t n_dest, CpmlPair *dest); +size_t cpml_primitive_put_intersections_with_segment + (const CpmlPrimitive *primitive, + const CpmlSegment *segment, + size_t n_dest, + CpmlPair *dest); void cpml_primitive_offset (CpmlPrimitive *primitive, double offset); cairo_bool_t cpml_primitive_join (CpmlPrimitive *primitive, CpmlPrimitive *primitive2); +void cpml_primitive_to_cairo (const CpmlPrimitive *primitive, + cairo_t *cr); +void cpml_primitive_dump (const CpmlPrimitive *primitive, + cairo_bool_t org_also); CAIRO_END_DECLS -- 2.11.4.GIT