oleaut32: Fix a crash in VB6.
[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 static 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 = this;
355   if (IsEqualGUID(&IID_IFont, riid))
356     *ppvObject = this;
357   if (IsEqualGUID(&IID_IDispatch, riid))
358     *ppvObject = &this->lpvtblIDispatch;
359   if (IsEqualGUID(&IID_IFontDisp, riid))
360     *ppvObject = &this->lpvtblIDispatch;
361   if (IsEqualIID(&IID_IPersist, riid) || IsEqualGUID(&IID_IPersistStream, riid))
362     *ppvObject = &this->lpvtblIPersistStream;
363   if (IsEqualGUID(&IID_IConnectionPointContainer, riid))
364     *ppvObject = &this->lpvtblIConnectionPointContainer;
365   if (IsEqualGUID(&IID_IPersistPropertyBag, riid))
366     *ppvObject = &this->lpvtblIPersistPropertyBag;
367   if (IsEqualGUID(&IID_IPersistStreamInit, riid))
368     *ppvObject = &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 static 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 static 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 (!name)
467     return CTL_E_INVALIDPROPERTYVALUE;
468
469   if (this->description.lpstrName==0)
470   {
471     this->description.lpstrName = HeapAlloc(GetProcessHeap(),
472                                             0,
473                                             (lstrlenW(name)+1) * sizeof(WCHAR));
474   }
475   else
476   {
477     this->description.lpstrName = HeapReAlloc(GetProcessHeap(),
478                                               0,
479                                               this->description.lpstrName,
480                                               (lstrlenW(name)+1) * sizeof(WCHAR));
481   }
482
483   if (this->description.lpstrName==0)
484     return E_OUTOFMEMORY;
485
486   strcpyW(this->description.lpstrName, name);
487   TRACE("new name %s\n", debugstr_w(this->description.lpstrName));
488   OLEFont_SendNotify(this, DISPID_FONT_NAME);
489   return S_OK;
490 }
491
492 /************************************************************************
493  * OLEFontImpl_get_Size (IFont)
494  *
495  * See Windows documentation for more details on IFont methods.
496  */
497 static HRESULT WINAPI OLEFontImpl_get_Size(
498   IFont* iface,
499   CY*    psize)
500 {
501   OLEFontImpl *this = (OLEFontImpl *)iface;
502   TRACE("(%p)->(%p)\n", this, psize);
503
504   /*
505    * Sanity check
506    */
507   if (psize==0)
508     return E_POINTER;
509
510   psize->s.Hi = 0;
511   psize->s.Lo = this->description.cySize.s.Lo;
512
513   return S_OK;
514 }
515
516 /************************************************************************
517  * OLEFontImpl_put_Size (IFont)
518  *
519  * See Windows documentation for more details on IFont methods.
520  */
521 static HRESULT WINAPI OLEFontImpl_put_Size(
522   IFont* iface,
523   CY     size)
524 {
525   OLEFontImpl *this = (OLEFontImpl *)iface;
526   TRACE("(%p)->(%d)\n", this, size.s.Lo);
527   this->description.cySize.s.Hi = 0;
528   this->description.cySize.s.Lo = size.s.Lo;
529   OLEFont_SendNotify(this, DISPID_FONT_SIZE);
530
531   return S_OK;
532 }
533
534 /************************************************************************
535  * OLEFontImpl_get_Bold (IFont)
536  *
537  * See Windows documentation for more details on IFont methods.
538  */
539 static HRESULT WINAPI OLEFontImpl_get_Bold(
540   IFont*  iface,
541   BOOL* pbold)
542 {
543   OLEFontImpl *this = (OLEFontImpl *)iface;
544   TRACE("(%p)->(%p)\n", this, pbold);
545   /*
546    * Sanity check
547    */
548   if (pbold==0)
549     return E_POINTER;
550
551   *pbold = this->description.sWeight > 550;
552
553   return S_OK;
554 }
555
556 /************************************************************************
557  * OLEFontImpl_put_Bold (IFont)
558  *
559  * See Windows documentation for more details on IFont methods.
560  */
561 static HRESULT WINAPI OLEFontImpl_put_Bold(
562   IFont* iface,
563   BOOL bold)
564 {
565   OLEFontImpl *this = (OLEFontImpl *)iface;
566   TRACE("(%p)->(%d)\n", this, bold);
567   this->description.sWeight = bold ? FW_BOLD : FW_NORMAL;
568   OLEFont_SendNotify(this, DISPID_FONT_BOLD);
569
570   return S_OK;
571 }
572
573 /************************************************************************
574  * OLEFontImpl_get_Italic (IFont)
575  *
576  * See Windows documentation for more details on IFont methods.
577  */
578 static HRESULT WINAPI OLEFontImpl_get_Italic(
579   IFont*  iface,
580   BOOL* pitalic)
581 {
582   OLEFontImpl *this = (OLEFontImpl *)iface;
583   TRACE("(%p)->(%p)\n", this, pitalic);
584   /*
585    * Sanity check
586    */
587   if (pitalic==0)
588     return E_POINTER;
589
590   *pitalic = this->description.fItalic;
591
592   return S_OK;
593 }
594
595 /************************************************************************
596  * OLEFontImpl_put_Italic (IFont)
597  *
598  * See Windows documentation for more details on IFont methods.
599  */
600 static HRESULT WINAPI OLEFontImpl_put_Italic(
601   IFont* iface,
602   BOOL italic)
603 {
604   OLEFontImpl *this = (OLEFontImpl *)iface;
605   TRACE("(%p)->(%d)\n", this, italic);
606
607   this->description.fItalic = italic;
608
609   OLEFont_SendNotify(this, DISPID_FONT_ITALIC);
610   return S_OK;
611 }
612
613 /************************************************************************
614  * OLEFontImpl_get_Underline (IFont)
615  *
616  * See Windows documentation for more details on IFont methods.
617  */
618 static HRESULT WINAPI OLEFontImpl_get_Underline(
619   IFont*  iface,
620   BOOL* punderline)
621 {
622   OLEFontImpl *this = (OLEFontImpl *)iface;
623   TRACE("(%p)->(%p)\n", this, punderline);
624
625   /*
626    * Sanity check
627    */
628   if (punderline==0)
629     return E_POINTER;
630
631   *punderline = this->description.fUnderline;
632
633   return S_OK;
634 }
635
636 /************************************************************************
637  * OLEFontImpl_put_Underline (IFont)
638  *
639  * See Windows documentation for more details on IFont methods.
640  */
641 static HRESULT WINAPI OLEFontImpl_put_Underline(
642   IFont* iface,
643   BOOL underline)
644 {
645   OLEFontImpl *this = (OLEFontImpl *)iface;
646   TRACE("(%p)->(%d)\n", this, underline);
647
648   this->description.fUnderline = underline;
649
650   OLEFont_SendNotify(this, DISPID_FONT_UNDER);
651   return S_OK;
652 }
653
654 /************************************************************************
655  * OLEFontImpl_get_Strikethrough (IFont)
656  *
657  * See Windows documentation for more details on IFont methods.
658  */
659 static HRESULT WINAPI OLEFontImpl_get_Strikethrough(
660   IFont*  iface,
661   BOOL* pstrikethrough)
662 {
663   OLEFontImpl *this = (OLEFontImpl *)iface;
664   TRACE("(%p)->(%p)\n", this, pstrikethrough);
665
666   /*
667    * Sanity check
668    */
669   if (pstrikethrough==0)
670     return E_POINTER;
671
672   *pstrikethrough = this->description.fStrikethrough;
673
674   return S_OK;
675 }
676
677 /************************************************************************
678  * OLEFontImpl_put_Strikethrough (IFont)
679  *
680  * See Windows documentation for more details on IFont methods.
681  */
682 static HRESULT WINAPI OLEFontImpl_put_Strikethrough(
683  IFont* iface,
684  BOOL strikethrough)
685 {
686   OLEFontImpl *this = (OLEFontImpl *)iface;
687   TRACE("(%p)->(%d)\n", this, strikethrough);
688
689   this->description.fStrikethrough = strikethrough;
690   OLEFont_SendNotify(this, DISPID_FONT_STRIKE);
691
692   return S_OK;
693 }
694
695 /************************************************************************
696  * OLEFontImpl_get_Weight (IFont)
697  *
698  * See Windows documentation for more details on IFont methods.
699  */
700 static HRESULT WINAPI OLEFontImpl_get_Weight(
701   IFont* iface,
702   short* pweight)
703 {
704   OLEFontImpl *this = (OLEFontImpl *)iface;
705   TRACE("(%p)->(%p)\n", this, pweight);
706
707   /*
708    * Sanity check
709    */
710   if (pweight==0)
711     return E_POINTER;
712
713   *pweight = this->description.sWeight;
714
715   return S_OK;
716 }
717
718 /************************************************************************
719  * OLEFontImpl_put_Weight (IFont)
720  *
721  * See Windows documentation for more details on IFont methods.
722  */
723 static HRESULT WINAPI OLEFontImpl_put_Weight(
724   IFont* iface,
725   short  weight)
726 {
727   OLEFontImpl *this = (OLEFontImpl *)iface;
728   TRACE("(%p)->(%d)\n", this, weight);
729
730   this->description.sWeight = weight;
731
732   OLEFont_SendNotify(this, DISPID_FONT_WEIGHT);
733   return S_OK;
734 }
735
736 /************************************************************************
737  * OLEFontImpl_get_Charset (IFont)
738  *
739  * See Windows documentation for more details on IFont methods.
740  */
741 static HRESULT WINAPI OLEFontImpl_get_Charset(
742   IFont* iface,
743   short* pcharset)
744 {
745   OLEFontImpl *this = (OLEFontImpl *)iface;
746   TRACE("(%p)->(%p)\n", this, pcharset);
747
748   /*
749    * Sanity check
750    */
751   if (pcharset==0)
752     return E_POINTER;
753
754   *pcharset = this->description.sCharset;
755
756   return S_OK;
757 }
758
759 /************************************************************************
760  * OLEFontImpl_put_Charset (IFont)
761  *
762  * See Windows documentation for more details on IFont methods.
763  */
764 static HRESULT WINAPI OLEFontImpl_put_Charset(
765   IFont* iface,
766   short charset)
767 {
768   OLEFontImpl *this = (OLEFontImpl *)iface;
769   TRACE("(%p)->(%d)\n", this, charset);
770
771   this->description.sCharset = charset;
772   OLEFont_SendNotify(this, DISPID_FONT_CHARSET);
773
774   return S_OK;
775 }
776
777 /************************************************************************
778  * OLEFontImpl_get_hFont (IFont)
779  *
780  * See Windows documentation for more details on IFont methods.
781  */
782 static HRESULT WINAPI OLEFontImpl_get_hFont(
783   IFont*   iface,
784   HFONT* phfont)
785 {
786   OLEFontImpl *this = (OLEFontImpl *)iface;
787   TRACE("(%p)->(%p)\n", this, phfont);
788   if (phfont==NULL)
789     return E_POINTER;
790
791   /*
792    * Realize the font if necessary
793  */
794   if (this->gdiFont==0)
795 {
796     LOGFONTW logFont;
797     INT      fontHeight;
798     CY       cySize;
799     PHFONTItem newEntry;
800
801     /*
802      * The height of the font returned by the get_Size property is the
803      * height of the font in points multiplied by 10000... Using some
804      * simple conversions and the ratio given by the application, it can
805      * be converted to a height in pixels.
806      */
807     IFont_get_Size(iface, &cySize);
808
809     /* Standard ratio is 72 / 2540, or 18 / 635 in lowest terms. */
810     /* Ratio is applied here relative to the standard. */
811     fontHeight = MulDiv( cySize.s.Lo, this->cyLogical*635, this->cyHimetric*18 );
812
813     memset(&logFont, 0, sizeof(LOGFONTW));
814
815     logFont.lfHeight          = ((fontHeight%10000L)>5000L) ?   (-fontHeight/10000L)-1 :
816                                                                 (-fontHeight/10000L);
817     logFont.lfItalic          = this->description.fItalic;
818     logFont.lfUnderline       = this->description.fUnderline;
819     logFont.lfStrikeOut       = this->description.fStrikethrough;
820     logFont.lfWeight          = this->description.sWeight;
821     logFont.lfCharSet         = this->description.sCharset;
822     logFont.lfOutPrecision    = OUT_CHARACTER_PRECIS;
823     logFont.lfClipPrecision   = CLIP_DEFAULT_PRECIS;
824     logFont.lfQuality         = DEFAULT_QUALITY;
825     logFont.lfPitchAndFamily  = DEFAULT_PITCH;
826     strcpyW(logFont.lfFaceName,this->description.lpstrName);
827
828     this->gdiFont = CreateFontIndirectW(&logFont);
829
830     /* Add font to the cache */
831     newEntry = HeapAlloc(GetProcessHeap(), 0, sizeof(HFONTItem));
832     newEntry->ref = 1;
833     newEntry->gdiFont = this->gdiFont;
834     EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
835     list_add_tail(&OLEFontImpl_hFontList,&newEntry->entry);
836     LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
837   }
838
839   *phfont = this->gdiFont;
840   TRACE("Returning %p\n", *phfont);
841   return S_OK;
842 }
843
844 /************************************************************************
845  * OLEFontImpl_Clone (IFont)
846  *
847  * See Windows documentation for more details on IFont methods.
848  */
849 static HRESULT WINAPI OLEFontImpl_Clone(
850   IFont*  iface,
851   IFont** ppfont)
852 {
853   OLEFontImpl* newObject = 0;
854   LOGFONTW logFont;
855   INT      fontHeight;
856   CY       cySize;
857   PHFONTItem newEntry;
858
859   OLEFontImpl *this = (OLEFontImpl *)iface;
860   TRACE("(%p)->(%p)\n", this, ppfont);
861
862   if (ppfont == NULL)
863     return E_POINTER;
864
865   *ppfont = NULL;
866
867   /*
868    * Allocate space for the object.
869    */
870   newObject = HeapAlloc(GetProcessHeap(), 0, sizeof(OLEFontImpl));
871
872   if (newObject==NULL)
873     return E_OUTOFMEMORY;
874
875   *newObject = *this;
876
877   /* We need to alloc new memory for the string, otherwise
878    * we free memory twice.
879    */
880   newObject->description.lpstrName = HeapAlloc(
881         GetProcessHeap(),0,
882         (1+strlenW(this->description.lpstrName))*2
883   );
884   strcpyW(newObject->description.lpstrName, this->description.lpstrName);
885   /* We need to clone the HFONT too. This is just cut & paste from above */
886   IFont_get_Size(iface, &cySize);
887
888   fontHeight = MulDiv(cySize.s.Lo, this->cyLogical*635,this->cyHimetric*18);
889
890   memset(&logFont, 0, sizeof(LOGFONTW));
891
892   logFont.lfHeight          = ((fontHeight%10000L)>5000L) ? (-fontHeight/10000L)-1 :
893                                                             (-fontHeight/10000L);
894   logFont.lfItalic          = this->description.fItalic;
895   logFont.lfUnderline       = this->description.fUnderline;
896   logFont.lfStrikeOut       = this->description.fStrikethrough;
897   logFont.lfWeight          = this->description.sWeight;
898   logFont.lfCharSet         = this->description.sCharset;
899   logFont.lfOutPrecision    = OUT_CHARACTER_PRECIS;
900   logFont.lfClipPrecision   = CLIP_DEFAULT_PRECIS;
901   logFont.lfQuality         = DEFAULT_QUALITY;
902   logFont.lfPitchAndFamily  = DEFAULT_PITCH;
903   strcpyW(logFont.lfFaceName,this->description.lpstrName);
904
905   newObject->gdiFont = CreateFontIndirectW(&logFont);
906
907   /* Add font to the cache */
908   InterlockedIncrement(&ifont_cnt);
909   newEntry = HeapAlloc(GetProcessHeap(), 0, sizeof(HFONTItem));
910   newEntry->ref = 1;
911   newEntry->gdiFont = newObject->gdiFont;
912   EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
913   list_add_tail(&OLEFontImpl_hFontList,&newEntry->entry);
914   LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
915
916   /* create new connection points */
917   newObject->pPropertyNotifyCP = NULL;
918   newObject->pFontEventsCP = NULL;
919   CreateConnectionPoint((IUnknown*)newObject, &IID_IPropertyNotifySink, &newObject->pPropertyNotifyCP);
920   CreateConnectionPoint((IUnknown*)newObject, &IID_IFontEventsDisp, &newObject->pFontEventsCP);
921
922   if (!newObject->pPropertyNotifyCP || !newObject->pFontEventsCP)
923   {
924     OLEFontImpl_Destroy(newObject);
925     return E_OUTOFMEMORY;
926   }
927
928   /* The cloned object starts with a reference count of 1 */
929   newObject->ref          = 1;
930
931   *ppfont = (IFont*)newObject;
932
933   return S_OK;
934 }
935
936 /************************************************************************
937  * OLEFontImpl_IsEqual (IFont)
938  *
939  * See Windows documentation for more details on IFont methods.
940  */
941 static HRESULT WINAPI OLEFontImpl_IsEqual(
942   IFont* iface,
943   IFont* pFontOther)
944 {
945   OLEFontImpl *left = (OLEFontImpl *)iface;
946   OLEFontImpl *right = (OLEFontImpl *)pFontOther;
947   INT ret;
948   INT left_len,right_len;
949
950   if((iface == NULL) || (pFontOther == NULL))
951     return E_POINTER;
952   else if (left->description.cySize.s.Lo != right->description.cySize.s.Lo)
953     return S_FALSE;
954   else if (left->description.cySize.s.Hi != right->description.cySize.s.Hi)
955     return S_FALSE;
956   else if (left->description.sWeight != right->description.sWeight)
957     return S_FALSE;
958   else if (left->description.sCharset != right->description.sCharset)
959     return S_FALSE;
960   else if (left->description.fItalic != right->description.fItalic)
961     return S_FALSE;
962   else if (left->description.fUnderline != right->description.fUnderline)
963     return S_FALSE;
964   else if (left->description.fStrikethrough != right->description.fStrikethrough)
965     return S_FALSE;
966
967   /* Check from string */
968   left_len = strlenW(left->description.lpstrName);
969   right_len = strlenW(right->description.lpstrName);
970   ret = CompareStringW(0,0,left->description.lpstrName, left_len,
971     right->description.lpstrName, right_len);
972   if (ret != CSTR_EQUAL)
973     return S_FALSE;
974
975   return S_OK;
976 }
977
978 /************************************************************************
979  * OLEFontImpl_SetRatio (IFont)
980  *
981  * See Windows documentation for more details on IFont methods.
982  */
983 static HRESULT WINAPI OLEFontImpl_SetRatio(
984   IFont* iface,
985   LONG   cyLogical,
986   LONG   cyHimetric)
987 {
988   OLEFontImpl *this = (OLEFontImpl *)iface;
989   TRACE("(%p)->(%d, %d)\n", this, cyLogical, cyHimetric);
990
991   this->cyLogical  = cyLogical;
992   this->cyHimetric = cyHimetric;
993
994   return S_OK;
995 }
996
997 /************************************************************************
998  * OLEFontImpl_QueryTextMetrics (IFont)
999  *
1000  * See Windows documentation for more details on IFont methods.
1001  */
1002 static HRESULT      WINAPI OLEFontImpl_QueryTextMetrics(
1003   IFont*         iface,
1004   TEXTMETRICOLE* ptm)
1005 {
1006   HDC hdcRef;
1007   HFONT hOldFont, hNewFont;
1008
1009   hdcRef = GetDC(0);
1010   OLEFontImpl_get_hFont(iface, &hNewFont);
1011   hOldFont = SelectObject(hdcRef, hNewFont);
1012   GetTextMetricsW(hdcRef, ptm);
1013   SelectObject(hdcRef, hOldFont);
1014   ReleaseDC(0, hdcRef);
1015   return S_OK;
1016 }
1017
1018 /************************************************************************
1019  * OLEFontImpl_AddRefHfont (IFont)
1020  *
1021  * See Windows documentation for more details on IFont methods.
1022  */
1023 static HRESULT WINAPI OLEFontImpl_AddRefHfont(
1024   IFont*  iface,
1025   HFONT hfont)
1026 {
1027   OLEFontImpl *this = (OLEFontImpl *)iface;
1028   PHFONTItem ptr, next;
1029   HRESULT hres = S_FALSE; /* assume not present */
1030
1031   TRACE("(%p)->(%p)\n", this, hfont);
1032
1033   if (!hfont)
1034     return E_INVALIDARG;
1035
1036   /* Check of the hFont is already in the list */
1037   EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
1038   LIST_FOR_EACH_ENTRY_SAFE(ptr, next, &OLEFontImpl_hFontList, HFONTItem, entry)
1039   {
1040     if (ptr->gdiFont == hfont)
1041     {
1042       ptr->ref++;
1043       hres = S_OK;
1044       break;
1045     }
1046   }
1047   LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
1048
1049   return hres;
1050 }
1051
1052 /************************************************************************
1053  * OLEFontImpl_ReleaseHfont (IFont)
1054  *
1055  * See Windows documentation for more details on IFont methods.
1056  */
1057 static HRESULT WINAPI OLEFontImpl_ReleaseHfont(
1058   IFont*  iface,
1059   HFONT hfont)
1060 {
1061   OLEFontImpl *this = (OLEFontImpl *)iface;
1062   PHFONTItem ptr, next;
1063   HRESULT hres = S_FALSE; /* assume not present */
1064
1065   TRACE("(%p)->(%p)\n", this, hfont);
1066
1067   if (!hfont)
1068     return E_INVALIDARG;
1069
1070   /* Check of the hFont is already in the list */
1071   EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
1072   LIST_FOR_EACH_ENTRY_SAFE(ptr, next, &OLEFontImpl_hFontList, HFONTItem, entry)
1073   {
1074     if ((ptr->gdiFont == hfont) && ptr->ref)
1075     {
1076       /* Remove from cache and delete object if not referenced */
1077       if (!--ptr->ref)
1078       {
1079         if (ptr->gdiFont == this->gdiFont)
1080           this->gdiFont = NULL;
1081       }
1082       hres = S_OK;
1083       break;
1084     }
1085   }
1086   LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
1087
1088   return hres;
1089 }
1090
1091 /************************************************************************
1092  * OLEFontImpl_SetHdc (IFont)
1093  *
1094  * See Windows documentation for more details on IFont methods.
1095  */
1096 static HRESULT WINAPI OLEFontImpl_SetHdc(
1097   IFont* iface,
1098   HDC  hdc)
1099 {
1100   OLEFontImpl *this = (OLEFontImpl *)iface;
1101   FIXME("(%p)->(%p): Stub\n", this, hdc);
1102   return E_NOTIMPL;
1103 }
1104
1105 /*
1106  * Virtual function tables for the OLEFontImpl class.
1107  */
1108 static const IFontVtbl OLEFontImpl_VTable =
1109 {
1110   OLEFontImpl_QueryInterface,
1111   OLEFontImpl_AddRef,
1112   OLEFontImpl_Release,
1113   OLEFontImpl_get_Name,
1114   OLEFontImpl_put_Name,
1115   OLEFontImpl_get_Size,
1116   OLEFontImpl_put_Size,
1117   OLEFontImpl_get_Bold,
1118   OLEFontImpl_put_Bold,
1119   OLEFontImpl_get_Italic,
1120   OLEFontImpl_put_Italic,
1121   OLEFontImpl_get_Underline,
1122   OLEFontImpl_put_Underline,
1123   OLEFontImpl_get_Strikethrough,
1124   OLEFontImpl_put_Strikethrough,
1125   OLEFontImpl_get_Weight,
1126   OLEFontImpl_put_Weight,
1127   OLEFontImpl_get_Charset,
1128   OLEFontImpl_put_Charset,
1129   OLEFontImpl_get_hFont,
1130   OLEFontImpl_Clone,
1131   OLEFontImpl_IsEqual,
1132   OLEFontImpl_SetRatio,
1133   OLEFontImpl_QueryTextMetrics,
1134   OLEFontImpl_AddRefHfont,
1135   OLEFontImpl_ReleaseHfont,
1136   OLEFontImpl_SetHdc
1137 };
1138
1139 /************************************************************************
1140  * OLEFontImpl_IDispatch_QueryInterface (IUnknown)
1141  *
1142  * See Windows documentation for more details on IUnknown methods.
1143  */
1144 static HRESULT WINAPI OLEFontImpl_IDispatch_QueryInterface(
1145   IDispatch* iface,
1146   REFIID     riid,
1147   VOID**     ppvoid)
1148 {
1149   OLEFontImpl *this = impl_from_IDispatch(iface);
1150
1151   return IFont_QueryInterface((IFont *)this, riid, ppvoid);
1152 }
1153
1154 /************************************************************************
1155  * OLEFontImpl_IDispatch_Release (IUnknown)
1156  *
1157  * See Windows documentation for more details on IUnknown methods.
1158  */
1159 static ULONG WINAPI OLEFontImpl_IDispatch_Release(
1160   IDispatch* iface)
1161 {
1162   OLEFontImpl *this = impl_from_IDispatch(iface);
1163
1164   return IFont_Release((IFont *)this);
1165 }
1166
1167 /************************************************************************
1168  * OLEFontImpl_IDispatch_AddRef (IUnknown)
1169  *
1170  * See Windows documentation for more details on IUnknown methods.
1171  */
1172 static ULONG WINAPI OLEFontImpl_IDispatch_AddRef(
1173   IDispatch* iface)
1174 {
1175   OLEFontImpl *this = impl_from_IDispatch(iface);
1176
1177   return IFont_AddRef((IFont *)this);
1178 }
1179
1180 /************************************************************************
1181  * OLEFontImpl_GetTypeInfoCount (IDispatch)
1182  *
1183  * See Windows documentation for more details on IDispatch methods.
1184  */
1185 static HRESULT WINAPI OLEFontImpl_GetTypeInfoCount(
1186   IDispatch*    iface,
1187   unsigned int* pctinfo)
1188 {
1189   OLEFontImpl *this = impl_from_IDispatch(iface);
1190   TRACE("(%p)->(%p)\n", this, pctinfo);
1191   *pctinfo = 1;
1192
1193   return S_OK;
1194 }
1195
1196 /************************************************************************
1197  * OLEFontImpl_GetTypeInfo (IDispatch)
1198  *
1199  * See Windows documentation for more details on IDispatch methods.
1200  */
1201 static HRESULT WINAPI OLEFontImpl_GetTypeInfo(
1202   IDispatch*  iface,
1203   UINT      iTInfo,
1204   LCID        lcid,
1205   ITypeInfo** ppTInfo)
1206 {
1207   static const WCHAR stdole2tlb[] = {'s','t','d','o','l','e','2','.','t','l','b',0};
1208   ITypeLib *tl;
1209   HRESULT hres;
1210
1211   OLEFontImpl *this = impl_from_IDispatch(iface);
1212   TRACE("(%p, iTInfo=%d, lcid=%04x, %p)\n", this, iTInfo, (int)lcid, ppTInfo);
1213   if (iTInfo != 0)
1214     return E_FAIL;
1215   hres = LoadTypeLib(stdole2tlb, &tl);
1216   if (FAILED(hres)) {
1217     ERR("Could not load the stdole2.tlb?\n");
1218     return hres;
1219   }
1220   hres = ITypeLib_GetTypeInfoOfGuid(tl, &IID_IFontDisp, ppTInfo);
1221   ITypeLib_Release(tl);
1222   if (FAILED(hres)) {
1223     FIXME("Did not IDispatch typeinfo from typelib, hres %x\n",hres);
1224   }
1225   return hres;
1226 }
1227
1228 /************************************************************************
1229  * OLEFontImpl_GetIDsOfNames (IDispatch)
1230  *
1231  * See Windows documentation for more details on IDispatch methods.
1232  */
1233 static HRESULT WINAPI OLEFontImpl_GetIDsOfNames(
1234   IDispatch*  iface,
1235   REFIID      riid,
1236   LPOLESTR* rgszNames,
1237   UINT      cNames,
1238   LCID        lcid,
1239   DISPID*     rgDispId)
1240 {
1241   ITypeInfo * pTInfo;
1242   HRESULT hres;
1243
1244   OLEFontImpl *this = impl_from_IDispatch(iface);
1245
1246   TRACE("(%p,%s,%p,cNames=%d,lcid=%04x,%p)\n", this, debugstr_guid(riid),
1247         rgszNames, cNames, (int)lcid, rgDispId);
1248
1249   if (cNames == 0)
1250   {
1251     return E_INVALIDARG;
1252   }
1253   else
1254   {
1255     /* retrieve type information */
1256     hres = OLEFontImpl_GetTypeInfo(iface, 0, lcid, &pTInfo);
1257
1258     if (FAILED(hres))
1259     {
1260       ERR("GetTypeInfo failed.\n");
1261       return hres;
1262     }
1263
1264     /* convert names to DISPIDs */
1265     hres = DispGetIDsOfNames (pTInfo, rgszNames, cNames, rgDispId);
1266     ITypeInfo_Release(pTInfo);
1267
1268     return hres;
1269   }
1270 }
1271
1272 /************************************************************************
1273  * OLEFontImpl_Invoke (IDispatch)
1274  *
1275  * See Windows documentation for more details on IDispatch methods.
1276  * 
1277  * Note: Do not call _put_Xxx methods, since setting things here
1278  * should not call notify functions as I found out debugging the generic
1279  * MS VB5 installer.
1280  */
1281 static HRESULT WINAPI OLEFontImpl_Invoke(
1282   IDispatch*  iface,
1283   DISPID      dispIdMember,
1284   REFIID      riid,
1285   LCID        lcid,
1286   WORD        wFlags,
1287   DISPPARAMS* pDispParams,
1288   VARIANT*    pVarResult,
1289   EXCEPINFO*  pExepInfo,
1290   UINT*     puArgErr)
1291 {
1292   OLEFontImpl *this = impl_from_IDispatch(iface);
1293   HRESULT hr;
1294
1295   TRACE("%p->(%d,%s,0x%x,0x%x,%p,%p,%p,%p)\n", this, dispIdMember,
1296     debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult, pExepInfo,
1297     puArgErr);
1298
1299   /* validate parameters */
1300
1301   if (!IsEqualIID(riid, &IID_NULL))
1302   {
1303     ERR("riid was %s instead of IID_NULL\n", debugstr_guid(riid));
1304     return DISP_E_UNKNOWNINTERFACE;
1305   }
1306
1307   if (wFlags & DISPATCH_PROPERTYGET)
1308   {
1309     if (!pVarResult)
1310     {
1311       ERR("null pVarResult not allowed when DISPATCH_PROPERTYGET specified\n");
1312       return DISP_E_PARAMNOTOPTIONAL;
1313     }
1314   }
1315   else if (wFlags & DISPATCH_PROPERTYPUT)
1316   {
1317     if (!pDispParams)
1318     {
1319       ERR("null pDispParams not allowed when DISPATCH_PROPERTYPUT specified\n");
1320       return DISP_E_PARAMNOTOPTIONAL;
1321     }
1322     if (pDispParams->cArgs != 1)
1323     {
1324       ERR("param count for DISPATCH_PROPERTYPUT was %d instead of 1\n", pDispParams->cArgs);
1325       return DISP_E_BADPARAMCOUNT;
1326     }
1327   }
1328   else
1329   {
1330     ERR("one of DISPATCH_PROPERTYGET or DISPATCH_PROPERTYPUT must be specified\n");
1331     return DISP_E_MEMBERNOTFOUND;
1332   }
1333
1334   switch (dispIdMember) {
1335   case DISPID_FONT_NAME:
1336     if (wFlags & DISPATCH_PROPERTYGET) {
1337       V_VT(pVarResult) = VT_BSTR;
1338       return IFont_get_Name((IFont *)this, &V_BSTR(pVarResult));
1339     } else {
1340       VARIANTARG vararg;
1341
1342       VariantInit(&vararg);
1343       hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_BSTR);
1344       if (FAILED(hr))
1345         return hr;
1346
1347       hr = IFont_put_Name((IFont *)this, V_BSTR(&vararg));
1348
1349       VariantClear(&vararg);
1350       return hr;
1351     }
1352     break;
1353   case DISPID_FONT_BOLD:
1354     if (wFlags & DISPATCH_PROPERTYGET) {
1355       BOOL value;
1356       hr = IFont_get_Bold((IFont *)this, &value);
1357       V_VT(pVarResult) = VT_BOOL;
1358       V_BOOL(pVarResult) = value ? VARIANT_TRUE : VARIANT_FALSE;
1359       return hr;
1360     } else {
1361       VARIANTARG vararg;
1362
1363       VariantInit(&vararg);
1364       hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_BOOL);
1365       if (FAILED(hr))
1366         return hr;
1367
1368       hr = IFont_put_Bold((IFont *)this, V_BOOL(&vararg));
1369
1370       VariantClear(&vararg);
1371       return hr;
1372     }
1373     break;
1374   case DISPID_FONT_ITALIC:
1375     if (wFlags & DISPATCH_PROPERTYGET) {
1376       BOOL value;
1377       hr = IFont_get_Italic((IFont *)this, &value);
1378       V_VT(pVarResult) = VT_BOOL;
1379       V_BOOL(pVarResult) = value ? VARIANT_TRUE : VARIANT_FALSE;
1380       return hr;
1381     } else {
1382       VARIANTARG vararg;
1383
1384       VariantInit(&vararg);
1385       hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_BOOL);
1386       if (FAILED(hr))
1387         return hr;
1388
1389       hr = IFont_put_Italic((IFont *)this, V_BOOL(&vararg));
1390
1391       VariantClear(&vararg);
1392       return hr;
1393     }
1394     break;
1395   case DISPID_FONT_UNDER:
1396     if (wFlags & DISPATCH_PROPERTYGET) {
1397       BOOL value;
1398       hr = IFont_get_Underline((IFont *)this, &value);
1399       V_VT(pVarResult) = VT_BOOL;
1400       V_BOOL(pVarResult) = value ? VARIANT_TRUE : VARIANT_FALSE;
1401       return hr;
1402     } else {
1403       VARIANTARG vararg;
1404
1405       VariantInit(&vararg);
1406       hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_BOOL);
1407       if (FAILED(hr))
1408         return hr;
1409
1410       hr = IFont_put_Underline((IFont *)this, V_BOOL(&vararg));
1411
1412       VariantClear(&vararg);
1413       return hr;
1414     }
1415     break;
1416   case DISPID_FONT_STRIKE:
1417     if (wFlags & DISPATCH_PROPERTYGET) {
1418       BOOL value;
1419       hr = IFont_get_Strikethrough((IFont *)this, &value);
1420       V_VT(pVarResult) = VT_BOOL;
1421       V_BOOL(pVarResult) = value ? VARIANT_TRUE : VARIANT_FALSE;
1422       return hr;
1423     } else {
1424       VARIANTARG vararg;
1425
1426       VariantInit(&vararg);
1427       hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_BOOL);
1428       if (FAILED(hr))
1429         return hr;
1430
1431       hr = IFont_put_Strikethrough((IFont *)this, V_BOOL(&vararg));
1432
1433       VariantClear(&vararg);
1434       return hr;
1435     }
1436     break;
1437   case DISPID_FONT_SIZE:
1438     if (wFlags & DISPATCH_PROPERTYGET) {
1439       V_VT(pVarResult) = VT_CY;
1440       return OLEFontImpl_get_Size((IFont *)this, &V_CY(pVarResult));
1441     } else {
1442       VARIANTARG vararg;
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
1462       VariantInit(&vararg);
1463       hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_I2);
1464       if (FAILED(hr))
1465         return hr;
1466
1467       hr = IFont_put_Weight((IFont *)this, V_I2(&vararg));
1468
1469       VariantClear(&vararg);
1470       return hr;
1471     }
1472     break;
1473   case DISPID_FONT_CHARSET:
1474     if (wFlags & DISPATCH_PROPERTYGET) {
1475       V_VT(pVarResult) = VT_I2;
1476       return OLEFontImpl_get_Charset((IFont *)this, &V_I2(pVarResult));
1477     } else {
1478       VARIANTARG vararg;
1479
1480       VariantInit(&vararg);
1481       hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_I2);
1482       if (FAILED(hr))
1483         return hr;
1484
1485       hr = IFont_put_Charset((IFont *)this, V_I2(&vararg));
1486
1487       VariantClear(&vararg);
1488       return hr;
1489     }
1490     break;
1491   default:
1492     ERR("member not found for dispid 0x%x\n", dispIdMember);
1493     return DISP_E_MEMBERNOTFOUND;
1494   }
1495 }
1496
1497 static const IDispatchVtbl OLEFontImpl_IDispatch_VTable =
1498 {
1499   OLEFontImpl_IDispatch_QueryInterface,
1500   OLEFontImpl_IDispatch_AddRef,
1501   OLEFontImpl_IDispatch_Release,
1502   OLEFontImpl_GetTypeInfoCount,
1503   OLEFontImpl_GetTypeInfo,
1504   OLEFontImpl_GetIDsOfNames,
1505   OLEFontImpl_Invoke
1506 };
1507
1508 /************************************************************************
1509  * OLEFontImpl_IPersistStream_QueryInterface (IUnknown)
1510  *
1511  * See Windows documentation for more details on IUnknown methods.
1512  */
1513 static HRESULT WINAPI OLEFontImpl_IPersistStream_QueryInterface(
1514   IPersistStream* iface,
1515   REFIID     riid,
1516   VOID**     ppvoid)
1517 {
1518   OLEFontImpl *this = impl_from_IPersistStream(iface);
1519
1520   return IFont_QueryInterface((IFont *)this, riid, ppvoid);
1521 }
1522
1523 /************************************************************************
1524  * OLEFontImpl_IPersistStream_Release (IUnknown)
1525  *
1526  * See Windows documentation for more details on IUnknown methods.
1527  */
1528 static ULONG WINAPI OLEFontImpl_IPersistStream_Release(
1529   IPersistStream* iface)
1530 {
1531   OLEFontImpl *this = impl_from_IPersistStream(iface);
1532
1533   return IFont_Release((IFont *)this);
1534 }
1535
1536 /************************************************************************
1537  * OLEFontImpl_IPersistStream_AddRef (IUnknown)
1538  *
1539  * See Windows documentation for more details on IUnknown methods.
1540  */
1541 static ULONG WINAPI OLEFontImpl_IPersistStream_AddRef(
1542   IPersistStream* iface)
1543 {
1544   OLEFontImpl *this = impl_from_IPersistStream(iface);
1545
1546   return IFont_AddRef((IFont *)this);
1547 }
1548
1549 /************************************************************************
1550  * OLEFontImpl_GetClassID (IPersistStream)
1551  *
1552  * See Windows documentation for more details on IPersistStream methods.
1553  */
1554 static HRESULT WINAPI OLEFontImpl_GetClassID(
1555   IPersistStream* iface,
1556   CLSID*                pClassID)
1557 {
1558   TRACE("(%p,%p)\n",iface,pClassID);
1559   if (pClassID==0)
1560     return E_POINTER;
1561
1562   *pClassID = CLSID_StdFont;
1563
1564   return S_OK;
1565 }
1566
1567 /************************************************************************
1568  * OLEFontImpl_IsDirty (IPersistStream)
1569  *
1570  * See Windows documentation for more details on IPersistStream methods.
1571  */
1572 static HRESULT WINAPI OLEFontImpl_IsDirty(
1573   IPersistStream*  iface)
1574 {
1575   TRACE("(%p)\n",iface);
1576   return S_OK;
1577 }
1578
1579 /************************************************************************
1580  * OLEFontImpl_Load (IPersistStream)
1581  *
1582  * See Windows documentation for more details on IPersistStream methods.
1583  *
1584  * This is the format of the standard font serialization as far as I
1585  * know
1586  *
1587  * Offset   Type   Value           Comment
1588  * 0x0000   Byte   Unknown         Probably a version number, contains 0x01
1589  * 0x0001   Short  Charset         Charset value from the FONTDESC structure
1590  * 0x0003   Byte   Attributes      Flags defined as follows:
1591  *                                     00000010 - Italic
1592  *                                     00000100 - Underline
1593  *                                     00001000 - Strikethrough
1594  * 0x0004   Short  Weight          Weight value from FONTDESC structure
1595  * 0x0006   DWORD  size            "Low" portion of the cySize member of the FONTDESC
1596  *                                 structure/
1597  * 0x000A   Byte   name length     Length of the font name string (no null character)
1598  * 0x000B   String name            Name of the font (ASCII, no nul character)
1599  */
1600 static HRESULT WINAPI OLEFontImpl_Load(
1601   IPersistStream*  iface,
1602   IStream*         pLoadStream)
1603 {
1604   char  readBuffer[0x100];
1605   ULONG cbRead;
1606   BYTE  bVersion;
1607   BYTE  bAttributes;
1608   BYTE  bStringSize;
1609   INT len;
1610
1611   OLEFontImpl *this = impl_from_IPersistStream(iface);
1612
1613   /*
1614    * Read the version byte
1615    */
1616   IStream_Read(pLoadStream, &bVersion, 1, &cbRead);
1617
1618   if ( (cbRead!=1) ||
1619        (bVersion!=0x01) )
1620     return E_FAIL;
1621
1622   /*
1623    * Charset
1624    */
1625   IStream_Read(pLoadStream, &this->description.sCharset, 2, &cbRead);
1626
1627   if (cbRead!=2)
1628     return E_FAIL;
1629
1630   /*
1631    * Attributes
1632    */
1633   IStream_Read(pLoadStream, &bAttributes, 1, &cbRead);
1634
1635   if (cbRead!=1)
1636     return E_FAIL;
1637
1638   this->description.fItalic        = (bAttributes & FONTPERSIST_ITALIC) != 0;
1639   this->description.fStrikethrough = (bAttributes & FONTPERSIST_STRIKETHROUGH) != 0;
1640   this->description.fUnderline     = (bAttributes & FONTPERSIST_UNDERLINE) != 0;
1641
1642   /*
1643    * Weight
1644    */
1645   IStream_Read(pLoadStream, &this->description.sWeight, 2, &cbRead);
1646
1647   if (cbRead!=2)
1648     return E_FAIL;
1649
1650   /*
1651    * Size
1652    */
1653   IStream_Read(pLoadStream, &this->description.cySize.s.Lo, 4, &cbRead);
1654
1655   if (cbRead!=4)
1656     return E_FAIL;
1657
1658   this->description.cySize.s.Hi = 0;
1659
1660   /*
1661    * FontName
1662    */
1663   IStream_Read(pLoadStream, &bStringSize, 1, &cbRead);
1664
1665   if (cbRead!=1)
1666     return E_FAIL;
1667
1668   IStream_Read(pLoadStream, readBuffer, bStringSize, &cbRead);
1669
1670   if (cbRead!=bStringSize)
1671     return E_FAIL;
1672
1673   HeapFree(GetProcessHeap(), 0, this->description.lpstrName);
1674
1675   len = MultiByteToWideChar( CP_ACP, 0, readBuffer, bStringSize, NULL, 0 );
1676   this->description.lpstrName = HeapAlloc( GetProcessHeap(), 0, (len+1) * sizeof(WCHAR) );
1677   MultiByteToWideChar( CP_ACP, 0, readBuffer, bStringSize, this->description.lpstrName, len );
1678   this->description.lpstrName[len] = 0;
1679
1680   /* Ensure use of this font causes a new one to be created @@@@ */
1681   DeleteObject(this->gdiFont);
1682   this->gdiFont = 0;
1683
1684   return S_OK;
1685 }
1686
1687 /************************************************************************
1688  * OLEFontImpl_Save (IPersistStream)
1689  *
1690  * See Windows documentation for more details on IPersistStream methods.
1691  */
1692 static HRESULT WINAPI OLEFontImpl_Save(
1693   IPersistStream*  iface,
1694   IStream*         pOutStream,
1695   BOOL             fClearDirty)
1696 {
1697   char* writeBuffer = NULL;
1698   ULONG cbWritten;
1699   BYTE  bVersion = 0x01;
1700   BYTE  bAttributes;
1701   BYTE  bStringSize;
1702
1703   OLEFontImpl *this = impl_from_IPersistStream(iface);
1704
1705   /*
1706    * Read the version byte
1707    */
1708   IStream_Write(pOutStream, &bVersion, 1, &cbWritten);
1709
1710   if (cbWritten!=1)
1711     return E_FAIL;
1712
1713   /*
1714    * Charset
1715    */
1716   IStream_Write(pOutStream, &this->description.sCharset, 2, &cbWritten);
1717
1718   if (cbWritten!=2)
1719     return E_FAIL;
1720
1721   /*
1722    * Attributes
1723    */
1724   bAttributes = 0;
1725
1726   if (this->description.fItalic)
1727     bAttributes |= FONTPERSIST_ITALIC;
1728
1729   if (this->description.fStrikethrough)
1730     bAttributes |= FONTPERSIST_STRIKETHROUGH;
1731
1732   if (this->description.fUnderline)
1733     bAttributes |= FONTPERSIST_UNDERLINE;
1734
1735   IStream_Write(pOutStream, &bAttributes, 1, &cbWritten);
1736
1737   if (cbWritten!=1)
1738     return E_FAIL;
1739
1740   /*
1741    * Weight
1742    */
1743   IStream_Write(pOutStream, &this->description.sWeight, 2, &cbWritten);
1744
1745   if (cbWritten!=2)
1746     return E_FAIL;
1747
1748   /*
1749    * Size
1750    */
1751   IStream_Write(pOutStream, &this->description.cySize.s.Lo, 4, &cbWritten);
1752
1753   if (cbWritten!=4)
1754     return E_FAIL;
1755
1756   /*
1757    * FontName
1758    */
1759   if (this->description.lpstrName!=0)
1760     bStringSize = WideCharToMultiByte( CP_ACP, 0, this->description.lpstrName,
1761                                        strlenW(this->description.lpstrName), NULL, 0, NULL, NULL );
1762   else
1763     bStringSize = 0;
1764
1765   IStream_Write(pOutStream, &bStringSize, 1, &cbWritten);
1766
1767   if (cbWritten!=1)
1768     return E_FAIL;
1769
1770   if (bStringSize!=0)
1771   {
1772       if (!(writeBuffer = HeapAlloc( GetProcessHeap(), 0, bStringSize ))) return E_OUTOFMEMORY;
1773       WideCharToMultiByte( CP_ACP, 0, this->description.lpstrName,
1774                            strlenW(this->description.lpstrName),
1775                            writeBuffer, bStringSize, NULL, NULL );
1776
1777     IStream_Write(pOutStream, writeBuffer, bStringSize, &cbWritten);
1778     HeapFree(GetProcessHeap(), 0, writeBuffer);
1779
1780     if (cbWritten!=bStringSize)
1781       return E_FAIL;
1782   }
1783
1784   return S_OK;
1785 }
1786
1787 /************************************************************************
1788  * OLEFontImpl_GetSizeMax (IPersistStream)
1789  *
1790  * See Windows documentation for more details on IPersistStream methods.
1791  */
1792 static HRESULT WINAPI OLEFontImpl_GetSizeMax(
1793   IPersistStream*  iface,
1794   ULARGE_INTEGER*  pcbSize)
1795 {
1796   OLEFontImpl *this = impl_from_IPersistStream(iface);
1797
1798   if (pcbSize==NULL)
1799     return E_POINTER;
1800
1801   pcbSize->u.HighPart = 0;
1802   pcbSize->u.LowPart = 0;
1803
1804   pcbSize->u.LowPart += sizeof(BYTE);  /* Version */
1805   pcbSize->u.LowPart += sizeof(WORD);  /* Lang code */
1806   pcbSize->u.LowPart += sizeof(BYTE);  /* Flags */
1807   pcbSize->u.LowPart += sizeof(WORD);  /* Weight */
1808   pcbSize->u.LowPart += sizeof(DWORD); /* Size */
1809   pcbSize->u.LowPart += sizeof(BYTE);  /* StrLength */
1810
1811   if (this->description.lpstrName!=0)
1812       pcbSize->u.LowPart += WideCharToMultiByte( CP_ACP, 0, this->description.lpstrName,
1813                                                  strlenW(this->description.lpstrName),
1814                                                  NULL, 0, NULL, NULL );
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 = &STDFONT_CF; }