fz_error * fz_endpath(fz_pathnode *path, fz_pathkind paint, fz_stroke *stroke, fz_dash *dash) { fz_pathel *newels; newels = fz_realloc(path->els, path->len * sizeof(fz_pathel)); if (!newels) return fz_outofmem; path->els = newels; path->paint = paint; path->dash = dash; if (stroke) { path->linecap = stroke->linecap; path->linejoin = stroke->linejoin; path->linewidth = stroke->linewidth; path->miterlimit = stroke->miterlimit; } if (path->linewidth < 0.01) path->linewidth = 0.01f; return nil; }
void fz_resize_buffer(fz_buffer *buf, int size) { buf->data = fz_realloc(buf->data, size, 1); buf->cap = size; if (buf->len > buf->cap) buf->len = buf->cap; }
static void growpath(fz_path *path, int n) { if (path->len + n < path->cap) return; while (path->len + n > path->cap) path->cap = path->cap + 36; path->els = fz_realloc(path->els, path->cap, sizeof(fz_pathel)); }
/***** SumatraPDF: various string fixups *****/ static void ensurespanlength(fz_text_span *span, int mincap) { if (span->cap < mincap) { span->cap = mincap * 3 / 2; span->text = fz_realloc(span->text, span->cap, sizeof(fz_text_char)); } }
static void fz_growtext(fz_text *text, int n) { if (text->len + n < text->cap) return; while (text->len + n > text->cap) text->cap = text->cap + 36; text->els = fz_realloc(text->els, sizeof (fz_textel) * text->cap); }
static fz_error gatherpsobjs(int page, fz_obj *pageobj, fz_obj *dict) { int i; for (i = 0; i < fz_dictlen(dict); i++) { fz_obj *ref; fz_obj *xobjdict; fz_obj *type; fz_obj *subtype; int k; xobjdict = ref = fz_dictgetval(dict, i); if (!fz_isdict(xobjdict)) return fz_throw("not a xobject dict (%d %d R)", fz_tonum(ref), fz_togen(ref)); type = fz_dictgets(xobjdict, "Subtype"); if (!fz_isname(type)) return fz_throw("not a xobject type (%d %d R)", fz_tonum(ref), fz_togen(ref)); if (strcmp(fz_toname(type), "Form")) continue; subtype = fz_dictgets(xobjdict, "Subtype2"); if (subtype && !fz_isname(subtype)) return fz_throw("not a xobject subtype (%d %d R)", fz_tonum(ref), fz_togen(ref)); if (strcmp(fz_toname(type), "PS") && (strcmp(fz_toname(type), "Form") || strcmp(fz_toname(subtype), "PS"))) continue; for (k = 0; k < psobjs; k++) if (fz_tonum(psobj[k]->ref) == fz_tonum(ref) && fz_togen(psobj[k]->ref) == fz_togen(ref)) break; if (k < psobjs) continue; psobjs++; psobj = fz_realloc(psobj, psobjs * sizeof (struct info *)); if (!psobj) return fz_throw("out of memory"); psobj[psobjs - 1] = fz_malloc(sizeof (struct info)); if (!psobj[psobjs - 1]) return fz_throw("out of memory"); psobj[psobjs - 1]->page = page; psobj[psobjs - 1]->pageobj = pageobj; psobj[psobjs - 1]->ref = ref; } return fz_okay; }
static void fz_array_grow(fz_context *ctx, fz_obj *obj) { int i; obj->u.a.cap = (obj->u.a.cap * 3) / 2; obj->u.a.items = fz_realloc(ctx, obj->u.a.items, obj->u.a.cap * sizeof(fz_obj*)); for (i = obj->u.a.len ; i < obj->u.a.cap; i++) obj->u.a.items[i] = NULL; }
static void fz_add_text_char_imp(fz_text_span *span, int c, fz_bbox bbox) { if (span->len + 1 >= span->cap) { span->cap = span->cap > 1 ? (span->cap * 3) / 2 : 80; span->text = fz_realloc(span->text, span->cap, sizeof(fz_text_char)); } span->text[span->len].c = c; span->text[span->len].bbox = bbox; span->len ++; }
void pdf_addhmtx(pdf_fontdesc *font, int lo, int hi, int w) { if (font->nhmtx + 1 >= font->hmtxcap) { font->hmtxcap = font->hmtxcap + 16; font->hmtx = fz_realloc(font->hmtx, sizeof(pdf_hmtx) * font->hmtxcap); } font->hmtx[font->nhmtx].lo = lo; font->hmtx[font->nhmtx].hi = hi; font->hmtx[font->nhmtx].w = w; font->nhmtx++; }
static void pdf_grow_mesh(fz_shade *shade, int amount) { if (shade->mesh_len + amount < shade->mesh_cap) return; if (shade->mesh_cap == 0) shade->mesh_cap = 1024; while (shade->mesh_len + amount > shade->mesh_cap) shade->mesh_cap = (shade->mesh_cap * 3) / 2; shade->mesh = fz_realloc(shade->mesh, shade->mesh_cap, sizeof(float)); }
void pdf_add_hmtx(pdf_font_desc *font, int lo, int hi, int w) { if (font->hmtx_len + 1 >= font->hmtx_cap) { font->hmtx_cap = font->hmtx_cap + 16; font->hmtx = fz_realloc(font->hmtx, font->hmtx_cap, sizeof(pdf_hmtx)); } font->hmtx[font->hmtx_len].lo = lo; font->hmtx[font->hmtx_len].hi = hi; font->hmtx[font->hmtx_len].w = w; font->hmtx_len++; }
static fz_error gathershadings(int page, fz_obj *pageobj, fz_obj *dict) { int i; for (i = 0; i < fz_dictlen(dict); i++) { fz_obj *ref; fz_obj *shade; fz_obj *type; int k; shade = ref = fz_dictgetval(dict, i); if (!fz_isdict(shade)) return fz_throw("not a shading dict (%d %d R)", fz_tonum(ref), fz_togen(ref)); type = fz_dictgets(shade, "ShadingType"); if (!fz_isint(type) || fz_toint(type) < 1 || fz_toint(type) > 7) { fz_warn("not a shading type (%d %d R)", fz_tonum(ref), fz_togen(ref)); type = nil; } for (k = 0; k < shadings; k++) if (fz_tonum(shading[k]->ref) == fz_tonum(ref) && fz_togen(shading[k]->ref) == fz_togen(ref)) break; if (k < shadings) continue; shadings++; shading = fz_realloc(shading, shadings * sizeof (struct info *)); if (!shading) return fz_throw("out of memory"); shading[shadings - 1] = fz_malloc(sizeof (struct info)); if (!shading[shadings - 1]) return fz_throw("out of memory"); shading[shadings - 1]->page = page; shading[shadings - 1]->pageobj = pageobj; shading[shadings - 1]->ref = ref; shading[shadings - 1]->u.shading.type = type; } return fz_okay; }
static fz_error resizecode(pdf_function *func, int newsize) { if (newsize >= func->u.p.cap) { int newcodecap = func->u.p.cap + 64; psobj *newcode; newcode = fz_realloc(func->u.p.code, newcodecap * sizeof(psobj)); if (!newcode) return fz_rethrow(-1, "out of memory: calculator function code"); func->u.p.cap = newcodecap; func->u.p.code = newcode; } return fz_okay; }
/* * Add an integer to the table. */ static void add_table(fz_context *ctx, pdf_cmap *cmap, int value) { if (cmap->tlen == USHRT_MAX) { fz_warn(ctx, "cmap table is full; ignoring additional entries"); return; } if (cmap->tlen + 1 > cmap->tcap) { cmap->tcap = cmap->tcap > 1 ? (cmap->tcap * 3) / 2 : 256; cmap->table = fz_realloc(ctx, cmap->table, cmap->tcap * sizeof(unsigned short)); } cmap->table[cmap->tlen++] = value; }
void pdf_add_vmtx(fz_context *ctx, pdf_font_desc *font, int lo, int hi, int x, int y, int w) { if (font->vmtx_len + 1 >= font->vmtx_cap) { font->vmtx_cap = font->vmtx_cap + 16; font->vmtx = fz_realloc(ctx, font->vmtx, font->vmtx_cap * sizeof(pdf_vmtx)); } font->vmtx[font->vmtx_len].lo = lo; font->vmtx[font->vmtx_len].hi = hi; font->vmtx[font->vmtx_len].x = x; font->vmtx[font->vmtx_len].y = y; font->vmtx[font->vmtx_len].w = w; font->vmtx_len++; }
void pdf_resize_xref(pdf_xref *xref, int newlen) { int i; xref->table = fz_realloc(xref->table, newlen, sizeof(pdf_xref_entry)); for (i = xref->len; i < newlen; i++) { xref->table[i].type = 0; xref->table[i].ofs = 0; xref->table[i].gen = 0; xref->table[i].stm_ofs = 0; xref->table[i].obj = NULL; } xref->len = newlen; }
static fz_error * growshademesh(fz_shade *shade, int amount) { float *newmesh; int newcap; newcap = shade->meshcap + amount; newmesh = fz_realloc(shade->mesh, sizeof(float) * newcap); if (!newmesh) return fz_outofmem; shade->mesh = newmesh; shade->meshcap = newcap; return nil; }
void pdf_addvmtx(pdf_fontdesc *font, int lo, int hi, int x, int y, int w) { if (font->nvmtx + 1 >= font->vmtxcap) { font->vmtxcap = font->vmtxcap + 16; font->vmtx = fz_realloc(font->vmtx, font->vmtxcap, sizeof(pdf_vmtx)); } font->vmtx[font->nvmtx].lo = lo; font->vmtx[font->nvmtx].hi = hi; font->vmtx[font->nvmtx].x = x; font->vmtx[font->nvmtx].y = y; font->vmtx[font->nvmtx].w = w; font->nvmtx++; }
static void fz_grow_stack(fz_draw_device *dev) { int max = dev->stack_max * 2; fz_draw_stack *stack; if (dev->stack == &dev->init_stack[0]) { stack = fz_malloc(dev->ctx, sizeof(*stack) * max); memcpy(stack, dev->stack, sizeof(*stack) * dev->stack_max); } else { stack = fz_realloc(dev->ctx, dev->stack, max * sizeof(*stack)); } dev->stack = stack; dev->stack_max = max; }
static void grow_system_font_list(fz_context *ctx, pdf_windows_fontlist *fl) { int newcap; pdf_windows_fontmap *newitems; if (fl->cap == 0) newcap = 1024; else newcap = fl->cap * 2; newitems = fz_realloc(ctx, fl->fontmap, newcap * sizeof(pdf_windows_fontmap)); memset(newitems + fl->cap, 0, sizeof(pdf_windows_fontmap) * (newcap - fl->cap)); fl->fontmap = newitems; fl->cap = newcap; }
static void insert_active(fz_gel *gel, int y, int *e) { /* insert edges that start here */ while (*e < gel->len && gel->edges[*e].y == y) { if (gel->alen + 1 == gel->acap) { int newcap = gel->acap + 64; fz_edge **newactive = fz_realloc(gel->ctx, gel->active, newcap * sizeof(fz_edge*)); gel->active = newactive; gel->acap = newcap; } gel->active[gel->alen++] = &gel->edges[(*e)++]; } /* shell-sort the edges by increasing x */ sort_active(gel->active, gel->alen); }
static fz_error * growpath(fz_pathnode *path, int n) { int newcap; fz_pathel *newels; while (path->len + n > path->cap) { newcap = path->cap + 36; newels = fz_realloc(path->els, sizeof (fz_pathel) * newcap); if (!newels) return fz_outofmem; path->cap = newcap; path->els = newels; } return nil; }
static fz_error growtext(fz_textnode *text, int n) { int newcap; fz_textel *newels; while (text->len + n > text->cap) { newcap = text->cap + 36; newels = fz_realloc(text->els, sizeof (fz_textel) * newcap); if (!newels) return fz_rethrow(-1, "out of memory"); text->cap = newcap; text->els = newels; } return fz_okay; }
static fz_error insertael(fz_ael *ael, fz_gel *gel, int y, int *e) { /* insert edges that start here */ while (*e < gel->len && gel->edges[*e].y == y) { if (ael->len + 1 == ael->cap) { int newcap = ael->cap + 64; fz_edge **newedges = fz_realloc(ael->edges, sizeof(fz_edge*) * newcap); ael->edges = newedges; ael->cap = newcap; } ael->edges[ael->len++] = &gel->edges[(*e)++]; } /* shell-sort the edges by increasing x */ sortael(ael->edges, ael->len); return fz_okay; }
static fz_error gatherdimensions(int page, fz_obj *pageobj) { fz_obj *ref; fz_rect bbox; fz_obj *obj; int j; obj = ref = fz_dictgets(pageobj, "MediaBox"); if (!fz_isarray(obj)) return fz_throw("cannot find page bounds (%d %d R)", fz_tonum(ref), fz_togen(ref)); bbox = pdf_torect(obj); for (j = 0; j < dims; j++) if (!memcmp(dim[j]->u.dim.bbox, &bbox, sizeof (fz_rect))) break; if (j < dims) return fz_okay; dims++; dim = fz_realloc(dim, dims * sizeof (struct info *)); if (!dim) return fz_throw("out of memory"); dim[dims - 1] = fz_malloc(sizeof (struct info)); if (!dim[dims - 1]) return fz_throw("out of memory"); dim[dims - 1]->u.dim.bbox = fz_malloc(sizeof (fz_rect)); if (!dim[dims - 1]->u.dim.bbox) return fz_throw("out of memory"); dim[dims - 1]->page = page; dim[dims - 1]->pageobj = pageobj; dim[dims - 1]->ref = nil; memcpy(dim[dims - 1]->u.dim.bbox, &bbox, sizeof (fz_rect)); return fz_okay; }
/* * Add a range. */ static void add_range(fz_context *ctx, pdf_cmap *cmap, int low, int high, int flag, int offset) { /* If the range is too large to be represented, split it */ if (high - low > 0x3fff) { add_range(ctx, cmap, low, low+0x3fff, flag, offset); add_range(ctx, cmap, low+0x3fff, high, flag, offset+0x3fff); return; } if (cmap->rlen + 1 > cmap->rcap) { cmap->rcap = cmap->rcap > 1 ? (cmap->rcap * 3) / 2 : 256; cmap->ranges = fz_realloc(ctx, cmap->ranges, cmap->rcap * sizeof(pdf_range)); } cmap->ranges[cmap->rlen].low = low; pdf_range_set_high(&cmap->ranges[cmap->rlen], high); pdf_range_set_flags(&cmap->ranges[cmap->rlen], flag); cmap->ranges[cmap->rlen].offset = offset; cmap->rlen ++; }
fz_error * fz_growbuffer(fz_buffer *buf) { unsigned char *newbp; int rp = buf->rp - buf->bp; int wp = buf->wp - buf->bp; int ep = buf->ep - buf->bp; assert(buf->ownsdata); newbp = fz_realloc(buf->bp, ep * 2); if (!newbp) return fz_outofmem; buf->bp = newbp; buf->rp = buf->bp + rp; buf->wp = buf->bp + wp; buf->ep = buf->bp + ep * 2; return nil; }
fz_error * fz_growbuffer(fz_buffer *buf) { unsigned char *newbp; int rp = buf->rp - buf->bp; int wp = buf->wp - buf->bp; int ep = buf->ep - buf->bp; if (!buf->ownsdata) return fz_throw("assert: grow borrowed memory"); newbp = fz_realloc(buf->bp, ep * 2); if (!newbp) return fz_throw("outofmem: resize buffer memory"); buf->bp = newbp; buf->rp = buf->bp + rp; buf->wp = buf->bp + wp; buf->ep = buf->bp + ep * 2; return fz_okay; }
static fz_error * growfontlist(pdf_fontlistMS *fl) { int newcap; pdf_fontmapMS *newitems; if(fl->cap == 0) newcap = 32; else newcap = fl->cap * 2; newitems = fz_realloc(fl->fontmap, sizeof(pdf_fontmapMS) * newcap); if (!newitems) return fz_outofmem; memset(newitems + fl->cap, 0, sizeof(struct fz_keyval_s) * (newcap - fl->cap)); fl->fontmap = newitems; fl->cap = newcap; return nil; }
static void fz_insertgelraw(fz_gel *gel, int x0, int y0, int x1, int y1) { fz_edge *edge; int dx, dy; int winding; int width; int tmp; if (y0 == y1) return; if (y0 > y1) { winding = -1; tmp = x0; x0 = x1; x1 = tmp; tmp = y0; y0 = y1; y1 = tmp; } else winding = 1; if (x0 < gel->bbox.x0) gel->bbox.x0 = x0; if (x0 > gel->bbox.x1) gel->bbox.x1 = x0; if (x1 < gel->bbox.x0) gel->bbox.x0 = x1; if (x1 > gel->bbox.x1) gel->bbox.x1 = x1; if (y0 < gel->bbox.y0) gel->bbox.y0 = y0; if (y1 > gel->bbox.y1) gel->bbox.y1 = y1; if (gel->len + 1 == gel->cap) { gel->cap = gel->cap + 512; gel->edges = fz_realloc(gel->edges, sizeof(fz_edge) * gel->cap); } edge = &gel->edges[gel->len++]; dy = y1 - y0; dx = x1 - x0; width = ABS(dx); edge->xdir = dx > 0 ? 1 : -1; edge->ydir = winding; edge->x = x0; edge->y = y0; edge->h = dy; edge->adjdown = dy; /* initial error term going l->r and r->l */ if (dx >= 0) edge->e = 0; else edge->e = -dy + 1; /* y-major edge */ if (dy >= width) { edge->xmove = 0; edge->adjup = width; } /* x-major edge */ else { edge->xmove = (width / dy) * edge->xdir; edge->adjup = width % dy; } }