gdi32: Fix leak in GdiDeleteSpoolFileHandle.
[wine.git] / dlls / oleaut32 / olefont.c
blobaea8b596498cd7a9fe5d6d2716891e40fe2ce1fe
1 /*
2 * OLE Font encapsulation implementation
4 * This file contains an implementation of the IFont
5 * interface and the OleCreateFontIndirect API call.
7 * Copyright 1999 Francis Beaudet
8 * Copyright 2006 (Google) Benjamin Arai
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.
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.
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
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <string.h>
28 #define COBJMACROS
29 #include "winerror.h"
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winuser.h"
34 #include "wine/list.h"
35 #include "objbase.h"
36 #include "oleauto.h" /* for SysAllocString(....) */
37 #include "ole2.h"
38 #include "olectl.h"
39 #include "wine/debug.h"
40 #include "connpt.h" /* for CreateConnectionPoint */
41 #include "oaidl.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(ole);
45 /***********************************************************************
46 * Declaration of constants used when serializing the font object.
48 #define FONTPERSIST_ITALIC 0x02
49 #define FONTPERSIST_UNDERLINE 0x04
50 #define FONTPERSIST_STRIKETHROUGH 0x08
52 static HDC olefont_hdc;
54 /***********************************************************************
55 * List of the HFONTs it has given out, with each one having a separate
56 * ref count.
58 typedef struct _HFONTItem
60 struct list entry;
62 /* Reference count of any IFont objects that own this hfont */
63 LONG int_refs;
65 /* Total reference count of any refs held by the application obtained by AddRefHfont plus any internal refs */
66 LONG total_refs;
68 /* The font associated with this object. */
69 HFONT gdiFont;
71 } HFONTItem, *PHFONTItem;
73 static struct list OLEFontImpl_hFontList = LIST_INIT(OLEFontImpl_hFontList);
75 /* Counts how many fonts contain at least one lock */
76 static LONG ifont_cnt = 0;
78 /***********************************************************************
79 * Critical section for OLEFontImpl_hFontList
81 static CRITICAL_SECTION OLEFontImpl_csHFONTLIST;
82 static CRITICAL_SECTION_DEBUG OLEFontImpl_csHFONTLIST_debug =
84 0, 0, &OLEFontImpl_csHFONTLIST,
85 { &OLEFontImpl_csHFONTLIST_debug.ProcessLocksList,
86 &OLEFontImpl_csHFONTLIST_debug.ProcessLocksList },
87 0, 0, { (DWORD_PTR)(__FILE__ ": OLEFontImpl_csHFONTLIST") }
89 static CRITICAL_SECTION OLEFontImpl_csHFONTLIST = { &OLEFontImpl_csHFONTLIST_debug, -1, 0, 0, 0, 0 };
91 static HDC get_dc(void)
93 HDC hdc;
94 EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
95 if(!olefont_hdc)
96 olefont_hdc = CreateCompatibleDC(NULL);
97 hdc = olefont_hdc;
98 LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
99 return hdc;
102 static void delete_dc(void)
104 EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
105 if(olefont_hdc)
107 DeleteDC(olefont_hdc);
108 olefont_hdc = NULL;
110 LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
113 static void HFONTItem_Delete(PHFONTItem item)
115 DeleteObject(item->gdiFont);
116 list_remove(&item->entry);
117 free(item);
120 /* Find hfont item entry in the list. Should be called while holding the crit sect */
121 static HFONTItem *find_hfontitem(HFONT hfont)
123 HFONTItem *item;
125 LIST_FOR_EACH_ENTRY(item, &OLEFontImpl_hFontList, HFONTItem, entry)
127 if (item->gdiFont == hfont)
128 return item;
130 return NULL;
133 /* Add an item to the list with one internal reference */
134 static HRESULT add_hfontitem(HFONT hfont)
136 HFONTItem *new_item = malloc(sizeof(*new_item));
138 if(!new_item) return E_OUTOFMEMORY;
140 new_item->int_refs = 1;
141 new_item->total_refs = 1;
142 new_item->gdiFont = hfont;
143 EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
144 list_add_tail(&OLEFontImpl_hFontList,&new_item->entry);
145 LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
146 return S_OK;
149 static HRESULT inc_int_ref(HFONT hfont)
151 HFONTItem *item;
152 HRESULT hr = S_FALSE;
154 EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
155 item = find_hfontitem(hfont);
157 if(item)
159 item->int_refs++;
160 item->total_refs++;
161 hr = S_OK;
163 LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
165 return hr;
168 /* decrements the internal ref of a hfont item. If both refs are zero it'll
169 remove the item from the list and delete the hfont */
170 static HRESULT dec_int_ref(HFONT hfont)
172 HFONTItem *item;
173 HRESULT hr = S_FALSE;
175 EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
176 item = find_hfontitem(hfont);
178 if(item)
180 item->int_refs--;
181 item->total_refs--;
182 if(item->int_refs == 0 && item->total_refs == 0)
183 HFONTItem_Delete(item);
184 hr = S_OK;
186 LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
188 return hr;
191 static HRESULT inc_ext_ref(HFONT hfont)
193 HFONTItem *item;
194 HRESULT hr = S_FALSE;
196 EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
198 item = find_hfontitem(hfont);
199 if(item)
201 item->total_refs++;
202 hr = S_OK;
204 LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
206 return hr;
209 static HRESULT dec_ext_ref(HFONT hfont)
211 HFONTItem *item;
212 HRESULT hr = S_FALSE;
214 EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
216 item = find_hfontitem(hfont);
217 if(item)
219 if(--item->total_refs >= 0) hr = S_OK;
221 LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
223 return hr;
226 /***********************************************************************
227 * Declaration of the implementation class for the IFont interface
229 typedef struct OLEFontImpl OLEFontImpl;
231 struct OLEFontImpl
234 * This class supports many interfaces. IUnknown, IFont,
235 * IDispatch, IDispFont IPersistStream and IConnectionPointContainer.
236 * The first two are supported by the first vtable, the next two are
237 * supported by the second table and the last two have their own.
239 IFont IFont_iface;
240 IDispatch IDispatch_iface;
241 IPersistStream IPersistStream_iface;
242 IConnectionPointContainer IConnectionPointContainer_iface;
243 IPersistPropertyBag IPersistPropertyBag_iface;
245 * Reference count for that instance of the class.
247 LONG ref;
250 * This structure contains the description of the class.
252 FONTDESC description;
255 * Contain the font associated with this object.
257 HFONT gdiFont;
258 BOOL dirty;
260 * Size ratio
262 LONG cyLogical;
263 LONG cyHimetric;
266 * Stash realized height (pixels) from TEXTMETRIC - used in get_Size()
268 LONG nRealHeight;
270 IConnectionPoint *pPropertyNotifyCP;
271 IConnectionPoint *pFontEventsCP;
274 static inline OLEFontImpl *impl_from_IFont(IFont *iface)
276 return CONTAINING_RECORD(iface, OLEFontImpl, IFont_iface);
279 static inline OLEFontImpl *impl_from_IDispatch( IDispatch *iface )
281 return CONTAINING_RECORD(iface, OLEFontImpl, IDispatch_iface);
284 static inline OLEFontImpl *impl_from_IPersistStream( IPersistStream *iface )
286 return CONTAINING_RECORD(iface, OLEFontImpl, IPersistStream_iface);
289 static inline OLEFontImpl *impl_from_IConnectionPointContainer( IConnectionPointContainer *iface )
291 return CONTAINING_RECORD(iface, OLEFontImpl, IConnectionPointContainer_iface);
294 static inline OLEFontImpl *impl_from_IPersistPropertyBag( IPersistPropertyBag *iface )
296 return CONTAINING_RECORD(iface, OLEFontImpl, IPersistPropertyBag_iface);
300 /***********************************************************************
301 * Prototypes for the implementation functions for the IFont
302 * interface
304 static OLEFontImpl* OLEFontImpl_Construct(const FONTDESC *fontDesc);
305 static void OLEFontImpl_Destroy(OLEFontImpl* fontDesc);
306 static ULONG WINAPI OLEFontImpl_AddRef(IFont* iface);
308 /******************************************************************************
309 * OleCreateFontIndirect [OLEAUT32.420]
311 HRESULT WINAPI OleCreateFontIndirect(
312 LPFONTDESC lpFontDesc,
313 REFIID riid,
314 LPVOID* ppvObj)
316 OLEFontImpl* newFont;
317 HRESULT hr;
318 FONTDESC fd;
320 TRACE("(%p, %s, %p)\n", lpFontDesc, debugstr_guid(riid), ppvObj);
322 if (!ppvObj) return E_POINTER;
324 *ppvObj = 0;
326 if (!lpFontDesc) {
327 static WCHAR fname[] = L"System";
329 fd.cbSizeofstruct = sizeof(fd);
330 fd.lpstrName = fname;
331 fd.cySize.Lo = 80000;
332 fd.cySize.Hi = 0;
333 fd.sWeight = 0;
334 fd.sCharset = 0;
335 fd.fItalic = FALSE;
336 fd.fUnderline = FALSE;
337 fd.fStrikethrough = FALSE;
338 lpFontDesc = &fd;
341 newFont = OLEFontImpl_Construct(lpFontDesc);
342 if (!newFont) return E_OUTOFMEMORY;
344 hr = IFont_QueryInterface(&newFont->IFont_iface, riid, ppvObj);
345 IFont_Release(&newFont->IFont_iface);
347 return hr;
351 /***********************************************************************
352 * Implementation of the OLEFontImpl class.
355 /***********************************************************************
356 * OLEFont_SendNotify (internal)
358 * Sends notification messages of changed properties to any interested
359 * connections.
361 static void OLEFont_SendNotify(OLEFontImpl* this, DISPID dispID)
363 static const LPCWSTR dispid_mapping[] =
365 L"Name",
366 NULL,
367 L"Size",
368 L"Bold",
369 L"Italic",
370 L"Underline",
371 L"Strikethrough",
372 L"Weight",
373 L"Charset"
376 IEnumConnections *pEnum;
377 CONNECTDATA CD;
378 HRESULT hres;
380 this->dirty = TRUE;
382 hres = IConnectionPoint_EnumConnections(this->pPropertyNotifyCP, &pEnum);
383 if (SUCCEEDED(hres))
385 while(IEnumConnections_Next(pEnum, 1, &CD, NULL) == S_OK) {
386 IPropertyNotifySink *sink;
388 IUnknown_QueryInterface(CD.pUnk, &IID_IPropertyNotifySink, (void**)&sink);
389 IPropertyNotifySink_OnChanged(sink, dispID);
390 IPropertyNotifySink_Release(sink);
391 IUnknown_Release(CD.pUnk);
393 IEnumConnections_Release(pEnum);
396 hres = IConnectionPoint_EnumConnections(this->pFontEventsCP, &pEnum);
397 if (SUCCEEDED(hres))
399 DISPPARAMS dispparams;
400 VARIANTARG vararg;
402 VariantInit(&vararg);
403 V_VT(&vararg) = VT_BSTR;
404 V_BSTR(&vararg) = SysAllocString(dispid_mapping[dispID]);
406 dispparams.cArgs = 1;
407 dispparams.cNamedArgs = 0;
408 dispparams.rgdispidNamedArgs = NULL;
409 dispparams.rgvarg = &vararg;
411 while(IEnumConnections_Next(pEnum, 1, &CD, NULL) == S_OK) {
412 IFontEventsDisp *disp;
414 IUnknown_QueryInterface(CD.pUnk, &IID_IFontEventsDisp, (void**)&disp);
415 IFontEventsDisp_Invoke(disp, DISPID_FONT_CHANGED, &IID_NULL,
416 LOCALE_NEUTRAL, INVOKE_FUNC, &dispparams, NULL,
417 NULL, NULL);
419 IFontEventsDisp_Release(disp);
420 IUnknown_Release(CD.pUnk);
422 VariantClear(&vararg);
423 IEnumConnections_Release(pEnum);
427 /************************************************************************
428 * OLEFontImpl_QueryInterface (IUnknown)
430 * See Windows documentation for more details on IUnknown methods.
432 static HRESULT WINAPI OLEFontImpl_QueryInterface(
433 IFont* iface,
434 REFIID riid,
435 void** ppvObject)
437 OLEFontImpl *this = impl_from_IFont(iface);
439 TRACE("(%p)->(%s, %p)\n", this, debugstr_guid(riid), ppvObject);
441 *ppvObject = 0;
443 if (IsEqualGUID(&IID_IUnknown, riid) ||
444 IsEqualGUID(&IID_IFont, riid))
446 *ppvObject = this;
448 else if (IsEqualGUID(&IID_IDispatch, riid) ||
449 IsEqualGUID(&IID_IFontDisp, riid))
451 *ppvObject = &this->IDispatch_iface;
453 else if (IsEqualGUID(&IID_IPersist, riid) ||
454 IsEqualGUID(&IID_IPersistStream, riid))
456 *ppvObject = &this->IPersistStream_iface;
458 else if (IsEqualGUID(&IID_IConnectionPointContainer, riid))
460 *ppvObject = &this->IConnectionPointContainer_iface;
462 else if (IsEqualGUID(&IID_IPersistPropertyBag, riid))
464 *ppvObject = &this->IPersistPropertyBag_iface;
467 if (!*ppvObject)
469 FIXME("() : asking for unsupported interface %s\n", debugstr_guid(riid));
470 return E_NOINTERFACE;
473 IFont_AddRef(iface);
475 return S_OK;
478 static ULONG WINAPI OLEFontImpl_AddRef(IFont* iface)
480 OLEFontImpl *this = impl_from_IFont(iface);
481 ULONG ref = InterlockedIncrement(&this->ref);
482 TRACE("%p, refcount %lu.\n", iface, ref);
483 return ref;
486 static ULONG WINAPI OLEFontImpl_Release(IFont* iface)
488 OLEFontImpl *this = impl_from_IFont(iface);
489 ULONG ref = InterlockedDecrement(&this->ref);
491 TRACE("%p, refcount %lu.\n", iface, ref);
493 if (ref == 0)
495 ULONG fontlist_refs = InterlockedDecrement(&ifont_cnt);
497 /* Final IFont object so destroy font cache */
498 if (fontlist_refs == 0)
500 HFONTItem *item, *cursor2;
502 EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
503 LIST_FOR_EACH_ENTRY_SAFE(item, cursor2, &OLEFontImpl_hFontList, HFONTItem, entry)
504 HFONTItem_Delete(item);
505 LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
506 delete_dc();
508 else
510 dec_int_ref(this->gdiFont);
512 OLEFontImpl_Destroy(this);
515 return ref;
518 typedef struct
520 short orig_cs;
521 short avail_cs;
522 } enum_data;
524 static int CALLBACK font_enum_proc(const LOGFONTW *elf, const TEXTMETRICW *ntm, DWORD type, LPARAM lp)
526 enum_data *data = (enum_data*)lp;
528 if(elf->lfCharSet == data->orig_cs)
530 data->avail_cs = data->orig_cs;
531 return 0;
533 if(data->avail_cs == -1) data->avail_cs = elf->lfCharSet;
534 return 1;
537 static void realize_font(OLEFontImpl *This)
539 LOGFONTW logFont;
540 INT fontHeight;
541 WCHAR text_face[LF_FACESIZE];
542 HDC hdc = get_dc();
543 HFONT old_font;
544 TEXTMETRICW tm;
546 if (!This->dirty) return;
548 text_face[0] = 0;
550 if(This->gdiFont)
552 old_font = SelectObject(hdc, This->gdiFont);
553 GetTextFaceW(hdc, ARRAY_SIZE(text_face), text_face);
554 SelectObject(hdc, old_font);
555 dec_int_ref(This->gdiFont);
556 This->gdiFont = 0;
559 memset(&logFont, 0, sizeof(LOGFONTW));
561 lstrcpynW(logFont.lfFaceName, This->description.lpstrName, LF_FACESIZE);
562 logFont.lfCharSet = This->description.sCharset;
564 /* If the font name has been changed then enumerate all charsets
565 and pick one that'll result in the font specified being selected */
566 if(text_face[0] && lstrcmpiW(text_face, This->description.lpstrName))
568 enum_data data;
569 data.orig_cs = This->description.sCharset;
570 data.avail_cs = -1;
571 logFont.lfCharSet = DEFAULT_CHARSET;
572 EnumFontFamiliesExW(get_dc(), &logFont, font_enum_proc, (LPARAM)&data, 0);
573 if(data.avail_cs != -1) logFont.lfCharSet = data.avail_cs;
577 * The height of the font returned by the get_Size property is the
578 * height of the font in points multiplied by 10000... Using some
579 * simple conversions and the ratio given by the application, it can
580 * be converted to a height in pixels.
582 * Standard ratio is 72 / 2540, or 18 / 635 in lowest terms.
583 * Ratio is applied here relative to the standard.
586 fontHeight = MulDiv( This->description.cySize.Lo, This->cyLogical*635, This->cyHimetric*18 );
588 logFont.lfHeight = ((fontHeight%10000L)>5000L) ? (-fontHeight/10000L) - 1 :
589 (-fontHeight/10000L);
590 logFont.lfItalic = This->description.fItalic;
591 logFont.lfUnderline = This->description.fUnderline;
592 logFont.lfStrikeOut = This->description.fStrikethrough;
593 logFont.lfWeight = This->description.sWeight;
594 logFont.lfOutPrecision = OUT_CHARACTER_PRECIS;
595 logFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
596 logFont.lfQuality = DEFAULT_QUALITY;
597 logFont.lfPitchAndFamily = DEFAULT_PITCH;
599 This->gdiFont = CreateFontIndirectW(&logFont);
600 This->dirty = FALSE;
602 add_hfontitem(This->gdiFont);
604 /* Fixup the name and charset properties so that they match the
605 selected font */
606 old_font = SelectObject(get_dc(), This->gdiFont);
607 GetTextFaceW(hdc, ARRAY_SIZE(text_face), text_face);
608 if(lstrcmpiW(text_face, This->description.lpstrName))
610 free(This->description.lpstrName);
611 This->description.lpstrName = wcsdup(text_face);
613 GetTextMetricsW(hdc, &tm);
614 This->description.sCharset = tm.tmCharSet;
615 /* While we have it handy, stash the realized font height for use by get_Size() */
616 This->nRealHeight = tm.tmHeight - tm.tmInternalLeading; /* corresponds to LOGFONT lfHeight */
617 SelectObject(hdc, old_font);
620 /************************************************************************
621 * OLEFontImpl_get_Name (IFont)
623 * See Windows documentation for more details on IFont methods.
625 static HRESULT WINAPI OLEFontImpl_get_Name(
626 IFont* iface,
627 BSTR* pname)
629 OLEFontImpl *this = impl_from_IFont(iface);
630 TRACE("(%p)->(%p)\n", this, pname);
632 if (pname==0)
633 return E_POINTER;
635 realize_font(this);
637 if (this->description.lpstrName!=0)
638 *pname = SysAllocString(this->description.lpstrName);
639 else
640 *pname = 0;
642 return S_OK;
645 /************************************************************************
646 * OLEFontImpl_put_Name (IFont)
648 static HRESULT WINAPI OLEFontImpl_put_Name(
649 IFont* iface,
650 BSTR name)
652 OLEFontImpl *This = impl_from_IFont(iface);
653 TRACE("(%p)->(%p)\n", This, name);
655 if (!name)
656 return CTL_E_INVALIDPROPERTYVALUE;
658 free(This->description.lpstrName);
659 This->description.lpstrName = wcsdup(name);
660 if (!This->description.lpstrName) return E_OUTOFMEMORY;
662 TRACE("new name %s\n", debugstr_w(This->description.lpstrName));
663 OLEFont_SendNotify(This, DISPID_FONT_NAME);
664 return S_OK;
667 /************************************************************************
668 * OLEFontImpl_get_Size (IFont)
670 static HRESULT WINAPI OLEFontImpl_get_Size(
671 IFont* iface,
672 CY* psize)
674 OLEFontImpl *this = impl_from_IFont(iface);
675 TRACE("(%p)->(%p)\n", this, psize);
677 if (!psize) return E_POINTER;
679 realize_font(this);
682 * Convert realized font height in pixels to points descaled by current
683 * scaling ratio then scaled up by 10000.
685 psize->Lo = MulDiv(this->nRealHeight, this->cyHimetric * 72 * 10000, this->cyLogical * 2540);
686 psize->Hi = 0;
688 return S_OK;
691 static HRESULT WINAPI OLEFontImpl_put_Size(IFont *iface, CY size)
693 OLEFontImpl *this = impl_from_IFont(iface);
694 TRACE("%p, %ld.\n", iface, size.Lo);
695 this->description.cySize.Hi = 0;
696 this->description.cySize.Lo = size.Lo;
697 OLEFont_SendNotify(this, DISPID_FONT_SIZE);
699 return S_OK;
702 static HRESULT WINAPI OLEFontImpl_get_Bold(IFont *iface, BOOL *pbold)
704 OLEFontImpl *this = impl_from_IFont(iface);
705 TRACE("(%p)->(%p)\n", this, pbold);
707 if (!pbold) return E_POINTER;
709 realize_font(this);
711 *pbold = this->description.sWeight > 550;
713 return S_OK;
716 /************************************************************************
717 * OLEFontImpl_put_Bold (IFont)
719 static HRESULT WINAPI OLEFontImpl_put_Bold(
720 IFont* iface,
721 BOOL bold)
723 OLEFontImpl *this = impl_from_IFont(iface);
724 TRACE("(%p)->(%d)\n", this, bold);
725 this->description.sWeight = bold ? FW_BOLD : FW_NORMAL;
726 OLEFont_SendNotify(this, DISPID_FONT_BOLD);
728 return S_OK;
731 /************************************************************************
732 * OLEFontImpl_get_Italic (IFont)
734 static HRESULT WINAPI OLEFontImpl_get_Italic(
735 IFont* iface,
736 BOOL* pitalic)
738 OLEFontImpl *this = impl_from_IFont(iface);
739 TRACE("(%p)->(%p)\n", this, pitalic);
741 if (pitalic==0)
742 return E_POINTER;
744 realize_font(this);
746 *pitalic = this->description.fItalic;
748 return S_OK;
751 /************************************************************************
752 * OLEFontImpl_put_Italic (IFont)
754 static HRESULT WINAPI OLEFontImpl_put_Italic(
755 IFont* iface,
756 BOOL italic)
758 OLEFontImpl *this = impl_from_IFont(iface);
759 TRACE("(%p)->(%d)\n", this, italic);
761 this->description.fItalic = italic;
763 OLEFont_SendNotify(this, DISPID_FONT_ITALIC);
764 return S_OK;
767 /************************************************************************
768 * OLEFontImpl_get_Underline (IFont)
770 static HRESULT WINAPI OLEFontImpl_get_Underline(
771 IFont* iface,
772 BOOL* punderline)
774 OLEFontImpl *this = impl_from_IFont(iface);
775 TRACE("(%p)->(%p)\n", this, punderline);
777 if (punderline==0)
778 return E_POINTER;
780 realize_font(this);
782 *punderline = this->description.fUnderline;
784 return S_OK;
787 /************************************************************************
788 * OLEFontImpl_put_Underline (IFont)
790 static HRESULT WINAPI OLEFontImpl_put_Underline(
791 IFont* iface,
792 BOOL underline)
794 OLEFontImpl *this = impl_from_IFont(iface);
795 TRACE("(%p)->(%d)\n", this, underline);
797 this->description.fUnderline = underline;
799 OLEFont_SendNotify(this, DISPID_FONT_UNDER);
800 return S_OK;
803 /************************************************************************
804 * OLEFontImpl_get_Strikethrough (IFont)
806 static HRESULT WINAPI OLEFontImpl_get_Strikethrough(
807 IFont* iface,
808 BOOL* pstrikethrough)
810 OLEFontImpl *this = impl_from_IFont(iface);
811 TRACE("(%p)->(%p)\n", this, pstrikethrough);
813 if (pstrikethrough==0)
814 return E_POINTER;
816 realize_font(this);
818 *pstrikethrough = this->description.fStrikethrough;
820 return S_OK;
823 /************************************************************************
824 * OLEFontImpl_put_Strikethrough (IFont)
826 static HRESULT WINAPI OLEFontImpl_put_Strikethrough(
827 IFont* iface,
828 BOOL strikethrough)
830 OLEFontImpl *this = impl_from_IFont(iface);
831 TRACE("(%p)->(%d)\n", this, strikethrough);
833 this->description.fStrikethrough = strikethrough;
834 OLEFont_SendNotify(this, DISPID_FONT_STRIKE);
836 return S_OK;
839 /************************************************************************
840 * OLEFontImpl_get_Weight (IFont)
842 static HRESULT WINAPI OLEFontImpl_get_Weight(
843 IFont* iface,
844 short* pweight)
846 OLEFontImpl *this = impl_from_IFont(iface);
847 TRACE("(%p)->(%p)\n", this, pweight);
849 if (pweight==0)
850 return E_POINTER;
852 realize_font(this);
854 *pweight = this->description.sWeight;
856 return S_OK;
859 /************************************************************************
860 * OLEFontImpl_put_Weight (IFont)
862 static HRESULT WINAPI OLEFontImpl_put_Weight(
863 IFont* iface,
864 short weight)
866 OLEFontImpl *this = impl_from_IFont(iface);
867 TRACE("(%p)->(%d)\n", this, weight);
869 this->description.sWeight = weight;
871 OLEFont_SendNotify(this, DISPID_FONT_WEIGHT);
872 return S_OK;
875 /************************************************************************
876 * OLEFontImpl_get_Charset (IFont)
878 static HRESULT WINAPI OLEFontImpl_get_Charset(
879 IFont* iface,
880 short* pcharset)
882 OLEFontImpl *this = impl_from_IFont(iface);
883 TRACE("(%p)->(%p)\n", this, pcharset);
885 if (pcharset==0)
886 return E_POINTER;
888 realize_font(this);
890 *pcharset = this->description.sCharset;
892 return S_OK;
895 /************************************************************************
896 * OLEFontImpl_put_Charset (IFont)
898 static HRESULT WINAPI OLEFontImpl_put_Charset(
899 IFont* iface,
900 short charset)
902 OLEFontImpl *this = impl_from_IFont(iface);
903 TRACE("(%p)->(%d)\n", this, charset);
905 this->description.sCharset = charset;
906 OLEFont_SendNotify(this, DISPID_FONT_CHARSET);
908 return S_OK;
911 /************************************************************************
912 * OLEFontImpl_get_hFont (IFont)
914 static HRESULT WINAPI OLEFontImpl_get_hFont(
915 IFont* iface,
916 HFONT* phfont)
918 OLEFontImpl *this = impl_from_IFont(iface);
919 TRACE("(%p)->(%p)\n", this, phfont);
920 if (phfont==NULL)
921 return E_POINTER;
923 realize_font(this);
925 *phfont = this->gdiFont;
926 TRACE("Returning %p\n", *phfont);
927 return S_OK;
930 /************************************************************************
931 * OLEFontImpl_Clone (IFont)
933 static HRESULT WINAPI OLEFontImpl_Clone(
934 IFont* iface,
935 IFont** ppfont)
937 OLEFontImpl *this = impl_from_IFont(iface);
938 OLEFontImpl* newObject;
940 TRACE("(%p)->(%p)\n", this, ppfont);
942 if (ppfont == NULL)
943 return E_POINTER;
945 *ppfont = NULL;
947 newObject = malloc(sizeof(OLEFontImpl));
948 if (newObject==NULL)
949 return E_OUTOFMEMORY;
951 *newObject = *this;
952 /* allocate separate buffer */
953 newObject->description.lpstrName = wcsdup(this->description.lpstrName);
955 /* Increment internal ref in hfont item list */
956 if(newObject->gdiFont) inc_int_ref(newObject->gdiFont);
958 InterlockedIncrement(&ifont_cnt);
960 newObject->pPropertyNotifyCP = NULL;
961 newObject->pFontEventsCP = NULL;
962 CreateConnectionPoint((IUnknown*)&newObject->IFont_iface, &IID_IPropertyNotifySink,
963 &newObject->pPropertyNotifyCP);
964 CreateConnectionPoint((IUnknown*)&newObject->IFont_iface, &IID_IFontEventsDisp,
965 &newObject->pFontEventsCP);
967 if (!newObject->pPropertyNotifyCP || !newObject->pFontEventsCP)
969 OLEFontImpl_Destroy(newObject);
970 return E_OUTOFMEMORY;
973 /* The cloned object starts with a reference count of 1 */
974 newObject->ref = 1;
976 *ppfont = &newObject->IFont_iface;
978 return S_OK;
981 /************************************************************************
982 * OLEFontImpl_IsEqual (IFont)
984 static HRESULT WINAPI OLEFontImpl_IsEqual(
985 IFont* iface,
986 IFont* pFontOther)
988 OLEFontImpl *left = impl_from_IFont(iface);
989 OLEFontImpl *right = impl_from_IFont(pFontOther);
990 INT ret;
991 INT left_len,right_len;
993 if(pFontOther == NULL)
994 return E_POINTER;
995 else if (left->description.cySize.Lo != right->description.cySize.Lo)
996 return S_FALSE;
997 else if (left->description.cySize.Hi != right->description.cySize.Hi)
998 return S_FALSE;
999 else if (left->description.sWeight != right->description.sWeight)
1000 return S_FALSE;
1001 else if (left->description.sCharset != right->description.sCharset)
1002 return S_FALSE;
1003 else if (left->description.fItalic != right->description.fItalic)
1004 return S_FALSE;
1005 else if (left->description.fUnderline != right->description.fUnderline)
1006 return S_FALSE;
1007 else if (left->description.fStrikethrough != right->description.fStrikethrough)
1008 return S_FALSE;
1010 /* Check from string */
1011 left_len = lstrlenW(left->description.lpstrName);
1012 right_len = lstrlenW(right->description.lpstrName);
1013 ret = CompareStringW(0,0,left->description.lpstrName, left_len,
1014 right->description.lpstrName, right_len);
1015 if (ret != CSTR_EQUAL)
1016 return S_FALSE;
1018 return S_OK;
1021 /************************************************************************
1022 * OLEFontImpl_SetRatio (IFont)
1024 static HRESULT WINAPI OLEFontImpl_SetRatio(
1025 IFont* iface,
1026 LONG cyLogical,
1027 LONG cyHimetric)
1029 OLEFontImpl *this = impl_from_IFont(iface);
1031 TRACE("%p, %ld, %ld.\n", iface, cyLogical, cyHimetric);
1033 if(cyLogical == 0 || cyHimetric == 0)
1034 return E_FAIL;
1036 /* cyLogical and cyHimetric both set to 1 is a special case that
1037 does not change the scaling but also does not fail */
1038 if(cyLogical == 1 && cyHimetric == 1)
1039 return S_OK;
1041 this->cyLogical = cyLogical;
1042 this->cyHimetric = cyHimetric;
1043 this->dirty = TRUE;
1045 return S_OK;
1048 /************************************************************************
1049 * OLEFontImpl_QueryTextMetrics (IFont)
1051 static HRESULT WINAPI OLEFontImpl_QueryTextMetrics(
1052 IFont* iface,
1053 TEXTMETRICOLE* ptm)
1055 HDC hdcRef;
1056 HFONT hOldFont, hNewFont;
1058 hdcRef = GetDC(0);
1059 IFont_get_hFont(iface, &hNewFont);
1060 hOldFont = SelectObject(hdcRef, hNewFont);
1061 GetTextMetricsW(hdcRef, ptm);
1062 SelectObject(hdcRef, hOldFont);
1063 ReleaseDC(0, hdcRef);
1064 return S_OK;
1067 /************************************************************************
1068 * OLEFontImpl_AddRefHfont (IFont)
1070 static HRESULT WINAPI OLEFontImpl_AddRefHfont(
1071 IFont* iface,
1072 HFONT hfont)
1074 OLEFontImpl *this = impl_from_IFont(iface);
1076 TRACE("(%p)->(%p)\n", this, hfont);
1078 if (!hfont) return E_INVALIDARG;
1080 return inc_ext_ref(hfont);
1083 /************************************************************************
1084 * OLEFontImpl_ReleaseHfont (IFont)
1086 static HRESULT WINAPI OLEFontImpl_ReleaseHfont(
1087 IFont* iface,
1088 HFONT hfont)
1090 OLEFontImpl *this = impl_from_IFont(iface);
1092 TRACE("(%p)->(%p)\n", this, hfont);
1094 if (!hfont) return E_INVALIDARG;
1096 return dec_ext_ref(hfont);
1099 /************************************************************************
1100 * OLEFontImpl_SetHdc (IFont)
1102 static HRESULT WINAPI OLEFontImpl_SetHdc(
1103 IFont* iface,
1104 HDC hdc)
1106 OLEFontImpl *this = impl_from_IFont(iface);
1107 FIXME("(%p)->(%p): Stub\n", this, hdc);
1108 return E_NOTIMPL;
1111 static const IFontVtbl OLEFontImpl_VTable =
1113 OLEFontImpl_QueryInterface,
1114 OLEFontImpl_AddRef,
1115 OLEFontImpl_Release,
1116 OLEFontImpl_get_Name,
1117 OLEFontImpl_put_Name,
1118 OLEFontImpl_get_Size,
1119 OLEFontImpl_put_Size,
1120 OLEFontImpl_get_Bold,
1121 OLEFontImpl_put_Bold,
1122 OLEFontImpl_get_Italic,
1123 OLEFontImpl_put_Italic,
1124 OLEFontImpl_get_Underline,
1125 OLEFontImpl_put_Underline,
1126 OLEFontImpl_get_Strikethrough,
1127 OLEFontImpl_put_Strikethrough,
1128 OLEFontImpl_get_Weight,
1129 OLEFontImpl_put_Weight,
1130 OLEFontImpl_get_Charset,
1131 OLEFontImpl_put_Charset,
1132 OLEFontImpl_get_hFont,
1133 OLEFontImpl_Clone,
1134 OLEFontImpl_IsEqual,
1135 OLEFontImpl_SetRatio,
1136 OLEFontImpl_QueryTextMetrics,
1137 OLEFontImpl_AddRefHfont,
1138 OLEFontImpl_ReleaseHfont,
1139 OLEFontImpl_SetHdc
1142 /************************************************************************
1143 * OLEFontImpl_IDispatch_QueryInterface (IUnknown)
1145 static HRESULT WINAPI OLEFontImpl_IDispatch_QueryInterface(
1146 IDispatch* iface,
1147 REFIID riid,
1148 VOID** ppvoid)
1150 OLEFontImpl *this = impl_from_IDispatch(iface);
1151 return IFont_QueryInterface(&this->IFont_iface, riid, ppvoid);
1154 /************************************************************************
1155 * OLEFontImpl_IDispatch_Release (IUnknown)
1157 static ULONG WINAPI OLEFontImpl_IDispatch_Release(
1158 IDispatch* iface)
1160 OLEFontImpl *this = impl_from_IDispatch(iface);
1161 return IFont_Release(&this->IFont_iface);
1164 /************************************************************************
1165 * OLEFontImpl_IDispatch_AddRef (IUnknown)
1167 static ULONG WINAPI OLEFontImpl_IDispatch_AddRef(
1168 IDispatch* iface)
1170 OLEFontImpl *this = impl_from_IDispatch(iface);
1171 return IFont_AddRef(&this->IFont_iface);
1174 /************************************************************************
1175 * OLEFontImpl_GetTypeInfoCount (IDispatch)
1177 static HRESULT WINAPI OLEFontImpl_GetTypeInfoCount(
1178 IDispatch* iface,
1179 unsigned int* pctinfo)
1181 OLEFontImpl *this = impl_from_IDispatch(iface);
1182 TRACE("(%p)->(%p)\n", this, pctinfo);
1183 *pctinfo = 1;
1185 return S_OK;
1188 /************************************************************************
1189 * OLEFontImpl_GetTypeInfo (IDispatch)
1191 static HRESULT WINAPI OLEFontImpl_GetTypeInfo(
1192 IDispatch* iface,
1193 UINT iTInfo,
1194 LCID lcid,
1195 ITypeInfo** ppTInfo)
1197 ITypeLib *tl;
1198 HRESULT hres;
1200 OLEFontImpl *this = impl_from_IDispatch(iface);
1201 TRACE("(%p, iTInfo=%d, lcid=%04x, %p)\n", this, iTInfo, (int)lcid, ppTInfo);
1202 if (iTInfo != 0)
1203 return E_FAIL;
1204 hres = LoadTypeLib(L"stdole2.tlb", &tl);
1205 if (FAILED(hres)) {
1206 ERR("Could not load the stdole2.tlb?\n");
1207 return hres;
1209 hres = ITypeLib_GetTypeInfoOfGuid(tl, &IID_IFontDisp, ppTInfo);
1210 ITypeLib_Release(tl);
1211 if (FAILED(hres))
1212 FIXME("Did not IDispatch typeinfo from typelib, hres %#lx.\n", hres);
1214 return hres;
1217 /************************************************************************
1218 * OLEFontImpl_GetIDsOfNames (IDispatch)
1220 static HRESULT WINAPI OLEFontImpl_GetIDsOfNames(
1221 IDispatch* iface,
1222 REFIID riid,
1223 LPOLESTR* rgszNames,
1224 UINT cNames,
1225 LCID lcid,
1226 DISPID* rgDispId)
1228 ITypeInfo * pTInfo;
1229 HRESULT hres;
1231 OLEFontImpl *this = impl_from_IDispatch(iface);
1233 TRACE("(%p,%s,%p,cNames=%d,lcid=%04x,%p)\n", this, debugstr_guid(riid),
1234 rgszNames, cNames, (int)lcid, rgDispId);
1236 if (cNames == 0) return E_INVALIDARG;
1238 hres = IDispatch_GetTypeInfo(iface, 0, lcid, &pTInfo);
1239 if (FAILED(hres))
1241 ERR("GetTypeInfo failed.\n");
1242 return hres;
1245 /* convert names to DISPIDs */
1246 hres = DispGetIDsOfNames (pTInfo, rgszNames, cNames, rgDispId);
1247 ITypeInfo_Release(pTInfo);
1249 return hres;
1252 /************************************************************************
1253 * OLEFontImpl_Invoke (IDispatch)
1256 static HRESULT WINAPI OLEFontImpl_Invoke(
1257 IDispatch* iface,
1258 DISPID dispIdMember,
1259 REFIID riid,
1260 LCID lcid,
1261 WORD wFlags,
1262 DISPPARAMS* pDispParams,
1263 VARIANT* pVarResult,
1264 EXCEPINFO* pExepInfo,
1265 UINT* puArgErr)
1267 OLEFontImpl *this = impl_from_IDispatch(iface);
1268 HRESULT hr;
1270 TRACE("%p, %ld, %s, %#lx, %#x, %p, %p, %p, %p.\n", iface, dispIdMember,
1271 debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult, pExepInfo,
1272 puArgErr);
1274 /* validate parameters */
1276 if (!IsEqualIID(riid, &IID_NULL))
1278 ERR("riid was %s instead of IID_NULL\n", debugstr_guid(riid));
1279 return DISP_E_UNKNOWNINTERFACE;
1282 if (wFlags & DISPATCH_PROPERTYGET)
1284 if (!pVarResult)
1286 ERR("null pVarResult not allowed when DISPATCH_PROPERTYGET specified\n");
1287 return DISP_E_PARAMNOTOPTIONAL;
1290 else if (wFlags & DISPATCH_PROPERTYPUT)
1292 if (!pDispParams)
1294 ERR("null pDispParams not allowed when DISPATCH_PROPERTYPUT specified\n");
1295 return DISP_E_PARAMNOTOPTIONAL;
1297 if (pDispParams->cArgs != 1)
1299 ERR("param count for DISPATCH_PROPERTYPUT was %d instead of 1\n", pDispParams->cArgs);
1300 return DISP_E_BADPARAMCOUNT;
1303 else
1305 ERR("one of DISPATCH_PROPERTYGET or DISPATCH_PROPERTYPUT must be specified\n");
1306 return DISP_E_MEMBERNOTFOUND;
1309 switch (dispIdMember) {
1310 case DISPID_FONT_NAME:
1311 if (wFlags & DISPATCH_PROPERTYGET) {
1312 V_VT(pVarResult) = VT_BSTR;
1313 return IFont_get_Name(&this->IFont_iface, &V_BSTR(pVarResult));
1314 } else {
1315 VARIANTARG vararg;
1317 VariantInit(&vararg);
1318 hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_BSTR);
1319 if (FAILED(hr))
1320 return hr;
1322 hr = IFont_put_Name(&this->IFont_iface, V_BSTR(&vararg));
1324 VariantClear(&vararg);
1325 return hr;
1327 break;
1328 case DISPID_FONT_BOLD:
1329 if (wFlags & DISPATCH_PROPERTYGET) {
1330 BOOL value;
1331 hr = IFont_get_Bold(&this->IFont_iface, &value);
1332 V_VT(pVarResult) = VT_BOOL;
1333 V_BOOL(pVarResult) = value ? VARIANT_TRUE : VARIANT_FALSE;
1334 return hr;
1335 } else {
1336 VARIANTARG vararg;
1338 VariantInit(&vararg);
1339 hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_BOOL);
1340 if (FAILED(hr))
1341 return hr;
1343 hr = IFont_put_Bold(&this->IFont_iface, V_BOOL(&vararg));
1345 VariantClear(&vararg);
1346 return hr;
1348 break;
1349 case DISPID_FONT_ITALIC:
1350 if (wFlags & DISPATCH_PROPERTYGET) {
1351 BOOL value;
1352 hr = IFont_get_Italic(&this->IFont_iface, &value);
1353 V_VT(pVarResult) = VT_BOOL;
1354 V_BOOL(pVarResult) = value ? VARIANT_TRUE : VARIANT_FALSE;
1355 return hr;
1356 } else {
1357 VARIANTARG vararg;
1359 VariantInit(&vararg);
1360 hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_BOOL);
1361 if (FAILED(hr))
1362 return hr;
1364 hr = IFont_put_Italic(&this->IFont_iface, V_BOOL(&vararg));
1366 VariantClear(&vararg);
1367 return hr;
1369 break;
1370 case DISPID_FONT_UNDER:
1371 if (wFlags & DISPATCH_PROPERTYGET) {
1372 BOOL value;
1373 hr = IFont_get_Underline(&this->IFont_iface, &value);
1374 V_VT(pVarResult) = VT_BOOL;
1375 V_BOOL(pVarResult) = value ? VARIANT_TRUE : VARIANT_FALSE;
1376 return hr;
1377 } else {
1378 VARIANTARG vararg;
1380 VariantInit(&vararg);
1381 hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_BOOL);
1382 if (FAILED(hr))
1383 return hr;
1385 hr = IFont_put_Underline(&this->IFont_iface, V_BOOL(&vararg));
1387 VariantClear(&vararg);
1388 return hr;
1390 break;
1391 case DISPID_FONT_STRIKE:
1392 if (wFlags & DISPATCH_PROPERTYGET) {
1393 BOOL value;
1394 hr = IFont_get_Strikethrough(&this->IFont_iface, &value);
1395 V_VT(pVarResult) = VT_BOOL;
1396 V_BOOL(pVarResult) = value ? VARIANT_TRUE : VARIANT_FALSE;
1397 return hr;
1398 } else {
1399 VARIANTARG vararg;
1401 VariantInit(&vararg);
1402 hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_BOOL);
1403 if (FAILED(hr))
1404 return hr;
1406 hr = IFont_put_Strikethrough(&this->IFont_iface, V_BOOL(&vararg));
1408 VariantClear(&vararg);
1409 return hr;
1411 break;
1412 case DISPID_FONT_SIZE:
1413 if (wFlags & DISPATCH_PROPERTYGET) {
1414 V_VT(pVarResult) = VT_CY;
1415 return IFont_get_Size(&this->IFont_iface, &V_CY(pVarResult));
1416 } else {
1417 VARIANTARG vararg;
1419 VariantInit(&vararg);
1420 hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_CY);
1421 if (FAILED(hr))
1422 return hr;
1424 hr = IFont_put_Size(&this->IFont_iface, V_CY(&vararg));
1426 VariantClear(&vararg);
1427 return hr;
1429 break;
1430 case DISPID_FONT_WEIGHT:
1431 if (wFlags & DISPATCH_PROPERTYGET) {
1432 V_VT(pVarResult) = VT_I2;
1433 return IFont_get_Weight(&this->IFont_iface, &V_I2(pVarResult));
1434 } else {
1435 VARIANTARG vararg;
1437 VariantInit(&vararg);
1438 hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_I2);
1439 if (FAILED(hr))
1440 return hr;
1442 hr = IFont_put_Weight(&this->IFont_iface, V_I2(&vararg));
1444 VariantClear(&vararg);
1445 return hr;
1447 break;
1448 case DISPID_FONT_CHARSET:
1449 if (wFlags & DISPATCH_PROPERTYGET) {
1450 V_VT(pVarResult) = VT_I2;
1451 return OLEFontImpl_get_Charset(&this->IFont_iface, &V_I2(pVarResult));
1452 } else {
1453 VARIANTARG vararg;
1455 VariantInit(&vararg);
1456 hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_I2);
1457 if (FAILED(hr))
1458 return hr;
1460 hr = IFont_put_Charset(&this->IFont_iface, V_I2(&vararg));
1462 VariantClear(&vararg);
1463 return hr;
1465 break;
1466 default:
1467 ERR("member not found for dispid %#lx.\n", dispIdMember);
1468 return DISP_E_MEMBERNOTFOUND;
1472 static const IDispatchVtbl OLEFontImpl_IDispatch_VTable =
1474 OLEFontImpl_IDispatch_QueryInterface,
1475 OLEFontImpl_IDispatch_AddRef,
1476 OLEFontImpl_IDispatch_Release,
1477 OLEFontImpl_GetTypeInfoCount,
1478 OLEFontImpl_GetTypeInfo,
1479 OLEFontImpl_GetIDsOfNames,
1480 OLEFontImpl_Invoke
1483 /************************************************************************
1484 * OLEFontImpl_IPersistStream_QueryInterface (IUnknown)
1486 static HRESULT WINAPI OLEFontImpl_IPersistStream_QueryInterface(
1487 IPersistStream* iface,
1488 REFIID riid,
1489 VOID** ppvoid)
1491 OLEFontImpl *this = impl_from_IPersistStream(iface);
1493 return IFont_QueryInterface(&this->IFont_iface, riid, ppvoid);
1496 /************************************************************************
1497 * OLEFontImpl_IPersistStream_Release (IUnknown)
1499 static ULONG WINAPI OLEFontImpl_IPersistStream_Release(
1500 IPersistStream* iface)
1502 OLEFontImpl *this = impl_from_IPersistStream(iface);
1504 return IFont_Release(&this->IFont_iface);
1507 /************************************************************************
1508 * OLEFontImpl_IPersistStream_AddRef (IUnknown)
1510 static ULONG WINAPI OLEFontImpl_IPersistStream_AddRef(
1511 IPersistStream* iface)
1513 OLEFontImpl *this = impl_from_IPersistStream(iface);
1515 return IFont_AddRef(&this->IFont_iface);
1518 /************************************************************************
1519 * OLEFontImpl_GetClassID (IPersistStream)
1521 static HRESULT WINAPI OLEFontImpl_GetClassID(
1522 IPersistStream* iface,
1523 CLSID* pClassID)
1525 TRACE("(%p,%p)\n",iface,pClassID);
1526 if (pClassID==0)
1527 return E_POINTER;
1529 *pClassID = CLSID_StdFont;
1531 return S_OK;
1534 /************************************************************************
1535 * OLEFontImpl_IsDirty (IPersistStream)
1537 * See Windows documentation for more details on IPersistStream methods.
1539 static HRESULT WINAPI OLEFontImpl_IsDirty(
1540 IPersistStream* iface)
1542 TRACE("(%p)\n",iface);
1543 return S_OK;
1546 /************************************************************************
1547 * OLEFontImpl_Load (IPersistStream)
1549 * See Windows documentation for more details on IPersistStream methods.
1551 * This is the format of the standard font serialization as far as I
1552 * know
1554 * Offset Type Value Comment
1555 * 0x0000 Byte Unknown Probably a version number, contains 0x01
1556 * 0x0001 Short Charset Charset value from the FONTDESC structure
1557 * 0x0003 Byte Attributes Flags defined as follows:
1558 * 00000010 - Italic
1559 * 00000100 - Underline
1560 * 00001000 - Strikethrough
1561 * 0x0004 Short Weight Weight value from FONTDESC structure
1562 * 0x0006 DWORD size "Low" portion of the cySize member of the FONTDESC
1563 * structure/
1564 * 0x000A Byte name length Length of the font name string (no null character)
1565 * 0x000B String name Name of the font (ASCII, no nul character)
1567 static HRESULT WINAPI OLEFontImpl_Load(
1568 IPersistStream* iface,
1569 IStream* pLoadStream)
1571 OLEFontImpl *this = impl_from_IPersistStream(iface);
1572 BYTE version, attributes, string_size;
1573 char readBuffer[0x100];
1574 ULONG cbRead;
1575 INT len;
1577 /* Version */
1578 IStream_Read(pLoadStream, &version, sizeof(BYTE), &cbRead);
1579 if ((cbRead != sizeof(BYTE)) || (version != 0x01)) return E_FAIL;
1581 /* Charset */
1582 IStream_Read(pLoadStream, &this->description.sCharset, sizeof(WORD), &cbRead);
1583 if (cbRead != sizeof(WORD)) return E_FAIL;
1585 /* Attributes */
1586 IStream_Read(pLoadStream, &attributes, sizeof(BYTE), &cbRead);
1587 if (cbRead != sizeof(BYTE)) return E_FAIL;
1589 this->description.fItalic = (attributes & FONTPERSIST_ITALIC) != 0;
1590 this->description.fStrikethrough = (attributes & FONTPERSIST_STRIKETHROUGH) != 0;
1591 this->description.fUnderline = (attributes & FONTPERSIST_UNDERLINE) != 0;
1593 /* Weight */
1594 IStream_Read(pLoadStream, &this->description.sWeight, sizeof(WORD), &cbRead);
1595 if (cbRead != sizeof(WORD)) return E_FAIL;
1597 /* Size */
1598 IStream_Read(pLoadStream, &this->description.cySize.Lo, sizeof(DWORD), &cbRead);
1599 if (cbRead != sizeof(DWORD)) return E_FAIL;
1601 this->description.cySize.Hi = 0;
1603 /* Name */
1604 IStream_Read(pLoadStream, &string_size, sizeof(BYTE), &cbRead);
1605 if (cbRead != sizeof(BYTE)) return E_FAIL;
1607 IStream_Read(pLoadStream, readBuffer, string_size, &cbRead);
1608 if (cbRead != string_size) return E_FAIL;
1610 free(this->description.lpstrName);
1612 len = MultiByteToWideChar( CP_ACP, 0, readBuffer, string_size, NULL, 0 );
1613 this->description.lpstrName = malloc((len + 1) * sizeof(WCHAR));
1614 MultiByteToWideChar( CP_ACP, 0, readBuffer, string_size, this->description.lpstrName, len );
1615 this->description.lpstrName[len] = 0;
1617 /* Ensure use of this font causes a new one to be created */
1618 dec_int_ref(this->gdiFont);
1619 this->gdiFont = 0;
1620 this->dirty = TRUE;
1622 return S_OK;
1625 /************************************************************************
1626 * OLEFontImpl_Save (IPersistStream)
1628 static HRESULT WINAPI OLEFontImpl_Save(
1629 IPersistStream* iface,
1630 IStream* pOutStream,
1631 BOOL fClearDirty)
1633 OLEFontImpl *this = impl_from_IPersistStream(iface);
1634 BYTE attributes, string_size;
1635 const BYTE version = 0x01;
1636 char* writeBuffer = NULL;
1637 ULONG written;
1639 TRACE("(%p)->(%p %d)\n", this, pOutStream, fClearDirty);
1641 /* Version */
1642 IStream_Write(pOutStream, &version, sizeof(BYTE), &written);
1643 if (written != sizeof(BYTE)) return E_FAIL;
1645 /* Charset */
1646 IStream_Write(pOutStream, &this->description.sCharset, sizeof(WORD), &written);
1647 if (written != sizeof(WORD)) return E_FAIL;
1649 /* Attributes */
1650 attributes = 0;
1652 if (this->description.fItalic)
1653 attributes |= FONTPERSIST_ITALIC;
1655 if (this->description.fStrikethrough)
1656 attributes |= FONTPERSIST_STRIKETHROUGH;
1658 if (this->description.fUnderline)
1659 attributes |= FONTPERSIST_UNDERLINE;
1661 IStream_Write(pOutStream, &attributes, sizeof(BYTE), &written);
1662 if (written != sizeof(BYTE)) return E_FAIL;
1664 /* Weight */
1665 IStream_Write(pOutStream, &this->description.sWeight, sizeof(WORD), &written);
1666 if (written != sizeof(WORD)) return E_FAIL;
1668 /* Size */
1669 IStream_Write(pOutStream, &this->description.cySize.Lo, sizeof(DWORD), &written);
1670 if (written != sizeof(DWORD)) return E_FAIL;
1672 /* FontName */
1673 if (this->description.lpstrName)
1674 string_size = WideCharToMultiByte( CP_ACP, 0, this->description.lpstrName,
1675 lstrlenW(this->description.lpstrName), NULL, 0, NULL, NULL );
1676 else
1677 string_size = 0;
1679 IStream_Write(pOutStream, &string_size, sizeof(BYTE), &written);
1680 if (written != sizeof(BYTE)) return E_FAIL;
1682 if (string_size)
1684 if (!(writeBuffer = malloc(string_size))) return E_OUTOFMEMORY;
1685 WideCharToMultiByte( CP_ACP, 0, this->description.lpstrName,
1686 lstrlenW(this->description.lpstrName),
1687 writeBuffer, string_size, NULL, NULL );
1689 IStream_Write(pOutStream, writeBuffer, string_size, &written);
1690 free(writeBuffer);
1692 if (written != string_size) return E_FAIL;
1695 return S_OK;
1698 /************************************************************************
1699 * OLEFontImpl_GetSizeMax (IPersistStream)
1701 static HRESULT WINAPI OLEFontImpl_GetSizeMax(
1702 IPersistStream* iface,
1703 ULARGE_INTEGER* pcbSize)
1705 OLEFontImpl *this = impl_from_IPersistStream(iface);
1707 if (pcbSize==NULL)
1708 return E_POINTER;
1710 pcbSize->u.HighPart = 0;
1711 pcbSize->u.LowPart = 0;
1713 pcbSize->u.LowPart += sizeof(BYTE); /* Version */
1714 pcbSize->u.LowPart += sizeof(WORD); /* Lang code */
1715 pcbSize->u.LowPart += sizeof(BYTE); /* Flags */
1716 pcbSize->u.LowPart += sizeof(WORD); /* Weight */
1717 pcbSize->u.LowPart += sizeof(DWORD); /* Size */
1718 pcbSize->u.LowPart += sizeof(BYTE); /* StrLength */
1720 if (this->description.lpstrName!=0)
1721 pcbSize->u.LowPart += WideCharToMultiByte( CP_ACP, 0, this->description.lpstrName,
1722 lstrlenW(this->description.lpstrName),
1723 NULL, 0, NULL, NULL );
1725 return S_OK;
1728 static const IPersistStreamVtbl OLEFontImpl_IPersistStream_VTable =
1730 OLEFontImpl_IPersistStream_QueryInterface,
1731 OLEFontImpl_IPersistStream_AddRef,
1732 OLEFontImpl_IPersistStream_Release,
1733 OLEFontImpl_GetClassID,
1734 OLEFontImpl_IsDirty,
1735 OLEFontImpl_Load,
1736 OLEFontImpl_Save,
1737 OLEFontImpl_GetSizeMax
1740 /************************************************************************
1741 * OLEFontImpl_IConnectionPointContainer_QueryInterface (IUnknown)
1743 static HRESULT WINAPI OLEFontImpl_IConnectionPointContainer_QueryInterface(
1744 IConnectionPointContainer* iface,
1745 REFIID riid,
1746 VOID** ppvoid)
1748 OLEFontImpl *this = impl_from_IConnectionPointContainer(iface);
1750 return IFont_QueryInterface(&this->IFont_iface, riid, ppvoid);
1753 /************************************************************************
1754 * OLEFontImpl_IConnectionPointContainer_Release (IUnknown)
1756 static ULONG WINAPI OLEFontImpl_IConnectionPointContainer_Release(
1757 IConnectionPointContainer* iface)
1759 OLEFontImpl *this = impl_from_IConnectionPointContainer(iface);
1761 return IFont_Release(&this->IFont_iface);
1764 /************************************************************************
1765 * OLEFontImpl_IConnectionPointContainer_AddRef (IUnknown)
1767 static ULONG WINAPI OLEFontImpl_IConnectionPointContainer_AddRef(
1768 IConnectionPointContainer* iface)
1770 OLEFontImpl *this = impl_from_IConnectionPointContainer(iface);
1772 return IFont_AddRef(&this->IFont_iface);
1775 /************************************************************************
1776 * OLEFontImpl_EnumConnectionPoints (IConnectionPointContainer)
1778 static HRESULT WINAPI OLEFontImpl_EnumConnectionPoints(
1779 IConnectionPointContainer* iface,
1780 IEnumConnectionPoints **ppEnum)
1782 OLEFontImpl *this = impl_from_IConnectionPointContainer(iface);
1784 FIXME("(%p)->(%p): stub\n", this, ppEnum);
1785 return E_NOTIMPL;
1788 /************************************************************************
1789 * OLEFontImpl_FindConnectionPoint (IConnectionPointContainer)
1791 static HRESULT WINAPI OLEFontImpl_FindConnectionPoint(
1792 IConnectionPointContainer* iface,
1793 REFIID riid,
1794 IConnectionPoint **ppCp)
1796 OLEFontImpl *this = impl_from_IConnectionPointContainer(iface);
1797 TRACE("(%p)->(%s, %p)\n", this, debugstr_guid(riid), ppCp);
1799 if(IsEqualIID(riid, &IID_IPropertyNotifySink)) {
1800 return IConnectionPoint_QueryInterface(this->pPropertyNotifyCP, &IID_IConnectionPoint,
1801 (void**)ppCp);
1802 } else if(IsEqualIID(riid, &IID_IFontEventsDisp)) {
1803 return IConnectionPoint_QueryInterface(this->pFontEventsCP, &IID_IConnectionPoint,
1804 (void**)ppCp);
1805 } else {
1806 FIXME("no connection point for %s\n", debugstr_guid(riid));
1807 return CONNECT_E_NOCONNECTION;
1811 static const IConnectionPointContainerVtbl
1812 OLEFontImpl_IConnectionPointContainer_VTable =
1814 OLEFontImpl_IConnectionPointContainer_QueryInterface,
1815 OLEFontImpl_IConnectionPointContainer_AddRef,
1816 OLEFontImpl_IConnectionPointContainer_Release,
1817 OLEFontImpl_EnumConnectionPoints,
1818 OLEFontImpl_FindConnectionPoint
1821 /************************************************************************
1822 * OLEFontImpl implementation of IPersistPropertyBag.
1824 static HRESULT WINAPI OLEFontImpl_IPersistPropertyBag_QueryInterface(
1825 IPersistPropertyBag *iface, REFIID riid, LPVOID *ppvObj
1827 OLEFontImpl *this = impl_from_IPersistPropertyBag(iface);
1828 return IFont_QueryInterface(&this->IFont_iface,riid,ppvObj);
1831 static ULONG WINAPI OLEFontImpl_IPersistPropertyBag_AddRef(
1832 IPersistPropertyBag *iface
1834 OLEFontImpl *this = impl_from_IPersistPropertyBag(iface);
1835 return IFont_AddRef(&this->IFont_iface);
1838 static ULONG WINAPI OLEFontImpl_IPersistPropertyBag_Release(
1839 IPersistPropertyBag *iface
1841 OLEFontImpl *this = impl_from_IPersistPropertyBag(iface);
1842 return IFont_Release(&this->IFont_iface);
1845 static HRESULT WINAPI OLEFontImpl_IPersistPropertyBag_GetClassID(
1846 IPersistPropertyBag *iface, CLSID *classid
1848 FIXME("(%p,%p), stub!\n", iface, classid);
1849 return E_FAIL;
1852 static HRESULT WINAPI OLEFontImpl_IPersistPropertyBag_InitNew(
1853 IPersistPropertyBag *iface
1855 FIXME("(%p), stub!\n", iface);
1856 return S_OK;
1859 static HRESULT WINAPI OLEFontImpl_IPersistPropertyBag_Load(
1860 IPersistPropertyBag *iface, IPropertyBag* pPropBag, IErrorLog* pErrorLog
1862 /* (from Visual Basic 6 property bag)
1863 Name = "MS Sans Serif"
1864 Size = 13.8
1865 Charset = 0
1866 Weight = 400
1867 Underline = 0 'False
1868 Italic = 0 'False
1869 Strikethrough = 0 'False
1871 OLEFontImpl *this = impl_from_IPersistPropertyBag(iface);
1872 VARIANT value;
1873 HRESULT iRes;
1875 VariantInit(&value);
1877 iRes = IPropertyBag_Read(pPropBag, L"Name", &value, pErrorLog);
1878 if (iRes == S_OK)
1880 iRes = VariantChangeType(&value, &value, 0, VT_BSTR);
1881 if (iRes == S_OK)
1882 iRes = IFont_put_Name(&this->IFont_iface, V_BSTR(&value));
1884 else if (iRes == E_INVALIDARG)
1885 iRes = S_OK;
1887 VariantClear(&value);
1889 if (iRes == S_OK) {
1890 iRes = IPropertyBag_Read(pPropBag, L"Size", &value, pErrorLog);
1891 if (iRes == S_OK)
1893 iRes = VariantChangeType(&value, &value, 0, VT_CY);
1894 if (iRes == S_OK)
1895 iRes = IFont_put_Size(&this->IFont_iface, V_CY(&value));
1897 else if (iRes == E_INVALIDARG)
1898 iRes = S_OK;
1900 VariantClear(&value);
1903 if (iRes == S_OK) {
1904 iRes = IPropertyBag_Read(pPropBag, L"Charset", &value, pErrorLog);
1905 if (iRes == S_OK)
1907 iRes = VariantChangeType(&value, &value, 0, VT_I2);
1908 if (iRes == S_OK)
1909 iRes = IFont_put_Charset(&this->IFont_iface, V_I2(&value));
1911 else if (iRes == E_INVALIDARG)
1912 iRes = S_OK;
1914 VariantClear(&value);
1917 if (iRes == S_OK) {
1918 iRes = IPropertyBag_Read(pPropBag, L"Weight", &value, pErrorLog);
1919 if (iRes == S_OK)
1921 iRes = VariantChangeType(&value, &value, 0, VT_I2);
1922 if (iRes == S_OK)
1923 iRes = IFont_put_Weight(&this->IFont_iface, V_I2(&value));
1925 else if (iRes == E_INVALIDARG)
1926 iRes = S_OK;
1928 VariantClear(&value);
1931 if (iRes == S_OK) {
1932 iRes = IPropertyBag_Read(pPropBag, L"Underline", &value, pErrorLog);
1933 if (iRes == S_OK)
1935 iRes = VariantChangeType(&value, &value, 0, VT_BOOL);
1936 if (iRes == S_OK)
1937 iRes = IFont_put_Underline(&this->IFont_iface, V_BOOL(&value));
1939 else if (iRes == E_INVALIDARG)
1940 iRes = S_OK;
1942 VariantClear(&value);
1945 if (iRes == S_OK) {
1946 iRes = IPropertyBag_Read(pPropBag, L"Italic", &value, pErrorLog);
1947 if (iRes == S_OK)
1949 iRes = VariantChangeType(&value, &value, 0, VT_BOOL);
1950 if (iRes == S_OK)
1951 iRes = IFont_put_Italic(&this->IFont_iface, V_BOOL(&value));
1953 else if (iRes == E_INVALIDARG)
1954 iRes = S_OK;
1956 VariantClear(&value);
1959 if (iRes == S_OK) {
1960 iRes = IPropertyBag_Read(pPropBag, L"Strikethrough", &value, pErrorLog);
1961 if (iRes == S_OK)
1963 iRes = VariantChangeType(&value, &value, 0, VT_BOOL);
1964 if (iRes == S_OK)
1965 IFont_put_Strikethrough(&this->IFont_iface, V_BOOL(&value));
1967 else if (iRes == E_INVALIDARG)
1968 iRes = S_OK;
1970 VariantClear(&value);
1973 if (FAILED(iRes))
1974 WARN("-- %#lx.\n", iRes);
1975 return iRes;
1978 static HRESULT WINAPI OLEFontImpl_IPersistPropertyBag_Save(
1979 IPersistPropertyBag *iface, IPropertyBag* pPropBag, BOOL fClearDirty,
1980 BOOL fSaveAllProperties
1982 FIXME("(%p,%p,%d,%d), stub!\n", iface, pPropBag, fClearDirty, fSaveAllProperties);
1983 return E_FAIL;
1986 static const IPersistPropertyBagVtbl OLEFontImpl_IPersistPropertyBag_VTable =
1988 OLEFontImpl_IPersistPropertyBag_QueryInterface,
1989 OLEFontImpl_IPersistPropertyBag_AddRef,
1990 OLEFontImpl_IPersistPropertyBag_Release,
1992 OLEFontImpl_IPersistPropertyBag_GetClassID,
1993 OLEFontImpl_IPersistPropertyBag_InitNew,
1994 OLEFontImpl_IPersistPropertyBag_Load,
1995 OLEFontImpl_IPersistPropertyBag_Save
1998 /************************************************************************
1999 * OLEFontImpl_Construct
2001 * This method will construct a new instance of the OLEFontImpl
2002 * class.
2004 * The caller of this method must release the object when it's
2005 * done with it.
2007 static OLEFontImpl* OLEFontImpl_Construct(const FONTDESC *fontDesc)
2009 OLEFontImpl* newObject;
2011 newObject = malloc(sizeof(OLEFontImpl));
2013 if (newObject==0)
2014 return newObject;
2016 newObject->IFont_iface.lpVtbl = &OLEFontImpl_VTable;
2017 newObject->IDispatch_iface.lpVtbl = &OLEFontImpl_IDispatch_VTable;
2018 newObject->IPersistStream_iface.lpVtbl = &OLEFontImpl_IPersistStream_VTable;
2019 newObject->IConnectionPointContainer_iface.lpVtbl = &OLEFontImpl_IConnectionPointContainer_VTable;
2020 newObject->IPersistPropertyBag_iface.lpVtbl = &OLEFontImpl_IPersistPropertyBag_VTable;
2022 newObject->ref = 1;
2024 newObject->description.cbSizeofstruct = sizeof(FONTDESC);
2025 newObject->description.lpstrName = wcsdup(fontDesc->lpstrName);
2026 newObject->description.cySize = fontDesc->cySize;
2027 newObject->description.sWeight = fontDesc->sWeight;
2028 newObject->description.sCharset = fontDesc->sCharset;
2029 newObject->description.fItalic = fontDesc->fItalic;
2030 newObject->description.fUnderline = fontDesc->fUnderline;
2031 newObject->description.fStrikethrough = fontDesc->fStrikethrough;
2033 newObject->gdiFont = 0;
2034 newObject->dirty = TRUE;
2035 newObject->cyLogical = GetDeviceCaps(get_dc(), LOGPIXELSY);
2036 newObject->cyHimetric = 2540L;
2037 newObject->pPropertyNotifyCP = NULL;
2038 newObject->pFontEventsCP = NULL;
2040 CreateConnectionPoint((IUnknown*)&newObject->IFont_iface, &IID_IPropertyNotifySink, &newObject->pPropertyNotifyCP);
2041 CreateConnectionPoint((IUnknown*)&newObject->IFont_iface, &IID_IFontEventsDisp, &newObject->pFontEventsCP);
2043 if (!newObject->pPropertyNotifyCP || !newObject->pFontEventsCP)
2045 OLEFontImpl_Destroy(newObject);
2046 return NULL;
2049 InterlockedIncrement(&ifont_cnt);
2051 TRACE("returning %p\n", newObject);
2052 return newObject;
2055 /************************************************************************
2056 * OLEFontImpl_Destroy
2058 * This method is called by the Release method when the reference
2059 * count goes down to 0. It will free all resources used by
2060 * this object.
2062 static void OLEFontImpl_Destroy(OLEFontImpl* fontDesc)
2064 TRACE("(%p)\n", fontDesc);
2066 free(fontDesc->description.lpstrName);
2068 if (fontDesc->pPropertyNotifyCP)
2069 IConnectionPoint_Release(fontDesc->pPropertyNotifyCP);
2070 if (fontDesc->pFontEventsCP)
2071 IConnectionPoint_Release(fontDesc->pFontEventsCP);
2073 free(fontDesc);
2076 /*******************************************************************************
2077 * StdFont ClassFactory
2079 typedef struct
2081 /* IUnknown fields */
2082 IClassFactory IClassFactory_iface;
2083 LONG ref;
2084 } IClassFactoryImpl;
2086 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
2088 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
2091 static HRESULT WINAPI SFCF_QueryInterface(IClassFactory *iface, REFIID riid, void **obj)
2093 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
2095 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), obj);
2097 *obj = NULL;
2099 if (IsEqualIID(&IID_IClassFactory, riid) || IsEqualIID(&IID_IUnknown, riid))
2101 *obj = iface;
2102 IClassFactory_AddRef(iface);
2103 return S_OK;
2106 return E_NOINTERFACE;
2109 static ULONG WINAPI
2110 SFCF_AddRef(LPCLASSFACTORY iface) {
2111 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
2112 return InterlockedIncrement(&This->ref);
2115 static ULONG WINAPI SFCF_Release(LPCLASSFACTORY iface) {
2116 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
2117 /* static class, won't be freed */
2118 return InterlockedDecrement(&This->ref);
2121 static HRESULT WINAPI SFCF_CreateInstance(
2122 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
2124 return OleCreateFontIndirect(NULL,riid,ppobj);
2128 static HRESULT WINAPI SFCF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
2129 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
2130 FIXME("(%p)->(%d),stub!\n",This,dolock);
2131 return S_OK;
2134 static const IClassFactoryVtbl SFCF_Vtbl = {
2135 SFCF_QueryInterface,
2136 SFCF_AddRef,
2137 SFCF_Release,
2138 SFCF_CreateInstance,
2139 SFCF_LockServer
2141 static IClassFactoryImpl STDFONT_CF = {{&SFCF_Vtbl}, 1 };
2143 void _get_STDFONT_CF(LPVOID *ppv) { *ppv = &STDFONT_CF; }