diff options
author | yzrh <yzrh@noema.org> | 2022-12-31 10:48:29 +0000 |
---|---|---|
committer | yzrh <yzrh@noema.org> | 2022-12-31 10:48:29 +0000 |
commit | 220a81c2adc0fb51137068c46cbcbe7641115a03 (patch) | |
tree | 34ed26c14a85f9ba1e9f8d8bed212f9199140b73 /src | |
parent | 1d899d934d26a5e19333712deb50c39b3843076d (diff) | |
download | melon-220a81c2adc0fb51137068c46cbcbe7641115a03.tar.gz melon-220a81c2adc0fb51137068c46cbcbe7641115a03.tar.zst |
Fix HN image compositing.
Signed-off-by: yzrh <yzrh@noema.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/cnki_pdf.c | 77 |
1 files changed, 50 insertions, 27 deletions
diff --git a/src/cnki_pdf.c b/src/cnki_pdf.c index 72500ab..6cb8c9f 100644 --- a/src/cnki_pdf.c +++ b/src/cnki_pdf.c @@ -496,10 +496,10 @@ cnki_pdf_hn(cnki_t **param) int stream_size; char *stream; - int *dim; + double *dim; if (ptr->image_length > 0) { - dim = malloc(2 * ptr->image_length * sizeof(int)); + dim = malloc(2 * ptr->image_length * sizeof(double)); if (dim == NULL) { free(root_kid); @@ -911,24 +911,35 @@ cnki_pdf_hn(cnki_t **param) strcat(dictionary, "\n"); } + /* FIXME: Use the text somehow? */ + memset(dictionary, 0, dictionary_size); + if (ptr->image_length > 0) { - char resize_str[64]; double resize_x = 1; double resize_y = 1; + double margin_x = 0; + double margin_y = 0; + if (ptr->image_data[0].x == 0 && ptr->image_data[0].y == 0 && dim[0] > 0 && dim[1] > 0) { /* Scale within bound of A4 paper */ - resize_x = 2381.102 / dim[0]; - resize_y = 3367.559 / dim[1]; - - if (resize_y < resize_x) - snprintf(resize_str, 64, "%f 0 0 %f 0 0 cm\n", - resize_y, resize_y); - else - snprintf(resize_str, 64, "%f 0 0 %f 0 0 cm\n", - resize_x, resize_x); - } else { - memset(resize_str, 0, 64); + resize_x = 2480.315 / dim[0]; + resize_y = 3507.874 / dim[1]; + + if (resize_y < resize_x) { + for (int i = 0; i < ptr->image_length; i++) { + dim[i * 2] *= resize_y; + dim[i * 2 + 1] *= resize_y; + } + } else { + for (int i = 0; i < ptr->image_length; i++) { + dim[i * 2] *= resize_x; + dim[i * 2 + 1] *= resize_x; + } + } + + margin_x = (2480.315 - dim[0]) / 2; + margin_y = (3507.874 - dim[1]) / 2; } for (int i = 0; i < ptr->image_length; i++) { @@ -937,36 +948,48 @@ cnki_pdf_hn(cnki_t **param) strcat(dictionary, "q\n"); - strcat(dictionary, "0.25 0 0 0.25 0 0 cm\n"); + strcat(dictionary, "0.24 0 0 0.24 0 0 cm\n"); /* Rotate image */ if (ptr->image_data[i].format == JBIG || ptr->image_data[i].format == DCT_1) { - snprintf(buf, 64, "1 0 0 1 0 %d cm\n", - dim[i * 2 + 1]); + snprintf(buf, 64, "1 0 0 -1 0 %f cm\n", dim[i * 2 + 1]); strcat(dictionary, buf); - - strcat(dictionary, "1 0 0 -1 0 0 cm\n"); } /* Translate figure */ if (ptr->image_data[i].x > 0 || ptr->image_data[i].y > 0) { - double origin_x = ptr->image_data[i].x * dim[i * 2] / ptr->image_data[i].w; - double origin_y = ptr->image_data[i].y * dim[i * 2 + 1] / ptr->image_data[i].h; + double origin_x = ptr->image_data[i].x * 0.40433; + double origin_y = ptr->image_data[i].y * 0.40433; + + if (resize_y < resize_x) { + origin_x *= resize_y; + origin_y *= resize_y; + } else { + origin_x *= resize_x; + origin_y *= resize_x; + } if (ptr->image_data[i].format == JBIG || ptr->image_data[i].format == DCT_1) - origin_y = -3367.559 + origin_y + dim[i * 2 + 1]; + origin_y = -3507.874 + origin_y + dim[i * 2 + 1]; else - origin_y = 3367.559 - origin_y - dim[i * 2 + 1]; + origin_y = 3507.874 - origin_y - dim[i * 2 + 1]; snprintf(buf, 64, "1 0 0 1 %f %f cm\n", origin_x, origin_y); strcat(dictionary, buf); } - snprintf(buf, 64, "%d 0 0 %d 0 0 cm\n", - dim[i * 2], dim[i * 2 + 1]); - strcat(dictionary, buf); + if (margin_x > 0 || margin_y > 0) { + if (ptr->image_data[i].format == JBIG || ptr->image_data[i].format == DCT_1) { + snprintf(buf, 64, "1 0 0 1 %f %f cm\n", margin_x, -margin_y); + strcat(dictionary, buf); + } else { + snprintf(buf, 64, "1 0 0 1 %f %f cm\n", margin_x, margin_y); + strcat(dictionary, buf); + } + } - strcat(dictionary, resize_str); + snprintf(buf, 64, "%f 0 0 %f 0 0 cm\n", dim[i * 2], dim[i * 2 + 1]); + strcat(dictionary, buf); snprintf(buf, 64, "/Im%d Do\n", i); strcat(dictionary, buf); |