static void pdf_flush_font (pdf_font *font) { char *fontname, *uniqueTag; if (!font) { return; } if (font->resource && font->reference) { if (font->subtype != PDF_FONT_FONTTYPE_TYPE3) { if (pdf_font_get_flag(font, PDF_FONT_FLAG_NOEMBED)) { texpdf_add_dict(font->resource, texpdf_new_name("BaseFont"), texpdf_new_name(font->fontname)); if (font->descriptor) { texpdf_add_dict(font->descriptor, texpdf_new_name("FontName"), texpdf_new_name(font->fontname)); } } else { if (!font->fontname) { ERROR("Undefined in fontname... (%s)", font->ident); } fontname = NEW(7+strlen(font->fontname)+1, char); uniqueTag = pdf_font_get_uniqueTag(font); sprintf(fontname, "%6s+%s", uniqueTag, font->fontname); texpdf_add_dict(font->resource, texpdf_new_name("BaseFont"), texpdf_new_name(fontname)); if (font->descriptor) { texpdf_add_dict(font->descriptor, texpdf_new_name("FontName"), texpdf_new_name(fontname)); } RELEASE(fontname); } if (font->descriptor) { texpdf_add_dict(font->resource, texpdf_new_name("FontDescriptor"), texpdf_ref_obj(font->descriptor)); } } } if (font->resource) texpdf_release_obj(font->resource); if (font->descriptor) texpdf_release_obj(font->descriptor); if (font->reference) texpdf_release_obj(font->reference); font->reference = NULL; font->resource = NULL; font->descriptor = NULL; return; }
static pdf_obj * JPEG_get_iccp (struct JPEG_info *j_info) { pdf_obj *icc_stream; struct JPEG_APPn_ICC *icc; int i, prev_id = 0, num_icc_seg = -1; icc_stream = texpdf_new_stream(STREAM_COMPRESS); for (i = 0; i < j_info->num_appn; i++) { if (j_info->appn[i].marker != JM_APP2 || j_info->appn[i].app_sig != JS_APPn_ICC) continue; icc = (struct JPEG_APPn_ICC *) j_info->appn[i].app_data; if (num_icc_seg < 0 && prev_id == 0) { num_icc_seg = icc->num_chunks; /* ICC chunks are sorted? */ } else if (icc->seq_id != prev_id + 1 || num_icc_seg != icc->num_chunks || icc->seq_id > icc->num_chunks) { WARN("Invalid JPEG ICC chunk: %d (p:%d, n:%d)", icc->seq_id, prev_id, icc->num_chunks); texpdf_release_obj(icc_stream); icc_stream = NULL; break; } texpdf_add_stream(icc_stream, icc->chunk, icc->length); prev_id = icc->seq_id; num_icc_seg = icc->num_chunks; } return icc_stream; }
int pdf_end_annotation(lua_State *L) { const char* dictionary = luaL_checkstring(L, 1); pdf_rect rect; pdf_obj* dict; rect.llx = luaL_checknumber(L, 2); rect.lly = luaL_checknumber(L, 3); rect.urx = luaL_checknumber(L, 4); rect.ury = luaL_checknumber(L, 5); dict = texpdf_parse_pdf_dict(&dictionary, dictionary + strlen(dictionary), NULL); if (!dict) { luaL_error(L, "Unparsable annotation dictionary"); return 0; } texpdf_doc_add_annot(p, texpdf_doc_current_page_number(p), &rect, dict, 1); texpdf_release_obj(dict); return 0; }
int pdf_release(lua_State *L) { pdf_obj* o1 = lua_touserdata(L, 1); texpdf_release_obj(o1); return 0; }
int jpeg_include_image (pdf_ximage *ximage, FILE *fp) { pdf_obj *stream; pdf_obj *stream_dict; pdf_obj *colorspace; int colortype; ximage_info info; struct JPEG_info j_info; if (!texpdf_check_for_jpeg(fp)) { WARN("%s: Not a JPEG file?", JPEG_DEBUG_STR); rewind(fp); return -1; } /* File position is 2 here... */ texpdf_ximage_init_image_info(&info); JPEG_info_init(&j_info); if (JPEG_scan_file(&j_info, fp) < 0) { WARN("%s: Not a JPEG file?", JPEG_DEBUG_STR); JPEG_info_clear(&j_info); return -1; } switch (j_info.num_components) { case 1: colortype = PDF_COLORSPACE_TYPE_GRAY; break; case 3: colortype = PDF_COLORSPACE_TYPE_RGB; break; case 4: colortype = PDF_COLORSPACE_TYPE_CMYK; break; default: WARN("%s: Unknown color space (num components: %d)", JPEG_DEBUG_STR, info.num_components); JPEG_info_clear(&j_info); return -1; } /* JPEG image use DCTDecode. */ stream = texpdf_new_stream (0); stream_dict = texpdf_stream_dict(stream); texpdf_add_dict(stream_dict, texpdf_new_name("Filter"), texpdf_new_name("DCTDecode")); /* XMP Metadata */ if (texpdf_get_version() >= 4) { if (j_info.flags & HAVE_APPn_XMP) { pdf_obj *XMP_stream; XMP_stream = JPEG_get_XMP(&j_info); texpdf_add_dict(stream_dict, texpdf_new_name("Metadata"), texpdf_ref_obj(XMP_stream)); texpdf_release_obj(XMP_stream); } } /* Check embedded ICC Profile */ colorspace = NULL; if (j_info.flags & HAVE_APPn_ICC) { pdf_obj *icc_stream, *intent; int cspc_id; icc_stream = JPEG_get_iccp(&j_info); if (!icc_stream) colorspace = NULL; else { if (iccp_check_colorspace(colortype, pdf_stream_dataptr(icc_stream), pdf_stream_length (icc_stream)) < 0) colorspace = NULL; else { cspc_id = iccp_load_profile(NULL, /* noname */ pdf_stream_dataptr(icc_stream), pdf_stream_length (icc_stream)); if (cspc_id < 0) colorspace = NULL; else { colorspace = texpdf_get_colorspace_reference(cspc_id); intent = iccp_get_rendering_intent(pdf_stream_dataptr(icc_stream), pdf_stream_length (icc_stream)); if (intent) texpdf_add_dict(stream_dict, texpdf_new_name("Intent"), intent); } } texpdf_release_obj(icc_stream); } } /* No ICC or invalid ICC profile. */ if (!colorspace) { switch (colortype) { case PDF_COLORSPACE_TYPE_GRAY: colorspace = texpdf_new_name("DeviceGray"); break; case PDF_COLORSPACE_TYPE_RGB: colorspace = texpdf_new_name("DeviceRGB"); break; case PDF_COLORSPACE_TYPE_CMYK: colorspace = texpdf_new_name("DeviceCMYK"); break; } } texpdf_add_dict(stream_dict, texpdf_new_name("ColorSpace"), colorspace); #define IS_ADOBE_CMYK(j) (((j).flags & HAVE_APPn_ADOBE) && (j).num_components == 4) if (IS_ADOBE_CMYK(j_info)) { pdf_obj *decode; int i; WARN("Adobe CMYK JPEG: Inverted color assumed."); decode = texpdf_new_array(); for (i = 0; i < j_info.num_components; i++) { texpdf_add_array(decode, texpdf_new_number(1.0)); texpdf_add_array(decode, texpdf_new_number(0.0)); } texpdf_add_dict(stream_dict, texpdf_new_name("Decode"), decode); } /* Copy file */ JPEG_copy_stream(&j_info, stream, fp); info.width = j_info.width; info.height = j_info.height; info.bits_per_component = j_info.bits_per_component; info.num_components = j_info.num_components; jpeg_get_density(&j_info, &info.xdensity, &info.ydensity); texpdf_ximage_set_image(ximage, &info, stream); JPEG_info_clear(&j_info); return 0; }