olefont.c 67.1 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/unicode.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 55 56
 * Declaration of constants used when serializing the font object.
 */
#define FONTPERSIST_ITALIC        0x02
#define FONTPERSIST_UNDERLINE     0x04
#define FONTPERSIST_STRIKETHROUGH 0x08

/***********************************************************************
 * Declaration of the implementation class for the IFont interface
57 58 59 60 61 62
 */
typedef struct OLEFontImpl OLEFontImpl;

struct OLEFontImpl
{
  /*
63
   * This class supports many interfaces. IUnknown, IFont,
64 65 66
   * 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.
67
   */
68 69 70 71 72 73
  const IFontVtbl*                     lpVtbl;
  const IDispatchVtbl*                 lpvtblIDispatch;
  const IPersistStreamVtbl*            lpvtblIPersistStream;
  const IConnectionPointContainerVtbl* lpvtblIConnectionPointContainer;
  const IPersistPropertyBagVtbl*       lpvtblIPersistPropertyBag;
  const IPersistStreamInitVtbl*        lpvtblIPersistStreamInit;
74 75 76
  /*
   * Reference count for that instance of the class.
   */
77
  LONG ref;
78 79 80 81 82

  /*
   * This structure contains the description of the class.
   */
  FONTDESC description;
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

  /*
   * Contain the font associated with this object.
   */
  HFONT gdiFont;

  /*
   * Font lock count.
   */
  DWORD fontLock;

  /*
   * Size ratio
   */
  long cyLogical;
  long cyHimetric;
99

100 101
  IConnectionPoint *pPropertyNotifyCP;
  IConnectionPoint *pFontEventsCP;
102 103 104
};

/*
105
 * Here, I define utility macros to help with the casting of the
106
 * "this" parameter.
107
 * There is a version to accommodate all of the VTables implemented
108
 * by this object.
109
 */
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

static inline OLEFontImpl *impl_from_IDispatch( IDispatch *iface )
{
    return (OLEFontImpl *)((char*)iface - FIELD_OFFSET(OLEFontImpl, lpvtblIDispatch));
}

static inline OLEFontImpl *impl_from_IPersistStream( IPersistStream *iface )
{
    return (OLEFontImpl *)((char*)iface - FIELD_OFFSET(OLEFontImpl, lpvtblIPersistStream));
}

static inline OLEFontImpl *impl_from_IConnectionPointContainer( IConnectionPointContainer *iface )
{
    return (OLEFontImpl *)((char*)iface - FIELD_OFFSET(OLEFontImpl, lpvtblIConnectionPointContainer));
}

static inline OLEFontImpl *impl_from_IPersistPropertyBag( IPersistPropertyBag *iface )
{
    return (OLEFontImpl *)((char*)iface - FIELD_OFFSET(OLEFontImpl, lpvtblIPersistPropertyBag));
}

static inline OLEFontImpl *impl_from_IPersistStreamInit( IPersistStreamInit *iface )
{
    return (OLEFontImpl *)((char*)iface - FIELD_OFFSET(OLEFontImpl, lpvtblIPersistStreamInit));
}
135

136 137 138 139 140 141 142

/***********************************************************************
 * Prototypes for the implementation functions for the IFont
 * interface
 */
static OLEFontImpl* OLEFontImpl_Construct(LPFONTDESC fontDesc);
static void         OLEFontImpl_Destroy(OLEFontImpl* fontDesc);
Marcus Meissner's avatar
Marcus Meissner committed
143 144 145
static HRESULT      WINAPI OLEFontImpl_QueryInterface(IFont* iface, REFIID riid, VOID** ppvoid);
static ULONG        WINAPI OLEFontImpl_AddRef(IFont* iface);
static ULONG        WINAPI OLEFontImpl_Release(IFont* iface);
146 147
static HRESULT      WINAPI OLEFontImpl_get_Name(IFont* iface, BSTR* pname);
static HRESULT      WINAPI OLEFontImpl_put_Name(IFont* iface, BSTR name);
Marcus Meissner's avatar
Marcus Meissner committed
148 149
static HRESULT      WINAPI OLEFontImpl_get_Size(IFont* iface, CY* psize);
static HRESULT      WINAPI OLEFontImpl_put_Size(IFont* iface, CY size);
150 151 152 153 154 155 156 157
static HRESULT      WINAPI OLEFontImpl_get_Bold(IFont* iface, BOOL* pbold);
static HRESULT      WINAPI OLEFontImpl_put_Bold(IFont* iface, BOOL bold);
static HRESULT      WINAPI OLEFontImpl_get_Italic(IFont* iface, BOOL* pitalic);
static HRESULT      WINAPI OLEFontImpl_put_Italic(IFont* iface, BOOL italic);
static HRESULT      WINAPI OLEFontImpl_get_Underline(IFont* iface, BOOL* punderline);
static HRESULT      WINAPI OLEFontImpl_put_Underline(IFont* iface, BOOL underline);
static HRESULT      WINAPI OLEFontImpl_get_Strikethrough(IFont* iface, BOOL* pstrikethrough);
static HRESULT      WINAPI OLEFontImpl_put_Strikethrough(IFont* iface, BOOL strikethrough);
Marcus Meissner's avatar
Marcus Meissner committed
158 159 160 161
static HRESULT      WINAPI OLEFontImpl_get_Weight(IFont* iface, short* pweight);
static HRESULT      WINAPI OLEFontImpl_put_Weight(IFont* iface, short weight);
static HRESULT      WINAPI OLEFontImpl_get_Charset(IFont* iface, short* pcharset);
static HRESULT      WINAPI OLEFontImpl_put_Charset(IFont* iface, short charset);
162
static HRESULT      WINAPI OLEFontImpl_get_hFont(IFont* iface, HFONT* phfont);
Marcus Meissner's avatar
Marcus Meissner committed
163 164
static HRESULT      WINAPI OLEFontImpl_Clone(IFont* iface, IFont** ppfont);
static HRESULT      WINAPI OLEFontImpl_IsEqual(IFont* iface, IFont* pFontOther);
165
static HRESULT      WINAPI OLEFontImpl_SetRatio(IFont* iface, LONG cyLogical, LONG cyHimetric);
Marcus Meissner's avatar
Marcus Meissner committed
166
static HRESULT      WINAPI OLEFontImpl_QueryTextMetrics(IFont* iface, TEXTMETRICOLE* ptm);
167 168 169
static HRESULT      WINAPI OLEFontImpl_AddRefHfont(IFont* iface, HFONT hfont);
static HRESULT      WINAPI OLEFontImpl_ReleaseHfont(IFont* iface, HFONT hfont);
static HRESULT      WINAPI OLEFontImpl_SetHdc(IFont* iface, HDC hdc);
170 171 172 173 174

/***********************************************************************
 * Prototypes for the implementation functions for the IDispatch
 * interface
 */
175 176
static HRESULT WINAPI OLEFontImpl_IDispatch_QueryInterface(IDispatch* iface,
						    REFIID     riid,
177
						    VOID**     ppvoid);
Marcus Meissner's avatar
Marcus Meissner committed
178 179
static ULONG   WINAPI OLEFontImpl_IDispatch_AddRef(IDispatch* iface);
static ULONG   WINAPI OLEFontImpl_IDispatch_Release(IDispatch* iface);
180
static HRESULT WINAPI OLEFontImpl_GetTypeInfoCount(IDispatch*    iface,
Marcus Meissner's avatar
Marcus Meissner committed
181
					           unsigned int* pctinfo);
182
static HRESULT WINAPI OLEFontImpl_GetTypeInfo(IDispatch*  iface,
183
				       	      UINT      iTInfo,
184
				              LCID        lcid,
Marcus Meissner's avatar
Marcus Meissner committed
185 186
				              ITypeInfo** ppTInfo);
static HRESULT WINAPI OLEFontImpl_GetIDsOfNames(IDispatch*  iface,
187 188 189
					        REFIID      riid,
					        LPOLESTR* rgszNames,
					        UINT      cNames,
Marcus Meissner's avatar
Marcus Meissner committed
190 191 192
					        LCID        lcid,
					        DISPID*     rgDispId);
static HRESULT WINAPI OLEFontImpl_Invoke(IDispatch*  iface,
193 194 195
				         DISPID      dispIdMember,
				         REFIID      riid,
				         LCID        lcid,
Marcus Meissner's avatar
Marcus Meissner committed
196 197
				         WORD        wFlags,
				         DISPPARAMS* pDispParams,
198
				         VARIANT*    pVarResult,
Marcus Meissner's avatar
Marcus Meissner committed
199
				         EXCEPINFO*  pExepInfo,
200
				         UINT*     puArgErr);
201

202 203 204 205
/***********************************************************************
 * Prototypes for the implementation functions for the IPersistStream
 * interface
 */
206 207
static HRESULT WINAPI OLEFontImpl_IPersistStream_QueryInterface(IPersistStream* iface,
						    REFIID     riid,
208 209 210
						    VOID**     ppvoid);
