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