1 /*
2  * << Haru Free PDF Library >> -- hpdf_fontdef_cid.c
3  *
4  * URL: http://libharu.org
5  *
6  * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
7  * Copyright (c) 2007-2009 Antony Dovgal <tony@daylessday.org>
8  *
9  * Permission to use, copy, modify, distribute and sell this software
10  * and its documentation for any purpose is hereby granted without fee,
11  * provided that the above copyright notice appear in all copies and
12  * that both that copyright notice and this permission notice appear
13  * in supporting documentation.
14  * It is provided "as is" without express or implied warranty.
15  *
16  */
17 
18 #include "hpdf_conf.h"
19 #include "hpdf_utils.h"
20 #include "hpdf_fontdef.h"
21 
22 void
23 HPDF_CIDFontDef_FreeWidth  (HPDF_FontDef  fontdef);
24 
25 
26 void
27 HPDF_CIDFontDef_FreeFunc (HPDF_FontDef  fontdef);
28 
29 
30 /*----------------------------------------------------------------------*/
31 /*----- HPDF_CIDFontDef ------------------------------------------------*/
32 
33 void
HPDF_CIDFontDef_FreeWidth(HPDF_FontDef fontdef)34 HPDF_CIDFontDef_FreeWidth  (HPDF_FontDef  fontdef)
35 {
36     HPDF_CIDFontDefAttr attr = (HPDF_CIDFontDefAttr)fontdef->attr;
37     HPDF_UINT i;
38 
39     HPDF_PTRACE ((" HPDF_FontDef_Validate\n"));
40 
41     for (i = 0; i < attr->widths->count; i++) {
42         HPDF_CID_Width *w =
43                     (HPDF_CID_Width *)HPDF_List_ItemAt (attr->widths, i);
44 
45         HPDF_FreeMem (fontdef->mmgr, w);
46     }
47 
48     HPDF_List_Free (attr->widths);
49     attr->widths = NULL;
50 
51     fontdef->valid = HPDF_FALSE;
52 }
53 
54 
55 HPDF_FontDef
HPDF_CIDFontDef_New(HPDF_MMgr mmgr,char * name,HPDF_FontDef_InitFunc init_fn)56 HPDF_CIDFontDef_New  (HPDF_MMgr               mmgr,
57                       char              *name,
58                       HPDF_FontDef_InitFunc   init_fn)
59 {
60     HPDF_FontDef fontdef;
61     HPDF_CIDFontDefAttr fontdef_attr;
62 
63     HPDF_PTRACE ((" HPDF_CIDFontDef_New\n"));
64 
65     if (!mmgr)
66         return NULL;
67 
68     fontdef = HPDF_GetMem (mmgr, sizeof(HPDF_FontDef_Rec));
69     if (!fontdef)
70         return NULL;
71 
72     HPDF_MemSet (fontdef, 0, sizeof(HPDF_FontDef_Rec));
73     fontdef->sig_bytes = HPDF_FONTDEF_SIG_BYTES;
74     HPDF_StrCpy (fontdef->base_font, name, fontdef->base_font +
75                     HPDF_LIMIT_MAX_NAME_LEN);
76     fontdef->mmgr = mmgr;
77     fontdef->error = mmgr->error;
78     fontdef->type = HPDF_FONTDEF_TYPE_UNINITIALIZED;
79     fontdef->free_fn = HPDF_CIDFontDef_FreeFunc;
80     fontdef->init_fn = init_fn;
81     fontdef->valid = HPDF_FALSE;
82     fontdef_attr = HPDF_GetMem (mmgr, sizeof(HPDF_CIDFontDefAttr_Rec));
83     if (!fontdef_attr) {
84         HPDF_FreeMem (fontdef->mmgr, fontdef);
85         return NULL;
86     }
87 
88     fontdef->attr = fontdef_attr;
89     HPDF_MemSet ((HPDF_BYTE *)fontdef_attr, 0,
90                 sizeof(HPDF_CIDFontDefAttr_Rec));
91 
92     fontdef_attr->widths = HPDF_List_New (mmgr, HPDF_DEF_CHAR_WIDTHS_NUM);
93     if (!fontdef_attr->widths) {
94         HPDF_FreeMem (fontdef->mmgr, fontdef);
95         HPDF_FreeMem (fontdef->mmgr, fontdef_attr);
96         return NULL;
97     }
98 
99     fontdef->missing_width = 500;
100     fontdef_attr->DW = 1000;
101     fontdef_attr->DW2[0] = 880;
102     fontdef_attr->DW2[1] = -1000;
103 
104     return fontdef;
105 }
106 
107 
108 HPDF_INT16
HPDF_CIDFontDef_GetCIDWidth(HPDF_FontDef fontdef,HPDF_UINT16 cid)109 HPDF_CIDFontDef_GetCIDWidth  (HPDF_FontDef  fontdef,
110                               HPDF_UINT16    cid)
111 {
112     HPDF_CIDFontDefAttr attr = (HPDF_CIDFontDefAttr)fontdef->attr;
113     HPDF_UINT i;
114 
115     HPDF_PTRACE ((" HPDF_CIDFontDef_GetCIDWidth\n"));
116 
117     for (i = 0; i < attr->widths->count; i++) {
118         HPDF_CID_Width *w = (HPDF_CID_Width *)HPDF_List_ItemAt (attr->widths,
119                 i);
120 
121         if (w->cid == cid)
122             return w->width;
123     }
124 
125     /* Not found in pdf_cid_width array. */
126     return attr->DW;
127 }
128 
129 void
HPDF_CIDFontDef_FreeFunc(HPDF_FontDef fontdef)130 HPDF_CIDFontDef_FreeFunc (HPDF_FontDef  fontdef)
131 {
132     HPDF_CIDFontDefAttr attr = (HPDF_CIDFontDefAttr)fontdef->attr;
133 
134     HPDF_PTRACE ((" HPDF_CIDFontDef_FreeFunc\n"));
135 
136     HPDF_CIDFontDef_FreeWidth (fontdef);
137     HPDF_FreeMem (fontdef->mmgr, attr);
138 }
139 
140 
141 HPDF_STATUS
HPDF_CIDFontDef_AddWidth(HPDF_FontDef fontdef,const HPDF_CID_Width * widths)142 HPDF_CIDFontDef_AddWidth  (HPDF_FontDef            fontdef,
143                            const HPDF_CID_Width   *widths)
144 {
145     HPDF_CIDFontDefAttr attr = (HPDF_CIDFontDefAttr)fontdef->attr;
146 
147     HPDF_PTRACE ((" HPDF_CIDFontDef_AddWidth\n"));
148 
149     while (widths->cid != 0xFFFF) {
150         HPDF_CID_Width *w = HPDF_GetMem (fontdef->mmgr,
151                 sizeof (HPDF_CID_Width));
152         HPDF_STATUS ret;
153 
154         if (!w)
155             return fontdef->error->error_no;
156 
157         w->cid = widths->cid;
158         w->width = widths->width;
159 
160         if ((ret = HPDF_List_Add (attr->widths, w)) != HPDF_OK) {
161             HPDF_FreeMem (fontdef->mmgr, w);
162 
163             return ret;
164         }
165 
166         widths++;
167     }
168 
169     return HPDF_OK;
170 }
171 
172 
173 HPDF_STATUS
HPDF_CIDFontDef_ChangeStyle(HPDF_FontDef fontdef,HPDF_BOOL bold,HPDF_BOOL italic)174 HPDF_CIDFontDef_ChangeStyle  (HPDF_FontDef   fontdef,
175                               HPDF_BOOL      bold,
176                               HPDF_BOOL      italic)
177 {
178     HPDF_PTRACE ((" HPDF_CIDFontDef_ChangeStyle\n"));
179 
180     if (!fontdef || !fontdef->attr)
181         return HPDF_INVALID_FONTDEF_DATA;
182 
183     if (bold) {
184         fontdef->stemv *= 2;
185         fontdef->flags |= HPDF_FONT_FOURCE_BOLD;
186     }
187 
188     if (italic) {
189         fontdef->italic_angle -= 11;
190         fontdef->flags |= HPDF_FONT_ITALIC;
191     }
192 
193     return HPDF_OK;
194 }
195 
196 
197 
198