initial import
[vuplus_webkit] / Source / WebCore / platform / wx / wxcode / gdiplus / non-kerned-drawing.cpp
1 /*
2  * Copyright (C) 2007 Kevin Watters, Kevin Ollivier.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27
28 #include "AffineTransform.h"
29 #include "GlyphBuffer.h"
30 #include "GraphicsContext.h"
31 #include "SimpleFontData.h"
32
33 #include <wx/defs.h>
34
35 #if 1 // !wxUSE_CAIRO
36
37 #include <wx/dcclient.h>
38 #include <wx/dcgraph.h>
39 #include <wx/gdicmn.h>
40 #include <vector>
41
42 using namespace std;
43
44 //-----------------------------------------------------------------------------
45 // constants
46 //-----------------------------------------------------------------------------
47
48 const double RAD2DEG = 180.0 / M_PI;
49
50 //-----------------------------------------------------------------------------
51 // Local functions
52 //-----------------------------------------------------------------------------
53
54 static inline double dmin(double a, double b) { return a < b ? a : b; }
55 static inline double dmax(double a, double b) { return a > b ? a : b; }
56
57 static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; }
58 static inline double RadToDeg(double deg) { return (deg * 180.0) / M_PI; }
59
60 #include "wx/msw/private.h"
61
62 // TODO remove this dependency (gdiplus needs the macros)
63
64 #ifndef max
65 #define max(a,b)            (((a) > (b)) ? (a) : (b))
66 #endif
67
68 #ifndef min
69 #define min(a,b)            (((a) < (b)) ? (a) : (b))
70 #endif
71
72 #include "gdiplus.h"
73  
74 #if wxUSE_CAIRO
75 #include <cairo.h>
76 #include <cairo-win32.h>
77 #endif
78
79 namespace WebCore {
80
81 void drawTextWithSpacing(GraphicsContext* graphicsContext, const SimpleFontData* font, const wxColour& color, const GlyphBuffer& glyphBuffer, int from, int numGlyphs, const FloatPoint& point)
82 {
83
84
85 #if USE(WXGC)
86     wxGCDC* dc = static_cast<wxGCDC*>(graphicsContext->platformContext());
87 #else
88     wxDC* dc = graphicsContext->platformContext();
89 #endif
90
91     // get the native HDC handle to draw using native APIs
92     HDC hdc = 0;
93     float y = point.y() - font->fontMetrics().ascent();
94     float x = point.x();
95
96
97 #if USE(WXGC)
98
99     wxGraphicsContext* gc = dc->GetGraphicsContext();
100 #if wxUSE_CAIRO
101     cairo_t* context = (cairo_t*)gc->GetNativeContext();
102     if (context) {
103         cairo_surface_t* surface = cairo_get_target(context);
104         hdc = cairo_win32_surface_get_dc(surface);
105     }
106 #else
107     // when going from GdiPlus -> Gdi, any GdiPlus transformations are lost
108     // so we need to alter the coordinates to reflect their transformed point.
109     double xtrans = 0;
110     double ytrans = 0;
111     
112     gc->GetTransform().TransformPoint(&xtrans, &ytrans);
113     Gdiplus::Graphics* g;
114     if (gc) {
115         g = (Gdiplus::Graphics*)gc->GetNativeContext();
116         hdc = g->GetHDC();
117     }
118     x += (int)xtrans;
119     y += (int)ytrans;
120 #endif // wxUSE_CAIRO
121 #else
122     hdc = static_cast<HDC>(dc->GetHDC());
123 #endif
124
125     // if the context has been scaled, we must manually re-apply that scale
126     // to the HDC.
127     FloatSize scale = graphicsContext->currentScale();
128     if (scale != FloatSize(1.0, 1.0)) {
129         SetGraphicsMode(hdc, GM_ADVANCED);
130         XFORM xForm;
131         xForm.eM11 = scale.width();
132         xForm.eM12 = 0.0;
133         xForm.eM21 = 0.0;
134         xForm.eM22 = scale.height();
135         xForm.eDx = 0.0;
136         xForm.eDy = 0.0;
137         SetWorldTransform(hdc, &xForm);
138     }
139     // ExtTextOut wants the offsets as an array of ints, so extract them
140     // from the glyph buffer
141     const GlyphBufferGlyph*   glyphs   = glyphBuffer.glyphs(from);
142     const GlyphBufferAdvance* advances = glyphBuffer.advances(from);
143
144     int* spacing = new int[numGlyphs - from];
145     for (unsigned i = 0; i < numGlyphs; ++i)
146         spacing[i] = advances[i].width();
147
148     wxFont* wxfont = font->getWxFont();
149     if (wxfont && wxfont->IsOk())
150         ::SelectObject(hdc, GetHfontOf(*wxfont));
151
152     if (color.Ok())
153         ::SetTextColor(hdc, color.GetPixel());
154
155     // do not draw background behind characters
156     ::SetBkMode(hdc, TRANSPARENT);
157
158     // draw text with optional character widths array
159     wxString string = wxString((wxChar*)(&glyphs[from]), numGlyphs);
160     ::ExtTextOut(hdc, x, y,  ETO_GLYPH_INDEX, 0, reinterpret_cast<const WCHAR*>(glyphs), numGlyphs, spacing);
161
162     ::SetBkMode(hdc, TRANSPARENT);
163
164     #if USE(WXGC) && !wxUSE_CAIRO
165         g->ReleaseHDC(hdc);
166     #endif
167
168     delete [] spacing;
169 }
170
171 }
172 #endif