olefont.c 63.2 KB
Newer Older
1 2 3 4 5 6 7
/*
 * OLE Font encapsulation implementation
 *
 * This file contains an implementation of the IFont
 * interface and the OleCreateFontIndirect API call.
 *
 * Copyright 1999 Francis Beaudet
8
 * Copyright 2006 (Google) Benjamin Arai
9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
22
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 24
 */
#include <assert.h>
25
#include <stdarg.h>
26
#include <string.h>
27

28
#define COBJMACROS
29 30
#define NONAMELESSUNION
#define NONAMELESSSTRUCT
31

32
#include "winerror.h"
33
#include "windef.h"
34 35
#include "winbase.h"
#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"
45

46
WINE_DEFAULT_DEBUG_CHANNEL(ole);
47

48
/***********************************************************************
49 50 51 52 53 54
 * Declaration of constants used when serializing the font object.
 */
#define FONTPERSIST_ITALIC        0x02
#define FONTPERSIST_UNDERLINE     0x04
#define FONTPERSIST_STRIKETHROUGH 0x08

55
static HDC olefont_hdc;
56 57 58 59 60 61 62 63 64

/***********************************************************************
 * List of the HFONTs it has given out, with each one having a separate
 * ref count.
 */
typedef struct _HFONTItem
{
  struct list entry;

65 66 67 68 69
  /* Reference count of any IFont objects that own this hfont */
  LONG int_refs;

  /* Total reference count of any refs held by the application obtained by AddRefHfont plus any internal refs */
  LONG total_refs;
70

71
  /* The font associated with this object. */
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
  HFONT gdiFont;

} HFONTItem, *PHFONTItem;

static struct list OLEFontImpl_hFontList = LIST_INIT(OLEFontImpl_hFontList);

/* Counts how many fonts contain at least one lock */
static LONG ifont_cnt = 0;

/***********************************************************************
 * Critical section for OLEFontImpl_hFontList
 */
static CRITICAL_SECTION OLEFontImpl_csHFONTLIST;
static CRITICAL_SECTION_DEBUG OLEFontImpl_csHFONTLIST_debug =
{
  0, 0, &OLEFontImpl_csHFONTLIST,
  { &OLEFontImpl_csHFONTLIST_debug.ProcessLocksList,
    &OLEFontImpl_csHFONTLIST_debug.ProcessLocksList },
    0, 0, { (DWORD_PTR)(__FILE__ ": OLEFontImpl_csHFONTLIST") }
};
static CRITICAL_SECTION OLEFontImpl_csHFONTLIST = { &OLEFontImpl_csHFONTLIST_debug, -1, 0, 0, 0, 0 };

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
static HDC get_dc(void)
{
    HDC hdc;
    EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
    if(!olefont_hdc)
        olefont_hdc = CreateCompatibleDC(NULL);
    hdc = olefont_hdc;
    LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
    return hdc;
}

static void delete_dc(void)
{
    EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
    if(olefont_hdc)
    {
        DeleteDC(olefont_hdc);
        olefont_hdc = NULL;
    }
    LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
}

116 117 118 119 120 121 122
static void HFONTItem_Delete(PHFONTItem item)
{
  DeleteObject(item->gdiFont);
  list_remove(&item->entry);
  HeapFree(GetProcessHeap(), 0, item);
}

123 124 125 126 127 128 129 130 131 132 133 134 135
/* Find hfont item entry in the list.  Should be called while holding the crit sect */
static HFONTItem *find_hfontitem(HFONT hfont)
{
    HFONTItem *item;

    LIST_FOR_EACH_ENTRY(item, &OLEFontImpl_hFontList, HFONTItem, entry)
    {
        if (item->gdiFont == hfont)
            return item;
    }
    return NULL;
}

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
/* Add an item to the list with one internal reference */
static HRESULT add_hfontitem(HFONT hfont)
{
    HFONTItem *new_item = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_item));

    if(!new_item) return E_OUTOFMEMORY;

    new_item->int_refs = 1;
    new_item->total_refs = 1;
    new_item->gdiFont = hfont;
    EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
    list_add_tail(&OLEFontImpl_hFontList,&new_item->entry);
    LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
    return S_OK;
}

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
static HRESULT inc_int_ref(HFONT hfont)
{
    HFONTItem *item;
    HRESULT hr = S_FALSE;

    EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
    item = find_hfontitem(hfont);

    if(item)
    {
        item->int_refs++;
        item->total_refs++;
        hr = S_OK;
    }
    LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);

    return hr;
}

/* decrements the internal ref of a hfont item.  If both refs are zero it'll
   remove the item from the list and delete the hfont */
static HRESULT dec_int_ref(HFONT hfont)
{
    HFONTItem *item;
    HRESULT hr = S_FALSE;

    EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
    item = find_hfontitem(hfont);

    if(item)
    {
        item->int_refs--;
        item->total_refs--;
        if(item->int_refs == 0 && item->total_refs == 0)
            HFONTItem_Delete(item);
        hr = S_OK;
    }
    LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);

    return hr;
}

static HRESULT inc_ext_ref(HFONT hfont)
{
    HFONTItem *item;
    HRESULT hr = S_FALSE;

    EnterCriticalSection(&OLEFontImpl_csHFONTLIST);

    item = find_hfontitem(hfont);
    if(item)
    {
        item->total_refs++;
        hr = S_OK;
    }
    LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);

    return hr;
}

static HRESULT dec_ext_ref(HFONT hfont)
{
    HFONTItem *item;
    HRESULT hr = S_FALSE;

    EnterCriticalSection(&OLEFontImpl_csHFONTLIST);

    item = find_hfontitem(hfont);
    if(item)
    {
        if(--item->total_refs >= 0) hr = S_OK;
    }
    LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);

    return hr;
}

229 230 231
static WCHAR *strdupW(const WCHAR* str)
{
    WCHAR *ret;
232
    DWORD size = (lstrlenW(str) + 1) * sizeof(WCHAR);
233 234 235 236 237 238 239

    ret = HeapAlloc(GetProcessHeap(), 0, size);
    if(ret)
        memcpy(ret, str, size);
    return ret;
}

240 241
/***********************************************************************
 * Declaration of the implementation class for the IFont interface
242 243 244 245 246 247
 */
typedef struct OLEFontImpl OLEFontImpl;

struct OLEFontImpl
{
  /*
248
   * This class supports many interfaces. IUnknown, IFont,
249 250 251
   * IDispatch, IDispFont IPersistStream and IConnectionPointContainer.
   * The first two are supported by the first vtable, the next two are
   * supported by the second table and the last two have their own.
252
   */
253 254 255 256 257 258
  IFont                       IFont_iface;
  IDispatch                   IDispatch_iface;
  IPersistStream              IPersistStream_iface;
  IConnectionPointContainer   IConnectionPointContainer_iface;
  IPersistPropertyBag         IPersistPropertyBag_iface;
  IPersistStreamInit          IPersistStreamInit_iface;
259 260 261
  /*
   * Reference count for that instance of the class.
   */
262
  LONG ref;
263 264 265 266 267

  /*
   * This structure contains the description of the class.
   */
  FONTDESC description;
268 269 270 271 272

  /*
   * Contain the font associated with this object.
   */
  HFONT gdiFont;
273
  BOOL dirty;
274 275 276
  /*
   * Size ratio
   */
277 278
  LONG cyLogical;
  LONG cyHimetric;
279

280 281 282 283 284
  /*
   * Stash realized height (pixels) from TEXTMETRIC - used in get_Size()
   */
  LONG nRealHeight;

285 286
  IConnectionPoint *pPropertyNotifyCP;
  IConnectionPoint *pFontEventsCP;
287 288
};

289 290 291 292 293
static inline OLEFontImpl *impl_from_IFont(IFont *iface)
{
    return CONTAINING_RECORD(iface, OLEFontImpl, IFont_iface);
}

294 295
static inline OLEFontImpl *impl_from_IDispatch( IDispatch *iface )
{
296
    return CONTAINING_RECORD(iface, OLEFontImpl, IDispatch_iface);
297 298 299 300
}

static inline OLEFontImpl *impl_from_IPersistStream( IPersistStream *iface )
{
301
    return CONTAINING_RECORD(iface, OLEFontImpl, IPersistStream_iface);
302 303 304 305
}

static inline OLEFontImpl *impl_from_IConnectionPointContainer( IConnectionPointContainer *iface )
{
306
    return CONTAINING_RECORD(iface, OLEFontImpl, IConnectionPointContainer_iface);
307 308 309 310
}

static inline OLEFontImpl *impl_from_IPersistPropertyBag( IPersistPropertyBag *iface )
{
311
    return CONTAINING_RECORD(iface, OLEFontImpl, IPersistPropertyBag_iface);
312 313 314 315
}

static inline OLEFontImpl *impl_from_IPersistStreamInit( IPersistStreamInit *iface )
{
316
    return CONTAINING_RECORD(iface, OLEFontImpl, IPersistStreamInit_iface);
317
}
318

319 320 321 322 323

/***********************************************************************
 * Prototypes for the implementation functions for the IFont
 * interface
 */
324
static OLEFontImpl* OLEFontImpl_Construct(const FONTDESC *fontDesc);
325
static void         OLEFontImpl_Destroy(OLEFontImpl* fontDesc);
Marcus Meissner's avatar
Marcus Meissner committed
326
static ULONG        WINAPI OLEFontImpl_AddRef(IFont* iface);
327

328 329 330
/******************************************************************************
 *		OleCreateFontIndirect	[OLEAUT32.420]
 */
331
HRESULT WINAPI OleCreateFontIndirect(
332 333
  LPFONTDESC lpFontDesc,
  REFIID     riid,
334
  LPVOID*     ppvObj)
335
{
336 337
  OLEFontImpl* newFont;
  HRESULT      hr;
338
  FONTDESC     fd;
339

340
  TRACE("(%p, %s, %p)\n", lpFontDesc, debugstr_guid(riid), ppvObj);
341 342

  if (!ppvObj) return E_POINTER;
343 344 345

  *ppvObj = 0;

346
  if (!lpFontDesc) {
347
    static WCHAR fname[] = { 'S','y','s','t','e','m',0 };
348 349

    fd.cbSizeofstruct = sizeof(fd);
350
    fd.lpstrName      = fname;
351 352 353 354
    fd.cySize.s.Lo    = 80000;
    fd.cySize.s.Hi    = 0;
    fd.sWeight 	      = 0;
    fd.sCharset       = 0;
355 356 357
    fd.fItalic        = FALSE;
    fd.fUnderline     = FALSE;
    fd.fStrikethrough = FALSE;
358 359
    lpFontDesc = &fd;
  }
360

361
  newFont = OLEFontImpl_Construct(lpFontDesc);
362
  if (!newFont) return E_OUTOFMEMORY;
363

364 365
  hr = IFont_QueryInterface(&newFont->IFont_iface, riid, ppvObj);
  IFont_Release(&newFont->IFont_iface);
366 367 368 369 370 371 372 373 374

  return hr;
}


