-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Generate path strings in C++ for PDF and PS #4197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1f0e4ad
Generate path strings in C++ for PDF and PS
mdboom cc41bff
Address comments in the PR.
mdboom 23e6cf5
Return error on unknown code
mdboom c27bc78
Tack null on more carefully
mdboom 3f51ef5
Don't need to NULL terminate
mdboom 1b2a762
Don't copy string
mdboom d04bc6b
Python doesn't like creating 0-length strings.
mdboom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -939,79 +939,166 @@ void cleanup_path(PathIterator &path, | |
} | ||
} | ||
|
||
void quad2cubic(double x0, double y0, | ||
double x1, double y1, | ||
double x2, double y2, | ||
double *outx, double *outy) | ||
{ | ||
|
||
outx[0] = x0 + 2./3. * (x1 - x0); | ||
outy[0] = y0 + 2./3. * (y1 - y0); | ||
outx[1] = outx[0] + 1./3. * (x2 - x0); | ||
outy[1] = outy[0] + 1./3. * (y2 - y0); | ||
outx[2] = x2; | ||
outy[2] = y2; | ||
} | ||
|
||
char *__append_to_string(char *p, char *buffer, size_t buffersize, | ||
const char *content) | ||
{ | ||
int buffersize_int = (int)buffersize; | ||
|
||
for (const char *i = content; *i; ++i) { | ||
if (p < buffer || p - buffer >= buffersize_int) { | ||
return NULL; | ||
} | ||
|
||
*p++ = *i; | ||
} | ||
|
||
return p; | ||
} | ||
|
||
template <class PathIterator> | ||
void convert_to_svg(PathIterator &path, | ||
agg::trans_affine &trans, | ||
agg::rect_d &clip_rect, | ||
bool simplify, | ||
int precision, | ||
char *buffer, | ||
size_t *buffersize) | ||
int __convert_to_string(PathIterator &path, | ||
int precision, | ||
char **codes, | ||
bool postfix, | ||
char *buffer, | ||
size_t *buffersize) | ||
{ | ||
#if PY_VERSION_HEX < 0x02070000 | ||
char format[64]; | ||
snprintf(format, 64, "%s.%dg", "%", precision); | ||
#endif | ||
|
||
typedef agg::conv_transform<py::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; | ||
|
||
bool do_clip = (clip_rect.x1 < clip_rect.x2 && clip_rect.y1 < clip_rect.y2); | ||
|
||
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()); | ||
|
||
char *p = buffer; | ||
double x[3]; | ||
double y[3]; | ||
double last_x = 0.0; | ||
double last_y = 0.0; | ||
|
||
const char codes[] = { 'M', 'L', 'Q', 'C' }; | ||
const int waits[] = { 1, 1, 2, 3 }; | ||
|
||
int wait = 0; | ||
const int sizes[] = { 1, 1, 2, 3 }; | ||
int size = 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; | ||
while ((code = path.vertex(&x[0], &y[0])) != agg::path_cmd_stop) { | ||
if (code == 0x4f) { | ||
if ((p = __append_to_string(p, buffer, *buffersize, codes[4])) == NULL) return 1; | ||
} else if (code < 5) { | ||
size = sizes[code - 1]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably have a check that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. This should be fixed now as well. |
||
|
||
for (int i = 1; i < size; ++i) { | ||
unsigned subcode = path.vertex(&x[i], &y[i]); | ||
if (subcode != code) { | ||
return 2; | ||
} | ||
} | ||
|
||
*p++ = codes[code - 1]; | ||
wait = waits[code - 1]; | ||
} else { | ||
*p++ = ' '; | ||
} | ||
/* For formats that don't support quad curves, convert to | ||
cubic curves */ | ||
if (code == CURVE3 && codes[code - 1][0] == '\0') { | ||
quad2cubic(last_x, last_y, x[0], y[0], x[1], y[1], x, y); | ||
code++; | ||
size = 3; | ||
} | ||
|
||
if (!postfix) { | ||
if ((p = __append_to_string(p, buffer, *buffersize, codes[code - 1])) == NULL) return 1; | ||
if ((p = __append_to_string(p, buffer, *buffersize, " ")) == NULL) return 1; | ||
} | ||
|
||
for (int i = 0; i < size; ++i) { | ||
#if PY_VERSION_HEX >= 0x02070000 | ||
char *str; | ||
str = PyOS_double_to_string(x, 'g', precision, 0, NULL); | ||
p += snprintf(p, *buffersize - (p - buffer), "%s", str); | ||
PyMem_Free(str); | ||
*p++ = ' '; | ||
str = PyOS_double_to_string(y, 'g', precision, 0, NULL); | ||
p += snprintf(p, *buffersize - (p - buffer), "%s", str); | ||
PyMem_Free(str); | ||
char *str; | ||
str = PyOS_double_to_string(x[i], 'g', precision, 0, NULL); | ||
if ((p = __append_to_string(p, buffer, *buffersize, str)) == NULL) { | ||
PyMem_Free(str); | ||
return 1; | ||
} | ||
PyMem_Free(str); | ||
if ((p = __append_to_string(p, buffer, *buffersize, " ")) == NULL) return 1; | ||
str = PyOS_double_to_string(y[i], 'g', precision, 0, NULL); | ||
if ((p = __append_to_string(p, buffer, *buffersize, str)) == NULL) { | ||
PyMem_Free(str); | ||
return 1; | ||
} | ||
PyMem_Free(str); | ||
if ((p = __append_to_string(p, buffer, *buffersize, " ")) == NULL) return 1; | ||
#else | ||
char str[64]; | ||
PyOS_ascii_formatd(str, 64, format, x); | ||
p += snprintf(p, *buffersize - (p - buffer), "%s", str); | ||
*p++ = ' '; | ||
PyOS_ascii_formatd(str, 64, format, y); | ||
p += snprintf(p, *buffersize - (p - buffer), "%s", str); | ||
char str[64]; | ||
PyOS_ascii_formatd(str, 64, format, x[i]); | ||
if ((p = __append_to_string(p, buffer, *buffersize, str)) == NULL) return 1; | ||
p = __append_to_string(p, buffer, *buffersize, " "); | ||
PyOS_ascii_formatd(str, 64, format, y[i]); | ||
if ((p = __append_to_string(p, buffer, *buffersize, str)) == NULL) return 1; | ||
if ((p = __append_to_string(p, buffer, *buffersize, " ")) == NULL) return 1; | ||
#endif | ||
} | ||
|
||
if (postfix) { | ||
if ((p = __append_to_string(p, buffer, *buffersize, codes[code - 1])) == NULL) return 1; | ||
} | ||
|
||
last_x = x[size - 1]; | ||
last_y = y[size - 1]; | ||
} else { | ||
// Unknown code value | ||
return 2; | ||
} | ||
|
||
--wait; | ||
if ((p = __append_to_string(p, buffer, *buffersize, "\n")) == NULL) return 1; | ||
} | ||
|
||
*p = '\0'; | ||
*buffersize = p - buffer; | ||
|
||
return 0; | ||
} | ||
|
||
template <class PathIterator> | ||
int convert_to_string(PathIterator &path, | ||
agg::trans_affine &trans, | ||
agg::rect_d &clip_rect, | ||
bool simplify, | ||
SketchParams sketch_params, | ||
int precision, | ||
char **codes, | ||
bool postfix, | ||
char *buffer, | ||
size_t *buffersize) | ||
{ | ||
typedef agg::conv_transform<py::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; | ||
typedef agg::conv_curve<simplify_t> curve_t; | ||
typedef Sketch<curve_t> sketch_t; | ||
|
||
bool do_clip = (clip_rect.x1 < clip_rect.x2 && clip_rect.y1 < clip_rect.y2); | ||
|
||
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()); | ||
|
||
if (sketch_params.scale == 0.0) { | ||
return __convert_to_string(simplified, precision, codes, postfix, buffer, buffersize); | ||
} else { | ||
curve_t curve(simplified); | ||
sketch_t sketch(curve, sketch_params.scale, sketch_params.length, sketch_params.randomness); | ||
return __convert_to_string(sketch, precision, codes, postfix, buffer, buffersize); | ||
} | ||
|
||
} | ||
|
||
#endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we consider this part of the public API?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. It's in a module with a leading underscore (
_path
).