int pdf_init (lua_State *L) { pdf_rect mediabox; const char* fn = luaL_checkstring(L, 1); double w = luaL_checknumber(L, 2); height = luaL_checknumber(L, 3); p = texpdf_open_document(fn, 0, w, height, 0,0,0); texpdf_init_device(p, 1/precision, 2, 0); mediabox.llx = 0.0; mediabox.lly = 0.0; mediabox.urx = w; mediabox.ury = height; texpdf_files_init(); texpdf_init_fontmaps(); texpdf_doc_set_mediabox(p, 0, &mediabox); texpdf_add_dict(p->info, texpdf_new_name("Producer"), texpdf_new_string("SILE", 4)); return 0; }
int pdf_new_string(lua_State *L) { const char* s = luaL_checkstring(L, 1); int l = lua_rawlen(L, 1); lua_pushlightuserdata(L, texpdf_new_string(s, l)); return 1; }
pdf_obj * CMap_create_stream (CMap *cmap) { pdf_obj *stream; pdf_obj *stream_dict; CIDSysInfo *csi; struct sbuf wbuf; struct rangeDef *ranges; unsigned char *codestr; int i, j, count = 0; if (!cmap || !CMap_is_valid(cmap)) { WARN("Invalid CMap"); return NULL; } if (cmap->type == CMAP_TYPE_IDENTITY) return NULL; stream = texpdf_new_stream(STREAM_COMPRESS); stream_dict = texpdf_stream_dict(stream); csi = CMap_get_CIDSysInfo(cmap); if (!csi) { csi = (cmap->type != CMAP_TYPE_TO_UNICODE) ? &CSI_IDENTITY : &CSI_UNICODE; } if (cmap->type != CMAP_TYPE_TO_UNICODE) { pdf_obj *csi_dict; csi_dict = texpdf_new_dict(); texpdf_add_dict(csi_dict, texpdf_new_name("Registry"), texpdf_new_string(csi->registry, strlen(csi->registry))); texpdf_add_dict(csi_dict, texpdf_new_name("Ordering"), texpdf_new_string(csi->ordering, strlen(csi->ordering))); texpdf_add_dict(csi_dict, texpdf_new_name("Supplement"), texpdf_new_number(csi->supplement)); texpdf_add_dict(stream_dict, texpdf_new_name("Type"), texpdf_new_name("CMap")); texpdf_add_dict(stream_dict, texpdf_new_name("CMapName"), texpdf_new_name(cmap->name)); texpdf_add_dict(stream_dict, texpdf_new_name("CIDSystemInfo"), csi_dict); if (cmap->wmode != 0) texpdf_add_dict(stream_dict, texpdf_new_name("WMode"), texpdf_new_number(cmap->wmode)); } /* TODO: * Predefined CMaps need not to be embedded. */ if (cmap->useCMap) { ERROR("UseCMap found (not supported yet)..."); if (CMap_is_Identity(cmap->useCMap)) { /* not sure */ if (CMap_get_wmode(cmap) == 1) { texpdf_add_dict(stream_dict, texpdf_new_name("UseCMap"), texpdf_new_name("Identity-V")); } else { texpdf_add_dict(stream_dict, texpdf_new_name("UseCMap"), texpdf_new_name("Identity-H")); } } else { long res_id; pdf_obj *ucmap_ref; res_id = pdf_findresource("CMap", CMap_get_name(cmap->useCMap)); if (res_id >= 0) { ucmap_ref = texpdf_get_resource_reference(res_id); } else { pdf_obj *ucmap_obj; ucmap_obj = CMap_create_stream(cmap->useCMap); if (!ucmap_obj) { ERROR("Uh ah. I cannot continue..."); } res_id = pdf_defineresource("CMap", CMap_get_name(cmap->useCMap), ucmap_obj, PDF_RES_FLUSH_IMMEDIATE); ucmap_ref = texpdf_get_resource_reference(res_id); } texpdf_add_dict(stream_dict, texpdf_new_name("UseCMap"), ucmap_ref); } } #define WBUF_SIZE 4096 wbuf.buf = NEW(WBUF_SIZE, char); codestr = NEW(cmap->profile.maxBytesIn, unsigned char); memset(codestr, 0, cmap->profile.maxBytesIn); wbuf.curptr = wbuf.buf; wbuf.limptr = wbuf.buf + WBUF_SIZE - 2 * (cmap->profile.maxBytesIn + cmap->profile.maxBytesOut) + 16; /* Start CMap */ texpdf_add_stream(stream, (const void *) CMAP_BEGIN, strlen(CMAP_BEGIN)); wbuf.curptr += sprintf(wbuf.curptr, "/CMapName /%s def\n", cmap->name); wbuf.curptr += sprintf(wbuf.curptr, "/CMapType %d def\n" , cmap->type); if (cmap->wmode != 0 && cmap->type != CMAP_TYPE_TO_UNICODE) wbuf.curptr += sprintf(wbuf.curptr, "/WMode %d def\n", cmap->wmode); #define CMAP_CSI_FMT "/CIDSystemInfo <<\n\ /Registry (%s)\n\ /Ordering (%s)\n\ /Supplement %d\n\ >> def\n" wbuf.curptr += sprintf(wbuf.curptr, CMAP_CSI_FMT, csi->registry, csi->ordering, csi->supplement); texpdf_add_stream(stream, wbuf.buf, (long)(wbuf.curptr - wbuf.buf)); wbuf.curptr = wbuf.buf; /* codespacerange */ ranges = cmap->codespace.ranges; wbuf.curptr += sprintf(wbuf.curptr, "%d begincodespacerange\n", cmap->codespace.num); for (i = 0; i < cmap->codespace.num; i++) { *(wbuf.curptr)++ = '<'; for (j = 0; j < ranges[i].dim; j++) { sputx(ranges[i].codeLo[j], &(wbuf.curptr), wbuf.limptr); } *(wbuf.curptr)++ = '>'; *(wbuf.curptr)++ = ' '; *(wbuf.curptr)++ = '<'; for (j = 0; j < ranges[i].dim; j++) { sputx(ranges[i].codeHi[j], &(wbuf.curptr), wbuf.limptr); } *(wbuf.curptr)++ = '>'; *(wbuf.curptr)++ = '\n'; } texpdf_add_stream(stream, wbuf.buf, (long)(wbuf.curptr - wbuf.buf)); wbuf.curptr = wbuf.buf; texpdf_add_stream(stream, "endcodespacerange\n", strlen("endcodespacerange\n")); /* CMap body */ if (cmap->mapTbl) { count = write_map(cmap->mapTbl, 0, codestr, 0, &wbuf, stream); /* Top node */ if (count > 0) { /* Flush */ char fmt_buf[32]; if (count > 100) ERROR("Unexpected error....: %d", count); sprintf(fmt_buf, "%d beginbfchar\n", count); texpdf_add_stream(stream, fmt_buf, strlen(fmt_buf)); texpdf_add_stream(stream, wbuf.buf, (long) (wbuf.curptr - wbuf.buf)); texpdf_add_stream(stream, "endbfchar\n", strlen("endbfchar\n")); count = 0; wbuf.curptr = wbuf.buf; } } /* End CMap */ texpdf_add_stream(stream, CMAP_END, strlen(CMAP_END)); RELEASE(codestr); RELEASE(wbuf.buf); return stream; }