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; }
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 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); }
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_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; }