From d9bf14b9a1a5fa5770808c18d8039da0fb34021c Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Thu, 8 Feb 2018 23:43:19 +0000 Subject: fix line width scaling --- src/cos_stream_filter.c | 2 +- src/document.c | 16 ++++++++++++++++ src/graphics_state.h | 12 +++++++++++- src/page.c | 30 ++++++++++++++++++++++++++---- 4 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/cos_stream_filter.c b/src/cos_stream_filter.c index 0c08442..c12f641 100644 --- a/src/cos_stream_filter.c +++ b/src/cos_stream_filter.c @@ -105,7 +105,7 @@ nspdf__cos_stream_filter(struct nspdf_doc *doc, nspdferror res; //printf("applying filter %s\n", filter_name); - + /** \todo implement all the other mandantory stream filters */ if (strcmp(filter_name, "FlateDecode") == 0) { res = cos_stream_inflate(doc, stream_out); } else { diff --git a/src/document.c b/src/document.c index bd3d314..9e8ed22 100644 --- a/src/document.c +++ b/src/document.c @@ -22,6 +22,22 @@ #include "xref.h" #include "pdf_doc.h" +/* + * And you may find yourself + * Writing another PDF parser + * And you may find yourself + * In a middle of a debug session + * And you may find yourself + * Behind an British English keyboard + * And you may find yourself in a gdb + * With a long stacktrace + * And you may ask yourself, well + * How did I get here? + * + * Andrew Shadura 2018 + */ + + #define SLEN(x) (sizeof((x)) - 1) /* byte data acessory, allows for more complex buffer handling in future */ diff --git a/src/graphics_state.h b/src/graphics_state.h index e5cc2bf..40060dd 100644 --- a/src/graphics_state.h +++ b/src/graphics_state.h @@ -52,7 +52,17 @@ struct graphics_state_param { struct { struct graphics_state_color colour; } other; - /* text state */ + /** text state */ + struct { + float charspacing; + float wordspacing; + float hscale; + float leading; + float fontsize; + unsigned int rendermode; + float rise; + /* knockout */ + } text; float line_width; unsigned int line_cap; unsigned int line_join; diff --git a/src/page.c b/src/page.c index 2a3a836..7a0a33b 100644 --- a/src/page.c +++ b/src/page.c @@ -13,6 +13,8 @@ #include #include #include +#include + #include #include "graphics_state.h" @@ -409,14 +411,31 @@ render_operation_f(struct graphics_state *gs, struct nspdf_render_ctx* render_ct return NSPDFERROR_OK; } +static inline nspdferror +scale_stroke_width(float *ctm, float unscaled, float *scaled) +{ + float avscale; + avscale = (fabs(ctm[0]) + fabs(ctm[3])) / 2.0; /* average scale of x and y axis */ + + *scaled = unscaled * avscale; + if (*scaled < 0.1) { + /* printf("sx:%f sy:%f av:%f un:%f sc:%f\n", + ctm[0], ctm[3], avscale, unscaled, *scaled);*/ + *scaled = 0.1; + } + return NSPDFERROR_OK; +} static inline nspdferror render_operation_B(struct graphics_state *gs, struct nspdf_render_ctx* render_ctx) { struct nspdf_style style; style.stroke_type = NSPDF_OP_TYPE_SOLID; - style.stroke_width = gs->param_stack[gs->param_stack_idx].line_width; - gsc_to_device(&gs->param_stack[gs->param_stack_idx].stroke.colour, &style.stroke_colour); + scale_stroke_width(gs->param_stack[gs->param_stack_idx].ctm, + gs->param_stack[gs->param_stack_idx].line_width, + &style.stroke_width); + gsc_to_device(&gs->param_stack[gs->param_stack_idx].stroke.colour, + &style.stroke_colour); style.fill_type = NSPDF_OP_TYPE_SOLID; gsc_to_device(&gs->param_stack[gs->param_stack_idx].other.colour, &style.fill_colour); @@ -441,8 +460,11 @@ render_operation_S(struct graphics_state *gs, struct nspdf_render_ctx* render_ct style.fill_colour = 0x01000000; style.stroke_type = NSPDF_OP_TYPE_SOLID; - style.stroke_width = gs->param_stack[gs->param_stack_idx].line_width; - gsc_to_device(&gs->param_stack[gs->param_stack_idx].stroke.colour, &style.stroke_colour); + scale_stroke_width(gs->param_stack[gs->param_stack_idx].ctm, + gs->param_stack[gs->param_stack_idx].line_width, + &style.stroke_width); + gsc_to_device(&gs->param_stack[gs->param_stack_idx].stroke.colour, + &style.stroke_colour); render_ctx->path(&style, gs->path, -- cgit v1.2.3