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 #include "GlyphBuffer.h" 28 #include "GraphicsContext.h" 29 #include "SimpleFontData.h" 30 31 #include <wx/defs.h> 32 #include <wx/dcclient.h> 33 #include <wx/dcgraph.h> 34 #include <wx/gdicmn.h> 35 #include <vector> 36 37 using namespace std; 38 39 //----------------------------------------------------------------------------- 40 // constants 41 //----------------------------------------------------------------------------- 42 43 const double RAD2DEG = 180.0 / M_PI; 44 45 //----------------------------------------------------------------------------- 46 // Local functions 47 //----------------------------------------------------------------------------- 48 49 static inline double dmin(double a, double b) { return a < b ? a : b; } 50 static inline double dmax(double a, double b) { return a > b ? a : b; } 51 52 static inline double DegToRad(double deg) { return (deg * M_PI) / 180.0; } 53 static inline double RadToDeg(double deg) { return (deg * 180.0) / M_PI; } 54 55 #include "wx/msw/private.h" 56 57 // TODO remove this dependency (gdiplus needs the macros) 58 59 #ifndef max 60 #define max(a,b) (((a) > (b)) ? (a) : (b)) 61 #endif 62 63 #ifndef min 64 #define min(a,b) (((a) < (b)) ? (a) : (b)) 65 #endif 66 67 #include "gdiplus.h" 68 69 70 namespace WebCore { 71 72 void drawTextWithSpacing(GraphicsContext* graphicsContext, const SimpleFontData* font, const wxColour& color, const GlyphBuffer& glyphBuffer, int from, int numGlyphs, const FloatPoint& point) 73 { 74 #if USE(WXGC) 75 wxGCDC* dc = static_cast<wxGCDC*>(graphicsContext->platformContext()); 76 #else 77 wxDC* dc = graphicsContext->platformContext(); 78 #endif 79 80 // get the native HDC handle to draw using native APIs 81 HDC hdc = 0; 82 float y = point.y() - font->fontMetrics().ascent(); 83 float x = point.x(); 84 85 #if USE(WXGC) 86 // when going from GdiPlus -> Gdi, any GdiPlus transformations are lost 87 // so we need to alter the coordinates to reflect their transformed point. 88 double xtrans = 0; 89 double ytrans = 0; 90 91 wxGraphicsContext* gc = dc->GetGraphicsContext(); 92 gc->GetTransform().TransformPoint(&xtrans, &ytrans); 93 Gdiplus::Graphics* g; 94 if (gc) { 95 g = (Gdiplus::Graphics*)gc->GetNativeContext(); 96 hdc = g->GetHDC(); 97 } 98 x += (int)xtrans; 99 y += (int)ytrans; 100 #else 101 hdc = static_cast<HDC>(dc->GetHDC()); 102 #endif 103 104 // ExtTextOut wants the offsets as an array of ints, so extract them 105 // from the glyph buffer 106 const GlyphBufferGlyph* glyphs = glyphBuffer.glyphs(from); 107 const GlyphBufferAdvance* advances = glyphBuffer.advances(from); 108 109 int* spacing = new int[numGlyphs - from]; 110 for (unsigned i = 0; i < numGlyphs; ++i) 111 spacing[i] = advances[i].width(); 112 113 wxFont* wxfont = font->getWxFont(); 114 if (wxfont && wxfont->IsOk()) 115 ::SelectObject(hdc, GetHfontOf(*wxfont)); 116 117 if (color.Ok()) 118 ::SetTextColor(hdc, color.GetPixel()); 119 120 // do not draw background behind characters 121 ::SetBkMode(hdc, TRANSPARENT); 122 123 // draw text with optional character widths array 124 wxString string = wxString((wxChar*)(&glyphs[from]), numGlyphs); 125 ::ExtTextOut(hdc, x, y, ETO_GLYPH_INDEX, 0, reinterpret_cast<const WCHAR*>(glyphs), numGlyphs, spacing); 126 127 ::SetBkMode(hdc, TRANSPARENT); 128 129 #if USE(WXGC) 130 g->ReleaseHDC(hdc); 131 #endif 132 133 delete [] spacing; 134 } 135 136 } 137