/***********************************************************************
 * Implementation of the OLEFontImpl class.
 */

375 376 377 378 379 380 381 382
/***********************************************************************
 *    OLEFont_SendNotify (internal)
 *
 * Sends notification messages of changed properties to any interested
 * connections.
 */
static void OLEFont_SendNotify(OLEFontImpl* this, DISPID dispID)
{
383 384 385 386 387 388 389
  static const WCHAR wszName[] = {'N','a','m','e',0};
  static const WCHAR wszSize[] = {'S','i','z','e',0};
  static const WCHAR wszBold[] = {'B','o','l','d',0};
  static const WCHAR wszItalic[] = {'I','t','a','l','i','c',0};
  static const WCHAR wszUnder[] = {'U','n','d','e','r','l','i','n','e',0};
  static const WCHAR wszStrike[] = {'S','t','r','i','k','e','t','h','r','o','u','g','h',0};
  static const WCHAR wszWeight[] = {'W','e','i','g','h','t',0};
390
  static const WCHAR wszCharset[] = {'C','h','a','r','s','e','t',0};
391 392 393 394 395 396 397 398 399 400 401 402 403
  static const LPCWSTR dispid_mapping[] =
  {
    wszName,
    NULL,
    wszSize,
    wszBold,
    wszItalic,
    wszUnder,
    wszStrike,
    wszWeight,
    wszCharset
  };

404 405
  IEnumConnections *pEnum;
  CONNECTDATA CD;
406
  HRESULT hres;
407

408 409
  this->dirty = TRUE;

410 411 412 413 414 415
  hres = IConnectionPoint_EnumConnections(this->pPropertyNotifyCP, &pEnum);
  if (SUCCEEDED(hres))
  {
    while(IEnumConnections_Next(pEnum, 1, &CD, NULL) == S_OK) {
      IPropertyNotifySink *sink;

416
      IUnknown_QueryInterface(CD.pUnk, &IID_IPropertyNotifySink, (void**)&sink);
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
      IPropertyNotifySink_OnChanged(sink, dispID);
      IPropertyNotifySink_Release(sink);
      IUnknown_Release(CD.pUnk);
    }
    IEnumConnections_Release(pEnum);
  }

  hres = IConnectionPoint_EnumConnections(this->pFontEventsCP, &pEnum);
  if (SUCCEEDED(hres))
  {
    DISPPARAMS dispparams;
    VARIANTARG vararg;

    VariantInit(&vararg);
    V_VT(&vararg) = VT_BSTR;
    V_BSTR(&vararg) = SysAllocString(dispid_mapping[dispID]);
433

434 435 436 437
    dispparams.cArgs = 1;
    dispparams.cNamedArgs = 0;
    dispparams.rgdispidNamedArgs = NULL;
    dispparams.rgvarg = &vararg;
438

439 440 441
    while(IEnumConnections_Next(pEnum, 1, &CD, NULL) == S_OK) {
        IFontEventsDisp *disp;

442
        IUnknown_QueryInterface(CD.pUnk, &IID_IFontEventsDisp, (void**)&disp);
443 444 445
        IFontEventsDisp_Invoke(disp, DISPID_FONT_CHANGED, &IID_NULL,
                               LOCALE_NEUTRAL, INVOKE_FUNC, &dispparams, NULL,
                               NULL, NULL);
446

447
        IFontEventsDisp_Release(disp);
448 449 450 451
        IUnknown_Release(CD.pUnk);
    }
    VariantClear(&vararg);
    IEnumConnections_Release(pEnum);
452 453
  }
}
454

455 456 457 458 459
/************************************************************************
 * OLEFontImpl_QueryInterface (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
460
static HRESULT WINAPI OLEFontImpl_QueryInterface(
461 462 463 464
  IFont*  iface,
  REFIID  riid,
  void**  ppvObject)
{
465
  OLEFontImpl *this = impl_from_IFont(iface);
466

467
  TRACE("(%p)->(%s, %p)\n", this, debugstr_guid(riid), ppvObject);
468 469

  *ppvObject = 0;
470

471 472 473
  if (IsEqualGUID(&IID_IUnknown, riid) ||
      IsEqualGUID(&IID_IFont, riid))
  {
474
    *ppvObject = this;
475 476 477 478
  }
  else if (IsEqualGUID(&IID_IDispatch, riid) ||
           IsEqualGUID(&IID_IFontDisp, riid))
  {
479
    *ppvObject = &this->IDispatch_iface;
480 481 482 483
  }
  else if (IsEqualGUID(&IID_IPersist, riid) ||
           IsEqualGUID(&IID_IPersistStream, riid))
  {
484
    *ppvObject = &this->IPersistStream_iface;
485 486 487
  }
  else if (IsEqualGUID(&IID_IConnectionPointContainer, riid))
  {
488
    *ppvObject = &this->IConnectionPointContainer_iface;
489 490 491
  }
  else if (IsEqualGUID(&IID_IPersistPropertyBag, riid))
  {
492
    *ppvObject = &this->IPersistPropertyBag_iface;
493 494 495
  }
  else if (IsEqualGUID(&IID_IPersistStreamInit, riid))
  {
496
    *ppvObject = &this->IPersistStreamInit_iface;
497
  }
498

499
  if (!*ppvObject)
500
  {
501
    FIXME("() : asking for unsupported interface %s\n", debugstr_guid(riid));
502
    return E_NOINTERFACE;
503
  }
504 505 506

  IFont_AddRef(iface);

507
  return S_OK;
508
}
509

510 511 512
/************************************************************************
 * OLEFontImpl_AddRef (IUnknown)
 */
513
static ULONG WINAPI OLEFontImpl_AddRef(
514 515
  IFont* iface)
{
516
  OLEFontImpl *this = impl_from_IFont(iface);
517
  TRACE("(%p)->(ref=%d)\n", this, this->ref);
518
  return InterlockedIncrement(&this->ref);
519
}
520

521 522 523
/************************************************************************
 * OLEFontImpl_Release (IUnknown)
 */
524
static ULONG WINAPI OLEFontImpl_Release(IFont* iface)
525
{
526
  OLEFontImpl *this = impl_from_IFont(iface);
527 528
  ULONG ref;

529
  TRACE("(%p)->(ref=%d)\n", this, this->ref);
530

531
  ref = InterlockedDecrement(&this->ref);
532

533
  if (ref == 0)
534 535
  {
    ULONG fontlist_refs = InterlockedDecrement(&ifont_cnt);
536 537

    /* Final IFont object so destroy font cache */
538 539
    if (fontlist_refs == 0)
    {
540 541
      HFONTItem *item, *cursor2;

542
      EnterCriticalSection(&OLEFontImpl_csHFONTLIST);
543 544
      LIST_FOR_EACH_ENTRY_SAFE(item, cursor2, &OLEFontImpl_hFontList, HFONTItem, entry)
        HFONTItem_Delete(item);
545
      LeaveCriticalSection(&OLEFontImpl_csHFONTLIST);
546
      delete_dc();
547
    }
548 549 550 551
    else
    {
      dec_int_ref(this->gdiFont);
    }
552 553
    OLEFontImpl_Destroy(this);
  }
554

555
  return ref;
556
}
557

558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
typedef struct
{
    short orig_cs;
    short avail_cs;
} enum_data;

static int CALLBACK font_enum_proc(const LOGFONTW *elf, const TEXTMETRICW *ntm, DWORD type, LPARAM lp)
{
    enum_data *data = (enum_data*)lp;

    if(elf->lfCharSet == data->orig_cs)
    {
        data->avail_cs = data->orig_cs;
        return 0;
    }
    if(data->avail_cs == -1) data->avail_cs = elf->lfCharSet;
    return 1;
}

577 578
static void realize_font(OLEFontImpl *This)
{
579 580 581 582 583 584 585 586 587 588 589 590
    LOGFONTW logFont;
    INT fontHeight;
    WCHAR text_face[LF_FACESIZE];
    HDC hdc = get_dc();
    HFONT old_font;
    TEXTMETRICW tm;

    if (!This->dirty) return;

    text_face[0] = 0;

    if(This->gdiFont)
591
    {
592
        old_font = SelectObject(hdc, This->gdiFont);
593
        GetTextFaceW(hdc, ARRAY_SIZE(text_face), text_face);
594
        SelectObject(hdc, old_font);
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
        dec_int_ref(This->gdiFont);
        This->gdiFont = 0;
    }

    memset(&logFont, 0, sizeof(LOGFONTW));

    lstrcpynW(logFont.lfFaceName, This->description.lpstrName, LF_FACESIZE);
    logFont.lfCharSet = This->description.sCharset;

    /* If the font name has been changed then enumerate all charsets
       and pick one that'll result in the font specified being selected */
    if(text_face[0] && lstrcmpiW(text_face, This->description.lpstrName))
    {
        enum_data data;
        data.orig_cs = This->description.sCharset;
        data.avail_cs = -1;
        logFont.lfCharSet = DEFAULT_CHARSET;
        EnumFontFamiliesExW(get_dc(), &logFont, font_enum_proc, (LPARAM)&data, 0);
        if(data.avail_cs != -1) logFont.lfCharSet = data.avail_cs;
    }

    /*
     * The height of the font returned by the get_Size property is the
     * height of the font in points multiplied by 10000... Using some
     * simple conversions and the ratio given by the application, it can
     * be converted to a height in pixels.
     *
     * Standard ratio is 72 / 2540, or 18 / 635 in lowest terms.
     * Ratio is applied here relative to the standard.
     */

    fontHeight = MulDiv( This->description.cySize.s.Lo, This->cyLogical*635, This->cyHimetric*18 );

    logFont.lfHeight          = ((fontHeight%10000L)>5000L) ? (-fontHeight/10000L) - 1 :
                                                                  (-fontHeight/10000L);
    logFont.lfItalic          = This->description.fItalic;
    logFont.lfUnderline       = This->description.fUnderline;
    logFont.lfStrikeOut       = This->description.fStrikethrough;
    logFont.lfWeight          = This->description.sWeight;
    logFont.lfOutPrecision    = OUT_CHARACTER_PRECIS;
    logFont.lfClipPrecision   = CLIP_DEFAULT_PRECIS;
    logFont.lfQuality         = DEFAULT_QUALITY;
    logFont.lfPitchAndFamily  = DEFAULT_PITCH;

    This->gdiFont = CreateFontIndirectW(&logFont);
    This->dirty = FALSE;

    add_hfontitem(This->gdiFont);

    /* Fixup the name and charset properties so that they match the
       selected font */
    old_font = SelectObject(get_dc(), This->gdiFont);
647
    GetTextFaceW(hdc, ARRAY_SIZE(text_face), text_face);
648 649 650 651
    if(lstrcmpiW(text_face, This->description.lpstrName))
    {
        HeapFree(GetProcessHeap(), 0, This->description.lpstrName);
        This->description.lpstrName = strdupW(text_face);
652
    }
653 654 655 656 657
    GetTextMetricsW(hdc, &tm);
    This->description.sCharset = tm.tmCharSet;
    /* While we have it handy, stash the realized font height for use by get_Size() */
    This->nRealHeight = tm.tmHeight - tm.tmInternalLeading; /* corresponds to LOGFONT lfHeight */
    SelectObject(hdc, old_font);
658 659
}