static ULONG   WINAPI OLEFontImpl_IPersistStream_AddRef(IPersistStream* iface);
static ULONG   WINAPI OLEFontImpl_IPersistStream_Release(IPersistStream* iface);
211
static HRESULT WINAPI OLEFontImpl_GetClassID(IPersistStream* iface,
212 213 214 215 216 217
					     CLSID*                pClassID);
static HRESULT WINAPI OLEFontImpl_IsDirty(IPersistStream*  iface);
static HRESULT WINAPI OLEFontImpl_Load(IPersistStream*  iface,
				       IStream*         pLoadStream);
static HRESULT WINAPI OLEFontImpl_Save(IPersistStream*  iface,
				       IStream*         pOutStream,
218
				       BOOL             fClearDirty);
219
static HRESULT WINAPI OLEFontImpl_GetSizeMax(IPersistStream*  iface,
220
					     ULARGE_INTEGER*  pcbSize);
221

222 223 224 225 226
/***********************************************************************
 * Prototypes for the implementation functions for the
 * IConnectionPointContainer interface
 */
static HRESULT WINAPI OLEFontImpl_IConnectionPointContainer_QueryInterface(
227 228
					    IConnectionPointContainer* iface,
					    REFIID     riid,
229 230 231 232 233 234 235 236 237 238 239 240 241
					    VOID**     ppvoid);
static ULONG   WINAPI OLEFontImpl_IConnectionPointContainer_AddRef(
					    IConnectionPointContainer* iface);
static ULONG   WINAPI OLEFontImpl_IConnectionPointContainer_Release(
					    IConnectionPointContainer* iface);
static HRESULT WINAPI OLEFontImpl_EnumConnectionPoints(
					    IConnectionPointContainer* iface,
					    IEnumConnectionPoints **ppEnum);
static HRESULT WINAPI OLEFontImpl_FindConnectionPoint(
					    IConnectionPointContainer* iface,
					    REFIID riid,
					    IConnectionPoint **ppCp);

242 243 244
/*
 * Virtual function tables for the OLEFontImpl class.
 */
245
static const IFontVtbl OLEFontImpl_VTable =
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
{
  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,
267
  OLEFontImpl_Clone,
268 269 270 271 272 273 274 275
  OLEFontImpl_IsEqual,
  OLEFontImpl_SetRatio,
  OLEFontImpl_QueryTextMetrics,
  OLEFontImpl_AddRefHfont,
  OLEFontImpl_ReleaseHfont,
  OLEFontImpl_SetHdc
};

276
static const IDispatchVtbl OLEFontImpl_IDispatch_VTable =
277 278 279 280 281 282 283 284 285 286
{
  OLEFontImpl_IDispatch_QueryInterface,
  OLEFontImpl_IDispatch_AddRef,
  OLEFontImpl_IDispatch_Release,
  OLEFontImpl_GetTypeInfoCount,
  OLEFontImpl_GetTypeInfo,
  OLEFontImpl_GetIDsOfNames,
  OLEFontImpl_Invoke
};

287
static const IPersistStreamVtbl OLEFontImpl_IPersistStream_VTable =
288 289 290 291 292 293 294 295 296 297
{
  OLEFontImpl_IPersistStream_QueryInterface,
  OLEFontImpl_IPersistStream_AddRef,
  OLEFontImpl_IPersistStream_Release,
  OLEFontImpl_GetClassID,
  OLEFontImpl_IsDirty,
  OLEFontImpl_Load,
  OLEFontImpl_Save,
  OLEFontImpl_GetSizeMax
};
298

299
static const IConnectionPointContainerVtbl
300 301 302 303 304 305 306 307 308
     OLEFontImpl_IConnectionPointContainer_VTable =
{
  OLEFontImpl_IConnectionPointContainer_QueryInterface,
  OLEFontImpl_IConnectionPointContainer_AddRef,
  OLEFontImpl_IConnectionPointContainer_Release,
  OLEFontImpl_EnumConnectionPoints,
  OLEFontImpl_FindConnectionPoint
};

309 310 311
static const IPersistPropertyBagVtbl OLEFontImpl_IPersistPropertyBag_VTable;
static const IPersistStreamInitVtbl OLEFontImpl_IPersistStreamInit_VTable;

312 313 314
/******************************************************************************
 *		OleCreateFontIndirect	[OLEAUT32.420]
 */
315
HRESULT WINAPI OleCreateFontIndirect(
316 317
  LPFONTDESC lpFontDesc,
  REFIID     riid,
318
  LPVOID*     ppvObj)
319 320 321 322
{
  OLEFontImpl* newFont = 0;
  HRESULT      hr      = S_OK;

323
  TRACE("(%p, %s, %p)\n", lpFontDesc, debugstr_guid(riid), ppvObj);
324 325 326 327 328 329 330 331
  /*
   * Sanity check
   */
  if (ppvObj==0)
    return E_POINTER;

  *ppvObj = 0;

332 333 334
  if (!lpFontDesc) {
    FONTDESC fd;

335
    static WCHAR fname[] = { 'S','y','s','t','e','m',0 };
336 337

    fd.cbSizeofstruct = sizeof(fd);
338
    fd.lpstrName      = fname;
339 340 341 342 343 344 345 346 347
    fd.cySize.s.Lo    = 80000;
    fd.cySize.s.Hi    = 0;
    fd.sWeight 	      = 0;
    fd.sCharset       = 0;
    fd.fItalic	      = 0;
    fd.fUnderline     = 0;
    fd.fStrikethrough = 0;
    lpFontDesc = &fd;
  }
348

349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
  /*
   * Try to construct a new instance of the class.
   */
  newFont = OLEFontImpl_Construct(lpFontDesc);

  if (newFont == 0)
    return E_OUTOFMEMORY;

  /*
   * Make sure it supports the interface required by the caller.
   */
  hr = IFont_QueryInterface((IFont*)newFont, riid, ppvObj);

  /*
   * Release the reference obtained in the constructor. If
   * the QueryInterface was unsuccessful, it will free the class.
   */
  IFont_Release((IFont*)newFont);

  return hr;
}


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

376 377 378 379 380 381 382 383
/***********************************************************************
 *    OLEFont_SendNotify (internal)
 *
 * Sends notification messages of changed properties to any interested
 * connections.
 */
static void OLEFont_SendNotify(OLEFontImpl* this, DISPID dispID)
{
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
  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};
  static const WCHAR wszCharset[] = {'C','h','a','r','s','s','e','t',0};
  static const LPCWSTR dispid_mapping[] =
  {
    wszName,
    NULL,
    wszSize,
    wszBold,
    wszItalic,
    wszUnder,
    wszStrike,
    wszWeight,
    wszCharset
  };

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

409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
  hres = IConnectionPoint_EnumConnections(this->pPropertyNotifyCP, &pEnum);
  if (SUCCEEDED(hres))
  {
    while(IEnumConnections_Next(pEnum, 1, &CD, NULL) == S_OK) {
      IPropertyNotifySink *sink;

      IUnknown_QueryInterface(CD.pUnk, &IID_IPropertyNotifySink, (LPVOID)&sink);
      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]);
432

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

438 439 440 441 442 443 444 445 446 447 448 449 450
    while(IEnumConnections_Next(pEnum, 1, &CD, NULL) == S_OK) {
        IFontEventsDisp *disp;

        IUnknown_QueryInterface(CD.pUnk, &IID_IFontEventsDisp, (LPVOID)&disp);
        IDispatch_Invoke(disp, DISPID_FONT_CHANGED, &IID_NULL,
                         LOCALE_NEUTRAL, INVOKE_FUNC, &dispparams, NULL,
                         NULL, NULL);

        IDispatch_Release(disp);
        IUnknown_Release(CD.pUnk);
    }
    VariantClear(&vararg);
    IEnumConnections_Release(pEnum);
451 452
  }
}
453

454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
/************************************************************************
 * 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.
 */
static OLEFontImpl* OLEFontImpl_Construct(LPFONTDESC fontDesc)
{
  OLEFontImpl* newObject = 0;

  /*
   * Allocate space for the object.
   */
  newObject = HeapAlloc(GetProcessHeap(), 0, sizeof(OLEFontImpl));

  if (newObject==0)
    return newObject;
474

475 476 477
  /*
   * Initialize the virtual function table.
   */
478 479 480 481 482 483
  newObject->lpVtbl = &OLEFontImpl_VTable;
  newObject->lpvtblIDispatch = &OLEFontImpl_IDispatch_VTable;
  newObject->lpvtblIPersistStream = &OLEFontImpl_IPersistStream_VTable;
  newObject->lpvtblIConnectionPointContainer = &OLEFontImpl_IConnectionPointContainer_VTable;
  newObject->lpvtblIPersistPropertyBag = &OLEFontImpl_IPersistPropertyBag_VTable;
  newObject->lpvtblIPersistStreamInit = &OLEFontImpl_IPersistStreamInit_VTable;
484

485
  /*
486
   * Start with one reference count. The caller of this function
487 488 489 490 491 492 493 494 495 496 497
   * must release the interface pointer when it is done.
   */
  newObject->ref = 1;

  /*
   * Copy the description of the font in the object.
   */
  assert(fontDesc->cbSizeofstruct >= sizeof(FONTDESC));

  newObject->description.cbSizeofstruct = sizeof(FONTDESC);
  newObject->description.lpstrName = HeapAlloc(GetProcessHeap(),
498
					       0,
499
					       (lstrlenW(fontDesc->lpstrName)+1) * sizeof(WCHAR));
500
  strcpyW(newObject->description.lpstrName, fontDesc->lpstrName);
501 502 503 504 505
  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;
506
  newObject->description.fStrikethrough = fontDesc->fStrikethrough;
507

508 509 510
  /*
   * Initializing all the other members.
   */
511
  newObject->gdiFont  = 0;
512
  newObject->fontLock = 0;
513 514
  newObject->cyLogical  = 72L;
  newObject->cyHimetric = 2540L;
515 516 517
  newObject->pPropertyNotifyCP = NULL;
  newObject->pFontEventsCP = NULL;

518 519
  CreateConnectionPoint((IUnknown*)newObject, &IID_IPropertyNotifySink, &newObject->pPropertyNotifyCP);
  CreateConnectionPoint((IUnknown*)newObject, &IID_IFontEventsDisp, &newObject->pFontEventsCP);
520 521 522 523 524 525 526

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

527
  TRACE("returning %p\n", newObject);
528 529 530 531
  return newObject;
}

