oleaut32: Fix sizeof() usage (Coverity).
[wine] / dlls / oleaut32 / olefont.c
1 /*
2  * OLE Font encapsulation implementation
3  *
4  * This file contains an implementation of the IFont
5  * interface and the OleCreateFontIndirect API call.
6  *
7  * Copyright 1999 Francis Beaudet
8  * Copyright 2006 (Google) Benjamin Arai
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <string.h>
27
28 #define COBJMACROS
29 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
31
32 #include "winerror.h"
33 #include "windef.h"
34 #include "winbase.h"
35 #include "wingdi.h"
36 #include "winuser.h"
37 #include "wine/list.h"
38 #include "wine/unicode.h"
39 #include "objbase.h"
40 #include "oleauto.h"    /* for SysAllocString(....) */
41 #include "ole2.h"
42 #include "olectl.h"
43 #include "wine/debug.h"
44 #include "connpt.h" /* for CreateConnectionPoint */
45 #include "oaidl.h"
46
47 WINE_DEFAULT_DEBUG_CHANNEL(ole);
48
49 /***********************************************************************
50  * Declaration of constants used when serializing the font object.
51  */
52 #define FONTPERSIST_ITALIC        0x02
53 #define FONTPERSIST_UNDERLINE     0x04
54 #define FONTPERSIST_STRIKETHROUGH 0x08
55
56 static HDC olefont_hdc;
57
58 /***********************************************************************
59  * List of the HFONTs it has given out, with each one having a separate
60  * ref count.
61  */
62 typedef struct _HFONTItem
63 {
64   struct list entry;
65
66   /* Reference count of any IFont objects that own this hfont */
67   LONG int_refs;
68
69   /* Total reference count of any refs held by the application obtained by AddRefHfont plus any internal refs */
70   LONG total_refs;
71
72   /* The font associated with this object. */
73   HFONT gdiFont;
74
75 } HFONTItem, *PHFONTItem;
76
77 static struct list OLEFontImpl_hFontList = LIST_INIT(OLEFontImpl_hFontList);
78
79 /* Counts how many fonts contain at least one lock */
80 static LONG ifont_cnt = 0;
81
82 /***********************************************************************
83  * Critical section for OLEFontImpl_hFontList
84  */
85 static CRITICAL_SECTION OLEFontImpl_csHFONTLIST;
86 static CRITICAL_SECTION_DEBUG OLEFontImpl_csHFONTLIST_debug =
87 {
88   0, 0, &OLEFontImpl_csHFONTLIST,
89   { &OLEFontImpl_csHFONTLIST_debug.ProcessLocksList,
90     &OLEFontImpl_csHFONTLIST_debug.ProcessLocksList },
91     0, 0, { (DWORD_PTR)(__FILE__ ": OLEFontImpl_csHFONTLIST") }
92 };
93 static CRITICAL_SECTION OLEFontImpl_csHFONTLIST = { &OLEFontImpl_csHFONTLIST_debug, -1, 0, 0, 0, 0 };
94
95 static HDC get_dc(void)
96 {
97     HDC hdc;
98     EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
99     if(!olefont_hdc)
100         olefont_hdc = CreateCompatibleDC(NULL);
101     hdc = olefont_hdc;
102     LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
103     return hdc;
104 }
105
106 static void delete_dc(void)
107 {
108     EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
109     if(olefont_hdc)
110     {
111         DeleteDC(olefont_hdc);
112         olefont_hdc = NULL;
113     }
114     LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
115 }
116
117 static void HFONTItem_Delete(PHFONTItem item)
118 {
119   DeleteObject(item->gdiFont);
120   list_remove(&item->entry);
121   HeapFree(GetProcessHeap(), 0, item);
122 }
123
124 /* Find hfont item entry in the list.  Should be called while holding the crit sect */
125 static HFONTItem *find_hfontitem(HFONT hfont)
126 {
127     HFONTItem *item;
128
129     LIST_FOR_EACH_ENTRY(item, &OLEFontImpl_hFontList, HFONTItem, entry)
130     {
131         if (item->gdiFont == hfont)
132             return item;
133     }
134     return NULL;
135 }
136
137 /* Add an item to the list with one internal reference */
138 static HRESULT add_hfontitem(HFONT hfont)
139 {
140     HFONTItem *new_item = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_item));
141
142     if(!new_item) return E_OUTOFMEMORY;
143
144     new_item->int_refs = 1;
145     new_item->total_refs = 1;
146     new_item->gdiFont = hfont;
147     EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
148     list_add_tail(&OLEFontImpl_hFontList,&new_item->entry);
149     LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
150     return S_OK;
151 }
152
153 static HRESULT inc_int_ref(HFONT hfont)
154 {
155     HFONTItem *item;
156     HRESULT hr = S_FALSE;
157
158     EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
159     item = find_hfontitem(hfont);
160
161     if(item)
162     {
163         item->int_refs++;
164         item->total_refs++;
165         hr = S_OK;
166     }
167     LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
168
169     return hr;
170 }
171
172 /* decrements the internal ref of a hfont item.  If both refs are zero it'll
173    remove the item from the list and delete the hfont */
174 static HRESULT dec_int_ref(HFONT hfont)
175 {
176     HFONTItem *item;
177     HRESULT hr = S_FALSE;
178
179     EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
180     item = find_hfontitem(hfont);
181
182     if(item)
183     {
184         item->int_refs--;
185         item->total_refs--;
186         if(item->int_refs == 0 && item->total_refs == 0)
187             HFONTItem_Delete(item);
188         hr = S_OK;
189     }
190     LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
191
192     return hr;
193 }
194
195 static HRESULT inc_ext_ref(HFONT hfont)
196 {
197     HFONTItem *item;
198     HRESULT hr = S_FALSE;
199
200     EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
201
202     item = find_hfontitem(hfont);
203     if(item)
204     {
205         item->total_refs++;
206         hr = S_OK;
207     }
208     LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
209
210     return hr;
211 }
212
213 static HRESULT dec_ext_ref(HFONT hfont)
214 {
215     HFONTItem *item;
216     HRESULT hr = S_FALSE;
217
218     EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
219
220     item = find_hfontitem(hfont);
221     if(item)
222     {
223         if(--item->total_refs >= 0) hr = S_OK;
224     }
225     LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
226
227     return hr;
228 }
229
230 static WCHAR *strdupW(const WCHAR* str)
231 {
232     WCHAR *ret;
233     DWORD size = (strlenW(str) + 1) * sizeof(WCHAR);
234
235     ret = HeapAlloc(GetProcessHeap(), 0, size);
236     if(ret)
237         memcpy(ret, str, size);
238     return ret;
239 }
240
241 /***********************************************************************
242  * Declaration of the implementation class for the IFont interface
243  */
244 typedef struct OLEFontImpl OLEFontImpl;
245
246 struct OLEFontImpl
247 {
248   /*
249    * This class supports many interfaces. IUnknown, IFont,
250    * IDispatch, IDispFont IPersistStream and IConnectionPointContainer.
251    * The first two are supported by the first vtable, the next two are
252    * supported by the second table and the last two have their own.
253    */
254   IFont                       IFont_iface;
255   IDispatch                   IDispatch_iface;
256   IPersistStream              IPersistStream_iface;
257   IConnectionPointContainer   IConnectionPointContainer_iface;
258   IPersistPropertyBag         IPersistPropertyBag_iface;
259   IPersistStreamInit          IPersistStreamInit_iface;
260   /*
261    * Reference count for that instance of the class.
262    */
263   LONG ref;
264
265   /*
266    * This structure contains the description of the class.
267    */
268   FONTDESC description;
269
270   /*
271    * Contain the font associated with this object.
272    */
273   HFONT gdiFont;
274   BOOL dirty;
275   /*
276    * Size ratio
277    */
278   LONG cyLogical;
279   LONG cyHimetric;
280
281   IConnectionPoint *pPropertyNotifyCP;
282   IConnectionPoint *pFontEventsCP;
283 };
284
285 /*
286  * Here, I define utility macros to help with the casting of the
287  * "this" parameter.
288  * There is a version to accommodate all of the VTables implemented
289  * by this object.
290  */
291
292 static inline OLEFontImpl *impl_from_IFont(IFont *iface)
293 {
294     return CONTAINING_RECORD(iface, OLEFontImpl, IFont_iface);
295 }
296
297 static inline OLEFontImpl *impl_from_IDispatch( IDispatch *iface )
298 {
299     return CONTAINING_RECORD(iface, OLEFontImpl, IDispatch_iface);
300 }
301
302 static inline OLEFontImpl *impl_from_IPersistStream( IPersistStream *iface )
303 {
304     return CONTAINING_RECORD(iface, OLEFontImpl, IPersistStream_iface);
305 }
306
307 static inline OLEFontImpl *impl_from_IConnectionPointContainer( IConnectionPointContainer *iface )
308 {
309     return CONTAINING_RECORD(iface, OLEFontImpl, IConnectionPointContainer_iface);
310 }
311
312 static inline OLEFontImpl *impl_from_IPersistPropertyBag( IPersistPropertyBag *iface )
313 {
314     return CONTAINING_RECORD(iface, OLEFontImpl, IPersistPropertyBag_iface);
315 }
316
317 static inline OLEFontImpl *impl_from_IPersistStreamInit( IPersistStreamInit *iface )
318 {
319     return CONTAINING_RECORD(iface, OLEFontImpl, IPersistStreamInit_iface);
320 }
321
322
323 /***********************************************************************
324  * Prototypes for the implementation functions for the IFont
325  * interface
326  */
327 static OLEFontImpl* OLEFontImpl_Construct(const FONTDESC *fontDesc);
328 static void         OLEFontImpl_Destroy(OLEFontImpl* fontDesc);
329 static ULONG        WINAPI OLEFontImpl_AddRef(IFont* iface);
330
331 /******************************************************************************
332  *              OleCreateFontIndirect   [OLEAUT32.420]
333  */
334 HRESULT WINAPI OleCreateFontIndirect(
335   LPFONTDESC lpFontDesc,
336   REFIID     riid,
337   LPVOID*     ppvObj)
338 {
339   OLEFontImpl* newFont = 0;
340   HRESULT      hr      = S_OK;
341
342   TRACE("(%p, %s, %p)\n", lpFontDesc, debugstr_guid(riid), ppvObj);
343   /*
344    * Sanity check
345    */
346   if (ppvObj==0)
347     return E_POINTER;
348
349   *ppvObj = 0;
350
351   if (!lpFontDesc) {
352     FONTDESC fd;
353
354     static WCHAR fname[] = { 'S','y','s','t','e','m',0 };
355
356     fd.cbSizeofstruct = sizeof(fd);
357     fd.lpstrName      = fname;
358     fd.cySize.s.Lo    = 80000;
359     fd.cySize.s.Hi    = 0;
360     fd.sWeight        = 0;
361     fd.sCharset       = 0;
362     fd.fItalic        = 0;
363     fd.fUnderline     = 0;
364     fd.fStrikethrough = 0;
365     lpFontDesc = &fd;
366   }
367
368   /*
369    * Try to construct a new instance of the class.
370    */
371   newFont = OLEFontImpl_Construct(lpFontDesc);
372
373   if (newFont == 0)
374     return E_OUTOFMEMORY;
375
376   /*
377    * Make sure it supports the interface required by the caller.
378    */
379   hr = IFont_QueryInterface(&newFont->IFont_iface, riid, ppvObj);
380
381   /*
382    * Release the reference obtained in the constructor. If
383    * the QueryInterface was unsuccessful, it will free the class.
384    */
385   IFont_Release(&newFont->IFont_iface);
386
387   return hr;
388 }
389
390
391 /***********************************************************************
392  * Implementation of the OLEFontImpl class.
393  */
394
395 /***********************************************************************
396  *    OLEFont_SendNotify (internal)
397  *
398  * Sends notification messages of changed properties to any interested
399  * connections.
400  */
401 static void OLEFont_SendNotify(OLEFontImpl* this, DISPID dispID)
402 {
403   static const WCHAR wszName[] = {'N','a','m','e',0};
404   static const WCHAR wszSize[] = {'S','i','z','e',0};
405   static const WCHAR wszBold[] = {'B','o','l','d',0};
406   static const WCHAR wszItalic[] = {'I','t','a','l','i','c',0};
407   static const WCHAR wszUnder[] = {'U','n','d','e','r','l','i','n','e',0};
408   static const WCHAR wszStrike[] = {'S','t','r','i','k','e','t','h','r','o','u','g','h',0};
409   static const WCHAR wszWeight[] = {'W','e','i','g','h','t',0};
410   static const WCHAR wszCharset[] = {'C','h','a','r','s','s','e','t',0};
411   static const LPCWSTR dispid_mapping[] =
412   {
413     wszName,
414     NULL,
415     wszSize,
416     wszBold,
417     wszItalic,
418     wszUnder,
419     wszStrike,
420     wszWeight,
421     wszCharset
422   };
423
424   IEnumConnections *pEnum;
425   CONNECTDATA CD;
426   HRESULT hres;
427
428   this->dirty = TRUE;
429
430   hres = IConnectionPoint_EnumConnections(this->pPropertyNotifyCP, &pEnum);
431   if (SUCCEEDED(hres))
432   {
433     while(IEnumConnections_Next(pEnum, 1, &CD, NULL) == S_OK) {
434       IPropertyNotifySink *sink;
435
436       IUnknown_QueryInterface(CD.pUnk, &IID_IPropertyNotifySink, (LPVOID)&sink);
437       IPropertyNotifySink_OnChanged(sink, dispID);
438       IPropertyNotifySink_Release(sink);
439       IUnknown_Release(CD.pUnk);
440     }
441     IEnumConnections_Release(pEnum);
442   }
443
444   hres = IConnectionPoint_EnumConnections(this->pFontEventsCP, &pEnum);
445   if (SUCCEEDED(hres))
446   {
447     DISPPARAMS dispparams;
448     VARIANTARG vararg;
449
450     VariantInit(&vararg);
451     V_VT(&vararg) = VT_BSTR;
452     V_BSTR(&vararg) = SysAllocString(dispid_mapping[dispID]);
453
454     dispparams.cArgs = 1;
455     dispparams.cNamedArgs = 0;
456     dispparams.rgdispidNamedArgs = NULL;
457     dispparams.rgvarg = &vararg;
458
459     while(IEnumConnections_Next(pEnum, 1, &CD, NULL) == S_OK) {
460         IFontEventsDisp *disp;
461
462         IUnknown_QueryInterface(CD.pUnk, &IID_IFontEventsDisp, (LPVOID)&disp);
463         IDispatch_Invoke(disp, DISPID_FONT_CHANGED, &IID_NULL,
464                          LOCALE_NEUTRAL, INVOKE_FUNC, &dispparams, NULL,
465                          NULL, NULL);
466
467         IDispatch_Release(disp);
468         IUnknown_Release(CD.pUnk);
469     }
470     VariantClear(&vararg);
471     IEnumConnections_Release(pEnum);
472   }
473 }
474
475 /************************************************************************
476  * OLEFontImpl_QueryInterface (IUnknown)
477  *
478  * See Windows documentation for more details on IUnknown methods.
479  */
480 static HRESULT WINAPI OLEFontImpl_QueryInterface(
481   IFont*  iface,
482   REFIID  riid,
483   void**  ppvObject)
484 {
485   OLEFontImpl *this = impl_from_IFont(iface);
486   TRACE("(%p)->(%s, %p)\n", this, debugstr_guid(riid), ppvObject);
487
488   *ppvObject = 0;
489
490   /*
491    * Compare the riid with the interface IDs implemented by this object.
492    */
493   if (IsEqualGUID(&IID_IUnknown, riid))
494     *ppvObject = this;
495   if (IsEqualGUID(&IID_IFont, riid))
496     *ppvObject = this;
497   if (IsEqualGUID(&IID_IDispatch, riid))
498     *ppvObject = &this->IDispatch_iface;
499   if (IsEqualGUID(&IID_IFontDisp, riid))
500     *ppvObject = &this->IDispatch_iface;
501   if (IsEqualIID(&IID_IPersist, riid) || IsEqualGUID(&IID_IPersistStream, riid))
502     *ppvObject = &this->IPersistStream_iface;
503   if (IsEqualGUID(&IID_IConnectionPointContainer, riid))
504     *ppvObject = &this->IConnectionPointContainer_iface;
505   if (IsEqualGUID(&IID_IPersistPropertyBag, riid))
506     *ppvObject = &this->IPersistPropertyBag_iface;
507   if (IsEqualGUID(&IID_IPersistStreamInit, riid))
508     *ppvObject = &this->IPersistStreamInit_iface;
509
510   /*
511    * Check that we obtained an interface.
512    */
513   if ((*ppvObject)==0)
514   {
515     FIXME("() : asking for unsupported interface %s\n",debugstr_guid(riid));
516     return E_NOINTERFACE;
517   }
518   OLEFontImpl_AddRef(&this->IFont_iface);
519   return S_OK;
520 }
521
522 /************************************************************************
523  * OLEFontImpl_AddRef (IUnknown)
524  *
525  * See Windows documentation for more details on IUnknown methods.
526  */
527 static ULONG WINAPI OLEFontImpl_AddRef(
528   IFont* iface)
529 {
530   OLEFontImpl *this = impl_from_IFont(iface);
531   TRACE("(%p)->(ref=%d)\n", this, this->ref);
532   return InterlockedIncrement(&this->ref);
533 }
534
535 /************************************************************************
536  * OLEFontImpl_Release (IUnknown)
537  *
538  * See Windows documentation for more details on IUnknown methods.
539  */
540 static ULONG WINAPI OLEFontImpl_Release(
541       IFont* iface)
542 {
543   OLEFontImpl *this = impl_from_IFont(iface);
544   ULONG ret;
545   TRACE("(%p)->(ref=%d)\n", this, this->ref);
546
547   /* Decrease the reference count for current interface */
548   ret = InterlockedDecrement(&this->ref);
549
550   /* If the reference count goes down to 0, destroy. */
551   if (ret == 0)
552   {
553     ULONG fontlist_refs = InterlockedDecrement(&ifont_cnt);
554
555     /* Final IFont object so destroy font cache */
556     if (fontlist_refs == 0)
557     {
558       HFONTItem *item, *cursor2;
559
560       EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
561       LIST_FOR_EACH_ENTRY_SAFE(item, cursor2, &OLEFontImpl_hFontList, HFONTItem, entry)
562         HFONTItem_Delete(item);
563       LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
564       delete_dc();
565     }
566     else
567     {
568       dec_int_ref(this->gdiFont);
569     }
570     OLEFontImpl_Destroy(this);
571   }
572
573   return ret;
574 }
575
576 typedef struct
577 {
578     short orig_cs;
579     short avail_cs;
580 } enum_data;
581
582 static int CALLBACK font_enum_proc(const LOGFONTW *elf, const TEXTMETRICW *ntm, DWORD type, LPARAM lp)
583 {
584     enum_data *data = (enum_data*)lp;
585
586     if(elf->lfCharSet == data->orig_cs)
587     {
588         data->avail_cs = data->orig_cs;
589         return 0;
590     }
591     if(data->avail_cs == -1) data->avail_cs = elf->lfCharSet;
592     return 1;
593 }
594
595 static void realize_font(OLEFontImpl *This)
596 {
597     if (This->dirty)
598     {
599         LOGFONTW logFont;
600         INT fontHeight;
601         WCHAR text_face[LF_FACESIZE];
602         HDC hdc = get_dc();
603         HFONT old_font;
604         TEXTMETRICW tm;
605
606         text_face[0] = 0;
607
608         if(This->gdiFont)
609         {
610             old_font = SelectObject(hdc, This->gdiFont);
611             GetTextFaceW(hdc, sizeof(text_face) / sizeof(text_face[0]), text_face);
612             SelectObject(hdc, old_font);
613             dec_int_ref(This->gdiFont);
614             This->gdiFont = 0;
615         }
616
617         memset(&logFont, 0, sizeof(LOGFONTW));
618
619         lstrcpynW(logFont.lfFaceName, This->description.lpstrName, LF_FACESIZE);
620         logFont.lfCharSet         = This->description.sCharset;
621
622         /* If the font name has been changed then enumerate all charsets
623            and pick one that'll result in the font specified being selected */
624         if(text_face[0] && lstrcmpiW(text_face, This->description.lpstrName))
625         {
626             enum_data data;
627             data.orig_cs = This->description.sCharset;
628             data.avail_cs = -1;
629             logFont.lfCharSet = DEFAULT_CHARSET;
630             EnumFontFamiliesExW(get_dc(), &logFont, font_enum_proc, (LPARAM)&data, 0);
631             if(data.avail_cs != -1) logFont.lfCharSet = data.avail_cs;
632         }
633
634
635         /*
636          * The height of the font returned by the get_Size property is the
637          * height of the font in points multiplied by 10000... Using some
638          * simple conversions and the ratio given by the application, it can
639          * be converted to a height in pixels.
640          *
641          * Standard ratio is 72 / 2540, or 18 / 635 in lowest terms.
642          * Ratio is applied here relative to the standard.
643          */
644
645         fontHeight = MulDiv( This->description.cySize.s.Lo, This->cyLogical*635, This->cyHimetric*18 );
646
647
648         logFont.lfHeight          = ((fontHeight%10000L)>5000L) ? (-fontHeight/10000L) - 1 :
649                                                                   (-fontHeight/10000L);
650         logFont.lfItalic          = This->description.fItalic;
651         logFont.lfUnderline       = This->description.fUnderline;
652         logFont.lfStrikeOut       = This->description.fStrikethrough;
653         logFont.lfWeight          = This->description.sWeight;
654         logFont.lfOutPrecision    = OUT_CHARACTER_PRECIS;
655         logFont.lfClipPrecision   = CLIP_DEFAULT_PRECIS;
656         logFont.lfQuality         = DEFAULT_QUALITY;
657         logFont.lfPitchAndFamily  = DEFAULT_PITCH;
658
659         This->gdiFont = CreateFontIndirectW(&logFont);
660         This->dirty = FALSE;
661
662         add_hfontitem(This->gdiFont);
663
664         /* Fixup the name and charset properties so that they match the
665            selected font */
666         old_font = SelectObject(get_dc(), This->gdiFont);
667         GetTextFaceW(hdc, sizeof(text_face) / sizeof(text_face[0]), text_face);
668         if(lstrcmpiW(text_face, This->description.lpstrName))
669         {
670             HeapFree(GetProcessHeap(), 0, This->description.lpstrName);
671             This->description.lpstrName = strdupW(text_face);
672         }
673         GetTextMetricsW(hdc, &tm);
674         This->description.sCharset = tm.tmCharSet;
675         SelectObject(hdc, old_font);
676     }
677 }
678
679 /************************************************************************
680  * OLEFontImpl_get_Name (IFont)
681  *
682  * See Windows documentation for more details on IFont methods.
683  */
684 static HRESULT WINAPI OLEFontImpl_get_Name(
685   IFont*  iface,
686   BSTR* pname)
687 {
688   OLEFontImpl *this = impl_from_IFont(iface);
689   TRACE("(%p)->(%p)\n", this, pname);
690   /*
691    * Sanity check.
692    */
693   if (pname==0)
694     return E_POINTER;
695
696   if(this->dirty) realize_font(this);
697
698   if (this->description.lpstrName!=0)
699     *pname = SysAllocString(this->description.lpstrName);
700   else
701     *pname = 0;
702
703   return S_OK;
704 }
705
706 /************************************************************************
707  * OLEFontImpl_put_Name (IFont)
708  *
709  * See Windows documentation for more details on IFont methods.
710  */
711 static HRESULT WINAPI OLEFontImpl_put_Name(
712   IFont* iface,
713   BSTR name)
714 {
715   OLEFontImpl *this = impl_from_IFont(iface);
716   TRACE("(%p)->(%p)\n", this, name);
717
718   if (!name)
719     return CTL_E_INVALIDPROPERTYVALUE;
720
721   if (this->description.lpstrName==0)
722   {
723     this->description.lpstrName = HeapAlloc(GetProcessHeap(),
724                                             0,
725                                             (lstrlenW(name)+1) * sizeof(WCHAR));
726   }
727   else
728   {
729     this->description.lpstrName = HeapReAlloc(GetProcessHeap(),
730                                               0,
731                                               this->description.lpstrName,
732                                               (lstrlenW(name)+1) * sizeof(WCHAR));
733   }
734
735   if (this->description.lpstrName==0)
736     return E_OUTOFMEMORY;
737
738   strcpyW(this->description.lpstrName, name);
739   TRACE("new name %s\n", debugstr_w(this->description.lpstrName));
740   OLEFont_SendNotify(this, DISPID_FONT_NAME);
741   return S_OK;
742 }
743
744 /************************************************************************
745  * OLEFontImpl_get_Size (IFont)
746  *
747  * See Windows documentation for more details on IFont methods.
748  */
749 static HRESULT WINAPI OLEFontImpl_get_Size(
750   IFont* iface,
751   CY*    psize)
752 {
753   OLEFontImpl *this = impl_from_IFont(iface);
754   TRACE("(%p)->(%p)\n", this, psize);
755
756   /*
757    * Sanity check
758    */
759   if (psize==0)
760     return E_POINTER;
761
762   if(this->dirty) realize_font(this);
763
764   psize->s.Hi = 0;
765   psize->s.Lo = this->description.cySize.s.Lo;
766
767   return S_OK;
768 }
769
770 /************************************************************************
771  * OLEFontImpl_put_Size (IFont)
772  *
773  * See Windows documentation for more details on IFont methods.
774  */
775 static HRESULT WINAPI OLEFontImpl_put_Size(
776   IFont* iface,
777   CY     size)
778 {
779   OLEFontImpl *this = impl_from_IFont(iface);
780   TRACE("(%p)->(%d)\n", this, size.s.Lo);
781   this->description.cySize.s.Hi = 0;
782   this->description.cySize.s.Lo = size.s.Lo;
783   OLEFont_SendNotify(this, DISPID_FONT_SIZE);
784
785   return S_OK;
786 }
787
788 /************************************************************************
789  * OLEFontImpl_get_Bold (IFont)
790  *
791  * See Windows documentation for more details on IFont methods.
792  */
793 static HRESULT WINAPI OLEFontImpl_get_Bold(
794   IFont*  iface,
795   BOOL* pbold)
796 {
797   OLEFontImpl *this = impl_from_IFont(iface);
798   TRACE("(%p)->(%p)\n", this, pbold);
799   /*
800    * Sanity check
801    */
802   if (pbold==0)
803     return E_POINTER;
804
805   if(this->dirty) realize_font(this);
806
807   *pbold = this->description.sWeight > 550;
808
809   return S_OK;
810 }
811
812 /************************************************************************
813  * OLEFontImpl_put_Bold (IFont)
814  *
815  * See Windows documentation for more details on IFont methods.
816  */
817 static HRESULT WINAPI OLEFontImpl_put_Bold(
818   IFont* iface,
819   BOOL bold)
820 {
821   OLEFontImpl *this = impl_from_IFont(iface);
822   TRACE("(%p)->(%d)\n", this, bold);
823   this->description.sWeight = bold ? FW_BOLD : FW_NORMAL;
824   OLEFont_SendNotify(this, DISPID_FONT_BOLD);
825
826   return S_OK;
827 }
828
829 /************************************************************************
830  * OLEFontImpl_get_Italic (IFont)
831  *
832  * See Windows documentation for more details on IFont methods.
833  */
834 static HRESULT WINAPI OLEFontImpl_get_Italic(
835   IFont*  iface,
836   BOOL* pitalic)
837 {
838   OLEFontImpl *this = impl_from_IFont(iface);
839   TRACE("(%p)->(%p)\n", this, pitalic);
840   /*
841    * Sanity check
842    */
843   if (pitalic==0)
844     return E_POINTER;
845
846   if(this->dirty) realize_font(this);
847
848   *pitalic = this->description.fItalic;
849
850   return S_OK;
851 }
852
853 /************************************************************************
854  * OLEFontImpl_put_Italic (IFont)
855  *
856  * See Windows documentation for more details on IFont methods.
857  */
858 static HRESULT WINAPI OLEFontImpl_put_Italic(
859   IFont* iface,
860   BOOL italic)
861 {
862   OLEFontImpl *this = impl_from_IFont(iface);
863   TRACE("(%p)->(%d)\n", this, italic);
864
865   this->description.fItalic = italic;
866
867   OLEFont_SendNotify(this, DISPID_FONT_ITALIC);
868   return S_OK;
869 }
870
871 /************************************************************************
872  * OLEFontImpl_get_Underline (IFont)
873  *
874  * See Windows documentation for more details on IFont methods.
875  */
876 static HRESULT WINAPI OLEFontImpl_get_Underline(
877   IFont*  iface,
878   BOOL* punderline)
879 {
880   OLEFontImpl *this = impl_from_IFont(iface);
881   TRACE("(%p)->(%p)\n", this, punderline);
882
883   /*
884    * Sanity check
885    */
886   if (punderline==0)
887     return E_POINTER;
888
889   if(this->dirty) realize_font(this);
890
891   *punderline = this->description.fUnderline;
892
893   return S_OK;
894 }
895
896 /************************************************************************
897  * OLEFontImpl_put_Underline (IFont)
898  *
899  * See Windows documentation for more details on IFont methods.
900  */
901 static HRESULT WINAPI OLEFontImpl_put_Underline(
902   IFont* iface,
903   BOOL underline)
904 {
905   OLEFontImpl *this = impl_from_IFont(iface);
906   TRACE("(%p)->(%d)\n", this, underline);
907
908   this->description.fUnderline = underline;
909
910   OLEFont_SendNotify(this, DISPID_FONT_UNDER);
911   return S_OK;
912 }
913
914 /************************************************************************
915  * OLEFontImpl_get_Strikethrough (IFont)
916  *
917  * See Windows documentation for more details on IFont methods.
918  */
919 static HRESULT WINAPI OLEFontImpl_get_Strikethrough(
920   IFont*  iface,
921   BOOL* pstrikethrough)
922 {
923   OLEFontImpl *this = impl_from_IFont(iface);
924   TRACE("(%p)->(%p)\n", this, pstrikethrough);
925
926   /*
927    * Sanity check
928    */
929   if (pstrikethrough==0)
930     return E_POINTER;
931
932   if(this->dirty) realize_font(this);
933
934   *pstrikethrough = this->description.fStrikethrough;
935
936   return S_OK;
937 }
938
939 /************************************************************************
940  * OLEFontImpl_put_Strikethrough (IFont)
941  *
942  * See Windows documentation for more details on IFont methods.
943  */
944 static HRESULT WINAPI OLEFontImpl_put_Strikethrough(
945  IFont* iface,
946  BOOL strikethrough)
947 {
948   OLEFontImpl *this = impl_from_IFont(iface);
949   TRACE("(%p)->(%d)\n", this, strikethrough);
950
951   this->description.fStrikethrough = strikethrough;
952   OLEFont_SendNotify(this, DISPID_FONT_STRIKE);
953
954   return S_OK;
955 }
956
957 /************************************************************************
958  * OLEFontImpl_get_Weight (IFont)
959  *
960  * See Windows documentation for more details on IFont methods.
961  */
962 static HRESULT WINAPI OLEFontImpl_get_Weight(
963   IFont* iface,
964   short* pweight)
965 {
966   OLEFontImpl *this = impl_from_IFont(iface);
967   TRACE("(%p)->(%p)\n", this, pweight);
968
969   /*
970    * Sanity check
971    */
972   if (pweight==0)
973     return E_POINTER;
974
975   if(this->dirty) realize_font(this);
976
977   *pweight = this->description.sWeight;
978
979   return S_OK;
980 }
981
982 /************************************************************************
983  * OLEFontImpl_put_Weight (IFont)
984  *
985  * See Windows documentation for more details on IFont methods.
986  */
987 static HRESULT WINAPI OLEFontImpl_put_Weight(
988   IFont* iface,
989   short  weight)
990 {
991   OLEFontImpl *this = impl_from_IFont(iface);
992   TRACE("(%p)->(%d)\n", this, weight);
993
994   this->description.sWeight = weight;
995
996   OLEFont_SendNotify(this, DISPID_FONT_WEIGHT);
997   return S_OK;
998 }
999
1000 /************************************************************************
1001  * OLEFontImpl_get_Charset (IFont)
1002  *
1003  * See Windows documentation for more details on IFont methods.
1004  */
1005 static HRESULT WINAPI OLEFontImpl_get_Charset(
1006   IFont* iface,
1007   short* pcharset)
1008 {
1009   OLEFontImpl *this = impl_from_IFont(iface);
1010   TRACE("(%p)->(%p)\n", this, pcharset);
1011
1012   /*
1013    * Sanity check
1014    */
1015   if (pcharset==0)
1016     return E_POINTER;
1017
1018   if(this->dirty) realize_font(this);
1019
1020   *pcharset = this->description.sCharset;
1021
1022   return S_OK;
1023 }
1024
1025 /************************************************************************
1026  * OLEFontImpl_put_Charset (IFont)
1027  *
1028  * See Windows documentation for more details on IFont methods.
1029  */
1030 static HRESULT WINAPI OLEFontImpl_put_Charset(
1031   IFont* iface,
1032   short charset)
1033 {
1034   OLEFontImpl *this = impl_from_IFont(iface);
1035   TRACE("(%p)->(%d)\n", this, charset);
1036
1037   this->description.sCharset = charset;
1038   OLEFont_SendNotify(this, DISPID_FONT_CHARSET);
1039
1040   return S_OK;
1041 }
1042
1043 /************************************************************************
1044  * OLEFontImpl_get_hFont (IFont)
1045  *
1046  * See Windows documentation for more details on IFont methods.
1047  */
1048 static HRESULT WINAPI OLEFontImpl_get_hFont(
1049   IFont*   iface,
1050   HFONT* phfont)
1051 {
1052   OLEFontImpl *this = impl_from_IFont(iface);
1053   TRACE("(%p)->(%p)\n", this, phfont);
1054   if (phfont==NULL)
1055     return E_POINTER;
1056
1057   if(this->dirty) realize_font(this);
1058
1059   *phfont = this->gdiFont;
1060   TRACE("Returning %p\n", *phfont);
1061   return S_OK;
1062 }
1063
1064 /************************************************************************
1065  * OLEFontImpl_Clone (IFont)
1066  *
1067  * See Windows documentation for more details on IFont methods.
1068  */
1069 static HRESULT WINAPI OLEFontImpl_Clone(
1070   IFont*  iface,
1071   IFont** ppfont)
1072 {
1073   OLEFontImpl* newObject = 0;
1074   OLEFontImpl *this = impl_from_IFont(iface);
1075
1076   TRACE("(%p)->(%p)\n", this, ppfont);
1077
1078   if (ppfont == NULL)
1079     return E_POINTER;
1080
1081   *ppfont = NULL;
1082
1083   /*
1084    * Allocate space for the object.
1085    */
1086   newObject = HeapAlloc(GetProcessHeap(), 0, sizeof(OLEFontImpl));
1087
1088   if (newObject==NULL)
1089     return E_OUTOFMEMORY;
1090
1091   *newObject = *this;
1092
1093   /* We need to alloc new memory for the string, otherwise
1094    * we free memory twice.
1095    */
1096   newObject->description.lpstrName = HeapAlloc(
1097         GetProcessHeap(),0,
1098         (1+strlenW(this->description.lpstrName))*2
1099   );
1100   strcpyW(newObject->description.lpstrName, this->description.lpstrName);
1101
1102
1103   /* Increment internal ref in hfont item list */
1104   if(newObject->gdiFont) inc_int_ref(newObject->gdiFont);
1105
1106   InterlockedIncrement(&ifont_cnt);
1107
1108   /* create new connection points */
1109   newObject->pPropertyNotifyCP = NULL;
1110   newObject->pFontEventsCP = NULL;
1111   CreateConnectionPoint((IUnknown*)newObject, &IID_IPropertyNotifySink, &newObject->pPropertyNotifyCP);
1112   CreateConnectionPoint((IUnknown*)newObject, &IID_IFontEventsDisp, &newObject->pFontEventsCP);
1113
1114   if (!newObject->pPropertyNotifyCP || !newObject->pFontEventsCP)
1115   {
1116     OLEFontImpl_Destroy(newObject);
1117     return E_OUTOFMEMORY;
1118   }
1119
1120   /* The cloned object starts with a reference count of 1 */
1121   newObject->ref          = 1;
1122
1123   *ppfont = &newObject->IFont_iface;
1124
1125   return S_OK;
1126 }
1127
1128 /************************************************************************
1129  * OLEFontImpl_IsEqual (IFont)
1130  *
1131  * See Windows documentation for more details on IFont methods.
1132  */
1133 static HRESULT WINAPI OLEFontImpl_IsEqual(
1134   IFont* iface,
1135   IFont* pFontOther)
1136 {
1137   OLEFontImpl *left = impl_from_IFont(iface);
1138   OLEFontImpl *right = impl_from_IFont(pFontOther);
1139   INT ret;
1140   INT left_len,right_len;
1141
1142   if(pFontOther == NULL)
1143     return E_POINTER;
1144   else if (left->description.cySize.s.Lo != right->description.cySize.s.Lo)
1145     return S_FALSE;
1146   else if (left->description.cySize.s.Hi != right->description.cySize.s.Hi)
1147     return S_FALSE;
1148   else if (left->description.sWeight != right->description.sWeight)
1149     return S_FALSE;
1150   else if (left->description.sCharset != right->description.sCharset)
1151     return S_FALSE;
1152   else if (left->description.fItalic != right->description.fItalic)
1153     return S_FALSE;
1154   else if (left->description.fUnderline != right->description.fUnderline)
1155     return S_FALSE;
1156   else if (left->description.fStrikethrough != right->description.fStrikethrough)
1157     return S_FALSE;
1158
1159   /* Check from string */
1160   left_len = strlenW(left->description.lpstrName);
1161   right_len = strlenW(right->description.lpstrName);
1162   ret = CompareStringW(0,0,left->description.lpstrName, left_len,
1163     right->description.lpstrName, right_len);
1164   if (ret != CSTR_EQUAL)
1165     return S_FALSE;
1166
1167   return S_OK;
1168 }
1169
1170 /************************************************************************
1171  * OLEFontImpl_SetRatio (IFont)
1172  *
1173  * See Windows documentation for more details on IFont methods.
1174  */
1175 static HRESULT WINAPI OLEFontImpl_SetRatio(
1176   IFont* iface,
1177   LONG   cyLogical,
1178   LONG   cyHimetric)
1179 {
1180   OLEFontImpl *this = impl_from_IFont(iface);
1181   TRACE("(%p)->(%d, %d)\n", this, cyLogical, cyHimetric);
1182
1183   this->cyLogical  = cyLogical;
1184   this->cyHimetric = cyHimetric;
1185
1186   return S_OK;
1187 }
1188
1189 /************************************************************************
1190  * OLEFontImpl_QueryTextMetrics (IFont)
1191  *
1192  * See Windows documentation for more details on IFont methods.
1193  */
1194 static HRESULT      WINAPI OLEFontImpl_QueryTextMetrics(
1195   IFont*         iface,
1196   TEXTMETRICOLE* ptm)
1197 {
1198   HDC hdcRef;
1199   HFONT hOldFont, hNewFont;
1200
1201   hdcRef = GetDC(0);
1202   OLEFontImpl_get_hFont(iface, &hNewFont);
1203   hOldFont = SelectObject(hdcRef, hNewFont);
1204   GetTextMetricsW(hdcRef, ptm);
1205   SelectObject(hdcRef, hOldFont);
1206   ReleaseDC(0, hdcRef);
1207   return S_OK;
1208 }
1209
1210 /************************************************************************
1211  * OLEFontImpl_AddRefHfont (IFont)
1212  *
1213  * See Windows documentation for more details on IFont methods.
1214  */
1215 static HRESULT WINAPI OLEFontImpl_AddRefHfont(
1216   IFont*  iface,
1217   HFONT hfont)
1218 {
1219     OLEFontImpl *this = impl_from_IFont(iface);
1220
1221     TRACE("(%p)->(%p)\n", this, hfont);
1222
1223     if (!hfont) return E_INVALIDARG;
1224
1225     return inc_ext_ref(hfont);
1226 }
1227
1228 /************************************************************************
1229  * OLEFontImpl_ReleaseHfont (IFont)
1230  *
1231  * See Windows documentation for more details on IFont methods.
1232  */
1233 static HRESULT WINAPI OLEFontImpl_ReleaseHfont(
1234   IFont*  iface,
1235   HFONT hfont)
1236 {
1237     OLEFontImpl *this = impl_from_IFont(iface);
1238
1239     TRACE("(%p)->(%p)\n", this, hfont);
1240
1241     if (!hfont) return E_INVALIDARG;
1242
1243     return dec_ext_ref(hfont);
1244 }
1245
1246 /************************************************************************
1247  * OLEFontImpl_SetHdc (IFont)
1248  *
1249  * See Windows documentation for more details on IFont methods.
1250  */
1251 static HRESULT WINAPI OLEFontImpl_SetHdc(
1252   IFont* iface,
1253   HDC  hdc)
1254 {
1255   OLEFontImpl *this = impl_from_IFont(iface);
1256   FIXME("(%p)->(%p): Stub\n", this, hdc);
1257   return E_NOTIMPL;
1258 }
1259
1260 /*
1261  * Virtual function tables for the OLEFontImpl class.
1262  */
1263 static const IFontVtbl OLEFontImpl_VTable =
1264 {
1265   OLEFontImpl_QueryInterface,
1266   OLEFontImpl_AddRef,
1267   OLEFontImpl_Release,
1268   OLEFontImpl_get_Name,
1269   OLEFontImpl_put_Name,
1270   OLEFontImpl_get_Size,
1271   OLEFontImpl_put_Size,
1272   OLEFontImpl_get_Bold,
1273   OLEFontImpl_put_Bold,
1274   OLEFontImpl_get_Italic,
1275   OLEFontImpl_put_Italic,
1276   OLEFontImpl_get_Underline,
1277   OLEFontImpl_put_Underline,
1278   OLEFontImpl_get_Strikethrough,
1279   OLEFontImpl_put_Strikethrough,
1280   OLEFontImpl_get_Weight,
1281   OLEFontImpl_put_Weight,
1282   OLEFontImpl_get_Charset,
1283   OLEFontImpl_put_Charset,
1284   OLEFontImpl_get_hFont,
1285   OLEFontImpl_Clone,
1286   OLEFontImpl_IsEqual,
1287   OLEFontImpl_SetRatio,
1288   OLEFontImpl_QueryTextMetrics,
1289   OLEFontImpl_AddRefHfont,
1290   OLEFontImpl_ReleaseHfont,
1291   OLEFontImpl_SetHdc
1292 };
1293
1294 /************************************************************************
1295  * OLEFontImpl_IDispatch_QueryInterface (IUnknown)
1296  *
1297  * See Windows documentation for more details on IUnknown methods.
1298  */
1299 static HRESULT WINAPI OLEFontImpl_IDispatch_QueryInterface(
1300   IDispatch* iface,
1301   REFIID     riid,
1302   VOID**     ppvoid)
1303 {
1304   OLEFontImpl *this = impl_from_IDispatch(iface);
1305
1306   return IFont_QueryInterface(&this->IFont_iface, riid, ppvoid);
1307 }
1308
1309 /************************************************************************
1310  * OLEFontImpl_IDispatch_Release (IUnknown)
1311  *
1312  * See Windows documentation for more details on IUnknown methods.
1313  */
1314 static ULONG WINAPI OLEFontImpl_IDispatch_Release(
1315   IDispatch* iface)
1316 {
1317   OLEFontImpl *this = impl_from_IDispatch(iface);
1318
1319   return IFont_Release(&this->IFont_iface);
1320 }
1321
1322 /************************************************************************
1323  * OLEFontImpl_IDispatch_AddRef (IUnknown)
1324  *
1325  * See Windows documentation for more details on IUnknown methods.
1326  */
1327 static ULONG WINAPI OLEFontImpl_IDispatch_AddRef(
1328   IDispatch* iface)
1329 {
1330   OLEFontImpl *this = impl_from_IDispatch(iface);
1331
1332   return IFont_AddRef(&this->IFont_iface);
1333 }
1334
1335 /************************************************************************
1336  * OLEFontImpl_GetTypeInfoCount (IDispatch)
1337  *
1338  * See Windows documentation for more details on IDispatch methods.
1339  */
1340 static HRESULT WINAPI OLEFontImpl_GetTypeInfoCount(
1341   IDispatch*    iface,
1342   unsigned int* pctinfo)
1343 {
1344   OLEFontImpl *this = impl_from_IDispatch(iface);
1345   TRACE("(%p)->(%p)\n", this, pctinfo);
1346   *pctinfo = 1;
1347
1348   return S_OK;
1349 }
1350
1351 /************************************************************************
1352  * OLEFontImpl_GetTypeInfo (IDispatch)
1353  *
1354  * See Windows documentation for more details on IDispatch methods.
1355  */
1356 static HRESULT WINAPI OLEFontImpl_GetTypeInfo(
1357   IDispatch*  iface,
1358   UINT      iTInfo,
1359   LCID        lcid,
1360   ITypeInfo** ppTInfo)
1361 {
1362   static const WCHAR stdole2tlb[] = {'s','t','d','o','l','e','2','.','t','l','b',0};
1363   ITypeLib *tl;
1364   HRESULT hres;
1365
1366   OLEFontImpl *this = impl_from_IDispatch(iface);
1367   TRACE("(%p, iTInfo=%d, lcid=%04x, %p)\n", this, iTInfo, (int)lcid, ppTInfo);
1368   if (iTInfo != 0)
1369     return E_FAIL;
1370   hres = LoadTypeLib(stdole2tlb, &tl);
1371   if (FAILED(hres)) {
1372     ERR("Could not load the stdole2.tlb?\n");
1373     return hres;
1374   }
1375   hres = ITypeLib_GetTypeInfoOfGuid(tl, &IID_IFontDisp, ppTInfo);
1376   ITypeLib_Release(tl);
1377   if (FAILED(hres)) {
1378     FIXME("Did not IDispatch typeinfo from typelib, hres %x\n",hres);
1379   }
1380   return hres;
1381 }
1382
1383 /************************************************************************
1384  * OLEFontImpl_GetIDsOfNames (IDispatch)
1385  *
1386  * See Windows documentation for more details on IDispatch methods.
1387  */
1388 static HRESULT WINAPI OLEFontImpl_GetIDsOfNames(
1389   IDispatch*  iface,
1390   REFIID      riid,
1391   LPOLESTR* rgszNames,
1392   UINT      cNames,
1393   LCID        lcid,
1394   DISPID*     rgDispId)
1395 {
1396   ITypeInfo * pTInfo;
1397   HRESULT hres;
1398
1399   OLEFontImpl *this = impl_from_IDispatch(iface);
1400
1401   TRACE("(%p,%s,%p,cNames=%d,lcid=%04x,%p)\n", this, debugstr_guid(riid),
1402         rgszNames, cNames, (int)lcid, rgDispId);
1403
1404   if (cNames == 0)
1405   {
1406     return E_INVALIDARG;
1407   }
1408   else
1409   {
1410     /* retrieve type information */
1411     hres = OLEFontImpl_GetTypeInfo(iface, 0, lcid, &pTInfo);
1412
1413     if (FAILED(hres))
1414     {
1415       ERR("GetTypeInfo failed.\n");
1416       return hres;
1417     }
1418
1419     /* convert names to DISPIDs */
1420     hres = DispGetIDsOfNames (pTInfo, rgszNames, cNames, rgDispId);
1421     ITypeInfo_Release(pTInfo);
1422
1423     return hres;
1424   }
1425 }
1426
1427 /************************************************************************
1428  * OLEFontImpl_Invoke (IDispatch)
1429  *
1430  * See Windows documentation for more details on IDispatch methods.
1431  * 
1432  * Note: Do not call _put_Xxx methods, since setting things here
1433  * should not call notify functions as I found out debugging the generic
1434  * MS VB5 installer.
1435  */
1436 static HRESULT WINAPI OLEFontImpl_Invoke(
1437   IDispatch*  iface,
1438   DISPID      dispIdMember,
1439   REFIID      riid,
1440   LCID        lcid,
1441   WORD        wFlags,
1442   DISPPARAMS* pDispParams,
1443   VARIANT*    pVarResult,
1444   EXCEPINFO*  pExepInfo,
1445   UINT*     puArgErr)
1446 {
1447   OLEFontImpl *this = impl_from_IDispatch(iface);
1448   HRESULT hr;
1449
1450   TRACE("%p->(%d,%s,0x%x,0x%x,%p,%p,%p,%p)\n", this, dispIdMember,
1451     debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult, pExepInfo,
1452     puArgErr);
1453
1454   /* validate parameters */
1455
1456   if (!IsEqualIID(riid, &IID_NULL))
1457   {
1458     ERR("riid was %s instead of IID_NULL\n", debugstr_guid(riid));
1459     return DISP_E_UNKNOWNINTERFACE;
1460   }
1461
1462   if (wFlags & DISPATCH_PROPERTYGET)
1463   {
1464     if (!pVarResult)
1465     {
1466       ERR("null pVarResult not allowed when DISPATCH_PROPERTYGET specified\n");
1467       return DISP_E_PARAMNOTOPTIONAL;
1468     }
1469   }
1470   else if (wFlags & DISPATCH_PROPERTYPUT)
1471   {
1472     if (!pDispParams)
1473     {
1474       ERR("null pDispParams not allowed when DISPATCH_PROPERTYPUT specified\n");
1475       return DISP_E_PARAMNOTOPTIONAL;
1476     }
1477     if (pDispParams->cArgs != 1)
1478     {
1479       ERR("param count for DISPATCH_PROPERTYPUT was %d instead of 1\n", pDispParams->cArgs);
1480       return DISP_E_BADPARAMCOUNT;
1481     }
1482   }
1483   else
1484   {
1485     ERR("one of DISPATCH_PROPERTYGET or DISPATCH_PROPERTYPUT must be specified\n");
1486     return DISP_E_MEMBERNOTFOUND;
1487   }
1488
1489   switch (dispIdMember) {
1490   case DISPID_FONT_NAME:
1491     if (wFlags & DISPATCH_PROPERTYGET) {
1492       V_VT(pVarResult) = VT_BSTR;
1493       return IFont_get_Name(&this->IFont_iface, &V_BSTR(pVarResult));
1494     } else {
1495       VARIANTARG vararg;
1496
1497       VariantInit(&vararg);
1498       hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_BSTR);
1499       if (FAILED(hr))
1500         return hr;
1501
1502       hr = IFont_put_Name(&this->IFont_iface, V_BSTR(&vararg));
1503
1504       VariantClear(&vararg);
1505       return hr;
1506     }
1507     break;
1508   case DISPID_FONT_BOLD:
1509     if (wFlags & DISPATCH_PROPERTYGET) {
1510       BOOL value;
1511       hr = IFont_get_Bold(&this->IFont_iface, &value);
1512       V_VT(pVarResult) = VT_BOOL;
1513       V_BOOL(pVarResult) = value ? VARIANT_TRUE : VARIANT_FALSE;
1514       return hr;
1515     } else {
1516       VARIANTARG vararg;
1517
1518       VariantInit(&vararg);
1519       hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_BOOL);
1520       if (FAILED(hr))
1521         return hr;
1522
1523       hr = IFont_put_Bold(&this->IFont_iface, V_BOOL(&vararg));
1524
1525       VariantClear(&vararg);
1526       return hr;
1527     }
1528     break;
1529   case DISPID_FONT_ITALIC:
1530     if (wFlags & DISPATCH_PROPERTYGET) {
1531       BOOL value;
1532       hr = IFont_get_Italic(&this->IFont_iface, &value);
1533       V_VT(pVarResult) = VT_BOOL;
1534       V_BOOL(pVarResult) = value ? VARIANT_TRUE : VARIANT_FALSE;
1535       return hr;
1536     } else {
1537       VARIANTARG vararg;
1538
1539       VariantInit(&vararg);
1540       hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_BOOL);
1541       if (FAILED(hr))
1542         return hr;
1543
1544       hr = IFont_put_Italic(&this->IFont_iface, V_BOOL(&vararg));
1545
1546       VariantClear(&vararg);
1547       return hr;
1548     }
1549     break;
1550   case DISPID_FONT_UNDER:
1551     if (wFlags & DISPATCH_PROPERTYGET) {
1552       BOOL value;
1553       hr = IFont_get_Underline(&this->IFont_iface, &value);
1554       V_VT(pVarResult) = VT_BOOL;
1555       V_BOOL(pVarResult) = value ? VARIANT_TRUE : VARIANT_FALSE;
1556       return hr;
1557     } else {
1558       VARIANTARG vararg;
1559
1560       VariantInit(&vararg);
1561       hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_BOOL);
1562       if (FAILED(hr))
1563         return hr;
1564
1565       hr = IFont_put_Underline(&this->IFont_iface, V_BOOL(&vararg));
1566
1567       VariantClear(&vararg);
1568       return hr;
1569     }
1570     break;
1571   case DISPID_FONT_STRIKE:
1572     if (wFlags & DISPATCH_PROPERTYGET) {
1573       BOOL value;
1574       hr = IFont_get_Strikethrough(&this->IFont_iface, &value);
1575       V_VT(pVarResult) = VT_BOOL;
1576       V_BOOL(pVarResult) = value ? VARIANT_TRUE : VARIANT_FALSE;
1577       return hr;
1578     } else {
1579       VARIANTARG vararg;
1580
1581       VariantInit(&vararg);
1582       hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_BOOL);
1583       if (FAILED(hr))
1584         return hr;
1585
1586       hr = IFont_put_Strikethrough(&this->IFont_iface, V_BOOL(&vararg));
1587
1588       VariantClear(&vararg);
1589       return hr;
1590     }
1591     break;
1592   case DISPID_FONT_SIZE:
1593     if (wFlags & DISPATCH_PROPERTYGET) {
1594       V_VT(pVarResult) = VT_CY;
1595       return OLEFontImpl_get_Size(&this->IFont_iface, &V_CY(pVarResult));
1596     } else {
1597       VARIANTARG vararg;
1598
1599       VariantInit(&vararg);
1600       hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_CY);
1601       if (FAILED(hr))
1602         return hr;
1603
1604       hr = IFont_put_Size(&this->IFont_iface, V_CY(&vararg));
1605
1606       VariantClear(&vararg);
1607       return hr;
1608     }
1609     break;
1610   case DISPID_FONT_WEIGHT:
1611     if (wFlags & DISPATCH_PROPERTYGET) {
1612       V_VT(pVarResult) = VT_I2;
1613       return OLEFontImpl_get_Weight(&this->IFont_iface, &V_I2(pVarResult));
1614     } else {
1615       VARIANTARG vararg;
1616
1617       VariantInit(&vararg);
1618       hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_I2);
1619       if (FAILED(hr))
1620         return hr;
1621
1622       hr = IFont_put_Weight(&this->IFont_iface, V_I2(&vararg));
1623
1624       VariantClear(&vararg);
1625       return hr;
1626     }
1627     break;
1628   case DISPID_FONT_CHARSET:
1629     if (wFlags & DISPATCH_PROPERTYGET) {
1630       V_VT(pVarResult) = VT_I2;
1631       return OLEFontImpl_get_Charset(&this->IFont_iface, &V_I2(pVarResult));
1632     } else {
1633       VARIANTARG vararg;
1634
1635       VariantInit(&vararg);
1636       hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_I2);
1637       if (FAILED(hr))
1638         return hr;
1639
1640       hr = IFont_put_Charset(&this->IFont_iface, V_I2(&vararg));
1641
1642       VariantClear(&vararg);
1643       return hr;
1644     }
1645     break;
1646   default:
1647     ERR("member not found for dispid 0x%x\n", dispIdMember);
1648     return DISP_E_MEMBERNOTFOUND;
1649   }
1650 }
1651
1652 static const IDispatchVtbl OLEFontImpl_IDispatch_VTable =
1653 {
1654   OLEFontImpl_IDispatch_QueryInterface,
1655   OLEFontImpl_IDispatch_AddRef,
1656   OLEFontImpl_IDispatch_Release,
1657   OLEFontImpl_GetTypeInfoCount,
1658   OLEFontImpl_GetTypeInfo,
1659   OLEFontImpl_GetIDsOfNames,
1660   OLEFontImpl_Invoke
1661 };
1662
1663 /************************************************************************
1664  * OLEFontImpl_IPersistStream_QueryInterface (IUnknown)
1665  *
1666  * See Windows documentation for more details on IUnknown methods.
1667  */
1668 static HRESULT WINAPI OLEFontImpl_IPersistStream_QueryInterface(
1669   IPersistStream* iface,
1670   REFIID     riid,
1671   VOID**     ppvoid)
1672 {
1673   OLEFontImpl *this = impl_from_IPersistStream(iface);
1674
1675   return IFont_QueryInterface(&this->IFont_iface, riid, ppvoid);
1676 }
1677
1678 /************************************************************************
1679  * OLEFontImpl_IPersistStream_Release (IUnknown)
1680  *
1681  * See Windows documentation for more details on IUnknown methods.
1682  */
1683 static ULONG WINAPI OLEFontImpl_IPersistStream_Release(
1684   IPersistStream* iface)
1685 {
1686   OLEFontImpl *this = impl_from_IPersistStream(iface);
1687
1688   return IFont_Release(&this->IFont_iface);
1689 }
1690
1691 /************************************************************************
1692  * OLEFontImpl_IPersistStream_AddRef (IUnknown)
1693  *
1694  * See Windows documentation for more details on IUnknown methods.
1695  */
1696 static ULONG WINAPI OLEFontImpl_IPersistStream_AddRef(
1697   IPersistStream* iface)
1698 {
1699   OLEFontImpl *this = impl_from_IPersistStream(iface);
1700
1701   return IFont_AddRef(&this->IFont_iface);
1702 }
1703
1704 /************************************************************************
1705  * OLEFontImpl_GetClassID (IPersistStream)
1706  *
1707  * See Windows documentation for more details on IPersistStream methods.
1708  */
1709 static HRESULT WINAPI OLEFontImpl_GetClassID(
1710   IPersistStream* iface,
1711   CLSID*                pClassID)
1712 {
1713   TRACE("(%p,%p)\n",iface,pClassID);
1714   if (pClassID==0)
1715     return E_POINTER;
1716
1717   *pClassID = CLSID_StdFont;
1718
1719   return S_OK;
1720 }
1721
1722 /************************************************************************
1723  * OLEFontImpl_IsDirty (IPersistStream)
1724  *
1725  * See Windows documentation for more details on IPersistStream methods.
1726  */
1727 static HRESULT WINAPI OLEFontImpl_IsDirty(
1728   IPersistStream*  iface)
1729 {
1730   TRACE("(%p)\n",iface);
1731   return S_OK;
1732 }
1733
1734 /************************************************************************
1735  * OLEFontImpl_Load (IPersistStream)
1736  *
1737  * See Windows documentation for more details on IPersistStream methods.
1738  *
1739  * This is the format of the standard font serialization as far as I
1740  * know
1741  *
1742  * Offset   Type   Value           Comment
1743  * 0x0000   Byte   Unknown         Probably a version number, contains 0x01
1744  * 0x0001   Short  Charset         Charset value from the FONTDESC structure
1745  * 0x0003   Byte   Attributes      Flags defined as follows:
1746  *                                     00000010 - Italic
1747  *                                     00000100 - Underline
1748  *                                     00001000 - Strikethrough
1749  * 0x0004   Short  Weight          Weight value from FONTDESC structure
1750  * 0x0006   DWORD  size            "Low" portion of the cySize member of the FONTDESC
1751  *                                 structure/
1752  * 0x000A   Byte   name length     Length of the font name string (no null character)
1753  * 0x000B   String name            Name of the font (ASCII, no nul character)
1754  */
1755 static HRESULT WINAPI OLEFontImpl_Load(
1756   IPersistStream*  iface,
1757   IStream*         pLoadStream)
1758 {
1759   char  readBuffer[0x100];
1760   ULONG cbRead;
1761   BYTE  bVersion;
1762   BYTE  bAttributes;
1763   BYTE  bStringSize;
1764   INT len;
1765
1766   OLEFontImpl *this = impl_from_IPersistStream(iface);
1767
1768   /*
1769    * Read the version byte
1770    */
1771   IStream_Read(pLoadStream, &bVersion, 1, &cbRead);
1772
1773   if ( (cbRead!=1) ||
1774        (bVersion!=0x01) )
1775     return E_FAIL;
1776
1777   /*
1778    * Charset
1779    */
1780   IStream_Read(pLoadStream, &this->description.sCharset, 2, &cbRead);
1781
1782   if (cbRead!=2)
1783     return E_FAIL;
1784
1785   /*
1786    * Attributes
1787    */
1788   IStream_Read(pLoadStream, &bAttributes, 1, &cbRead);
1789
1790   if (cbRead!=1)
1791     return E_FAIL;
1792
1793   this->description.fItalic        = (bAttributes & FONTPERSIST_ITALIC) != 0;
1794   this->description.fStrikethrough = (bAttributes & FONTPERSIST_STRIKETHROUGH) != 0;
1795   this->description.fUnderline     = (bAttributes & FONTPERSIST_UNDERLINE) != 0;
1796
1797   /*
1798    * Weight
1799    */
1800   IStream_Read(pLoadStream, &this->description.sWeight, 2, &cbRead);
1801
1802   if (cbRead!=2)
1803     return E_FAIL;
1804
1805   /*
1806    * Size
1807    */
1808   IStream_Read(pLoadStream, &this->description.cySize.s.Lo, 4, &cbRead);
1809
1810   if (cbRead!=4)
1811     return E_FAIL;
1812
1813   this->description.cySize.s.Hi = 0;
1814
1815   /*
1816    * FontName
1817    */
1818   IStream_Read(pLoadStream, &bStringSize, 1, &cbRead);
1819
1820   if (cbRead!=1)
1821     return E_FAIL;
1822
1823   IStream_Read(pLoadStream, readBuffer, bStringSize, &cbRead);
1824
1825   if (cbRead!=bStringSize)
1826     return E_FAIL;
1827
1828   HeapFree(GetProcessHeap(), 0, this->description.lpstrName);
1829
1830   len = MultiByteToWideChar( CP_ACP, 0, readBuffer, bStringSize, NULL, 0 );
1831   this->description.lpstrName = HeapAlloc( GetProcessHeap(), 0, (len+1) * sizeof(WCHAR) );
1832   MultiByteToWideChar( CP_ACP, 0, readBuffer, bStringSize, this->description.lpstrName, len );
1833   this->description.lpstrName[len] = 0;
1834
1835   /* Ensure use of this font causes a new one to be created */
1836   dec_int_ref(this->gdiFont);
1837   this->gdiFont = 0;
1838   this->dirty = TRUE;
1839
1840   return S_OK;
1841 }
1842
1843 /************************************************************************
1844  * OLEFontImpl_Save (IPersistStream)
1845  *
1846  * See Windows documentation for more details on IPersistStream methods.
1847  */
1848 static HRESULT WINAPI OLEFontImpl_Save(
1849   IPersistStream*  iface,
1850   IStream*         pOutStream,
1851   BOOL             fClearDirty)
1852 {
1853   char* writeBuffer = NULL;
1854   ULONG cbWritten;
1855   BYTE  bVersion = 0x01;
1856   BYTE  bAttributes;
1857   BYTE  bStringSize;
1858
1859   OLEFontImpl *this = impl_from_IPersistStream(iface);
1860
1861   /*
1862    * Read the version byte
1863    */
1864   IStream_Write(pOutStream, &bVersion, 1, &cbWritten);
1865
1866   if (cbWritten!=1)
1867     return E_FAIL;
1868
1869   /*
1870    * Charset
1871    */
1872   IStream_Write(pOutStream, &this->description.sCharset, 2, &cbWritten);
1873
1874   if (cbWritten!=2)
1875     return E_FAIL;
1876
1877   /*
1878    * Attributes
1879    */
1880   bAttributes = 0;
1881
1882   if (this->description.fItalic)
1883     bAttributes |= FONTPERSIST_ITALIC;
1884
1885   if (this->description.fStrikethrough)
1886     bAttributes |= FONTPERSIST_STRIKETHROUGH;
1887
1888   if (this->description.fUnderline)
1889     bAttributes |= FONTPERSIST_UNDERLINE;
1890
1891   IStream_Write(pOutStream, &bAttributes, 1, &cbWritten);
1892
1893   if (cbWritten!=1)
1894     return E_FAIL;
1895
1896   /*
1897    * Weight
1898    */
1899   IStream_Write(pOutStream, &this->description.sWeight, 2, &cbWritten);
1900
1901   if (cbWritten!=2)
1902     return E_FAIL;
1903
1904   /*
1905    * Size
1906    */
1907   IStream_Write(pOutStream, &this->description.cySize.s.Lo, 4, &cbWritten);
1908
1909   if (cbWritten!=4)
1910     return E_FAIL;
1911
1912   /*
1913    * FontName
1914    */
1915   if (this->description.lpstrName!=0)
1916     bStringSize = WideCharToMultiByte( CP_ACP, 0, this->description.lpstrName,
1917                                        strlenW(this->description.lpstrName), NULL, 0, NULL, NULL );
1918   else
1919     bStringSize = 0;
1920
1921   IStream_Write(pOutStream, &bStringSize, 1, &cbWritten);
1922
1923   if (cbWritten!=1)
1924     return E_FAIL;
1925
1926   if (bStringSize!=0)
1927   {
1928       if (!(writeBuffer = HeapAlloc( GetProcessHeap(), 0, bStringSize ))) return E_OUTOFMEMORY;
1929       WideCharToMultiByte( CP_ACP, 0, this->description.lpstrName,
1930                            strlenW(this->description.lpstrName),
1931                            writeBuffer, bStringSize, NULL, NULL );
1932
1933     IStream_Write(pOutStream, writeBuffer, bStringSize, &cbWritten);
1934     HeapFree(GetProcessHeap(), 0, writeBuffer);
1935
1936     if (cbWritten!=bStringSize)
1937       return E_FAIL;
1938   }
1939
1940   return S_OK;
1941 }
1942
1943 /************************************************************************
1944  * OLEFontImpl_GetSizeMax (IPersistStream)
1945  *
1946  * See Windows documentation for more details on IPersistStream methods.
1947  */
1948 static HRESULT WINAPI OLEFontImpl_GetSizeMax(
1949   IPersistStream*  iface,
1950   ULARGE_INTEGER*  pcbSize)
1951 {
1952   OLEFontImpl *this = impl_from_IPersistStream(iface);
1953
1954   if (pcbSize==NULL)
1955     return E_POINTER;
1956
1957   pcbSize->u.HighPart = 0;
1958   pcbSize->u.LowPart = 0;
1959
1960   pcbSize->u.LowPart += sizeof(BYTE);  /* Version */
1961   pcbSize->u.LowPart += sizeof(WORD);  /* Lang code */
1962   pcbSize->u.LowPart += sizeof(BYTE);  /* Flags */
1963   pcbSize->u.LowPart += sizeof(WORD);  /* Weight */
1964   pcbSize->u.LowPart += sizeof(DWORD); /* Size */
1965   pcbSize->u.LowPart += sizeof(BYTE);  /* StrLength */
1966
1967   if (this->description.lpstrName!=0)
1968       pcbSize->u.LowPart += WideCharToMultiByte( CP_ACP, 0, this->description.lpstrName,
1969                                                  strlenW(this->description.lpstrName),
1970                                                  NULL, 0, NULL, NULL );
1971
1972   return S_OK;
1973 }
1974
1975 static const IPersistStreamVtbl OLEFontImpl_IPersistStream_VTable =
1976 {
1977   OLEFontImpl_IPersistStream_QueryInterface,
1978   OLEFontImpl_IPersistStream_AddRef,
1979   OLEFontImpl_IPersistStream_Release,
1980   OLEFontImpl_GetClassID,
1981   OLEFontImpl_IsDirty,
1982   OLEFontImpl_Load,
1983   OLEFontImpl_Save,
1984   OLEFontImpl_GetSizeMax
1985 };
1986
1987 /************************************************************************
1988  * OLEFontImpl_IConnectionPointContainer_QueryInterface (IUnknown)
1989  *
1990  * See Windows documentation for more details on IUnknown methods.
1991  */
1992 static HRESULT WINAPI OLEFontImpl_IConnectionPointContainer_QueryInterface(
1993   IConnectionPointContainer* iface,
1994   REFIID     riid,
1995   VOID**     ppvoid)
1996 {
1997   OLEFontImpl *this = impl_from_IConnectionPointContainer(iface);
1998
1999   return IFont_QueryInterface(&this->IFont_iface, riid, ppvoid);
2000 }
2001
2002 /************************************************************************
2003  * OLEFontImpl_IConnectionPointContainer_Release (IUnknown)
2004  *
2005  * See Windows documentation for more details on IUnknown methods.
2006  */
2007 static ULONG WINAPI OLEFontImpl_IConnectionPointContainer_Release(
2008   IConnectionPointContainer* iface)
2009 {
2010   OLEFontImpl *this = impl_from_IConnectionPointContainer(iface);
2011
2012   return IFont_Release(&this->IFont_iface);
2013 }
2014
2015 /************************************************************************
2016  * OLEFontImpl_IConnectionPointContainer_AddRef (IUnknown)
2017  *
2018  * See Windows documentation for more details on IUnknown methods.
2019  */
2020 static ULONG WINAPI OLEFontImpl_IConnectionPointContainer_AddRef(
2021   IConnectionPointContainer* iface)
2022 {
2023   OLEFontImpl *this = impl_from_IConnectionPointContainer(iface);
2024
2025   return IFont_AddRef(&this->IFont_iface);
2026 }
2027
2028 /************************************************************************
2029  * OLEFontImpl_EnumConnectionPoints (IConnectionPointContainer)
2030  *
2031  * See Windows documentation for more details on IConnectionPointContainer
2032  * methods.
2033  */
2034 static HRESULT WINAPI OLEFontImpl_EnumConnectionPoints(
2035   IConnectionPointContainer* iface,
2036   IEnumConnectionPoints **ppEnum)
2037 {
2038   OLEFontImpl *this = impl_from_IConnectionPointContainer(iface);
2039
2040   FIXME("(%p)->(%p): stub\n", this, ppEnum);
2041   return E_NOTIMPL;
2042 }
2043
2044 /************************************************************************
2045  * OLEFontImpl_FindConnectionPoint (IConnectionPointContainer)
2046  *
2047  * See Windows documentation for more details on IConnectionPointContainer
2048  * methods.
2049  */
2050 static HRESULT WINAPI OLEFontImpl_FindConnectionPoint(
2051    IConnectionPointContainer* iface,
2052    REFIID riid,
2053    IConnectionPoint **ppCp)
2054 {
2055   OLEFontImpl *this = impl_from_IConnectionPointContainer(iface);
2056   TRACE("(%p)->(%s, %p)\n", this, debugstr_guid(riid), ppCp);
2057
2058   if(IsEqualIID(riid, &IID_IPropertyNotifySink)) {
2059     return IConnectionPoint_QueryInterface(this->pPropertyNotifyCP,
2060                                            &IID_IConnectionPoint,
2061                                            (LPVOID)ppCp);
2062   } else if(IsEqualIID(riid, &IID_IFontEventsDisp)) {
2063     return IConnectionPoint_QueryInterface(this->pFontEventsCP,
2064                                            &IID_IConnectionPoint,
2065                                            (LPVOID)ppCp);
2066   } else {
2067     FIXME("no connection point for %s\n", debugstr_guid(riid));
2068     return CONNECT_E_NOCONNECTION;
2069   }
2070 }
2071
2072 static const IConnectionPointContainerVtbl
2073      OLEFontImpl_IConnectionPointContainer_VTable =
2074 {
2075   OLEFontImpl_IConnectionPointContainer_QueryInterface,
2076   OLEFontImpl_IConnectionPointContainer_AddRef,
2077   OLEFontImpl_IConnectionPointContainer_Release,
2078   OLEFontImpl_EnumConnectionPoints,
2079   OLEFontImpl_FindConnectionPoint
2080 };
2081
2082 /************************************************************************
2083  * OLEFontImpl implementation of IPersistPropertyBag.
2084  */
2085 static HRESULT WINAPI OLEFontImpl_IPersistPropertyBag_QueryInterface(
2086    IPersistPropertyBag *iface, REFIID riid, LPVOID *ppvObj
2087 ) {
2088   OLEFontImpl *this = impl_from_IPersistPropertyBag(iface);
2089   return IFont_QueryInterface(&this->IFont_iface,riid,ppvObj);
2090 }
2091
2092 static ULONG WINAPI OLEFontImpl_IPersistPropertyBag_AddRef(
2093    IPersistPropertyBag *iface
2094 ) {
2095   OLEFontImpl *this = impl_from_IPersistPropertyBag(iface);
2096   return IFont_AddRef(&this->IFont_iface);
2097 }
2098
2099 static ULONG WINAPI OLEFontImpl_IPersistPropertyBag_Release(
2100    IPersistPropertyBag *iface
2101 ) {
2102   OLEFontImpl *this = impl_from_IPersistPropertyBag(iface);
2103   return IFont_Release(&this->IFont_iface);
2104 }
2105
2106 static HRESULT WINAPI OLEFontImpl_IPersistPropertyBag_GetClassID(
2107    IPersistPropertyBag *iface, CLSID *classid
2108 ) {
2109   FIXME("(%p,%p), stub!\n", iface, classid);
2110   return E_FAIL;
2111 }
2112
2113 static HRESULT WINAPI OLEFontImpl_IPersistPropertyBag_InitNew(
2114    IPersistPropertyBag *iface
2115 ) {
2116   FIXME("(%p), stub!\n", iface);
2117   return S_OK;
2118 }
2119
2120 static HRESULT WINAPI OLEFontImpl_IPersistPropertyBag_Load(
2121    IPersistPropertyBag *iface, IPropertyBag* pPropBag, IErrorLog* pErrorLog
2122 ) {
2123 /* (from Visual Basic 6 property bag)
2124          Name            =   "MS Sans Serif"
2125          Size            =   13.8
2126          Charset         =   0
2127          Weight          =   400
2128          Underline       =   0   'False
2129          Italic          =   0   'False
2130          Strikethrough   =   0   'False
2131 */
2132     static const WCHAR sAttrName[] = {'N','a','m','e',0};
2133     static const WCHAR sAttrSize[] = {'S','i','z','e',0};
2134     static const WCHAR sAttrCharset[] = {'C','h','a','r','s','e','t',0};
2135     static const WCHAR sAttrWeight[] = {'W','e','i','g','h','t',0};
2136     static const WCHAR sAttrUnderline[] = {'U','n','d','e','r','l','i','n','e',0};
2137     static const WCHAR sAttrItalic[] = {'I','t','a','l','i','c',0};
2138     static const WCHAR sAttrStrikethrough[] = {'S','t','r','i','k','e','t','h','r','o','u','g','h',0};
2139     OLEFontImpl *this = impl_from_IPersistPropertyBag(iface);
2140     VARIANT value;
2141     HRESULT iRes;
2142
2143     VariantInit(&value);
2144
2145     iRes = IPropertyBag_Read(pPropBag, sAttrName, &value, pErrorLog);
2146     if (iRes == S_OK)
2147     {
2148         iRes = VariantChangeType(&value, &value, 0, VT_BSTR);
2149         if (iRes == S_OK)
2150             iRes = IFont_put_Name(&this->IFont_iface, V_BSTR(&value));
2151     }
2152     else if (iRes == E_INVALIDARG)
2153         iRes = S_OK;
2154
2155     VariantClear(&value);
2156
2157     if (iRes == S_OK) {
2158         iRes = IPropertyBag_Read(pPropBag, sAttrSize, &value, pErrorLog);
2159         if (iRes == S_OK)
2160         {
2161             iRes = VariantChangeType(&value, &value, 0, VT_CY);
2162             if (iRes == S_OK)
2163                 iRes = IFont_put_Size(&this->IFont_iface, V_CY(&value));
2164         }
2165         else if (iRes == E_INVALIDARG)
2166             iRes = S_OK;
2167
2168         VariantClear(&value);
2169     }
2170
2171     if (iRes == S_OK) {
2172         iRes = IPropertyBag_Read(pPropBag, sAttrCharset, &value, pErrorLog);
2173         if (iRes == S_OK)
2174         {
2175             iRes = VariantChangeType(&value, &value, 0, VT_I2);
2176             if (iRes == S_OK)
2177                 iRes = IFont_put_Charset(&this->IFont_iface, V_I2(&value));
2178         }
2179         else if (iRes == E_INVALIDARG)
2180             iRes = S_OK;
2181
2182         VariantClear(&value);
2183     }
2184
2185     if (iRes == S_OK) {
2186         iRes = IPropertyBag_Read(pPropBag, sAttrWeight, &value, pErrorLog);
2187         if (iRes == S_OK)
2188         {
2189             iRes = VariantChangeType(&value, &value, 0, VT_I2);
2190             if (iRes == S_OK)
2191                 iRes = IFont_put_Weight(&this->IFont_iface, V_I2(&value));
2192         }
2193         else if (iRes == E_INVALIDARG)
2194             iRes = S_OK;
2195
2196         VariantClear(&value);
2197     }
2198
2199     if (iRes == S_OK) {
2200         iRes = IPropertyBag_Read(pPropBag, sAttrUnderline, &value, pErrorLog);
2201         if (iRes == S_OK)
2202         {
2203             iRes = VariantChangeType(&value, &value, 0, VT_BOOL);
2204             if (iRes == S_OK)
2205                 iRes = IFont_put_Underline(&this->IFont_iface, V_BOOL(&value));
2206         }
2207         else if (iRes == E_INVALIDARG)
2208             iRes = S_OK;
2209
2210         VariantClear(&value);
2211     }
2212
2213     if (iRes == S_OK) {
2214         iRes = IPropertyBag_Read(pPropBag, sAttrItalic, &value, pErrorLog);
2215         if (iRes == S_OK)
2216         {
2217             iRes = VariantChangeType(&value, &value, 0, VT_BOOL);
2218             if (iRes == S_OK)
2219                 iRes = IFont_put_Italic(&this->IFont_iface, V_BOOL(&value));
2220         }
2221         else if (iRes == E_INVALIDARG)
2222             iRes = S_OK;
2223
2224         VariantClear(&value);
2225     }
2226
2227     if (iRes == S_OK) {
2228         iRes = IPropertyBag_Read(pPropBag, sAttrStrikethrough, &value, pErrorLog);
2229         if (iRes == S_OK)
2230         {
2231             iRes = VariantChangeType(&value, &value, 0, VT_BOOL);
2232             if (iRes == S_OK)
2233                 IFont_put_Strikethrough(&this->IFont_iface, V_BOOL(&value));
2234         }
2235         else if (iRes == E_INVALIDARG)
2236             iRes = S_OK;
2237
2238         VariantClear(&value);
2239     }
2240
2241     if (FAILED(iRes))
2242         WARN("-- 0x%08x\n", iRes);
2243     return iRes;
2244 }
2245
2246 static HRESULT WINAPI OLEFontImpl_IPersistPropertyBag_Save(
2247    IPersistPropertyBag *iface, IPropertyBag* pPropBag, BOOL fClearDirty,
2248    BOOL fSaveAllProperties
2249 ) {
2250   FIXME("(%p,%p,%d,%d), stub!\n", iface, pPropBag, fClearDirty, fSaveAllProperties);
2251   return E_FAIL;
2252 }
2253
2254 static const IPersistPropertyBagVtbl OLEFontImpl_IPersistPropertyBag_VTable = 
2255 {
2256   OLEFontImpl_IPersistPropertyBag_QueryInterface,
2257   OLEFontImpl_IPersistPropertyBag_AddRef,
2258   OLEFontImpl_IPersistPropertyBag_Release,
2259
2260   OLEFontImpl_IPersistPropertyBag_GetClassID,
2261   OLEFontImpl_IPersistPropertyBag_InitNew,
2262   OLEFontImpl_IPersistPropertyBag_Load,
2263   OLEFontImpl_IPersistPropertyBag_Save
2264 };
2265
2266 /************************************************************************
2267  * OLEFontImpl implementation of IPersistStreamInit.
2268  */
2269 static HRESULT WINAPI OLEFontImpl_IPersistStreamInit_QueryInterface(
2270    IPersistStreamInit *iface, REFIID riid, LPVOID *ppvObj
2271 ) {
2272   OLEFontImpl *this = impl_from_IPersistStreamInit(iface);
2273   return IFont_QueryInterface(&this->IFont_iface,riid,ppvObj);
2274 }
2275
2276 static ULONG WINAPI OLEFontImpl_IPersistStreamInit_AddRef(
2277    IPersistStreamInit *iface
2278 ) {
2279   OLEFontImpl *this = impl_from_IPersistStreamInit(iface);
2280   return IFont_AddRef(&this->IFont_iface);
2281 }
2282
2283 static ULONG WINAPI OLEFontImpl_IPersistStreamInit_Release(
2284    IPersistStreamInit *iface
2285 ) {
2286   OLEFontImpl *this = impl_from_IPersistStreamInit(iface);
2287   return IFont_Release(&this->IFont_iface);
2288 }
2289
2290 static HRESULT WINAPI OLEFontImpl_IPersistStreamInit_GetClassID(
2291    IPersistStreamInit *iface, CLSID *classid
2292 ) {
2293   FIXME("(%p,%p), stub!\n", iface, classid);
2294   return E_FAIL;
2295 }
2296
2297 static HRESULT WINAPI OLEFontImpl_IPersistStreamInit_IsDirty(
2298    IPersistStreamInit *iface
2299 ) {
2300   FIXME("(%p), stub!\n", iface);
2301   return E_FAIL;
2302 }
2303
2304 static HRESULT WINAPI OLEFontImpl_IPersistStreamInit_Load(
2305    IPersistStreamInit *iface, LPSTREAM pStm
2306 ) {
2307   FIXME("(%p,%p), stub!\n", iface, pStm);
2308   return E_FAIL;
2309 }
2310
2311 static HRESULT WINAPI OLEFontImpl_IPersistStreamInit_Save(
2312    IPersistStreamInit *iface, LPSTREAM pStm, BOOL fClearDirty
2313 ) {
2314   FIXME("(%p,%p,%d), stub!\n", iface, pStm, fClearDirty);
2315   return E_FAIL;
2316 }
2317
2318 static HRESULT WINAPI OLEFontImpl_IPersistStreamInit_GetSizeMax(
2319    IPersistStreamInit *iface, ULARGE_INTEGER *pcbSize
2320 ) {
2321   FIXME("(%p,%p), stub!\n", iface, pcbSize);
2322   return E_FAIL;
2323 }
2324
2325 static HRESULT WINAPI OLEFontImpl_IPersistStreamInit_InitNew(
2326    IPersistStreamInit *iface
2327 ) {
2328   FIXME("(%p), stub!\n", iface);
2329   return S_OK;
2330 }
2331
2332 static const IPersistStreamInitVtbl OLEFontImpl_IPersistStreamInit_VTable = 
2333 {
2334   OLEFontImpl_IPersistStreamInit_QueryInterface,
2335   OLEFontImpl_IPersistStreamInit_AddRef,
2336   OLEFontImpl_IPersistStreamInit_Release,
2337
2338   OLEFontImpl_IPersistStreamInit_GetClassID,
2339   OLEFontImpl_IPersistStreamInit_IsDirty,
2340   OLEFontImpl_IPersistStreamInit_Load,
2341   OLEFontImpl_IPersistStreamInit_Save,
2342   OLEFontImpl_IPersistStreamInit_GetSizeMax,
2343   OLEFontImpl_IPersistStreamInit_InitNew
2344 };
2345
2346 /************************************************************************
2347  * OLEFontImpl_Construct
2348  *
2349  * This method will construct a new instance of the OLEFontImpl
2350  * class.
2351  *
2352  * The caller of this method must release the object when it's
2353  * done with it.
2354  */
2355 static OLEFontImpl* OLEFontImpl_Construct(const FONTDESC *fontDesc)
2356 {
2357   OLEFontImpl* newObject = 0;
2358
2359   /*
2360    * Allocate space for the object.
2361    */
2362   newObject = HeapAlloc(GetProcessHeap(), 0, sizeof(OLEFontImpl));
2363
2364   if (newObject==0)
2365     return newObject;
2366
2367   /*
2368    * Initialize the virtual function table.
2369    */
2370   newObject->IFont_iface.lpVtbl = &OLEFontImpl_VTable;
2371   newObject->IDispatch_iface.lpVtbl = &OLEFontImpl_IDispatch_VTable;
2372   newObject->IPersistStream_iface.lpVtbl = &OLEFontImpl_IPersistStream_VTable;
2373   newObject->IConnectionPointContainer_iface.lpVtbl = &OLEFontImpl_IConnectionPointContainer_VTable;
2374   newObject->IPersistPropertyBag_iface.lpVtbl = &OLEFontImpl_IPersistPropertyBag_VTable;
2375   newObject->IPersistStreamInit_iface.lpVtbl = &OLEFontImpl_IPersistStreamInit_VTable;
2376
2377   /*
2378    * Start with one reference count. The caller of this function
2379    * must release the interface pointer when it is done.
2380    */
2381   newObject->ref = 1;
2382
2383   /*
2384    * Copy the description of the font in the object.
2385    */
2386   assert(fontDesc->cbSizeofstruct >= sizeof(FONTDESC));
2387
2388   newObject->description.cbSizeofstruct = sizeof(FONTDESC);
2389   newObject->description.lpstrName = HeapAlloc(GetProcessHeap(),
2390                                                0,
2391                                                (lstrlenW(fontDesc->lpstrName)+1) * sizeof(WCHAR));
2392   strcpyW(newObject->description.lpstrName, fontDesc->lpstrName);
2393   newObject->description.cySize         = fontDesc->cySize;
2394   newObject->description.sWeight        = fontDesc->sWeight;
2395   newObject->description.sCharset       = fontDesc->sCharset;
2396   newObject->description.fItalic        = fontDesc->fItalic;
2397   newObject->description.fUnderline     = fontDesc->fUnderline;
2398   newObject->description.fStrikethrough = fontDesc->fStrikethrough;
2399
2400   /*
2401    * Initializing all the other members.
2402    */
2403   newObject->gdiFont  = 0;
2404   newObject->dirty = TRUE;
2405   newObject->cyLogical  = 72L;
2406   newObject->cyHimetric = 2540L;
2407   newObject->pPropertyNotifyCP = NULL;
2408   newObject->pFontEventsCP = NULL;
2409
2410   CreateConnectionPoint((IUnknown*)newObject, &IID_IPropertyNotifySink, &newObject->pPropertyNotifyCP);
2411   CreateConnectionPoint((IUnknown*)newObject, &IID_IFontEventsDisp, &newObject->pFontEventsCP);
2412
2413   if (!newObject->pPropertyNotifyCP || !newObject->pFontEventsCP)
2414   {
2415     OLEFontImpl_Destroy(newObject);
2416     return NULL;
2417   }
2418
2419   InterlockedIncrement(&ifont_cnt);
2420
2421   TRACE("returning %p\n", newObject);
2422   return newObject;
2423 }
2424
2425 /************************************************************************
2426  * OLEFontImpl_Destroy
2427  *
2428  * This method is called by the Release method when the reference
2429  * count goes down to 0. It will free all resources used by
2430  * this object.
2431  */
2432 static void OLEFontImpl_Destroy(OLEFontImpl* fontDesc)
2433 {
2434   TRACE("(%p)\n", fontDesc);
2435
2436   HeapFree(GetProcessHeap(), 0, fontDesc->description.lpstrName);
2437
2438   if (fontDesc->pPropertyNotifyCP)
2439       IConnectionPoint_Release(fontDesc->pPropertyNotifyCP);
2440   if (fontDesc->pFontEventsCP)
2441       IConnectionPoint_Release(fontDesc->pFontEventsCP);
2442
2443   HeapFree(GetProcessHeap(), 0, fontDesc);
2444 }
2445
2446 /*******************************************************************************
2447  * StdFont ClassFactory
2448  */
2449 typedef struct
2450 {
2451     /* IUnknown fields */
2452     IClassFactory IClassFactory_iface;
2453     LONG          ref;
2454 } IClassFactoryImpl;
2455
2456 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
2457 {
2458         return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
2459 }
2460
2461 static HRESULT WINAPI
2462 SFCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
2463         IClassFactoryImpl *This = impl_from_IClassFactory(iface);
2464
2465         FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
2466         return E_NOINTERFACE;
2467 }
2468
2469 static ULONG WINAPI
2470 SFCF_AddRef(LPCLASSFACTORY iface) {
2471         IClassFactoryImpl *This = impl_from_IClassFactory(iface);
2472         return InterlockedIncrement(&This->ref);
2473 }
2474
2475 static ULONG WINAPI SFCF_Release(LPCLASSFACTORY iface) {
2476         IClassFactoryImpl *This = impl_from_IClassFactory(iface);
2477         /* static class, won't be  freed */
2478         return InterlockedDecrement(&This->ref);
2479 }
2480
2481 static HRESULT WINAPI SFCF_CreateInstance(
2482         LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
2483 ) {
2484         return OleCreateFontIndirect(NULL,riid,ppobj);
2485
2486 }
2487
2488 static HRESULT WINAPI SFCF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
2489         IClassFactoryImpl *This = impl_from_IClassFactory(iface);
2490         FIXME("(%p)->(%d),stub!\n",This,dolock);
2491         return S_OK;
2492 }
2493
2494 static const IClassFactoryVtbl SFCF_Vtbl = {
2495         SFCF_QueryInterface,
2496         SFCF_AddRef,
2497         SFCF_Release,
2498         SFCF_CreateInstance,
2499         SFCF_LockServer
2500 };
2501 static IClassFactoryImpl STDFONT_CF = {{&SFCF_Vtbl}, 1 };
2502
2503 void _get_STDFONT_CF(LPVOID *ppv) { *ppv = &STDFONT_CF; }