Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

600 lines
13 KiB

  1. /***************************************************************************
  2. * PANMAP.CPP - Implementaion for ElseWare PANOSE(tm) 1.0 Font Mapper.
  3. * OLE 2.0 Implementation
  4. *
  5. *
  6. * Copyright (C) 1991-94 ElseWare Corporation. All rights reserved.
  7. ***************************************************************************/
  8. #define _INC_OLE
  9. #define CONST_VTABLE
  10. #include <windows.h>
  11. #include <ole2.h>
  12. // #pragma data_seg(".text")
  13. #define INITGUID
  14. #include <initguid.h>
  15. #include <cguid.h>
  16. #include "elsepan.h"
  17. #include "panmap.h"
  18. #undef INITGUID
  19. // #pragma data_seg()
  20. ULONG g_ulRefThisDll = 0;
  21. ULONG g_ulLock = 0; //
  22. ULONG g_ulObjs = 0; // Number of objects
  23. typedef void (FAR PASCAL *LPFNDESTROYED)(void);
  24. ////////////////////////////////////////////////////////////////////////////
  25. //
  26. // Overloaded allocation operators.
  27. //
  28. // Needed by NT since we don't yet link to real C++ runtimes
  29. //
  30. ////////////////////////////////////////////////////////////////////////////
  31. static inline void * __cdecl operator new(
  32. size_t size)
  33. {
  34. return ((void *)LocalAlloc(LPTR, size));
  35. }
  36. static inline void __cdecl operator delete(
  37. void *ptr)
  38. {
  39. LocalFree(ptr);
  40. }
  41. extern "C" inline __cdecl _purecall(void)
  42. {
  43. return (0);
  44. }
  45. /**********************************************************************
  46. */
  47. class CPanoseMapper : public IPANOSEMapper {
  48. public:
  49. CPanoseMapper(LPUNKNOWN pUnk);
  50. ~CPanoseMapper(void);
  51. BOOL bInit(void);
  52. /* IUnknown
  53. */
  54. STDMETHODIMP QueryInterface ( REFIID riid, LPVOID FAR* ppvObj);
  55. STDMETHODIMP_(ULONG) AddRef( void );
  56. STDMETHODIMP_(ULONG) Release( void ) ;
  57. /* The PANOSE Mapper interface.
  58. */
  59. STDMETHODIMP_(USHORT) unPANMatchFonts(
  60. LPBYTE lpPanWant,
  61. ULONG ulSizeWant, EW_LPBYTE lpPanThis, ULONG ulSizeThis,
  62. BYTE jMapToFamily);
  63. STDMETHODIMP_(EW_VOID) vPANMakeDummy(
  64. LPBYTE lpPanThis, USHORT unSize ) ;
  65. STDMETHODIMP_(SHORT) nPANGetMapDefault(
  66. LPBYTE lpPanDef,
  67. USHORT unSizePanDef ) ;
  68. STDMETHODIMP_(SHORT) nPANSetMapDefault(
  69. LPBYTE lpPanDef,
  70. USHORT unSizePanDef ) ;
  71. STDMETHODIMP_(BOOL) bPANEnableMapDefault (
  72. BOOL bEnable ) ;
  73. STDMETHODIMP_(BOOL) bPANIsDefaultEnabled( ) ;
  74. STDMETHODIMP_(USHORT) unPANPickFonts (
  75. USHORT FAR *lpIndsBest,
  76. USHORT FAR *lpMatchValues, LPBYTE lpPanWant,
  77. USHORT unNumInds, LPBYTE lpPanFirst, USHORT unNumAvail,
  78. SHORT nRecSize, BYTE jMapToFamily ) ;
  79. STDMETHODIMP_(USHORT) unPANGetMapThreshold( ) ;
  80. STDMETHODIMP_(BOOL) bPANSetMapThreshold (
  81. USHORT unThreshold ) ;
  82. STDMETHODIMP_(BOOL) bPANIsThresholdRelaxed( ) ;
  83. STDMETHODIMP_(VOID) vPANRelaxThreshold( ) ;
  84. STDMETHODIMP_(BOOL) bPANRestoreThreshold( ) ;
  85. STDMETHODIMP_(BOOL) bPANGetMapWeights (
  86. BYTE jFamilyA,
  87. BYTE jFamilyB, LPBYTE lpjWts, LPBOOL lpbIsCustom ) ;
  88. STDMETHODIMP_(BOOL) bPANSetMapWeights (
  89. BYTE jFamilyA,
  90. BYTE jFamilyB, LPBYTE lpjWts ) ;
  91. STDMETHODIMP_(BOOL) bPANClearMapWeights (
  92. BYTE jFamilyA,
  93. BYTE jFamilyB ) ;
  94. private:
  95. LPUNKNOWN m_pUnkOuter; //Controlling unknown
  96. EW_MAPSTATE m_MapState; // Used for Core Mapper.
  97. };
  98. /**********************************************************************
  99. */
  100. class CWrapper : public IUnknown {
  101. public:
  102. CWrapper(LPUNKNOWN pUnk, LPFNDESTROYED pfn);
  103. ~CWrapper(void);
  104. BOOL bInit(void);
  105. /* IUnknown
  106. */
  107. STDMETHODIMP QueryInterface ( REFIID riid, LPVOID FAR* ppvObj);
  108. STDMETHODIMP_(ULONG) AddRef( void );
  109. STDMETHODIMP_(ULONG) Release( void ) ;
  110. private:
  111. ULONG m_ulRef; //Object reference count
  112. LPUNKNOWN m_pUnkOuter; //Controlling unknown
  113. LPFNDESTROYED m_pfnDestroy; // Function to call when destroyed.
  114. CPanoseMapper * m_pPM; // the actual PAN mapper.
  115. };
  116. /**********************************************************************
  117. */
  118. class CImpIClassFactory : public IClassFactory
  119. {
  120. public:
  121. CImpIClassFactory()
  122. { m_ulRef = 0; g_ulRefThisDll++;}
  123. ~CImpIClassFactory() { g_ulRefThisDll--; }
  124. // *** IUnknown methods ***
  125. STDMETHODIMP QueryInterface(REFIID riid, LPVOID FAR* ppvObj);
  126. STDMETHODIMP_(ULONG) AddRef(void);
  127. STDMETHODIMP_(ULONG) Release(void);
  128. // *** IClassFactory methods ***
  129. STDMETHODIMP CreateInstance(LPUNKNOWN pUnkOuter,
  130. REFIID riid,
  131. LPVOID FAR* ppvObject);
  132. STDMETHODIMP LockServer(BOOL fLock);
  133. private:
  134. int m_ulRef;
  135. };
  136. // ******************************************************************
  137. // LibMain
  138. // extern "C" BOOL APIENTRY DllMain(HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved)
  139. STDAPI_(BOOL) DllMain(HINSTANCE hDll, DWORD dwReason, LPVOID lpReserved)
  140. {
  141. switch(dwReason)
  142. {
  143. case DLL_PROCESS_ATTACH:
  144. break;
  145. case DLL_PROCESS_DETACH:
  146. break;
  147. case DLL_THREAD_ATTACH:
  148. break;
  149. case DLL_THREAD_DETACH:
  150. break;
  151. default:
  152. break;
  153. } // switch
  154. return(TRUE);
  155. }
  156. // ******************************************************************
  157. // DllCanUnloadNow
  158. STDAPI DllCanUnloadNow()
  159. {
  160. HRESULT retval;
  161. retval = ResultFromScode((g_ulRefThisDll == 0) &&( g_ulLock == 0 )
  162. &&( g_ulObjs = 0 )
  163. ? S_OK : S_FALSE);
  164. return(retval);
  165. }
  166. //*******************************************************************
  167. // Object destruction notification proc.
  168. void FAR PASCAL vObjectDestroyed(void)
  169. {
  170. g_ulObjs--;
  171. }
  172. // ******************************************************************
  173. // DllGetClassObject
  174. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid,
  175. LPVOID FAR* ppvObj)
  176. {
  177. if(!(rclsid == CLSID_PANOSEMapper))
  178. return(ResultFromScode(E_FAIL));
  179. if(!(riid == IID_IUnknown) && !(riid == IID_IClassFactory))
  180. return(ResultFromScode(E_NOINTERFACE));
  181. *ppvObj = (LPVOID) new CImpIClassFactory;
  182. if(!*ppvObj)
  183. return(ResultFromScode(E_OUTOFMEMORY));
  184. ( (LPUNKNOWN )*ppvObj)->AddRef();
  185. return NOERROR;
  186. }
  187. // ***********************************************************************
  188. // ***********************************************************************
  189. // CImpIClassFactory member functions
  190. // *** IUnknown methods ***
  191. STDMETHODIMP CImpIClassFactory::QueryInterface(REFIID riid, LPVOID FAR* ppvObj)
  192. {
  193. *ppvObj = NULL;
  194. // Any interface on this object is the object pointer
  195. if((riid == IID_IUnknown) ||( riid == IID_IClassFactory ))
  196. *ppvObj =( LPVOID ) this;
  197. if(*ppvObj) {
  198. ( (LPUNKNOWN )*ppvObj)->AddRef();
  199. return NOERROR;
  200. }
  201. return(ResultFromScode(E_NOINTERFACE));
  202. }
  203. STDMETHODIMP_(ULONG) CImpIClassFactory::AddRef(void)
  204. {
  205. return(++m_ulRef);
  206. }
  207. STDMETHODIMP_(ULONG) CImpIClassFactory::Release(void)
  208. {
  209. ULONG retval;
  210. retval = --m_ulRef;
  211. if(!retval) delete this;
  212. return(retval);
  213. }
  214. // *** IClassFactory methods ***
  215. STDMETHODIMP CImpIClassFactory::CreateInstance(LPUNKNOWN pUnkOuter,
  216. REFIID riid, LPVOID FAR* ppvObj)
  217. {
  218. HRESULT retval;
  219. CWrapper * pPM;
  220. *ppvObj=NULL;
  221. retval = ResultFromScode(E_OUTOFMEMORY);
  222. // Verify that a controlling unknown asks for IUnknown
  223. //
  224. if( NULL != pUnkOuter && !(riid == IID_IUnknown))
  225. return( E_NOINTERFACE );
  226. pPM = new CWrapper(pUnkOuter, vObjectDestroyed);
  227. // Increment the object count if we were successful.
  228. //
  229. if(!pPM)
  230. return(retval);
  231. g_ulObjs++;
  232. // Initialize the Panose Mapper. It will destroy itself if initialization
  233. // fails.
  234. //
  235. if(pPM->bInit())
  236. retval = pPM->QueryInterface(riid, ppvObj);
  237. return(retval);
  238. }
  239. STDMETHODIMP CImpIClassFactory::LockServer(BOOL fLock)
  240. {
  241. if(fLock)
  242. g_ulLock++;
  243. else
  244. g_ulLock--;
  245. return(NOERROR);
  246. }
  247. // ***********************************************************************
  248. // ***********************************************************************
  249. // CWrapper member functions
  250. CWrapper::CWrapper( LPUNKNOWN pUnk, LPFNDESTROYED pfn )
  251. : m_pUnkOuter(pUnk),
  252. m_pfnDestroy(pfn),
  253. m_ulRef(0) ,
  254. m_pPM(0)
  255. {
  256. }
  257. CWrapper::~CWrapper(void)
  258. {
  259. // Clean up the mapper.
  260. //
  261. if( m_pPM )
  262. delete m_pPM;
  263. // Notify of destruction.
  264. //
  265. if( NULL != m_pfnDestroy )
  266. ( *m_pfnDestroy )();
  267. }
  268. BOOL CWrapper::bInit(void)
  269. {
  270. BOOL bRet = FALSE;
  271. LPUNKNOWN pUnk = this;
  272. if( m_pUnkOuter )
  273. pUnk = m_pUnkOuter;
  274. m_pPM = new CPanoseMapper(pUnk);
  275. if(m_pPM) {
  276. bRet = m_pPM->bInit();
  277. if( !bRet )
  278. m_pPM = NULL;
  279. }
  280. if( !bRet )
  281. delete this;
  282. return bRet;
  283. }
  284. STDMETHODIMP CWrapper::QueryInterface( REFIID riid, LPVOID FAR* ppvObj )
  285. {
  286. *ppvObj=NULL;
  287. /*
  288. * The only calls for IUnknown are either in a nonaggregated
  289. * case or when created in an aggregation, so in either case
  290. * always return our IUnknown for IID_IUnknown.
  291. */
  292. if( riid == IID_IUnknown )
  293. *ppvObj=(LPVOID)this;
  294. /*
  295. * Return
  296. */
  297. if( riid == IID_IPANOSEMapper )
  298. *ppvObj=(LPVOID)m_pPM;
  299. //AddRef any interface we'll return.
  300. if( NULL!=*ppvObj ) {
  301. ( (LPUNKNOWN )*ppvObj)->AddRef();
  302. return NOERROR;
  303. }
  304. return ResultFromScode(E_NOINTERFACE);
  305. }
  306. STDMETHODIMP_(ULONG) CWrapper::AddRef( void )
  307. {
  308. m_ulRef++;
  309. return m_ulRef;
  310. }
  311. STDMETHODIMP_(ULONG) CWrapper::Release( void )
  312. {
  313. ULONG ulRet = --m_ulRef;
  314. if(( ULONG )0 == ulRet )
  315. delete this;
  316. return ulRet;
  317. }
  318. // ***********************************************************************
  319. // ***********************************************************************
  320. // CPanoseMapper member functions
  321. CPanoseMapper::CPanoseMapper( LPUNKNOWN pUnk )
  322. : m_pUnkOuter(pUnk)
  323. {
  324. }
  325. CPanoseMapper::~CPanoseMapper(void)
  326. {
  327. // Clean up the mapper.
  328. //
  329. ::bPANMapClose( &m_MapState );
  330. }
  331. BOOL CPanoseMapper::bInit(void)
  332. {
  333. // Initialize the MapState struct
  334. //
  335. BOOL bOK =( ::nPANMapInit(&m_MapState, sizeof(m_MapState )) > 0);
  336. // If init fails, then this is not valid. Delete it.
  337. //
  338. if( ! bOK )
  339. delete this;
  340. return bOK;
  341. }
  342. /* IUnknown
  343. */
  344. STDMETHODIMP CPanoseMapper::QueryInterface( REFIID riid, LPVOID FAR* ppvObj )
  345. {
  346. return m_pUnkOuter->QueryInterface(riid, ppvObj);
  347. }
  348. STDMETHODIMP_(ULONG) CPanoseMapper::AddRef( void )
  349. {
  350. return m_pUnkOuter->AddRef();
  351. }
  352. STDMETHODIMP_(ULONG) CPanoseMapper::Release( void )
  353. {
  354. return m_pUnkOuter->Release();
  355. }
  356. /* The PANOSE Mapper interface.
  357. */
  358. STDMETHODIMP_(USHORT) CPanoseMapper::unPANMatchFonts(
  359. LPBYTE lpPanWant,
  360. ULONG ulSizeWant, LPBYTE lpPanThis, ULONG ulSizeThis,
  361. BYTE jMapToFamily)
  362. {
  363. return ::unPANMatchFonts(
  364. &m_MapState,
  365. lpPanWant,
  366. ulSizeWant,
  367. lpPanThis,
  368. ulSizeThis,
  369. jMapToFamily);
  370. }
  371. STDMETHODIMP_(VOID) CPanoseMapper::vPANMakeDummy(
  372. LPBYTE lpPanThis, USHORT unSize )
  373. {
  374. ::vPANMakeDummy( lpPanThis, unSize );
  375. }
  376. STDMETHODIMP_(SHORT) CPanoseMapper::nPANGetMapDefault(
  377. LPBYTE lpPanDef,
  378. USHORT unSizePanDef )
  379. {
  380. return ::nPANGetMapDefault( &m_MapState, lpPanDef, unSizePanDef );
  381. }
  382. STDMETHODIMP_(SHORT) CPanoseMapper::nPANSetMapDefault(
  383. LPBYTE lpPanDef,
  384. USHORT unSizePanDef )
  385. {
  386. return ::nPANSetMapDefault( &m_MapState, lpPanDef, unSizePanDef );
  387. }
  388. STDMETHODIMP_(BOOL) CPanoseMapper::bPANEnableMapDefault (
  389. BOOL bEnable )
  390. {
  391. return ::bPANEnableMapDefault( &m_MapState, (EW_BOOL)bEnable );
  392. }
  393. STDMETHODIMP_(BOOL) CPanoseMapper::bPANIsDefaultEnabled( )
  394. {
  395. return ::bPANIsDefaultEnabled( &m_MapState );
  396. }
  397. STDMETHODIMP_(USHORT) CPanoseMapper::unPANPickFonts (
  398. USHORT FAR * lpIndsBest,
  399. USHORT FAR * lpMatchValues, LPBYTE lpPanWant,
  400. USHORT unNumInds, LPBYTE lpPanFirst, USHORT unNumAvail,
  401. SHORT nRecSize, BYTE jMapToFamily )
  402. {
  403. return ::unPANPickFonts (
  404. &m_MapState,
  405. lpIndsBest,
  406. lpMatchValues,
  407. lpPanWant,
  408. unNumInds,
  409. lpPanFirst,
  410. unNumAvail,
  411. nRecSize,
  412. jMapToFamily );
  413. }
  414. STDMETHODIMP_(USHORT) CPanoseMapper::unPANGetMapThreshold( )
  415. {
  416. return ::unPANGetMapThreshold( &m_MapState );
  417. }
  418. STDMETHODIMP_(BOOL) CPanoseMapper::bPANSetMapThreshold (
  419. USHORT unThreshold )
  420. {
  421. return ::bPANSetMapThreshold( &m_MapState, unThreshold );
  422. }
  423. STDMETHODIMP_(BOOL) CPanoseMapper::bPANIsThresholdRelaxed( )
  424. {
  425. return ::bPANIsThresholdRelaxed( &m_MapState );
  426. }
  427. STDMETHODIMP_(VOID) CPanoseMapper::vPANRelaxThreshold( )
  428. {
  429. ::vPANRelaxThreshold( &m_MapState );
  430. }
  431. STDMETHODIMP_(BOOL) CPanoseMapper::bPANRestoreThreshold( )
  432. {
  433. return ::bPANRestoreThreshold( &m_MapState );
  434. }
  435. STDMETHODIMP_(BOOL) CPanoseMapper::bPANGetMapWeights (
  436. BYTE jFamilyA,
  437. BYTE jFamilyB, LPBYTE lpjWts, LPBOOL lpbIsCustom )
  438. {
  439. *lpbIsCustom =( BOOL )0;
  440. return ::bPANGetMapWeights (&m_MapState, jFamilyA,
  441. jFamilyB, lpjWts,( EW_LPBOOL )lpbIsCustom );
  442. }
  443. STDMETHODIMP_(BOOL) CPanoseMapper::bPANSetMapWeights (
  444. BYTE jFamilyA,
  445. BYTE jFamilyB, LPBYTE lpjWts )
  446. {
  447. return ::bPANSetMapWeights (
  448. &m_MapState, jFamilyA,
  449. jFamilyB, lpjWts );
  450. }
  451. STDMETHODIMP_(BOOL) CPanoseMapper::bPANClearMapWeights (
  452. BYTE jFamilyA,
  453. BYTE jFamilyB )
  454. {
  455. return ::bPANClearMapWeights( &m_MapState, jFamilyA, jFamilyB );
  456. }