From 4da5b1291683f605b3c49c8907fbc6412c66fd01 Mon Sep 17 00:00:00 2001 From: Nicola Fontana Date: Fri, 12 Feb 2010 16:23:01 +0100 Subject: [PATCH] [CPML] Implemented get_length() as a virtual method Removed public cpml_..._get_length() APIs and left only cpml_primitive_get_length() that accesses the underlying private implementations. --- cpml/cpml-arc.c | 63 +++++++++++++++++++++++---------------------------- cpml/cpml-arc.h | 1 - cpml/cpml-curve.c | 29 ++++++------------------ cpml/cpml-curve.h | 1 - cpml/cpml-line.c | 42 ++++++++++++++-------------------- cpml/cpml-line.h | 1 - cpml/cpml-primitive.c | 33 ++++++++++++--------------- 7 files changed, 67 insertions(+), 103 deletions(-) diff --git a/cpml/cpml-arc.c b/cpml/cpml-arc.c index 672b5768..f286f1f1 100644 --- a/cpml/cpml-arc.c +++ b/cpml/cpml-arc.c @@ -77,17 +77,18 @@ #define ARC_MAX_ANGLE M_PI_2 -static cairo_bool_t get_center (const CpmlPair *p, - CpmlPair *dest); -static void get_angles (const CpmlPair *p, - const CpmlPair *center, - double *start, - double *end); -static void arc_to_curve (CpmlPrimitive *curve, - const CpmlPair *center, - double r, - double start, - double end); +static double get_length (const CpmlPrimitive *arc); +static cairo_bool_t get_center (const CpmlPair *p, + CpmlPair *dest); +static void get_angles (const CpmlPair *p, + const CpmlPair *center, + double *start, + double *end); +static void arc_to_curve (CpmlPrimitive *curve, + const CpmlPair *center, + double r, + double start, + double end); const _CpmlPrimitiveClass * @@ -98,7 +99,7 @@ _cpml_arc_get_class(void) if (p_class == NULL) { static _CpmlPrimitiveClass class_data = { "arc", 3, - NULL, + get_length, NULL, NULL, NULL, @@ -178,29 +179,6 @@ cpml_arc_info(const CpmlPrimitive *arc, CpmlPair *center, return 1; } -/** - * cpml_arc_get_length: - * @arc: the #CpmlPrimitive arc data - * - * Given the @arc primitive, returns its length. - * - * Returns: the requested length or 0 on errors - **/ -double -cpml_arc_get_length(const CpmlPrimitive *arc) -{ - double r, start, end, delta; - - if (!cpml_arc_info(arc, NULL, &r, &start, &end) || start == end) - return 0.; - - delta = end - start; - if (delta < 0) - delta += M_PI*2; - - return r*delta; -} - /* Hardcoded macro to save a lot of typing and make the * cpml_arc_put_extents() code clearer */ #define ANGLE_INCLUDED(d) \ @@ -540,6 +518,21 @@ cpml_arc_to_curves(const CpmlPrimitive *arc, CpmlSegment *segment, } +static double +get_length(const CpmlPrimitive *arc) +{ + double r, start, end, delta; + + if (!cpml_arc_info(arc, NULL, &r, &start, &end) || start == end) + return 0.; + + delta = end - start; + if (delta < 0) + delta += M_PI*2; + + return r*delta; +} + static cairo_bool_t get_center(const CpmlPair *p, CpmlPair *dest) { diff --git a/cpml/cpml-arc.h b/cpml/cpml-arc.h index 6d6df1d3..354763e8 100644 --- a/cpml/cpml-arc.h +++ b/cpml/cpml-arc.h @@ -35,7 +35,6 @@ cairo_bool_t double *r, double *start, double *end); -double cpml_arc_get_length (const CpmlPrimitive *arc); void cpml_arc_put_extents (const CpmlPrimitive *arc, CpmlExtents *extents); void cpml_arc_put_pair_at (const CpmlPrimitive *arc, diff --git a/cpml/cpml-curve.c b/cpml/cpml-curve.c index ae0ea262..782916a0 100644 --- a/cpml/cpml-curve.c +++ b/cpml/cpml-curve.c @@ -27,6 +27,13 @@ * No validation is made on the input so use the following methods * only when you are sure the primitive argument * is effectively a cubic Bézier curve. + * + * + * TODO + * + * the get_length() method must be implemented; + * + * **/ @@ -63,28 +70,6 @@ _cpml_curve_get_class(void) /** - * cpml_curve_get_length: - * @curve: the #CpmlPrimitive curve data - * - * Given the @curve primitive, returns the approximated length of - * the Bézier curve. - * - * - * TODO - * - * To be implemented... - * - * - * - * Returns: the requested length - **/ -double -cpml_curve_get_length(const CpmlPrimitive *curve) -{ - return 0.; -} - -/** * cpml_curve_put_extents: * @curve: the #CpmlPrimitive curve data * @extents: where to store the extents diff --git a/cpml/cpml-curve.h b/cpml/cpml-curve.h index 748c7cb4..277ffbe7 100644 --- a/cpml/cpml-curve.h +++ b/cpml/cpml-curve.h @@ -29,7 +29,6 @@ CAIRO_BEGIN_DECLS -double cpml_curve_get_length (const CpmlPrimitive *curve); void cpml_curve_put_extents (const CpmlPrimitive *curve, CpmlExtents *extents); void cpml_curve_put_pair_at_time (const CpmlPrimitive *curve, diff --git a/cpml/cpml-line.c b/cpml/cpml-line.c index b28f64fd..b2721cdd 100644 --- a/cpml/cpml-line.c +++ b/cpml/cpml-line.c @@ -55,9 +55,10 @@ #include -static cairo_bool_t intersection (const CpmlPair *p, - CpmlPair *dest, - double *get_factor); +static double get_length (const CpmlPrimitive *line); +static cairo_bool_t intersection (const CpmlPair *p, + CpmlPair *dest, + double *get_factor); const _CpmlPrimitiveClass * @@ -68,7 +69,7 @@ _cpml_line_get_class(void) if (p_class == NULL) { static _CpmlPrimitiveClass class_data = { "line", 2, - NULL, + get_length, NULL, NULL, NULL, @@ -91,7 +92,7 @@ _cpml_close_get_class(void) if (p_class == NULL) { static _CpmlPrimitiveClass class_data = { "close", 2, - NULL, + get_length, NULL, NULL, NULL, @@ -108,26 +109,6 @@ _cpml_close_get_class(void) /** - * cpml_line_get_length: - * @line: the #CpmlPrimitive line data - * - * Given the @line primitive, returns the distance between its - * start and end points. - * - * Returns: the requested distance, that is the @line length - **/ -double -cpml_line_get_length(const CpmlPrimitive *line) -{ - CpmlPair p1, p2; - - cpml_pair_from_cairo(&p1, cpml_primitive_get_point(line, 0)); - cpml_pair_from_cairo(&p2, cpml_primitive_get_point(line, -1)); - - return cpml_pair_distance(&p1, &p2); -} - -/** * cpml_line_put_extents: * @line: the #CpmlPrimitive line data * @extents: where to store the extents @@ -377,6 +358,17 @@ cpml_close_offset(CpmlPrimitive *close, double offset) } +static double +get_length(const CpmlPrimitive *line) +{ + CpmlPair p1, p2; + + cpml_pair_from_cairo(&p1, cpml_primitive_get_point(line, 0)); + cpml_pair_from_cairo(&p2, cpml_primitive_get_point(line, -1)); + + return cpml_pair_distance(&p1, &p2); +} + static cairo_bool_t intersection(const CpmlPair *p, CpmlPair *dest, double *get_factor) { diff --git a/cpml/cpml-line.h b/cpml/cpml-line.h index 3f87d926..24b35fb2 100644 --- a/cpml/cpml-line.h +++ b/cpml/cpml-line.h @@ -29,7 +29,6 @@ CAIRO_BEGIN_DECLS -double cpml_line_get_length (const CpmlPrimitive *line); void cpml_line_put_extents (const CpmlPrimitive *line, CpmlExtents *extents); void cpml_line_put_pair_at (const CpmlPrimitive *line, diff --git a/cpml/cpml-primitive.c b/cpml/cpml-primitive.c index 7720a7df..b1b532d7 100644 --- a/cpml/cpml-primitive.c +++ b/cpml/cpml-primitive.c @@ -71,7 +71,9 @@ static const _CpmlPrimitiveClass * - get_class (CpmlPrimitiveType type); + get_class_from_type (CpmlPrimitiveType type); +static const _CpmlPrimitiveClass * + get_class (const CpmlPrimitive *primitive); static void dump_cairo_point (const cairo_path_data_t *path_data); @@ -383,7 +385,7 @@ cpml_primitive_put_intersections_with_segment(const CpmlPrimitive *primitive, int cpml_primitive_type_get_npoints(CpmlPrimitiveType type) { - const _CpmlPrimitiveClass *class_data = get_class(type); + const _CpmlPrimitiveClass *class_data = get_class_from_type(type); if (class_data == NULL) return -1; @@ -409,23 +411,12 @@ cpml_primitive_type_get_npoints(CpmlPrimitiveType type) double cpml_primitive_get_length(const CpmlPrimitive *primitive) { - switch (primitive->data->header.type) { - - case CAIRO_PATH_LINE_TO: - case CAIRO_PATH_CLOSE_PATH: - return cpml_line_get_length(primitive); - - case CAIRO_PATH_ARC_TO: - return cpml_arc_get_length(primitive); - - case CAIRO_PATH_CURVE_TO: - return cpml_curve_get_length(primitive); + const _CpmlPrimitiveClass *class_data = get_class(primitive); - default: - break; - } + if (class_data == NULL || class_data->get_length == NULL) + return 0; - return 0.; + return class_data->get_length(primitive); } /** @@ -800,7 +791,7 @@ cpml_primitive_offset(CpmlPrimitive *primitive, double offset) static const _CpmlPrimitiveClass * -get_class(CpmlPrimitiveType type) +get_class_from_type(CpmlPrimitiveType type) { switch (type) { case CAIRO_PATH_LINE_TO: @@ -818,6 +809,12 @@ get_class(CpmlPrimitiveType type) return NULL; } +static const _CpmlPrimitiveClass * +get_class(const CpmlPrimitive *primitive) +{ + return get_class_from_type(primitive->data->header.type); +} + static void dump_cairo_point(const cairo_path_data_t *path_data) { -- 2.11.4.GIT