diff options
52 files changed, 458 insertions, 242 deletions
@@ -210,6 +210,8 @@ all: all-nojs $(JSTARGETS) all-nojs: $(THIRD_LIBS) $(FITZ_LIB) $(MUVIEW) $(MUDRAW) $(MUBUSY) +third: $(THIRD_LIBS) + clean: rm -rf $(OUT) nuke: diff --git a/apps/man/mupdf.1 b/apps/man/mupdf.1 index dc2dfca1..99179d69 100644 --- a/apps/man/mupdf.1 +++ b/apps/man/mupdf.1 @@ -1,4 +1,4 @@ -.TH MUPDF 1 "March 19, 2012" +.TH MUPDF 1 "June 12, 2012" .\" Please adjust this date whenever revising the manpage. .SH NAME mupdf \- MuPDF is a lightweight PDF viewer written in portable C @@ -49,7 +49,7 @@ Reload file. .B . pgdn right space Go to the next page .TP -.B , pgup left b +.B , pgup left b backspace Go to the previous page .TP .B <, > diff --git a/apps/mubusy.c b/apps/mubusy.c index b520d0ec..e11d488c 100644 --- a/apps/mubusy.c +++ b/apps/mubusy.c @@ -58,7 +58,7 @@ int main(int argc, char **argv) { strcpy(buf, "mupdf"); strcat(buf, tools[i].name); - if (namematch(end, start, buf)) + if (namematch(end, start, buf) || namematch(end, start, buf+2)) return tools[i].func(argc, argv); } } diff --git a/apps/mudraw.c b/apps/mudraw.c index 1d16ac25..dd5d88db 100644 --- a/apps/mudraw.c +++ b/apps/mudraw.c @@ -535,8 +535,8 @@ static void drawrange(fz_context *ctx, fz_document *doc, char *range) epage = pagecount; } - spage = CLAMP(spage, 1, pagecount); - epage = CLAMP(epage, 1, pagecount); + spage = fz_clampi(spage, 1, pagecount); + epage = fz_clampi(epage, 1, pagecount); if (spage < epage) for (page = spage; page <= epage; page++) diff --git a/apps/mupdfclean.c b/apps/mupdfclean.c index 8eb30efe..6b185668 100644 --- a/apps/mupdfclean.c +++ b/apps/mupdfclean.c @@ -87,8 +87,8 @@ static void retainpages(int argc, char **argv) if (spage > epage) page = spage, spage = epage, epage = page; - spage = CLAMP(spage, 1, pagecount); - epage = CLAMP(epage, 1, pagecount); + spage = fz_clampi(spage, 1, pagecount); + epage = fz_clampi(epage, 1, pagecount); for (page = spage; page <= epage; page++) { @@ -123,8 +123,9 @@ static void retainpages(int argc, char **argv) pdf_obj *names = pdf_new_dict(ctx, 1); pdf_obj *dests = pdf_new_dict(ctx, 1); pdf_obj *names_list = pdf_new_array(ctx, 32); + int len = pdf_dict_len(olddests); - for (i = 0; i < pdf_dict_len(olddests); i++) + for (i = 0; i < len; i++) { pdf_obj *key = pdf_dict_get_key(olddests, i); pdf_obj *val = pdf_dict_get_val(olddests, i); diff --git a/apps/mupdfextract.c b/apps/mupdfextract.c index 8db6ceaf..95f27be9 100644 --- a/apps/mupdfextract.c +++ b/apps/mupdfextract.c @@ -178,7 +178,8 @@ int pdfextract_main(int argc, char **argv) if (fz_optind == argc) { - for (o = 0; o < pdf_count_objects(doc); o++) + int len = pdf_count_objects(doc); + for (o = 0; o < len; o++) showobject(o); } else diff --git a/apps/mupdfinfo.c b/apps/mupdfinfo.c index 6e4db812..5bdf760e 100644 --- a/apps/mupdfinfo.c +++ b/apps/mupdfinfo.c @@ -202,6 +202,16 @@ gatherdimensions(int page, pdf_obj *pageref, pdf_obj *pageobj) bbox = pdf_to_rect(ctx, obj); + obj = pdf_dict_gets(pageobj, "UserUnit"); + if (pdf_is_real(obj)) + { + float unit = pdf_to_real(obj); + bbox.x0 *= unit; + bbox.y0 *= unit; + bbox.x1 *= unit; + bbox.y1 *= unit; + } + for (j = 0; j < dims; j++) if (!memcmp(dim[j].u.dim.bbox, &bbox, sizeof (fz_rect))) break; @@ -911,8 +921,8 @@ showinfo(char *filename, int show, char *pagelist) if (spage > epage) page = spage, spage = epage, epage = page; - spage = CLAMP(spage, 1, pagecount); - epage = CLAMP(epage, 1, pagecount); + spage = fz_clampi(spage, 1, pagecount); + epage = fz_clampi(epage, 1, pagecount); if (allpages) printf("Retrieving info from pages %d-%d...\n", spage, epage); diff --git a/apps/mupdfshow.c b/apps/mupdfshow.c index 252e7dc2..8e8425b0 100644 --- a/apps/mupdfshow.c +++ b/apps/mupdfshow.c @@ -142,9 +142,10 @@ static void showobject(int num, int gen) static void showgrep(char *filename) { pdf_obj *obj; - int i; + int i, len; - for (i = 0; i < pdf_count_objects(doc); i++) + len = pdf_count_objects(doc); + for (i = 0; i < len; i++) { if (doc->table[i].type == 'n' || doc->table[i].type == 'o') { diff --git a/apps/pdfapp.c b/apps/pdfapp.c index 6efa89ad..c02ed3f2 100644 --- a/apps/pdfapp.c +++ b/apps/pdfapp.c @@ -56,7 +56,7 @@ char *pdfapp_usage(pdfapp_t *app) "f\t\t-- fullscreen\n" "r\t\t-- reload file\n" ". pgdn right spc\t-- next page\n" - ", pgup left b\t-- previous page\n" + ", pgup left b bkspc\t-- previous page\n" ">\t\t-- next 10 pages\n" "<\t\t-- back 10 pages\n" "m\t\t-- mark page for snap back\n" @@ -894,6 +894,7 @@ void pdfapp_onkey(pdfapp_t *app, int c) app->pageno++; break; + case '\b': case 'b': panto = DONT_PAN; if (app->numberlen > 0) @@ -1131,10 +1132,10 @@ void pdfapp_onmouse(pdfapp_t *app, int x, int y, int btn, int modifiers, int sta if (app->iscopying) { app->iscopying = 0; - app->selr.x0 = MIN(app->selx, x) - app->panx + rect.x0; - app->selr.x1 = MAX(app->selx, x) - app->panx + rect.x0; - app->selr.y0 = MIN(app->sely, y) - app->pany + rect.y0; - app->selr.y1 = MAX(app->sely, y) - app->pany + rect.y0; + app->selr.x0 = fz_mini(app->selx, x) - app->panx + rect.x0; + app->selr.x1 = fz_maxi(app->selx, x) - app->panx + rect.x0; + app->selr.y0 = fz_mini(app->sely, y) - app->pany + rect.y0; + app->selr.y1 = fz_maxi(app->sely, y) - app->pany + rect.y0; winrepaint(app); if (app->selr.x0 < app->selr.x1 && app->selr.y0 < app->selr.y1) windocopy(app); @@ -1201,10 +1202,10 @@ void pdfapp_onmouse(pdfapp_t *app, int x, int y, int btn, int modifiers, int sta else if (app->iscopying) { - app->selr.x0 = MIN(app->selx, x) - app->panx + rect.x0; - app->selr.x1 = MAX(app->selx, x) - app->panx + rect.x0; - app->selr.y0 = MIN(app->sely, y) - app->pany + rect.y0; - app->selr.y1 = MAX(app->sely, y) - app->pany + rect.y0; + app->selr.x0 = fz_mini(app->selx, x) - app->panx + rect.x0; + app->selr.x1 = fz_maxi(app->selx, x) - app->panx + rect.x0; + app->selr.y0 = fz_mini(app->sely, y) - app->pany + rect.y0; + app->selr.y1 = fz_maxi(app->sely, y) - app->pany + rect.y0; winrepaint(app); } diff --git a/apps/x11_image.c b/apps/x11_image.c index 06764313..6392d868 100644 --- a/apps/x11_image.c +++ b/apps/x11_image.c @@ -432,10 +432,10 @@ ximage_blit(Drawable d, GC gc, for (ay = 0; ay < srch; ay += HEIGHT) { - h = MIN(srch - ay, HEIGHT); + h = fz_mini(srch - ay, HEIGHT); for (ax = 0; ax < srcw; ax += WIDTH) { - w = MIN(srcw - ax, WIDTH); + w = fz_mini(srcw - ax, WIDTH); image = next_pool_image(); diff --git a/cbz/mucbz.c b/cbz/mucbz.c index a8a1097a..f557236f 100644 --- a/cbz/mucbz.c +++ b/cbz/mucbz.c @@ -263,8 +263,8 @@ cbz_read_zip_dir(cbz_document *doc) fz_seek(file, 0, 2); filesize = fz_tell(file); - maxback = MIN(filesize, 0xFFFF + sizeof buf); - back = MIN(maxback, sizeof buf); + maxback = fz_mini(filesize, 0xFFFF + sizeof buf); + back = fz_mini(maxback, sizeof buf); while (back < maxback) { diff --git a/draw/draw_blend.c b/draw/draw_blend.c index 8a751c08..e70699d4 100644 --- a/draw/draw_blend.c +++ b/draw/draw_blend.c @@ -63,12 +63,12 @@ static inline int fz_overlay_byte(int b, int s) static inline int fz_darken_byte(int b, int s) { - return MIN(b, s); + return fz_mini(b, s); } static inline int fz_lighten_byte(int b, int s) { - return MAX(b, s); + return fz_maxi(b, s); } static inline int fz_color_dodge_byte(int b, int s) @@ -111,7 +111,7 @@ static inline int fz_soft_light_byte(int b, int s) static inline int fz_difference_byte(int b, int s) { - return ABS(b - s); + return fz_absi(b - s); } static inline int fz_exclusion_byte(int b, int s) @@ -139,13 +139,13 @@ fz_luminosity_rgb(unsigned char *rd, unsigned char *gd, unsigned char *bd, int r if (delta > 0) { int max; - max = MAX(r, MAX(g, b)); + max = fz_maxi(r, fz_maxi(g, b)); scale = (max == y ? 0 : ((255 - y) << 16) / (max - y)); } else { int min; - min = MIN(r, MIN(g, b)); + min = fz_mini(r, fz_mini(g, b)); scale = (y == min ? 0 : (y << 16) / (y - min)); } r = y + (((r - y) * scale + 0x8000) >> 16); @@ -153,9 +153,9 @@ fz_luminosity_rgb(unsigned char *rd, unsigned char *gd, unsigned char *bd, int r b = y + (((b - y) * scale + 0x8000) >> 16); } - *rd = CLAMP(r, 0, 255); - *gd = CLAMP(g, 0, 255); - *bd = CLAMP(b, 0, 255); + *rd = fz_clampi(r, 0, 255); + *gd = fz_clampi(g, 0, 255); + *bd = fz_clampi(b, 0, 255); } static void @@ -167,20 +167,20 @@ fz_saturation_rgb(unsigned char *rd, unsigned char *gd, unsigned char *bd, int r int scale; int r, g, b; - minb = MIN(rb, MIN(gb, bb)); - maxb = MAX(rb, MAX(gb, bb)); + minb = fz_mini(rb, fz_mini(gb, bb)); + maxb = fz_maxi(rb, fz_maxi(gb, bb)); if (minb == maxb) { /* backdrop has zero saturation, avoid divide by 0 */ - gb = CLAMP(gb, 0, 255); + gb = fz_clampi(gb, 0, 255); *rd = gb; *gd = gb; *bd = gb; return; } - mins = MIN(rs, MIN(gs, bs)); - maxs = MAX(rs, MAX(gs, bs)); + mins = fz_mini(rs, fz_mini(gs, bs)); + maxs = fz_maxi(rs, fz_maxi(gs, bs)); scale = ((maxs - mins) << 16) / (maxb - minb); y = (rb * 77 + gb * 151 + bb * 28 + 0x80) >> 8; @@ -193,8 +193,8 @@ fz_saturation_rgb(unsigned char *rd, unsigned char *gd, unsigned char *bd, int r int scalemin, scalemax; int min, max; - min = MIN(r, MIN(g, b)); - max = MAX(r, MAX(g, b)); + min = fz_mini(r, fz_mini(g, b)); + max = fz_maxi(r, fz_maxi(g, b)); if (min < 0) scalemin = (y << 16) / (y - min); @@ -206,15 +206,15 @@ fz_saturation_rgb(unsigned char *rd, unsigned char *gd, unsigned char *bd, int r else scalemax = 0x10000; - scale = MIN(scalemin, scalemax); + scale = fz_mini(scalemin, scalemax); r = y + (((r - y) * scale + 0x8000) >> 16); g = y + (((g - y) * scale + 0x8000) >> 16); b = y + (((b - y) * scale + 0x8000) >> 16); } - *rd = CLAMP(r, 0, 255); - *gd = CLAMP(g, 0, 255); - *bd = CLAMP(b, 0, 255); + *rd = fz_clampi(r, 0, 255); + *gd = fz_clampi(g, 0, 255); + *bd = fz_clampi(b, 0, 255); } static void diff --git a/draw/draw_edge.c b/draw/draw_edge.c index eac7cfba..48496acd 100644 --- a/draw/draw_edge.c +++ b/draw/draw_edge.c @@ -316,7 +316,7 @@ fz_insert_gel_raw(fz_gel *gel, int x0, int y0, int x1, int y1) dy = y1 - y0; dx = x1 - x0; - width = ABS(dx); + width = fz_absi(dx); edge->xdir = dx > 0 ? 1 : -1; edge->ydir = winding; @@ -356,10 +356,10 @@ fz_insert_gel(fz_gel *gel, float fx0, float fy0, float fx1, float fy1) fy0 = floorf(fy0 * fz_aa_vscale); fy1 = floorf(fy1 * fz_aa_vscale); - x0 = CLAMP(fx0, BBOX_MIN * fz_aa_hscale, BBOX_MAX * fz_aa_hscale); - y0 = CLAMP(fy0, BBOX_MIN * fz_aa_vscale, BBOX_MAX * fz_aa_vscale); - x1 = CLAMP(fx1, BBOX_MIN * fz_aa_hscale, BBOX_MAX * fz_aa_hscale); - y1 = CLAMP(fy1, BBOX_MIN * fz_aa_vscale, BBOX_MAX * fz_aa_vscale); + x0 = fz_clampi(fx0, BBOX_MIN * fz_aa_hscale, BBOX_MAX * fz_aa_hscale); + y0 = fz_clampi(fy0, BBOX_MIN * fz_aa_vscale, BBOX_MAX * fz_aa_vscale); + x1 = fz_clampi(fx1, BBOX_MIN * fz_aa_hscale, BBOX_MAX * fz_aa_hscale); + y1 = fz_clampi(fy1, BBOX_MIN * fz_aa_vscale, BBOX_MAX * fz_aa_vscale); d = clip_lerp_y(gel->clip.y0, 0, x0, y0, x1, y1, &v); if (d == OUTSIDE) return; @@ -721,8 +721,8 @@ static inline void blit_sharp(int x0, int x1, int y, fz_bbox clip, fz_pixmap *dst, unsigned char *color) { unsigned char *dp; - x0 = CLAMP(x0, dst->x, dst->x + dst->w); - x1 = CLAMP(x1, dst->x, dst->x + dst->w); + x0 = fz_clampi(x0, dst->x, dst->x + dst->w); + x1 = fz_clampi(x1, dst->x, dst->x + dst->w); if (x0 < x1) { dp = dst->samples + (unsigned int)(( (y - dst->y) * dst->w + (x0 - dst->x) ) * dst->n); diff --git a/draw/draw_path.c b/draw/draw_path.c index 755283b7..b0889279 100644 --- a/draw/draw_path.c +++ b/draw/draw_path.c @@ -28,10 +28,10 @@ bezier(fz_gel *gel, fz_matrix *ctm, float flatness, float xabcd, yabcd; /* termination check */ - dmax = ABS(xa - xb); - dmax = MAX(dmax, ABS(ya - yb)); - dmax = MAX(dmax, ABS(xd - xc)); - dmax = MAX(dmax, ABS(yd - yc)); + dmax = fz_abs(xa - xb); + dmax = fz_max(dmax, fz_abs(ya - yb)); + dmax = fz_max(dmax, fz_abs(xd - xc)); + dmax = fz_max(dmax, fz_abs(yd - yc)); if (dmax < flatness || depth >= MAX_DEPTH) { line(gel, ctm, xa, ya, xd, yd); @@ -499,10 +499,10 @@ fz_stroke_bezier(struct sctx *s, float xabcd, yabcd; /* termination check */ - dmax = ABS(xa - xb); - dmax = MAX(dmax, ABS(ya - yb)); - dmax = MAX(dmax, ABS(xd - xc)); - dmax = MAX(dmax, ABS(yd - yc)); + dmax = fz_abs(xa - xb); + dmax = fz_max(dmax, fz_abs(ya - yb)); + dmax = fz_max(dmax, fz_abs(xd - xc)); + dmax = fz_max(dmax, fz_abs(yd - yc)); if (dmax < s->flatness || depth >= MAX_DEPTH) { fz_point p; @@ -705,10 +705,10 @@ fz_dash_bezier(struct sctx *s, float xabcd, yabcd; /* termination check */ - dmax = ABS(xa - xb); - dmax = MAX(dmax, ABS(ya - yb)); - dmax = MAX(dmax, ABS(xd - xc)); - dmax = MAX(dmax, ABS(yd - yc)); + dmax = fz_abs(xa - xb); + dmax = fz_max(dmax, fz_abs(ya - yb)); + dmax = fz_max(dmax, fz_abs(xd - xc)); + dmax = fz_max(dmax, fz_abs(yd - yc)); if (dmax < s->flatness || depth >= MAX_DEPTH) { fz_point p; @@ -780,7 +780,7 @@ fz_flatten_dash_path(fz_gel *gel, fz_path *path, fz_stroke_state *stroke, fz_mat phase_len = 0; for (i = 0; i < stroke->dash_len; i++) phase_len += stroke->dash_list[i]; - max_expand = MAX(MAX(fabs(ctm.a),fabs(ctm.b)),MAX(fabs(ctm.c),fabs(ctm.d))); + max_expand = fz_max(fz_max(fz_abs(ctm.a),fz_abs(ctm.b)),fz_max(fz_abs(ctm.c),fz_abs(ctm.d))); if (phase_len < 0.01f || phase_len * max_expand < 0.5f) { fz_flatten_stroke_path(gel, path, stroke, ctm, flatness, linewidth); diff --git a/draw/draw_unpack.c b/draw/draw_unpack.c index 39c287c6..e7952d04 100644 --- a/draw/draw_unpack.c +++ b/draw/draw_unpack.c @@ -199,7 +199,7 @@ fz_decode_indexed_tile(fz_pixmap *pix, float *decode, int maxval) for (k = 0; k < n; k++) { int value = (add[k] + (((p[k] << 8) * mul[k]) >> 8)) >> 8; - p[k] = CLAMP(value, 0, 255); + p[k] = fz_clampi(value, 0, 255); } p += n + 1; } @@ -212,7 +212,7 @@ fz_decode_tile(fz_pixmap *pix, float *decode) int mul[FZ_MAX_COLORS]; unsigned char *p = pix->samples; int len = pix->w * pix->h; - int n = MAX(1, pix->n - 1); + int n = fz_maxi(1, pix->n - 1); int needed; int k; @@ -234,7 +234,7 @@ fz_decode_tile(fz_pixmap *pix, float *decode) for (k = 0; k < n; k++) { int value = add[k] + fz_mul255(p[k], mul[k]); - p[k] = CLAMP(value, 0, 255); + p[k] = fz_clampi(value, 0, 255); } p += pix->n; } 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); diff --git a/pdf/pdf_annot.c b/pdf/pdf_annot.c index 5109ae45..02f91e33 100644 --- a/pdf/pdf_annot.c +++ b/pdf/pdf_annot.c @@ -415,16 +415,12 @@ pdf_load_annots(pdf_document *xref, pdf_obj *annots, fz_matrix page_ctm) annot->next = NULL; - - if (annot) + if (!head) + head = tail = annot; + else { - if (!head) - head = tail = annot; - else - { - tail->next = annot; - tail = annot; - } + tail->next = annot; + tail = annot; } } } diff --git a/pdf/pdf_colorspace.c b/pdf/pdf_colorspace.c index ee5bc5fc..13323ce3 100644 --- a/pdf/pdf_colorspace.c +++ b/pdf/pdf_colorspace.c @@ -47,9 +47,9 @@ lab_to_rgb(fz_context *ctx, fz_colorspace *cs, float *lab, float *rgb) r = (3.240449f * x + -1.537136f * y + -0.498531f * z) * 0.830026f; g = (-0.969265f * x + 1.876011f * y + 0.041556f * z) * 1.05452f; b = (0.055643f * x + -0.204026f * y + 1.057229f * z) * 1.1003f; - rgb[0] = sqrtf(CLAMP(r, 0, 1)); - rgb[1] = sqrtf(CLAMP(g, 0, 1)); - rgb[2] = sqrtf(CLAMP(b, 0, 1)); + rgb[0] = sqrtf(fz_clamp(r, 0, 1)); + rgb[1] = sqrtf(fz_clamp(g, 0, 1)); + rgb[2] = sqrtf(fz_clamp(b, 0, 1)); } static void @@ -160,7 +160,7 @@ indexed_to_rgb(fz_context *ctx, fz_colorspace *cs, float *color, float *rgb) float alt[FZ_MAX_COLORS]; int i, k; i = color[0] * 255; - i = CLAMP(i, 0, idx->high); + i = fz_clampi(i, 0, idx->high); for (k = 0; k < idx->base->n; k++) alt[k] = idx->lookup[i * idx->base->n + k] / 255.0f; idx->base->to_rgb(ctx, idx->base, alt, rgb); @@ -203,7 +203,7 @@ pdf_expand_indexed_pixmap(fz_context *ctx, fz_pixmap *src) { int v = *s++; int a = *s++; - v = MIN(v, high); + v = fz_mini(v, high); for (k = 0; k < n; k++) *d++ = fz_mul255(lookup[v * n + k], a); *d++ = a; @@ -240,7 +240,7 @@ load_indexed(pdf_document *xref, pdf_obj *array) idx->lookup = NULL; idx->base = base; idx->high = pdf_to_int(highobj); - idx->high = CLAMP(idx->high, 0, 255); + idx->high = fz_clampi(idx->high, 0, 255); n = base->n * (idx->high + 1); idx->lookup = fz_malloc_array(ctx, 1, n); diff --git a/pdf/pdf_font.c b/pdf/pdf_font.c index 3c1a3205..f8fb643c 100644 --- a/pdf/pdf_font.c +++ b/pdf/pdf_font.c @@ -175,9 +175,19 @@ static int lookup_mre_code(char *name) static void pdf_load_builtin_font(fz_context *ctx, pdf_font_desc *fontdesc, char *fontname) { + char buf[256], *comma = NULL; unsigned char *data; unsigned int len; + if (strchr(fontname, ',')) + { + fz_strlcpy(buf, fontname, sizeof buf); + comma = strchr(buf, ','); + if (comma) + *comma++ = 0; + fontname = buf; + } + data = pdf_lookup_builtin_font(fontname, &len); if (!data) fz_throw(ctx, "cannot find builtin font: '%s'", fontname); @@ -187,6 +197,14 @@ pdf_load_builtin_font(fz_context *ctx, pdf_font_desc *fontdesc, char *fontname) if (!strcmp(fontname, "Symbol") || !strcmp(fontname, "ZapfDingbats")) fontdesc->flags |= PDF_FD_SYMBOLIC; + + if (comma) + { + if (strstr(comma, "Italic")) + fontdesc->font->ft_italic = 1; + if (strstr(comma, "Bold")) + fontdesc->font->ft_bold = 1; + } } static void @@ -742,7 +760,7 @@ load_cid_font(pdf_document *xref, pdf_obj *dict, pdf_obj *encoding, pdf_obj *to_ fz_throw(ctx, "cid font is missing info"); obj = pdf_dict_gets(cidinfo, "Registry"); - tmplen = MIN(sizeof tmpstr - 1, pdf_to_str_len(obj)); + tmplen = fz_mini(sizeof tmpstr - 1, pdf_to_str_len(obj)); memcpy(tmpstr, pdf_to_str_buf(obj), tmplen); tmpstr[tmplen] = '\0'; fz_strlcpy(collection, tmpstr, sizeof collection); @@ -750,7 +768,7 @@ load_cid_font(pdf_document *xref, pdf_obj *dict, pdf_obj *encoding, pdf_obj *to_ fz_strlcat(collection, "-", sizeof collection); obj = pdf_dict_gets(cidinfo, "Ordering"); - tmplen = MIN(sizeof tmpstr - 1, pdf_to_str_len(obj)); + tmplen = fz_mini(sizeof tmpstr - 1, pdf_to_str_len(obj)); memcpy(tmpstr, pdf_to_str_buf(obj), tmplen); tmpstr[tmplen] = '\0'; fz_strlcat(collection, tmpstr, sizeof collection); @@ -1206,4 +1224,4 @@ float pdf_text_stride(fz_context *ctx, pdf_font_desc *fontdesc, float fontsize, *count = i; return x; -}
\ No newline at end of file +} diff --git a/pdf/pdf_form.c b/pdf/pdf_form.c index 3a9c7a46..dbc3a326 100644 --- a/pdf/pdf_form.c +++ b/pdf/pdf_form.c @@ -632,7 +632,7 @@ static int text_splitter_layout(fz_context *ctx, text_splitter *splitter) vstretchwidth = splitter->width * (splitter->max_lines + 1) * splitter->fontsize / splitter->height; - bestwidth = MIN(fitwidth, MIN(hstretchwidth, vstretchwidth)); + bestwidth = fz_min(fitwidth, fz_min(hstretchwidth, vstretchwidth)); if (bestwidth == vstretchwidth) splitter->max_lines ++; @@ -827,7 +827,7 @@ static fz_buffer *create_text_appearance(pdf_document *doc, fz_rect *bbox, fz_ma } else if (info->comb) { - int i, n = MIN((int)strlen(text), info->max_len); + int i, n = fz_mini((int)strlen(text), info->max_len); float comb_width = full_width/info->max_len; float char_width = pdf_text_stride(ctx, info->font_rec.font, fontsize, (unsigned char *)"M", 1, FLT_MAX, NULL); float init_skip = (comb_width - char_width)/2.0; diff --git a/pdf/pdf_function.c b/pdf/pdf_function.c index 9a3bf1c2..7c123476 100644 --- a/pdf/pdf_function.c +++ b/pdf/pdf_function.c @@ -209,7 +209,7 @@ ps_push_real(ps_stack *st, float n) * cause a divide by 0. Same reason as in fz_atof. */ n = 1.0; } - st->stack[st->sp].u.f = CLAMP(n, -FLT_MAX, FLT_MAX); + st->stack[st->sp].u.f = fz_clamp(n, -FLT_MAX, FLT_MAX); st->sp++; } } @@ -897,7 +897,7 @@ eval_postscript_func(fz_context *ctx, pdf_function *func, float *in, float *out) for (i = 0; i < func->m; i++) { - x = CLAMP(in[i], func->domain[i][0], func->domain[i][1]); + x = fz_clamp(in[i], func->domain[i][0], func->domain[i][1]); ps_push_real(&st, x); } @@ -906,7 +906,7 @@ eval_postscript_func(fz_context *ctx, pdf_function *func, float *in, float *out) for (i = func->n - 1; i >= 0; i--) { x = ps_pop_real(&st); - out[i] = CLAMP(x, func->range[i][0], func->range[i][1]); + out[i] = fz_clamp(x, func->range[i][0], func->range[i][1]); } } @@ -1068,10 +1068,10 @@ eval_sample_func(fz_context *ctx, pdf_function *func, float *in, float *out) /* encode input coordinates */ for (i = 0; i < func->m; i++) { - x = CLAMP(in[i], func->domain[i][0], func->domain[i][1]); + x = fz_clamp(in[i], func->domain[i][0], func->domain[i][1]); x = lerp(x, func->domain[i][0], func->domain[i][1], func->u.sa.encode[i][0], func->u.sa.encode[i][1]); - x = CLAMP(x, 0, func->u.sa.size[i] - 1); + x = fz_clamp(x, 0, func->u.sa.size[i] - 1); e0[i] = floorf(x); e1[i] = ceilf(x); efrac[i] = x - floorf(x); @@ -1091,7 +1091,7 @@ eval_sample_func(fz_context *ctx, pdf_function *func, float *in, float *out) float ab = a + (b - a) * efrac[0]; out[i] = lerp(ab, 0, 1, func->u.sa.decode[i][0], func->u.sa.decode[i][1]); - out[i] = CLAMP(out[i], func->range[i][0], func->range[i][1]); + out[i] = fz_clamp(out[i], func->range[i][0], func->range[i][1]); } else if (func->m == 2) @@ -1109,14 +1109,14 @@ eval_sample_func(fz_context *ctx, pdf_function *func, float *in, float *out) float abcd = ab + (cd - ab) * efrac[1]; out[i] = lerp(abcd, 0, 1, func->u.sa.decode[i][0], func->u.sa.decode[i][1]); - out[i] = CLAMP(out[i], func->range[i][0], func->range[i][1]); + out[i] = fz_clamp(out[i], func->range[i][0], func->range[i][1]); } else { float x = interpolate_sample(func, scale, e0, e1, efrac, func->m - 1, i); out[i] = lerp(x, 0, 1, func->u.sa.decode[i][0], func->u.sa.decode[i][1]); - out[i] = CLAMP(out[i], func->range[i][0], func->range[i][1]); + out[i] = fz_clamp(out[i], func->range[i][0], func->range[i][1]); } } } @@ -1177,7 +1177,7 @@ eval_exponential_func(fz_context *ctx, pdf_function *func, float in, float *out) float tmp; int i; - x = CLAMP(x, func->domain[0][0], func->domain[0][1]); + x = fz_clamp(x, func->domain[0][0], func->domain[0][1]); /* constraint */ if ((func->u.e.n != (int)func->u.e.n && x < 0) || (func->u.e.n < 0 && x == 0)) @@ -1191,7 +1191,7 @@ eval_exponential_func(fz_context *ctx, pdf_function *func, float in, float *out) { out[i] = func->u.e.c0[i] + tmp * (func->u.e.c1[i] - func->u.e.c0[i]); if (func->has_range) - out[i] = CLAMP(out[i], func->range[i][0], func->range[i][1]); + out[i] = fz_clamp(out[i], func->range[i][0], func->range[i][1]); } } @@ -1287,7 +1287,7 @@ eval_stitching_func(fz_context *ctx, pdf_function *func, float in, float *out) float *bounds = func->u.st.bounds; int i; - in = CLAMP(in, func->domain[0][0], func->domain[0][1]); + in = fz_clamp(in, func->domain[0][0], func->domain[0][1]); for (i = 0; i < k - 1; i++) { diff --git a/pdf/pdf_interpret.c b/pdf/pdf_interpret.c index a2fdacb9..ab55ee66 100644 --- a/pdf/pdf_interpret.c +++ b/pdf/pdf_interpret.c @@ -556,10 +556,10 @@ pdf_flush_text(pdf_csi *csi) { pdf_gstate *gstate = csi->gstate + csi->gtop; fz_text *text; - int dofill = 0; - int dostroke = 0; - int doclip = 0; - int doinvisible = 0; + int dofill; + int dostroke; + int doclip; + int doinvisible; fz_context *ctx = csi->dev->ctx; if (!csi->text) @@ -590,14 +590,6 @@ pdf_flush_text(pdf_csi *csi) if (doinvisible) fz_ignore_text(csi->dev, text, gstate->ctm); - if (doclip) - { - if (csi->accumulate < 2) - gstate->clip_depth++; - fz_clip_text(csi->dev, text, gstate->ctm, csi->accumulate); - csi->accumulate = 2; - } - if (dofill) { switch (gstate->fill.kind) @@ -656,6 +648,14 @@ pdf_flush_text(pdf_csi *csi) } } + if (doclip) + { + if (csi->accumulate < 2) + gstate->clip_depth++; + fz_clip_text(csi->dev, text, gstate->ctm, csi->accumulate); + csi->accumulate = 2; + } + pdf_end_group(csi); } fz_catch(ctx) @@ -1521,10 +1521,10 @@ pdf_run_extgstate(pdf_csi *csi, pdf_obj *rdb, pdf_obj *extgstate) } else if (!strcmp(s, "CA")) - gstate->stroke.alpha = pdf_to_real(val); + gstate->stroke.alpha = fz_clamp(pdf_to_real(val), 0, 1); else if (!strcmp(s, "ca")) - gstate->fill.alpha = pdf_to_real(val); + gstate->fill.alpha = fz_clamp(pdf_to_real(val), 0, 1); else if (!strcmp(s, "BM")) { diff --git a/pdf/pdf_nametree.c b/pdf/pdf_nametree.c index 73755e22..7d8ac319 100644 --- a/pdf/pdf_nametree.c +++ b/pdf/pdf_nametree.c @@ -120,14 +120,16 @@ pdf_load_name_tree_imp(pdf_obj *dict, pdf_document *xref, pdf_obj *node) if (kids && !pdf_dict_mark(node)) { - for (i = 0; i < pdf_array_len(kids); i++) + int len = pdf_array_len(kids); + for (i = 0; i < len; i++) pdf_load_name_tree_imp(dict, xref, pdf_array_get(kids, i)); pdf_dict_unmark(node); } if (names) { - for (i = 0; i + 1 < pdf_array_len(names); i += 2) + int len = pdf_array_len(names); + for (i = 0; i + 1 < len; i += 2) { pdf_obj *key = pdf_array_get(names, i); pdf_obj *val = pdf_array_get(names, i + 1); diff --git a/pdf/pdf_object.c b/pdf/pdf_object.c index 2f96f213..531eb8eb 100644 --- a/pdf/pdf_object.c +++ b/pdf/pdf_object.c @@ -558,9 +558,10 @@ pdf_array_insert(pdf_obj *obj, pdf_obj *item) int pdf_array_contains(pdf_obj *arr, pdf_obj *obj) { - int i; + int i, len; - for (i = 0; i < pdf_array_len(arr); i++) + len = pdf_array_len(arr); + for (i = 0; i < len; i++) if (!pdf_objcmp(pdf_array_get(arr, i), obj)) return 1; diff --git a/pdf/pdf_page.c b/pdf/pdf_page.c index d348ad40..8715590c 100644 --- a/pdf/pdf_page.c +++ b/pdf/pdf_page.c @@ -290,6 +290,7 @@ pdf_load_page(pdf_document *xref, int number) pdf_obj *pageobj, *pageref, *obj; fz_rect mediabox, cropbox, realbox; fz_matrix ctm; + float userunit; pdf_load_page_tree(xref); if (number < 0 || number >= xref->page_len) @@ -305,6 +306,12 @@ pdf_load_page(pdf_document *xref, int number) page->links = NULL; page->annots = NULL; + obj = pdf_dict_gets(pageobj, "UserUnit"); + if (pdf_is_real(obj)) + userunit = pdf_to_real(obj); + else + userunit = 1; + mediabox = pdf_to_rect(ctx, pdf_dict_gets(pageobj, "MediaBox")); if (fz_is_empty_rect(mediabox)) { @@ -319,10 +326,10 @@ pdf_load_page(pdf_document *xref, int number) if (!fz_is_empty_rect(cropbox)) mediabox = fz_intersect_rect(mediabox, cropbox); - page->mediabox.x0 = MIN(mediabox.x0, mediabox.x1); - page->mediabox.y0 = MIN(mediabox.y0, mediabox.y1); - page->mediabox.x1 = MAX(mediabox.x0, mediabox.x1); - page->mediabox.y1 = MAX(mediabox.y0, mediabox.y1); + page->mediabox.x0 = fz_min(mediabox.x0, mediabox.x1) * userunit; + page->mediabox.y0 = fz_min(mediabox.y0, mediabox.y1) * userunit; + page->mediabox.x1 = fz_max(mediabox.x0, mediabox.x1) * userunit; + page->mediabox.y1 = fz_max(mediabox.y0, mediabox.y1) * userunit; if (page->mediabox.x1 - page->mediabox.x0 < 1 || page->mediabox.y1 - page->mediabox.y0 < 1) { @@ -342,7 +349,9 @@ pdf_load_page(pdf_document *xref, int number) ctm = fz_concat(fz_rotate(-page->rotate), fz_scale(1, -1)); realbox = fz_transform_rect(ctm, page->mediabox); - page->ctm = fz_concat(ctm, fz_translate(-realbox.x0, -realbox.y0)); + ctm = fz_concat(ctm, fz_scale(userunit, userunit)); + ctm = fz_concat(ctm, fz_translate(-realbox.x0, -realbox.y0)); + page->ctm = ctm; obj = pdf_dict_gets(pageobj, "Annots"); if (obj) diff --git a/pdf/pdf_parse.c b/pdf/pdf_parse.c index 5018844c..0ba6b0a4 100644 --- a/pdf/pdf_parse.c +++ b/pdf/pdf_parse.c @@ -9,10 +9,10 @@ pdf_to_rect(fz_context *ctx, pdf_obj *array) float b = pdf_to_real(pdf_array_get(array, 1)); float c = pdf_to_real(pdf_array_get(array, 2)); float d = pdf_to_real(pdf_array_get(array, 3)); - r.x0 = MIN(a, c); - r.y0 = MIN(b, d); - r.x1 = MAX(a, c); - r.y1 = MAX(b, d); + r.x0 = fz_min(a, c); + r.y0 = fz_min(b, d); + r.x1 = fz_max(a, c); + r.y1 = fz_max(b, d); return r; } diff --git a/pdf/pdf_repair.c b/pdf/pdf_repair.c index 85709219..0874c2f8 100644 --- a/pdf/pdf_repair.c +++ b/pdf/pdf_repair.c @@ -237,7 +237,7 @@ pdf_repair_xref(pdf_document *xref, pdf_lexbuf *buf) list = fz_malloc_array(ctx, listcap, sizeof(struct entry)); /* look for '%PDF' version marker within first kilobyte of file */ - n = fz_read(xref->file, (unsigned char *)buf->scratch, MIN(buf->size, 1024)); + n = fz_read(xref->file, (unsigned char *)buf->scratch, fz_mini(buf->size, 1024)); if (n < 0) fz_throw(ctx, "cannot read from file"); diff --git a/pdf/pdf_shade.c b/pdf/pdf_shade.c index 0847f4d2..2d9e74b8 100644 --- a/pdf/pdf_shade.c +++ b/pdf/pdf_shade.c @@ -177,14 +177,14 @@ draw_stripe(fz_context *ctx, pdf_tensor_patch *p, fz_shade *shade, int depth) if (depth == 0) { /* if no more subdividing, draw two new patches... */ - triangulate_patch(ctx, s0, shade); triangulate_patch(ctx, s1, shade); + triangulate_patch(ctx, s0, shade); } else { /* ...otherwise, continue subdividing. */ - draw_stripe(ctx, &s0, shade, depth); draw_stripe(ctx, &s1, shade, depth); + draw_stripe(ctx, &s0, shade, depth); } } diff --git a/pdf/pdf_write.c b/pdf/pdf_write.c index 8d32febe..3527961f 100644 --- a/pdf/pdf_write.c +++ b/pdf/pdf_write.c @@ -151,7 +151,6 @@ static void page_objects_insert(fz_context *ctx, page_objects **ppo, int i) { page_objects *po; - int j; /* Make a page_objects if we don't have one */ if (*ppo == NULL) @@ -165,7 +164,6 @@ page_objects_insert(fz_context *ctx, page_objects **ppo, int i) po->cap *= 2; *ppo = po; } - j = po->len; po->object[po->len++] = i; } @@ -585,11 +583,11 @@ static void removeduplicateobjs(pdf_document *xref, pdf_write_options *opts) continue; /* Keep the lowest numbered object */ - newnum = MIN(num, other); + newnum = fz_mini(num, other); opts->renumber_map[num] = newnum; opts->renumber_map[other] = newnum; opts->rev_renumber_map[newnum] = num; /* Either will do */ - opts->use_list[MAX(num, other)] = 0; + opts->use_list[fz_maxi(num, other)] = 0; /* One duplicate was found, do not look for another */ break; @@ -1516,6 +1514,34 @@ static void expandstream(pdf_document *xref, pdf_write_options *opts, pdf_obj *o pdf_drop_obj(obj); } +static int is_image_filter(char *s) +{ + if ( !strcmp(s, "CCITTFaxDecode") || !strcmp(s, "CCF") || + !strcmp(s, "DCTDecode") || !strcmp(s, "DCT") || + !strcmp(s, "RunLengthDecode") || !strcmp(s, "RL") || + !strcmp(s, "JBIG2Decode") || + !strcmp(s, "JPXDecode")) + return 1; + return 0; +} + +static int filter_implies_image(pdf_document *xref, pdf_obj *o) +{ + if (!o) + return 0; + if (pdf_is_name(o)) + return is_image_filter(pdf_to_name(o)); + if (pdf_is_array(o)) + { + int i, len; + len = pdf_array_len(o); + for (i = 0; i < len; i++) + if (is_image_filter(pdf_to_name(pdf_array_get(o, i)))) + return 1; + } + return 0; +} + static void writeobject(pdf_document *xref, pdf_write_options *opts, int num, int gen) { pdf_obj *obj; @@ -1578,6 +1604,10 @@ static void writeobject(pdf_document *xref, pdf_write_options *opts, int num, in dontexpand = !(opts->do_expand & fz_expand_fonts); if (o = pdf_dict_gets(obj, "Subtype"), !strcmp(pdf_to_name(o), "CIDFontType0C")) dontexpand = !(opts->do_expand & fz_expand_fonts); + if (o = pdf_dict_gets(obj, "Filter"), filter_implies_image(xref, o)) + dontexpand = !(opts->do_expand & fz_expand_images); + if (pdf_dict_gets(obj, "Width") != NULL && pdf_dict_gets(obj, "Height") != NULL) + dontexpand = !(opts->do_expand & fz_expand_images); } if (opts->do_expand && !dontexpand && !pdf_is_jpx_image(ctx, obj)) expandstream(xref, opts, obj, num, gen); diff --git a/pdf/pdf_xref.c b/pdf/pdf_xref.c index 4da0577c..2deebfbd 100644 --- a/pdf/pdf_xref.c +++ b/pdf/pdf_xref.c @@ -36,7 +36,7 @@ pdf_read_start_xref(pdf_document *xref) xref->file_size = fz_tell(xref->file); - t = MAX(0, xref->file_size - (int)sizeof buf); + t = fz_maxi(0, xref->file_size - (int)sizeof buf); fz_seek(xref->file, t, 0); n = fz_read(xref->file, buf, sizeof buf); diff --git a/pdf/pdf_xref_aux.c b/pdf/pdf_xref_aux.c index 9acec96c..531a7e75 100644 --- a/pdf/pdf_xref_aux.c +++ b/pdf/pdf_xref_aux.c @@ -3,7 +3,7 @@ /* These functions have been split out of pdf_xref.c to allow tools - to be linked without pulling in the interpreter. The interpreter + to be linked without pulling in the interpreter. The interpreter references the built-in font and cmap resources which are quite big. Not linking those into the tools saves roughly 6MB in the resulting executables. diff --git a/scripts/cmapdump.c b/scripts/cmapdump.c index 53247339..2a83533e 100644 --- a/scripts/cmapdump.c +++ b/scripts/cmapdump.c @@ -78,6 +78,11 @@ main(int argc, char **argv) if (strlen(realname) > (sizeof name - 1)) { fprintf(stderr, "cmapdump: file name too long\n"); + if (fclose(fo)) + { + fprintf(stderr, "cmapdump: could not close output file '%s'\n", argv[1]); + return 1; + } return 1; } diff --git a/xps/xps_gradient.c b/xps/xps_gradient.c index 7fb0c427..e5699719 100644 --- a/xps/xps_gradient.c +++ b/xps/xps_gradient.c @@ -326,11 +326,11 @@ xps_draw_radial_gradient(xps_document *doc, fz_matrix ctm, fz_rect area, if (radius_y_att) yrad = fz_atof(radius_y_att); - xrad = MAX(0.01f, xrad); - yrad = MAX(0.01f, yrad); + xrad = fz_max(0.01f, xrad); + yrad = fz_max(0.01f, yrad); /* scale the ctm to make ellipses */ - if (fabsf(xrad) > FLT_EPSILON) + if (fz_abs(xrad) > FLT_EPSILON) ctm = fz_concat(fz_scale(1, yrad / xrad), ctm); if (yrad != 0.0) @@ -344,10 +344,10 @@ xps_draw_radial_gradient(xps_document *doc, fz_matrix ctm, fz_rect area, r1 = xrad; area = fz_transform_rect(fz_invert_matrix(ctm), area); - ma = MAX(ma, ceilf(hypotf(area.x0 - x0, area.y0 - y0) / xrad)); - ma = MAX(ma, ceilf(hypotf(area.x1 - x0, area.y0 - y0) / xrad)); - ma = MAX(ma, ceilf(hypotf(area.x0 - x0, area.y1 - y0) / xrad)); - ma = MAX(ma, ceilf(hypotf(area.x1 - x0, area.y1 - y0) / xrad)); + ma = fz_maxi(ma, ceilf(hypotf(area.x0 - x0, area.y0 - y0) / xrad)); + ma = fz_maxi(ma, ceilf(hypotf(area.x1 - x0, area.y0 - y0) / xrad)); + ma = fz_maxi(ma, ceilf(hypotf(area.x0 - x0, area.y1 - y0) / xrad)); + ma = fz_maxi(ma, ceilf(hypotf(area.x1 - x0, area.y1 - y0) / xrad)); if (spread == SPREAD_REPEAT) { @@ -402,11 +402,11 @@ xps_draw_linear_gradient(xps_document *doc, fz_matrix ctm, fz_rect area, k = ((area.x0 - p1.x) * x + (area.y0 - p1.y) * y) / (x * x + y * y); mi = floorf(k); ma = ceilf(k); k = ((area.x1 - p1.x) * x + (area.y0 - p1.y) * y) / (x * x + y * y); - mi = MIN(mi, floorf(k)); ma = MAX(ma, ceilf(k)); + mi = fz_mini(mi, floorf(k)); ma = fz_maxi(ma, ceilf(k)); k = ((area.x0 - p1.x) * x + (area.y1 - p1.y) * y) / (x * x + y * y); - mi = MIN(mi, floorf(k)); ma = MAX(ma, ceilf(k)); + mi = fz_mini(mi, floorf(k)); ma = fz_maxi(ma, ceilf(k)); k = ((area.x1 - p1.x) * x + (area.y1 - p1.y) * y) / (x * x + y * y); - mi = MIN(mi, floorf(k)); ma = MAX(ma, ceilf(k)); + mi = fz_mini(mi, floorf(k)); ma = fz_maxi(ma, ceilf(k)); dx = x1 - x0; dy = y1 - y0; if (spread == SPREAD_REPEAT) diff --git a/xps/xps_zip.c b/xps/xps_zip.c index f82343bc..58f04009 100644 --- a/xps/xps_zip.c +++ b/xps/xps_zip.c @@ -302,8 +302,8 @@ xps_find_and_read_zip_dir(xps_document *doc) fz_seek(doc->file, 0, SEEK_END); file_size = fz_tell(doc->file); - maxback = MIN(file_size, 0xFFFF + sizeof buf); - back = MIN(maxback, sizeof buf); + maxback = fz_mini(file_size, 0xFFFF + sizeof buf); + back = fz_mini(maxback, sizeof buf); while (back < maxback) { |