660 661 662 663 664
/************************************************************************
 * OLEFontImpl_get_Name (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
665
static HRESULT WINAPI OLEFontImpl_get_Name(
666
  IFont*  iface,
667
  BSTR* pname)
668
{
669
  OLEFontImpl *this = impl_from_IFont(iface);
670
  TRACE("(%p)->(%p)\n", this, pname);
671

672 673 674
  if (pname==0)
    return E_POINTER;

675
  realize_font(this);
676

677
  if (this->description.lpstrName!=0)
678
    *pname = SysAllocString(this->description.lpstrName);
679 680 681 682 683 684 685 686 687
  else
    *pname = 0;

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_put_Name (IFont)
 */
Marcus Meissner's avatar
Marcus Meissner committed
688
static HRESULT WINAPI OLEFontImpl_put_Name(
689
  IFont* iface,
690
  BSTR name)
691
{
692 693
  OLEFontImpl *This = impl_from_IFont(iface);
  TRACE("(%p)->(%p)\n", This, name);
694

695 696 697
  if (!name)
    return CTL_E_INVALIDPROPERTYVALUE;

698 699 700
  HeapFree(GetProcessHeap(), 0, This->description.lpstrName);
  This->description.lpstrName = strdupW(name);
  if (!This->description.lpstrName) return E_OUTOFMEMORY;
701

702 703
  TRACE("new name %s\n", debugstr_w(This->description.lpstrName));
  OLEFont_SendNotify(This, DISPID_FONT_NAME);
704 705 706 707 708 709
  return S_OK;
}

/************************************************************************
 * OLEFontImpl_get_Size (IFont)
 */