/************************************************************************
532
 * OLEFontImpl_Destroy
533 534
 *
 * This method is called by the Release method when the reference
Andreas Mohr's avatar
Andreas Mohr committed
535
 * count goes down to 0. It will free all resources used by
536 537 538 539
 * this object.
 */
static void OLEFontImpl_Destroy(OLEFontImpl* fontDesc)
{
540 541
  TRACE("(%p)\n", fontDesc);

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

544
  if (fontDesc->gdiFont!=0)
545 546
    DeleteObject(fontDesc->gdiFont);

547 548 549 550 551
  if (fontDesc->pPropertyNotifyCP)
      IConnectionPoint_Release(fontDesc->pPropertyNotifyCP);
  if (fontDesc->pFontEventsCP)
      IConnectionPoint_Release(fontDesc->pFontEventsCP);

552 553 554 555 556 557 558 559 560 561 562 563 564
  HeapFree(GetProcessHeap(), 0, fontDesc);
}

/************************************************************************
 * OLEFontImpl_QueryInterface (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
HRESULT WINAPI OLEFontImpl_QueryInterface(
  IFont*  iface,
  REFIID  riid,
  void**  ppvObject)
{
565
  OLEFontImpl *this = (OLEFontImpl *)iface;
566
  TRACE("(%p)->(%s, %p)\n", this, debugstr_guid(riid), ppvObject);
567 568 569 570 571 572

  /*
   * Perform a sanity check on the parameters.
   */
  if ( (this==0) || (ppvObject==0) )
    return E_INVALIDARG;
573

574 575 576 577
  /*
   * Initialize the return parameter.
   */
  *ppvObject = 0;
578

579 580 581
  /*
   * Compare the riid with the interface IDs implemented by this object.
   */
582
  if (IsEqualGUID(&IID_IUnknown, riid))
583
    *ppvObject = (IFont*)this;
584
  if (IsEqualGUID(&IID_IFont, riid))
585
    *ppvObject = (IFont*)this;
586
  if (IsEqualGUID(&IID_IDispatch, riid))
587
    *ppvObject = (IDispatch*)&(this->lpvtblIDispatch);
588
  if (IsEqualGUID(&IID_IFontDisp, riid))
589
    *ppvObject = (IDispatch*)&(this->lpvtblIDispatch);
590
  if (IsEqualIID(&IID_IPersist, riid) || IsEqualGUID(&IID_IPersistStream, riid))
591
    *ppvObject = (IPersistStream*)&(this->lpvtblIPersistStream);
592
  if (IsEqualGUID(&IID_IConnectionPointContainer, riid))
593
    *ppvObject = (IConnectionPointContainer*)&(this->lpvtblIConnectionPointContainer);
594
  if (IsEqualGUID(&IID_IPersistPropertyBag, riid))
595
    *ppvObject = (IPersistPropertyBag*)&(this->lpvtblIPersistPropertyBag);
596
  if (IsEqualGUID(&IID_IPersistStreamInit, riid))
597
    *ppvObject = (IPersistStreamInit*)&(this->lpvtblIPersistStreamInit);
598

599 600 601 602
  /*
   * Check that we obtained an interface.
   */
  if ((*ppvObject)==0)
603
  {
604
    FIXME("() : asking for unsupported interface %s\n",debugstr_guid(riid));
605
    return E_NOINTERFACE;
606
  }
607
  OLEFontImpl_AddRef((IFont*)this);
608
  return S_OK;
609
}
610

611 612 613 614 615
/************************************************************************
 * OLEFontImpl_AddRef (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
616
ULONG WINAPI OLEFontImpl_AddRef(
617 618
  IFont* iface)
{
619
  OLEFontImpl *this = (OLEFontImpl *)iface;
620
  TRACE("(%p)->(ref=%d)\n", this, this->ref);
621
  return InterlockedIncrement(&this->ref);
622
}
623

624 625 626 627 628
/************************************************************************
 * OLEFontImpl_Release (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
629
ULONG WINAPI OLEFontImpl_Release(
630 631
      IFont* iface)
{
632
  OLEFontImpl *this = (OLEFontImpl *)iface;
633
  ULONG ret;
634
  TRACE("(%p)->(ref=%d)\n", this, this->ref);
635 636 637 638

  /*
   * Decrease the reference count on this object.
   */
639
  ret = InterlockedDecrement(&this->ref);
640 641 642 643

  /*
   * If the reference count goes down to 0, perform suicide.
   */
644
  if (ret==0) OLEFontImpl_Destroy(this);
645

646
  return ret;
647
}
648

649 650 651 652 653
/************************************************************************
 * OLEFontImpl_get_Name (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
654
static HRESULT WINAPI OLEFontImpl_get_Name(
655
  IFont*  iface,
656
  BSTR* pname)
657
{
658
  OLEFontImpl *this = (OLEFontImpl *)iface;
659
  TRACE("(%p)->(%p)\n", this, pname);
660 661 662 663 664 665 666
  /*
   * Sanity check.
   */
  if (pname==0)
    return E_POINTER;

  if (this->description.lpstrName!=0)
667
    *pname = SysAllocString(this->description.lpstrName);
