HPDF_Destination HPDF_Destination_New (HPDF_MMgr mmgr, HPDF_Page target, HPDF_Xref xref) { HPDF_Destination dst; HPDF_PTRACE((" HPDF_Destination_New\n")); if (!HPDF_Page_Validate (target)) { HPDF_SetError (mmgr->error, HPDF_INVALID_PAGE, 0); return NULL; } dst = HPDF_Array_New (mmgr); if (!dst) return NULL; dst->header.obj_class |= HPDF_OSUBCLASS_DESTINATION; if (HPDF_Xref_Add (xref, dst) != HPDF_OK) return NULL; /* first item of array must be target page */ if (HPDF_Array_Add (dst, target) != HPDF_OK) return NULL; /* default type is HPDF_FIT */ if (HPDF_Array_AddName (dst, HPDF_DESTINATION_TYPE_NAMES[(HPDF_INT)HPDF_FIT]) != HPDF_OK) return NULL; return dst; }
HPDF_Pages HPDF_Pages_New (HPDF_MMgr mmgr, HPDF_Pages parent, HPDF_Xref xref) { HPDF_STATUS ret = HPDF_OK; HPDF_Pages pages; HPDF_PTRACE((" HPDF_Pages_New\n")); pages = HPDF_Dict_New (mmgr); if (!pages) return NULL; pages->header.obj_class |= HPDF_OSUBCLASS_PAGES; pages->before_write_fn = Pages_BeforeWrite; if (HPDF_Xref_Add (xref, pages) != HPDF_OK) return NULL; /* add requiered elements */ ret += HPDF_Dict_AddName (pages, "Type", "Pages"); ret += HPDF_Dict_Add (pages, "Kids", HPDF_Array_New (pages->mmgr)); ret += HPDF_Dict_Add (pages, "Count", HPDF_Number_New (pages->mmgr, 0)); if (ret == HPDF_OK && parent) ret += HPDF_Pages_AddKids (parent, pages); if (ret != HPDF_OK) return NULL; return pages; }
HPDF_ExData HPDF_3DAnnotExData_New(HPDF_MMgr mmgr, HPDF_Xref xref) { HPDF_ExData exdata; HPDF_STATUS ret = HPDF_OK; HPDF_PTRACE (" HPDF_ExData_New\n"); exdata = HPDF_Dict_New (mmgr); if (!exdata) return NULL; if (HPDF_Xref_Add (xref, exdata) != HPDF_OK) return NULL; ret += HPDF_Dict_AddName (exdata, "Type", "ExData"); ret += HPDF_Dict_AddName (exdata, "Subtype", "3DM"); if (ret != HPDF_OK) return NULL; return exdata; }
HPDF_Catalog HPDF_Catalog_New (HPDF_MMgr mmgr, HPDF_Xref xref) { HPDF_Catalog catalog; HPDF_STATUS ret = 0; catalog = HPDF_Dict_New (mmgr); if (!catalog) return NULL; catalog->header.obj_class |= HPDF_OSUBCLASS_CATALOG; if (HPDF_Xref_Add (xref, catalog) != HPDF_OK) return NULL; /* add requiered elements */ ret += HPDF_Dict_AddName (catalog, "Type", "Catalog"); ret += HPDF_Dict_Add (catalog, "Pages", HPDF_Pages_New (mmgr, NULL, xref)); if (ret != HPDF_OK) return NULL; return catalog; }
HPDF_EncryptDict HPDF_EncryptDict_New (HPDF_MMgr mmgr, HPDF_Xref xref) { HPDF_Encrypt attr; HPDF_EncryptDict dict; HPDF_PTRACE((" HPDF_EncryptDict_New\n")); dict = HPDF_Dict_New (mmgr); if (!dict) return NULL; dict->header.obj_class |= HPDF_OSUBCLASS_ENCRYPT; dict->free_fn = HPDF_EncryptDict_OnFree; attr = HPDF_GetMem (dict->mmgr, sizeof(HPDF_Encrypt_Rec)); if (!attr) { HPDF_Dict_Free (dict); return NULL; } dict->attr = attr; HPDF_Encrypt_Init (attr); if (HPDF_Xref_Add (xref, dict) != HPDF_OK) return NULL; return dict; }
HPDF_NameTree HPDF_NameTree_New (HPDF_MMgr mmgr, HPDF_Xref xref) { HPDF_STATUS ret = HPDF_OK; HPDF_NameTree ntree; HPDF_Array items; HPDF_PTRACE (" HPDF_NameTree_New\n"); ntree = HPDF_Dict_New (mmgr); if (!ntree) return NULL; if (HPDF_Xref_Add (xref, ntree) != HPDF_OK) return NULL; ntree->header.obj_class |= HPDF_OSUBCLASS_NAMETREE; items = HPDF_Array_New (mmgr); if (!ntree) return NULL; ret += HPDF_Dict_Add (ntree, "Names", items); if (ret != HPDF_OK) return NULL; return ntree; }
HPDF_Page HPDF_Page_New (HPDF_MMgr mmgr, HPDF_Xref xref) { HPDF_STATUS ret; HPDF_PageAttr attr; HPDF_Page page; HPDF_PTRACE((" HPDF_Page_New\n")); page = HPDF_Dict_New (mmgr); if (!page) return NULL; page->header.obj_class |= HPDF_OSUBCLASS_PAGE; page->free_fn = Page_OnFree; page->before_write_fn = Page_BeforeWrite; attr = HPDF_GetMem (page->mmgr, sizeof(HPDF_PageAttr_Rec)); if (!attr) { HPDF_Dict_Free (page); return NULL; } page->attr = attr; HPDF_MemSet (attr, 0, sizeof(HPDF_PageAttr_Rec)); attr->gmode = HPDF_GMODE_PAGE_DESCRIPTION; attr->cur_pos = HPDF_ToPoint (0, 0); attr->text_pos = HPDF_ToPoint (0, 0); ret = HPDF_Xref_Add (xref, page); if (ret != HPDF_OK) return NULL; attr->gstate = HPDF_GState_New (page->mmgr, NULL); attr->contents = HPDF_DictStream_New (page->mmgr, xref); if (!attr->gstate || !attr->contents) return NULL; attr->stream = attr->contents->stream; attr->xref = xref; /* add requiered elements */ ret += HPDF_Dict_AddName (page, "Type", "Page"); ret += HPDF_Dict_Add (page, "MediaBox", HPDF_Box_Array_New (page->mmgr, HPDF_ToBox (0, 0, (HPDF_INT16)(HPDF_DEF_PAGE_WIDTH), (HPDF_INT16)(HPDF_DEF_PAGE_HEIGHT)))); ret += HPDF_Dict_Add (page, "Contents", attr->contents); ret += AddResource (page); if (ret != HPDF_OK) return NULL; return page; }
HPDF_Annotation HPDF_Annotation_New (HPDF_MMgr mmgr, HPDF_Xref xref, HPDF_AnnotType type, HPDF_Rect rect) { HPDF_Annotation annot; HPDF_Array array; HPDF_STATUS ret = HPDF_OK; HPDF_REAL tmp; HPDF_PTRACE((" HPDF_Annotation_New\n")); annot = HPDF_Dict_New (mmgr); if (!annot) return NULL; if (HPDF_Xref_Add (xref, annot) != HPDF_OK) return NULL; array = HPDF_Array_New (mmgr); if (!array) return NULL; if (HPDF_Dict_Add (annot, "Rect", array) != HPDF_OK) return NULL; if (rect.top < rect.bottom) { tmp = rect.top; rect.top = rect.bottom; rect.bottom = tmp; } ret += HPDF_Array_AddReal (array, rect.left); ret += HPDF_Array_AddReal (array, rect.bottom); ret += HPDF_Array_AddReal (array, rect.right); ret += HPDF_Array_AddReal (array, rect.top); ret += HPDF_Dict_AddName (annot, "Type", "Annot"); ret += HPDF_Dict_AddName (annot, "Subtype", HPDF_ANNOT_TYPE_NAMES[(HPDF_INT)type]); if (ret != HPDF_OK) return NULL; annot->header.obj_class |= HPDF_OSUBCLASS_ANNOTATION; return annot; }
static HPDF_Dict GetInfo (HPDF_Doc pdf) { if (!HPDF_HasDoc (pdf)) return NULL; if (!pdf->info) { pdf->info = HPDF_Dict_New (pdf->mmgr); if (!pdf->info || HPDF_Xref_Add (pdf->xref, pdf->info) != HPDF_OK) pdf->info = NULL; } return pdf->info; }
HPDF_EmbeddedFile HPDF_EmbeddedFile_New (HPDF_MMgr mmgr, HPDF_Xref xref, const char *file) { HPDF_STATUS ret = HPDF_OK; HPDF_Dict ef; /* the dictionary for the embedded file: /Type /EF */ HPDF_String name; /* the name of the file: /F (name) */ HPDF_Dict eff; /* ef has an /EF <<blah>> key - this is it */ HPDF_Dict filestream; /* the stream that /EF <</F _ _ R>> refers to */ HPDF_Stream stream; ef = HPDF_Dict_New (mmgr); if (!ef) return NULL; if (HPDF_Xref_Add (xref, ef) != HPDF_OK) return NULL; filestream = HPDF_DictStream_New (mmgr, xref); if (!filestream) return NULL; stream = HPDF_FileReader_New (mmgr, file); if (!stream) return NULL; HPDF_Stream_Free(filestream->stream); filestream->stream = stream; filestream->filter = HPDF_STREAM_FILTER_FLATE_DECODE; eff = HPDF_Dict_New (mmgr); if (!eff) return NULL; name = HPDF_String_New (mmgr, file, NULL); if (!name) return NULL; ret += HPDF_Dict_AddName (ef, "Type", "F"); ret += HPDF_Dict_Add (ef, "F", name); ret += HPDF_Dict_Add (ef, "EF", eff); ret += HPDF_Dict_Add (eff, "F", filestream); if (ret != HPDF_OK) return NULL; return ef; }
HPDF_Dict HPDF_3DView_New( HPDF_MMgr mmgr, HPDF_Xref xref, HPDF_U3D u3d, const char *name) { HPDF_STATUS ret = HPDF_OK; HPDF_Dict view; HPDF_PTRACE ((" HPDF_3DView_New\n")); if (name == NULL || name[0] == '\0') { return NULL; } view = HPDF_Dict_New (mmgr); if (!view) { return NULL; } if (HPDF_Xref_Add (xref, view) != HPDF_OK) return NULL; ret = HPDF_Dict_AddName (view, "TYPE", "3DView"); if (ret != HPDF_OK) { HPDF_Dict_Free (view); return NULL; } ret = HPDF_Dict_Add (view, "XN", HPDF_String_New (mmgr, name, NULL)); if (ret != HPDF_OK) { HPDF_Dict_Free (view); return NULL; } ret = HPDF_Dict_Add (view, "IN", HPDF_String_New (mmgr, name, NULL)); if (ret != HPDF_OK) { HPDF_Dict_Free (view); return NULL; } ret = HPDF_U3D_Add3DView( u3d, view); if (ret != HPDF_OK) { HPDF_Dict_Free (view); return NULL; } return view; }
HPDF_STATUS HPDF_PDFA_AppendOutputIntents(HPDF_Doc pdf, const char *iccname, HPDF_Dict iccdict) { HPDF_Array intents; HPDF_Dict intent; HPDF_STATUS ret; if (!HPDF_HasDoc (pdf)) return HPDF_INVALID_DOCUMENT; /* prepare intent */ intent = HPDF_Dict_New (pdf->mmgr); ret = HPDF_Xref_Add (pdf->xref, intent); if ( ret != HPDF_OK) { HPDF_Dict_Free(intent); return ret; } ret += HPDF_Dict_AddName (intent, "Type", "OutputIntent"); ret += HPDF_Dict_AddName (intent, "S", "GTS_PDFA1"); ret += HPDF_Dict_Add (intent, "OutputConditionIdentifier", HPDF_String_New (pdf->mmgr, iccname, NULL)); ret += HPDF_Dict_Add (intent, "OutputCondition", HPDF_String_New (pdf->mmgr, iccname,NULL)); ret += HPDF_Dict_Add (intent, "Info", HPDF_String_New (pdf->mmgr, iccname, NULL)); ret += HPDF_Dict_Add (intent, "DestOutputProfile ", iccdict); if ( ret != HPDF_OK) { HPDF_Dict_Free(intent); return ret; } /* Copied from HPDF_AddIntent - not public function */ intents = HPDF_Dict_GetItem (pdf->catalog, "OutputIntents", HPDF_OCLASS_ARRAY); if (intents == NULL) { intents = HPDF_Array_New (pdf->mmgr); if (intents) { HPDF_STATUS ret = HPDF_Dict_Add (pdf->catalog, "OutputIntents", intents); if (ret != HPDF_OK) { HPDF_CheckError (&pdf->error); return HPDF_Error_GetDetailCode (&pdf->error); } } } HPDF_Array_Add(intents,intent); return HPDF_Error_GetDetailCode (&pdf->error); }
HPDF_NameDict HPDF_NameDict_New (HPDF_MMgr mmgr, HPDF_Xref xref) { HPDF_NameDict ndict; HPDF_PTRACE (" HPDF_NameDict_New\n"); ndict = HPDF_Dict_New (mmgr); if (!ndict) return NULL; if (HPDF_Xref_Add (xref, ndict) != HPDF_OK) return NULL; ndict->header.obj_class |= HPDF_OSUBCLASS_NAMEDICT; return ndict; }
HPDF_Dict HPDF_ExtGState_New (HPDF_MMgr mmgr, HPDF_Xref xref) { HPDF_Dict obj = HPDF_Dict_New (mmgr); HPDF_PTRACE ((" HPDF_ExtGState_New\n")); if (!obj) return NULL; if (HPDF_Xref_Add (xref, obj) != HPDF_OK) return NULL; if (HPDF_Dict_AddName (obj, "Type", "ExtGState") != HPDF_OK) return NULL; obj->header.obj_class |= HPDF_OSUBCLASS_EXT_GSTATE; return obj; }
HPDF_STATUS HPDF_Doc_SetEncryptOn (HPDF_Doc pdf) { HPDF_PTRACE ((" HPDF_Doc_SetEncryptOn\n")); if (pdf->encrypt_on) return HPDF_OK; if (!pdf->encrypt_dict) return HPDF_SetError (&pdf->error, HPDF_DOC_ENCRYPTDICT_NOT_FOUND, 0); if (pdf->encrypt_dict->header.obj_id == HPDF_OTYPE_NONE) if (HPDF_Xref_Add (pdf->xref, pdf->encrypt_dict) != HPDF_OK) return pdf->error.error_no; if (HPDF_Dict_Add (pdf->trailer, "Encrypt", pdf->encrypt_dict) != HPDF_OK) return pdf->error.error_no; pdf->encrypt_on = HPDF_TRUE; 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_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_Font HPDF_Type0Font_New (HPDF_MMgr mmgr, HPDF_FontDef fontdef, HPDF_Encoder encoder, HPDF_Xref xref) { HPDF_Dict font; HPDF_FontAttr attr; HPDF_CMapEncoderAttr encoder_attr; HPDF_STATUS ret = 0; HPDF_Array descendant_fonts; HPDF_PTRACE ((" HPDF_Type0Font_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_DOUBLE_BYTE) { HPDF_SetError(font->error, HPDF_INVALID_ENCODER_TYPE, 0); return NULL; } if (fontdef->type != HPDF_FONTDEF_TYPE_CID && fontdef->type != HPDF_FONTDEF_TYPE_TRUETYPE) { HPDF_SetError(font->error, HPDF_INVALID_FONTDEF_TYPE, 0); return NULL; } 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 = NULL; font->free_fn = OnFree_Func; font->attr = attr; encoder_attr = (HPDF_CMapEncoderAttr)encoder->attr; HPDF_MemSet (attr, 0, sizeof(HPDF_FontAttr_Rec)); attr->writing_mode = encoder_attr->writing_mode; attr->text_width_fn = TextWidth; attr->measure_text_fn = MeasureText; attr->fontdef = fontdef; attr->encoder = encoder; attr->xref = xref; if (HPDF_Xref_Add (xref, font) != HPDF_OK) return NULL; ret += HPDF_Dict_AddName (font, "Type", "Font"); ret += HPDF_Dict_AddName (font, "BaseFont", fontdef->base_font); ret += HPDF_Dict_AddName (font, "Subtype", "Type0"); if (fontdef->type == HPDF_FONTDEF_TYPE_CID) { ret += HPDF_Dict_AddName (font, "Encoding", encoder->name); } else { attr->cmap_stream = CreateCMap (encoder, xref); if (attr->cmap_stream) { ret += HPDF_Dict_Add (font, "Encoding", attr->cmap_stream); } else return NULL; } if (ret != HPDF_OK) return NULL; descendant_fonts = HPDF_Array_New (mmgr); if (!descendant_fonts) return NULL; if (HPDF_Dict_Add (font, "DescendantFonts", descendant_fonts) != HPDF_OK) return NULL; if (fontdef->type == HPDF_FONTDEF_TYPE_CID) { attr->descendant_font = CIDFontType0_New (font, xref); attr->type = HPDF_FONT_TYPE0_CID; } else { attr->descendant_font = CIDFontType2_New (font, xref); attr->type = HPDF_FONT_TYPE0_TT; } if (!attr->descendant_font) return NULL; else if (HPDF_Array_Add (descendant_fonts, attr->descendant_font) != HPDF_OK) return NULL; return font; }
HPDF_Font HPDF_Type3RasterFont_New(HPDF_MMgr mmgr, HPDF_FontDef fontdef, HPDF_Encoder encoder, HPDF_Xref xref) { HPDF_Dict font; HPDF_FontAttr attr; HPDF_Type3RasterFontDefAttr fontdef_attr; HPDF_BasicEncoderAttr encoder_attr; HPDF_STATUS ret = 0; HPDF_UINT i; HPDF_PTRACE((" HPDF_Type3RasterFont_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_TYPE3RASTER) { 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 = Type3RasterFont_OnWrite; font->free_fn = Type3RasterFont_OnFree; HPDF_MemSet(attr, 0, sizeof(HPDF_FontAttr_Rec)); font->attr = attr; attr->type = HPDF_FONT_TYPE3; attr->writing_mode = HPDF_WMODE_HORIZONTAL; attr->text_width_fn = Type3RasterFont_TextWidth; attr->measure_text_fn = Type3RasterFont_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 = 0 /*encoder_attr->first_char*/; i <= 255 /*encoder_attr->last_char*/; i++) { HPDF_UINT16 w = HPDF_Type3RasterFontDef_GetWidth(fontdef, i); attr->widths[i] = w; } fontdef_attr = (HPDF_Type3RasterFontDefAttr)fontdef->attr; ret += HPDF_Dict_AddName(font, "Name", fontdef->base_font); ret += HPDF_Dict_AddName(font, "Type", "Font"); ret += HPDF_Dict_AddName(font, "Subtype", "Type3"); HPDF_REAL scale_x = 72.0f / (HPDF_REAL)fontdef_attr->dpi_x; HPDF_REAL scale_y = 72.0f / (HPDF_REAL)fontdef_attr->dpi_y; HPDF_TransMatrix font_matrix; font_matrix.a = scale_x; font_matrix.b = 0; font_matrix.c = 0; font_matrix.d = scale_y; font_matrix.x = 0; font_matrix.y = 0; HPDF_Array array = HPDF_Array_New(mmgr); ret += HPDF_Array_AddReal(array, font_matrix.a); ret += HPDF_Array_AddReal(array, font_matrix.b); ret += HPDF_Array_AddReal(array, font_matrix.c); ret += HPDF_Array_AddReal(array, font_matrix.d); ret += HPDF_Array_AddReal(array, font_matrix.x); ret += HPDF_Array_AddReal(array, font_matrix.y); ret += HPDF_Dict_Add(font, "FontMatrix", array); HPDF_Array diff_array = HPDF_Array_New(mmgr); ret += HPDF_Array_AddNumber(diff_array, 0); for (i = 0; i < 256; ++i) { char name[3]; sprintf(name, "%02X", i); ret += HPDF_Array_AddName(diff_array, name); } HPDF_Dict encoding = HPDF_Dict_New(mmgr); ret += HPDF_Dict_Add(encoding, "Differences", diff_array); ret += HPDF_Dict_Add(font, "Encoding", encoding); HPDF_Dict char_procs = HPDF_Dict_New(mmgr); for (i = 0; i < 256; ++i) { char name[3]; sprintf(name, "%02X", i); HPDF_Dict char_stream_dict = HPDF_DictStream_New(mmgr, xref); HPDF_Stream_WriteReal(char_stream_dict->stream, fontdef_attr->chars[i].width / scale_x); HPDF_Stream_WriteStr(char_stream_dict->stream, " 0 "); HPDF_Stream_WriteReal(char_stream_dict->stream, fontdef_attr->chars[i].left / scale_x); HPDF_Stream_WriteStr(char_stream_dict->stream, " "); HPDF_Stream_WriteReal(char_stream_dict->stream, fontdef_attr->chars[i].bottom / scale_y); HPDF_Stream_WriteStr(char_stream_dict->stream, " "); HPDF_Stream_WriteReal(char_stream_dict->stream, fontdef_attr->chars[i].right / scale_x); HPDF_Stream_WriteStr(char_stream_dict->stream, " "); HPDF_Stream_WriteReal(char_stream_dict->stream, fontdef_attr->chars[i].top / scale_y); HPDF_Stream_WriteStr(char_stream_dict->stream, " d1\012"); HPDF_Stream_WriteStr(char_stream_dict->stream, "q\012"); HPDF_Stream_WriteReal(char_stream_dict->stream, fontdef_attr->chars[i].pixels_x); HPDF_Stream_WriteStr(char_stream_dict->stream, " 0 0 "); HPDF_Stream_WriteReal(char_stream_dict->stream, fontdef_attr->chars[i].pixels_y); HPDF_Stream_WriteStr(char_stream_dict->stream, " "); HPDF_Stream_WriteReal(char_stream_dict->stream, fontdef_attr->chars[i].left / scale_x); HPDF_Stream_WriteStr(char_stream_dict->stream, " "); HPDF_Stream_WriteReal(char_stream_dict->stream, fontdef_attr->chars[i].bottom / scale_y); HPDF_Stream_WriteStr(char_stream_dict->stream, " cm\012"); HPDF_Stream_WriteStr(char_stream_dict->stream, "BI\012"); HPDF_Stream_WriteStr(char_stream_dict->stream, "/IM true\012"); HPDF_Stream_WriteStr(char_stream_dict->stream, "/W "); HPDF_Stream_WriteInt(char_stream_dict->stream, fontdef_attr->chars[i].pixels_x); HPDF_Stream_WriteStr(char_stream_dict->stream, " /H "); HPDF_Stream_WriteInt(char_stream_dict->stream, fontdef_attr->chars[i].pixels_y); HPDF_Stream_WriteStr(char_stream_dict->stream, "\012/BPC 1\012"); HPDF_Stream_WriteStr(char_stream_dict->stream, "/D [1 0]\012"); HPDF_Stream_WriteStr(char_stream_dict->stream, "ID\012"); if (fontdef_attr->chars[i].raster_data != NULL) HPDF_Stream_Write(char_stream_dict->stream, fontdef_attr->chars[i].raster_data, fontdef_attr->chars[i].raster_data_size); HPDF_Stream_WriteStr(char_stream_dict->stream, "\012EI\012"); HPDF_Stream_WriteStr(char_stream_dict->stream, "Q"); ret += HPDF_Dict_Add(char_procs, name, char_stream_dict); if (fontdef_attr->chars[i].left < fontdef->font_bbox.left) fontdef->font_bbox.left = fontdef_attr->chars[i].left; if (fontdef_attr->chars[i].bottom < fontdef->font_bbox.bottom) fontdef->font_bbox.bottom = fontdef_attr->chars[i].bottom; if (fontdef_attr->chars[i].right > fontdef->font_bbox.right) fontdef->font_bbox.right = fontdef_attr->chars[i].right; if (fontdef_attr->chars[i].top > fontdef->font_bbox.top) fontdef->font_bbox.top = fontdef_attr->chars[i].top; } fontdef->font_bbox.left /= scale_x; fontdef->font_bbox.bottom /= scale_y; fontdef->font_bbox.right /= scale_x; fontdef->font_bbox.top /= scale_y; ret += HPDF_Dict_Add(font, "CharProcs", char_procs); array = HPDF_Box_Array_New(mmgr, fontdef->font_bbox); ret += HPDF_Dict_Add(font, "FontBBox", array); 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 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_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 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_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; }
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_Type0Font_New (HPDF_MMgr mmgr, HPDF_FontDef fontdef, HPDF_Encoder encoder, HPDF_Xref xref) { HPDF_Dict font; HPDF_FontAttr attr; HPDF_CMapEncoderAttr encoder_attr; HPDF_STATUS ret = 0; HPDF_Array descendant_fonts; HPDF_PTRACE ((" HPDF_Type0Font_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_DOUBLE_BYTE) { HPDF_SetError(font->error, HPDF_INVALID_ENCODER_TYPE, 0); return NULL; } if (fontdef->type != HPDF_FONTDEF_TYPE_CID && fontdef->type != HPDF_FONTDEF_TYPE_TRUETYPE) { HPDF_SetError(font->error, HPDF_INVALID_FONTDEF_TYPE, 0); return NULL; } 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 = NULL; font->free_fn = OnFree_Func; font->attr = attr; encoder_attr = (HPDF_CMapEncoderAttr)encoder->attr; HPDF_MemSet (attr, 0, sizeof(HPDF_FontAttr_Rec)); attr->writing_mode = encoder_attr->writing_mode; attr->text_width_fn = TextWidth; attr->measure_text_fn = MeasureText; attr->fontdef = fontdef; attr->encoder = encoder; attr->xref = xref; if (HPDF_Xref_Add (xref, font) != HPDF_OK) return NULL; ret += HPDF_Dict_AddName (font, "Type", "Font"); ret += HPDF_Dict_AddName (font, "BaseFont", fontdef->base_font); ret += HPDF_Dict_AddName (font, "Subtype", "Type0"); if (fontdef->type == HPDF_FONTDEF_TYPE_CID) { ret += HPDF_Dict_AddName (font, "Encoding", encoder->name); } else { /* * Handle the Unicode encoding, see hpdf_encoding_utf.c For some * reason, xpdf-based readers cannot deal with our cmap but work * fine when using the predefined "Identity-H" * encoding. However, text selection does not work, unless we * add a ToUnicode cmap. This CMap should also be "Identity", * but that does not work -- specifying our cmap as a stream however * does work. Who can understand that ? */ if (HPDF_StrCmp(encoder_attr->ordering, "Identity-H") == 0) { ret += HPDF_Dict_AddName (font, "Encoding", "Identity-H"); attr->cmap_stream = CreateCMap (encoder, xref); if (attr->cmap_stream) { ret += HPDF_Dict_Add (font, "ToUnicode", attr->cmap_stream); } else return NULL; } else { attr->cmap_stream = CreateCMap (encoder, xref); if (attr->cmap_stream) { ret += HPDF_Dict_Add (font, "Encoding", attr->cmap_stream); } else return NULL; } } if (ret != HPDF_OK) return NULL; descendant_fonts = HPDF_Array_New (mmgr); if (!descendant_fonts) return NULL; if (HPDF_Dict_Add (font, "DescendantFonts", descendant_fonts) != HPDF_OK) return NULL; if (fontdef->type == HPDF_FONTDEF_TYPE_CID) { attr->descendant_font = CIDFontType0_New (font, xref); attr->type = HPDF_FONT_TYPE0_CID; } else { attr->descendant_font = CIDFontType2_New (font, xref); attr->type = HPDF_FONT_TYPE0_TT; } if (!attr->descendant_font) return NULL; else if (HPDF_Array_Add (descendant_fonts, attr->descendant_font) != HPDF_OK) return NULL; return font; }
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; }