Marcus Meissner's avatar
Marcus Meissner committed
710
static HRESULT WINAPI OLEFontImpl_get_Size(
711
  IFont* iface,
712 713
  CY*    psize)
{
714
  OLEFontImpl *this = impl_from_IFont(iface);
715
  TRACE("(%p)->(%p)\n", this, psize);
716

717
  if (!psize) return E_POINTER;
718

719
  realize_font(this);
720

721 722 723 724 725 726 727
  /*
   * Convert realized font height in pixels to points descaled by current
   * scaling ratio then scaled up by 10000.
   */
  psize->s.Lo = MulDiv(this->nRealHeight,
                       this->cyHimetric * 72 * 10000,
                       this->cyLogical * 2540);
728
  psize->s.Hi = 0;
729 730 731 732 733 734 735

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_put_Size (IFont)
 */
Marcus Meissner's avatar
Marcus Meissner committed
736
static HRESULT WINAPI OLEFontImpl_put_Size(
737
  IFont* iface,
738 739
  CY     size)
{
740
  OLEFontImpl *this = impl_from_IFont(iface);
741
  TRACE("(%p)->(%d)\n", this, size.s.Lo);
742
  this->description.cySize.s.Hi = 0;
743 744
  this->description.cySize.s.Lo = size.s.Lo;
  OLEFont_SendNotify(this, DISPID_FONT_SIZE);
745 746 747 748 749 750 751 752 753

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_get_Bold (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
754
static HRESULT WINAPI OLEFontImpl_get_Bold(
755
  IFont*  iface,
756
  BOOL* pbold)
757
{
758
  OLEFontImpl *this = impl_from_IFont(iface);
759
  TRACE("(%p)->(%p)\n", this, pbold);
760 761

  if (!pbold) return E_POINTER;
762

763
  realize_font(this);
764

765 766 767
  *pbold = this->description.sWeight > 550;

  return S_OK;
768 769 770 771 772
}

/************************************************************************
 * OLEFontImpl_put_Bold (IFont)
 */
Marcus Meissner's avatar
Marcus Meissner committed
773
static HRESULT WINAPI OLEFontImpl_put_Bold(
774
  IFont* iface,
775
  BOOL bold)
776
{
777
  OLEFontImpl *this = impl_from_IFont(iface);
778 779 780 781 782
  TRACE("(%p)->(%d)\n", this, bold);
  this->description.sWeight = bold ? FW_BOLD : FW_NORMAL;
  OLEFont_SendNotify(this, DISPID_FONT_BOLD);

  return S_OK;
783 784 785 786 787
}

/************************************************************************
 * OLEFontImpl_get_Italic (IFont)
 */
Marcus Meissner's avatar
Marcus Meissner committed
788
static HRESULT WINAPI OLEFontImpl_get_Italic(
789
  IFont*  iface,
790
  BOOL* pitalic)
791
{
792
  OLEFontImpl *this = impl_from_IFont(iface);
793
  TRACE("(%p)->(%p)\n", this, pitalic);
794

795 796 797
  if (pitalic==0)
    return E_POINTER;

798
  realize_font(this);
799

800 801 802 803 804 805 806 807
  *pitalic = this->description.fItalic;

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_put_Italic (IFont)
 */
Marcus Meissner's avatar
Marcus Meissner committed
808
static HRESULT WINAPI OLEFontImpl_put_Italic(
809
  IFont* iface,
810
  BOOL italic)
811
{
812
  OLEFontImpl *this = impl_from_IFont(iface);
813
  TRACE("(%p)->(%d)\n", this, italic);
814 815 816

  this->description.fItalic = italic;

817
  OLEFont_SendNotify(this, DISPID_FONT_ITALIC);
818 819 820 821 822 823
  return S_OK;
}

/************************************************************************
 * OLEFontImpl_get_Underline (IFont)
 */
Marcus Meissner's avatar
Marcus Meissner committed
824
static HRESULT WINAPI OLEFontImpl_get_Underline(
825
  IFont*  iface,
826
  BOOL* punderline)
827
{
828
  OLEFontImpl *this = impl_from_IFont(iface);
829
  TRACE("(%p)->(%p)\n", this, punderline);
830 831 832 833

  if (punderline==0)
    return E_POINTER;

834
  realize_font(this);
835

836 837 838 839 840 841 842 843
  *punderline = this->description.fUnderline;

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_put_Underline (IFont)
 */
Marcus Meissner's avatar
Marcus Meissner committed
844
static HRESULT WINAPI OLEFontImpl_put_Underline(
845
  IFont* iface,
846
  BOOL underline)
847
{
848
  OLEFontImpl *this = impl_from_IFont(iface);
849
  TRACE("(%p)->(%d)\n", this, underline);
850 851 852

  this->description.fUnderline = underline;

853
  OLEFont_SendNotify(this, DISPID_FONT_UNDER);
854 855 856 857 858 859
  return S_OK;
}

/************************************************************************
 * OLEFontImpl_get_Strikethrough (IFont)
 */
Marcus Meissner's avatar
Marcus Meissner committed
860
static HRESULT WINAPI OLEFontImpl_get_Strikethrough(
861
  IFont*  iface,
862
  BOOL* pstrikethrough)
863
{
864
  OLEFontImpl *this = impl_from_IFont(iface);
865
  TRACE("(%p)->(%p)\n", this, pstrikethrough);
866 867 868 869

  if (pstrikethrough==0)
    return E_POINTER;

870
  realize_font(this);
871

872
  *pstrikethrough = this->description.fStrikethrough;
873 874 875 876 877 878 879

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_put_Strikethrough (IFont)
 */
Marcus Meissner's avatar
Marcus Meissner committed
880
static HRESULT WINAPI OLEFontImpl_put_Strikethrough(
881
 IFont* iface,
882
 BOOL strikethrough)
883
{
884
  OLEFontImpl *this = impl_from_IFont(iface);
885
  TRACE("(%p)->(%d)\n", this, strikethrough);
886

887
  this->description.fStrikethrough = strikethrough;
888
  OLEFont_SendNotify(this, DISPID_FONT_STRIKE);
889 890 891 892 893 894 895

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_get_Weight (IFont)
 */
Marcus Meissner's avatar
Marcus Meissner committed
896
static HRESULT WINAPI OLEFontImpl_get_Weight(
897
  IFont* iface,
898 899
  short* pweight)
{
900
  OLEFontImpl *this = impl_from_IFont(iface);
901
  TRACE("(%p)->(%p)\n", this, pweight);
902 903 904 905

  if (pweight==0)
    return E_POINTER;

906
  realize_font(this);
907

908 909 910 911 912 913 914 915
  *pweight = this->description.sWeight;

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_put_Weight (IFont)
 */
Marcus Meissner's avatar
Marcus Meissner committed
916
static HRESULT WINAPI OLEFontImpl_put_Weight(
917
  IFont* iface,
918 919
  short  weight)
{
920
  OLEFontImpl *this = impl_from_IFont(iface);
921
  TRACE("(%p)->(%d)\n", this, weight);
922 923 924

  this->description.sWeight = weight;

925
  OLEFont_SendNotify(this, DISPID_FONT_WEIGHT);
926 927 928 929 930 931
  return S_OK;
}

/************************************************************************
 * OLEFontImpl_get_Charset (IFont)
 */
Marcus Meissner's avatar
Marcus Meissner committed
932
static HRESULT WINAPI OLEFontImpl_get_Charset(
933
  IFont* iface,
934 935
  short* pcharset)
{
936
  OLEFontImpl *this = impl_from_IFont(iface);
937
  TRACE("(%p)->(%p)\n", this, pcharset);
938 939 940 941

  if (pcharset==0)
    return E_POINTER;

942
  realize_font(this);
943

944 945 946 947 948 949 950 951
  *pcharset = this->description.sCharset;

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_put_Charset (IFont)
 */
Marcus Meissner's avatar
Marcus Meissner committed
952
static HRESULT WINAPI OLEFontImpl_put_Charset(
953
  IFont* iface,
954 955
  short charset)
{
956
  OLEFontImpl *this = impl_from_IFont(iface);
957
  TRACE("(%p)->(%d)\n", this, charset);
958 959

  this->description.sCharset = charset;
960
  OLEFont_SendNotify(this, DISPID_FONT_CHARSET);
961 962 963 964 965 966 967

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_get_hFont (IFont)
 */
Marcus Meissner's avatar
Marcus Meissner committed
968
static HRESULT WINAPI OLEFontImpl_get_hFont(
969
  IFont*   iface,
970
  HFONT* phfont)
971
{
972
  OLEFontImpl *this = impl_from_IFont(iface);
973
  TRACE("(%p)->(%p)\n", this, phfont);
974 975 976
  if (phfont==NULL)
    return E_POINTER;

977
  realize_font(this);
978 979

  *phfont = this->gdiFont;
980
  TRACE("Returning %p\n", *phfont);
981
  return S_OK;
982 983 984 985 986
}

/************************************************************************
 * OLEFontImpl_Clone (IFont)
 */
Marcus Meissner's avatar
Marcus Meissner committed
987
static HRESULT WINAPI OLEFontImpl_Clone(
988 989 990
  IFont*  iface,
  IFont** ppfont)
{
991
  OLEFontImpl *this = impl_from_IFont(iface);
992
  OLEFontImpl* newObject;
993

994
  TRACE("(%p)->(%p)\n", this, ppfont);
995 996 997 998 999 1000 1001 1002 1003 1004 1005

  if (ppfont == NULL)
    return E_POINTER;

  *ppfont = NULL;

  newObject = HeapAlloc(GetProcessHeap(), 0, sizeof(OLEFontImpl));
  if (newObject==NULL)
    return E_OUTOFMEMORY;

  *newObject = *this;
1006 1007
  /* allocate separate buffer */
  newObject->description.lpstrName = strdupW(this->description.lpstrName);
1008

1009 1010
  /* Increment internal ref in hfont item list */
  if(newObject->gdiFont) inc_int_ref(newObject->gdiFont);
1011

1012 1013
  InterlockedIncrement(&ifont_cnt);

1014 1015
  newObject->pPropertyNotifyCP = NULL;
  newObject->pFontEventsCP = NULL;
1016 1017 1018 1019
  CreateConnectionPoint((IUnknown*)&newObject->IFont_iface, &IID_IPropertyNotifySink,
                         &newObject->pPropertyNotifyCP);
  CreateConnectionPoint((IUnknown*)&newObject->IFont_iface, &IID_IFontEventsDisp,
                         &newObject->pFontEventsCP);
1020 1021 1022 1023 1024 1025

  if (!newObject->pPropertyNotifyCP || !newObject->pFontEventsCP)
  {
    OLEFontImpl_Destroy(newObject);
    return E_OUTOFMEMORY;
  }
1026 1027

  /* The cloned object starts with a reference count of 1 */
1028
  newObject->ref = 1;
1029

1030
  *ppfont = &newObject->IFont_iface;
1031 1032

  return S_OK;
1033 1034 1035 1036 1037
}

/************************************************************************
 * OLEFontImpl_IsEqual (IFont)
 */
Marcus Meissner's avatar
Marcus Meissner committed
1038
static HRESULT WINAPI OLEFontImpl_IsEqual(
1039
  IFont* iface,
1040 1041
  IFont* pFontOther)
{
1042 1043
  OLEFontImpl *left = impl_from_IFont(iface);
  OLEFontImpl *right = impl_from_IFont(pFontOther);
1044
  INT ret;
1045 1046
  INT left_len,right_len;

1047
  if(pFontOther == NULL)
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
    return E_POINTER;
  else if (left->description.cySize.s.Lo != right->description.cySize.s.Lo)
    return S_FALSE;
  else if (left->description.cySize.s.Hi != right->description.cySize.s.Hi)
    return S_FALSE;
  else if (left->description.sWeight != right->description.sWeight)
    return S_FALSE;
  else if (left->description.sCharset != right->description.sCharset)
    return S_FALSE;
  else if (left->description.fItalic != right->description.fItalic)
    return S_FALSE;
  else if (left->description.fUnderline != right->description.fUnderline)
    return S_FALSE;
  else if (left->description.fStrikethrough != right->description.fStrikethrough)
    return S_FALSE;

  /* Check from string */
1065 1066
  left_len = lstrlenW(left->description.lpstrName);
  right_len = lstrlenW(right->description.lpstrName);
1067
  ret = CompareStringW(0,0,left->description.lpstrName, left_len,
1068
    right->description.lpstrName, right_len);
1069
  if (ret != CSTR_EQUAL)
1070 1071 1072
    return S_FALSE;

  return S_OK;
1073 1074 1075 1076 1077
}

/************************************************************************
 * OLEFontImpl_SetRatio (IFont)
 */
Marcus Meissner's avatar
Marcus Meissner committed
1078
static HRESULT WINAPI OLEFontImpl_SetRatio(
1079
  IFont* iface,
1080 1081
  LONG   cyLogical,
  LONG   cyHimetric)
1082
{
1083
  OLEFontImpl *this = impl_from_IFont(iface);
1084
  TRACE("(%p)->(%d, %d)\n", this, cyLogical, cyHimetric);
1085

1086
  if(cyLogical == 0 || cyHimetric == 0)
1087
    return E_FAIL;
1088

1089 1090 1091 1092 1093
  /* cyLogical and cyHimetric both set to 1 is a special case that
     does not change the scaling but also does not fail */
  if(cyLogical == 1 && cyHimetric == 1)
    return S_OK;

1094 1095
  this->cyLogical  = cyLogical;
  this->cyHimetric = cyHimetric;
1096
  this->dirty = TRUE;
1097 1098

  return S_OK;
1099 1100 1101 1102 1103
}

/************************************************************************
 * OLEFontImpl_QueryTextMetrics (IFont)
 */
Marcus Meissner's avatar
Marcus Meissner committed
1104
static HRESULT      WINAPI OLEFontImpl_QueryTextMetrics(
1105
  IFont*         iface,
1106 1107
  TEXTMETRICOLE* ptm)
{
1108 1109 1110 1111
  HDC hdcRef;
  HFONT hOldFont, hNewFont;

  hdcRef = GetDC(0);
1112
  IFont_get_hFont(iface, &hNewFont);
1113 1114 1115 1116 1117
  hOldFont = SelectObject(hdcRef, hNewFont);
  GetTextMetricsW(hdcRef, ptm);
  SelectObject(hdcRef, hOldFont);
  ReleaseDC(0, hdcRef);
  return S_OK;
1118 1119 1120 1121 1122
}

/************************************************************************
 * OLEFontImpl_AddRefHfont (IFont)
 */
Marcus Meissner's avatar
Marcus Meissner committed
1123
static HRESULT WINAPI OLEFontImpl_AddRefHfont(
1124
  IFont*  iface,
1125
  HFONT hfont)
1126
{
1127
    OLEFontImpl *this = impl_from_IFont(iface);
1128

1129
    TRACE("(%p)->(%p)\n", this, hfont);
1130

1131
    if (!hfont) return E_INVALIDARG;
1132

1133
    return inc_ext_ref(hfont);
1134 1135 1136 1137 1138
}

/************************************************************************
 * OLEFontImpl_ReleaseHfont (IFont)
 */
Marcus Meissner's avatar
Marcus Meissner committed
1139
static HRESULT WINAPI OLEFontImpl_ReleaseHfont(
1140
  IFont*  iface,
1141
  HFONT hfont)
1142
{
1143
    OLEFontImpl *this = impl_from_IFont(iface);
1144

1145
    TRACE("(%p)->(%p)\n", this, hfont);
1146

1147
    if (!hfont) return E_INVALIDARG;
1148

1149
    return dec_ext_ref(hfont);
1150 1151 1152 1153 1154
}

/************************************************************************
 * OLEFontImpl_SetHdc (IFont)
 */
Marcus Meissner's avatar
Marcus Meissner committed
1155
static HRESULT WINAPI OLEFontImpl_SetHdc(
1156
  IFont* iface,
1157
  HDC  hdc)
1158
{
1159
  OLEFontImpl *this = impl_from_IFont(iface);
1160
  FIXME("(%p)->(%p): Stub\n", this, hdc);
1161 1162 1163
  return E_NOTIMPL;
}

1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
static const IFontVtbl OLEFontImpl_VTable =
{
  OLEFontImpl_QueryInterface,
  OLEFontImpl_AddRef,
  OLEFontImpl_Release,
  OLEFontImpl_get_Name,
  OLEFontImpl_put_Name,
  OLEFontImpl_get_Size,
  OLEFontImpl_put_Size,
  OLEFontImpl_get_Bold,
  OLEFontImpl_put_Bold,
  OLEFontImpl_get_Italic,
  OLEFontImpl_put_Italic,
  OLEFontImpl_get_Underline,
  OLEFontImpl_put_Underline,
  OLEFontImpl_get_Strikethrough,
  OLEFontImpl_put_Strikethrough,
  OLEFontImpl_get_Weight,
  OLEFontImpl_put_Weight,
  OLEFontImpl_get_Charset,
  OLEFontImpl_put_Charset,
  OLEFontImpl_get_hFont,
  OLEFontImpl_Clone,
  OLEFontImpl_IsEqual,
  OLEFontImpl_SetRatio,
  OLEFontImpl_QueryTextMetrics,
  OLEFontImpl_AddRefHfont,
  OLEFontImpl_ReleaseHfont,
  OLEFontImpl_SetHdc
};

1195 1196 1197
/************************************************************************
 * OLEFontImpl_IDispatch_QueryInterface (IUnknown)
 */
Marcus Meissner's avatar
Marcus Meissner committed
1198
static HRESULT WINAPI OLEFontImpl_IDispatch_QueryInterface(
1199 1200 1201 1202
  IDispatch* iface,
  REFIID     riid,
  VOID**     ppvoid)
{
1203
  OLEFontImpl *this = impl_from_IDispatch(iface);
1204
  return IFont_QueryInterface(&this->IFont_iface, riid, ppvoid);
1205 1206 1207 1208 1209
}

/************************************************************************
 * OLEFontImpl_IDispatch_Release (IUnknown)
 */
Marcus Meissner's avatar
Marcus Meissner committed
1210
static ULONG WINAPI OLEFontImpl_IDispatch_Release(
1211 1212
  IDispatch* iface)
{
1213
  OLEFontImpl *this = impl_from_IDispatch(iface);
1214
  return IFont_Release(&this->IFont_iface);
1215 1216 1217 1218 1219
}

/************************************************************************
 * OLEFontImpl_IDispatch_AddRef (IUnknown)
 */
Marcus Meissner's avatar
Marcus Meissner committed
1220
static ULONG WINAPI OLEFontImpl_IDispatch_AddRef(
1221 1222
  IDispatch* iface)
{
1223
  OLEFontImpl *this = impl_from_IDispatch(iface);
1224
  return IFont_AddRef(&this->IFont_iface);
1225 1226 1227 1228 1229
}

/************************************************************************
 * OLEFontImpl_GetTypeInfoCount (IDispatch)
 */
Marcus Meissner's avatar
Marcus Meissner committed
1230
static HRESULT WINAPI OLEFontImpl_GetTypeInfoCount(
1231
  IDispatch*    iface,
1232 1233
  unsigned int* pctinfo)
{
1234
  OLEFontImpl *this = impl_from_IDispatch(iface);
1235 1236
  TRACE("(%p)->(%p)\n", this, pctinfo);
  *pctinfo = 1;
1237

1238
  return S_OK;
1239 1240 1241 1242 1243
}

/************************************************************************
 * OLEFontImpl_GetTypeInfo (IDispatch)
 */
Marcus Meissner's avatar
Marcus Meissner committed
1244
static HRESULT WINAPI OLEFontImpl_GetTypeInfo(
1245
  IDispatch*  iface,
1246
  UINT      iTInfo,
1247
  LCID        lcid,
1248 1249
  ITypeInfo** ppTInfo)
{
1250
  static const WCHAR stdole2tlb[] = {'s','t','d','o','l','e','2','.','t','l','b',0};
1251 1252
  ITypeLib *tl;
  HRESULT hres;
1253

1254
  OLEFontImpl *this = impl_from_IDispatch(iface);
1255
  TRACE("(%p, iTInfo=%d, lcid=%04x, %p)\n", this, iTInfo, (int)lcid, ppTInfo);
1256 1257
  if (iTInfo != 0)
    return E_FAIL;
1258
  hres = LoadTypeLib(stdole2tlb, &tl);
1259
  if (FAILED(hres)) {
1260
    ERR("Could not load the stdole2.tlb?\n");
1261 1262
    return hres;
  }
1263
  hres = ITypeLib_GetTypeInfoOfGuid(tl, &IID_IFontDisp, ppTInfo);
1264
  ITypeLib_Release(tl);
1265
  if (FAILED(hres)) {
1266
    FIXME("Did not IDispatch typeinfo from typelib, hres %x\n",hres);
1267 1268
  }
  return hres;
1269 1270 1271 1272 1273
}

/************************************************************************
 * OLEFontImpl_GetIDsOfNames (IDispatch)
 */
Marcus Meissner's avatar
Marcus Meissner committed
1274
static HRESULT WINAPI OLEFontImpl_GetIDsOfNames(
1275
  IDispatch*  iface,
1276 1277 1278
  REFIID      riid,
  LPOLESTR* rgszNames,
  UINT      cNames,
1279 1280 1281
  LCID        lcid,
  DISPID*     rgDispId)
{
1282 1283 1284
  ITypeInfo * pTInfo;
  HRESULT hres;

1285
  OLEFontImpl *this = impl_from_IDispatch(iface);
1286 1287 1288 1289

  TRACE("(%p,%s,%p,cNames=%d,lcid=%04x,%p)\n", this, debugstr_guid(riid),
        rgszNames, cNames, (int)lcid, rgDispId);

1290 1291
  if (cNames == 0) return E_INVALIDARG;

1292
  hres = IDispatch_GetTypeInfo(iface, 0, lcid, &pTInfo);
1293
  if (FAILED(hres))
1294
  {
1295 1296
    ERR("GetTypeInfo failed.\n");
    return hres;
1297 1298
  }

1299 1300 1301
  /* convert names to DISPIDs */
  hres = DispGetIDsOfNames (pTInfo, rgszNames, cNames, rgDispId);
  ITypeInfo_Release(pTInfo);
1302

1303
  return hres;
1304 1305 1306 1307
}

/************************************************************************
 * OLEFontImpl_Invoke (IDispatch)
1308
 * 
1309
 */
Marcus Meissner's avatar
Marcus Meissner committed
1310
static HRESULT WINAPI OLEFontImpl_Invoke(
1311
  IDispatch*  iface,
1312 1313 1314
  DISPID      dispIdMember,
  REFIID      riid,
  LCID        lcid,
1315 1316
  WORD        wFlags,
  DISPPARAMS* pDispParams,
1317
  VARIANT*    pVarResult,
1318
  EXCEPINFO*  pExepInfo,
1319
  UINT*     puArgErr)
1320
{
1321
  OLEFontImpl *this = impl_from_IDispatch(iface);
1322
  HRESULT hr;
1323

1324
  TRACE("%p->(%d,%s,0x%x,0x%x,%p,%p,%p,%p)\n", this, dispIdMember,
1325 1326 1327
    debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult, pExepInfo,
    puArgErr);

1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
  /* validate parameters */

  if (!IsEqualIID(riid, &IID_NULL))
  {
    ERR("riid was %s instead of IID_NULL\n", debugstr_guid(riid));
    return DISP_E_UNKNOWNINTERFACE;
  }

  if (wFlags & DISPATCH_PROPERTYGET)
  {
    if (!pVarResult)
    {
      ERR("null pVarResult not allowed when DISPATCH_PROPERTYGET specified\n");
      return DISP_E_PARAMNOTOPTIONAL;
    }
  }
  else if (wFlags & DISPATCH_PROPERTYPUT)
  {
    if (!pDispParams)
    {
      ERR("null pDispParams not allowed when DISPATCH_PROPERTYPUT specified\n");
      return DISP_E_PARAMNOTOPTIONAL;
    }
    if (pDispParams->cArgs != 1)
    {
1353
      ERR("param count for DISPATCH_PROPERTYPUT was %d instead of 1\n", pDispParams->cArgs);
1354 1355 1356 1357 1358 1359 1360 1361 1362
      return DISP_E_BADPARAMCOUNT;
    }
  }
  else
  {
    ERR("one of DISPATCH_PROPERTYGET or DISPATCH_PROPERTYPUT must be specified\n");
    return DISP_E_MEMBERNOTFOUND;
  }

1363 1364
  switch (dispIdMember) {
  case DISPID_FONT_NAME:
1365
    if (wFlags & DISPATCH_PROPERTYGET) {
1366
      V_VT(pVarResult) = VT_BSTR;
1367
      return IFont_get_Name(&this->IFont_iface, &V_BSTR(pVarResult));
1368 1369 1370 1371 1372 1373 1374 1375
    } else {
      VARIANTARG vararg;

      VariantInit(&vararg);
      hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_BSTR);
      if (FAILED(hr))
        return hr;

1376
      hr = IFont_put_Name(&this->IFont_iface, V_BSTR(&vararg));
1377 1378 1379

      VariantClear(&vararg);
      return hr;
1380 1381 1382
    }
    break;
  case DISPID_FONT_BOLD:
1383 1384
    if (wFlags & DISPATCH_PROPERTYGET) {
      BOOL value;
1385
      hr = IFont_get_Bold(&this->IFont_iface, &value);
1386
      V_VT(pVarResult) = VT_BOOL;
1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
      V_BOOL(pVarResult) = value ? VARIANT_TRUE : VARIANT_FALSE;
      return hr;
    } else {
      VARIANTARG vararg;

      VariantInit(&vararg);
      hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_BOOL);
      if (FAILED(hr))
        return hr;

1397
      hr = IFont_put_Bold(&this->IFont_iface, V_BOOL(&vararg));
1398 1399 1400

      VariantClear(&vararg);
      return hr;
1401 1402 1403
    }
    break;
  case DISPID_FONT_ITALIC:
1404 1405
    if (wFlags & DISPATCH_PROPERTYGET) {
      BOOL value;
1406
      hr = IFont_get_Italic(&this->IFont_iface, &value);
1407
      V_VT(pVarResult) = VT_BOOL;
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
      V_BOOL(pVarResult) = value ? VARIANT_TRUE : VARIANT_FALSE;
      return hr;
    } else {
      VARIANTARG vararg;

      VariantInit(&vararg);
      hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_BOOL);
      if (FAILED(hr))
        return hr;

1418
      hr = IFont_put_Italic(&this->IFont_iface, V_BOOL(&vararg));
1419 1420 1421

      VariantClear(&vararg);
      return hr;
1422 1423 1424
    }
    break;
  case DISPID_FONT_UNDER:
1425 1426
    if (wFlags & DISPATCH_PROPERTYGET) {
      BOOL value;
1427
      hr = IFont_get_Underline(&this->IFont_iface, &value);
1428
      V_VT(pVarResult) = VT_BOOL;
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438
      V_BOOL(pVarResult) = value ? VARIANT_TRUE : VARIANT_FALSE;
      return hr;
    } else {
      VARIANTARG vararg;

      VariantInit(&vararg);
      hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_BOOL);
      if (FAILED(hr))
        return hr;

1439
      hr = IFont_put_Underline(&this->IFont_iface, V_BOOL(&vararg));
1440 1441 1442

      VariantClear(&vararg);
      return hr;
1443 1444 1445
    }
    break;
  case DISPID_FONT_STRIKE:
1446 1447
    if (wFlags & DISPATCH_PROPERTYGET) {
      BOOL value;
1448
      hr = IFont_get_Strikethrough(&this->IFont_iface, &value);
1449
      V_VT(pVarResult) = VT_BOOL;
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
      V_BOOL(pVarResult) = value ? VARIANT_TRUE : VARIANT_FALSE;
      return hr;
    } else {
      VARIANTARG vararg;

      VariantInit(&vararg);
      hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_BOOL);
      if (FAILED(hr))
        return hr;

1460
      hr = IFont_put_Strikethrough(&this->IFont_iface, V_BOOL(&vararg));
1461 1462 1463

      VariantClear(&vararg);
      return hr;
1464 1465 1466
    }
    break;
  case DISPID_FONT_SIZE:
1467
    if (wFlags & DISPATCH_PROPERTYGET) {
1468
      V_VT(pVarResult) = VT_CY;
1469
      return IFont_get_Size(&this->IFont_iface, &V_CY(pVarResult));
1470 1471 1472 1473 1474 1475 1476 1477
    } else {
      VARIANTARG vararg;

      VariantInit(&vararg);
      hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_CY);
      if (FAILED(hr))
        return hr;

1478
      hr = IFont_put_Size(&this->IFont_iface, V_CY(&vararg));
1479 1480 1481

      VariantClear(&vararg);
      return hr;
1482 1483
    }
    break;
1484 1485 1486
  case DISPID_FONT_WEIGHT:
    if (wFlags & DISPATCH_PROPERTYGET) {
      V_VT(pVarResult) = VT_I2;
1487
      return IFont_get_Weight(&this->IFont_iface, &V_I2(pVarResult));
1488 1489 1490 1491 1492 1493 1494 1495
    } else {
      VARIANTARG vararg;

      VariantInit(&vararg);
      hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_I2);
      if (FAILED(hr))
        return hr;

1496
      hr = IFont_put_Weight(&this->IFont_iface, V_I2(&vararg));
1497 1498 1499 1500 1501

      VariantClear(&vararg);
      return hr;
    }
    break;
1502
  case DISPID_FONT_CHARSET:
1503
    if (wFlags & DISPATCH_PROPERTYGET) {
1504
      V_VT(pVarResult) = VT_I2;
1505
      return OLEFontImpl_get_Charset(&this->IFont_iface, &V_I2(pVarResult));
1506 1507 1508 1509 1510 1511 1512 1513
    } else {
      VARIANTARG vararg;

      VariantInit(&vararg);
      hr = VariantChangeTypeEx(&vararg, &pDispParams->rgvarg[0], lcid, 0, VT_I2);
      if (FAILED(hr))
        return hr;

1514
      hr = IFont_put_Charset(&this->IFont_iface, V_I2(&vararg));
1515 1516 1517

      VariantClear(&vararg);
      return hr;
1518 1519
    }
    break;
1520
  default:
1521
    ERR("member not found for dispid 0x%x\n", dispIdMember);
1522
    return DISP_E_MEMBERNOTFOUND;
1523
  }
1524 1525
}

1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
static const IDispatchVtbl OLEFontImpl_IDispatch_VTable =
{
  OLEFontImpl_IDispatch_QueryInterface,
  OLEFontImpl_IDispatch_AddRef,
  OLEFontImpl_IDispatch_Release,
  OLEFontImpl_GetTypeInfoCount,
  OLEFontImpl_GetTypeInfo,
  OLEFontImpl_GetIDsOfNames,
  OLEFontImpl_Invoke
};

1537 1538 1539 1540 1541 1542 1543 1544
/************************************************************************
 * OLEFontImpl_IPersistStream_QueryInterface (IUnknown)
 */
static HRESULT WINAPI OLEFontImpl_IPersistStream_QueryInterface(
  IPersistStream* iface,
  REFIID     riid,
  VOID**     ppvoid)
{
1545
  OLEFontImpl *this = impl_from_IPersistStream(iface);
1546

1547
  return IFont_QueryInterface(&this->IFont_iface, riid, ppvoid);
1548 1549 1550 1551 1552 1553 1554 1555
}

/************************************************************************
 * OLEFontImpl_IPersistStream_Release (IUnknown)
 */
static ULONG WINAPI OLEFontImpl_IPersistStream_Release(
  IPersistStream* iface)
{
1556
  OLEFontImpl *this = impl_from_IPersistStream(iface);
1557

1558
  return IFont_Release(&this->IFont_iface);
1559 1560 1561 1562 1563 1564 1565 1566
}

/************************************************************************
 * OLEFontImpl_IPersistStream_AddRef (IUnknown)
 */
static ULONG WINAPI OLEFontImpl_IPersistStream_AddRef(
  IPersistStream* iface)
{
1567
  OLEFontImpl *this = impl_from_IPersistStream(iface);
1568

1569
  return IFont_AddRef(&this->IFont_iface);
1570 1571 1572 1573 1574 1575
}

/************************************************************************
 * OLEFontImpl_GetClassID (IPersistStream)
 */
static HRESULT WINAPI OLEFontImpl_GetClassID(
1576
  IPersistStream* iface,
1577 1578
  CLSID*                pClassID)
{
1579
  TRACE("(%p,%p)\n",iface,pClassID);
1580 1581 1582
  if (pClassID==0)
    return E_POINTER;

1583
  *pClassID = CLSID_StdFont;
1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_IsDirty (IPersistStream)
 *
 * See Windows documentation for more details on IPersistStream methods.
 */
static HRESULT WINAPI OLEFontImpl_IsDirty(
  IPersistStream*  iface)
{
1596
  TRACE("(%p)\n",iface);
1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624
  return S_OK;
}

/************************************************************************
 * OLEFontImpl_Load (IPersistStream)
 *
 * See Windows documentation for more details on IPersistStream methods.
 *
 * This is the format of the standard font serialization as far as I
 * know
 *
 * Offset   Type   Value           Comment
 * 0x0000   Byte   Unknown         Probably a version number, contains 0x01
 * 0x0001   Short  Charset         Charset value from the FONTDESC structure
 * 0x0003   Byte   Attributes      Flags defined as follows:
 *                                     00000010 - Italic
 *                                     00000100 - Underline
 *                                     00001000 - Strikethrough
 * 0x0004   Short  Weight          Weight value from FONTDESC structure
 * 0x0006   DWORD  size            "Low" portion of the cySize member of the FONTDESC
 *                                 structure/
 * 0x000A   Byte   name length     Length of the font name string (no null character)
 * 0x000B   String name            Name of the font (ASCII, no nul character)
 */
static HRESULT WINAPI OLEFontImpl_Load(
  IPersistStream*  iface,
  IStream*         pLoadStream)
{
1625 1626 1627
  OLEFontImpl *this = impl_from_IPersistStream(iface);
  BYTE  version, attributes, string_size;
  char readBuffer[0x100];
1628
  ULONG cbRead;
1629
  INT len;
1630

1631 1632 1633
  /* Version */
  IStream_Read(pLoadStream, &version, sizeof(BYTE), &cbRead);
  if ((cbRead != sizeof(BYTE)) || (version != 0x01)) return E_FAIL;
1634

1635 1636 1637
  /* Charset */
  IStream_Read(pLoadStream, &this->description.sCharset, sizeof(WORD), &cbRead);
  if (cbRead != sizeof(WORD)) return E_FAIL;
1638

1639 1640 1641
  /* Attributes */
  IStream_Read(pLoadStream, &attributes, sizeof(BYTE), &cbRead);
  if (cbRead != sizeof(BYTE)) return E_FAIL;
1642

1643 1644 1645
  this->description.fItalic        = (attributes & FONTPERSIST_ITALIC) != 0;
  this->description.fStrikethrough = (attributes & FONTPERSIST_STRIKETHROUGH) != 0;
  this->description.fUnderline     = (attributes & FONTPERSIST_UNDERLINE) != 0;
1646

1647 1648 1649
  /* Weight */
  IStream_Read(pLoadStream, &this->description.sWeight, sizeof(WORD), &cbRead);
  if (cbRead != sizeof(WORD)) return E_FAIL;
1650

1651 1652 1653
  /* Size */
  IStream_Read(pLoadStream, &this->description.cySize.s.Lo, sizeof(DWORD), &cbRead);
  if (cbRead != sizeof(DWORD)) return E_FAIL;
1654

1655
  this->description.cySize.s.Hi = 0;
1656

1657 1658 1659
  /* Name */
  IStream_Read(pLoadStream, &string_size, sizeof(BYTE), &cbRead);
  if (cbRead != sizeof(BYTE)) return E_FAIL;
1660

1661 1662
  IStream_Read(pLoadStream, readBuffer, string_size, &cbRead);
  if (cbRead != string_size) return E_FAIL;
1663

1664
  HeapFree(GetProcessHeap(), 0, this->description.lpstrName);
1665

1666
  len = MultiByteToWideChar( CP_ACP, 0, readBuffer, string_size, NULL, 0 );
1667
  this->description.lpstrName = HeapAlloc( GetProcessHeap(), 0, (len+1) * sizeof(WCHAR) );
1668
  MultiByteToWideChar( CP_ACP, 0, readBuffer, string_size, this->description.lpstrName, len );
1669
  this->description.lpstrName[len] = 0;
1670

1671
  /* Ensure use of this font causes a new one to be created */
1672
  dec_int_ref(this->gdiFont);
1673
  this->gdiFont = 0;
1674
  this->dirty = TRUE;
1675

1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686
  return S_OK;
}

/************************************************************************
 * OLEFontImpl_Save (IPersistStream)
 */
static HRESULT WINAPI OLEFontImpl_Save(
  IPersistStream*  iface,
  IStream*         pOutStream,
  BOOL             fClearDirty)
{
1687 1688 1689
  OLEFontImpl *this = impl_from_IPersistStream(iface);
  BYTE  attributes, string_size;
  const BYTE version = 0x01;
1690
  char* writeBuffer = NULL;
1691
  ULONG written;
1692

1693
  TRACE("(%p)->(%p %d)\n", this, pOutStream, fClearDirty);
1694

1695 1696 1697
  /* Version */
  IStream_Write(pOutStream, &version, sizeof(BYTE), &written);
  if (written != sizeof(BYTE)) return E_FAIL;
1698

1699 1700 1701
  /* Charset */
  IStream_Write(pOutStream, &this->description.sCharset, sizeof(WORD), &written);
  if (written != sizeof(WORD)) return E_FAIL;
1702

1703 1704
  /* Attributes */
  attributes = 0;
1705 1706

  if (this->description.fItalic)
1707
    attributes |= FONTPERSIST_ITALIC;
1708

1709
  if (this->description.fStrikethrough)
1710
    attributes |= FONTPERSIST_STRIKETHROUGH;
1711

1712
  if (this->description.fUnderline)
1713
    attributes |= FONTPERSIST_UNDERLINE;
1714

1715 1716
  IStream_Write(pOutStream, &attributes, sizeof(BYTE), &written);
  if (written != sizeof(BYTE)) return E_FAIL;
1717

1718 1719 1720
  /* Weight */
  IStream_Write(pOutStream, &this->description.sWeight, sizeof(WORD), &written);
  if (written != sizeof(WORD)) return E_FAIL;
1721

1722 1723 1724
  /* Size */
  IStream_Write(pOutStream, &this->description.cySize.s.Lo, sizeof(DWORD), &written);
  if (written != sizeof(DWORD)) return E_FAIL;
1725

1726 1727 1728
  /* FontName */
  if (this->description.lpstrName)
    string_size = WideCharToMultiByte( CP_ACP, 0, this->description.lpstrName,
1729
                                       lstrlenW(this->description.lpstrName), NULL, 0, NULL, NULL );
1730
  else
1731
    string_size = 0;
1732

1733 1734
  IStream_Write(pOutStream, &string_size, sizeof(BYTE), &written);
  if (written != sizeof(BYTE)) return E_FAIL;
1735

1736
  if (string_size)
1737
  {
1738
      if (!(writeBuffer = HeapAlloc( GetProcessHeap(), 0, string_size ))) return E_OUTOFMEMORY;
1739
      WideCharToMultiByte( CP_ACP, 0, this->description.lpstrName,
1740
                           lstrlenW(this->description.lpstrName),
1741
                           writeBuffer, string_size, NULL, NULL );
1742

1743 1744
      IStream_Write(pOutStream, writeBuffer, string_size, &written);
      HeapFree(GetProcessHeap(), 0, writeBuffer);
1745

1746
      if (written != string_size) return E_FAIL;
1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758
  }

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_GetSizeMax (IPersistStream)
 */
static HRESULT WINAPI OLEFontImpl_GetSizeMax(
  IPersistStream*  iface,
  ULARGE_INTEGER*  pcbSize)
{
1759
  OLEFontImpl *this = impl_from_IPersistStream(iface);
1760 1761 1762 1763

  if (pcbSize==NULL)
    return E_POINTER;

1764 1765
  pcbSize->u.HighPart = 0;
  pcbSize->u.LowPart = 0;
1766

1767 1768 1769 1770 1771 1772
  pcbSize->u.LowPart += sizeof(BYTE);  /* Version */
  pcbSize->u.LowPart += sizeof(WORD);  /* Lang code */
  pcbSize->u.LowPart += sizeof(BYTE);  /* Flags */
  pcbSize->u.LowPart += sizeof(WORD);  /* Weight */
  pcbSize->u.LowPart += sizeof(DWORD); /* Size */
  pcbSize->u.LowPart += sizeof(BYTE);  /* StrLength */
1773 1774

  if (this->description.lpstrName!=0)
1775
      pcbSize->u.LowPart += WideCharToMultiByte( CP_ACP, 0, this->description.lpstrName,
1776
                                                 lstrlenW(this->description.lpstrName),
1777
                                                 NULL, 0, NULL, NULL );
1778 1779 1780

  return S_OK;
}
1781

1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793
static const IPersistStreamVtbl OLEFontImpl_IPersistStream_VTable =
{
  OLEFontImpl_IPersistStream_QueryInterface,
  OLEFontImpl_IPersistStream_AddRef,
  OLEFontImpl_IPersistStream_Release,
  OLEFontImpl_GetClassID,
  OLEFontImpl_IsDirty,
  OLEFontImpl_Load,
  OLEFontImpl_Save,
  OLEFontImpl_GetSizeMax
};

1794 1795 1796 1797 1798 1799 1800 1801
/************************************************************************
 * OLEFontImpl_IConnectionPointContainer_QueryInterface (IUnknown)
 */
static HRESULT WINAPI OLEFontImpl_IConnectionPointContainer_QueryInterface(
  IConnectionPointContainer* iface,
  REFIID     riid,
  VOID**     ppvoid)
{
1802
  OLEFontImpl *this = impl_from_IConnectionPointContainer(iface);
1803

1804
  return IFont_QueryInterface(&this->IFont_iface, riid, ppvoid);
1805 1806 1807 1808 1809 1810 1811 1812
}

/************************************************************************
 * OLEFontImpl_IConnectionPointContainer_Release (IUnknown)
 */
static ULONG WINAPI OLEFontImpl_IConnectionPointContainer_Release(
  IConnectionPointContainer* iface)
{
1813
  OLEFontImpl *this = impl_from_IConnectionPointContainer(iface);
1814

1815
  return IFont_Release(&this->IFont_iface);
1816 1817 1818 1819 1820 1821 1822 1823
}

/************************************************************************
 * OLEFontImpl_IConnectionPointContainer_AddRef (IUnknown)
 */
static ULONG WINAPI OLEFontImpl_IConnectionPointContainer_AddRef(
  IConnectionPointContainer* iface)
{
1824
  OLEFontImpl *this = impl_from_IConnectionPointContainer(iface);
1825

1826
  return IFont_AddRef(&this->IFont_iface);
1827 1828 1829 1830 1831 1832 1833 1834 1835
}

/************************************************************************
 * OLEFontImpl_EnumConnectionPoints (IConnectionPointContainer)
 */
static HRESULT WINAPI OLEFontImpl_EnumConnectionPoints(
  IConnectionPointContainer* iface,
  IEnumConnectionPoints **ppEnum)
{
1836
  OLEFontImpl *this = impl_from_IConnectionPointContainer(iface);
1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849

  FIXME("(%p)->(%p): stub\n", this, ppEnum);
  return E_NOTIMPL;
}

/************************************************************************
 * OLEFontImpl_FindConnectionPoint (IConnectionPointContainer)
 */
static HRESULT WINAPI OLEFontImpl_FindConnectionPoint(
   IConnectionPointContainer* iface,
   REFIID riid,
   IConnectionPoint **ppCp)
{
1850
  OLEFontImpl *this = impl_from_IConnectionPointContainer(iface);
1851
  TRACE("(%p)->(%s, %p)\n", this, debugstr_guid(riid), ppCp);
1852

1853
  if(IsEqualIID(riid, &IID_IPropertyNotifySink)) {
1854 1855
    return IConnectionPoint_QueryInterface(this->pPropertyNotifyCP, &IID_IConnectionPoint,
                                           (void**)ppCp);
1856
  } else if(IsEqualIID(riid, &IID_IFontEventsDisp)) {
1857 1858
    return IConnectionPoint_QueryInterface(this->pFontEventsCP, &IID_IConnectionPoint,
                                           (void**)ppCp);
1859
  } else {
1860 1861
    FIXME("no connection point for %s\n", debugstr_guid(riid));
    return CONNECT_E_NOCONNECTION;
1862 1863 1864
  }
}

1865 1866 1867 1868 1869 1870 1871 1872 1873 1874
static const IConnectionPointContainerVtbl
     OLEFontImpl_IConnectionPointContainer_VTable =
{
  OLEFontImpl_IConnectionPointContainer_QueryInterface,
  OLEFontImpl_IConnectionPointContainer_AddRef,
  OLEFontImpl_IConnectionPointContainer_Release,
  OLEFontImpl_EnumConnectionPoints,
  OLEFontImpl_FindConnectionPoint
};

1875 1876 1877 1878 1879 1880
/************************************************************************
 * OLEFontImpl implementation of IPersistPropertyBag.
 */
static HRESULT WINAPI OLEFontImpl_IPersistPropertyBag_QueryInterface(
   IPersistPropertyBag *iface, REFIID riid, LPVOID *ppvObj
) {
1881
  OLEFontImpl *this = impl_from_IPersistPropertyBag(iface);
1882
  return IFont_QueryInterface(&this->IFont_iface,riid,ppvObj);
1883 1884 1885 1886 1887
}

static ULONG WINAPI OLEFontImpl_IPersistPropertyBag_AddRef(
   IPersistPropertyBag *iface
) {
1888
  OLEFontImpl *this = impl_from_IPersistPropertyBag(iface);
1889
  return IFont_AddRef(&this->IFont_iface);
1890 1891 1892 1893 1894
}

static ULONG WINAPI OLEFontImpl_IPersistPropertyBag_Release(
   IPersistPropertyBag *iface
) {
1895
  OLEFontImpl *this = impl_from_IPersistPropertyBag(iface);
1896
  return IFont_Release(&this->IFont_iface);
1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915
}

static HRESULT WINAPI OLEFontImpl_IPersistPropertyBag_GetClassID(
   IPersistPropertyBag *iface, CLSID *classid
) {
  FIXME("(%p,%p), stub!\n", iface, classid);
  return E_FAIL;
}

static HRESULT WINAPI OLEFontImpl_IPersistPropertyBag_InitNew(
   IPersistPropertyBag *iface
) {
  FIXME("(%p), stub!\n", iface);
  return S_OK;
}

static HRESULT WINAPI OLEFontImpl_IPersistPropertyBag_Load(
   IPersistPropertyBag *iface, IPropertyBag* pPropBag, IErrorLog* pErrorLog
) {
1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931
/* (from Visual Basic 6 property bag)
         Name            =   "MS Sans Serif"
         Size            =   13.8
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
*/
    static const WCHAR sAttrName[] = {'N','a','m','e',0};
    static const WCHAR sAttrSize[] = {'S','i','z','e',0};
    static const WCHAR sAttrCharset[] = {'C','h','a','r','s','e','t',0};
    static const WCHAR sAttrWeight[] = {'W','e','i','g','h','t',0};
    static const WCHAR sAttrUnderline[] = {'U','n','d','e','r','l','i','n','e',0};
    static const WCHAR sAttrItalic[] = {'I','t','a','l','i','c',0};
    static const WCHAR sAttrStrikethrough[] = {'S','t','r','i','k','e','t','h','r','o','u','g','h',0};
1932
    OLEFontImpl *this = impl_from_IPersistPropertyBag(iface);
1933 1934
    VARIANT value;
    HRESULT iRes;
1935

1936
    VariantInit(&value);
1937

1938 1939 1940 1941
    iRes = IPropertyBag_Read(pPropBag, sAttrName, &value, pErrorLog);
    if (iRes == S_OK)
    {
        iRes = VariantChangeType(&value, &value, 0, VT_BSTR);
1942
        if (iRes == S_OK)
1943
            iRes = IFont_put_Name(&this->IFont_iface, V_BSTR(&value));
1944
    }
1945 1946 1947 1948
    else if (iRes == E_INVALIDARG)
        iRes = S_OK;

    VariantClear(&value);
1949 1950

    if (iRes == S_OK) {
1951
        iRes = IPropertyBag_Read(pPropBag, sAttrSize, &value, pErrorLog);
1952 1953
        if (iRes == S_OK)
        {
1954
            iRes = VariantChangeType(&value, &value, 0, VT_CY);
1955
            if (iRes == S_OK)
1956
                iRes = IFont_put_Size(&this->IFont_iface, V_CY(&value));
1957 1958 1959
        }
        else if (iRes == E_INVALIDARG)
            iRes = S_OK;
1960 1961

        VariantClear(&value);
1962 1963 1964
    }

    if (iRes == S_OK) {
1965
        iRes = IPropertyBag_Read(pPropBag, sAttrCharset, &value, pErrorLog);
1966 1967
        if (iRes == S_OK)
        {
1968
            iRes = VariantChangeType(&value, &value, 0, VT_I2);
1969
            if (iRes == S_OK)
1970
                iRes = IFont_put_Charset(&this->IFont_iface, V_I2(&value));
1971 1972 1973
        }
        else if (iRes == E_INVALIDARG)
            iRes = S_OK;
1974 1975

        VariantClear(&value);
1976 1977 1978
    }

    if (iRes == S_OK) {
1979
        iRes = IPropertyBag_Read(pPropBag, sAttrWeight, &value, pErrorLog);
1980 1981
        if (iRes == S_OK)
        {
1982
            iRes = VariantChangeType(&value, &value, 0, VT_I2);
1983
            if (iRes == S_OK)
1984
                iRes = IFont_put_Weight(&this->IFont_iface, V_I2(&value));
1985 1986 1987 1988
        }
        else if (iRes == E_INVALIDARG)
            iRes = S_OK;

1989
        VariantClear(&value);
1990 1991 1992
    }

    if (iRes == S_OK) {
1993
        iRes = IPropertyBag_Read(pPropBag, sAttrUnderline, &value, pErrorLog);
1994 1995
        if (iRes == S_OK)
        {
1996
            iRes = VariantChangeType(&value, &value, 0, VT_BOOL);
1997
            if (iRes == S_OK)
1998
                iRes = IFont_put_Underline(&this->IFont_iface, V_BOOL(&value));
1999 2000 2001
        }
        else if (iRes == E_INVALIDARG)
            iRes = S_OK;
2002 2003

        VariantClear(&value);
2004 2005 2006
    }

    if (iRes == S_OK) {
2007
        iRes = IPropertyBag_Read(pPropBag, sAttrItalic, &value, pErrorLog);
2008 2009
        if (iRes == S_OK)
        {
2010
            iRes = VariantChangeType(&value, &value, 0, VT_BOOL);
2011
            if (iRes == S_OK)
2012
                iRes = IFont_put_Italic(&this->IFont_iface, V_BOOL(&value));
2013 2014 2015
        }
        else if (iRes == E_INVALIDARG)
            iRes = S_OK;
2016 2017

        VariantClear(&value);
2018 2019 2020
    }

    if (iRes == S_OK) {
2021
        iRes = IPropertyBag_Read(pPropBag, sAttrStrikethrough, &value, pErrorLog);
2022 2023
        if (iRes == S_OK)
        {
2024
            iRes = VariantChangeType(&value, &value, 0, VT_BOOL);
2025
            if (iRes == S_OK)
2026
                IFont_put_Strikethrough(&this->IFont_iface, V_BOOL(&value));
2027 2028 2029
        }
        else if (iRes == E_INVALIDARG)
            iRes = S_OK;
2030 2031

        VariantClear(&value);
2032 2033 2034
    }

    if (FAILED(iRes))
2035
        WARN("-- 0x%08x\n", iRes);
2036
    return iRes;
2037 2038 2039 2040 2041 2042 2043 2044 2045 2046
}

static HRESULT WINAPI OLEFontImpl_IPersistPropertyBag_Save(
   IPersistPropertyBag *iface, IPropertyBag* pPropBag, BOOL fClearDirty,
   BOOL fSaveAllProperties
) {
  FIXME("(%p,%p,%d,%d), stub!\n", iface, pPropBag, fClearDirty, fSaveAllProperties);
  return E_FAIL;
}

2047
static const IPersistPropertyBagVtbl OLEFontImpl_IPersistPropertyBag_VTable = 
2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064
{
  OLEFontImpl_IPersistPropertyBag_QueryInterface,
  OLEFontImpl_IPersistPropertyBag_AddRef,
  OLEFontImpl_IPersistPropertyBag_Release,

  OLEFontImpl_IPersistPropertyBag_GetClassID,
  OLEFontImpl_IPersistPropertyBag_InitNew,
  OLEFontImpl_IPersistPropertyBag_Load,
  OLEFontImpl_IPersistPropertyBag_Save
};

/************************************************************************
 * OLEFontImpl implementation of IPersistStreamInit.
 */
static HRESULT WINAPI OLEFontImpl_IPersistStreamInit_QueryInterface(
   IPersistStreamInit *iface, REFIID riid, LPVOID *ppvObj
) {
2065
  OLEFontImpl *this = impl_from_IPersistStreamInit(iface);
2066
  return IFont_QueryInterface(&this->IFont_iface,riid,ppvObj);
2067 2068 2069 2070 2071
}

static ULONG WINAPI OLEFontImpl_IPersistStreamInit_AddRef(
   IPersistStreamInit *iface
) {
2072
  OLEFontImpl *this = impl_from_IPersistStreamInit(iface);
2073
  return IFont_AddRef(&this->IFont_iface);
2074 2075 2076 2077 2078
}

static ULONG WINAPI OLEFontImpl_IPersistStreamInit_Release(
   IPersistStreamInit *iface
) {
2079
  OLEFontImpl *this = impl_from_IPersistStreamInit(iface);
2080
  return IFont_Release(&this->IFont_iface);
2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124
}

static HRESULT WINAPI OLEFontImpl_IPersistStreamInit_GetClassID(
   IPersistStreamInit *iface, CLSID *classid
) {
  FIXME("(%p,%p), stub!\n", iface, classid);
  return E_FAIL;
}

static HRESULT WINAPI OLEFontImpl_IPersistStreamInit_IsDirty(
   IPersistStreamInit *iface
) {
  FIXME("(%p), stub!\n", iface);
  return E_FAIL;
}

static HRESULT WINAPI OLEFontImpl_IPersistStreamInit_Load(
   IPersistStreamInit *iface, LPSTREAM pStm
) {
  FIXME("(%p,%p), stub!\n", iface, pStm);
  return E_FAIL;
}

static HRESULT WINAPI OLEFontImpl_IPersistStreamInit_Save(
   IPersistStreamInit *iface, LPSTREAM pStm, BOOL fClearDirty
) {
  FIXME("(%p,%p,%d), stub!\n", iface, pStm, fClearDirty);
  return E_FAIL;
}

static HRESULT WINAPI OLEFontImpl_IPersistStreamInit_GetSizeMax(
   IPersistStreamInit *iface, ULARGE_INTEGER *pcbSize
) {
  FIXME("(%p,%p), stub!\n", iface, pcbSize);
  return E_FAIL;
}

static HRESULT WINAPI OLEFontImpl_IPersistStreamInit_InitNew(
   IPersistStreamInit *iface
) {
  FIXME("(%p), stub!\n", iface);
  return S_OK;
}

2125
static const IPersistStreamInitVtbl OLEFontImpl_IPersistStreamInit_VTable = 
2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138
{
  OLEFontImpl_IPersistStreamInit_QueryInterface,
  OLEFontImpl_IPersistStreamInit_AddRef,
  OLEFontImpl_IPersistStreamInit_Release,

  OLEFontImpl_IPersistStreamInit_GetClassID,
  OLEFontImpl_IPersistStreamInit_IsDirty,
  OLEFontImpl_IPersistStreamInit_Load,
  OLEFontImpl_IPersistStreamInit_Save,
  OLEFontImpl_IPersistStreamInit_GetSizeMax,
  OLEFontImpl_IPersistStreamInit_InitNew
};

2139 2140 2141 2142 2143 2144 2145 2146 2147
/************************************************************************
 * OLEFontImpl_Construct
 *
 * This method will construct a new instance of the OLEFontImpl
 * class.
 *
 * The caller of this method must release the object when it's
 * done with it.
 */
2148
static OLEFontImpl* OLEFontImpl_Construct(const FONTDESC *fontDesc)
2149
{
2150
  OLEFontImpl* newObject;
2151 2152 2153 2154 2155 2156

  newObject = HeapAlloc(GetProcessHeap(), 0, sizeof(OLEFontImpl));

  if (newObject==0)
    return newObject;

2157 2158 2159 2160 2161 2162
  newObject->IFont_iface.lpVtbl = &OLEFontImpl_VTable;
  newObject->IDispatch_iface.lpVtbl = &OLEFontImpl_IDispatch_VTable;
  newObject->IPersistStream_iface.lpVtbl = &OLEFontImpl_IPersistStream_VTable;
  newObject->IConnectionPointContainer_iface.lpVtbl = &OLEFontImpl_IConnectionPointContainer_VTable;
  newObject->IPersistPropertyBag_iface.lpVtbl = &OLEFontImpl_IPersistPropertyBag_VTable;
  newObject->IPersistStreamInit_iface.lpVtbl = &OLEFontImpl_IPersistStreamInit_VTable;
2163 2164 2165 2166

  newObject->ref = 1;

  newObject->description.cbSizeofstruct = sizeof(FONTDESC);
2167
  newObject->description.lpstrName      = strdupW(fontDesc->lpstrName);
2168 2169 2170 2171 2172 2173 2174 2175
  newObject->description.cySize         = fontDesc->cySize;
  newObject->description.sWeight        = fontDesc->sWeight;
  newObject->description.sCharset       = fontDesc->sCharset;
  newObject->description.fItalic        = fontDesc->fItalic;
  newObject->description.fUnderline     = fontDesc->fUnderline;
  newObject->description.fStrikethrough = fontDesc->fStrikethrough;

  newObject->gdiFont  = 0;
2176
  newObject->dirty = TRUE;
2177
  newObject->cyLogical  = GetDeviceCaps(get_dc(), LOGPIXELSY);
2178 2179 2180 2181
  newObject->cyHimetric = 2540L;
  newObject->pPropertyNotifyCP = NULL;
  newObject->pFontEventsCP = NULL;

2182 2183
  CreateConnectionPoint((IUnknown*)&newObject->IFont_iface, &IID_IPropertyNotifySink, &newObject->pPropertyNotifyCP);
  CreateConnectionPoint((IUnknown*)&newObject->IFont_iface, &IID_IFontEventsDisp, &newObject->pFontEventsCP);
2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217

  if (!newObject->pPropertyNotifyCP || !newObject->pFontEventsCP)
  {
    OLEFontImpl_Destroy(newObject);
    return NULL;
  }

  InterlockedIncrement(&ifont_cnt);

  TRACE("returning %p\n", newObject);
  return newObject;
}

/************************************************************************
 * OLEFontImpl_Destroy
 *
 * This method is called by the Release method when the reference
 * count goes down to 0. It will free all resources used by
 * this object.
 */
static void OLEFontImpl_Destroy(OLEFontImpl* fontDesc)
{
  TRACE("(%p)\n", fontDesc);

  HeapFree(GetProcessHeap(), 0, fontDesc->description.lpstrName);

  if (fontDesc->pPropertyNotifyCP)
      IConnectionPoint_Release(fontDesc->pPropertyNotifyCP);
  if (fontDesc->pFontEventsCP)
      IConnectionPoint_Release(fontDesc->pFontEventsCP);

  HeapFree(GetProcessHeap(), 0, fontDesc);
}

2218 2219 2220 2221 2222 2223
/*******************************************************************************
 * StdFont ClassFactory
 */
typedef struct
{
    /* IUnknown fields */
2224 2225
    IClassFactory IClassFactory_iface;
    LONG          ref;
2226 2227
} IClassFactoryImpl;

2228 2229 2230 2231 2232
static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
{
        return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
}

2233 2234 2235 2236 2237 2238 2239
static HRESULT WINAPI SFCF_QueryInterface(IClassFactory *iface, REFIID riid, void **obj)
{
    IClassFactoryImpl *This = impl_from_IClassFactory(iface);

    TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), obj);

    *obj = NULL;
2240

2241 2242 2243 2244 2245 2246 2247 2248
    if (IsEqualIID(&IID_IClassFactory, riid) || IsEqualIID(&IID_IUnknown, riid))
    {
        *obj = iface;
        IClassFactory_AddRef(iface);
        return S_OK;
    }

    return E_NOINTERFACE;
2249 2250 2251 2252
}

static ULONG WINAPI
SFCF_AddRef(LPCLASSFACTORY iface) {
2253
	IClassFactoryImpl *This = impl_from_IClassFactory(iface);
2254
	return InterlockedIncrement(&This->ref);
2255 2256 2257
}

static ULONG WINAPI SFCF_Release(LPCLASSFACTORY iface) {
2258
	IClassFactoryImpl *This = impl_from_IClassFactory(iface);
2259
	/* static class, won't be  freed */
2260
	return InterlockedDecrement(&This->ref);
2261 2262 2263 2264 2265
}

static HRESULT WINAPI SFCF_CreateInstance(
	LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
) {
2266
	return OleCreateFontIndirect(NULL,riid,ppobj);
2267 2268 2269 2270

}

static HRESULT WINAPI SFCF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
2271
	IClassFactoryImpl *This = impl_from_IClassFactory(iface);
2272 2273 2274 2275
	FIXME("(%p)->(%d),stub!\n",This,dolock);
	return S_OK;
}

2276
static const IClassFactoryVtbl SFCF_Vtbl = {
2277 2278 2279 2280 2281 2282
	SFCF_QueryInterface,
	SFCF_AddRef,
	SFCF_Release,
	SFCF_CreateInstance,
	SFCF_LockServer
};
2283
static IClassFactoryImpl STDFONT_CF = {{&SFCF_Vtbl}, 1 };
2284

2285
void _get_STDFONT_CF(LPVOID *ppv) { *ppv = &STDFONT_CF; }