Integration of OCCT 6.5.0 from SVN
[occt.git] / src / Xw / Xw_def_fontmap.cxx
1
2 #include <Xw_Extension.h>
3
4         /* ifdef then trace on */
5 #ifdef TRACE
6 #define TRACE_DEF_FONTMAP
7 #endif
8
9 /*
10    XW_EXT_FONTMAP* Xw_def_fontmap(adisplay,nfont):
11    XW_EXT_DISPLAY *adisplay Extended Display structure
12
13    int nfont            Number of font cells to be allocated
14
15         Create a fontmap extension 
16         allocate the font cells in the fontmap as if possible
17         depending of the MAXFONT define .
18
19         Returns Fontmap extension address if successuful
20                 or NULL if ERROR
21
22    STATUS = Xw_close_fontmap(afontmap)
23    XW_EXT_FONTMAP* afontmap Extended fontmap
24
25
26         Destroy The Extended TypeMap
27
28         Returns ERROR if Bad Extended TypeMap Address
29                 SUCCESS if successfull
30
31 */
32
33 #ifdef XW_PROTOTYPE
34 void* Xw_def_fontmap (void* adisplay,int nfont)
35 #else
36 void* Xw_def_fontmap (adisplay,nfont)
37 void *adisplay ;
38 int nfont ;
39 #endif /*XW_PROTOTYPE*/
40 {
41 XW_EXT_DISPLAY *pdisplay = (XW_EXT_DISPLAY*)adisplay ;
42 XW_EXT_FONTMAP *pfontmap = NULL ;
43 XFontStruct *dfstruct ;
44 XGCValues values ;
45 GC gc ;
46 //int i,font,psize ;
47 int i,psize ;
48 char *dfstring = NULL ;
49
50     if( !Xw_isdefine_display(pdisplay) ) {
51         /*ERROR*Bad EXT_DISPLAY Address*/
52         Xw_set_error(96,"Xw_def_fontmap",pdisplay) ;
53         return (NULL) ;
54     }
55
56     gc = DefaultGCOfScreen(_DSCREEN) ;
57     XGetGCValues(_DDISPLAY,gc,GCFont,&values) ;
58     dfstruct = XQueryFont(_DDISPLAY,XGContextFromGC(gc)) ;
59     for( i=0 ; i<dfstruct->n_properties ; i++ ) {
60         if( dfstruct->properties[i].name == XA_FONT ) {
61             dfstring = XGetAtomName(_DDISPLAY,dfstruct->properties[i].card32) ;
62             break ;
63         }
64     }
65
66
67     if( !(pfontmap = Xw_add_fontmap_structure(sizeof(XW_EXT_FONTMAP))) ) 
68                                                                 return (NULL) ;
69
70     if( nfont <= 0 ) nfont = MAXFONT ;
71
72
73     pfontmap->connexion = pdisplay ;
74     pfontmap->maxfont = min(nfont,MAXFONT) ; 
75     pfontmap->gnames[0] = (char*) "Defaultfont";
76     pfontmap->snames[0] = dfstring ;
77     pfontmap->fonts[0] = dfstruct ;
78     pfontmap->fonts[0]->fid = values.font ;
79
80     psize = (pfontmap->fonts[0])->max_bounds.ascent + 
81                                 (pfontmap->fonts[0])->max_bounds.descent ;
82
83     pfontmap->gsizes[0] = (float)psize*HeightMMOfScreen(_DSCREEN)/
84                                                (float)HeightOfScreen(_DSCREEN) ;
85     pfontmap->fsizes[0] = pfontmap->gsizes[0];
86     pfontmap->fratios[0] = 0.;
87     pfontmap->ssizex[0] = pfontmap->ssizey[0] = pfontmap->fsizes[0] ;
88     pfontmap->gslants[0] = pfontmap->sslants[0] = 0. ;
89
90 #ifdef TRACE_DEF_FONTMAP
91 if( Xw_get_trace() ) {
92     printf(" %lx = Xw_def_fontmap(%lx,%d)\n", (long ) pfontmap,(long ) adisplay,nfont) ;
93 }
94 #endif
95
96     return (pfontmap);
97 }
98
99 static XW_EXT_FONTMAP *PfontmapList =NULL ;
100
101 #ifdef XW_PROTOTYPE
102 XW_EXT_FONTMAP* Xw_add_fontmap_structure(int size)
103 #else
104 XW_EXT_FONTMAP* Xw_add_fontmap_structure(size)
105 int size ;
106 #endif /*XW_PROTOTYPE*/
107 /*
108         Create and Insert one Extended fontmap structure in the
109         EXtended fontmap List
110
111         returns Extended fontmap address if successful
112                 or NULL if Bad Allocation
113 */
114 {
115 XW_EXT_FONTMAP *pfontmap = (XW_EXT_FONTMAP*) Xw_malloc(size) ;
116 int i ;
117
118         if( pfontmap ) {
119             pfontmap->type = FONTMAP_TYPE ;
120             pfontmap->link = PfontmapList ;
121             PfontmapList = pfontmap ;
122             pfontmap->connexion = NULL ;
123             pfontmap->maxfont = 0 ;
124             pfontmap->maxwindow = 0 ;
125             for( i=0 ; i<MAXFONT ; i++ ) {
126                 pfontmap->gnames[i] = NULL ;
127                 pfontmap->snames[i] = NULL ;
128                 pfontmap->fonts[i] = NULL ;
129                 pfontmap->gsizes[i] = 0. ;
130                 pfontmap->fsizes[i] = 0. ;
131                 pfontmap->ssizex[i] = 0. ;
132                 pfontmap->ssizey[i] = 0. ;
133                 pfontmap->gslants[i] = 0. ;
134                 pfontmap->sslants[i] = 0. ;
135                 pfontmap->fratios[i] = 0. ;
136             }
137         } else {
138             /*EXT_FONTMAP allocation failed*/
139             Xw_set_error(9,"Xw_add_fontmap_structure",NULL) ;
140         }
141
142         return (pfontmap) ;
143 }
144
145 #ifdef XW_PROTOTYPE
146 XW_STATUS Xw_close_fontmap(void* afontmap)
147 #else
148 XW_STATUS Xw_close_fontmap(afontmap)
149 void* afontmap ;
150 #endif /*XW_PROTOTYPE*/
151 {
152 XW_EXT_FONTMAP* pfontmap = (XW_EXT_FONTMAP*) afontmap ;
153 XW_STATUS status ;
154
155     if( !Xw_isdefine_fontmap(pfontmap) ) {
156         /*Bad EXT_FONTMAP Address*/
157         Xw_set_error(51,"Xw_close_fontmap",pfontmap) ;
158         return (XW_ERROR) ;
159     }
160
161     status = Xw_del_fontmap_structure(pfontmap) ;
162
163 #ifdef TRACE_DEF_FONTMAP
164 if( Xw_get_trace() ) {
165     printf(" %d = Xw_close_fontmap(%lx)\n",status,(long ) pfontmap) ;
166 }
167 #endif
168
169     return (status) ;
170 }
171
172 #ifdef XW_PROTOTYPE
173 XW_STATUS Xw_del_fontmap_structure(XW_EXT_FONTMAP* afontmap)
174 #else
175 XW_STATUS Xw_del_fontmap_structure(afontmap)
176 XW_EXT_FONTMAP *afontmap;
177 #endif /*XW_PROTOTYPE*/
178 /*
179         Remove the Extended fontmap address from the Extended List
180
181         returns ERROR if the fontmap address is not Found in the list
182         returns SUCCESS if successful
183 */
184 {
185 XW_EXT_FONTMAP *pfontmap = PfontmapList ;
186 int i ;
187
188     if( !afontmap ) return (XW_ERROR) ;
189
190     if( afontmap->maxwindow ) {
191         return (XW_ERROR) ;
192     } else {
193         for( i=1 ; i<MAXFONT ; i++) {
194             if( afontmap->fonts[i] ) {
195                 if( afontmap->gnames[i] ) Xw_free(afontmap->gnames[i]) ;
196                 if( afontmap->snames[i] ) Xw_free(afontmap->snames[i]) ;
197                 if( afontmap->fonts[i]->fid != afontmap->fonts[0]->fid )
198                                 XFreeFont(_FDISPLAY,afontmap->fonts[i]) ;
199             }
200         }
201
202         if( afontmap == pfontmap ) {
203             PfontmapList = (XW_EXT_FONTMAP*) afontmap->link ;
204         } else {
205             for( ; pfontmap ; pfontmap = (XW_EXT_FONTMAP*) pfontmap->link ) {
206                 if( pfontmap->link == afontmap ) {
207                     pfontmap->link = afontmap->link ;
208                     break ;
209                 }
210             }
211         }
212         Xw_free(afontmap) ;
213     }
214     return (XW_SUCCESS) ;
215 }