static HPDF_STATUS Resize (HPDF_List list, HPDF_UINT count) { void **new_obj; HPDF_PTRACE((" HPDF_List_Resize\n")); if (list->count >= count) { if (list->count == count) return HPDF_OK; else return HPDF_INVALID_PARAMETER; } new_obj = (void **)HPDF_GetMem (list->mmgr, count * sizeof(void *)); if (!new_obj) return HPDF_Error_GetCode (list->error); if (list->obj) HPDF_MemCpy ((HPDF_BYTE *)new_obj, (HPDF_BYTE *)list->obj, list->block_siz * sizeof(void *)); list->block_siz = count; if (list->obj) HPDF_FreeMem (list->mmgr, list->obj); list->obj = new_obj; return HPDF_OK; }
void HPDF_UInt16Swap (HPDF_UINT16 *value) { HPDF_BYTE u[2]; HPDF_MemCpy (u, (HPDF_BYTE*)value, 2); *value = (HPDF_UINT16)((HPDF_UINT16)u[0] << 8 | (HPDF_UINT16)u[1]); }
static void UINT32Swap (HPDF_UINT32 *value) { HPDF_BYTE b[4]; HPDF_MemCpy (b, (HPDF_BYTE *)value, 4); *value = (HPDF_UINT32)((HPDF_UINT32)b[0] << 24 | (HPDF_UINT32)b[1] << 16 | (HPDF_UINT32)b[2] << 8 | (HPDF_UINT32)b[3]); }
void HPDF_UInt32Swap (HPDF_UINT32 *value) { HPDF_BYTE u[4]; HPDF_MemCpy (u, (HPDF_BYTE*)value, 4); *value = (HPDF_UINT32)((HPDF_UINT32)u[0] << 24 | (HPDF_UINT32)u[1] << 16 | (HPDF_UINT32)u[2] << 8 | (HPDF_UINT32)u[3]); }
static const char* LoadTTFontFromStream2 (HPDF_Doc pdf, HPDF_Stream font_data, HPDF_UINT index, HPDF_BOOL embedding, const char *file_name) { HPDF_FontDef def; HPDF_PTRACE ((" HPDF_LoadTTFontFromStream2\n")); def = HPDF_TTFontDef_Load2 (pdf->mmgr, font_data, index, embedding); if (def) { HPDF_FontDef tmpdef = HPDF_Doc_FindFontDef (pdf, def->base_font); if (tmpdef) { HPDF_FontDef_Free (def); HPDF_SetError (&pdf->error, HPDF_FONT_EXISTS, 0); return NULL; } if (HPDF_List_Add (pdf->fontdef_list, def) != HPDF_OK) { HPDF_FontDef_Free (def); return NULL; } } else return NULL; if (embedding) { if (pdf->ttfont_tag[0] == 0) { HPDF_MemCpy (pdf->ttfont_tag, (HPDF_BYTE *)"HPDFAA", 6); } else { HPDF_INT i; for (i = 5; i >= 0; i--) { pdf->ttfont_tag[i] += 1; if (pdf->ttfont_tag[i] > 'Z') pdf->ttfont_tag[i] = 'A'; else break; } } HPDF_TTFontDef_SetTagName (def, (char *)pdf->ttfont_tag); } return def->base_font; }
HPDF_STATUS HPDF_Binary_SetValue (HPDF_Binary obj, HPDF_BYTE *value, HPDF_UINT len) { if (len > HPDF_LIMIT_MAX_STRING_LEN) return HPDF_SetError (obj->error, HPDF_BINARY_LENGTH_ERR, 0); if (obj->value) { HPDF_FreeMem (obj->mmgr, obj->value); obj->len = 0; } obj->value = HPDF_GetMem (obj->mmgr, len); if (!obj->value) return HPDF_Error_GetCode (obj->error); HPDF_MemCpy (obj->value, value, len); obj->len = len; return HPDF_OK; }
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; }
HPDF_STATUS HPDF_String_Write (HPDF_String obj, HPDF_Stream stream, HPDF_Encrypt e) { HPDF_STATUS ret; /* * When encoder is not NULL, text is changed to unicode using encoder, * and it outputs by HPDF_write_binary method. */ HPDF_PTRACE((" HPDF_String_Write\n")); if (e) HPDF_Encrypt_Reset (e); if (obj->encoder == NULL) { if (e) { if ((ret = HPDF_Stream_WriteChar (stream, '<')) != HPDF_OK) return ret; if ((ret = HPDF_Stream_WriteBinary (stream, obj->value, HPDF_StrLen ((char *)obj->value, -1), e)) != HPDF_OK) return ret; return HPDF_Stream_WriteChar (stream, '>'); } else { return HPDF_Stream_WriteEscapeText (stream, (char *)obj->value); } } else { HPDF_BYTE* src = obj->value; HPDF_BYTE buf[HPDF_TEXT_DEFAULT_LEN * 2]; HPDF_UINT tmp_len = 0; HPDF_BYTE* pbuf = buf; HPDF_INT32 len = obj->len; HPDF_ParseText_Rec parse_state; HPDF_UINT i; if ((ret = HPDF_Stream_WriteChar (stream, '<')) != HPDF_OK) return ret; if ((ret = HPDF_Stream_WriteBinary (stream, UNICODE_HEADER, 2, e)) != HPDF_OK) return ret; HPDF_Encoder_SetParseText (obj->encoder, &parse_state, src, len); for (i = 0; i < len; i++) { HPDF_BYTE b = src[i]; HPDF_UNICODE tmp_unicode; HPDF_ByteType btype = HPDF_Encoder_ByteType (obj->encoder, &parse_state); if (tmp_len >= HPDF_TEXT_DEFAULT_LEN - 1) { if ((ret = HPDF_Stream_WriteBinary (stream, buf, tmp_len * 2, e)) != HPDF_OK) return ret; tmp_len = 0; pbuf = buf; } if (btype != HPDF_BYTE_TYPE_TRIAL) { if (btype == HPDF_BYTE_TYPE_LEAD) { HPDF_BYTE b2 = src[i + 1]; HPDF_UINT16 char_code = (HPDF_UINT) b * 256 + b2; tmp_unicode = HPDF_Encoder_ToUnicode (obj->encoder, char_code); } else { tmp_unicode = HPDF_Encoder_ToUnicode (obj->encoder, b); } HPDF_UInt16Swap (&tmp_unicode); HPDF_MemCpy (pbuf, (HPDF_BYTE*)&tmp_unicode, 2); pbuf += 2; tmp_len++; } } if (tmp_len > 0) { if ((ret = HPDF_Stream_WriteBinary (stream, buf, tmp_len * 2, e)) != HPDF_OK) return ret; } if ((ret = HPDF_Stream_WriteChar (stream, '>')) != HPDF_OK) return ret; } return HPDF_OK; }
HPDF_STATUS HPDF_Info_SetInfoDateAttr (HPDF_Dict info, HPDF_InfoType type, HPDF_Date value) { char tmp[HPDF_DATE_TIME_STR_LEN + 1]; char* ptmp; const char* name = InfoTypeToName (type); HPDF_PTRACE((" HPDF_Info_SetInfoDateAttr\n")); if (type > HPDF_INFO_MOD_DATE) return HPDF_SetError (info->error, HPDF_INVALID_PARAMETER, 0); HPDF_MemSet (tmp, 0, HPDF_DATE_TIME_STR_LEN + 1); if (value.month < 1 || 12 < value.month || value.day < 1 || 23 < value.hour || 59 < value.minutes || 59 < value.seconds || (value.ind != '+' && value.ind != '-' && value.ind != 'Z' && value.ind != ' ') || 23 < value.off_hour || 59 < value.off_minutes) { return HPDF_SetError (info->error, HPDF_INVALID_DATE_TIME, 0); } switch (value.month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: if (value.day > 31) return HPDF_SetError (info->error, HPDF_INVALID_DATE_TIME, 0); break; case 4: case 6: case 9: case 11: if (value.day > 30) return HPDF_SetError (info->error, HPDF_INVALID_DATE_TIME, 0); break; case 2: if (value.day > 29 || (value.day == 29 && (value.year % 4 != 0 || (value.year % 100 == 0 && value.year % 400 != 0)))) return HPDF_SetError (info->error, HPDF_INVALID_DATE_TIME, 0); break; default: return HPDF_SetError (info->error, HPDF_INVALID_DATE_TIME, 0); } ptmp = (char *)HPDF_MemCpy ((HPDF_BYTE *)tmp, (HPDF_BYTE *)"D:", 2); ptmp = HPDF_IToA2 (ptmp, value.year, 5); ptmp = HPDF_IToA2 (ptmp, value.month, 3); ptmp = HPDF_IToA2 (ptmp, value.day, 3); ptmp = HPDF_IToA2 (ptmp, value.hour, 3); ptmp = HPDF_IToA2 (ptmp, value.minutes, 3); ptmp = HPDF_IToA2 (ptmp, value.seconds, 3); if (value.ind != ' ') { *ptmp++ = value.ind; ptmp = HPDF_IToA2 (ptmp, value.off_hour, 3); *ptmp++ = '\''; ptmp = HPDF_IToA2 (ptmp, value.off_minutes, 3); *ptmp++ = '\''; } *ptmp = 0; return HPDF_Dict_Add (info, name, HPDF_String_New (info->mmgr, tmp, NULL)); }
static HPDF_STATUS LoadFontData (HPDF_FontDef fontdef, HPDF_Stream stream) { HPDF_Type1FontDefAttr attr = (HPDF_Type1FontDefAttr)fontdef->attr; char buf[HPDF_STREAM_BUF_SIZ]; char* pbuf = buf; HPDF_UINT len = 0; HPDF_STATUS ret; HPDF_BOOL end_flg = HPDF_FALSE; HPDF_PTRACE (" LoadFontData\n"); attr->font_data = HPDF_MemStream_New (fontdef->mmgr, HPDF_STREAM_BUF_SIZ); if (!attr->font_data) return HPDF_Error_GetCode (fontdef->error); len = 11; ret = HPDF_Stream_Read (stream, (HPDF_BYTE *)pbuf, &len); if (ret != HPDF_OK) return ret; pbuf += 11; for (;;) { len = HPDF_STREAM_BUF_SIZ - 11; ret = HPDF_Stream_Read (stream, (HPDF_BYTE *)pbuf, &len); if (ret == HPDF_STREAM_EOF) { end_flg = HPDF_TRUE; } else if (ret != HPDF_OK) return ret; if (len > 0) { if (attr->length1 == 0) { const char *s1 = HPDF_StrStr (buf, "eexec", len + 11); /* length1 indicate the size of ascii-data of font-file. */ if (s1) attr->length1 = attr->font_data->size + (s1 - buf) + 6; } if (attr->length1 > 0 && attr->length2 == 0) { const char *s2 = HPDF_StrStr (buf, "cleartomark", len + 11); if (s2) attr->length2 = attr->font_data->size + - 520 - attr->length1 + (s2 - buf); /* length1 indicate the size of binary-data. * in most fonts, it is all right at 520 bytes . but it need * to modify because it does not fully satisfy the * specification of type-1 font. */ } } if (end_flg) { if ((ret = HPDF_Stream_Write (attr->font_data, (HPDF_BYTE *)buf, len + 11)) != HPDF_OK) return ret; break; } else { if ((ret = HPDF_Stream_Write (attr->font_data, (HPDF_BYTE *)buf, len)) != HPDF_OK) return ret; HPDF_MemCpy ((HPDF_BYTE *)buf, (HPDF_BYTE *)buf + len, 11); pbuf = buf + 11; } } if (attr->length1 == 0 || attr->length2 == 0) return HPDF_SetError (fontdef->error, HPDF_UNSUPPORTED_TYPE1_FONT, 0); attr->length3 = attr->font_data->size - attr->length1 - attr->length2; return HPDF_OK; }
static HPDF_STATUS CIDFontType2_BeforeWrite_Func (HPDF_Dict obj) { HPDF_FontAttr font_attr = (HPDF_FontAttr)obj->attr; HPDF_FontDef def = font_attr->fontdef; HPDF_TTFontDefAttr def_attr = (HPDF_TTFontDefAttr)def->attr; HPDF_STATUS ret = 0; HPDF_Font font; HPDF_Encoder encoder = font_attr->encoder; HPDF_CMapEncoderAttr encoder_attr = (HPDF_CMapEncoderAttr)encoder->attr; HPDF_Array array; HPDF_UINT i; HPDF_UNICODE tmp_map[65536]; HPDF_UINT16 max = 0; HPDF_PTRACE ((" CIDFontType2_BeforeWrite_Func\n")); font = font_attr->descendant_font; HPDF_MemSet (tmp_map, 0, sizeof(HPDF_UNICODE) * 65536); if (ret != HPDF_OK) return ret; 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 (def, unicode); tmp_map[cid] = gid; if (max < cid) max = cid; } } else { HPDF_UNICODE unicode = (i << 8) | j; HPDF_UINT16 gid = HPDF_TTFontDef_GetGlyphid (def, unicode); tmp_map[unicode] = gid; if (max < unicode) max = unicode; } } } if (def_attr->is_cidfont) { max = def_attr->num_glyphs; } if (max > 0) { HPDF_INT16 dw = def->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 HPDF_FAILD_TO_ALLOC_MEM; if (HPDF_Dict_Add (font, "W", array) != HPDF_OK) return HPDF_FAILD_TO_ALLOC_MEM; for (i = 0; i < max; i++) { HPDF_INT w; HPDF_UINT16 gid; if (def_attr->is_cidfont) { gid = i; } else { gid = *ptmp_map; ptmp_map++; } w = HPDF_TTFontDef_GetGidWidth (def, gid); if (def_attr->glyph_tbl.flgs[gid] && w != dw) { if (!tmp_array) { if (HPDF_Array_AddNumber (array, i) != HPDF_OK) return HPDF_FAILD_TO_ALLOC_MEM; tmp_array = HPDF_Array_New (font->mmgr); if (!tmp_array) return HPDF_FAILD_TO_ALLOC_MEM; if (HPDF_Array_Add (array, tmp_array) != HPDF_OK) return HPDF_FAILD_TO_ALLOC_MEM; } if ((ret = HPDF_Array_AddNumber (tmp_array, w)) != HPDF_OK) return HPDF_FAILD_TO_ALLOC_MEM; } else tmp_array = NULL; } /* create "CIDToGIDMap" data */ if (def_attr->embedding) { if (def_attr->is_cidfont) { ret += HPDF_Dict_AddName (font, "CIDToGIDMap", "Identity"); } else { font_attr->map_stream = HPDF_DictStream_New (font->mmgr, font_attr->xref); if (!font_attr->map_stream) return HPDF_FAILD_TO_ALLOC_MEM; if (HPDF_Dict_Add (font, "CIDToGIDMap", font_attr->map_stream) != HPDF_OK) return HPDF_FAILD_TO_ALLOC_MEM; 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 (font_attr->map_stream->stream, (HPDF_BYTE *)tmp_map, max * 2)) != HPDF_OK) return HPDF_FAILD_TO_ALLOC_MEM; } } } else {