HPDF_LineAnnot_SetCaption (HPDF_Annotation annot, HPDF_BOOL showCaption, HPDF_LineAnnotCapPosition position, HPDF_INT horzOffset, HPDF_INT vertOffset)
{
    HPDF_STATUS ret = HPDF_OK;
    HPDF_Array capOffset;
    HPDF_PTRACE((" HPDF_LineAnnot_SetCaption\n"));

    ret += HPDF_Dict_AddBoolean ( annot, "Cap", showCaption);
    ret += HPDF_Dict_AddName( annot, "CP", HPDF_LINE_ANNOT_CAP_POSITION_NAMES[(HPDF_INT)position]);

    if (ret != HPDF_OK)
       return HPDF_Error_GetCode ( annot->error);

    capOffset = HPDF_Array_New ( annot->mmgr);
    if ( !capOffset)
        return HPDF_Error_GetCode ( annot->error);

    if ((ret = HPDF_Dict_Add ( annot, "CO", capOffset)) != HPDF_OK)
        return ret;

    ret += HPDF_Array_AddNumber (capOffset, horzOffset);
    ret += HPDF_Array_AddNumber (capOffset, vertOffset);

    if (ret != HPDF_OK)
       return HPDF_Error_GetCode (capOffset->error);

    return HPDF_OK;
}
HPDF_LinkAnnot_SetBorderStyle  (HPDF_Annotation  annot,
                                HPDF_REAL        width,
                                HPDF_UINT16      dash_on,
                                HPDF_UINT16      dash_off)
{
    HPDF_Array array;
    HPDF_STATUS ret;

    HPDF_PTRACE((" HPDF_LinkAnnot_SetBorderStyle\n"));

    if (!CheckSubType (annot, HPDF_ANNOT_LINK))
        return HPDF_INVALID_ANNOTATION;

    if (width < 0)
        return HPDF_RaiseError (annot->error, HPDF_INVALID_PARAMETER, 0);

    array = HPDF_Array_New (annot->mmgr);
    if (!array)
        return HPDF_CheckError (annot->error);

    if ((ret = HPDF_Dict_Add (annot, "Border", array)) != HPDF_OK)
        return HPDF_CheckError (annot->error);

    ret += HPDF_Array_AddNumber (array, 0);
    ret += HPDF_Array_AddNumber (array, 0);
    ret += HPDF_Array_AddReal (array, width);

    if (ret != HPDF_OK)
        return HPDF_CheckError (annot->error);

    if (dash_on && dash_off) {
        HPDF_Array dash = HPDF_Array_New (annot->mmgr);
        if (!dash)
            return HPDF_CheckError (annot->error);

        if ((ret = HPDF_Array_Add (array, dash)) != HPDF_OK)
            return HPDF_CheckError (annot->error);

        ret += HPDF_Array_AddNumber (dash, dash_on);
        ret += HPDF_Array_AddNumber (dash, dash_off);

        if (ret != HPDF_OK)
           return HPDF_CheckError (annot->error);
    }

    return HPDF_OK;
}
static HPDF_STATUS
CreatePallet (HPDF_Dict    image,
              png_structp  png_ptr,
              png_infop    info_ptr)
{
    HPDF_INT num_pl = 0;
    png_color *src_pl = NULL;
    HPDF_BYTE *ppallet;
    HPDF_BYTE *p;
    HPDF_UINT i;
    HPDF_Array array;

    /* png_get_PLTE does not call PngErrorFunc even if it failed.
     * so we call HPDF_Set_Error to set error-code.
     */
    if (png_get_PLTE(png_ptr, info_ptr, (png_color**)&src_pl, &num_pl) !=
            PNG_INFO_PLTE)
        return HPDF_SetError (image->error, HPDF_LIBPNG_ERROR,
                    HPDF_CANNOT_GET_PALLET);


    /* make a pallet array for indexed image. */
    ppallet = HPDF_GetMem (image->mmgr, num_pl * 3);
    if (!ppallet)
        return image->error->error_no;

    p = ppallet;
    for (i = 0; i < num_pl; i++, src_pl++) {
        *p++ = src_pl->red;
        *p++ = src_pl->green;
        *p++ = src_pl->blue;
    }

    array = HPDF_Array_New (image->mmgr);
    if (array) {
        HPDF_Binary b;

        HPDF_Dict_Add (image, "ColorSpace", array);

        HPDF_Array_AddName (array, "Indexed");
        HPDF_Array_AddName (array, "DeviceRGB");
        HPDF_Array_AddNumber (array, num_pl - 1);

        b = HPDF_Binary_New (image->mmgr, ppallet, num_pl * 3);
        if (b)
            HPDF_Array_Add (array, b);
    }

    HPDF_FreeMem (image->mmgr, ppallet);

    return image->error->error_no;
}
Exemple #4
0
HPDF_Image_SetColorMask (HPDF_Image   image,
                         HPDF_UINT    rmin,
                         HPDF_UINT    rmax,
                         HPDF_UINT    gmin,
                         HPDF_UINT    gmax,
                         HPDF_UINT    bmin,
                         HPDF_UINT    bmax)
{
    HPDF_Array array;
    const char *name;
    HPDF_STATUS ret = HPDF_OK;

    if (!HPDF_Image_Validate (image))
        return HPDF_INVALID_IMAGE;

    if (HPDF_Dict_GetItem (image, "ImageMask", HPDF_OCLASS_BOOLEAN))
        return HPDF_RaiseError (image->error, HPDF_INVALID_OPERATION, 0);

    if (HPDF_Image_GetBitsPerComponent (image) != 8)
        return HPDF_RaiseError (image->error, HPDF_INVALID_BIT_PER_COMPONENT,
                0);

    name = HPDF_Image_GetColorSpace (image);
    if (!name || HPDF_StrCmp (COL_RGB, name) != 0)
        return HPDF_RaiseError (image->error, HPDF_INVALID_COLOR_SPACE, 0);

    /* Each integer must be in the range 0 to 2^BitsPerComponent - 1 */
    if (rmax > 255 || gmax > 255 || bmax > 255)
       return HPDF_RaiseError (image->error, HPDF_INVALID_PARAMETER, 0);

    array = HPDF_Array_New (image->mmgr);
    if (!array)
        return HPDF_CheckError (image->error);

    ret += HPDF_Dict_Add (image, "Mask", array);
    ret += HPDF_Array_AddNumber (array, rmin);
    ret += HPDF_Array_AddNumber (array, rmax);
    ret += HPDF_Array_AddNumber (array, gmin);
    ret += HPDF_Array_AddNumber (array, gmax);
    ret += HPDF_Array_AddNumber (array, bmin);
    ret += HPDF_Array_AddNumber (array, bmax);

    if (ret != HPDF_OK)
        return HPDF_CheckError (image->error);

    return HPDF_OK;
}
Exemple #5
0
HPDF_STATUS
HPDF_Catalog_AddPageLabel  (HPDF_Catalog   catalog,
                            HPDF_UINT      page_num,
                            HPDF_Dict      page_label)
{
    HPDF_STATUS ret;
    HPDF_Array nums;
    HPDF_Dict labels = HPDF_Dict_GetItem (catalog, "PageLabels",
        HPDF_OCLASS_DICT);

    HPDF_PTRACE ((" HPDF_Catalog_AddPageLabel\n"));

    if (!labels) {
        labels = HPDF_Dict_New (catalog->mmgr);

        if (!labels)
            return catalog->error->error_no;

        if ((ret = HPDF_Dict_Add (catalog, "PageLabels", labels)) != HPDF_OK)
            return ret;
    }

    nums = HPDF_Dict_GetItem (labels, "Nums", HPDF_OCLASS_ARRAY);

    if (!nums) {
        nums = HPDF_Array_New (catalog->mmgr);

        if (!nums)
            return catalog->error->error_no;

        if ((ret = HPDF_Dict_Add (labels, "Nums", nums)) != HPDF_OK)
            return ret;
    }

    if ((ret = HPDF_Array_AddNumber (nums, page_num)) != HPDF_OK)
        return ret;

    return HPDF_Array_Add (nums, page_label);
}
Exemple #6
0
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;
        }
Exemple #7
0
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_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;
}
Exemple #9
0
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 {
Exemple #10
0
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;
}