HPDF_Encoder HPDF_Doc_FindEncoder (HPDF_Doc pdf, const char *encoding_name) { HPDF_List list = pdf->encoder_list; HPDF_UINT i; HPDF_PTRACE ((" HPDF_Doc_FindEncoder\n")); for (i = 0; i < list->count; i++) { HPDF_Encoder encoder = (HPDF_Encoder)HPDF_List_ItemAt (list, i); if (HPDF_StrCmp (encoding_name, encoder->name) == 0) { /* if encoder is uninitialize, call init_fn() */ if (encoder->type == HPDF_ENCODER_TYPE_UNINITIALIZED) { if (!encoder->init_fn || encoder->init_fn (encoder) != HPDF_OK) return NULL; } return encoder; } } return NULL; }
HPDF_STATUS HPDF_Array_Write (HPDF_Array array, HPDF_Stream stream, HPDF_Encrypt e) { HPDF_UINT i; HPDF_STATUS ret; HPDF_PTRACE((" HPDF_Array_Write\n")); ret = HPDF_Stream_WriteStr (stream, "[ "); if (ret != HPDF_OK) return ret; for (i = 0; i < array->list->count; i++) { void * element = HPDF_List_ItemAt (array->list, i); ret = HPDF_Obj_Write (element, stream, e); if (ret != HPDF_OK) return ret; ret = HPDF_Stream_WriteChar (stream, ' '); if (ret != HPDF_OK) return ret; } ret = HPDF_Stream_WriteChar (stream, ']'); return ret; }
void* HPDF_Array_GetItem (HPDF_Array array, HPDF_UINT index, HPDF_UINT16 obj_class) { void *obj; HPDF_Obj_Header *header; HPDF_PTRACE((" HPDF_Array_GetItem\n")); obj = HPDF_List_ItemAt (array->list, index); if (!obj) { HPDF_SetError (array->error, HPDF_ARRAY_ITEM_NOT_FOUND, 0); return NULL; } header = (HPDF_Obj_Header *)obj; if (header->obj_class == HPDF_OCLASS_PROXY) { obj = ((HPDF_Proxy)obj)->obj; header = (HPDF_Obj_Header *)obj; } if ((header->obj_class & HPDF_OCLASS_ANY) != obj_class) { HPDF_SetError (array->error, HPDF_ARRAY_ITEM_UNEXPECTED_TYPE, 0); return NULL; } return obj; }
HPDF_FontDef HPDF_Doc_FindFontDef (HPDF_Doc pdf, const char *font_name) { HPDF_List list = pdf->fontdef_list; HPDF_UINT i; HPDF_PTRACE ((" HPDF_Doc_FindFontDef\n")); for (i = 0; i < list->count; i++) { HPDF_FontDef def = (HPDF_FontDef)HPDF_List_ItemAt (list, i); if (HPDF_StrCmp (font_name, def->base_font) == 0) { if (def->type == HPDF_FONTDEF_TYPE_UNINITIALIZED) { if (!def->init_fn || def->init_fn (def) != HPDF_OK) return NULL; } return def; } } return NULL; }
HPDF_XrefEntry HPDF_Xref_GetEntry (HPDF_Xref xref, HPDF_UINT index) { HPDF_PTRACE((" HPDF_Xref_GetEntry\n")); return (HPDF_XrefEntry)HPDF_List_ItemAt(xref->entries, index); }
static void CleanupFontDefList (HPDF_Doc pdf) { HPDF_List list = pdf->fontdef_list; HPDF_UINT i; HPDF_PTRACE ((" CleanupFontDefList\n")); for (i = 0; i < list->count; i++) { HPDF_FontDef def = (HPDF_FontDef)HPDF_List_ItemAt (list, i); HPDF_FontDef_Cleanup (def); } }
static void FreeFontDefList (HPDF_Doc pdf) { HPDF_List list = pdf->fontdef_list; HPDF_UINT i; HPDF_PTRACE ((" HPDF_Doc_FreeFontDefList\n")); for (i = 0; i < list->count; i++) { HPDF_FontDef def = (HPDF_FontDef)HPDF_List_ItemAt (list, i); HPDF_FontDef_Free (def); } HPDF_List_Free (list); pdf->fontdef_list = NULL; }
HPDF_GetPageByIndex (HPDF_Doc pdf, HPDF_UINT index) { HPDF_Page ret; HPDF_PTRACE ((" HPDF_GetPageByIndex\n")); if (!HPDF_HasDoc (pdf)) return NULL; ret = HPDF_List_ItemAt (pdf->page_list, index); if (!ret) { HPDF_RaiseError (&pdf->error, HPDF_INVALID_PAGE_INDEX, 0); return NULL; } return ret; }
static void FreeEncoderList (HPDF_Doc pdf) { HPDF_List list = pdf->encoder_list; HPDF_UINT i; HPDF_PTRACE ((" FreeEncoderList\n")); for (i = 0; i < list->count; i++) { HPDF_Encoder encoder = (HPDF_Encoder)HPDF_List_ItemAt (list, i); HPDF_Encoder_Free (encoder); } HPDF_List_Free (list); pdf->encoder_list = NULL; }
void HPDF_Array_Clear (HPDF_Array array) { HPDF_UINT i; HPDF_PTRACE((" HPDF_Array_Clear\n")); if (!array) return; for (i = 0; i < array->list->count; i++) { void * obj = HPDF_List_ItemAt (array->list, i); if (obj) { HPDF_Obj_Free (array->mmgr, obj); } } HPDF_List_Clear (array->list); }
void HPDF_CIDFontDef_FreeWidth (HPDF_FontDef fontdef) { HPDF_CIDFontDefAttr attr = (HPDF_CIDFontDefAttr)fontdef->attr; HPDF_UINT i; HPDF_PTRACE (" HPDF_FontDef_Validate\n"); for (i = 0; i < attr->widths->count; i++) { HPDF_CID_Width *w = (HPDF_CID_Width *)HPDF_List_ItemAt (attr->widths, i); HPDF_FreeMem (fontdef->mmgr, w); } HPDF_List_Free (attr->widths); attr->widths = NULL; fontdef->valid = HPDF_FALSE; }
HPDF_INT16 HPDF_CIDFontDef_GetCIDWidth (HPDF_FontDef fontdef, HPDF_UINT16 cid) { HPDF_CIDFontDefAttr attr = (HPDF_CIDFontDefAttr)fontdef->attr; HPDF_UINT i; HPDF_PTRACE (" HPDF_CIDFontDef_GetCIDWidth\n"); for (i = 0; i < attr->widths->count; i++) { HPDF_CID_Width *w = (HPDF_CID_Width *)HPDF_List_ItemAt (attr->widths, i); if (w->cid == cid) return w->width; } /* Not found in pdf_cid_width array. */ return attr->DW; }
HPDF_Font HPDF_Doc_FindFont (HPDF_Doc pdf, const char *font_name, const char *encoding_name) { HPDF_UINT i; HPDF_Font font; HPDF_PTRACE ((" HPDF_Doc_FindFont\n")); for (i = 0; i < pdf->font_mgr->count; i++) { HPDF_FontAttr attr; font = (HPDF_Font)HPDF_List_ItemAt (pdf->font_mgr, i); attr = (HPDF_FontAttr) font->attr; if (HPDF_StrCmp (attr->fontdef->base_font, font_name) == 0 && HPDF_StrCmp (attr->encoder->name, encoding_name) == 0) return font; } return NULL; }
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_STATUS HPDF_Array_Insert (HPDF_Array array, void *target, void *obj) { HPDF_Obj_Header *header; HPDF_STATUS ret; HPDF_UINT i; HPDF_PTRACE((" HPDF_Array_Insert\n")); if (!obj) { if (HPDF_Error_GetCode (array->error) == HPDF_OK) return HPDF_SetError (array->error, HPDF_INVALID_OBJECT, 0); else return HPDF_INVALID_OBJECT; } header = (HPDF_Obj_Header *)obj; if (header->obj_id & HPDF_OTYPE_DIRECT) { HPDF_PTRACE((" HPDF_Array_Add this object cannot owned by array " "obj=0x%08X\n", (HPDF_UINT)array)); return HPDF_SetError (array->error, HPDF_INVALID_OBJECT, 0); } if (array->list->count >= HPDF_LIMIT_MAX_ARRAY) { HPDF_PTRACE((" HPDF_Array_Add exceed limitatin of array count(%d)\n", HPDF_LIMIT_MAX_ARRAY)); HPDF_Obj_Free (array->mmgr, obj); return HPDF_SetError (array->error, HPDF_ARRAY_COUNT_ERR, 0); } if (header->obj_id & HPDF_OTYPE_INDIRECT) { HPDF_Proxy proxy = HPDF_Proxy_New (array->mmgr, obj); if (!proxy) { HPDF_Obj_Free (array->mmgr, obj); return HPDF_Error_GetCode (array->error); } proxy->header.obj_id |= HPDF_OTYPE_DIRECT; obj = proxy; } else header->obj_id |= HPDF_OTYPE_DIRECT; /* get the target-object from object-list * consider that the pointer contained in list may be proxy-object. */ for (i = 0; i < array->list->count; i++) { void *ptr = HPDF_List_ItemAt (array->list, i); void *obj_ptr; header = (HPDF_Obj_Header *)obj; if (header->obj_class == HPDF_OCLASS_PROXY) obj_ptr = ((HPDF_Proxy)ptr)->obj; else obj_ptr = ptr; if (obj_ptr == target) { ret = HPDF_List_Insert (array->list, ptr, obj); if (ret != HPDF_OK) HPDF_Obj_Free (array->mmgr, obj); return ret; } } HPDF_Obj_Free (array->mmgr, obj); return HPDF_ITEM_NOT_FOUND; }
HPDF_STATUS HPDF_Xref_WriteToStream (HPDF_Xref xref, HPDF_Stream stream, HPDF_Encrypt e) { HPDF_STATUS ret; HPDF_UINT i; char buf[HPDF_SHORT_BUF_SIZ]; char* pbuf; char* eptr = buf + HPDF_SHORT_BUF_SIZ - 1; HPDF_UINT str_idx; HPDF_Xref tmp_xref = xref; /* write each objects of xref to the specified stream */ HPDF_PTRACE((" HPDF_Xref_WriteToStream\n")); while (tmp_xref) { if (tmp_xref->start_offset == 0) str_idx = 1; else str_idx = 0; for (i = str_idx; i < tmp_xref->entries->count; i++) { HPDF_XrefEntry entry = (HPDF_XrefEntry)HPDF_List_ItemAt (tmp_xref->entries, i); HPDF_UINT obj_id = tmp_xref->start_offset + i; HPDF_UINT16 gen_no = entry->gen_no; entry->byte_offset = stream->size; pbuf = buf; pbuf = HPDF_IToA (pbuf, obj_id, eptr); *pbuf++ = ' '; pbuf = HPDF_IToA (pbuf, gen_no, eptr); HPDF_StrCpy(pbuf, " obj\012", eptr); if ((ret = HPDF_Stream_WriteStr (stream, buf)) != HPDF_OK) return ret; if (e) HPDF_Encrypt_InitKey (e, obj_id, gen_no); if ((ret = HPDF_Obj_WriteValue (entry->obj, stream, e)) != HPDF_OK) return ret; if ((ret = HPDF_Stream_WriteStr (stream, "\012endobj\012")) != HPDF_OK) return ret; } tmp_xref = tmp_xref->prev; } /* start to write cross-reference table */ tmp_xref = xref; while (tmp_xref) { tmp_xref->addr = stream->size; pbuf = buf; pbuf = (char *)HPDF_StrCpy (pbuf, "xref\012", eptr); pbuf = HPDF_IToA (pbuf, tmp_xref->start_offset, eptr); *pbuf++ = ' '; pbuf = HPDF_IToA (pbuf, tmp_xref->entries->count, eptr); HPDF_StrCpy (pbuf, "\012", eptr); ret = HPDF_Stream_WriteStr (stream, buf); if (ret != HPDF_OK) return ret; for (i = 0; i < tmp_xref->entries->count; i++) { HPDF_XrefEntry entry = HPDF_Xref_GetEntry(tmp_xref, i); pbuf = buf; pbuf = HPDF_IToA2 (pbuf, entry->byte_offset, HPDF_BYTE_OFFSET_LEN + 1); *pbuf++ = ' '; pbuf = HPDF_IToA2 (pbuf, entry->gen_no, HPDF_GEN_NO_LEN + 1); *pbuf++ = ' '; *pbuf++ = entry->entry_typ; HPDF_StrCpy (pbuf, "\015\012", eptr); ret = HPDF_Stream_WriteStr (stream, buf); if (ret != HPDF_OK) return ret; } tmp_xref = tmp_xref->prev; } /* write trailer dictionary */ ret = WriteTrailer (xref, stream); return ret; }