668 669 670 671 672 673 674 675 676 677 678
  else
    *pname = 0;

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_put_Name (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
679
static HRESULT WINAPI OLEFontImpl_put_Name(
680
  IFont* iface,
681
  BSTR name)
682
{
683
  OLEFontImpl *this = (OLEFontImpl *)iface;
684
  TRACE("(%p)->(%p)\n", this, name);
685 686 687 688

  if (this->description.lpstrName==0)
  {
    this->description.lpstrName = HeapAlloc(GetProcessHeap(),
689
					    0,
690
					    (lstrlenW(name)+1) * sizeof(WCHAR));
691 692 693 694
  }
  else
  {
    this->description.lpstrName = HeapReAlloc(GetProcessHeap(),
695
					      0,
696
					      this->description.lpstrName,
697
					      (lstrlenW(name)+1) * sizeof(WCHAR));
698 699 700 701 702
  }

  if (this->description.lpstrName==0)
    return E_OUTOFMEMORY;

703
  strcpyW(this->description.lpstrName, name);
704 705
  TRACE("new name %s\n", debugstr_w(this->description.lpstrName));
  OLEFont_SendNotify(this, DISPID_FONT_NAME);
706 707 708 709 710 711 712 713
  return S_OK;
}

/************************************************************************
 * OLEFontImpl_get_Size (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
714
static HRESULT WINAPI OLEFontImpl_get_Size(
715
  IFont* iface,
716 717
  CY*    psize)
{
718
  OLEFontImpl *this = (OLEFontImpl *)iface;
719
  TRACE("(%p)->(%p)\n", this, psize);
720 721 722 723 724 725 726

  /*
   * Sanity check
   */
  if (psize==0)
    return E_POINTER;

727 728
  psize->s.Hi = 0;
  psize->s.Lo = this->description.cySize.s.Lo;
729 730 731 732 733 734 735 736 737

  return S_OK;
}

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

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_get_Bold (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
756
static HRESULT WINAPI OLEFontImpl_get_Bold(
757
  IFont*  iface,
758
  BOOL* pbold)
759
{
760
  OLEFontImpl *this = (OLEFontImpl *)iface;
761 762 763 764 765 766 767 768 769 770
  TRACE("(%p)->(%p)\n", this, pbold);
  /*
   * Sanity check
   */
  if (pbold==0)
    return E_POINTER;

  *pbold = this->description.sWeight > 550;

  return S_OK;
771 772 773 774 775 776 777
}

/************************************************************************
 * OLEFontImpl_put_Bold (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
778
static HRESULT WINAPI OLEFontImpl_put_Bold(
779
  IFont* iface,
780
  BOOL bold)
781
{
782
  OLEFontImpl *this = (OLEFontImpl *)iface;
783 784 785 786 787
  TRACE("(%p)->(%d)\n", this, bold);
  this->description.sWeight = bold ? FW_BOLD : FW_NORMAL;
  OLEFont_SendNotify(this, DISPID_FONT_BOLD);

  return S_OK;
788 789 790 791 792 793 794
}

/************************************************************************
 * OLEFontImpl_get_Italic (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
795
static HRESULT WINAPI OLEFontImpl_get_Italic(
796
  IFont*  iface,
797
  BOOL* pitalic)
798
{
799
  OLEFontImpl *this = (OLEFontImpl *)iface;
800
  TRACE("(%p)->(%p)\n", this, pitalic);
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
  /*
   * Sanity check
   */
  if (pitalic==0)
    return E_POINTER;

  *pitalic = this->description.fItalic;

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_put_Italic (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
817
static HRESULT WINAPI OLEFontImpl_put_Italic(
818
  IFont* iface,
819
  BOOL italic)
820
{
821
  OLEFontImpl *this = (OLEFontImpl *)iface;
822
  TRACE("(%p)->(%d)\n", this, italic);
823 824 825

  this->description.fItalic = italic;

826
  OLEFont_SendNotify(this, DISPID_FONT_ITALIC);
827 828 829 830 831 832 833 834
  return S_OK;
}

/************************************************************************
 * OLEFontImpl_get_Underline (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
835
static HRESULT WINAPI OLEFontImpl_get_Underline(
836
  IFont*  iface,
837
  BOOL* punderline)
838
{
839
  OLEFontImpl *this = (OLEFontImpl *)iface;
840
  TRACE("(%p)->(%p)\n", this, punderline);
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857

  /*
   * Sanity check
   */
  if (punderline==0)
    return E_POINTER;

  *punderline = this->description.fUnderline;

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_put_Underline (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
858
static HRESULT WINAPI OLEFontImpl_put_Underline(
859
  IFont* iface,
860
  BOOL underline)
861
{
862
  OLEFontImpl *this = (OLEFontImpl *)iface;
863
  TRACE("(%p)->(%d)\n", this, underline);
864 865 866

  this->description.fUnderline = underline;

867
  OLEFont_SendNotify(this, DISPID_FONT_UNDER);
868 869 870 871 872 873 874 875
  return S_OK;
}

/************************************************************************
 * OLEFontImpl_get_Strikethrough (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
876
static HRESULT WINAPI OLEFontImpl_get_Strikethrough(
877
  IFont*  iface,
878
  BOOL* pstrikethrough)
879
{
880
  OLEFontImpl *this = (OLEFontImpl *)iface;
881
  TRACE("(%p)->(%p)\n", this, pstrikethrough);
882 883 884 885 886 887 888

  /*
   * Sanity check
   */
  if (pstrikethrough==0)
    return E_POINTER;

889
  *pstrikethrough = this->description.fStrikethrough;
890 891 892 893 894 895 896 897 898

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_put_Strikethrough (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
899
static HRESULT WINAPI OLEFontImpl_put_Strikethrough(
900
 IFont* iface,
901
 BOOL strikethrough)
902
{
903
  OLEFontImpl *this = (OLEFontImpl *)iface;
904
  TRACE("(%p)->(%d)\n", this, strikethrough);
905

906
  this->description.fStrikethrough = strikethrough;
907
  OLEFont_SendNotify(this, DISPID_FONT_STRIKE);
908 909 910 911 912 913 914 915 916

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_get_Weight (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
917
static HRESULT WINAPI OLEFontImpl_get_Weight(
918
  IFont* iface,
919 920
  short* pweight)
{
921
  OLEFontImpl *this = (OLEFontImpl *)iface;
922
  TRACE("(%p)->(%p)\n", this, pweight);
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939

  /*
   * Sanity check
   */
  if (pweight==0)
    return E_POINTER;

  *pweight = this->description.sWeight;

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_put_Weight (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
940
static HRESULT WINAPI OLEFontImpl_put_Weight(
941
  IFont* iface,
942 943
  short  weight)
{
944
  OLEFontImpl *this = (OLEFontImpl *)iface;
945
  TRACE("(%p)->(%d)\n", this, weight);
946 947 948

  this->description.sWeight = weight;

949
  OLEFont_SendNotify(this, DISPID_FONT_WEIGHT);
950 951 952 953 954 955 956 957
  return S_OK;
}

/************************************************************************
 * OLEFontImpl_get_Charset (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
958
static HRESULT WINAPI OLEFontImpl_get_Charset(
959
  IFont* iface,
960 961
  short* pcharset)
{
962
  OLEFontImpl *this = (OLEFontImpl *)iface;
963
  TRACE("(%p)->(%p)\n", this, pcharset);
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980

  /*
   * Sanity check
   */
  if (pcharset==0)
    return E_POINTER;

  *pcharset = this->description.sCharset;

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_put_Charset (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
981
static HRESULT WINAPI OLEFontImpl_put_Charset(
982
  IFont* iface,
983 984
  short charset)
{
985
  OLEFontImpl *this = (OLEFontImpl *)iface;
986
  TRACE("(%p)->(%d)\n", this, charset);
987 988

  this->description.sCharset = charset;
989
  OLEFont_SendNotify(this, DISPID_FONT_CHARSET);
990 991 992 993 994 995 996 997 998

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_get_hFont (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
999
static HRESULT WINAPI OLEFontImpl_get_hFont(
1000
  IFont*   iface,
1001
  HFONT* phfont)
1002
{
1003
  OLEFontImpl *this = (OLEFontImpl *)iface;
1004
  TRACE("(%p)->(%p)\n", this, phfont);
1005 1006 1007 1008 1009
  if (phfont==NULL)
    return E_POINTER;

  /*
   * Realize the font if necessary
1010
 */
1011
  if (this->gdiFont==0)
1012
{
1013 1014 1015
    LOGFONTW logFont;
    INT      fontHeight;
    CY       cySize;
1016

1017 1018 1019 1020 1021 1022 1023 1024
    /*
     * 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.
     */
    IFont_get_Size(iface, &cySize);

1025 1026 1027
    /* Standard ratio is 72 / 2540, or 18 / 635 in lowest terms. */
    /* Ratio is applied here relative to the standard. */
    fontHeight = MulDiv( cySize.s.Lo, this->cyLogical*635, this->cyHimetric*18 );
1028 1029 1030

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

1031 1032
    logFont.lfHeight          = ((fontHeight%10000L)>5000L) ?	(-fontHeight/10000L)-1 :
 						    		(-fontHeight/10000L);
1033 1034
    logFont.lfItalic          = this->description.fItalic;
    logFont.lfUnderline       = this->description.fUnderline;
1035
    logFont.lfStrikeOut       = this->description.fStrikethrough;
1036 1037 1038 1039 1040 1041
    logFont.lfWeight          = this->description.sWeight;
    logFont.lfCharSet         = this->description.sCharset;
    logFont.lfOutPrecision    = OUT_CHARACTER_PRECIS;
    logFont.lfClipPrecision   = CLIP_DEFAULT_PRECIS;
    logFont.lfQuality         = DEFAULT_QUALITY;
    logFont.lfPitchAndFamily  = DEFAULT_PITCH;
1042
    strcpyW(logFont.lfFaceName,this->description.lpstrName);
1043 1044 1045 1046 1047

    this->gdiFont = CreateFontIndirectW(&logFont);
  }

  *phfont = this->gdiFont;
1048
  TRACE("Returning %p\n", *phfont);
1049
  return S_OK;
1050 1051 1052 1053 1054 1055 1056
}

/************************************************************************
 * OLEFontImpl_Clone (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
1057
static HRESULT WINAPI OLEFontImpl_Clone(
1058 1059 1060
  IFont*  iface,
  IFont** ppfont)
{
1061
  OLEFontImpl* newObject = 0;
1062 1063 1064
  LOGFONTW logFont;
  INT      fontHeight;
  CY       cySize;
1065
  OLEFontImpl *this = (OLEFontImpl *)iface;
1066
  TRACE("(%p)->(%p)\n", this, ppfont);
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082

  if (ppfont == NULL)
    return E_POINTER;

  *ppfont = NULL;

  /*
   * Allocate space for the object.
   */
  newObject = HeapAlloc(GetProcessHeap(), 0, sizeof(OLEFontImpl));

  if (newObject==NULL)
    return E_OUTOFMEMORY;

  *newObject = *this;

1083 1084
  /* We need to alloc new memory for the string, otherwise
   * we free memory twice.
1085
   */
1086 1087 1088 1089
  newObject->description.lpstrName = HeapAlloc(
	GetProcessHeap(),0,
	(1+strlenW(this->description.lpstrName))*2
  );
1090
  strcpyW(newObject->description.lpstrName, this->description.lpstrName);
1091 1092 1093
  /* We need to clone the HFONT too. This is just cut & paste from above */
  IFont_get_Size(iface, &cySize);

1094
  fontHeight = MulDiv(cySize.s.Lo, this->cyLogical*635,this->cyHimetric*18);
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112

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

  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.lfCharSet         = this->description.sCharset;
  logFont.lfOutPrecision    = OUT_CHARACTER_PRECIS;
  logFont.lfClipPrecision   = CLIP_DEFAULT_PRECIS;
  logFont.lfQuality         = DEFAULT_QUALITY;
  logFont.lfPitchAndFamily  = DEFAULT_PITCH;
  strcpyW(logFont.lfFaceName,this->description.lpstrName);

  newObject->gdiFont = CreateFontIndirectW(&logFont);

1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
  /* create new connection points */
  newObject->pPropertyNotifyCP = NULL;
  newObject->pFontEventsCP = NULL;
  CreateConnectionPoint((IUnknown*)newObject, &IID_IPropertyNotifySink, &newObject->pPropertyNotifyCP);
  CreateConnectionPoint((IUnknown*)newObject, &IID_IFontEventsDisp, &newObject->pFontEventsCP);

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

  /* The cloned object starts with a reference count of 1 */
1126 1127 1128 1129 1130
  newObject->ref          = 1;

  *ppfont = (IFont*)newObject;

  return S_OK;
1131 1132 1133 1134 1135 1136 1137
}

/************************************************************************
 * OLEFontImpl_IsEqual (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
1138
static HRESULT WINAPI OLEFontImpl_IsEqual(
1139
  IFont* iface,
1140 1141
  IFont* pFontOther)
{
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
  OLEFontImpl *left = (OLEFontImpl *)iface;
  OLEFontImpl *right = (OLEFontImpl *)pFontOther;
  HRESULT hres;
  INT left_len,right_len;

  if((iface == NULL) || (pFontOther == NULL))
    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 */
  left_len = strlenW(left->description.lpstrName);
  right_len = strlenW(right->description.lpstrName);
  hres = CompareStringW(0,0,left->description.lpstrName, left_len,
    right->description.lpstrName, right_len);
  if (hres != CSTR_EQUAL)
    return S_FALSE;

  return S_OK;
1173 1174 1175 1176 1177 1178 1179
}

/************************************************************************
 * OLEFontImpl_SetRatio (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
1180
static HRESULT WINAPI OLEFontImpl_SetRatio(
1181
  IFont* iface,
1182 1183
  LONG   cyLogical,
  LONG   cyHimetric)
1184
{
1185
  OLEFontImpl *this = (OLEFontImpl *)iface;
1186
  TRACE("(%p)->(%d, %d)\n", this, cyLogical, cyHimetric);
1187 1188 1189 1190 1191

  this->cyLogical  = cyLogical;
  this->cyHimetric = cyHimetric;

  return S_OK;
1192 1193 1194 1195 1196 1197 1198
}

/************************************************************************
 * OLEFontImpl_QueryTextMetrics (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
1199
static HRESULT      WINAPI OLEFontImpl_QueryTextMetrics(
1200
  IFont*         iface,
1201 1202
  TEXTMETRICOLE* ptm)
{
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
  HDC hdcRef;
  HFONT hOldFont, hNewFont;

  hdcRef = GetDC(0);
  OLEFontImpl_get_hFont(iface, &hNewFont);
  hOldFont = SelectObject(hdcRef, hNewFont);
  GetTextMetricsW(hdcRef, ptm);
  SelectObject(hdcRef, hOldFont);
  ReleaseDC(0, hdcRef);
  return S_OK;
1213 1214 1215 1216 1217 1218 1219
}

/************************************************************************
 * OLEFontImpl_AddRefHfont (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
1220
static HRESULT WINAPI OLEFontImpl_AddRefHfont(
1221
  IFont*  iface,
1222
  HFONT hfont)
1223
{
1224
  OLEFontImpl *this = (OLEFontImpl *)iface;
1225
  TRACE("(%p)->(%p) (lock=%d)\n", this, hfont, this->fontLock);
1226

1227
  if ( (hfont == 0) ||
1228 1229 1230 1231 1232 1233
       (hfont != this->gdiFont) )
    return E_INVALIDARG;

  this->fontLock++;

  return S_OK;
1234 1235 1236 1237 1238 1239 1240
}

/************************************************************************
 * OLEFontImpl_ReleaseHfont (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
1241
static HRESULT WINAPI OLEFontImpl_ReleaseHfont(
1242
  IFont*  iface,
1243
  HFONT hfont)
1244
{
1245
  OLEFontImpl *this = (OLEFontImpl *)iface;
1246
  TRACE("(%p)->(%p) (lock=%d)\n", this, hfont, this->fontLock);
1247

1248
  if ( (hfont == 0) ||
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
       (hfont != this->gdiFont) )
    return E_INVALIDARG;

  this->fontLock--;

  /*
   * If we just released our last font reference, destroy it.
   */
  if (this->fontLock==0)
  {
    DeleteObject(this->gdiFont);
1260
    this->gdiFont = 0;
1261
  }
1262 1263

  return S_OK;
1264 1265 1266 1267 1268 1269 1270
}

/************************************************************************
 * OLEFontImpl_SetHdc (IFont)
 *
 * See Windows documentation for more details on IFont methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
1271
static HRESULT WINAPI OLEFontImpl_SetHdc(
1272
  IFont* iface,
1273
  HDC  hdc)
1274
{
1275
  OLEFontImpl *this = (OLEFontImpl *)iface;
1276
  FIXME("(%p)->(%p): Stub\n", this, hdc);
1277 1278 1279 1280 1281 1282 1283 1284
  return E_NOTIMPL;
}

/************************************************************************
 * OLEFontImpl_IDispatch_QueryInterface (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
1285
static HRESULT WINAPI OLEFontImpl_IDispatch_QueryInterface(
1286 1287 1288 1289
  IDispatch* iface,
  REFIID     riid,
  VOID**     ppvoid)
{
1290
  OLEFontImpl *this = impl_from_IDispatch(iface);
1291

1292
  return IFont_QueryInterface((IFont *)this, riid, ppvoid);
1293 1294 1295 1296 1297 1298 1299
}

/************************************************************************
 * OLEFontImpl_IDispatch_Release (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
1300
static ULONG WINAPI OLEFontImpl_IDispatch_Release(
1301 1302
  IDispatch* iface)
{
1303
  OLEFontImpl *this = impl_from_IDispatch(iface);
1304

1305
  return IFont_Release((IFont *)this);
1306 1307 1308 1309 1310 1311 1312
}

/************************************************************************
 * OLEFontImpl_IDispatch_AddRef (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
1313
static ULONG WINAPI OLEFontImpl_IDispatch_AddRef(
1314 1315
  IDispatch* iface)
{
1316
  OLEFontImpl *this = impl_from_IDispatch(iface);
1317

1318
  return IFont_AddRef((IFont *)this);
1319 1320 1321 1322 1323 1324 1325
}

/************************************************************************
 * OLEFontImpl_GetTypeInfoCount (IDispatch)
 *
 * See Windows documentation for more details on IDispatch methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
1326
static HRESULT WINAPI OLEFontImpl_GetTypeInfoCount(
1327
  IDispatch*    iface,
1328 1329
  unsigned int* pctinfo)
{
1330
  OLEFontImpl *this = impl_from_IDispatch(iface);
1331 1332
  TRACE("(%p)->(%p)\n", this, pctinfo);
  *pctinfo = 1;
1333

1334
  return S_OK;
1335 1336 1337 1338 1339 1340 1341
}

/************************************************************************
 * OLEFontImpl_GetTypeInfo (IDispatch)
 *
 * See Windows documentation for more details on IDispatch methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
1342
static HRESULT WINAPI OLEFontImpl_GetTypeInfo(
1343
  IDispatch*  iface,
1344
  UINT      iTInfo,
1345
  LCID        lcid,
1346 1347
  ITypeInfo** ppTInfo)
{
1348
  static const WCHAR stdole2tlb[] = {'s','t','d','o','l','e','2','.','t','l','b',0};
1349 1350
  ITypeLib *tl;
  HRESULT hres;
1351

1352
  OLEFontImpl *this = impl_from_IDispatch(iface);
1353
  TRACE("(%p, iTInfo=%d, lcid=%04x, %p)\n", this, iTInfo, (int)lcid, ppTInfo);
1354 1355
  if (iTInfo != 0)
    return E_FAIL;
1356
  hres = LoadTypeLib(stdole2tlb, &tl);
1357
  if (FAILED(hres)) {
1358
    ERR("Could not load the stdole2.tlb?\n");
1359 1360
    return hres;
  }
1361
  hres = ITypeLib_GetTypeInfoOfGuid(tl, &IID_IFontDisp, ppTInfo);
1362
  if (FAILED(hres)) {
1363
    FIXME("Did not IDispatch typeinfo from typelib, hres %x\n",hres);
1364 1365
  }
  return hres;
1366 1367 1368 1369 1370 1371 1372
}

/************************************************************************
 * OLEFontImpl_GetIDsOfNames (IDispatch)
 *
 * See Windows documentation for more details on IDispatch methods.
 */
Marcus Meissner's avatar
Marcus Meissner committed
1373
static HRESULT WINAPI OLEFontImpl_GetIDsOfNames(
1374
  IDispatch*  iface,
1375 1376 1377
  REFIID      riid,
  LPOLESTR* rgszNames,
  UINT      cNames,
1378 1379 1380
  LCID        lcid,
  DISPID*     rgDispId)
{
1381 1382 1383
  ITypeInfo * pTInfo;
  HRESULT hres;

1384
  OLEFontImpl *this = impl_from_IDispatch(iface);
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409

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

  if (cNames == 0)
  {
    return E_INVALIDARG;
  }
  else
  {
    /* retrieve type information */
    hres = OLEFontImpl_GetTypeInfo(iface, 0, lcid, &pTInfo);

    if (FAILED(hres))
    {
      ERR("GetTypeInfo failed.\n");
      return hres;
    }

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

    return hres;
  }
1410 1411 1412 1413 1414 1415
}

/************************************************************************
 * OLEFontImpl_Invoke (IDispatch)
 *
 * See Windows documentation for more details on IDispatch methods.
1416 1417 1418 1419
 * 
 * Note: Do not call _put_Xxx methods, since setting things here
 * should not call notify functions as I found out debugging the generic
 * MS VB5 installer.
1420
 */
Marcus Meissner's avatar
Marcus Meissner committed
1421
static HRESULT WINAPI OLEFontImpl_Invoke(
1422
  IDispatch*  iface,
1423 1424 1425
  DISPID      dispIdMember,
  REFIID      riid,
  LCID        lcid,
1426 1427
  WORD        wFlags,
  DISPPARAMS* pDispParams,
1428
  VARIANT*    pVarResult,
1429
  EXCEPINFO*  pExepInfo,
1430
  UINT*     puArgErr)
1431
{
1432
  OLEFontImpl *this = impl_from_IDispatch(iface);
1433
  HRESULT hr;
1434

1435
  TRACE("%p->(%d,%s,0x%x,0x%x,%p,%p,%p,%p)\n", this, dispIdMember,
1436 1437 1438
    debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult, pExepInfo,
    puArgErr);

1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
  /* 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)
    {
1464
      ERR("param count for DISPATCH_PROPERTYPUT was %d instead of 1\n", pDispParams->cArgs);
1465 1466 1467 1468 1469 1470 1471 1472 1473
      return DISP_E_BADPARAMCOUNT;
    }
  }
  else
  {
    ERR("one of DISPATCH_PROPERTYGET or DISPATCH_PROPERTYPUT must be specified\n");
    return DISP_E_MEMBERNOTFOUND;
  }

1474 1475
  switch (dispIdMember) {
  case DISPID_FONT_NAME:
1476
    if (wFlags & DISPATCH_PROPERTYGET) {
1477
      V_VT(pVarResult) = VT_BSTR;
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490
      return IFont_get_Name((IFont *)this, &V_BSTR(pVarResult));
    } else {
      VARIANTARG vararg;

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

      hr = IFont_put_Name((IFont *)this, V_BSTR(&vararg));

      VariantClear(&vararg);
      return hr;
1491 1492 1493
    }
    break;
  case DISPID_FONT_BOLD:
1494 1495 1496
    if (wFlags & DISPATCH_PROPERTYGET) {
      BOOL value;
      hr = IFont_get_Bold((IFont *)this, &value);
1497
      V_VT(pVarResult) = VT_BOOL;
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
      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;

      hr = IFont_put_Bold((IFont *)this, V_BOOL(&vararg));

      VariantClear(&vararg);
      return hr;
1512 1513 1514
    }
    break;
  case DISPID_FONT_ITALIC:
1515 1516 1517
    if (wFlags & DISPATCH_PROPERTYGET) {
      BOOL value;
      hr = IFont_get_Italic((IFont *)this, &value);
1518
      V_VT(pVarResult) = VT_BOOL;
1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533
      V_BOOL(pVarResult) = value ? VARIANT_TRUE : VARIANT_FALSE;
      return hr;
    } else {
      VARIANTARG vararg;
      HRESULT hr;

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

      hr = IFont_put_Italic((IFont *)this, V_BOOL(&vararg));

      VariantClear(&vararg);
      return hr;
1534 1535 1536
    }
    break;
  case DISPID_FONT_UNDER:
1537 1538 1539
    if (wFlags & DISPATCH_PROPERTYGET) {
      BOOL value;
      hr = IFont_get_Underline((IFont *)this, &value);
1540
      V_VT(pVarResult) = VT_BOOL;
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555
      V_BOOL(pVarResult) = value ? VARIANT_TRUE : VARIANT_FALSE;
      return hr;
    } else {
      VARIANTARG vararg;
      HRESULT hr;

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

      hr = IFont_put_Underline((IFont *)this, V_BOOL(&vararg));

      VariantClear(&vararg);
      return hr;
1556 1557 1558
    }
    break;
  case DISPID_FONT_STRIKE:
1559 1560 1561
    if (wFlags & DISPATCH_PROPERTYGET) {
      BOOL value;
      hr = IFont_get_Strikethrough((IFont *)this, &value);
1562
      V_VT(pVarResult) = VT_BOOL;
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577
      V_BOOL(pVarResult) = value ? VARIANT_TRUE : VARIANT_FALSE;
      return hr;
    } else {
      VARIANTARG vararg;
      HRESULT hr;

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

      hr = IFont_put_Strikethrough((IFont *)this, V_BOOL(&vararg));

      VariantClear(&vararg);
      return hr;
1578 1579 1580
    }
    break;
  case DISPID_FONT_SIZE:
1581
    if (wFlags & DISPATCH_PROPERTYGET) {
1582
      V_VT(pVarResult) = VT_CY;
1583
      return OLEFontImpl_get_Size((IFont *)this, &V_CY(pVarResult));
1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
    } else {
      VARIANTARG vararg;
      HRESULT hr;

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

      hr = IFont_put_Size((IFont *)this, V_CY(&vararg));

      VariantClear(&vararg);
      return hr;
1597 1598
    }
    break;
1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
  case DISPID_FONT_WEIGHT:
    if (wFlags & DISPATCH_PROPERTYGET) {
      V_VT(pVarResult) = VT_I2;
      return OLEFontImpl_get_Weight((IFont *)this, &V_I2(pVarResult));
    } else {
      VARIANTARG vararg;
      HRESULT hr;

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

      hr = IFont_put_Weight((IFont *)this, V_I2(&vararg));

      VariantClear(&vararg);
      return hr;
    }
    break;
1618
  case DISPID_FONT_CHARSET:
1619
    if (wFlags & DISPATCH_PROPERTYGET) {
1620
      V_VT(pVarResult) = VT_I2;
1621
      return OLEFontImpl_get_Charset((IFont *)this, &V_I2(pVarResult));
1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
    } else {
      VARIANTARG vararg;
      HRESULT hr;

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

      hr = IFont_put_Charset((IFont *)this, V_I2(&vararg));

      VariantClear(&vararg);
      return hr;
1635 1636
    }
    break;
1637
  default:
1638
    ERR("member not found for dispid 0x%x\n", dispIdMember);
1639
    return DISP_E_MEMBERNOTFOUND;
1640
  }
1641 1642
}

1643 1644 1645 1646 1647 1648 1649 1650 1651 1652
/************************************************************************
 * OLEFontImpl_IPersistStream_QueryInterface (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
static HRESULT WINAPI OLEFontImpl_IPersistStream_QueryInterface(
  IPersistStream* iface,
  REFIID     riid,
  VOID**     ppvoid)
{
1653
  OLEFontImpl *this = impl_from_IPersistStream(iface);
1654

1655
  return IFont_QueryInterface((IFont *)this, riid, ppvoid);
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
}

/************************************************************************
 * OLEFontImpl_IPersistStream_Release (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
static ULONG WINAPI OLEFontImpl_IPersistStream_Release(
  IPersistStream* iface)
{
1666
  OLEFontImpl *this = impl_from_IPersistStream(iface);
1667

1668
  return IFont_Release((IFont *)this);
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678
}

/************************************************************************
 * OLEFontImpl_IPersistStream_AddRef (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
static ULONG WINAPI OLEFontImpl_IPersistStream_AddRef(
  IPersistStream* iface)
{
1679
  OLEFontImpl *this = impl_from_IPersistStream(iface);
1680

1681
  return IFont_AddRef((IFont *)this);
1682 1683 1684 1685 1686 1687 1688 1689
}

/************************************************************************
 * OLEFontImpl_GetClassID (IPersistStream)
 *
 * See Windows documentation for more details on IPersistStream methods.
 */
static HRESULT WINAPI OLEFontImpl_GetClassID(
1690
  IPersistStream* iface,
1691 1692
  CLSID*                pClassID)
{
1693
  TRACE("(%p,%p)\n",iface,pClassID);
1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
  if (pClassID==0)
    return E_POINTER;

  memcpy(pClassID, &CLSID_StdFont, sizeof(CLSID_StdFont));

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_IsDirty (IPersistStream)
 *
 * See Windows documentation for more details on IPersistStream methods.
 */
static HRESULT WINAPI OLEFontImpl_IsDirty(
  IPersistStream*  iface)
{
1710
  TRACE("(%p)\n",iface);
1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
  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)
{
  char  readBuffer[0x100];
  ULONG cbRead;
  BYTE  bVersion;
  BYTE  bAttributes;
  BYTE  bStringSize;
1744
  INT len;
1745

1746
  OLEFontImpl *this = impl_from_IPersistStream(iface);
1747

1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
  /*
   * Read the version byte
   */
  IStream_Read(pLoadStream, &bVersion, 1, &cbRead);

  if ( (cbRead!=1) ||
       (bVersion!=0x01) )
    return E_FAIL;

  /*
   * Charset
   */
  IStream_Read(pLoadStream, &this->description.sCharset, 2, &cbRead);

  if (cbRead!=2)
    return E_FAIL;

  /*
   * Attributes
   */
  IStream_Read(pLoadStream, &bAttributes, 1, &cbRead);

  if (cbRead!=1)
    return E_FAIL;

  this->description.fItalic        = (bAttributes & FONTPERSIST_ITALIC) != 0;
1774
  this->description.fStrikethrough = (bAttributes & FONTPERSIST_STRIKETHROUGH) != 0;
1775
  this->description.fUnderline     = (bAttributes & FONTPERSIST_UNDERLINE) != 0;
1776

1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787
  /*
   * Weight
   */
  IStream_Read(pLoadStream, &this->description.sWeight, 2, &cbRead);

  if (cbRead!=2)
    return E_FAIL;

  /*
   * Size
   */
1788
  IStream_Read(pLoadStream, &this->description.cySize.s.Lo, 4, &cbRead);
1789 1790 1791 1792

  if (cbRead!=4)
    return E_FAIL;

1793
  this->description.cySize.s.Hi = 0;
1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807

  /*
   * FontName
   */
  IStream_Read(pLoadStream, &bStringSize, 1, &cbRead);

  if (cbRead!=1)
    return E_FAIL;

  IStream_Read(pLoadStream, readBuffer, bStringSize, &cbRead);

  if (cbRead!=bStringSize)
    return E_FAIL;

1808
  HeapFree(GetProcessHeap(), 0, this->description.lpstrName);
1809

1810 1811 1812 1813
  len = MultiByteToWideChar( CP_ACP, 0, readBuffer, bStringSize, NULL, 0 );
  this->description.lpstrName = HeapAlloc( GetProcessHeap(), 0, (len+1) * sizeof(WCHAR) );
  MultiByteToWideChar( CP_ACP, 0, readBuffer, bStringSize, this->description.lpstrName, len );
  this->description.lpstrName[len] = 0;
1814

1815 1816 1817 1818
  /* Ensure use of this font causes a new one to be created @@@@ */
  DeleteObject(this->gdiFont);
  this->gdiFont = 0;

1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836
  return S_OK;
}

/************************************************************************
 * OLEFontImpl_Save (IPersistStream)
 *
 * See Windows documentation for more details on IPersistStream methods.
 */
static HRESULT WINAPI OLEFontImpl_Save(
  IPersistStream*  iface,
  IStream*         pOutStream,
  BOOL             fClearDirty)
{
  char* writeBuffer = NULL;
  ULONG cbWritten;
  BYTE  bVersion = 0x01;
  BYTE  bAttributes;
  BYTE  bStringSize;
1837

1838
  OLEFontImpl *this = impl_from_IPersistStream(iface);
1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863

  /*
   * Read the version byte
   */
  IStream_Write(pOutStream, &bVersion, 1, &cbWritten);

  if (cbWritten!=1)
    return E_FAIL;

  /*
   * Charset
   */
  IStream_Write(pOutStream, &this->description.sCharset, 2, &cbWritten);

  if (cbWritten!=2)
    return E_FAIL;

  /*
   * Attributes
   */
  bAttributes = 0;

  if (this->description.fItalic)
    bAttributes |= FONTPERSIST_ITALIC;

1864
  if (this->description.fStrikethrough)
1865
    bAttributes |= FONTPERSIST_STRIKETHROUGH;
1866

1867 1868 1869 1870 1871 1872 1873
  if (this->description.fUnderline)
    bAttributes |= FONTPERSIST_UNDERLINE;

  IStream_Write(pOutStream, &bAttributes, 1, &cbWritten);

  if (cbWritten!=1)
    return E_FAIL;
1874

1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885
  /*
   * Weight
   */
  IStream_Write(pOutStream, &this->description.sWeight, 2, &cbWritten);

  if (cbWritten!=2)
    return E_FAIL;

  /*
   * Size
   */
1886
  IStream_Write(pOutStream, &this->description.cySize.s.Lo, 4, &cbWritten);
1887 1888 1889 1890 1891 1892 1893 1894

  if (cbWritten!=4)
    return E_FAIL;

  /*
   * FontName
   */
  if (this->description.lpstrName!=0)
1895 1896
    bStringSize = WideCharToMultiByte( CP_ACP, 0, this->description.lpstrName,
                                       strlenW(this->description.lpstrName), NULL, 0, NULL, NULL );
1897 1898 1899 1900 1901 1902 1903 1904 1905 1906
  else
    bStringSize = 0;

  IStream_Write(pOutStream, &bStringSize, 1, &cbWritten);

  if (cbWritten!=1)
    return E_FAIL;

  if (bStringSize!=0)
  {
1907 1908 1909 1910
      if (!(writeBuffer = HeapAlloc( GetProcessHeap(), 0, bStringSize ))) return E_OUTOFMEMORY;
      WideCharToMultiByte( CP_ACP, 0, this->description.lpstrName,
                           strlenW(this->description.lpstrName),
                           writeBuffer, bStringSize, NULL, NULL );
1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930

    IStream_Write(pOutStream, writeBuffer, bStringSize, &cbWritten);
    HeapFree(GetProcessHeap(), 0, writeBuffer);

    if (cbWritten!=bStringSize)
      return E_FAIL;
  }

  return S_OK;
}

/************************************************************************
 * OLEFontImpl_GetSizeMax (IPersistStream)
 *
 * See Windows documentation for more details on IPersistStream methods.
 */
static HRESULT WINAPI OLEFontImpl_GetSizeMax(
  IPersistStream*  iface,
  ULARGE_INTEGER*  pcbSize)
{
1931
  OLEFontImpl *this = impl_from_IPersistStream(iface);
1932 1933 1934 1935

  if (pcbSize==NULL)
    return E_POINTER;

1936 1937
  pcbSize->u.HighPart = 0;
  pcbSize->u.LowPart = 0;
1938

1939 1940 1941 1942 1943 1944
  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 */
1945 1946

  if (this->description.lpstrName!=0)
1947
    pcbSize->u.LowPart += lstrlenW(this->description.lpstrName);
1948 1949 1950

  return S_OK;
}
1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961

/************************************************************************
 * OLEFontImpl_IConnectionPointContainer_QueryInterface (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
static HRESULT WINAPI OLEFontImpl_IConnectionPointContainer_QueryInterface(
  IConnectionPointContainer* iface,
  REFIID     riid,
  VOID**     ppvoid)
{
1962
  OLEFontImpl *this = impl_from_IConnectionPointContainer(iface);
1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974

  return IFont_QueryInterface((IFont*)this, riid, ppvoid);
}

/************************************************************************
 * OLEFontImpl_IConnectionPointContainer_Release (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
static ULONG WINAPI OLEFontImpl_IConnectionPointContainer_Release(
  IConnectionPointContainer* iface)
{
1975
  OLEFontImpl *this = impl_from_IConnectionPointContainer(iface);
1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987

  return IFont_Release((IFont*)this);
}

/************************************************************************
 * OLEFontImpl_IConnectionPointContainer_AddRef (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
static ULONG WINAPI OLEFontImpl_IConnectionPointContainer_AddRef(
  IConnectionPointContainer* iface)
{
1988
  OLEFontImpl *this = impl_from_IConnectionPointContainer(iface);
1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002

  return IFont_AddRef((IFont*)this);
}

/************************************************************************
 * OLEFontImpl_EnumConnectionPoints (IConnectionPointContainer)
 *
 * See Windows documentation for more details on IConnectionPointContainer
 * methods.
 */
static HRESULT WINAPI OLEFontImpl_EnumConnectionPoints(
  IConnectionPointContainer* iface,
  IEnumConnectionPoints **ppEnum)
{
2003
  OLEFontImpl *this = impl_from_IConnectionPointContainer(iface);
2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019

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

/************************************************************************
 * OLEFontImpl_FindConnectionPoint (IConnectionPointContainer)
 *
 * See Windows documentation for more details on IConnectionPointContainer
 * methods.
 */
static HRESULT WINAPI OLEFontImpl_FindConnectionPoint(
   IConnectionPointContainer* iface,
   REFIID riid,
   IConnectionPoint **ppCp)
{
2020
  OLEFontImpl *this = impl_from_IConnectionPointContainer(iface);
2021
  TRACE("(%p)->(%s, %p)\n", this, debugstr_guid(riid), ppCp);
2022

2023 2024 2025 2026 2027 2028 2029 2030
  if(IsEqualIID(riid, &IID_IPropertyNotifySink)) {
    return IConnectionPoint_QueryInterface(this->pPropertyNotifyCP,
                                           &IID_IConnectionPoint,
                                           (LPVOID)ppCp);
  } else if(IsEqualIID(riid, &IID_IFontEventsDisp)) {
    return IConnectionPoint_QueryInterface(this->pFontEventsCP,
                                           &IID_IConnectionPoint,
                                           (LPVOID)ppCp);
2031
  } else {
2032 2033
    FIXME("no connection point for %s\n", debugstr_guid(riid));
    return CONNECT_E_NOCONNECTION;
2034 2035 2036
  }
}

2037 2038 2039 2040 2041 2042
/************************************************************************
 * OLEFontImpl implementation of IPersistPropertyBag.
 */
static HRESULT WINAPI OLEFontImpl_IPersistPropertyBag_QueryInterface(
   IPersistPropertyBag *iface, REFIID riid, LPVOID *ppvObj
) {
2043 2044
  OLEFontImpl *this = impl_from_IPersistPropertyBag(iface);
  return IFont_QueryInterface((IFont *)this,riid,ppvObj);
2045 2046 2047 2048 2049
}

static ULONG WINAPI OLEFontImpl_IPersistPropertyBag_AddRef(
   IPersistPropertyBag *iface
) {
2050 2051
  OLEFontImpl *this = impl_from_IPersistPropertyBag(iface);
  return IFont_AddRef((IFont *)this);
2052 2053 2054 2055 2056
}

static ULONG WINAPI OLEFontImpl_IPersistPropertyBag_Release(
   IPersistPropertyBag *iface
) {
2057 2058
  OLEFontImpl *this = impl_from_IPersistPropertyBag(iface);
  return IFont_Release((IFont *)this);
2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077
}

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
) {
2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096
/* (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};
    VARIANT rawAttr;
    VARIANT valueAttr;
    HRESULT iRes = S_OK;
2097
    OLEFontImpl *this = impl_from_IPersistPropertyBag(iface);
2098 2099 2100 2101 2102 2103 2104 2105 2106 2107

    VariantInit(&rawAttr);
    VariantInit(&valueAttr);

    if (iRes == S_OK) {
        iRes = IPropertyBag_Read(pPropBag, sAttrName, &rawAttr, pErrorLog);
        if (iRes == S_OK)
        {
            iRes = VariantChangeType(&rawAttr, &valueAttr, 0, VT_BSTR);
            if (iRes == S_OK)
2108
                iRes = IFont_put_Name((IFont *)this, V_BSTR(&valueAttr));
2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121
        }
        else if (iRes == E_INVALIDARG)
            iRes = S_OK;
        VariantClear(&rawAttr);
        VariantClear(&valueAttr);
    }

    if (iRes == S_OK) {
        iRes = IPropertyBag_Read(pPropBag, sAttrSize, &rawAttr, pErrorLog);
        if (iRes == S_OK)
        {
            iRes = VariantChangeType(&rawAttr, &valueAttr, 0, VT_CY);
            if (iRes == S_OK)
2122
                iRes = IFont_put_Size((IFont *)this, V_CY(&valueAttr));
2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135
        }
        else if (iRes == E_INVALIDARG)
            iRes = S_OK;
        VariantClear(&rawAttr);
        VariantClear(&valueAttr);
    }

    if (iRes == S_OK) {
        iRes = IPropertyBag_Read(pPropBag, sAttrCharset, &rawAttr, pErrorLog);
        if (iRes == S_OK)
        {
            iRes = VariantChangeType(&rawAttr, &valueAttr, 0, VT_I2);
            if (iRes == S_OK)
2136
                iRes = IFont_put_Charset((IFont *)this, V_I2(&valueAttr));
2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149
        }
        else if (iRes == E_INVALIDARG)
            iRes = S_OK;
        VariantClear(&rawAttr);
        VariantClear(&valueAttr);
    }

    if (iRes == S_OK) {
        iRes = IPropertyBag_Read(pPropBag, sAttrWeight, &rawAttr, pErrorLog);
        if (iRes == S_OK)
        {
            iRes = VariantChangeType(&rawAttr, &valueAttr, 0, VT_I2);
            if (iRes == S_OK)
2150
                iRes = IFont_put_Weight((IFont *)this, V_I2(&valueAttr));
2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164
        }
        else if (iRes == E_INVALIDARG)
            iRes = S_OK;
        VariantClear(&rawAttr);
        VariantClear(&valueAttr);

    }

    if (iRes == S_OK) {
        iRes = IPropertyBag_Read(pPropBag, sAttrUnderline, &rawAttr, pErrorLog);
        if (iRes == S_OK)
        {
            iRes = VariantChangeType(&rawAttr, &valueAttr, 0, VT_BOOL);
            if (iRes == S_OK)
2165
                iRes = IFont_put_Underline((IFont *)this, V_BOOL(&valueAttr));
2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178
        }
        else if (iRes == E_INVALIDARG)
            iRes = S_OK;
        VariantClear(&rawAttr);
        VariantClear(&valueAttr);
    }

    if (iRes == S_OK) {
        iRes = IPropertyBag_Read(pPropBag, sAttrItalic, &rawAttr, pErrorLog);
        if (iRes == S_OK)
        {
            iRes = VariantChangeType(&rawAttr, &valueAttr, 0, VT_BOOL);
            if (iRes == S_OK)
2179
                iRes = IFont_put_Italic((IFont *)this, V_BOOL(&valueAttr));
2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192
        }
        else if (iRes == E_INVALIDARG)
            iRes = S_OK;
        VariantClear(&rawAttr);
        VariantClear(&valueAttr);
    }

    if (iRes == S_OK) {
        iRes = IPropertyBag_Read(pPropBag, sAttrStrikethrough, &rawAttr, pErrorLog);
        if (iRes == S_OK)
        {
            iRes = VariantChangeType(&rawAttr, &valueAttr, 0, VT_BOOL);
            if (iRes == S_OK)
2193
                IFont_put_Strikethrough((IFont *)this, V_BOOL(&valueAttr));
2194 2195 2196 2197 2198 2199 2200 2201
        }
        else if (iRes == E_INVALIDARG)
            iRes = S_OK;
        VariantClear(&rawAttr);
        VariantClear(&valueAttr);
    }

    if (FAILED(iRes))
2202
        WARN("-- 0x%08x\n", iRes);
2203
    return iRes;
2204 2205 2206 2207 2208 2209 2210 2211 2212 2213
}

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;
}

2214
static const IPersistPropertyBagVtbl OLEFontImpl_IPersistPropertyBag_VTable = 
2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231
{
  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
) {
2232 2233
  OLEFontImpl *this = impl_from_IPersistStreamInit(iface);
  return IFont_QueryInterface((IFont *)this,riid,ppvObj);
2234 2235 2236 2237 2238
}

static ULONG WINAPI OLEFontImpl_IPersistStreamInit_AddRef(
   IPersistStreamInit *iface
) {
2239 2240
  OLEFontImpl *this = impl_from_IPersistStreamInit(iface);
  return IFont_AddRef((IFont *)this);
2241 2242 2243 2244 2245
}

static ULONG WINAPI OLEFontImpl_IPersistStreamInit_Release(
   IPersistStreamInit *iface
) {
2246 2247
  OLEFontImpl *this = impl_from_IPersistStreamInit(iface);
  return IFont_Release((IFont *)this);
2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291
}

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;
}

2292
static const IPersistStreamInitVtbl OLEFontImpl_IPersistStreamInit_VTable = 
2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305
{
  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
};

2306 2307 2308 2309 2310 2311
/*******************************************************************************
 * StdFont ClassFactory
 */
typedef struct
{
    /* IUnknown fields */
2312
    const IClassFactoryVtbl    *lpVtbl;
2313
    LONG                        ref;
2314 2315
} IClassFactoryImpl;

2316
static HRESULT WINAPI
2317
SFCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
2318
	IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
2319 2320 2321 2322 2323 2324 2325

	FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
	return E_NOINTERFACE;
}

static ULONG WINAPI
SFCF_AddRef(LPCLASSFACTORY iface) {
2326
	IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
2327
	return InterlockedIncrement(&This->ref);
2328 2329 2330
}

static ULONG WINAPI SFCF_Release(LPCLASSFACTORY iface) {
2331
	IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
2332
	/* static class, won't be  freed */
2333
	return InterlockedDecrement(&This->ref);
2334 2335 2336 2337 2338
}

static HRESULT WINAPI SFCF_CreateInstance(
	LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
) {
2339
	return OleCreateFontIndirect(NULL,riid,ppobj);
2340 2341 2342 2343

}

static HRESULT WINAPI SFCF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
2344
	IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
2345 2346 2347 2348
	FIXME("(%p)->(%d),stub!\n",This,dolock);
	return S_OK;
}

2349
static const IClassFactoryVtbl SFCF_Vtbl = {
2350 2351 2352 2353 2354 2355 2356 2357 2358
	SFCF_QueryInterface,
	SFCF_AddRef,
	SFCF_Release,
	SFCF_CreateInstance,
	SFCF_LockServer
};
static IClassFactoryImpl STDFONT_CF = {&SFCF_Vtbl, 1 };

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