winewayland.drv: Update desktop window size on display changes.
[wine.git] / dlls / oleaut32 / olefont.c
blobd08c26249b0191f93035400391eaa51b192c4a6e
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 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
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 "objbase.h"
39 #include "oleauto.h" /* for SysAllocString(....) */
40 #include "ole2.h"
41 #include "olectl.h"
42 #include "wine/debug.h"
43 #include "connpt.h" /* for CreateConnectionPoint */
44 #include "oaidl.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(ole);
48 /***********************************************************************
49 * Declaration of constants used when serializing the font object.
51 #define FONTPERSIST_ITALIC 0x02
52 #define FONTPERSIST_UNDERLINE 0x04
53 #define FONTPERSIST_STRIKETHROUGH 0x08
55 static HDC olefont_hdc;
57 /***********************************************************************
58 * List of the HFONTs it has given out, with each one having a separate
59 * ref count.
61 typedef struct _HFONTItem
63 struct list entry;
65 /* Reference count of any IFont objects that own this hfont */
66 LONG int_refs;
68 /* Total reference count of any refs held by the application obtained by AddRefHfont plus any internal refs */
69 LONG total_refs;
71 /* The font associated with this object. */
72 HFONT gdiFont;
74 } HFONTItem, *PHFONTItem;
76 static struct list OLEFontImpl_hFontList = LIST_INIT(OLEFontImpl_hFontList);
78 /* Counts how many fonts contain at least one lock */
79 static LONG ifont_cnt = 0;
81 /***********************************************************************
82 * Critical section for OLEFontImpl_hFontList
84 static CRITICAL_SECTION OLEFontImpl_csHFONTLIST;
85 static CRITICAL_SECTION_DEBUG OLEFontImpl_csHFONTLIST_debug =
87 0, 0, &OLEFontImpl_csHFONTLIST,
88 { &OLEFontImpl_csHFONTLIST_debug.ProcessLocksList,
89 &OLEFontImpl_csHFONTLIST_debug.ProcessLocksList },
90 0, 0, { (DWORD_PTR)(__FILE__ ": OLEFontImpl_csHFONTLIST") }
92 static CRITICAL_SECTION OLEFontImpl_csHFONTLIST = { &OLEFontImpl_csHFONTLIST_debug, -1, 0, 0, 0, 0 };
94 static HDC get_dc(void)
96 HDC hdc;
97 EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
98 if(!olefont_hdc)
99 olefont_hdc = CreateCompatibleDC(NULL);
100 hdc = olefont_hdc;
101 LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
102 return hdc;
105 static void delete_dc(void)
107 EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
108 if(olefont_hdc)
110 DeleteDC(olefont_hdc);
111 olefont_hdc = NULL;
113 LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
116 static void HFONTItem_Delete(PHFONTItem item)
118 DeleteObject(item->gdiFont);
119 list_remove(&item->entry);
120 free(item);
123 /* Find hfont item entry in the list. Should be called while holding the crit sect */
124 static HFONTItem *find_hfontitem(HFONT hfont)
126 HFONTItem *item;
128 LIST_FOR_EACH_ENTRY(item, &OLEFontImpl_hFontList, HFONTItem, entry)
130 if (item->gdiFont == hfont)
131 return item;
133 return NULL;
136 /* Add an item to the list with one internal reference */
137 static HRESULT add_hfontitem(HFONT hfont)
139 HFONTItem *new_item = malloc(sizeof(*new_item));
141 if(!new_item) return E_OUTOFMEMORY;
143 new_item->int_refs = 1;
144 new_item->total_refs = 1;
145 new_item->gdiFont = hfont;
146 EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
147 list_add_tail(&OLEFontImpl_hFontList,&new_item->entry);
148 LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
149 return S_OK;
152 static HRESULT inc_int_ref(HFONT hfont)
154 HFONTItem *item;
155 HRESULT hr = S_FALSE;
157 EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
158 item = find_hfontitem(hfont);
160 if(item)
162 item->int_refs++;
163 item->total_refs++;
164 hr = S_OK;
166 LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
168 return hr;
171 /* decrements the internal ref of a hfont item. If both refs are zero it'll
172 remove the item from the list and delete the hfont */
173 static HRESULT dec_int_ref(HFONT hfont)
175 HFONTItem *item;
176 HRESULT hr = S_FALSE;
178 EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
179 item = find_hfontitem(hfont);
181 if(item)
183 item->int_refs--;
184 item->total_refs--;
185 if(item->int_refs == 0 && item->total_refs == 0)
186 HFONTItem_Delete(item);
187 hr = S_OK;
189 LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
191 return hr;
194 static HRESULT inc_ext_ref(HFONT hfont)
196 HFONTItem *item;
197 HRESULT hr = S_FALSE;
199 EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
201 item = find_hfontitem(hfont);
202 if(item)
204 item->total_refs++;
205 hr = S_OK;
207 LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
209 return hr;
212 static HRESULT dec_ext_ref(HFONT hfont)
214 HFONTItem *item;
215 HRESULT hr = S_FALSE;
217 EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
219 item = find_hfontitem(hfont);
220 if(item)
222 if(--item->total_refs >= 0) hr = S_OK;
224 LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
226 return hr;
229 /***********************************************************************
230 * Declaration of the implementation class for the IFont interface
232 typedef struct OLEFontImpl OLEFontImpl;
234 struct OLEFontImpl
237 * This class supports many interfaces. IUnknown, IFont,
238 * IDispatch, IDispFont IPersistStream and IConnectionPointContainer.
239 * The first two are supported by the first vtable, the next two are
240 * supported by the second table and the last two have their own.
242 IFont IFont_iface;
243 IDispatch IDispatch_iface;
244 IPersistStream IPersistStream_iface;
245 IConnectionPointContainer IConnectionPointContainer_iface;
246 IPersistPropertyBag IPersistPropertyBag_iface;
248 * Reference count for that instance of the class.
250 LONG ref;
253 * This structure contains the description of the class.
255 FONTDESC description;
258 * Contain the font associated with this object.
260 HFONT gdiFont;
261 BOOL dirty;
263 * Size ratio
265 LONG cyLogical;
266 LONG cyHimetric;
269 * Stash realized height (pixels) from TEXTMETRIC - used in get_Size()
271 LONG nRealHeight;
273 IConnectionPoint *pPropertyNotifyCP;
274 IConnectionPoint *pFontEventsCP;
277 static inline OLEFontImpl *impl_from_IFont(IFont *iface)
279 return CONTAINING_RECORD(iface, OLEFontImpl, IFont_iface);
282 static inline OLEFontImpl *impl_from_IDispatch( IDispatch *iface )
284 return CONTAINING_RECORD(iface, OLEFontImpl, IDispatch_iface);
287 static inline OLEFontImpl *impl_from_IPersistStream( IPersistStream *iface )
289 return CONTAINING_RECORD(iface, OLEFontImpl, IPersistStream_iface);
292 static inline OLEFontImpl *impl_from_IConnectionPointContainer( IConnectionPointContainer *iface )
294 return CONTAINING_RECORD(iface, OLEFontImpl, IConnectionPointContainer_iface);
297 static inline OLEFontImpl *impl_from_IPersistPropertyBag( IPersistPropertyBag *iface )
299 return CONTAINING_RECORD(iface, OLEFontImpl, IPersistPropertyBag_iface);
303 /***********************************************************************
304 * Prototypes for the implementation functions for the IFont
305 * interface
307 static OLEFontImpl* OLEFontImpl_Construct(const FONTDESC *fontDesc);
308 static void OLEFontImpl_Destroy(OLEFontImpl* fontDesc);
309 static ULONG WINAPI OLEFontImpl_AddRef(IFont* iface);
311 /******************************************************************************
312 * OleCreateFontIndirect [OLEAUT32.420]
314 HRESULT WINAPI OleCreateFontIndirect(
315 LPFONTDESC lpFontDesc,
316 REFIID riid,
317 LPVOID* ppvObj)
319 OLEFontImpl* newFont;
320 HRESULT hr;
321 FONTDESC fd;
323 TRACE("(%p, %s, %p)\n", lpFontDesc, debugstr_guid(riid), ppvObj);
325 if (!ppvObj) return E_POINTER;
327 *ppvObj = 0;
329 if (!lpFontDesc) {
330 static WCHAR fname[] = L"System";
332 fd.cbSizeofstruct = sizeof(fd);
333 fd.lpstrName = fname;
334 fd.cySize.s.Lo = 80000;
335 fd.cySize.s.Hi = 0;
336 fd.sWeight = 0;
337 fd.sCharset = 0;
338 fd.fItalic = FALSE;
339 fd.fUnderline = FALSE;
340 fd.fStrikethrough = FALSE;
341 lpFontDesc = &fd;
344 newFont = OLEFontImpl_Construct(lpFontDesc);
345 if (!newFont) return E_OUTOFMEMORY;
347 hr = IFont_QueryInterface(&newFont->IFont_iface, riid, ppvObj);
348 IFont_Release(&newFont->IFont_iface);
350 return hr;
354 /***********************************************************************
355 * Implementation of the OLEFontImpl class.
358 /***********************************************************************
359 * OLEFont_SendNotify (internal)
361 * Sends notification messages of changed properties to any interested
362 * connections.
364 static void OLEFont_SendNotify(OLEFontImpl* this, DISPID dispID)
366 static const LPCWSTR dispid_mapping[] =
368 L"Name",
369 NULL,
370 L"Size",
371 L"Bold",
372 L"Italic",
373 L"Underline",
374 L"Strikethrough",
375 L"Weight",
376 L"Charset"
379 IEnumConnections *pEnum;
380 CONNECTDATA CD;
381 HRESULT hres;
383 this->dirty = TRUE;
385 hres = IConnectionPoint_EnumConnections(this->pPropertyNotifyCP, &pEnum);
386 if (SUCCEEDED(hres))
388 while(IEnumConnections_Next(pEnum, 1, &CD, NULL) == S_OK) {
389 IPropertyNotifySink *sink;
391 IUnknown_QueryInterface(CD.pUnk, &IID_IPropertyNotifySink, (void**)&sink);
392 IPropertyNotifySink_OnChanged(sink, dispID);
393 IPropertyNotifySink_Release(sink);
394 IUnknown_Release(CD.pUnk);
396 IEnumConnections_Release(pEnum);
399 hres = IConnectionPoint_EnumConnections(this->pFontEventsCP, &pEnum);
400 if (SUCCEEDED(hres))
402 DISPPARAMS dispparams;
403 VARIANTARG vararg;
405 VariantInit(&vararg);
406 V_VT(&vararg) = VT_BSTR;
407 V_BSTR(&vararg) = SysAllocString(dispid_mapping[dispID]);
409 dispparams.cArgs = 1;
410 dispparams.cNamedArgs = 0;
411 dispparams.rgdispidNamedArgs = NULL;
412 dispparams.rgvarg = &vararg;
414 while(IEnumConnections_Next(pEnum, 1, &CD, NULL) == S_OK) {
415 IFontEventsDisp *disp;
417 IUnknown_QueryInterface(CD.pUnk, &IID_IFontEventsDisp, (void**)&disp);
418 IFontEventsDisp_Invoke(disp, DISPID_FONT_CHANGED, &IID_NULL,
419 LOCALE_NEUTRAL, INVOKE_FUNC, &dispparams, NULL,
420 NULL, NULL);
422 IFontEventsDisp_Release(disp);
423 IUnknown_Release(CD.pUnk);
425 VariantClear(&vararg);
426 IEnumConnections_Release(pEnum);
430 /************************************************************************
431 * OLEFontImpl_QueryInterface (IUnknown)
433 * See Windows documentation for more details on IUnknown methods.
435 static HRESULT WINAPI OLEFontImpl_QueryInterface(
436 IFont* iface,
437 REFIID riid,
438 void** ppvObject)
440 OLEFontImpl *this = impl_from_IFont(iface);
442 TRACE("(%p)->(%s, %p)\n", this, debugstr_guid(riid), ppvObject);
444 *ppvObject = 0;
446 if (IsEqualGUID(&IID_IUnknown, riid) ||
447 IsEqualGUID(&IID_IFont, riid))
449 *ppvObject = this;
451 else if (IsEqualGUID(&IID_IDispatch, riid) ||
452 IsEqualGUID(&IID_IFontDisp, riid))
454 *ppvObject = &this->IDispatch_iface;
456 else if (IsEqualGUID(&IID_IPersist, riid) ||
457 IsEqualGUID(&IID_IPersistStream, riid))
459 *ppvObject = &this->IPersistStream_iface;
461 else if (IsEqualGUID(&IID_IConnectionPointContainer, riid))
463 *ppvObject = &this->IConnectionPointContainer_iface;
465 else if (IsEqualGUID(&IID_IPersistPropertyBag, riid))
467 *ppvObject = &this->IPersistPropertyBag_iface;
470 if (!*ppvObject)
472 FIXME("() : asking for unsupported interface %s\n", debugstr_guid(riid));
473 return E_NOINTERFACE;
476 IFont_AddRef(iface);
478 return S_OK;
481 static ULONG WINAPI OLEFontImpl_AddRef(IFont* iface)
483 OLEFontImpl *this = impl_from_IFont(iface);
484 ULONG ref = InterlockedIncrement(&this->ref);
485 TRACE("%p, refcount %lu.\n", iface, ref);
486 return ref;
489 static ULONG WINAPI OLEFontImpl_Release(IFont* iface)
491 OLEFontImpl *this = impl_from_IFont(iface);
492 ULONG ref = InterlockedDecrement(&this->ref);
494 TRACE("%p, refcount %lu.\n", iface, ref);
496 if (ref == 0)
498 ULONG fontlist_refs = InterlockedDecrement(&ifont_cnt);
500 /* Final IFont object so destroy font cache */
501 if (fontlist_refs == 0)
503 HFONTItem *item, *cursor2;
505 EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
506 LIST_FOR_EACH_ENTRY_SAFE(item, cursor2, &OLEFontImpl_hFontList, HFONTItem, entry)
507 HFONTItem_Delete(item);
508 LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
509 delete_dc();
511 else
513 dec_int_ref(this->gdiFont);
515 OLEFontImpl_Destroy(this);
518 return ref;
521 typedef struct
523 short orig_cs;
524 short avail_cs;
525 } enum_data;
527 static int CALLBACK font_enum_proc(const LOGFONTW *elf, const TEXTMETRICW *ntm, DWORD type, LPARAM lp)
529 enum_data *data = (enum_data*)lp;
531 if(elf->lfCharSet == data->orig_cs)
533 data->avail_cs = data->orig_cs;
534 return 0;
536 if(data->avail_cs == -1) data->avail_cs = elf->lfCharSet;
537 return 1;
540 static void realize_font(OLEFontImpl *This)
542 LOGFONTW logFont;
543 INT fontHeight;
544 WCHAR text_face[LF_FACESIZE];
545 HDC hdc = get_dc();
546 HFONT old_font;
547 TEXTMETRICW tm;
549 if (!This->dirty) return;
551 text_face[0] = 0;
553 if(This->gdiFont)
555 old_font = SelectObject(hdc, This->gdiFont);
556 GetTextFaceW(hdc, ARRAY_SIZE(text_face), text_face);
557 SelectObject(hdc, old_font);
558 dec_int_ref(This->gdiFont);
559 This->gdiFont = 0;
562 memset(&logFont, 0, sizeof(LOGFONTW));
564 lstrcpynW(logFont.lfFaceName, This->description.lpstrName, LF_FACESIZE);
565 logFont.lfCharSet = This->description.sCharset;
567 /* If the font name has been changed then enumerate all charsets
568 and pick one that'll result in the font specified being selected */
569 if(text_face[0] && lstrcmpiW(text_face, This->description.lpstrName))
571 enum_data data;
572 data.orig_cs = This->description.sCharset;
573 data.avail_cs = -1;
574 logFont.lfCharSet = DEFAULT_CHARSET;
575 EnumFontFamiliesExW(get_dc(), &logFont, font_enum_proc, (LPARAM)&data, 0);
576 if(data.avail_cs != -1) logFont.lfCharSet = data.avail_cs;
580 * The height of the font returned by the get_Size property is the
581 * height of the font in points multiplied by 10000... Using some
582 * simple conversions and the ratio given by the application, it can
583 * be converted to a height in pixels.
585 * Standard ratio is 72 / 2540, or 18 / 635 in lowest terms.
586 * Ratio is applied here relative to the standard.
589 fontHeight = MulDiv( This->description.cySize.s.Lo, This->cyLogical*635, This->cyHimetric*18 );
591 logFont.lfHeight = ((fontHeight%10000L)>5000L) ? (-fontHeight/10000L) - 1 :
592 (-fontHeight/10000L);
593 logFont.lfItalic = This->description.fItalic;
594 logFont.lfUnderline = This->description.fUnderline;
595 logFont.lfStrikeOut = This->description.fStrikethrough;
596 logFont.lfWeight = This->description.sWeight;
597 logFont.lfOutPrecision = OUT_CHARACTER_PRECIS;
598 logFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
599 logFont.lfQuality = DEFAULT_QUALITY;
600 logFont.lfPitchAndFamily = DEFAULT_PITCH;
602 This->gdiFont = CreateFontIndirectW(&logFont);
603 This->dirty = FALSE;
605 add_hfontitem(This->gdiFont);
607 /* Fixup the name and charset properties so that they match the
608 selected font */
609 old_font = SelectObject(get_dc(), This->gdiFont);
610 GetTextFaceW(hdc, ARRAY_SIZE(text_face), text_face);
611 if(lstrcmpiW(text_face, This->description.lpstrName))
613 free(This->description.lpstrName);
614 This->description.lpstrName = wcsdup(text_face);
616 GetTextMetricsW(hdc, &tm);
617 This->description.sCharset = tm.tmCharSet;
618 /* While we have it handy, stash the realized font height for use by get_Size() */
619 This->nRealHeight = tm.tmHeight - tm.tmInternalLeading; /* corresponds to LOGFONT lfHeight */
620 SelectObject(hdc, old_font);
623 /************************************************************************
624 * OLEFontImpl_get_Name (IFont)
626 * See Windows documentation for more details on IFont methods.
628 static HRESULT WINAPI OLEFontImpl_get_Name(
629 IFont* iface,
630 BSTR* pname)
632 OLEFontImpl *this = impl_from_IFont(iface);
633 TRACE("(%p)->(%p)\n", this, pname);
635 if (pname==0)
636 return E_POINTER;
638 realize_font(this);
640 if (this->description.lpstrName!=0)
641 *pname = SysAllocString(this->description.lpstrName);
642 else
643 *pname = 0;
645 return S_OK;
648 /************************************************************************
649 * OLEFontImpl_put_Name (IFont)
651 static HRESULT WINAPI OLEFontImpl_put_Name(
652 IFont* iface,
653 BSTR name)
655 OLEFontImpl *This = impl_from_IFont(iface);
656 TRACE("(%p)->(%p)\n", This, name);
658 if (!name)
659 return CTL_E_INVALIDPROPERTYVALUE;
661 free(This->description.lpstrName);
662 This->description.lpstrName = wcsdup(name);
663 if (!This->description.lpstrName) return E_OUTOFMEMORY;
665 TRACE("new name %s\n", debugstr_w(This->description.lpstrName));
666 OLEFont_SendNotify(This, DISPID_FONT_NAME);
667 return S_OK;
670 /************************************************************************
671 * OLEFontImpl_get_Size (IFont)
673 static HRESULT WINAPI OLEFontImpl_get_Size(
674 IFont* iface,
675 CY* psize)
677 OLEFontImpl *this = impl_from_IFont(iface);
678 TRACE("(%p)->(%p)\n", this, psize);
680 if (!psize) return E_POINTER;
682 realize_font(this);
685 * Convert realized font height in pixels to points descaled by current
686 * scaling ratio then scaled up by 10000.
688 psize->s.Lo = MulDiv(this->nRealHeight,
689 this->cyHimetric * 72 * 10000,
690 this->cyLogical * 2540);
691 psize->s.Hi = 0;
693 return S_OK;
696 static HRESULT WINAPI OLEFontImpl_put_Size(IFont *iface, CY size)
698 OLEFontImpl *this = impl_from_IFont(iface);
699 TRACE("%p, %ld.\n", iface, size.s.Lo);
700 this->description.cySize.s.Hi = 0;
701 this->description.cySize.s.Lo = size.s.Lo;
702 OLEFont_SendNotify(this, DISPID_FONT_SIZE);
704 return S_OK;
707 static HRESULT WINAPI OLEFontImpl_get_Bold(IFont *iface, BOOL *pbold)
709 OLEFontImpl *this = impl_from_IFont(iface);
710 TRACE("(%p)->(%p)\n", this, pbold);
712 if (!pbold) return E_POINTER;
714 realize_font(this);
716 *pbold = this->description.sWeight > 550;
718 return S_OK;
721 /************************************************************************
722 * OLEFontImpl_put_Bold (IFont)
724 static HRESULT WINAPI OLEFontImpl_put_Bold(
725 IFont* iface,
726 BOOL bold)
728 OLEFontImpl *this = impl_from_IFont(iface);
729 TRACE("(%p)->(%d)\n", this, bold);
730 this->description.sWeight = bold ? FW_BOLD : FW_NORMAL;
731 OLEFont_SendNotify(this, DISPID_FONT_BOLD);
733 return S_OK;
736 /************************************************************************
737 * OLEFontImpl_get_Italic (IFont)
739 static HRESULT WINAPI OLEFontImpl_get_Italic(
740 IFont* iface,
741 BOOL* pitalic)
743 OLEFontImpl *this = impl_from_IFont(iface);
744 TRACE("(%p)->(%p)\n", this, pitalic);
746 if (pitalic==0)
747 return E_POINTER;
749 realize_font(this);
751 *pitalic = this->description.fItalic;
753 return S_OK;
756 /************************************************************************
757 * OLEFontImpl_put_Italic (IFont)
759 static HRESULT WINAPI OLEFontImpl_put_Italic(
760 IFont* iface,
761 BOOL italic)
763 OLEFontImpl *this = impl_from_IFont(iface);
764 TRACE("(%p)->(%d)\n", this, italic);
766 this->description.fItalic = italic;
768 OLEFont_SendNotify(this, DISPID_FONT_ITALIC);
769 return S_OK;
772 /************************************************************************
773 * OLEFontImpl_get_Underline (IFont)
775 static HRESULT WINAPI OLEFontImpl_get_Underline(
776 IFont* iface,
777 BOOL* punderline)
779 OLEFontImpl *this = impl_from_IFont(iface);
780 TRACE("(%p)->(%p)\n", this, punderline);
782 if (punderline==0)
783 return E_POINTER;
785 realize_font(this);
787 *punderline = this->description.fUnderline;
789 return S_OK;
792 /************************************************************************
793 * OLEFontImpl_put_Underline (IFont)
795 static HRESULT WINAPI OLEFontImpl_put_Underline(
796 IFont* iface,
797 BOOL underline)
799 OLEFontImpl *this = impl_from_IFont(iface);
800 TRACE("(%p)->(%d)\n", this, underline);
802 this->description.fUnderline = underline;
804 OLEFont_SendNotify(this, DISPID_FONT_UNDER);
805 return S_OK;
808 /************************************************************************
809 * OLEFontImpl_get_Strikethrough (IFont)
811 static HRESULT WINAPI OLEFontImpl_get_Strikethrough(
812 IFont* iface,
813 BOOL* pstrikethrough)
815 OLEFontImpl *this = impl_from_IFont(iface);
816 TRACE("(%p)->(%p)\n", this, pstrikethrough);
818 if (pstrikethrough==0)
819 return E_POINTER;
821 realize_font(this);
823 *pstrikethrough = this->description.fStrikethrough;
825 return S_OK;
828 /************************************************************************
829 * OLEFontImpl_put_Strikethrough (IFont)
831 static HRESULT WINAPI OLEFontImpl_put_Strikethrough(
832 IFont* iface,
833 BOOL strikethrough)
835 OLEFontImpl *this = impl_from_IFont(iface);
836 TRACE("(%p)->(%d)\n", this, strikethrough);
838 this->description.fStrikethrough = strikethrough;
839 OLEFont_SendNotify(this, DISPID_FONT_STRIKE);
841 return S_OK;
844 /************************************************************************
845 * OLEFontImpl_get_Weight (IFont)
847 static HRESULT WINAPI OLEFontImpl_get_Weight(
848 IFont* iface,
849 short* pweight)
851 OLEFontImpl *this = impl_from_IFont(iface);
852 TRACE("(%p)->(%p)\n", this, pweight);
854 if (pweight==0)
855 return E_POINTER;
857 realize_font(this);
859 *pweight = this->description.sWeight;
861 return S_OK;
864 /************************************************************************
865 * OLEFontImpl_put_Weight (IFont)
867 static HRESULT WINAPI OLEFontImpl_put_Weight(
868 IFont* iface,
869 short weight)
871 OLEFontImpl *this = impl_from_IFont(iface);
872 TRACE("(%p)->(%d)\n", this, weight);
874 this->description.sWeight = weight;
876 OLEFont_SendNotify(this, DISPID_FONT_WEIGHT);
877 return S_OK;
880 /************************************************************************
881 * OLEFontImpl_get_Charset (IFont)
883 static HRESULT WINAPI OLEFontImpl_get_Charset(
884 IFont* iface,
885 short* pcharset)
887 OLEFontImpl *this = impl_from_IFont(iface);
888 TRACE("(%p)->(%p)\n", this, pcharset);
890 if (pcharset==0)
891 return E_POINTER;
893 realize_font(this);
895 *pcharset = this->description.sCharset;
897 return S_OK;
900 /************************************************************************
901 * OLEFontImpl_put_Charset (IFont)
903 static HRESULT WINAPI OLEFontImpl_put_Charset(
904 IFont* iface,
905 short charset)
907 OLEFontImpl *this = impl_from_IFont(iface);
908 TRACE("(%p)->(%d)\n", this, charset);
910 this->description.sCharset = charset;
911 OLEFont_SendNotify(this, DISPID_FONT_CHARSET);
913 return S_OK;
916 /************************************************************************
917 * OLEFontImpl_get_hFont (IFont)
919 static HRESULT WINAPI OLEFontImpl_get_hFont(
920 IFont* iface,
921 HFONT* phfont)
923 OLEFontImpl *this = impl_from_IFont(iface);
924 TRACE("(%p)->(%p)\n", this, phfont);
925 if (phfont==NULL)
926 return E_POINTER;
928 realize_font(this);
930 *phfont = this->gdiFont;
931 TRACE("Returning %p\n", *phfont);
932 return S_OK;
935 /************************************************************************
936 * OLEFontImpl_Clone (IFont)
938 static HRESULT WINAPI OLEFontImpl_Clone(
939 IFont* iface,
940 IFont** ppfont)
942 OLEFontImpl *this = impl_from_IFont(iface);
943 OLEFontImpl* newObject;
945 TRACE("(%p)->(%p)\n", this, ppfont);
947 if (ppfont == NULL)
948 return E_POINTER;
950 *ppfont = NULL;
952 newObject = malloc(sizeof(OLEFontImpl));
953 if (newObject==NULL)
954 return E_OUTOFMEMORY;
956 *newObject = *this;
957 /* allocate separate buffer */
958 newObject->description.lpstrName = wcsdup(this->description.lpstrName);
960 /* Increment internal ref in hfont item list */
961 if(newObject->gdiFont) inc_int_ref(newObject->gdiFont);
963 InterlockedIncrement(&ifont_cnt);
965 newObject->pPropertyNotifyCP = NULL;
966 newObject->pFontEventsCP = NULL;
967 CreateConnectionPoint((IUnknown*)&newObject->IFont_iface, &IID_IPropertyNotifySink,
968 &newObject->pPropertyNotifyCP);
969 CreateConnectionPoint((IUnknown*)&newObject->IFont_iface, &IID_IFontEventsDisp,
970 &newObject->pFontEventsCP);
972 if (!newObject->pPropertyNotifyCP || !newObject->pFontEventsCP)
974 OLEFontImpl_Destroy(newObject);
975 return E_OUTOFMEMORY;
978 /* The cloned object starts with a reference count of 1 */
979 newObject->ref = 1;
981 *ppfont = &newObject->IFont_iface;
983 return S_OK;
986 /************************************************************************
987 * OLEFontImpl_IsEqual (IFont)
989 static HRESULT WINAPI OLEFontImpl_IsEqual(
990 IFont* iface,
991 IFont* pFontOther)
993 OLEFontImpl *left = impl_from_IFont(iface);
994 OLEFontImpl *right = impl_from_IFont(pFontOther);
995 INT ret;
996 INT left_len,right_len;
998 if(pFontOther == NULL)
999 return E_POINTER;
1000 else if (left->description.cySize.s.Lo != right->description.cySize.s.Lo)
1001 return S_FALSE;
1002 else if (left->description.cySize.s.Hi != right->description.cySize.s.Hi)
1003 return S_FALSE;
1004 else if (left->description.sWeight != right->description.sWeight)
1005 return S_FALSE;
1006 else if (left->description.sCharset != right->description.sCharset)
1007 return S_FALSE;
1008 else if (left->description.fItalic != right->description.fItalic)
1009 return S_FALSE;
1010 else if (left->description.fUnderline != right->description.fUnderline)
1011 return S_FALSE;
1012 else if (left->description.fStrikethrough != right->description.fStrikethrough)
1013 return S_FALSE;
1015 /* Check from string */
1016 left_len = lstrlenW(left->description.lpstrName);
1017 right_len = lstrlenW(right->description.lpstrName);
1018 ret = CompareStringW(0,0,left->description.lpstrName, left_len,
1019 right->description.lpstrName, right_len);
1020 if (ret != CSTR_EQUAL)
1021 return S_FALSE;
1023 return S_OK;
1026 /************************************************************************
1027 * OLEFontImpl_SetRatio (IFont)
1029 static HRESULT WINAPI OLEFontImpl_SetRatio(
1030 IFont* iface,
1031 LONG cyLogical,
1032 LONG cyHimetric)
1034 OLEFontImpl *this = impl_from_IFont(iface);
1036 TRACE("%p, %ld, %ld.\n", iface, cyLogical, cyHimetric);
1038 if(cyLogical == 0 || cyHimetric == 0)
1039 return E_FAIL;
1041 /* cyLogical and cyHimetric both set to 1 is a special case that
1042 does not change the scaling but also does not fail */
1043 if(cyLogical == 1 && cyHimetric == 1)
1044 return S_OK;
1046 this->cyLogical = cyLogical;
1047 this->cyHimetric = cyHimetric;
1048 this->dirty = TRUE;
1050 return S_OK;
1053 /************************************************************************
1054 * OLEFontImpl_QueryTextMetrics (IFont)
1056 static HRESULT WINAPI OLEFontImpl_QueryTextMetrics(
1057 IFont* iface,
1058 TEXTMETRICOLE* ptm)
1060 HDC hdcRef;
1061 HFONT hOldFont, hNewFont;
1063 hdcRef = GetDC(0);
1064 IFont_get_hFont(iface, &hNewFont);
1065 hOldFont = SelectObject(hdcRef, hNewFont);
1066 GetTextMetricsW(hdcRef, ptm);
1067 SelectObject(hdcRef, hOldFont);
1068 ReleaseDC(0, hdcRef);
1069 return S_OK;
1072 /************************************************************************
1073 * OLEFontImpl_AddRefHfont (IFont)
1075 static HRESULT WINAPI OLEFontImpl_AddRefHfont(
1076 IFont* iface,
1077 HFONT hfont)
1079 OLEFontImpl *this = impl_from_IFont(iface);
1081 TRACE("(%p)->(%p)\n", this, hfont);
1083 if (!hfont) return E_INVALIDARG;
1085 return inc_ext_ref(hfont);
1088 /************************************************************************
1089 * OLEFontImpl_ReleaseHfont (IFont)
1091 static HRESULT WINAPI OLEFontImpl_ReleaseHfont(
1092 IFont* iface,
1093 HFONT hfont)
1095 OLEFontImpl *this = impl_from_IFont(iface);
1097 TRACE("(%p)->(%p)\n", this, hfont);
1099 if (!hfont) return E_INVALIDARG;
1101 return dec_ext_ref(hfont);
1104 /************************************************************************
1105 * OLEFontImpl_SetHdc (IFont)
1107 static HRESULT WINAPI OLEFontImpl_SetHdc(
1108 IFont* iface,
1109 HDC hdc)
1111 OLEFontImpl *this = impl_from_IFont(iface);
1112 FIXME("(%p)->(%p): Stub\n", this, hdc);
1113 return E_NOTIMPL;
1116 static const IFontVtbl OLEFontImpl_VTable =
1118 OLEFontImpl_QueryInterface,
1119 OLEFontImpl_AddRef,
1120 OLEFontImpl_Release,
1121 OLEFontImpl_get_Name,
1122 OLEFontImpl_put_Name,
1123 OLEFontImpl_get_Size,
1124 OLEFontImpl_put_Size,
1125 OLEFontImpl_get_Bold,
1126 OLEFontImpl_put_Bold,
1127 OLEFontImpl_get_Italic,
1128 OLEFontImpl_put_Italic,
1129 OLEFontImpl_get_Underline,
1130 OLEFontImpl_put_Underline,
1131 OLEFontImpl_get_Strikethrough,
1132 OLEFontImpl_put_Strikethrough,
1133 OLEFontImpl_get_Weight,
1134 OLEFontImpl_put_Weight,
1135 OLEFontImpl_get_Charset,
1136 OLEFontImpl_put_Charset,
1137 OLEFontImpl_get_hFont,
1138 OLEFontImpl_Clone,
1139 OLEFontImpl_IsEqual,
1140 OLEFontImpl_SetRatio,
1141 OLEFontImpl_QueryTextMetrics,
1142 OLEFontImpl_AddRefHfont,
1143 OLEFontImpl_ReleaseHfont,
1144 OLEFontImpl_SetHdc
1147 /************************************************************************
1148 * OLEFontImpl_IDispatch_QueryInterface (IUnknown)
1150 static HRESULT WINAPI OLEFontImpl_IDispatch_QueryInterface(
1151 IDispatch* iface,
1152 REFIID riid,
1153 VOID** ppvoid)
1155 OLEFontImpl *this = impl_from_IDispatch(iface);
1156 return IFont_QueryInterface(&this->IFont_iface, riid, ppvoid);
1159 /************************************************************************
1160 * OLEFontImpl_IDispatch_Release (IUnknown)
1162 static ULONG WINAPI OLEFontImpl_IDispatch_Release(
1163 IDispatch* iface)
1165 OLEFontImpl *this = impl_from_IDispatch(iface);
1166 return IFont_Release(&this->IFont_iface);
1169 /************************************************************************
1170 * OLEFontImpl_IDispatch_AddRef (IUnknown)
1172 static ULONG WINAPI OLEFontImpl_IDispatch_AddRef(
1173 IDispatch* iface)
1175 OLEFontImpl *this = impl_from_IDispatch(iface);
1176 return IFont_AddRef(&this->IFont_iface);
1179 /************************************************************************
1180 * OLEFontImpl_GetTypeInfoCount (IDispatch)
1182 static HRESULT WINAPI OLEFontImpl_GetTypeInfoCount(
1183 IDispatch* iface,
1184 unsigned int* pctinfo)
1186 OLEFontImpl *this = impl_from_IDispatch(iface);
1187 TRACE("(%p)->(%p)\n", this, pctinfo);
1188 *pctinfo = 1;
1190 return S_OK;
1193 /************************************************************************
1194 * OLEFontImpl_GetTypeInfo (IDispatch)
1196 static HRESULT WINAPI OLEFontImpl_GetTypeInfo(
1197 IDispatch* iface,
1198 UINT iTInfo,
1199 LCID lcid,
1200 ITypeInfo** ppTInfo)
1202 ITypeLib *tl;
1203 HRESULT hres;
1205 OLEFontImpl *this = impl_from_IDispatch(iface);
1206 TRACE("(%p, iTInfo=%d, lcid=%04x, %p)\n", this, iTInfo, (int)lcid, ppTInfo);
1207 if (iTInfo != 0)
1208 return E_FAIL;
1209 hres = LoadTypeLib(L"stdole2.tlb", &tl);
1210 if (FAILED(hres)) {
1211 ERR("Could not load the stdole2.tlb?\n");
1212 return hres;
1214 hres = ITypeLib_GetTypeInfoOfGuid(tl, &IID_IFontDisp, ppTInfo);
1215 ITypeLib_Release(tl);
1216 if (FAILED(hres))
1217 FIXME("Did not IDispatch typeinfo from typelib, hres %#lx.\n", hres);
1219 return hres;
1222 /************************************************************************
1223 * OLEFontImpl_GetIDsOfNames (IDispatch)
1225 static HRESULT WINAPI OLEFontImpl_GetIDsOfNames(
1226 IDispatch* iface,
1227 REFIID riid,
1228 LPOLESTR* rgszNames,
1229 UINT cNames,
1230 LCID lcid,
1231 DISPID* rgDispId)
1233 ITypeInfo * pTInfo;
1234 HRESULT hres;
1236 OLEFontImpl *this = impl_from_IDispatch(iface);
1238 TRACE("(%p,%s,%p,cNames=%d,lcid=%04x,%p)\n", this, debugstr_guid(riid),
1239 rgszNames, cNames, (int)lcid, rgDispId);
1241 if (cNames == 0) return E_INVALIDARG;
1243 hres = IDispatch_GetTypeInfo(iface, 0, lcid, &pTInfo);
1244 if (FAILED(hres))
1246 ERR("GetTypeInfo failed.\n");
1247 return hres;
1250 /* convert names to DISPIDs */
1251 hres = DispGetIDsOfNames (pTInfo, rgszNames, cNames, rgDispId);
1252 ITypeInfo_Release(pTInfo);
1254 return hres;
1257 /************************************************************************
1258 * OLEFontImpl_Invoke (IDispatch)
1261 static HRESULT WINAPI OLEFontImpl_Invoke(
1262 IDispatch* iface,
1263 DISPID dispIdMember,
1264 REFIID riid,
1265 LCID lcid,
1266 WORD wFlags,
1267 DISPPARAMS* pDispParams,
1268 VARIANT* pVarResult,
1269 EXCEPINFO* pExepInfo,
1270 UINT* puArgErr)
1272 OLEFontImpl *this = impl_from_IDispatch(iface);
1273 HRESULT hr;
1275 TRACE("%p, %ld, %s, %#lx, %#x, %p, %p, %p, %p.\n", iface, dispIdMember,
1276 debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult, pExepInfo,
1277 puArgErr);
1279 /* validate parameters */
1281 if (!IsEqualIID(riid, &IID_NULL))
1283 ERR("riid was %s instead of IID_NULL\n", debugstr_guid(riid));
1284 return DISP_E_UNKNOWNINTERFACE;
1287 if (wFlags & DISPATCH_PROPERTYGET)
1289 if (!pVarResult)
1291 ERR("null pVarResult not allowed when DISPATCH_PROPERTYGET specified\n");
1292 return DISP_E_PARAMNOTOPTIONAL;
1295 else if (wFlags & DISPATCH_PROPERTYPUT)
1297 if (!pDispParams)
1299 ERR("null pDispParams not allowed when DISPATCH_PROPERTYPUT specified\n");
1300 return DISP_E_PARAMNOTOPTIONAL;
1302 if (pDispParams->cArgs != 1)
1304 ERR("param count for DISPATCH_PROPERTYPUT was %d instead of 1\n", pDispParams->cArgs);
1305 return DISP_E_BADPARAMCOUNT;
1308 else
1310 ERR("one of DISPATCH_PROPERTYGET or DISPATCH_PROPERTYPUT must be specified\n");
1311 return DISP_E_MEMBERNOTFOUND;
1314 switch (dispIdMember) {
1315 case DISPID_FONT_NAME:
1316 if (wFlags & DISPATCH_PROPERTYGET) {
1317 V_VT(pVarResult) = VT_BSTR;
1318 return IFont_get_Name(&this->IFont_iface, &V_BSTR(pVarResult));
1319 } else {
1320 VARIANTARG vararg;
1322 VariantInit(&vararg);
1323 hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_BSTR);
1324 if (FAILED(hr))
1325 return hr;
1327 hr = IFont_put_Name(&this->IFont_iface, V_BSTR(&vararg));
1329 VariantClear(&vararg);
1330 return hr;
1332 break;
1333 case DISPID_FONT_BOLD:
1334 if (wFlags & DISPATCH_PROPERTYGET) {
1335 BOOL value;
1336 hr = IFont_get_Bold(&this->IFont_iface, &value);
1337 V_VT(pVarResult) = VT_BOOL;
1338 V_BOOL(pVarResult) = value ? VARIANT_TRUE : VARIANT_FALSE;
1339 return hr;
1340 } else {
1341 VARIANTARG vararg;
1343 VariantInit(&vararg);
1344 hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_BOOL);
1345 if (FAILED(hr))
1346 return hr;
1348 hr = IFont_put_Bold(&this->IFont_iface, V_BOOL(&vararg));
1350 VariantClear(&vararg);
1351 return hr;
1353 break;
1354 case DISPID_FONT_ITALIC:
1355 if (wFlags & DISPATCH_PROPERTYGET) {
1356 BOOL value;
1357 hr = IFont_get_Italic(&this->IFont_iface, &value);
1358 V_VT(pVarResult) = VT_BOOL;
1359 V_BOOL(pVarResult) = value ? VARIANT_TRUE : VARIANT_FALSE;
1360 return hr;
1361 } else {
1362 VARIANTARG vararg;
1364 VariantInit(&vararg);
1365 hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_BOOL);
1366 if (FAILED(hr))
1367 return hr;
1369 hr = IFont_put_Italic(&this->IFont_iface, V_BOOL(&vararg));
1371 VariantClear(&vararg);
1372 return hr;
1374 break;
1375 case DISPID_FONT_UNDER:
1376 if (wFlags & DISPATCH_PROPERTYGET) {
1377 BOOL value;
1378 hr = IFont_get_Underline(&this->IFont_iface, &value);
1379 V_VT(pVarResult) = VT_BOOL;
1380 V_BOOL(pVarResult) = value ? VARIANT_TRUE : VARIANT_FALSE;
1381 return hr;
1382 } else {
1383 VARIANTARG vararg;
1385 VariantInit(&vararg);
1386 hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_BOOL);
1387 if (FAILED(hr))
1388 return hr;
1390 hr = IFont_put_Underline(&this->IFont_iface, V_BOOL(&vararg));
1392 VariantClear(&vararg);
1393 return hr;
1395 break;
1396 case DISPID_FONT_STRIKE:
1397 if (wFlags & DISPATCH_PROPERTYGET) {
1398 BOOL value;
1399 hr = IFont_get_Strikethrough(&this->IFont_iface, &value);
1400 V_VT(pVarResult) = VT_BOOL;
1401 V_BOOL(pVarResult) = value ? VARIANT_TRUE : VARIANT_FALSE;
1402 return hr;
1403 } else {
1404 VARIANTARG vararg;
1406 VariantInit(&vararg);
1407 hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_BOOL);
1408 if (FAILED(hr))
1409 return hr;
1411 hr = IFont_put_Strikethrough(&this->IFont_iface, V_BOOL(&vararg));
1413 VariantClear(&vararg);
1414 return hr;
1416 break;
1417 case DISPID_FONT_SIZE:
1418 if (wFlags & DISPATCH_PROPERTYGET) {
1419 V_VT(pVarResult) = VT_CY;
1420 return IFont_get_Size(&this->IFont_iface, &V_CY(pVarResult));
1421 } else {
1422 VARIANTARG vararg;
1424 VariantInit(&vararg);
1425 hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_CY);
1426 if (FAILED(hr))
1427 return hr;
1429 hr = IFont_put_Size(&this->IFont_iface, V_CY(&vararg));
1431 VariantClear(&vararg);
1432 return hr;
1434 break;
1435 case DISPID_FONT_WEIGHT:
1436 if (wFlags & DISPATCH_PROPERTYGET) {
1437 V_VT(pVarResult) = VT_I2;
1438 return IFont_get_Weight(&this->IFont_iface, &V_I2(pVarResult));
1439 } else {
1440 VARIANTARG vararg;
1442 VariantInit(&vararg);
1443 hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_I2);
1444 if (FAILED(hr))
1445 return hr;
1447 hr = IFont_put_Weight(&this->IFont_iface, V_I2(&vararg));
1449 VariantClear(&vararg);
1450 return hr;
1452 break;
1453 case DISPID_FONT_CHARSET:
1454 if (wFlags & DISPATCH_PROPERTYGET) {
1455 V_VT(pVarResult) = VT_I2;
1456 return OLEFontImpl_get_Charset(&this->IFont_iface, &V_I2(pVarResult));
1457 } else {
1458 VARIANTARG vararg;
1460 VariantInit(&vararg);
1461 hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_I2);
1462 if (FAILED(hr))
1463 return hr;
1465 hr = IFont_put_Charset(&this->IFont_iface, V_I2(&vararg));
1467 VariantClear(&vararg);
1468 return hr;
1470 break;
1471 default:
1472 ERR("member not found for dispid %#lx.\n", dispIdMember);
1473 return DISP_E_MEMBERNOTFOUND;
1477 static const IDispatchVtbl OLEFontImpl_IDispatch_VTable =
1479 OLEFontImpl_IDispatch_QueryInterface,
1480 OLEFontImpl_IDispatch_AddRef,
1481 OLEFontImpl_IDispatch_Release,
1482 OLEFontImpl_GetTypeInfoCount,
1483 OLEFontImpl_GetTypeInfo,
1484 OLEFontImpl_GetIDsOfNames,
1485 OLEFontImpl_Invoke
1488 /************************************************************************
1489 * OLEFontImpl_IPersistStream_QueryInterface (IUnknown)
1491 static HRESULT WINAPI OLEFontImpl_IPersistStream_QueryInterface(
1492 IPersistStream* iface,
1493 REFIID riid,
1494 VOID** ppvoid)
1496 OLEFontImpl *this = impl_from_IPersistStream(iface);
1498 return IFont_QueryInterface(&this->IFont_iface, riid, ppvoid);
1501 /************************************************************************
1502 * OLEFontImpl_IPersistStream_Release (IUnknown)
1504 static ULONG WINAPI OLEFontImpl_IPersistStream_Release(
1505 IPersistStream* iface)
1507 OLEFontImpl *this = impl_from_IPersistStream(iface);
1509 return IFont_Release(&this->IFont_iface);
1512 /************************************************************************
1513 * OLEFontImpl_IPersistStream_AddRef (IUnknown)
1515 static ULONG WINAPI OLEFontImpl_IPersistStream_AddRef(
1516 IPersistStream* iface)
1518 OLEFontImpl *this = impl_from_IPersistStream(iface);
1520 return IFont_AddRef(&this->IFont_iface);
1523 /************************************************************************
1524 * OLEFontImpl_GetClassID (IPersistStream)
1526 static HRESULT WINAPI OLEFontImpl_GetClassID(
1527 IPersistStream* iface,
1528 CLSID* pClassID)
1530 TRACE("(%p,%p)\n",iface,pClassID);
1531 if (pClassID==0)
1532 return E_POINTER;
1534 *pClassID = CLSID_StdFont;
1536 return S_OK;
1539 /************************************************************************
1540 * OLEFontImpl_IsDirty (IPersistStream)
1542 * See Windows documentation for more details on IPersistStream methods.
1544 static HRESULT WINAPI OLEFontImpl_IsDirty(
1545 IPersistStream* iface)
1547 TRACE("(%p)\n",iface);
1548 return S_OK;
1551 /************************************************************************
1552 * OLEFontImpl_Load (IPersistStream)
1554 * See Windows documentation for more details on IPersistStream methods.
1556 * This is the format of the standard font serialization as far as I
1557 * know
1559 * Offset Type Value Comment
1560 * 0x0000 Byte Unknown Probably a version number, contains 0x01
1561 * 0x0001 Short Charset Charset value from the FONTDESC structure
1562 * 0x0003 Byte Attributes Flags defined as follows:
1563 * 00000010 - Italic
1564 * 00000100 - Underline
1565 * 00001000 - Strikethrough
1566 * 0x0004 Short Weight Weight value from FONTDESC structure
1567 * 0x0006 DWORD size "Low" portion of the cySize member of the FONTDESC
1568 * structure/
1569 * 0x000A Byte name length Length of the font name string (no null character)
1570 * 0x000B String name Name of the font (ASCII, no nul character)
1572 static HRESULT WINAPI OLEFontImpl_Load(
1573 IPersistStream* iface,
1574 IStream* pLoadStream)
1576 OLEFontImpl *this = impl_from_IPersistStream(iface);
1577 BYTE version, attributes, string_size;
1578 char readBuffer[0x100];
1579 ULONG cbRead;
1580 INT len;
1582 /* Version */
1583 IStream_Read(pLoadStream, &version, sizeof(BYTE), &cbRead);
1584 if ((cbRead != sizeof(BYTE)) || (version != 0x01)) return E_FAIL;
1586 /* Charset */
1587 IStream_Read(pLoadStream, &this->description.sCharset, sizeof(WORD), &cbRead);
1588 if (cbRead != sizeof(WORD)) return E_FAIL;
1590 /* Attributes */
1591 IStream_Read(pLoadStream, &attributes, sizeof(BYTE), &cbRead);
1592 if (cbRead != sizeof(BYTE)) return E_FAIL;
1594 this->description.fItalic = (attributes & FONTPERSIST_ITALIC) != 0;
1595 this->description.fStrikethrough = (attributes & FONTPERSIST_STRIKETHROUGH) != 0;
1596 this->description.fUnderline = (attributes & FONTPERSIST_UNDERLINE) != 0;
1598 /* Weight */
1599 IStream_Read(pLoadStream, &this->description.sWeight, sizeof(WORD), &cbRead);
1600 if (cbRead != sizeof(WORD)) return E_FAIL;
1602 /* Size */
1603 IStream_Read(pLoadStream, &this->description.cySize.s.Lo, sizeof(DWORD), &cbRead);
1604 if (cbRead != sizeof(DWORD)) return E_FAIL;
1606 this->description.cySize.s.Hi = 0;
1608 /* Name */
1609 IStream_Read(pLoadStream, &string_size, sizeof(BYTE), &cbRead);
1610 if (cbRead != sizeof(BYTE)) return E_FAIL;
1612 IStream_Read(pLoadStream, readBuffer, string_size, &cbRead);
1613 if (cbRead != string_size) return E_FAIL;
1615 free(this->description.lpstrName);
1617 len = MultiByteToWideChar( CP_ACP, 0, readBuffer, string_size, NULL, 0 );
1618 this->description.lpstrName = malloc((len + 1) * sizeof(WCHAR));
1619 MultiByteToWideChar( CP_ACP, 0, readBuffer, string_size, this->description.lpstrName, len );
1620 this->description.lpstrName[len] = 0;
1622 /* Ensure use of this font causes a new one to be created */
1623 dec_int_ref(this->gdiFont);
1624 this->gdiFont = 0;
1625 this->dirty = TRUE;
1627 return S_OK;
1630 /************************************************************************
1631 * OLEFontImpl_Save (IPersistStream)
1633 static HRESULT WINAPI OLEFontImpl_Save(
1634 IPersistStream* iface,
1635 IStream* pOutStream,
1636 BOOL fClearDirty)
1638 OLEFontImpl *this = impl_from_IPersistStream(iface);
1639 BYTE attributes, string_size;
1640 const BYTE version = 0x01;
1641 char* writeBuffer = NULL;
1642 ULONG written;
1644 TRACE("(%p)->(%p %d)\n", this, pOutStream, fClearDirty);
1646 /* Version */
1647 IStream_Write(pOutStream, &version, sizeof(BYTE), &written);
1648 if (written != sizeof(BYTE)) return E_FAIL;
1650 /* Charset */
1651 IStream_Write(pOutStream, &this->description.sCharset, sizeof(WORD), &written);
1652 if (written != sizeof(WORD)) return E_FAIL;
1654 /* Attributes */
1655 attributes = 0;
1657 if (this->description.fItalic)
1658 attributes |= FONTPERSIST_ITALIC;
1660 if (this->description.fStrikethrough)
1661 attributes |= FONTPERSIST_STRIKETHROUGH;
1663 if (this->description.fUnderline)
1664 attributes |= FONTPERSIST_UNDERLINE;
1666 IStream_Write(pOutStream, &attributes, sizeof(BYTE), &written);
1667 if (written != sizeof(BYTE)) return E_FAIL;
1669 /* Weight */
1670 IStream_Write(pOutStream, &this->description.sWeight, sizeof(WORD), &written);
1671 if (written != sizeof(WORD)) return E_FAIL;
1673 /* Size */
1674 IStream_Write(pOutStream, &this->description.cySize.s.Lo, sizeof(DWORD), &written);
1675 if (written != sizeof(DWORD)) return E_FAIL;
1677 /* FontName */
1678 if (this->description.lpstrName)
1679 string_size = WideCharToMultiByte( CP_ACP, 0, this->description.lpstrName,
1680 lstrlenW(this->description.lpstrName), NULL, 0, NULL, NULL );
1681 else
1682 string_size = 0;
1684 IStream_Write(pOutStream, &string_size, sizeof(BYTE), &written);
1685 if (written != sizeof(BYTE)) return E_FAIL;
1687 if (string_size)
1689 if (!(writeBuffer = malloc(string_size))) return E_OUTOFMEMORY;
1690 WideCharToMultiByte( CP_ACP, 0, this->description.lpstrName,
1691 lstrlenW(this->description.lpstrName),
1692 writeBuffer, string_size, NULL, NULL );
1694 IStream_Write(pOutStream, writeBuffer, string_size, &written);
1695 free(writeBuffer);
1697 if (written != string_size) return E_FAIL;
1700 return S_OK;
1703 /************************************************************************
1704 * OLEFontImpl_GetSizeMax (IPersistStream)
1706 static HRESULT WINAPI OLEFontImpl_GetSizeMax(
1707 IPersistStream* iface,
1708 ULARGE_INTEGER* pcbSize)
1710 OLEFontImpl *this = impl_from_IPersistStream(iface);
1712 if (pcbSize==NULL)
1713 return E_POINTER;
1715 pcbSize->u.HighPart = 0;
1716 pcbSize->u.LowPart = 0;
1718 pcbSize->u.LowPart += sizeof(BYTE); /* Version */
1719 pcbSize->u.LowPart += sizeof(WORD); /* Lang code */
1720 pcbSize->u.LowPart += sizeof(BYTE); /* Flags */
1721 pcbSize->u.LowPart += sizeof(WORD); /* Weight */
1722 pcbSize->u.LowPart += sizeof(DWORD); /* Size */
1723 pcbSize->u.LowPart += sizeof(BYTE); /* StrLength */
1725 if (this->description.lpstrName!=0)
1726 pcbSize->u.LowPart += WideCharToMultiByte( CP_ACP, 0, this->description.lpstrName,
1727 lstrlenW(this->description.lpstrName),
1728 NULL, 0, NULL, NULL );
1730 return S_OK;
1733 static const IPersistStreamVtbl OLEFontImpl_IPersistStream_VTable =
1735 OLEFontImpl_IPersistStream_QueryInterface,
1736 OLEFontImpl_IPersistStream_AddRef,
1737 OLEFontImpl_IPersistStream_Release,
1738 OLEFontImpl_GetClassID,
1739 OLEFontImpl_IsDirty,
1740 OLEFontImpl_Load,
1741 OLEFontImpl_Save,
1742 OLEFontImpl_GetSizeMax
1745 /************************************************************************
1746 * OLEFontImpl_IConnectionPointContainer_QueryInterface (IUnknown)
1748 static HRESULT WINAPI OLEFontImpl_IConnectionPointContainer_QueryInterface(
1749 IConnectionPointContainer* iface,
1750 REFIID riid,
1751 VOID** ppvoid)
1753 OLEFontImpl *this = impl_from_IConnectionPointContainer(iface);
1755 return IFont_QueryInterface(&this->IFont_iface, riid, ppvoid);
1758 /************************************************************************
1759 * OLEFontImpl_IConnectionPointContainer_Release (IUnknown)
1761 static ULONG WINAPI OLEFontImpl_IConnectionPointContainer_Release(
1762 IConnectionPointContainer* iface)
1764 OLEFontImpl *this = impl_from_IConnectionPointContainer(iface);
1766 return IFont_Release(&this->IFont_iface);
1769 /************************************************************************
1770 * OLEFontImpl_IConnectionPointContainer_AddRef (IUnknown)
1772 static ULONG WINAPI OLEFontImpl_IConnectionPointContainer_AddRef(
1773 IConnectionPointContainer* iface)
1775 OLEFontImpl *this = impl_from_IConnectionPointContainer(iface);
1777 return IFont_AddRef(&this->IFont_iface);
1780 /************************************************************************
1781 * OLEFontImpl_EnumConnectionPoints (IConnectionPointContainer)
1783 static HRESULT WINAPI OLEFontImpl_EnumConnectionPoints(
1784 IConnectionPointContainer* iface,
1785 IEnumConnectionPoints **ppEnum)
1787 OLEFontImpl *this = impl_from_IConnectionPointContainer(iface);
1789 FIXME("(%p)->(%p): stub\n", this, ppEnum);
1790 return E_NOTIMPL;
1793 /************************************************************************
1794 * OLEFontImpl_FindConnectionPoint (IConnectionPointContainer)
1796 static HRESULT WINAPI OLEFontImpl_FindConnectionPoint(
1797 IConnectionPointContainer* iface,
1798 REFIID riid,
1799 IConnectionPoint **ppCp)
1801 OLEFontImpl *this = impl_from_IConnectionPointContainer(iface);
1802 TRACE("(%p)->(%s, %p)\n", this, debugstr_guid(riid), ppCp);
1804 if(IsEqualIID(riid, &IID_IPropertyNotifySink)) {
1805 return IConnectionPoint_QueryInterface(this->pPropertyNotifyCP, &IID_IConnectionPoint,
1806 (void**)ppCp);
1807 } else if(IsEqualIID(riid, &IID_IFontEventsDisp)) {
1808 return IConnectionPoint_QueryInterface(this->pFontEventsCP, &IID_IConnectionPoint,
1809 (void**)ppCp);
1810 } else {
1811 FIXME("no connection point for %s\n", debugstr_guid(riid));
1812 return CONNECT_E_NOCONNECTION;
1816 static const IConnectionPointContainerVtbl
1817 OLEFontImpl_IConnectionPointContainer_VTable =
1819 OLEFontImpl_IConnectionPointContainer_QueryInterface,
1820 OLEFontImpl_IConnectionPointContainer_AddRef,
1821 OLEFontImpl_IConnectionPointContainer_Release,
1822 OLEFontImpl_EnumConnectionPoints,
1823 OLEFontImpl_FindConnectionPoint
1826 /************************************************************************
1827 * OLEFontImpl implementation of IPersistPropertyBag.
1829 static HRESULT WINAPI OLEFontImpl_IPersistPropertyBag_QueryInterface(
1830 IPersistPropertyBag *iface, REFIID riid, LPVOID *ppvObj
1832 OLEFontImpl *this = impl_from_IPersistPropertyBag(iface);
1833 return IFont_QueryInterface(&this->IFont_iface,riid,ppvObj);
1836 static ULONG WINAPI OLEFontImpl_IPersistPropertyBag_AddRef(
1837 IPersistPropertyBag *iface
1839 OLEFontImpl *this = impl_from_IPersistPropertyBag(iface);
1840 return IFont_AddRef(&this->IFont_iface);
1843 static ULONG WINAPI OLEFontImpl_IPersistPropertyBag_Release(
1844 IPersistPropertyBag *iface
1846 OLEFontImpl *this = impl_from_IPersistPropertyBag(iface);
1847 return IFont_Release(&this->IFont_iface);
1850 static HRESULT WINAPI OLEFontImpl_IPersistPropertyBag_GetClassID(
1851 IPersistPropertyBag *iface, CLSID *classid
1853 FIXME("(%p,%p), stub!\n", iface, classid);
1854 return E_FAIL;
1857 static HRESULT WINAPI OLEFontImpl_IPersistPropertyBag_InitNew(
1858 IPersistPropertyBag *iface
1860 FIXME("(%p), stub!\n", iface);
1861 return S_OK;
1864 static HRESULT WINAPI OLEFontImpl_IPersistPropertyBag_Load(
1865 IPersistPropertyBag *iface, IPropertyBag* pPropBag, IErrorLog* pErrorLog
1867 /* (from Visual Basic 6 property bag)
1868 Name = "MS Sans Serif"
1869 Size = 13.8
1870 Charset = 0
1871 Weight = 400
1872 Underline = 0 'False
1873 Italic = 0 'False
1874 Strikethrough = 0 'False
1876 OLEFontImpl *this = impl_from_IPersistPropertyBag(iface);
1877 VARIANT value;
1878 HRESULT iRes;
1880 VariantInit(&value);
1882 iRes = IPropertyBag_Read(pPropBag, L"Name", &value, pErrorLog);
1883 if (iRes == S_OK)
1885 iRes = VariantChangeType(&value, &value, 0, VT_BSTR);
1886 if (iRes == S_OK)
1887 iRes = IFont_put_Name(&this->IFont_iface, V_BSTR(&value));
1889 else if (iRes == E_INVALIDARG)
1890 iRes = S_OK;
1892 VariantClear(&value);
1894 if (iRes == S_OK) {
1895 iRes = IPropertyBag_Read(pPropBag, L"Size", &value, pErrorLog);
1896 if (iRes == S_OK)
1898 iRes = VariantChangeType(&value, &value, 0, VT_CY);
1899 if (iRes == S_OK)
1900 iRes = IFont_put_Size(&this->IFont_iface, V_CY(&value));
1902 else if (iRes == E_INVALIDARG)
1903 iRes = S_OK;
1905 VariantClear(&value);
1908 if (iRes == S_OK) {
1909 iRes = IPropertyBag_Read(pPropBag, L"Charset", &value, pErrorLog);
1910 if (iRes == S_OK)
1912 iRes = VariantChangeType(&value, &value, 0, VT_I2);
1913 if (iRes == S_OK)
1914 iRes = IFont_put_Charset(&this->IFont_iface, V_I2(&value));
1916 else if (iRes == E_INVALIDARG)
1917 iRes = S_OK;
1919 VariantClear(&value);
1922 if (iRes == S_OK) {
1923 iRes = IPropertyBag_Read(pPropBag, L"Weight", &value, pErrorLog);
1924 if (iRes == S_OK)
1926 iRes = VariantChangeType(&value, &value, 0, VT_I2);
1927 if (iRes == S_OK)
1928 iRes = IFont_put_Weight(&this->IFont_iface, V_I2(&value));
1930 else if (iRes == E_INVALIDARG)
1931 iRes = S_OK;
1933 VariantClear(&value);
1936 if (iRes == S_OK) {
1937 iRes = IPropertyBag_Read(pPropBag, L"Underline", &value, pErrorLog);
1938 if (iRes == S_OK)
1940 iRes = VariantChangeType(&value, &value, 0, VT_BOOL);
1941 if (iRes == S_OK)
1942 iRes = IFont_put_Underline(&this->IFont_iface, V_BOOL(&value));
1944 else if (iRes == E_INVALIDARG)
1945 iRes = S_OK;
1947 VariantClear(&value);
1950 if (iRes == S_OK) {
1951 iRes = IPropertyBag_Read(pPropBag, L"Italic", &value, pErrorLog);
1952 if (iRes == S_OK)
1954 iRes = VariantChangeType(&value, &value, 0, VT_BOOL);
1955 if (iRes == S_OK)
1956 iRes = IFont_put_Italic(&this->IFont_iface, V_BOOL(&value));
1958 else if (iRes == E_INVALIDARG)
1959 iRes = S_OK;
1961 VariantClear(&value);
1964 if (iRes == S_OK) {
1965 iRes = IPropertyBag_Read(pPropBag, L"Strikethrough", &value, pErrorLog);
1966 if (iRes == S_OK)
1968 iRes = VariantChangeType(&value, &value, 0, VT_BOOL);
1969 if (iRes == S_OK)
1970 IFont_put_Strikethrough(&this->IFont_iface, V_BOOL(&value));
1972 else if (iRes == E_INVALIDARG)
1973 iRes = S_OK;
1975 VariantClear(&value);
1978 if (FAILED(iRes))
1979 WARN("-- %#lx.\n", iRes);
1980 return iRes;
1983 static HRESULT WINAPI OLEFontImpl_IPersistPropertyBag_Save(
1984 IPersistPropertyBag *iface, IPropertyBag* pPropBag, BOOL fClearDirty,
1985 BOOL fSaveAllProperties
1987 FIXME("(%p,%p,%d,%d), stub!\n", iface, pPropBag, fClearDirty, fSaveAllProperties);
1988 return E_FAIL;
1991 static const IPersistPropertyBagVtbl OLEFontImpl_IPersistPropertyBag_VTable =
1993 OLEFontImpl_IPersistPropertyBag_QueryInterface,
1994 OLEFontImpl_IPersistPropertyBag_AddRef,
1995 OLEFontImpl_IPersistPropertyBag_Release,
1997 OLEFontImpl_IPersistPropertyBag_GetClassID,
1998 OLEFontImpl_IPersistPropertyBag_InitNew,
1999 OLEFontImpl_IPersistPropertyBag_Load,
2000 OLEFontImpl_IPersistPropertyBag_Save
2003 /************************************************************************
2004 * OLEFontImpl_Construct
2006 * This method will construct a new instance of the OLEFontImpl
2007 * class.
2009 * The caller of this method must release the object when it's
2010 * done with it.
2012 static OLEFontImpl* OLEFontImpl_Construct(const FONTDESC *fontDesc)
2014 OLEFontImpl* newObject;
2016 newObject = malloc(sizeof(OLEFontImpl));
2018 if (newObject==0)
2019 return newObject;
2021 newObject->IFont_iface.lpVtbl = &OLEFontImpl_VTable;
2022 newObject->IDispatch_iface.lpVtbl = &OLEFontImpl_IDispatch_VTable;
2023 newObject->IPersistStream_iface.lpVtbl = &OLEFontImpl_IPersistStream_VTable;
2024 newObject->IConnectionPointContainer_iface.lpVtbl = &OLEFontImpl_IConnectionPointContainer_VTable;
2025 newObject->IPersistPropertyBag_iface.lpVtbl = &OLEFontImpl_IPersistPropertyBag_VTable;
2027 newObject->ref = 1;
2029 newObject->description.cbSizeofstruct = sizeof(FONTDESC);
2030 newObject->description.lpstrName = wcsdup(fontDesc->lpstrName);
2031 newObject->description.cySize = fontDesc->cySize;
2032 newObject->description.sWeight = fontDesc->sWeight;
2033 newObject->description.sCharset = fontDesc->sCharset;
2034 newObject->description.fItalic = fontDesc->fItalic;
2035 newObject->description.fUnderline = fontDesc->fUnderline;
2036 newObject->description.fStrikethrough = fontDesc->fStrikethrough;
2038 newObject->gdiFont = 0;
2039 newObject->dirty = TRUE;
2040 newObject->cyLogical = GetDeviceCaps(get_dc(), LOGPIXELSY);
2041 newObject->cyHimetric = 2540L;
2042 newObject->pPropertyNotifyCP = NULL;
2043 newObject->pFontEventsCP = NULL;
2045 CreateConnectionPoint((IUnknown*)&newObject->IFont_iface, &IID_IPropertyNotifySink, &newObject->pPropertyNotifyCP);
2046 CreateConnectionPoint((IUnknown*)&newObject->IFont_iface, &IID_IFontEventsDisp, &newObject->pFontEventsCP);
2048 if (!newObject->pPropertyNotifyCP || !newObject->pFontEventsCP)
2050 OLEFontImpl_Destroy(newObject);
2051 return NULL;
2054 InterlockedIncrement(&ifont_cnt);
2056 TRACE("returning %p\n", newObject);
2057 return newObject;
2060 /************************************************************************
2061 * OLEFontImpl_Destroy
2063 * This method is called by the Release method when the reference
2064 * count goes down to 0. It will free all resources used by
2065 * this object.
2067 static void OLEFontImpl_Destroy(OLEFontImpl* fontDesc)
2069 TRACE("(%p)\n", fontDesc);
2071 free(fontDesc->description.lpstrName);
2073 if (fontDesc->pPropertyNotifyCP)
2074 IConnectionPoint_Release(fontDesc->pPropertyNotifyCP);
2075 if (fontDesc->pFontEventsCP)
2076 IConnectionPoint_Release(fontDesc->pFontEventsCP);
2078 free(fontDesc);
2081 /*******************************************************************************
2082 * StdFont ClassFactory
2084 typedef struct
2086 /* IUnknown fields */
2087 IClassFactory IClassFactory_iface;
2088 LONG ref;
2089 } IClassFactoryImpl;
2091 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
2093 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
2096 static HRESULT WINAPI SFCF_QueryInterface(IClassFactory *iface, REFIID riid, void **obj)
2098 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
2100 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), obj);
2102 *obj = NULL;
2104 if (IsEqualIID(&IID_IClassFactory, riid) || IsEqualIID(&IID_IUnknown, riid))
2106 *obj = iface;
2107 IClassFactory_AddRef(iface);
2108 return S_OK;
2111 return E_NOINTERFACE;
2114 static ULONG WINAPI
2115 SFCF_AddRef(LPCLASSFACTORY iface) {
2116 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
2117 return InterlockedIncrement(&This->ref);
2120 static ULONG WINAPI SFCF_Release(LPCLASSFACTORY iface) {
2121 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
2122 /* static class, won't be freed */
2123 return InterlockedDecrement(&This->ref);
2126 static HRESULT WINAPI SFCF_CreateInstance(
2127 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
2129 return OleCreateFontIndirect(NULL,riid,ppobj);
2133 static HRESULT WINAPI SFCF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
2134 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
2135 FIXME("(%p)->(%d),stub!\n",This,dolock);
2136 return S_OK;
2139 static const IClassFactoryVtbl SFCF_Vtbl = {
2140 SFCF_QueryInterface,
2141 SFCF_AddRef,
2142 SFCF_Release,
2143 SFCF_CreateInstance,
2144 SFCF_LockServer
2146 static IClassFactoryImpl STDFONT_CF = {{&SFCF_Vtbl}, 1 };
2148 void _get_STDFONT_CF(LPVOID *ppv) { *ppv = &STDFONT_CF; }