diff options
Diffstat (limited to 'fitz')
-rw-r--r-- | fitz/base_geometry.c | 36 | ||||
-rw-r--r-- | fitz/base_hash.c | 2 | ||||
-rw-r--r-- | fitz/base_string.c | 2 | ||||
-rw-r--r-- | fitz/dev_list.c | 2 | ||||
-rw-r--r-- | fitz/dev_text.c | 10 | ||||
-rw-r--r-- | fitz/filt_basic.c | 10 | ||||
-rw-r--r-- | fitz/filt_dctd.c | 5 | ||||
-rw-r--r-- | fitz/filt_predict.c | 12 | ||||
-rw-r--r-- | fitz/fitz-internal.h | 1 | ||||
-rw-r--r-- | fitz/fitz.h | 63 | ||||
-rw-r--r-- | fitz/image_png.c | 2 | ||||
-rw-r--r-- | fitz/res_bitmap.c | 2 | ||||
-rw-r--r-- | fitz/res_colorspace.c | 52 | ||||
-rw-r--r-- | fitz/res_path.c | 30 | ||||
-rw-r--r-- | fitz/res_pixmap.c | 12 | ||||
-rw-r--r-- | fitz/res_text.c | 8 | ||||
-rw-r--r-- | fitz/stm_buffer.c | 74 | ||||
-rw-r--r-- | fitz/stm_open.c | 2 | ||||
-rw-r--r-- | fitz/stm_read.c | 4 |
19 files changed, 234 insertions, 95 deletions
diff --git a/fitz/base_geometry.c b/fitz/base_geometry.c index 4d83e8c2..27cc3cf3 100644 --- a/fitz/base_geometry.c +++ b/fitz/base_geometry.c @@ -1,7 +1,7 @@ #include "fitz-internal.h" -#define MAX4(a,b,c,d) MAX(MAX(a,b), MAX(c,d)) -#define MIN4(a,b,c,d) MIN(MIN(a,b), MIN(c,d)) +#define MAX4(a,b,c,d) fz_max(fz_max(a,b), fz_max(c,d)) +#define MIN4(a,b,c,d) fz_min(fz_min(a,b), fz_min(c,d)) /* Matrices, points and affine transformations */ @@ -209,10 +209,10 @@ fz_intersect_rect(fz_rect a, fz_rect b) if (fz_is_infinite_rect(b)) return a; if (fz_is_empty_rect(a)) return fz_empty_rect; if (fz_is_empty_rect(b)) return fz_empty_rect; - r.x0 = MAX(a.x0, b.x0); - r.y0 = MAX(a.y0, b.y0); - r.x1 = MIN(a.x1, b.x1); - r.y1 = MIN(a.y1, b.y1); + r.x0 = fz_max(a.x0, b.x0); + r.y0 = fz_max(a.y0, b.y0); + r.x1 = fz_min(a.x1, b.x1); + r.y1 = fz_min(a.y1, b.y1); return (r.x1 < r.x0 || r.y1 < r.y0) ? fz_empty_rect : r; } @@ -224,10 +224,10 @@ fz_union_rect(fz_rect a, fz_rect b) if (fz_is_infinite_rect(b)) return b; if (fz_is_empty_rect(a)) return b; if (fz_is_empty_rect(b)) return a; - r.x0 = MIN(a.x0, b.x0); - r.y0 = MIN(a.y0, b.y0); - r.x1 = MAX(a.x1, b.x1); - r.y1 = MAX(a.y1, b.y1); + r.x0 = fz_min(a.x0, b.x0); + r.y0 = fz_min(a.y0, b.y0); + r.x1 = fz_max(a.x1, b.x1); + r.y1 = fz_max(a.y1, b.y1); return r; } @@ -239,10 +239,10 @@ fz_intersect_bbox(fz_bbox a, fz_bbox b) if (fz_is_infinite_rect(b)) return a; if (fz_is_empty_rect(a)) return fz_empty_bbox; if (fz_is_empty_rect(b)) return fz_empty_bbox; - r.x0 = MAX(a.x0, b.x0); - r.y0 = MAX(a.y0, b.y0); - r.x1 = MIN(a.x1, b.x1); - r.y1 = MIN(a.y1, b.y1); + r.x0 = fz_maxi(a.x0, b.x0); + r.y0 = fz_maxi(a.y0, b.y0); + r.x1 = fz_mini(a.x1, b.x1); + r.y1 = fz_mini(a.y1, b.y1); return (r.x1 < r.x0 || r.y1 < r.y0) ? fz_empty_bbox : r; } @@ -254,10 +254,10 @@ fz_union_bbox(fz_bbox a, fz_bbox b) if (fz_is_infinite_rect(b)) return b; if (fz_is_empty_rect(a)) return b; if (fz_is_empty_rect(b)) return a; - r.x0 = MIN(a.x0, b.x0); - r.y0 = MIN(a.y0, b.y0); - r.x1 = MAX(a.x1, b.x1); - r.y1 = MAX(a.y1, b.y1); + r.x0 = fz_mini(a.x0, b.x0); + r.y0 = fz_mini(a.y0, b.y0); + r.x1 = fz_maxi(a.x1, b.x1); + r.y1 = fz_maxi(a.y1, b.y1); return r; } diff --git a/fitz/base_hash.c b/fitz/base_hash.c index 4ba02f4d..ccdffe63 100644 --- a/fitz/base_hash.c +++ b/fitz/base_hash.c @@ -138,7 +138,7 @@ static void fz_resize_hash(fz_context *ctx, fz_hash_table *table, int newsize) { fz_hash_entry *oldents = table->ents; - fz_hash_entry *newents = table->ents; + fz_hash_entry *newents; int oldsize = table->size; int oldload = table->load; int i; diff --git a/fitz/base_string.c b/fitz/base_string.c index 60e20ac4..fbb1cf4d 100644 --- a/fitz/base_string.c +++ b/fitz/base_string.c @@ -252,6 +252,6 @@ float fz_atof(const char *s) /* Return 1.0, as it's a small known value that won't cause a divide by 0. */ return 1.0; } - d = CLAMP(d, -FLT_MAX, FLT_MAX); + d = fz_clampd(d, -FLT_MAX, FLT_MAX); return (float)d; } diff --git a/fitz/dev_list.c b/fitz/dev_list.c index d84fb15b..560fb5c6 100644 --- a/fitz/dev_list.c +++ b/fitz/dev_list.c @@ -334,6 +334,7 @@ fz_list_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke, fz_m fz_try(ctx) { node->rect = fz_bound_text(dev->ctx, text, ctm); + fz_adjust_rect_for_stroke(&node->rect, stroke, &ctm); node->item.text = fz_clone_text(dev->ctx, text); node->stroke = fz_keep_stroke_state(dev->ctx, stroke); } @@ -377,6 +378,7 @@ fz_list_clip_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke, fz_try(ctx) { node->rect = fz_bound_text(dev->ctx, text, ctm); + fz_adjust_rect_for_stroke(&node->rect, stroke, &ctm); node->item.text = fz_clone_text(dev->ctx, text); node->stroke = fz_keep_stroke_state(dev->ctx, stroke); } diff --git a/fitz/dev_text.c b/fitz/dev_text.c index cd76830d..280d1b55 100644 --- a/fitz/dev_text.c +++ b/fitz/dev_text.c @@ -129,7 +129,7 @@ append_char(fz_context *ctx, fz_text_span *span, int c, fz_rect bbox) { if (span->len == span->cap) { - int new_cap = MAX(64, span->cap * 2); + int new_cap = fz_maxi(64, span->cap * 2); span->text = fz_resize_array(ctx, span->text, new_cap, sizeof(*span->text)); span->cap = new_cap; } @@ -155,7 +155,7 @@ append_span(fz_context *ctx, fz_text_line *line, fz_text_span *span) return; if (line->len == line->cap) { - int new_cap = MAX(8, line->cap * 2); + int new_cap = fz_maxi(8, line->cap * 2); line->spans = fz_resize_array(ctx, line->spans, new_cap, sizeof(*line->spans)); line->cap = new_cap; } @@ -176,7 +176,7 @@ append_line(fz_context *ctx, fz_text_block *block, fz_text_line *line) { if (block->len == block->cap) { - int new_cap = MAX(16, block->cap * 2); + int new_cap = fz_maxi(16, block->cap * 2); block->lines = fz_resize_array(ctx, block->lines, new_cap, sizeof *block->lines); block->cap = new_cap; } @@ -198,13 +198,13 @@ lookup_block_for_line(fz_context *ctx, fz_text_page *page, fz_text_line *line) float dy = line->bbox.y0 - block->bbox.y1; if (dy > -size * 1.5f && dy < size * PARAGRAPH_DIST) if (line->bbox.x0 <= block->bbox.x1 && line->bbox.x1 >= block->bbox.x0) - if (ABS(dx) < w / 2) + if (fz_abs(dx) < w / 2) return block; } if (page->len == page->cap) { - int new_cap = MAX(16, page->cap * 2); + int new_cap = fz_maxi(16, page->cap * 2); page->blocks = fz_resize_array(ctx, page->blocks, new_cap, sizeof(*page->blocks)); page->cap = new_cap; } diff --git a/fitz/filt_basic.c b/fitz/filt_basic.c index 7d504f29..db95533d 100644 --- a/fitz/filt_basic.c +++ b/fitz/filt_basic.c @@ -21,7 +21,7 @@ static int read_null(fz_stream *stm, unsigned char *buf, int len) { struct null_filter *state = stm->state; - int amount = MIN(len, state->remain); + int amount = fz_mini(len, state->remain); int n; fz_seek(state->chain, state->pos, 0); @@ -46,6 +46,8 @@ fz_open_null(fz_stream *chain, int len, int offset) struct null_filter *state; fz_context *ctx = chain->ctx; + if (len < 0) + len = 0; fz_try(ctx) { state = fz_malloc_struct(ctx, struct null_filter); @@ -350,7 +352,11 @@ read_a85d(fz_stream *stm, unsigned char *buf, int len) case 0: break; case 1: - fz_throw(stm->ctx, "partial final byte in a85d"); + /* Specifically illegal in the spec, but adobe + * and gs both cope. See normal_87.pdf for a + * case where this matters. */ + fz_warn(stm->ctx, "partial final byte in a85d"); + break; case 2: word = word * (85 * 85 * 85) + 0xffffff; state->bp[0] = word >> 24; diff --git a/fitz/filt_dctd.c b/fitz/filt_dctd.c index 23744f01..1b588d2a 100644 --- a/fitz/filt_dctd.c +++ b/fitz/filt_dctd.c @@ -101,12 +101,17 @@ read_dctd(fz_stream *stm, unsigned char *buf, int len) if (!state->init) { + int c; cinfo->client_data = state; cinfo->err = &state->errmgr; jpeg_std_error(cinfo->err); cinfo->err->error_exit = error_exit; jpeg_create_decompress(cinfo); + /* Skip over any stray returns at the start of the stream */ + while ((c = fz_peek_byte(state->chain)) == '\n' || c == '\r') + (void)fz_read_byte(state->chain); + cinfo->src = &state->srcmgr; cinfo->src->init_source = init_source; cinfo->src->fill_input_buffer = fill_input_buffer; diff --git a/fitz/filt_predict.c b/fitz/filt_predict.c index e68743d3..b82c1281 100644 --- a/fitz/filt_predict.c +++ b/fitz/filt_predict.c @@ -31,6 +31,7 @@ static inline int getcomponent(unsigned char *line, int x, int bpc) case 2: return (line[x >> 2] >> ( ( 3 - (x & 3) ) << 1 ) ) & 3; case 4: return (line[x >> 1] >> ( ( 1 - (x & 1) ) << 2 ) ) & 15; case 8: return line[x]; + case 16: return (line[x<<1]<<8)+line[(x<<1)+1]; } return 0; } @@ -43,6 +44,7 @@ static inline void putcomponent(unsigned char *buf, int x, int bpc, int value) case 2: buf[x >> 2] |= value << ((3 - (x & 3)) << 1); break; case 4: buf[x >> 1] |= value << ((1 - (x & 1)) << 2); break; case 8: buf[x] = value; break; + case 16: buf[x<<1] = value>>8; buf[(x<<1)+1] = value; break; } } @@ -50,9 +52,9 @@ static inline int paeth(int a, int b, int c) { /* The definitions of ac and bc are correct, not a typo. */ int ac = b - c, bc = a - c, abcc = ac + bc; - int pa = ABS(ac); - int pb = ABS(bc); - int pc = ABS(abcc); + int pa = fz_absi(ac); + int pb = fz_absi(bc); + int pc = fz_absi(abcc); return pa <= pb && pa <= pc ? a : pb <= pc ? b : c; } @@ -61,9 +63,11 @@ fz_predict_tiff(fz_predict *state, unsigned char *out, unsigned char *in, int le { int left[MAXC]; int i, k; + const int mask = (1 << state->bpc)-1; for (k = 0; k < state->colors; k++) left[k] = 0; + memset(out, 0, state->stride); for (i = 0; i < state->columns; i++) { @@ -71,7 +75,7 @@ fz_predict_tiff(fz_predict *state, unsigned char *out, unsigned char *in, int le { int a = getcomponent(in, i * state->colors + k, state->bpc); int b = a + left[k]; - int c = b % (1 << state->bpc); + int c = b & mask; putcomponent(out, i * state->colors + k, state->bpc, c); left[k] = c; } diff --git a/fitz/fitz-internal.h b/fitz/fitz-internal.h index b7401718..7130460a 100644 --- a/fitz/fitz-internal.h +++ b/fitz/fitz-internal.h @@ -831,6 +831,7 @@ void fz_transform_path(fz_context *ctx, fz_path *path, fz_matrix transform); fz_path *fz_clone_path(fz_context *ctx, fz_path *old); fz_rect fz_bound_path(fz_context *ctx, fz_path *path, fz_stroke_state *stroke, fz_matrix ctm); +void fz_adjust_rect_for_stroke(fz_rect *r, fz_stroke_state *stroke, fz_matrix *ctm); void fz_print_path(fz_context *ctx, FILE *out, fz_path *, int indent); fz_stroke_state *fz_new_stroke_state(fz_context *ctx); diff --git a/fitz/fitz.h b/fitz/fitz.h index 8fdb72da..520058b1 100644 --- a/fitz/fitz.h +++ b/fitz/fitz.h @@ -40,12 +40,6 @@ #define nelem(x) (sizeof(x)/sizeof((x)[0])) -#define ABS(x) ( (x) < 0 ? -(x) : (x) ) -#define MIN(a,b) ( (a) < (b) ? (a) : (b) ) -#define MAX(a,b) ( (a) > (b) ? (a) : (b) ) -#define CLAMP(x,a,b) ( (x) > (b) ? (b) : ( (x) < (a) ? (a) : (x) ) ) -#define DIV_BY_ZERO(a, b, min, max) (((a) < 0) ^ ((b) < 0) ? (min) : (max)) - /* Some differences in libc can be smoothed over */ @@ -127,6 +121,63 @@ int gettimeofday(struct timeval *tv, struct timezone *tz); #endif /* + Some standard math functions, done as static inlines for speed. + People with compilers that do not adequately implement inlines may + like to reimplement these using macros. +*/ +static inline float fz_abs(float f) +{ + return (f < 0 ? -f : f); +} + +static inline int fz_absi(int i) +{ + return (i < 0 ? -i : i); +} + +static inline float fz_min(float a, float b) +{ + return (a < b ? a : b); +} + +static inline int fz_mini(int a, int b) +{ + return (a < b ? a : b); +} + +static inline float fz_max(float a, float b) +{ + return (a > b ? a : b); +} + +static inline int fz_maxi(int a, int b) +{ + return (a > b ? a : b); +} + +static inline float fz_clamp(float f, float min, float max) +{ + return (f > min ? (f < max ? f : max) : min); +} + +static inline int fz_clampi(int i, float min, float max) +{ + return (i > min ? (i < max ? i : max) : min); +} + +static inline double fz_clampd(double d, double min, double max) +{ + return (d > min ? (d < max ? d : max) : min); +} + +static inline void *fz_clampp(void *p, void *min, void *max) +{ + return (p > min ? (p < max ? p : max) : min); +} + +#define DIV_BY_ZERO(a, b, min, max) (((a) < 0) ^ ((b) < 0) ? (min) : (max)) + +/* Contexts */ diff --git a/fitz/image_png.c b/fitz/image_png.c index 9e3f39c5..f28203ee 100644 --- a/fitz/image_png.c +++ b/fitz/image_png.c @@ -192,7 +192,7 @@ png_deinterlace(struct info *info, int *passw, int *passh, int *passofs) for (p = 0; p < 7; p++) { - unsigned char *sp = info->samples + (unsigned int)(passofs[p]); + unsigned char *sp = info->samples + (unsigned int)(passofs[p]); int w = passw[p]; int h = passh[p]; diff --git a/fitz/res_bitmap.c b/fitz/res_bitmap.c index 1bd2827d..4b871c23 100644 --- a/fitz/res_bitmap.c +++ b/fitz/res_bitmap.c @@ -117,5 +117,5 @@ void fz_bitmap_details(fz_bitmap *bit, int *w, int *h, int *n, int *stride) if (n) *n = bit->n; if (stride) - *w = bit->stride; + *stride = bit->stride; } diff --git a/fitz/res_colorspace.c b/fitz/res_colorspace.c index 587db381..c0898883 100644 --- a/fitz/res_colorspace.c +++ b/fitz/res_colorspace.c @@ -130,13 +130,13 @@ static void cmyk_to_rgb(fz_context *ctx, fz_colorspace *cs, float *cmyk, float * g += 0.2119 * x; b += 0.2235 * x; - rgb[0] = CLAMP(r, 0, 1); - rgb[1] = CLAMP(g, 0, 1); - rgb[2] = CLAMP(b, 0, 1); + rgb[0] = fz_clamp(r, 0, 1); + rgb[1] = fz_clamp(g, 0, 1); + rgb[2] = fz_clamp(b, 0, 1); #else - rgb[0] = 1 - MIN(1, cmyk[0] + cmyk[3]); - rgb[1] = 1 - MIN(1, cmyk[1] + cmyk[3]); - rgb[2] = 1 - MIN(1, cmyk[2] + cmyk[3]); + rgb[0] = 1 - fz_min(1, cmyk[0] + cmyk[3]); + rgb[1] = 1 - fz_min(1, cmyk[1] + cmyk[3]); + rgb[2] = 1 - fz_min(1, cmyk[2] + cmyk[3]); #endif } @@ -146,7 +146,7 @@ static void rgb_to_cmyk(fz_context *ctx, fz_colorspace *cs, float *rgb, float *c c = 1 - rgb[0]; m = 1 - rgb[1]; y = 1 - rgb[2]; - k = MIN(c, MIN(m, y)); + k = fz_min(c, fz_min(m, y)); cmyk[0] = c - k; cmyk[1] = m - k; cmyk[2] = y - k; @@ -251,7 +251,7 @@ static void fast_rgb_to_cmyk(fz_pixmap *dst, fz_pixmap *src) unsigned char c = 255 - s[0]; unsigned char m = 255 - s[1]; unsigned char y = 255 - s[2]; - unsigned char k = MIN(c, MIN(m, y)); + unsigned char k = (unsigned char)fz_mini(c, fz_mini(m, y)); d[0] = c - k; d[1] = m - k; d[2] = y - k; @@ -272,7 +272,7 @@ static void fast_bgr_to_cmyk(fz_pixmap *dst, fz_pixmap *src) unsigned char c = 255 - s[2]; unsigned char m = 255 - s[1]; unsigned char y = 255 - s[0]; - unsigned char k = MIN(c, MIN(m, y)); + unsigned char k = (unsigned char)fz_mini(c, fz_mini(m, y)); d[0] = c - k; d[1] = m - k; d[2] = y - k; @@ -293,7 +293,7 @@ static void fast_cmyk_to_gray(fz_pixmap *dst, fz_pixmap *src) unsigned char c = fz_mul255(s[0], 77); unsigned char m = fz_mul255(s[1], 150); unsigned char y = fz_mul255(s[2], 28); - d[0] = 255 - MIN(c + m + y + s[3], 255); + d[0] = 255 - (unsigned char)fz_mini(c + m + y + s[3], 255); d[1] = s[4]; s += 5; d += 2; @@ -318,9 +318,9 @@ static void fast_cmyk_to_rgb(fz_context *ctx, fz_pixmap *dst, fz_pixmap *src) d[1] = rgb[1] * 255; d[2] = rgb[2] * 255; #else - d[0] = 255 - MIN(s[0] + s[3], 255); - d[1] = 255 - MIN(s[1] + s[3], 255); - d[2] = 255 - MIN(s[2] + s[3], 255); + d[0] = 255 - (unsigned char)fz_mini(s[0] + s[3], 255); + d[1] = 255 - (unsigned char)fz_mini(s[1] + s[3], 255); + d[2] = 255 - (unsigned char)fz_mini(s[2] + s[3], 255); #endif d[3] = s[4]; s += 5; @@ -346,9 +346,9 @@ static void fast_cmyk_to_bgr(fz_context *ctx, fz_pixmap *dst, fz_pixmap *src) d[1] = rgb[1] * 255; d[2] = rgb[0] * 255; #else - d[0] = 255 - MIN(s[2] + s[3], 255); - d[1] = 255 - MIN(s[1] + s[3], 255); - d[2] = 255 - MIN(s[0] + s[3], 255); + d[0] = 255 - (unsigned char)fz_mini(s[2] + s[3], 255); + d[1] = 255 - (unsigned char)fz_mini(s[1] + s[3], 255); + d[2] = 255 - (unsigned char)fz_mini(s[0] + s[3], 255); #endif d[3] = s[4]; s += 5; @@ -557,7 +557,7 @@ fz_std_conv_color(fz_context *ctx, fz_colorspace *srcs, float *srcv, fz_colorspa srcs->to_rgb(ctx, srcs, srcv, rgb); dsts->from_rgb(ctx, dsts, rgb, dstv); for (i = 0; i < dsts->n; i++) - dstv[i] = CLAMP(dstv[i], 0, 1); + dstv[i] = fz_clamp(dstv[i], 0, 1); } else { @@ -605,7 +605,7 @@ fz_convert_color(fz_context *ctx, fz_colorspace *ds, float *dv, fz_colorspace *s float c = 1 - sv[0]; float m = 1 - sv[1]; float y = 1 - sv[2]; - float k = MIN(c, MIN(m, y)); + float k = fz_min(c, fz_min(m, y)); dv[0] = c - k; dv[1] = m - k; dv[2] = y - k; @@ -632,7 +632,7 @@ fz_convert_color(fz_context *ctx, fz_colorspace *ds, float *dv, fz_colorspace *s float c = 1 - sv[2]; float m = 1 - sv[1]; float y = 1 - sv[0]; - float k = MIN(c, MIN(m, y)); + float k = fz_min(c, fz_min(m, y)); dv[0] = c - k; dv[1] = m - k; dv[2] = y - k; @@ -649,16 +649,16 @@ fz_convert_color(fz_context *ctx, fz_colorspace *ds, float *dv, fz_colorspace *s float c = sv[0] * 0.3f; float m = sv[1] * 0.59f; float y = sv[2] * 0.11f; - dv[0] = 1 - MIN(c + m + y + sv[3], 1); + dv[0] = 1 - fz_min(c + m + y + sv[3], 1); } else if (ds == fz_device_rgb) { #ifdef SLOWCMYK cmyk_to_rgb(ctx, NULL, sv, dv); #else - dv[0] = 1 - MIN(sv[0] + sv[3], 1); - dv[1] = 1 - MIN(sv[1] + sv[3], 1); - dv[2] = 1 - MIN(sv[2] + sv[3], 1); + dv[0] = 1 - fz_min(sv[0] + sv[3], 1); + dv[1] = 1 - fz_min(sv[1] + sv[3], 1); + dv[2] = 1 - fz_min(sv[2] + sv[3], 1); #endif } else if (ds == fz_device_bgr) @@ -670,9 +670,9 @@ fz_convert_color(fz_context *ctx, fz_colorspace *ds, float *dv, fz_colorspace *s dv[1] = rgb[1]; dv[2] = rgb[0]; #else - dv[0] = 1 - MIN(sv[2] + sv[3], 1); - dv[1] = 1 - MIN(sv[1] + sv[3], 1); - dv[2] = 1 - MIN(sv[0] + sv[3], 1); + dv[0] = 1 - fz_min(sv[2] + sv[3], 1); + dv[1] = 1 - fz_min(sv[1] + sv[3], 1); + dv[2] = 1 - fz_min(sv[0] + sv[3], 1); #endif } else diff --git a/fitz/res_path.c b/fitz/res_path.c index e3f07192..b8a6a1a2 100644 --- a/fitz/res_path.c +++ b/fitz/res_path.c @@ -275,22 +275,32 @@ fz_bound_path(fz_context *ctx, fz_path *path, fz_stroke_state *stroke, fz_matrix if (stroke) { - float expand = stroke->linewidth; - if (expand == 0) - expand = 1.0f; - expand *= fz_matrix_max_expansion(ctm); - if ((stroke->linejoin == FZ_LINEJOIN_MITER || stroke->linejoin == FZ_LINEJOIN_MITER_XPS) && stroke->miterlimit > 1) - expand *= stroke->miterlimit; - r.x0 -= expand; - r.y0 -= expand; - r.x1 += expand; - r.y1 += expand; + fz_adjust_rect_for_stroke(&r, stroke, &ctm); } return r; } void +fz_adjust_rect_for_stroke(fz_rect *r, fz_stroke_state *stroke, fz_matrix *ctm) +{ + float expand; + + if (!stroke) + return; + expand = stroke->linewidth; + if (expand == 0) + expand = 1.0f; + expand *= fz_matrix_max_expansion(*ctm); + if ((stroke->linejoin == FZ_LINEJOIN_MITER || stroke->linejoin == FZ_LINEJOIN_MITER_XPS) && stroke->miterlimit > 1) + expand *= stroke->miterlimit; + r->x0 -= expand; + r->y0 -= expand; + r->x1 += expand; + r->y1 += expand; +} + +void fz_transform_path(fz_context *ctx, fz_path *path, fz_matrix ctm) { fz_point p; diff --git a/fitz/res_pixmap.c b/fitz/res_pixmap.c index b7cf5f58..e9dea86a 100644 --- a/fitz/res_pixmap.c +++ b/fitz/res_pixmap.c @@ -136,7 +136,7 @@ fz_pixmap_height(fz_context *ctx, fz_pixmap *pix) void fz_clear_pixmap(fz_context *ctx, fz_pixmap *pix) { - memset(pix->samples, 0, (unsigned int)(pix->w * pix->h * pix->n)); + memset(pix->samples, 0, (unsigned int)(pix->w * pix->h * pix->n)); } void @@ -223,7 +223,7 @@ fz_copy_pixmap_rect(fz_context *ctx, fz_pixmap *dest, fz_pixmap *src, fz_bbox r) for (x = w; x > 0; x--) { int v; - v = *srcp++; + v = *srcp++; v += *srcp++; v += *srcp++; *destp++ = (unsigned char)((v+1)/3); @@ -384,10 +384,10 @@ void fz_invert_pixmap_rect(fz_pixmap *image, fz_bbox rect) unsigned char *p; int x, y, n; - int x0 = CLAMP(rect.x0 - image->x, 0, image->w - 1); - int x1 = CLAMP(rect.x1 - image->x, 0, image->w - 1); - int y0 = CLAMP(rect.y0 - image->y, 0, image->h - 1); - int y1 = CLAMP(rect.y1 - image->y, 0, image->h - 1); + int x0 = fz_clampi(rect.x0 - image->x, 0, image->w - 1); + int x1 = fz_clampi(rect.x1 - image->x, 0, image->w - 1); + int y0 = fz_clampi(rect.y0 - image->y, 0, image->h - 1); + int y1 = fz_clampi(rect.y1 - image->y, 0, image->h - 1); for (y = y0; y < y1; y++) { diff --git a/fitz/res_text.c b/fitz/res_text.c index 6b5e3e3a..2bef6d4a 100644 --- a/fitz/res_text.c +++ b/fitz/res_text.c @@ -81,10 +81,10 @@ fz_bound_text(fz_context *ctx, fz_text *text, fz_matrix ctm) trm = fz_concat(tm, ctm); gbox = fz_bound_glyph(ctx, text->font, text->items[i].gid, trm); - bbox.x0 = MIN(bbox.x0, gbox.x0); - bbox.y0 = MIN(bbox.y0, gbox.y0); - bbox.x1 = MAX(bbox.x1, gbox.x1); - bbox.y1 = MAX(bbox.y1, gbox.y1); + bbox.x0 = fz_min(bbox.x0, gbox.x0); + bbox.y0 = fz_min(bbox.y0, gbox.y0); + bbox.x1 = fz_max(bbox.x1, gbox.x1); + bbox.y1 = fz_max(bbox.y1, gbox.y1); } } diff --git a/fitz/stm_buffer.c b/fitz/stm_buffer.c index a4b65cac..385817b4 100644 --- a/fitz/stm_buffer.c +++ b/fitz/stm_buffer.c @@ -123,9 +123,13 @@ void fz_write_buffer_byte(fz_context *ctx, fz_buffer *buf, int val) void fz_write_buffer_bits(fz_context *ctx, fz_buffer *buf, int val, int bits) { - int extra; int shift; + /* Throughout this code, the invariant is that we need to write the + * bottom 'bits' bits of 'val' into the stream. On entry we assume + * that val & ((1<<bits)-1) == val, but we do not rely on this after + * having written the first partial byte. */ + if (bits == 0) return; @@ -134,12 +138,16 @@ void fz_write_buffer_bits(fz_context *ctx, fz_buffer *buf, int val, int bits) * buf->unused_bits = the number of unused bits in the last byte. */ - /* Extend the buffer as required before we start; that way we never - * fail part way during writing. */ + /* Find the amount we need to shift val up by so that it will be in + * the correct position to be inserted into any existing data byte. */ shift = (buf->unused_bits - bits); + + /* Extend the buffer as required before we start; that way we never + * fail part way during writing. If shift < 0, then we'll need -shift + * more bits. */ if (shift < 0) { - extra = (7-buf->unused_bits)>>3; + int extra = (7-shift)>>3; /* Round up to bytes */ fz_ensure_buffer(ctx, buf, buf->len + extra); } @@ -148,8 +156,14 @@ void fz_write_buffer_bits(fz_context *ctx, fz_buffer *buf, int val, int bits) { buf->data[buf->len-1] |= (shift >= 0 ? (((unsigned int)val)<<shift) : (((unsigned int)val)>>-shift)); if (shift >= 0) + { + /* If we were shifting up, we're done. */ + buf->unused_bits -= bits; return; - bits += shift; + } + /* The number of bits left to write is the number that didn't + * fit in this first byte. */ + bits = -shift; } /* Write any whole bytes */ @@ -162,7 +176,7 @@ void fz_write_buffer_bits(fz_context *ctx, fz_buffer *buf, int val, int bits) /* Write trailing bits (with 0's in unused bits) */ if (bits > 0) { - bits += 8; + bits = 8-bits; buf->data[buf->len++] = val<<bits; } buf->unused_bits = bits; @@ -183,7 +197,7 @@ fz_buffer_printf(fz_context *ctx, fz_buffer *buffer, const char *fmt, ...) while(buffer->cap - buffer->len < 256) fz_grow_buffer(ctx, buffer); - buffer->len += vsprintf((char*)buffer->data + buffer->len, fmt, args); + buffer->len += vsprintf((char *)buffer->data + buffer->len, fmt, args); va_end(args); } @@ -263,3 +277,49 @@ fz_buffer_cat_pdf_string(fz_context *ctx, fz_buffer *buffer, const char *text) *d++ = ')'; buffer->len += len; } + +#ifdef TEST_BUFFER_WRITE + +#define TEST_LEN 1024 + +void +fz_test_buffer_write(fz_context *ctx) +{ + fz_buffer *master = fz_new_buffer(ctx, TEST_LEN); + fz_buffer *copy = fz_new_buffer(ctx, TEST_LEN); + fz_stream *stm; + int i, j, k; + + /* Make us a dummy buffer */ + for (i = 0; i < TEST_LEN; i++) + { + master->data[i] = rand(); + } + master->len = TEST_LEN; + + /* Now copy that buffer several times, checking it for validity */ + stm = fz_open_buffer(ctx, master); + for (i = 0; i < 256; i++) + { + memset(copy->data, i, TEST_LEN); + copy->len = 0; + j = TEST_LEN * 8; + do + { + k = (rand() & 31)+1; + if (k > j) + k = j; + fz_write_buffer_bits(ctx, copy, fz_read_bits(stm, k), k); + j -= k; + } + while (j); + + if (memcmp(copy->data, master->data, TEST_LEN) != 0) + fprintf(stderr, "Copied buffer is different!\n"); + fz_seek(stm, 0, 0); + } + fz_close(stm); + fz_drop_buffer(ctx, master); + fz_drop_buffer(ctx, copy); +} +#endif diff --git a/fitz/stm_open.c b/fitz/stm_open.c index be069fb9..1709340b 100644 --- a/fitz/stm_open.c +++ b/fitz/stm_open.c @@ -160,7 +160,7 @@ static void seek_buffer(fz_stream *stm, int offset, int whence) stm->rp += offset; if (whence == 2) stm->rp = stm->ep - offset; - stm->rp = CLAMP(stm->rp, stm->bp, stm->ep); + stm->rp = fz_clampp(stm->rp, stm->bp, stm->ep); stm->wp = stm->ep; } diff --git a/fitz/stm_read.c b/fitz/stm_read.c index 216d2207..848ceef1 100644 --- a/fitz/stm_read.c +++ b/fitz/stm_read.c @@ -7,7 +7,7 @@ fz_read(fz_stream *stm, unsigned char *buf, int len) { int count, n; - count = MIN(len, stm->wp - stm->rp); + count = fz_mini(len, stm->wp - stm->rp); if (count) { memcpy(buf, stm->rp, count); @@ -33,7 +33,7 @@ fz_read(fz_stream *stm, unsigned char *buf, int len) stm->pos += n; } - n = MIN(len - count, stm->wp - stm->rp); + n = fz_mini(len - count, stm->wp - stm->rp); if (n) { memcpy(buf + count, stm->rp, n); |