1 /* AbiWord
2  * Copyright (C) 1998 AbiSource, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA.
18  */
19 
20 #ifndef GR_WIN32GRAPHICS_H
21 #define GR_WIN32GRAPHICS_H
22 
23 #include <windows.h>
24 #include "ut_misc.h"
25 #include "gr_Graphics.h"
26 #include "gr_Win32CharWidths.h"
27 #include "ut_vector.h"
28 #include "ut_stack.h"
29 #include <wchar.h>
30 #include <winuser.h>
31 
32 class UT_ByteBuf;
33 
34 #define _MAX_CACHE_PENS 64
35 
36 //////////////////////////////////////////////////////////////////
37 //////////////////////////////////////////////////////////////////
38 
39 class ABI_EXPORT GR_Win32Font : public GR_Font
40 {
41 public:
42 	static GR_Win32Font * newFont(LOGFONTW & lf, double fPoints, HDC hdc, HDC printDC);
43 	virtual ~GR_Win32Font();
44 
45 	// need these to allow for adjustements in response to changes of device
setAscent(UT_uint32 n)46 	void        	setAscent(UT_uint32 n)  { m_tm.tmAscent = n; }
setDescent(UT_uint32 n)47 	void 	    setDescent(UT_uint32 n) { m_tm.tmDescent = n; }
setHeight(UT_uint32 n)48 	void         setHeight(UT_uint32 n)  { m_tm.tmHeight = n; }
49 
50 	UT_uint32	getAscent(HDC hdc, HDC printHDC);
51 	UT_uint32	getDescent(HDC hdc, HDC printHDC);
52 	UT_uint32	getHeight(HDC hdc, HDC printHDC);
getUnscaledHeight()53 	UT_uint32   getUnscaledHeight() const { return m_iHeight;}
54 
55 	HFONT       getDisplayFont(GR_Graphics * pGr);
56 
57 	virtual UT_sint32 measureUnremappedCharForCache(UT_UCSChar cChar) const;
58 	UT_sint32	measureUnRemappedChar(UT_UCSChar c, UT_uint32 * height = 0);
59 	virtual GR_CharWidths* newFontWidths(void) const;
60 //
61 // UT_Rect of glyph in Logical units.
62 // rec.left = bearing Left (distance from origin to start)
63 // rec.width = width of the glyph
64 // rec.top = distance from the origin to the top of the glyph
65 // rec.height = total height of the glyph
66 
67 	virtual bool glyphBox(UT_UCS4Char g, UT_Rect & rec, GR_Graphics * pG);
68 
69 	void        selectFontIntoDC(GR_Graphics * pGr, HDC hdc);
70 
markGUIFont()71 	void        markGUIFont() {m_bGUIFont = true;}
isFontGUI()72 	bool        isFontGUI() const {return m_bGUIFont;}
73 
getPrimaryHDC()74 	HDC   getPrimaryHDC() const {return m_hdc;}
getXHDC()75 	HDC   getXHDC() const {return m_xhdc;}
getYHDC()76 	HDC   getYHDC() const {return m_yhdc;}
77 
setPrimaryHDC(HDC hdc)78 	void        setPrimaryHDC(HDC hdc) {m_hdc = hdc;}
setXHDC(HDC hdc)79 	void        setXHDC(HDC hdc) {m_xhdc = hdc;}
setYHDC(HDC hdc)80 	void        setYHDC(HDC hdc) {m_yhdc = hdc;}
81 
82 
83 	// NB: the font handle is one which was associated with this font when it was
84 	// origianlly created; however, it is not necessarily one that is to be used for
85 	// drawing as that has to reflect zoom factor and has to be obtained using
86 	// GR_Win32Font::Acq::getDisplayFont()
87 	// (The handle returned by getFontHandle() can be used for things that are not
88 	// affected by zoom, such as retrieving face names, etc.)
getFontHandle()89 	HFONT       getFontHandle() const {return m_layoutFont;}
getPointSize()90 	double      getPointSize() const {return m_fPointSize;}
91 
92 protected:
93 	// all construction has to be done via the graphics class
94 	GR_Win32Font(LOGFONTW & lf, double fPoints, HDC hdc, HDC printHDC);
95 
_getCharWidths()96 	GR_Win32CharWidths * _getCharWidths() const
97 	{
98 #ifndef ABI_GRAPHICS_PLUGIN_NO_WIDTHS
99 		return reinterpret_cast<GR_Win32CharWidths *>(GR_Font::_getCharWidths());
100 #else
101 		UT_return_val_if_fail(UT_NOT_IMPLEMENTED,NULL);
102 #endif
103 	}
104 
105 	// this function should clear any cached information the font might cary
106 	// it is prinicipally intened to be used when we share fonts between screen and
107 	// printer
_clearAnyCachedInfo()108 	virtual void _clearAnyCachedInfo() {};
109 	void         _updateFontYMetrics(HDC hdc, HDC printHDC);
110 
111 
112 public:
113 	HFONT        getFontFromCache(UT_uint32 pixelsize, bool bIsLayout,
114 								  UT_uint32 zoomPercentage) const;
115 
116 	void         fetchFont(UT_uint32 pixelsize) const;
117 
getTextMetric()118 	const TEXTMETRICW & getTextMetric() const {return m_tm;}
119 
120 private:
121 
122 	struct allocFont
123 	{
124 		UT_uint32			pixelSize;
125 		HFONT			    hFont;
126 	};
127 
128 	void					insertFontInCache(UT_uint32 pixelsize, HFONT pFont) const;
129 
130 	// we will store three different HDC values
131 	// m_hdc is handle to the device on which we are meant to draw
132 	// m_xhdc is handle to the device which was used for obtaining x-axis metrics
133 	// m_yhdc is handle to the device which was used for obtaining y-axis metrics
134 	// we have no control over the lifetime of any of
135 	// these dc's -- we only use these to check that the metrics and other font info is
136 	// uptodate -- they should NEVER be passed to any win32 API
137 
138 	HDC				m_hdc;
139 	HDC				m_xhdc;
140 	HDC				m_yhdc;
141 
142 	UT_uint32				m_defaultCharWidth;
143 	HFONT                   m_layoutFont;
144 	TEXTMETRICW				m_tm;
145 	UT_uint32		        m_iHeight; // unscaled height
146 
147 	// a cache of 'allocFont *' at a given size
148 	mutable UT_Vector		m_allocFonts;
149 	bool                    m_bGUIFont;
150 	double                  m_fPointSize;
151 };
152 
153 //////////////////////////////////////////////////////////////////
154 //////////////////////////////////////////////////////////////////
155 
156 class ABI_EXPORT GR_Win32AllocInfo : public GR_AllocInfo
157 {
158   public:
GR_Win32AllocInfo()159 	GR_Win32AllocInfo():
160 		m_hdc(0), m_hwnd(0), m_pDocInfo(NULL), m_hDevMode(NULL) {};
161 
GR_Win32AllocInfo(HDC hdc,HWND hwnd)162 	GR_Win32AllocInfo(HDC hdc, HWND hwnd):
163 		m_hdc(hdc), m_hwnd(hwnd), m_pDocInfo(NULL), m_hDevMode(NULL) {};
164 
GR_Win32AllocInfo(HDC hdc,const DOCINFOW * pDoc,HGLOBAL devmode)165 	GR_Win32AllocInfo(HDC hdc, const DOCINFOW* pDoc, HGLOBAL devmode):
166 		m_hdc(hdc), m_hwnd(0), m_pDocInfo(pDoc), m_hDevMode(devmode) {};
167 
getType()168 	virtual GR_GraphicsId getType() const {return GRID_WIN32;}
isPrinterGraphics()169 	virtual bool isPrinterGraphics() const {return (m_pDocInfo != 0);}
170 
171 	HDC               m_hdc;
172 	HWND              m_hwnd;
173 	const DOCINFOW *   m_pDocInfo;
174 	HGLOBAL           m_hDevMode;
175 };
176 
177 
178 class ABI_EXPORT GR_Win32Graphics : public GR_Graphics
179 {
180 	// all constructors are protected; instances must be created via
181 	// GR_GraphicsFactory
182 public:
183 	virtual ~GR_Win32Graphics();
184 
s_getClassId()185 	static UT_uint32 s_getClassId() {return GRID_WIN32;}
getClassId()186 	virtual UT_uint32 getClassId() {return s_getClassId();}
187 
getCapability()188 	virtual GR_Capability getCapability() {return GRCAP_SCREEN_AND_PRINTER;}
189 
graphicsDescriptor()190 	static const char *    graphicsDescriptor(){return "Win32 Default";}
191 	static GR_Graphics *   graphicsAllocator(GR_AllocInfo&);
192 
193 	static  GR_Graphics *   getPrinterGraphics(const wchar_t * pPrinterName,
194 											   const wchar_t * pDocName);
195 
196 	virtual void			drawGlyph(UT_uint32 glyph_idx, UT_sint32 xoff, UT_sint32 yoff);
197 	virtual void			drawChar(UT_UCSChar Char, UT_sint32 xoff, UT_sint32 yoff);
198 	virtual void			drawChars(const UT_UCSChar* pChars,
199 									  int iCharOffset, int iLength,
200 									  UT_sint32 xoff, UT_sint32 yoff,
201 									  int * pCharWidth);
202 	virtual void			setFont(const GR_Font* pFont);
clearFont(void)203 	virtual void            clearFont(void) { m_pFont = NULL;}
204 	virtual UT_uint32		getFontHeight();
205 	virtual UT_sint32		measureUnRemappedChar(const UT_UCSChar c, UT_uint32 * height = 0);
206 	virtual void			setColor(const UT_RGBColor& clr);
207 	virtual void            getColor(UT_RGBColor& clr);
208 	virtual GR_Font*		getGUIFont();
209 
210 	virtual UT_uint32		getFontAscent();
211 	virtual UT_uint32		getFontDescent();
212 	virtual void			getCoverage(UT_NumberVector& coverage);
213 	virtual void			drawLine(UT_sint32, UT_sint32, UT_sint32, UT_sint32);
214 	virtual void			xorLine(UT_sint32, UT_sint32, UT_sint32, UT_sint32);
215 	virtual void			setLineWidth(UT_sint32);
216 
217 	virtual void            setLineProperties ( double inWidthPixels,
218 												JoinStyle inJoinStyle = JOIN_MITER,
219 												CapStyle inCapStyle   = CAP_BUTT,
220 												LineStyle inLineStyle = LINE_SOLID );
221 
222 	virtual void			polyLine(UT_Point * pts, UT_uint32 nPoints);
223 	virtual void			fillRect(const UT_RGBColor& c,
224 									 UT_sint32 x, UT_sint32 y,
225 									 UT_sint32 w, UT_sint32 h);
226 	virtual void			invertRect(const UT_Rect* pRect);
227 	virtual void			setClipRect(const UT_Rect* pRect);
228 	virtual void			scroll(UT_sint32 dx, UT_sint32 dy);
229 	virtual void			scroll(UT_sint32 x_dest, UT_sint32 y_dest,
230 								   UT_sint32 x_src, UT_sint32 y_src,
231 								   UT_sint32 width, UT_sint32 height);
232 	virtual void			clearArea(UT_sint32, UT_sint32, UT_sint32, UT_sint32);
233 
234 	virtual void			drawImage(GR_Image* pImg, UT_sint32 xDest, UT_sint32 yDest);
235 	virtual GR_Image*		createNewImage(const char* pszName, const UT_ByteBuf* pBB, const std::string& mimetype,
236 						       UT_sint32 iDisplayWidth, UT_sint32 iDisplayHeight, GR_Image::GRType iType = GR_Image::GRT_Raster);
237 
238 	virtual bool			queryProperties(GR_Graphics::Properties gp) const;
239 
240 	virtual bool			startPrint(void);
241 	virtual bool			startPage(const char * szPageLabel, UT_uint32 pageNumber,
242 									  bool bPortrait, UT_uint32 iWidth, UT_uint32 iHeight);
243 	virtual bool			endPrint(void);
244 
245 	virtual HWND			getHwnd(void) const;
246 
247 	virtual void			setColorSpace(GR_Graphics::ColorSpace c);
248 	virtual GR_Graphics::ColorSpace		getColorSpace(void) const;
249 
250 	virtual void			setCursor(GR_Graphics::Cursor c);
251 	virtual GR_Graphics::Cursor			getCursor(void) const;
252 	virtual void			handleSetCursorMessage(void);
253 
254 	virtual void			setColor3D(GR_Color3D c);
255 	void					init3dColors(void);
256 	virtual void			fillRect(GR_Color3D c,
257 									 UT_sint32 x, UT_sint32 y,
258 									 UT_sint32 w, UT_sint32 h);
259 	virtual void			fillRect(GR_Color3D c, UT_Rect &r);
260     virtual void            polygon(UT_RGBColor& c,UT_Point *pts,UT_uint32 nPoints);
261 	virtual UT_uint32		getFontAscent(const GR_Font *);
262 	virtual UT_uint32		getFontDescent(const GR_Font *);
263 	virtual UT_uint32		getFontHeight(const GR_Font *);
264 
265     virtual GR_Image * genImageFromRectangle(const UT_Rect & r);
266 	virtual void		  saveRectangle(UT_Rect & r, UT_uint32 iIndx);
267 	virtual void		  restoreRectangle(UT_uint32 iIndx);
268 	virtual void 		  flush(void);
setBrush(HBRUSH hBrush)269 	void setBrush(HBRUSH hBrush){ m_hClearBrush = hBrush;};
270 
271 
272 	virtual void          setPrintDC(HDC dc);
getPrintDC()273 	HDC                   getPrintDC() const {return m_printHDC;}
getPrimaryDC()274 	HDC                   getPrimaryDC() const {return m_hdc;}
275 
setPrintDCFontAllocNo(UT_uint32 i)276 	void                  setPrintDCFontAllocNo(UT_uint32 i){m_iPrintDCFontAllocNo = i;}
setDCFontAllocNo(UT_uint32 i)277 	void                  setDCFontAllocNo(UT_uint32 i){m_iDCFontAllocNo = i;}
278 
getXYRatio()279 	double                getXYRatio() const {return m_fXYRatio;}
getXYRatioPrint()280 	double                getXYRatioPrint() const {return m_fXYRatioPrint;}
281 
282 	static bool fixDevMode(HGLOBAL hModDev);
283 
284 	static DOCINFOW *getDocInfo();
285 	static HDC createbestmetafilehdc();
286 
287 protected:
288 	// all instances have to be created via GR_GraphicsFactory; see gr_Graphics.h
289 	GR_Win32Graphics(HDC, HWND);					/* for screen */
290 	GR_Win32Graphics(HDC, const DOCINFOW *, HGLOBAL hDevMode = NULL);	/* for printing */
291 
292 	BITMAPINFO * ConvertDDBToDIB(HBITMAP bitmap, HPALETTE hPal, DWORD dwCompression);
293 
294 	virtual GR_Font*		_findFont(const char* pszFontFamily,
295 									  const char* pszFontStyle,
296 									  const char* pszFontVariant,
297 									  const char* pszFontWeight,
298 									  const char* pszFontStretch,
299 									  const char* pszFontSize,
300 									  const char* pszLang);
301 
302 	virtual UT_uint32 	getDeviceResolution(void) const;
303 	void					_setColor(DWORD clrRef);
304 
305 	HDC m_bufferHdc;
306 	HDC m_dummyHdc;
307 
308 	void _DeviceContext_MeasureBitBltCopySpeed(HDC source, HDC dest, int width, int height);
309 	void getWidthAndHeightFromHWND(HWND h, int &width, int &height);
310 	void _DeviceContext_SwitchToBuffer();
311 	void _DeviceContext_SwitchToScreen();
312 	void _DeviceContext_DrawBufferToScreen();
313 
314 	void _DeviceContext_SuspendDrawing();
315 	void _DeviceContext_ResumeDrawing();
316 
317 	void _DoubleBuffering_SetUpDummyBuffer();
318 	void _DoubleBuffering_ReleaseDummyBuffer();
319 
320 	HDC _DoubleBuffering_CreateBuffer(HDC, int, int);
321 	void _DoubleBuffering_ReleaseBuffer(HDC);
322 
323 	struct _HDCSwitchRecord
324 	{
325 		HDC oldHdc;
_HDCSwitchRecord_HDCSwitchRecord326 		_HDCSwitchRecord(HDC h) : oldHdc(h) { }
327 	};
328 
329 	UT_Stack _HDCSwitchStack;
330 
331 	void _DeviceContext_RestorePrevHDCFromStack();
332 
333 private:
334 	virtual GR_Win32Font * _newFont(LOGFONTW & lf, double fPointSize, HDC hdc, HDC printDC);
335 
336   protected:
337 
338 	UT_uint32               m_iDCFontAllocNo;
339 	UT_uint32               m_iPrintDCFontAllocNo;
340 	HDC						m_hdc;
341 	HDC                     m_printHDC;
342 	static HDC				m_defPrintHDC;
343 	static UT_uint32		s_iInstanceCount;
344 	HWND 					m_hwnd;
345 	const DOCINFOW *			m_pDocInfo;
346 	bool					m_bPrint;
347 	bool					m_bStartPrint;
348 	bool					m_bStartPage;
349 	GR_Win32Font*			m_pFont;
350 	GR_Win32Font*			m_pFontGUI;
351 	UT_sint32				m_iLineWidth;
352     JoinStyle               m_eJoinStyle;
353 	CapStyle                m_eCapStyle;
354 	LineStyle               m_eLineStyle;
355 
356 	GR_Graphics::ColorSpace m_cs;
357 	GR_Graphics::Cursor		m_cursor;
358 
359 	DWORD					m_clrCurrent;
360 	DWORD					m_3dColors[COUNT_3D_COLORS];
361 	int                     m_nPrintLogPixelsY;
362 	double                  m_fXYRatio;
363 	double                  m_fXYRatioPrint;
364 
365 
366 private:
367 	void 					_constructorCommonCode(HDC);
368 	UT_uint16*				_remapGlyphs(const UT_UCSChar* pChars, int iCharOffset, int &iLength);
369 	virtual bool            _setTransform(const GR_Transform & tr);
370 
371 	DWORD					m_clrXorPen;
372 	HPEN					m_hXorPen;
373 
374 	UT_UCS2Char*			m_remapBuffer;
375 	UT_uint32				m_remapBufferSize;
376 	UT_UCS2Char*			m_remapIndices;
377 
378 	UT_RGBColor				m_curColor;
379 	UT_Vector				m_vSaveRect;
380 	UT_Vector 				m_vSaveRectBuf;
381 	HBRUSH					m_hClearBrush;
382 	int						m_nLogPixelsY;
383 	HGLOBAL					m_hDevMode;
384 
385 	typedef struct
386 	{
387 		HPEN 	hPen;
388 		int	 	nStyle;
389 		int 	nWidth;
390 		DWORD	dwColour;
391 
392 	} CACHE_PEN;
393 
394 	CACHE_PEN*				   m_pArPens;
395 	int						   m_nArPenPos;
396 	bool m_bIsPreview;
397 };
398 
399 #endif /* GR_WIN32GRAPHICS_H */
400