Example #1
0
static void
complex_to_buf(char *buf, int bufsz, PyComplexObject *v, int precision)
{
	char format[32];
	if (v->cval.real == 0.) {
		if (!Py_IS_FINITE(v->cval.imag)) {
			if (Py_IS_NAN(v->cval.imag))
				strncpy(buf, "nan*j", 6);
			else if (copysign(1, v->cval.imag) == 1)
				strncpy(buf, "inf*j", 6);
			else
				strncpy(buf, "-inf*j", 7);
		}
		else {
			PyOS_snprintf(format, sizeof(format), "%%.%ig", precision);
			PyOS_ascii_formatd(buf, bufsz - 1, format, v->cval.imag);
			strncat(buf, "j", 1);
		}
	} else {
		char re[64], im[64];
		/* Format imaginary part with sign, real part without */
		if (!Py_IS_FINITE(v->cval.real)) {
			if (Py_IS_NAN(v->cval.real))
				strncpy(re, "nan", 4);
			/* else if (copysign(1, v->cval.real) == 1) */
			else if (v->cval.real > 0)
				strncpy(re, "inf", 4);
			else
				strncpy(re, "-inf", 5);
		}
		else {
			PyOS_snprintf(format, sizeof(format), "%%.%ig", precision);
			PyOS_ascii_formatd(re, sizeof(re), format, v->cval.real);
		}
		if (!Py_IS_FINITE(v->cval.imag)) {
			if (Py_IS_NAN(v->cval.imag))
				strncpy(im, "+nan*", 6);
			/* else if (copysign(1, v->cval.imag) == 1) */
			else if (v->cval.imag > 0)
				strncpy(im, "+inf*", 6);
			else
				strncpy(im, "-inf*", 6);
		}
		else {
			PyOS_snprintf(format, sizeof(format), "%%+.%ig", precision);
			PyOS_ascii_formatd(im, sizeof(im), format, v->cval.imag);
		}
		PyOS_snprintf(buf, bufsz, "(%s%sj)", re, im);
	}
}
Example #2
0
static void
complex_to_buf(char *buf, int bufsz, PyComplexObject *v, int precision)
{
	char format[32];
	if (v->cval.real == 0.) {
		PyOS_snprintf(format, sizeof(format), "%%.%ig", precision);
		PyOS_ascii_formatd(buf, bufsz - 1, format, v->cval.imag);
		strncat(buf, "j", 1);
	} else {
		char re[64], im[64];
		/* Format imaginary part with sign, real part without */
		PyOS_snprintf(format, sizeof(format), "%%.%ig", precision);
		PyOS_ascii_formatd(re, sizeof(re), format, v->cval.real);
		PyOS_snprintf(format, sizeof(format), "%%+.%ig", precision);
		PyOS_ascii_formatd(im, sizeof(im), format, v->cval.imag);
		PyOS_snprintf(buf, bufsz, "(%s%sj)", re, im);
	}
}
Example #3
0
Py::Object
_path_module::convert_to_svg(const Py::Tuple& args)
{
    args.verify_length(5);

    PathIterator path(args[0]);
    agg::trans_affine trans = py_to_agg_transformation_matrix(args[1].ptr(), false);

    Py::Object clip_obj = args[2];
    bool do_clip;
    agg::rect_base<double> clip_rect(0, 0, 0, 0);
    if (clip_obj.isNone() || !clip_obj.isTrue())
    {
        do_clip = false;
    }
    else
    {
        double x1, y1, x2, y2;
        Py::Tuple clip_tuple(clip_obj);
        x1 = Py::Float(clip_tuple[0]);
        y1 = Py::Float(clip_tuple[1]);
        x2 = Py::Float(clip_tuple[2]);
        y2 = Py::Float(clip_tuple[3]);
        clip_rect.init(x1, y1, x2, y2);
        do_clip = true;
    }

    bool simplify;
    Py::Object simplify_obj = args[3];
    if (simplify_obj.isNone())
    {
        simplify = path.should_simplify();
    }
    else
    {
        simplify = simplify_obj.isTrue();
    }

    int precision = Py::Int(args[4]);

    #if PY_VERSION_HEX < 0x02070000
    char format[64];
    snprintf(format, 64, "%s.%dg", "%", precision);
    #endif

    typedef agg::conv_transform<PathIterator>  transformed_path_t;
    typedef PathNanRemover<transformed_path_t> nan_removal_t;
    typedef PathClipper<nan_removal_t>         clipped_t;
    typedef PathSimplifier<clipped_t>          simplify_t;

    transformed_path_t tpath(path, trans);
    nan_removal_t      nan_removed(tpath, true, path.has_curves());
    clipped_t          clipped(nan_removed, do_clip, clip_rect);
    simplify_t         simplified(clipped, simplify, path.simplify_threshold());

    size_t buffersize = path.total_vertices() * (precision + 5) * 4;
    char* buffer = (char *)malloc(buffersize);
    char* p = buffer;

    const char codes[] = {'M', 'L', 'Q', 'C'};
    const int  waits[] = {  1,   1,   2,   3};

    int wait = 0;
    unsigned code;
    double x = 0, y = 0;
    while ((code = simplified.vertex(&x, &y)) != agg::path_cmd_stop)
    {
        if (wait == 0)
        {
            *p++ = '\n';

            if (code == 0x4f)
            {
                *p++ = 'z';
                *p++ = '\n';
                continue;
            }

            *p++ = codes[code-1];
            wait = waits[code-1];
        }
        else
        {
            *p++ = ' ';
        }

        #if PY_VERSION_HEX >= 0x02070000
        char* str;
        str = PyOS_double_to_string(x, 'g', precision, 0, NULL);
        p += snprintf(p, buffersize - (p - buffer), str);
        PyMem_Free(str);
        *p++ = ' ';
        str = PyOS_double_to_string(y, 'g', precision, 0, NULL);
        p += snprintf(p, buffersize - (p - buffer), str);
        PyMem_Free(str);
        #else
        char str[64];
        PyOS_ascii_formatd(str, 64, format, x);
        p += snprintf(p, buffersize - (p - buffer), str);
        *p++ = ' ';
        PyOS_ascii_formatd(str, 64, format, y);
        p += snprintf(p, buffersize - (p - buffer), str);
        #endif

        --wait;
    }

    #if PY3K
    PyObject* result = PyUnicode_FromStringAndSize(buffer, p - buffer);
    #else
    PyObject* result = PyString_FromStringAndSize(buffer, p - buffer);
    #endif
    free(buffer);

    return Py::Object(result, true);
}