static HPDF_STATUS WriteTrailer (HPDF_Xref xref, HPDF_Stream stream) { HPDF_UINT max_obj_id = xref->entries->count + xref->start_offset; HPDF_STATUS ret; HPDF_PTRACE ((" WriteTrailer\n")); if ((ret = HPDF_Dict_AddNumber (xref->trailer, "Size", max_obj_id)) != HPDF_OK) return ret; if (xref->prev) if ((ret = HPDF_Dict_AddNumber (xref->trailer, "Prev", xref->prev->addr)) != HPDF_OK) return ret; if ((ret = HPDF_Stream_WriteStr (stream, "trailer\012")) != HPDF_OK) return ret; if ((ret = HPDF_Dict_Write (xref->trailer, stream, NULL)) != HPDF_OK) return ret; if ((ret = HPDF_Stream_WriteStr (stream, "\012startxref\012")) != HPDF_OK) return ret; if ((ret = HPDF_Stream_WriteUInt (stream, xref->addr)) != HPDF_OK) return ret; if ((ret = HPDF_Stream_WriteStr (stream, "\012%%EOF\012")) != HPDF_OK) return ret; return HPDF_OK; }
HPDF_LineAnnot_SetLeader (HPDF_Annotation annot, HPDF_INT leaderLen, HPDF_INT leaderExtLen, HPDF_INT leaderOffsetLen) { HPDF_STATUS ret = HPDF_OK; HPDF_PTRACE((" HPDF_LineAnnot_SetLeader\n")); ret += HPDF_Dict_AddNumber ( annot, "LL", leaderLen); ret += HPDF_Dict_AddNumber ( annot, "LLE", leaderExtLen); ret += HPDF_Dict_AddNumber ( annot, "LLO", leaderOffsetLen); if (ret != HPDF_OK) return HPDF_Error_GetCode ( annot->error); return HPDF_OK; }
HPDF_EXPORT(HPDF_STATUS) HPDF_U3D_Add3DView(HPDF_U3D u3d, HPDF_Dict view) { HPDF_Array views = NULL; HPDF_STATUS ret = HPDF_OK; HPDF_PTRACE ((" HPDF_Add3DView\n")); if (u3d == NULL || view == NULL) { return HPDF_INVALID_U3D_DATA; } views = (HPDF_Array)HPDF_Dict_GetItem (u3d, "VA", HPDF_OCLASS_ARRAY); if (views == NULL) { views = HPDF_Array_New (u3d->mmgr); if (!views) { return HPDF_Error_GetCode (u3d->error); } ret = HPDF_Dict_Add (u3d, "VA", views); if (ret == HPDF_OK) { ret = HPDF_Dict_AddNumber (u3d, "DV", 0); } else { HPDF_Array_Free (views); return ret; } } if (ret == HPDF_OK) { ret = HPDF_Array_Add( views, view); } return ret; }
HPDF_Dict HPDF_PageLabel_New (HPDF_Doc pdf, HPDF_PageNumStyle style, HPDF_INT first_page, const char *prefix) { HPDF_Dict obj = HPDF_Dict_New (pdf->mmgr); HPDF_PTRACE ((" HPDF_PageLabel_New\n")); if (!obj) return NULL; switch (style) { case HPDF_PAGE_NUM_STYLE_DECIMAL: if (HPDF_Dict_AddName (obj, "S", "D") != HPDF_OK) goto Fail; break; case HPDF_PAGE_NUM_STYLE_UPPER_ROMAN: if (HPDF_Dict_AddName (obj, "S", "R") != HPDF_OK) goto Fail; break; case HPDF_PAGE_NUM_STYLE_LOWER_ROMAN: if (HPDF_Dict_AddName (obj, "S", "r") != HPDF_OK) goto Fail; break; case HPDF_PAGE_NUM_STYLE_UPPER_LETTERS: if (HPDF_Dict_AddName (obj, "S", "A") != HPDF_OK) goto Fail; break; case HPDF_PAGE_NUM_STYLE_LOWER_LETTERS: if (HPDF_Dict_AddName (obj, "S", "a") != HPDF_OK) goto Fail; break; default: HPDF_SetError (&pdf->error, HPDF_PAGE_NUM_STYLE_OUT_OF_RANGE, (HPDF_STATUS)style); goto Fail; } if (prefix && prefix[0] != 0) if (HPDF_Dict_Add (obj, "P", HPDF_String_New (pdf->mmgr, prefix, pdf->def_encoder)) != HPDF_OK) goto Fail; if (first_page != 0) if (HPDF_Dict_AddNumber (obj, "St", first_page) != HPDF_OK) goto Fail; return obj; Fail: HPDF_Dict_Free (obj); return NULL; }
HPDF_MarkupAnnot_SetCloudEffect (HPDF_Annotation annot, HPDF_INT cloudIntensity) /* BE entry */ { HPDF_Dict borderEffect; HPDF_STATUS ret = HPDF_OK; HPDF_PTRACE((" HPDF_MarkupAnnot_SetCloudEffect\n")); borderEffect = HPDF_Dict_New ( annot->mmgr); if (!borderEffect) return HPDF_Error_GetCode ( annot->error); ret += HPDF_Dict_Add ( annot, "BE", borderEffect); ret += HPDF_Dict_AddName ( borderEffect, "S", "C"); ret += HPDF_Dict_AddNumber ( borderEffect, "I", cloudIntensity); if (ret != HPDF_OK) return HPDF_Error_GetCode (annot->error); return HPDF_OK; }
HPDF_STATUS HPDF_EncryptDict_Prepare (HPDF_EncryptDict dict, HPDF_Dict info, HPDF_Xref xref) { HPDF_STATUS ret; HPDF_Encrypt attr = (HPDF_Encrypt)dict->attr; HPDF_Binary user_key; HPDF_Binary owner_key; HPDF_PTRACE((" HPDF_EncryptDict_Prepare\n")); HPDF_EncryptDict_CreateID (dict, info, xref); HPDF_Encrypt_CreateOwnerKey (attr); HPDF_Encrypt_CreateEncryptionKey (attr); HPDF_Encrypt_CreateUserKey (attr); owner_key = HPDF_Binary_New (dict->mmgr, attr->owner_key, HPDF_PASSWD_LEN); if (!owner_key) return HPDF_Error_GetCode (dict->error); if ((ret = HPDF_Dict_Add (dict, "O", owner_key)) != HPDF_OK) return ret; user_key = HPDF_Binary_New (dict->mmgr, attr->user_key, HPDF_PASSWD_LEN); if (!user_key) return HPDF_Error_GetCode (dict->error); if ((ret = HPDF_Dict_Add (dict, "U", user_key)) != HPDF_OK) return ret; ret += HPDF_Dict_AddName (dict, "Filter", "Standard"); if (attr->mode == HPDF_ENCRYPT_R2) { ret += HPDF_Dict_AddNumber (dict, "V", 1); ret += HPDF_Dict_AddNumber (dict, "R", 2); } else if (attr->mode == HPDF_ENCRYPT_R3) { ret += HPDF_Dict_AddNumber (dict, "V", 2); ret += HPDF_Dict_AddNumber (dict, "R", 3); ret += HPDF_Dict_AddNumber (dict, "Length", attr->key_len * 8); } ret += HPDF_Dict_AddNumber (dict, "P", attr->permission); if (ret != HPDF_OK) return HPDF_Error_GetCode (dict->error); return HPDF_OK; }
HPDF_Page_SetRotate (HPDF_Page page, HPDF_UINT16 angle) { HPDF_Number n; HPDF_STATUS ret = HPDF_OK; HPDF_PTRACE((" HPDF_Page_SetRotate\n")); if (!HPDF_Page_Validate (page)) return HPDF_INVALID_PAGE; if (angle % 90 != 0) return HPDF_RaiseError (page->error, HPDF_PAGE_INVALID_ROTATE_VALUE, (HPDF_STATUS)angle); n = HPDF_Page_GetInheritableItem (page, "Rotate", HPDF_OCLASS_NUMBER); if (!n) ret = HPDF_Dict_AddNumber (page, "Rotate", angle); else n->value = angle; return ret; }
HPDF_EXPORT(HPDF_STATUS) HPDF_3DView_SetCamera(HPDF_Dict view, HPDF_REAL coox, HPDF_REAL cooy, HPDF_REAL cooz, HPDF_REAL c2cx, HPDF_REAL c2cy, HPDF_REAL c2cz, HPDF_REAL roo, HPDF_REAL roll) { HPDF_REAL viewx, viewy, viewz; HPDF_REAL leftx, lefty, leftz; HPDF_REAL upx, upy, upz; HPDF_REAL transx, transy, transz; HPDF_Array matrix; HPDF_STATUS ret = HPDF_OK; HPDF_PTRACE ((" HPDF_3DView_SetCamera\n")); if (view == NULL) { return HPDF_INVALID_U3D_DATA; } /* view vector (opposite to c2c) */ viewx = -c2cx; viewy = -c2cy; viewz = -c2cz; /* c2c = (0, -1, 0) by default */ if (viewx == 0.0 && viewy == 0.0 && viewz == 0.0) { viewy = 1.0; } /* normalize view vector */ normalize(viewx, viewy, viewz); /* rotation matrix */ /* top and bottom views */ leftx = -1.0f; lefty = 0.0f; leftz = 0.0f; /* up-vector */ if (viewz < 0.0) /* top view*/ { upx = 0.0f; upy = 1.0f; upz = 0.0f; } else /* bottom view*/ { upx = 0.0f; upy =-1.0f; upz = 0.0f; } if ( fabs(viewx) + fabs(viewy) != 0.0f) /* other views than top and bottom*/ { /* up-vector = up_world - (up_world dot view) view*/ upx = -viewz*viewx; upy = -viewz*viewy; upz = -viewz*viewz + 1.0f; /* normalize up-vector*/ normalize(upx, upy, upz); /* left vector = up x view*/ leftx = viewz*upy - viewy*upz; lefty = viewx*upz - viewz*upx; leftz = viewy*upx - viewx*upy; /* normalize left vector*/ normalize(leftx, lefty, leftz); } /* apply camera roll*/ { HPDF_REAL leftxprime, leftyprime, leftzprime; HPDF_REAL upxprime, upyprime, upzprime; HPDF_REAL sinroll, cosroll; sinroll = sin((roll/180.0f)*M_PI); cosroll = cos((roll/180.0f)*M_PI); leftxprime = leftx*cosroll + upx*sinroll; leftyprime = lefty*cosroll + upy*sinroll; leftzprime = leftz*cosroll + upz*sinroll; upxprime = upx*cosroll + leftx*sinroll; upyprime = upy*cosroll + lefty*sinroll; upzprime = upz*cosroll + leftz*sinroll; leftx = leftxprime; lefty = leftyprime; leftz = leftzprime; upx = upxprime; upy = upyprime; upz = upzprime; } /* translation vector*/ roo = fabs(roo); if (roo == 0.0) { roo = 0.000000000000000001; } transx = coox - roo*viewx; transy = cooy - roo*viewy; transz = cooz - roo*viewz; /* transformation matrix*/ matrix = HPDF_Array_New (view->mmgr); if (!matrix) { return HPDF_Error_GetCode (view->error); } ret = HPDF_Array_AddReal (matrix, leftx); if (ret != HPDF_OK) goto failed; ret = HPDF_Array_AddReal (matrix, lefty); if (ret != HPDF_OK) goto failed; ret = HPDF_Array_AddReal (matrix, leftz); if (ret != HPDF_OK) goto failed; ret = HPDF_Array_AddReal (matrix, upx); if (ret != HPDF_OK) goto failed; ret = HPDF_Array_AddReal (matrix, upy); if (ret != HPDF_OK) goto failed; ret = HPDF_Array_AddReal (matrix, upz); if (ret != HPDF_OK) goto failed; ret = HPDF_Array_AddReal (matrix, viewx); if (ret != HPDF_OK) goto failed; ret = HPDF_Array_AddReal (matrix, viewy); if (ret != HPDF_OK) goto failed; ret = HPDF_Array_AddReal (matrix, viewz); if (ret != HPDF_OK) goto failed; ret = HPDF_Array_AddReal (matrix, transx); if (ret != HPDF_OK) goto failed; ret = HPDF_Array_AddReal (matrix, transy); if (ret != HPDF_OK) goto failed; ret = HPDF_Array_AddReal (matrix, transz); if (ret != HPDF_OK) goto failed; ret = HPDF_Dict_AddName (view, "MS", "M"); if (ret != HPDF_OK) goto failed; ret = HPDF_Dict_Add (view, "C2W", matrix); if (ret != HPDF_OK) goto failed; ret = HPDF_Dict_AddNumber (view, "CO", roo); failed: if (ret != HPDF_OK) { HPDF_Array_Free (matrix); return ret; } return ret; }
static HPDF_Font CIDFontType2_New (HPDF_Font parent, HPDF_Xref xref) { HPDF_STATUS ret = HPDF_OK; HPDF_FontAttr attr = (HPDF_FontAttr)parent->attr; HPDF_FontDef fontdef = attr->fontdef; HPDF_Encoder encoder = attr->encoder; HPDF_CMapEncoderAttr encoder_attr = (HPDF_CMapEncoderAttr)encoder->attr; HPDF_Font font; HPDF_Array array; HPDF_Dict cid_system_info; HPDF_PTRACE ((" HPDF_CIDFontType2_New\n")); font = HPDF_Dict_New (parent->mmgr); if (!font) return NULL; if (HPDF_Xref_Add (xref, font) != HPDF_OK) return NULL; parent->before_write_fn = CIDFontType2_BeforeWrite_Func; ret += HPDF_Dict_AddName (font, "Type", "Font"); ret += HPDF_Dict_AddName (font, "Subtype", "CIDFontType2"); ret += HPDF_Dict_AddNumber (font, "DW", fontdef->missing_width); if (ret != HPDF_OK) return NULL; /* add 'DW2' element */ array = HPDF_Array_New (font->mmgr); if (!array) return NULL; if (HPDF_Dict_Add (font, "DW2", array) != HPDF_OK) return NULL; ret += HPDF_Array_AddNumber (array, (HPDF_INT32)(fontdef->font_bbox.bottom)); ret += HPDF_Array_AddNumber (array, (HPDF_INT32)(fontdef->font_bbox.bottom - fontdef->font_bbox.top)); /* create CIDSystemInfo dictionary */ cid_system_info = HPDF_Dict_New (parent->mmgr); if (!cid_system_info) return NULL; if (HPDF_Dict_Add (font, "CIDSystemInfo", cid_system_info) != HPDF_OK) return NULL; ret += HPDF_Dict_Add (cid_system_info, "Registry", HPDF_String_New (parent->mmgr, encoder_attr->registry, NULL)); ret += HPDF_Dict_Add (cid_system_info, "Ordering", HPDF_String_New (parent->mmgr, encoder_attr->ordering, NULL)); ret += HPDF_Dict_AddNumber (cid_system_info, "Supplement", encoder_attr->suppliment); if (ret != HPDF_OK) return NULL; return font; }
HPDF_Shading_New (HPDF_Doc pdf, HPDF_ShadingType type, HPDF_ColorSpace colorSpace, HPDF_REAL xMin, HPDF_REAL xMax, HPDF_REAL yMin, HPDF_REAL yMax) { HPDF_Shading shading; HPDF_Array decodeArray; HPDF_STATUS ret = HPDF_OK; int i; HPDF_PTRACE((" HPDF_Shading_New\n")); if (!HPDF_HasDoc(pdf)) { return NULL; } /* Validate shading type: */ switch (type) { case HPDF_SHADING_FREE_FORM_TRIANGLE_MESH: break; default: HPDF_SetError (pdf->mmgr->error, HPDF_INVALID_SHADING_TYPE, 0); return NULL; } decodeArray = HPDF_Array_New(pdf->mmgr); if (!decodeArray) { return NULL; } /* X-range */ ret += HPDF_Array_AddReal(decodeArray, xMin); ret += HPDF_Array_AddReal(decodeArray, xMax); /* Y-range */ ret += HPDF_Array_AddReal(decodeArray, yMin); ret += HPDF_Array_AddReal(decodeArray, yMax); const char *colName = NULL; switch (colorSpace) { case HPDF_CS_DEVICE_RGB: colName = COL_RGB; for (i = 0; i < 3; ++i) { ret += HPDF_Array_AddReal(decodeArray, 0.0); ret += HPDF_Array_AddReal(decodeArray, 1.0); } break; default: HPDF_SetError(pdf->mmgr->error, HPDF_INVALID_COLOR_SPACE, 0); return NULL; } if (ret != HPDF_OK) { return NULL; } shading = HPDF_DictStream_New(pdf->mmgr, pdf->xref); if (!shading) { return NULL; } shading->header.obj_class |= HPDF_OSUBCLASS_SHADING; ret += HPDF_Dict_AddNumber(shading, "ShadingType", type); ret += HPDF_Dict_AddName(shading, "ColorSpace", colName); switch (type) { case HPDF_SHADING_FREE_FORM_TRIANGLE_MESH: ret += HPDF_Dict_AddNumber(shading, "BitsPerCoordinate", 32); ret += HPDF_Dict_AddNumber(shading, "BitsPerComponent", 8); ret += HPDF_Dict_AddNumber(shading, "BitsPerFlag", 8); ret += HPDF_Dict_Add(shading, "Decode", decodeArray); break; default: HPDF_SetError (pdf->mmgr->error, HPDF_INVALID_SHADING_TYPE, 0); return NULL; } if (ret != HPDF_OK) { return NULL; } return shading; }
HPDF_Image HPDF_Image_LoadRawImageFromMem (HPDF_MMgr mmgr, const HPDF_BYTE *buf, HPDF_Xref xref, HPDF_UINT width, HPDF_UINT height, HPDF_ColorSpace color_space, HPDF_UINT bits_per_component) { HPDF_Dict image; HPDF_STATUS ret = HPDF_OK; HPDF_UINT size=0; HPDF_PTRACE ((" HPDF_Image_LoadRawImageFromMem\n")); if (color_space != HPDF_CS_DEVICE_GRAY && color_space != HPDF_CS_DEVICE_RGB && color_space != HPDF_CS_DEVICE_CMYK) { HPDF_SetError (mmgr->error, HPDF_INVALID_COLOR_SPACE, 0); return NULL; } if (bits_per_component != 1 && bits_per_component != 2 && bits_per_component != 4 && bits_per_component != 8) { HPDF_SetError (mmgr->error, HPDF_INVALID_IMAGE, 0); return NULL; } image = HPDF_DictStream_New (mmgr, xref); if (!image) return NULL; image->header.obj_class |= HPDF_OSUBCLASS_XOBJECT; ret += HPDF_Dict_AddName (image, "Type", "XObject"); ret += HPDF_Dict_AddName (image, "Subtype", "Image"); if (ret != HPDF_OK) return NULL; switch (color_space) { case HPDF_CS_DEVICE_GRAY: size = (HPDF_UINT)((HPDF_DOUBLE)width * height / (8 / bits_per_component) + 0.876); ret = HPDF_Dict_AddName (image, "ColorSpace", COL_GRAY); break; case HPDF_CS_DEVICE_RGB: size = (HPDF_UINT)((HPDF_DOUBLE)width * height / (8 / bits_per_component) + 0.876); size *= 3; ret = HPDF_Dict_AddName (image, "ColorSpace", COL_RGB); break; case HPDF_CS_DEVICE_CMYK: size = (HPDF_UINT)((HPDF_DOUBLE)width * height / (8 / bits_per_component) + 0.876); size *= 4; ret = HPDF_Dict_AddName (image, "ColorSpace", COL_CMYK); break; default:; } if (ret != HPDF_OK) return NULL; if (HPDF_Dict_AddNumber (image, "Width", width) != HPDF_OK) return NULL; if (HPDF_Dict_AddNumber (image, "Height", height) != HPDF_OK) return NULL; if (HPDF_Dict_AddNumber (image, "BitsPerComponent", bits_per_component) != HPDF_OK) return NULL; if (HPDF_Stream_Write (image->stream, buf, size) != HPDF_OK) return NULL; return image; }
static HPDF_STATUS LoadJpegHeader (HPDF_Image image, HPDF_Stream stream) { HPDF_UINT16 tag; HPDF_UINT16 height; HPDF_UINT16 width; HPDF_BYTE precision; HPDF_BYTE num_components; const char *color_space_name; HPDF_UINT len; HPDF_STATUS ret; HPDF_Array array; HPDF_PTRACE ((" HPDF_Image_LoadJpegHeader\n")); len = 2; if (HPDF_Stream_Read (stream, (HPDF_BYTE *)&tag, &len) != HPDF_OK) return HPDF_Error_GetCode (stream->error); HPDF_UInt16Swap (&tag); if (tag != 0xFFD8) return HPDF_INVALID_JPEG_DATA; /* find SOF record */ for (;;) { HPDF_UINT16 size; len = 2; if (HPDF_Stream_Read (stream, (HPDF_BYTE *)&tag, &len) != HPDF_OK) return HPDF_Error_GetCode (stream->error); HPDF_UInt16Swap (&tag); len = 2; if (HPDF_Stream_Read (stream, (HPDF_BYTE *)&size, &len) != HPDF_OK) return HPDF_Error_GetCode (stream->error); HPDF_UInt16Swap (&size); HPDF_PTRACE (("tag=%04X size=%u\n", tag, size)); if (tag == 0xFFC0 || tag == 0xFFC1 || tag == 0xFFC2 || tag == 0xFFC9) { len = 1; if (HPDF_Stream_Read (stream, (HPDF_BYTE *)&precision, &len) != HPDF_OK) return HPDF_Error_GetCode (stream->error); len = 2; if (HPDF_Stream_Read (stream, (HPDF_BYTE *)&height, &len) != HPDF_OK) return HPDF_Error_GetCode (stream->error); HPDF_UInt16Swap (&height); len = 2; if (HPDF_Stream_Read (stream, (HPDF_BYTE *)&width, &len) != HPDF_OK) return HPDF_Error_GetCode (stream->error); HPDF_UInt16Swap (&width); len = 1; if (HPDF_Stream_Read (stream, (HPDF_BYTE *)&num_components, &len) != HPDF_OK) return HPDF_Error_GetCode (stream->error); break; } else if ((tag | 0x00FF) != 0xFFFF) /* lost marker */ return HPDF_SetError (image->error, HPDF_UNSUPPORTED_JPEG_FORMAT, 0); if (HPDF_Stream_Seek (stream, size - 2, HPDF_SEEK_CUR) != HPDF_OK) return HPDF_Error_GetCode (stream->error); } if (HPDF_Dict_AddNumber (image, "Height", height) != HPDF_OK) return HPDF_Error_GetCode (stream->error); if (HPDF_Dict_AddNumber (image, "Width", width) != HPDF_OK) return HPDF_Error_GetCode (stream->error); /* classification of RGB and CMYK is less than perfect * YCbCr and YCCK are classified into RGB or CMYK. * * It is necessary to read APP14 data to distinguish colorspace perfectly. */ switch (num_components) { case 1: color_space_name = COL_GRAY; break; case 3: color_space_name = COL_RGB; break; case 4: array = HPDF_Array_New (image->mmgr); if (!array) return HPDF_Error_GetCode (stream->error); ret = HPDF_Dict_Add (image, "Decode", array); if (ret != HPDF_OK) return HPDF_Error_GetCode (stream->error); ret += HPDF_Array_Add (array, HPDF_Number_New (image->mmgr, 1)); ret += HPDF_Array_Add (array, HPDF_Number_New (image->mmgr, 0)); ret += HPDF_Array_Add (array, HPDF_Number_New (image->mmgr, 1)); ret += HPDF_Array_Add (array, HPDF_Number_New (image->mmgr, 0)); ret += HPDF_Array_Add (array, HPDF_Number_New (image->mmgr, 1)); ret += HPDF_Array_Add (array, HPDF_Number_New (image->mmgr, 0)); ret += HPDF_Array_Add (array, HPDF_Number_New (image->mmgr, 1)); ret += HPDF_Array_Add (array, HPDF_Number_New (image->mmgr, 0)); if (ret != HPDF_OK) return HPDF_Error_GetCode (stream->error); color_space_name = COL_CMYK; break; default: return HPDF_SetError (image->error, HPDF_UNSUPPORTED_JPEG_FORMAT, 0); } if (HPDF_Dict_Add (image, "ColorSpace", HPDF_Name_New (image->mmgr, color_space_name)) != HPDF_OK) return HPDF_Error_GetCode (stream->error); if (HPDF_Dict_Add (image, "BitsPerComponent", HPDF_Number_New (image->mmgr, precision)) != HPDF_OK) return HPDF_Error_GetCode (stream->error); return HPDF_OK; }
static HPDF_STATUS Type1Font_CreateDescriptor (HPDF_MMgr mmgr, HPDF_Font font, HPDF_Xref xref) { HPDF_FontAttr font_attr = (HPDF_FontAttr)font->attr; HPDF_FontDef def = font_attr->fontdef; HPDF_Type1FontDefAttr def_attr = (HPDF_Type1FontDefAttr)def->attr; HPDF_PTRACE ((" HPDF_Type1Font_CreateDescriptor\n")); if (!font_attr->fontdef->descriptor) { HPDF_Dict descriptor = HPDF_Dict_New (mmgr); HPDF_STATUS ret = 0; HPDF_Array array; if (!descriptor) return HPDF_Error_GetCode (font->error); ret += HPDF_Xref_Add (xref, descriptor); ret += HPDF_Dict_AddName (descriptor, "Type", "FontDescriptor"); ret += HPDF_Dict_AddNumber (descriptor, "Ascent", def->ascent); ret += HPDF_Dict_AddNumber (descriptor, "Descent", def->descent); if (def->cap_height) { ret += HPDF_Dict_AddNumber (descriptor, "CapHeight", def->cap_height); } ret += HPDF_Dict_AddNumber (descriptor, "Flags", def->flags); array = HPDF_Box_Array_New (mmgr, def->font_bbox); ret += HPDF_Dict_Add (descriptor, "FontBBox", array); if(strncmp (font_attr->fontdef->base_font, "HPDF_", 5) == 0 && font_attr->fontdef->is_form_font == HPDF_TRUE) { ret += HPDF_Dict_AddName (descriptor, "FontName", font_attr->fontdef->base_font + 5); } else { ret += HPDF_Dict_AddName (descriptor, "FontName", font_attr->fontdef->base_font); } ret += HPDF_Dict_AddNumber (descriptor, "ItalicAngle", def->italic_angle); ret += HPDF_Dict_AddNumber (descriptor, "StemV", def->stemv); ret += HPDF_Dict_AddNumber (descriptor, "XHeight", def->x_height); if (def_attr->char_set) ret += HPDF_Dict_AddName (descriptor, "CharSet", def_attr->char_set); if (ret != HPDF_OK) return HPDF_Error_GetCode (font->error); if (def_attr->font_data) { HPDF_Dict font_data = HPDF_DictStream_New (mmgr, xref); if (!font_data) return HPDF_Error_GetCode (font->error); if (HPDF_Stream_WriteToStream (def_attr->font_data, font_data->stream, HPDF_STREAM_FILTER_NONE, NULL) != HPDF_OK) return HPDF_Error_GetCode (font->error); ret += HPDF_Dict_Add (descriptor, "FontFile", font_data); ret += HPDF_Dict_AddNumber (font_data, "Length1", def_attr->length1); ret += HPDF_Dict_AddNumber (font_data, "Length2", def_attr->length2); ret += HPDF_Dict_AddNumber (font_data, "Length3", def_attr->length3); font_data->filter = font->filter; } if (ret != HPDF_OK) return HPDF_Error_GetCode (font->error); font_attr->fontdef->descriptor = descriptor; } return HPDF_Dict_Add (font, "FontDescriptor", font_attr->fontdef->descriptor); }
HPDF_Annotation HPDF_3DAnnot_New (HPDF_MMgr mmgr, HPDF_Xref xref, HPDF_Rect rect, HPDF_BOOL tb, HPDF_BOOL np, HPDF_U3D u3d, HPDF_Image ap) { HPDF_Annotation annot; HPDF_Dict action, appearance, stream; HPDF_STATUS ret; HPDF_PTRACE((" HPDF_3DAnnot_New\n")); annot = HPDF_Annotation_New (mmgr, xref, HPDF_ANNOT_3D, rect); if (!annot) { return NULL; } // include the flags HPDF_Dict_AddNumber (annot, "F", 68); //Bit 3:Print If set, print the annotation when the page is printed. //Bit 7:If set, do not allow the annotation to interact with the user. // The annotation may be displayed or printed (depending on the settings of the NoView and Print flags) // but should not respond to mouse clicks or change its appearance in response to mouse motions. HPDF_Dict_Add(annot, "Contents", HPDF_String_New (mmgr, "3D Model", NULL)); action = HPDF_Dict_New (mmgr); if (!action) { return NULL; } ret = HPDF_Dict_Add (annot, "3DA", action); if (ret != HPDF_OK) { return NULL; } // enable visibility on page open ret += HPDF_Dict_AddName (action, "A", "PO"); // enable visibility of ToolBar ret += HPDF_Dict_AddBoolean(action, "TB", tb); // enable visibility of Navigation Panel ret += HPDF_Dict_AddBoolean(action, "NP", np); // Set behavior of Annotation on Disabling ret += HPDF_Dict_AddName(action, "DIS", "U"); // Set behavior of Annotation upon activation ret += HPDF_Dict_AddName(action, "AIS", "L"); if (ret != HPDF_OK) { return NULL; } if (HPDF_Dict_Add (annot, "3DD", u3d) != HPDF_OK) { return NULL; } appearance = HPDF_Dict_New (mmgr); if (!appearance) { return NULL; } ret = HPDF_Dict_Add (annot, "AP", appearance); if (ret != HPDF_OK) { return NULL; } if (ap) { if (HPDF_Dict_Add (appearance, "N", ap) != HPDF_OK) return NULL; } else{ stream = HPDF_Dict_New (mmgr); if (!stream) { return NULL; } ret = HPDF_Dict_Add (appearance, "N", stream); } if (ret != HPDF_OK) { return NULL; } return annot; }
HPDF_Font HPDF_TTFont_New (HPDF_MMgr mmgr, HPDF_FontDef fontdef, HPDF_Encoder encoder, HPDF_Xref xref) { HPDF_Dict font; HPDF_FontAttr attr; HPDF_TTFontDefAttr fontdef_attr; HPDF_BasicEncoderAttr encoder_attr; HPDF_STATUS ret = 0; HPDF_PTRACE ((" HPDF_TTFont_New\n")); font = HPDF_Dict_New (mmgr); if (!font) return NULL; font->header.obj_class |= HPDF_OSUBCLASS_FONT; /* check whether the fontdef object and the encoder object is valid. */ if (encoder->type != HPDF_ENCODER_TYPE_SINGLE_BYTE) { HPDF_SetError(font->error, HPDF_INVALID_ENCODER_TYPE, 0); return NULL; } if (fontdef->type != HPDF_FONTDEF_TYPE_TRUETYPE) { HPDF_SetError(font->error, HPDF_INVALID_FONTDEF_TYPE, 0); return NULL; } attr = (HPDF_FontAttr)HPDF_GetMem (mmgr, sizeof(HPDF_FontAttr_Rec)); if (!attr) { HPDF_Dict_Free (font); return NULL; } HPDF_MemSet (attr, 0, sizeof(HPDF_FontAttr_Rec)); font->header.obj_class |= HPDF_OSUBCLASS_FONT; font->write_fn = OnWrite; font->before_write_fn = BeforeWrite; font->free_fn = OnFree; font->attr = attr; attr->type = HPDF_FONT_TRUETYPE; attr->writing_mode = HPDF_WMODE_HORIZONTAL; attr->text_width_fn = TextWidth; attr->measure_text_fn = MeasureText; attr->fontdef = fontdef; attr->encoder = encoder; attr->xref = xref; /* singlebyte-font has a widths-array which is an array of 256 signed * short integer. * in the case of type1-font, widths-array for all letters is made in * constructer. but in the case of true-type-font, the array is * initialized at 0, and set when the corresponding character was used * for the first time. */ attr->widths = (HPDF_INT16*)HPDF_GetMem (mmgr, sizeof(HPDF_INT16) * 256); if (!attr->widths) { HPDF_Dict_Free (font); return NULL; } HPDF_MemSet (attr->widths, 0, sizeof(HPDF_INT16) * 256); attr->used = (HPDF_BYTE*)HPDF_GetMem (mmgr, sizeof(HPDF_BYTE) * 256); if (!attr->used) { HPDF_Dict_Free (font); return NULL; } HPDF_MemSet (attr->used, 0, sizeof(HPDF_BYTE) * 256); fontdef_attr = (HPDF_TTFontDefAttr)fontdef->attr; ret += HPDF_Dict_AddName (font, "Type", "Font"); ret += HPDF_Dict_AddName (font, "BaseFont", fontdef_attr->base_font); ret += HPDF_Dict_AddName (font, "Subtype", "TrueType"); encoder_attr = (HPDF_BasicEncoderAttr)encoder->attr; ret += HPDF_Dict_AddNumber (font, "FirstChar", encoder_attr->first_char); ret += HPDF_Dict_AddNumber (font, "LastChar", encoder_attr->last_char); if (fontdef->missing_width != 0) ret += HPDF_Dict_AddNumber (font, "MissingWidth", fontdef->missing_width); if (ret != HPDF_OK) { HPDF_Dict_Free (font); return NULL; } if (HPDF_Xref_Add (xref, font) != HPDF_OK) return NULL; return font; }
static HPDF_STATUS CreateDescriptor (HPDF_Font font) { HPDF_FontAttr font_attr = (HPDF_FontAttr)font->attr; HPDF_FontDef def = font_attr->fontdef; HPDF_TTFontDefAttr def_attr = (HPDF_TTFontDefAttr)def->attr; HPDF_PTRACE ((" HPDF_TTFont_CreateDescriptor\n")); if (!font_attr->fontdef->descriptor) { HPDF_Dict descriptor = HPDF_Dict_New (font->mmgr); HPDF_STATUS ret = 0; HPDF_Array array; if (!descriptor) return HPDF_Error_GetCode (font->error); ret += HPDF_Xref_Add (font_attr->xref, descriptor); ret += HPDF_Dict_AddName (descriptor, "Type", "FontDescriptor"); ret += HPDF_Dict_AddNumber (descriptor, "Ascent", def->ascent); ret += HPDF_Dict_AddNumber (descriptor, "Descent", def->descent); ret += HPDF_Dict_AddNumber (descriptor, "Flags", def->flags); array = HPDF_Box_Array_New (font->mmgr, def->font_bbox); ret += HPDF_Dict_Add (descriptor, "FontBBox", array); ret += HPDF_Dict_AddName (descriptor, "FontName", def_attr->base_font); ret += HPDF_Dict_AddNumber (descriptor, "ItalicAngle", def->italic_angle); ret += HPDF_Dict_AddNumber (descriptor, "StemV", def->stemv); ret += HPDF_Dict_AddNumber (descriptor, "XHeight", def->x_height); if (def_attr->char_set) ret += HPDF_Dict_AddName (descriptor, "CharSet", def_attr->char_set); if (ret != HPDF_OK) return HPDF_Error_GetCode (font->error); if (def_attr->embedding) { HPDF_Dict font_data = HPDF_DictStream_New (font->mmgr, font_attr->xref); if (!font_data) return HPDF_Error_GetCode (font->error); if (HPDF_TTFontDef_SaveFontData (font_attr->fontdef, font_data->stream) != HPDF_OK) return HPDF_Error_GetCode (font->error); ret += HPDF_Dict_Add (descriptor, "FontFile2", font_data); ret += HPDF_Dict_AddNumber (font_data, "Length1", def_attr->length1); ret += HPDF_Dict_AddNumber (font_data, "Length2", 0); ret += HPDF_Dict_AddNumber (font_data, "Length3", 0); font_data->filter = font->filter; } if (ret != HPDF_OK) return HPDF_Error_GetCode (font->error); font_attr->fontdef->descriptor = descriptor; } return HPDF_Dict_Add (font, "FontDescriptor", font_attr->fontdef->descriptor); }
HPDF_Font HPDF_Type1Font_New (HPDF_MMgr mmgr, HPDF_FontDef fontdef, HPDF_Encoder encoder, HPDF_Xref xref) { HPDF_Dict font; HPDF_FontAttr attr; HPDF_Type1FontDefAttr fontdef_attr; HPDF_BasicEncoderAttr encoder_attr; HPDF_STATUS ret = 0; HPDF_UINT i; HPDF_PTRACE ((" HPDF_Type1Font_New\n")); /* check whether the fontdef object and the encoder object is valid. */ if (encoder->type != HPDF_ENCODER_TYPE_SINGLE_BYTE) { HPDF_SetError(mmgr->error, HPDF_INVALID_ENCODER_TYPE, 0); return NULL; } if (fontdef->type != HPDF_FONTDEF_TYPE_TYPE1) { HPDF_SetError(mmgr->error, HPDF_INVALID_FONTDEF_TYPE, 0); return NULL; } font = HPDF_Dict_New (mmgr); if (!font) return NULL; font->header.obj_class |= HPDF_OSUBCLASS_FONT; attr = HPDF_GetMem (mmgr, sizeof(HPDF_FontAttr_Rec)); if (!attr) { HPDF_Dict_Free (font); return NULL; } font->header.obj_class |= HPDF_OSUBCLASS_FONT; font->write_fn = Type1Font_OnWrite; font->free_fn = Type1Font_OnFree; HPDF_MemSet (attr, 0, sizeof(HPDF_FontAttr_Rec)); font->attr = attr; attr->type = HPDF_FONT_TYPE1; attr->writing_mode = HPDF_WMODE_HORIZONTAL; attr->text_width_fn = Type1Font_TextWidth; attr->measure_text_fn = Type1Font_MeasureText; attr->fontdef = fontdef; attr->encoder = encoder; attr->xref = xref; /* singlebyte-font has a widths-array which is an array of 256 signed * short integer. */ attr->widths = HPDF_GetMem (mmgr, sizeof(HPDF_INT16) * 256); if (!attr->widths) { HPDF_Dict_Free (font); return NULL; } encoder_attr = (HPDF_BasicEncoderAttr)encoder->attr; HPDF_MemSet (attr->widths, 0, sizeof(HPDF_INT16) * 256); for (i = encoder_attr->first_char; i <= encoder_attr->last_char; i++) { HPDF_UNICODE u = encoder_attr->unicode_map[i]; HPDF_UINT16 w = HPDF_Type1FontDef_GetWidth (fontdef, u); attr->widths[i] = w; } fontdef_attr = (HPDF_Type1FontDefAttr)fontdef->attr; if(strncmp (fontdef->base_font, "HPDF_", 5) == 0 && fontdef->is_form_font == HPDF_TRUE) { ret += HPDF_Dict_AddName (font, "Type", "Font"); ret += HPDF_Dict_AddName (font, "BaseFont", fontdef->base_font + 5); ret += HPDF_Dict_AddName (font, "Subtype", "TrueType"); } else{ ret += HPDF_Dict_AddName (font, "Type", "Font"); ret += HPDF_Dict_AddName (font, "BaseFont", fontdef->base_font); ret += HPDF_Dict_AddName (font, "Subtype", "Type1"); } if (!fontdef_attr->is_base14font) { if (fontdef->missing_width != 0) ret += HPDF_Dict_AddNumber (font, "MissingWidth", fontdef->missing_width); ret += Type1Font_CreateDescriptor (mmgr, font, xref); } if (ret != HPDF_OK) { HPDF_Dict_Free (font); return NULL; } if (HPDF_Xref_Add (xref, font) != HPDF_OK) return NULL; return font; }
static HPDF_Font CIDFontType0_New (HPDF_Font parent, HPDF_Xref xref) { HPDF_STATUS ret = HPDF_OK; HPDF_FontAttr attr = (HPDF_FontAttr)parent->attr; HPDF_FontDef fontdef = attr->fontdef; HPDF_CIDFontDefAttr fontdef_attr = (HPDF_CIDFontDefAttr)fontdef->attr; HPDF_Encoder encoder = attr->encoder; HPDF_CMapEncoderAttr encoder_attr = (HPDF_CMapEncoderAttr)encoder->attr; HPDF_UINT16 save_cid = 0; HPDF_Font font; HPDF_Array array; HPDF_Array sub_array = NULL; HPDF_UINT i; HPDF_Dict descriptor; HPDF_Dict cid_system_info; HPDF_PTRACE ((" HPDF_CIDFontType0_New\n")); font = HPDF_Dict_New (parent->mmgr); if (!font) return NULL; if (HPDF_Xref_Add (xref, font) != HPDF_OK) return NULL; ret += HPDF_Dict_AddName (font, "Type", "Font"); ret += HPDF_Dict_AddName (font, "Subtype", "CIDFontType0"); ret += HPDF_Dict_AddNumber (font, "DW", fontdef_attr->DW); ret += HPDF_Dict_AddName (font, "BaseFont", fontdef->base_font); if (ret != HPDF_OK) return NULL; /* add 'DW2' element */ array = HPDF_Array_New (parent->mmgr); if (!array) return NULL; if (HPDF_Dict_Add (font, "DW2", array) != HPDF_OK) return NULL; ret += HPDF_Array_AddNumber (array, fontdef_attr->DW2[0]); ret += HPDF_Array_AddNumber (array, fontdef_attr->DW2[1]); if (ret != HPDF_OK) return NULL; /* add 'W' element */ array = HPDF_Array_New (parent->mmgr); if (!array) return NULL; if (HPDF_Dict_Add (font, "W", array) != HPDF_OK) return NULL; /* Create W array. */ for (i = 0; i< fontdef_attr->widths->count; i++) { HPDF_CID_Width *w = (HPDF_CID_Width *)HPDF_List_ItemAt (fontdef_attr->widths, i); if (w->cid != save_cid + 1 || !sub_array) { sub_array = HPDF_Array_New (parent->mmgr); if (!sub_array) return NULL; ret += HPDF_Array_AddNumber (array, w->cid); ret += HPDF_Array_Add (array, sub_array); } ret += HPDF_Array_AddNumber (sub_array, w->width); save_cid = w->cid; if (ret != HPDF_OK) return NULL; } /* create descriptor */ descriptor = HPDF_Dict_New (parent->mmgr); if (!descriptor) return NULL; if (HPDF_Xref_Add (xref, descriptor) != HPDF_OK) return NULL; if (HPDF_Dict_Add (font, "FontDescriptor", descriptor) != HPDF_OK) return NULL; ret += HPDF_Dict_AddName (descriptor, "Type", "FontDescriptor"); ret += HPDF_Dict_AddName (descriptor, "FontName", fontdef->base_font); ret += HPDF_Dict_AddNumber (descriptor, "Ascent", fontdef->ascent); ret += HPDF_Dict_AddNumber (descriptor, "Descent", fontdef->descent); ret += HPDF_Dict_AddNumber (descriptor, "CapHeight", fontdef->cap_height); ret += HPDF_Dict_AddNumber (descriptor, "MissingWidth", fontdef->missing_width); ret += HPDF_Dict_AddNumber (descriptor, "Flags", fontdef->flags); if (ret != HPDF_OK) return NULL; array = HPDF_Box_Array_New (parent->mmgr, fontdef->font_bbox); if (!array) return NULL; ret += HPDF_Dict_Add (descriptor, "FontBBox", array); ret += HPDF_Dict_AddNumber (descriptor, "ItalicAngle", fontdef->italic_angle); ret += HPDF_Dict_AddNumber (descriptor, "StemV", fontdef->stemv); if (ret != HPDF_OK) return NULL; /* create CIDSystemInfo dictionary */ cid_system_info = HPDF_Dict_New (parent->mmgr); if (!cid_system_info) return NULL; if (HPDF_Dict_Add (font, "CIDSystemInfo", cid_system_info) != HPDF_OK) return NULL; ret += HPDF_Dict_Add (cid_system_info, "Registry", HPDF_String_New (parent->mmgr, encoder_attr->registry, NULL)); ret += HPDF_Dict_Add (cid_system_info, "Ordering", HPDF_String_New (parent->mmgr, encoder_attr->ordering, NULL)); ret += HPDF_Dict_AddNumber (cid_system_info, "Supplement", encoder_attr->suppliment); if (ret != HPDF_OK) return NULL; return font; }
HPDF_Image HPDF_Image_LoadRawImage (HPDF_MMgr mmgr, HPDF_Stream raw_data, HPDF_Xref xref, HPDF_UINT width, HPDF_UINT height, HPDF_ColorSpace color_space) { HPDF_Dict image; HPDF_STATUS ret = HPDF_OK; HPDF_UINT size; HPDF_PTRACE ((" HPDF_Image_LoadRawImage\n")); if (color_space != HPDF_CS_DEVICE_GRAY && color_space != HPDF_CS_DEVICE_RGB && color_space != HPDF_CS_DEVICE_CMYK) { HPDF_SetError (mmgr->error, HPDF_INVALID_COLOR_SPACE, 0); return NULL; } image = HPDF_DictStream_New (mmgr, xref); if (!image) return NULL; image->header.obj_class |= HPDF_OSUBCLASS_XOBJECT; ret += HPDF_Dict_AddName (image, "Type", "XObject"); ret += HPDF_Dict_AddName (image, "Subtype", "Image"); if (ret != HPDF_OK) return NULL; if (color_space == HPDF_CS_DEVICE_GRAY) { size = width * height; ret = HPDF_Dict_AddName (image, "ColorSpace", COL_GRAY); } else if (color_space == HPDF_CS_DEVICE_CMYK) { size = width * height * 4; ret = HPDF_Dict_AddName (image, "ColorSpace", COL_CMYK); } else { size = width * height * 3; ret = HPDF_Dict_AddName (image, "ColorSpace", COL_RGB); } if (ret != HPDF_OK) return NULL; if (HPDF_Dict_AddNumber (image, "Width", width) != HPDF_OK) return NULL; if (HPDF_Dict_AddNumber (image, "Height", height) != HPDF_OK) return NULL; if (HPDF_Dict_AddNumber (image, "BitsPerComponent", 8) != HPDF_OK) return NULL; if (HPDF_Stream_WriteToStream (raw_data, image->stream, 0, NULL) != HPDF_OK) return NULL; if (image->stream->size != size) { HPDF_SetError (image->error, HPDF_INVALID_IMAGE, 0); return NULL; } return image; }
static HPDF_Font CIDFontType2_New (HPDF_Font parent, HPDF_Xref xref) { HPDF_STATUS ret = HPDF_OK; HPDF_FontAttr attr = (HPDF_FontAttr)parent->attr; HPDF_FontDef fontdef = attr->fontdef; HPDF_TTFontDefAttr fontdef_attr = (HPDF_TTFontDefAttr)fontdef->attr; HPDF_Encoder encoder = attr->encoder; HPDF_CMapEncoderAttr encoder_attr = (HPDF_CMapEncoderAttr)encoder->attr; HPDF_Font font; HPDF_Array array; HPDF_UINT i; HPDF_UNICODE tmp_map[65536]; HPDF_Dict cid_system_info; HPDF_UINT16 max = 0; HPDF_PTRACE ((" HPDF_CIDFontType2_New\n")); font = HPDF_Dict_New (parent->mmgr); if (!font) return NULL; if (HPDF_Xref_Add (xref, font) != HPDF_OK) return NULL; parent->before_write_fn = CIDFontType2_BeforeWrite_Func; ret += HPDF_Dict_AddName (font, "Type", "Font"); ret += HPDF_Dict_AddName (font, "Subtype", "CIDFontType2"); ret += HPDF_Dict_AddNumber (font, "DW", fontdef->missing_width); if (ret != HPDF_OK) return NULL; /* add 'DW2' element */ array = HPDF_Array_New (font->mmgr); if (!array) return NULL; if (HPDF_Dict_Add (font, "DW2", array) != HPDF_OK) return NULL; ret += HPDF_Array_AddNumber (array, (HPDF_INT32)(fontdef->font_bbox.bottom)); ret += HPDF_Array_AddNumber (array, (HPDF_INT32)(fontdef->font_bbox.bottom - fontdef->font_bbox.top)); HPDF_MemSet (tmp_map, 0, sizeof(HPDF_UNICODE) * 65536); if (ret != HPDF_OK) return NULL; for (i = 0; i < 256; i++) { HPDF_UINT j; for (j = 0; j < 256; j++) { if (encoder->to_unicode_fn == HPDF_CMapEncoder_ToUnicode) { HPDF_UINT16 cid = encoder_attr->cid_map[i][j]; if (cid != 0) { HPDF_UNICODE unicode = encoder_attr->unicode_map[i][j]; HPDF_UINT16 gid = HPDF_TTFontDef_GetGlyphid (fontdef, unicode); tmp_map[cid] = gid; if (max < cid) max = cid; } } else { HPDF_UNICODE unicode = (i << 8) | j; HPDF_UINT16 gid = HPDF_TTFontDef_GetGlyphid (fontdef, unicode); tmp_map[unicode] = gid; if (max < unicode) max = unicode; } } } if (max > 0) { HPDF_INT16 dw = fontdef->missing_width; HPDF_UNICODE *ptmp_map = tmp_map; HPDF_Array tmp_array = NULL; /* add 'W' element */ array = HPDF_Array_New (font->mmgr); if (!array) return NULL; if (HPDF_Dict_Add (font, "W", array) != HPDF_OK) return NULL; for (i = 0; i < max; i++, ptmp_map++) { HPDF_INT w = HPDF_TTFontDef_GetGidWidth (fontdef, *ptmp_map); if (w != dw) { if (!tmp_array) { if (HPDF_Array_AddNumber (array, i) != HPDF_OK) return NULL; tmp_array = HPDF_Array_New (font->mmgr); if (!tmp_array) return NULL; if (HPDF_Array_Add (array, tmp_array) != HPDF_OK) return NULL; } if ((ret = HPDF_Array_AddNumber (tmp_array, w)) != HPDF_OK) return NULL; } else tmp_array = NULL; } /* create "CIDToGIDMap" data */ if (fontdef_attr->embedding) { attr->map_stream = HPDF_DictStream_New (font->mmgr, xref); if (!attr->map_stream) return NULL; if (HPDF_Dict_Add (font, "CIDToGIDMap", attr->map_stream) != HPDF_OK) return NULL; for (i = 0; i < max; i++) { HPDF_BYTE u[2]; HPDF_UINT16 gid = tmp_map[i]; u[0] = (HPDF_BYTE)(gid >> 8); u[1] = (HPDF_BYTE)gid; HPDF_MemCpy ((HPDF_BYTE *)(tmp_map + i), u, 2); } if ((ret = HPDF_Stream_Write (attr->map_stream->stream, (HPDF_BYTE *)tmp_map, max * 2)) != HPDF_OK) return NULL; }
static HPDF_STATUS LoadPngData (HPDF_Dict image, HPDF_Xref xref, HPDF_Stream png_data, HPDF_BOOL delayed_loading) { HPDF_STATUS ret = HPDF_OK; png_uint_32 width, height; int bit_depth, color_type; png_structp png_ptr = NULL; png_infop info_ptr = NULL; HPDF_PTRACE ((" HPDF_Image_LoadPngImage\n")); /* create read_struct. */ png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, image->error, PngErrorFunc, PngErrorFunc); if (png_ptr == NULL) { HPDF_SetError (image->error, HPDF_FAILD_TO_ALLOC_MEM, 0); return HPDF_FAILD_TO_ALLOC_MEM; } /* create info-struct */ info_ptr = png_create_info_struct (png_ptr); if (info_ptr == NULL) { HPDF_SetError (image->error, HPDF_FAILD_TO_ALLOC_MEM, 0); goto Exit; } png_set_sig_bytes (png_ptr, HPDF_PNG_BYTES_TO_CHECK); png_set_read_fn (png_ptr, (void *)png_data, (png_rw_ptr)&PngReadFunc); /* reading info structure. */ png_read_info(png_ptr, info_ptr); if (image->error->error_no != HPDF_OK) { goto Exit; } png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, NULL, NULL, NULL); /* 16bit images are not supported. */ if (bit_depth == 16) { png_set_strip_16(png_ptr); } png_read_update_info(png_ptr, info_ptr); if (image->error->error_no != HPDF_OK) { goto Exit; } /* check palette-based images for transparent areas and load them immediately if found */ if (xref && PNG_COLOR_TYPE_PALETTE & color_type) { png_bytep trans; int num_trans; HPDF_Dict smask; png_bytep smask_data; if (!png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS) || !png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, NULL)) { goto no_transparent_color_in_palette; } smask = HPDF_DictStream_New (image->mmgr, xref); if (!smask) { ret = HPDF_FAILD_TO_ALLOC_MEM; goto Exit; } smask->header.obj_class |= HPDF_OSUBCLASS_XOBJECT; ret = HPDF_Dict_AddName (smask, "Type", "XObject"); ret += HPDF_Dict_AddName (smask, "Subtype", "Image"); ret += HPDF_Dict_AddNumber (smask, "Width", (HPDF_UINT)width); ret += HPDF_Dict_AddNumber (smask, "Height", (HPDF_UINT)height); ret += HPDF_Dict_AddName (smask, "ColorSpace", "DeviceGray"); ret += HPDF_Dict_AddNumber (smask, "BitsPerComponent", (HPDF_UINT)bit_depth); if (ret != HPDF_OK) { HPDF_Dict_Free(smask); ret = HPDF_INVALID_PNG_IMAGE; goto Exit; } smask_data = HPDF_GetMem(image->mmgr, width * height); if (!smask_data) { HPDF_Dict_Free(smask); ret = HPDF_FAILD_TO_ALLOC_MEM; goto Exit; } if (ReadTransparentPaletteData(image, png_ptr, info_ptr, smask_data, trans, num_trans) != HPDF_OK) { HPDF_FreeMem(image->mmgr, smask_data); HPDF_Dict_Free(smask); ret = HPDF_INVALID_PNG_IMAGE; goto Exit; } if (HPDF_Stream_Write(smask->stream, smask_data, width * height) != HPDF_OK) { HPDF_FreeMem(image->mmgr, smask_data); HPDF_Dict_Free(smask); ret = HPDF_FILE_IO_ERROR; goto Exit; } HPDF_FreeMem(image->mmgr, smask_data); ret += CreatePallet(image, png_ptr, info_ptr); ret += HPDF_Dict_AddNumber (image, "Width", (HPDF_UINT)width); ret += HPDF_Dict_AddNumber (image, "Height", (HPDF_UINT)height); ret += HPDF_Dict_AddNumber (image, "BitsPerComponent", (HPDF_UINT)bit_depth); ret += HPDF_Dict_Add (image, "SMask", smask); png_destroy_read_struct(&png_ptr, &info_ptr, NULL); return HPDF_OK; } no_transparent_color_in_palette: /* read images with alpha channel right away we have to do this because image transparent mask must be added to the Xref */ if (xref && PNG_COLOR_MASK_ALPHA & color_type) { HPDF_Dict smask; png_bytep smask_data; smask = HPDF_DictStream_New (image->mmgr, xref); if (!smask) { ret = HPDF_FAILD_TO_ALLOC_MEM; goto Exit; } smask->header.obj_class |= HPDF_OSUBCLASS_XOBJECT; ret = HPDF_Dict_AddName (smask, "Type", "XObject"); ret += HPDF_Dict_AddName (smask, "Subtype", "Image"); ret += HPDF_Dict_AddNumber (smask, "Width", (HPDF_UINT)width); ret += HPDF_Dict_AddNumber (smask, "Height", (HPDF_UINT)height); ret += HPDF_Dict_AddName (smask, "ColorSpace", "DeviceGray"); ret += HPDF_Dict_AddNumber (smask, "BitsPerComponent", (HPDF_UINT)bit_depth); if (ret != HPDF_OK) { HPDF_Dict_Free(smask); ret = HPDF_INVALID_PNG_IMAGE; goto Exit; } smask_data = HPDF_GetMem(image->mmgr, width * height); if (!smask_data) { HPDF_Dict_Free(smask); ret = HPDF_FAILD_TO_ALLOC_MEM; goto Exit; } if (ReadTransparentPngData(image, png_ptr, info_ptr, smask_data) != HPDF_OK) { HPDF_FreeMem(image->mmgr, smask_data); HPDF_Dict_Free(smask); ret = HPDF_INVALID_PNG_IMAGE; goto Exit; } if (HPDF_Stream_Write(smask->stream, smask_data, width * height) != HPDF_OK) { HPDF_FreeMem(image->mmgr, smask_data); HPDF_Dict_Free(smask); ret = HPDF_FILE_IO_ERROR; goto Exit; } HPDF_FreeMem(image->mmgr, smask_data); if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA) { ret += HPDF_Dict_AddName (image, "ColorSpace", "DeviceGray"); } else { ret += HPDF_Dict_AddName (image, "ColorSpace", "DeviceRGB"); } ret += HPDF_Dict_AddNumber (image, "Width", (HPDF_UINT)width); ret += HPDF_Dict_AddNumber (image, "Height", (HPDF_UINT)height); ret += HPDF_Dict_AddNumber (image, "BitsPerComponent", (HPDF_UINT)bit_depth); ret += HPDF_Dict_Add (image, "SMask", smask); png_destroy_read_struct(&png_ptr, &info_ptr, NULL); return HPDF_OK; } /* if the image has color palette, copy the pallet of the image to * create color map. */ if (color_type == PNG_COLOR_TYPE_PALETTE) ret = CreatePallet(image, png_ptr, info_ptr); else if (color_type == PNG_COLOR_TYPE_GRAY) ret = HPDF_Dict_AddName (image, "ColorSpace", "DeviceGray"); else ret = HPDF_Dict_AddName (image, "ColorSpace", "DeviceRGB"); if (ret != HPDF_OK) goto Exit; /* read image-data * if the image is interlaced, read whole image at once. * if delayed_loading is HPDF_TRUE, the data does not load this phase. */ if (delayed_loading) { image->before_write_fn = PngBeforeWrite; image->after_write_fn = PngAfterWrite; } else { if (png_get_interlace_type(png_ptr, info_ptr) != PNG_INTERLACE_NONE) ret = ReadPngData_Interlaced(image, png_ptr, info_ptr); else ret = ReadPngData(image, png_ptr, info_ptr); if (ret != HPDF_OK) goto Exit; } /* setting the info of the image. */ if (HPDF_Dict_AddNumber (image, "Width", (HPDF_UINT)width) != HPDF_OK) goto Exit; if (HPDF_Dict_AddNumber (image, "Height", (HPDF_UINT)height) != HPDF_OK) goto Exit; if (HPDF_Dict_AddNumber (image, "BitsPerComponent", (HPDF_UINT)bit_depth) != HPDF_OK) goto Exit; /* clean up */ png_destroy_read_struct(&png_ptr, &info_ptr, NULL); return HPDF_OK; Exit: png_destroy_read_struct(&png_ptr, &info_ptr, NULL); if (ret != HPDF_OK) { return ret; } return image->error->error_no; }