| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserv
ed. | |
| 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 "GraphicsContext.h" | |
| 28 | |
| 29 #include "AffineTransform.h" | |
| 30 #include "BitmapInfo.h" | |
| 31 #include "TransformationMatrix.h" | |
| 32 #include "NotImplemented.h" | |
| 33 #include "Path.h" | |
| 34 #include <wtf/MathExtras.h> | |
| 35 | |
| 36 using namespace std; | |
| 37 | |
| 38 namespace WebCore { | |
| 39 | |
| 40 static void fillWithClearColor(HBITMAP bitmap) | |
| 41 { | |
| 42 BITMAP bmpInfo; | |
| 43 GetObject(bitmap, sizeof(bmpInfo), &bmpInfo); | |
| 44 int bufferSize = bmpInfo.bmWidthBytes * bmpInfo.bmHeight; | |
| 45 memset(bmpInfo.bmBits, 0, bufferSize); | |
| 46 } | |
| 47 | |
| 48 #if PLATFORM(WIN) | |
| 49 void GraphicsContext::setShouldIncludeChildWindows(bool include) | |
| 50 { | |
| 51 m_data->m_shouldIncludeChildWindows = include; | |
| 52 } | |
| 53 | |
| 54 bool GraphicsContext::shouldIncludeChildWindows() const | |
| 55 { | |
| 56 return m_data->m_shouldIncludeChildWindows; | |
| 57 } | |
| 58 | |
| 59 GraphicsContext::WindowsBitmap::WindowsBitmap(HDC hdc, const IntSize& size) | |
| 60 : m_hdc(0) | |
| 61 { | |
| 62 BitmapInfo bitmapInfo = BitmapInfo::create(size); | |
| 63 | |
| 64 void* storage = 0; | |
| 65 m_bitmap = CreateDIBSection(0, &bitmapInfo, DIB_RGB_COLORS, &storage, 0, 0); | |
| 66 if (!m_bitmap) | |
| 67 return; | |
| 68 | |
| 69 m_hdc = CreateCompatibleDC(hdc); | |
| 70 SelectObject(m_hdc, m_bitmap); | |
| 71 | |
| 72 m_pixelData.initialize(m_bitmap); | |
| 73 | |
| 74 ASSERT(storage == m_pixelData.buffer()); | |
| 75 | |
| 76 SetGraphicsMode(m_hdc, GM_ADVANCED); | |
| 77 } | |
| 78 | |
| 79 GraphicsContext::WindowsBitmap::~WindowsBitmap() | |
| 80 { | |
| 81 if (!m_bitmap) | |
| 82 return; | |
| 83 | |
| 84 DeleteDC(m_hdc); | |
| 85 DeleteObject(m_bitmap); | |
| 86 } | |
| 87 | |
| 88 PassOwnPtr<GraphicsContext::WindowsBitmap> GraphicsContext::createWindowsBitmap(
const IntSize& size) | |
| 89 { | |
| 90 return adoptPtr(new WindowsBitmap(m_data->m_hdc, size)); | |
| 91 } | |
| 92 #endif | |
| 93 | |
| 94 HDC GraphicsContext::getWindowsContext(const IntRect& dstRect, bool supportAlpha
Blend, bool mayCreateBitmap) | |
| 95 { | |
| 96 // FIXME: Should a bitmap be created also when a shadow is set? | |
| 97 if (mayCreateBitmap && (!m_data->m_hdc || isInTransparencyLayer())) { | |
| 98 if (dstRect.isEmpty()) | |
| 99 return 0; | |
| 100 | |
| 101 // Create a bitmap DC in which to draw. | |
| 102 BitmapInfo bitmapInfo = BitmapInfo::create(dstRect.size()); | |
| 103 | |
| 104 void* pixels = 0; | |
| 105 HBITMAP bitmap = ::CreateDIBSection(NULL, &bitmapInfo, DIB_RGB_COLORS, &
pixels, 0, 0); | |
| 106 if (!bitmap) | |
| 107 return 0; | |
| 108 | |
| 109 HDC bitmapDC = ::CreateCompatibleDC(m_data->m_hdc); | |
| 110 ::SelectObject(bitmapDC, bitmap); | |
| 111 | |
| 112 // Fill our buffer with clear if we're going to alpha blend. | |
| 113 if (supportAlphaBlend) | |
| 114 fillWithClearColor(bitmap); | |
| 115 | |
| 116 // Make sure we can do world transforms. | |
| 117 SetGraphicsMode(bitmapDC, GM_ADVANCED); | |
| 118 | |
| 119 // Apply a translation to our context so that the drawing done will be a
t (0,0) of the bitmap. | |
| 120 XFORM xform = TransformationMatrix().translate(-dstRect.x(), -dstRect.y(
)); | |
| 121 | |
| 122 ::SetWorldTransform(bitmapDC, &xform); | |
| 123 | |
| 124 return bitmapDC; | |
| 125 } | |
| 126 | |
| 127 m_data->flush(); | |
| 128 m_data->save(); | |
| 129 return m_data->m_hdc; | |
| 130 } | |
| 131 | |
| 132 #if PLATFORM(WIN) | |
| 133 void GraphicsContextPlatformPrivate::save() | |
| 134 { | |
| 135 if (!m_hdc) | |
| 136 return; | |
| 137 SaveDC(m_hdc); | |
| 138 } | |
| 139 | |
| 140 void GraphicsContextPlatformPrivate::restore() | |
| 141 { | |
| 142 if (!m_hdc) | |
| 143 return; | |
| 144 RestoreDC(m_hdc, -1); | |
| 145 } | |
| 146 | |
| 147 void GraphicsContextPlatformPrivate::clip(const FloatRect& clipRect) | |
| 148 { | |
| 149 if (!m_hdc) | |
| 150 return; | |
| 151 IntersectClipRect(m_hdc, clipRect.x(), clipRect.y(), clipRect.maxX(), clipRe
ct.maxY()); | |
| 152 } | |
| 153 | |
| 154 void GraphicsContextPlatformPrivate::clip(const Path&) | |
| 155 { | |
| 156 notImplemented(); | |
| 157 } | |
| 158 | |
| 159 void GraphicsContextPlatformPrivate::scale(const FloatSize& size) | |
| 160 { | |
| 161 if (!m_hdc) | |
| 162 return; | |
| 163 | |
| 164 XFORM xform = TransformationMatrix().scaleNonUniform(size.width(), size.heig
ht()); | |
| 165 ModifyWorldTransform(m_hdc, &xform, MWT_LEFTMULTIPLY); | |
| 166 } | |
| 167 | |
| 168 static const double deg2rad = 0.017453292519943295769; // pi/180 | |
| 169 | |
| 170 void GraphicsContextPlatformPrivate::rotate(float degreesAngle) | |
| 171 { | |
| 172 XFORM xform = TransformationMatrix().rotate(degreesAngle); | |
| 173 ModifyWorldTransform(m_hdc, &xform, MWT_LEFTMULTIPLY); | |
| 174 } | |
| 175 | |
| 176 void GraphicsContextPlatformPrivate::translate(float x , float y) | |
| 177 { | |
| 178 if (!m_hdc) | |
| 179 return; | |
| 180 | |
| 181 XFORM xform = TransformationMatrix().translate(x, y); | |
| 182 ModifyWorldTransform(m_hdc, &xform, MWT_LEFTMULTIPLY); | |
| 183 } | |
| 184 | |
| 185 void GraphicsContextPlatformPrivate::concatCTM(const AffineTransform& transform) | |
| 186 { | |
| 187 if (!m_hdc) | |
| 188 return; | |
| 189 | |
| 190 XFORM xform = transform.toTransformationMatrix(); | |
| 191 ModifyWorldTransform(m_hdc, &xform, MWT_LEFTMULTIPLY); | |
| 192 } | |
| 193 | |
| 194 void GraphicsContextPlatformPrivate::setCTM(const AffineTransform& transform) | |
| 195 { | |
| 196 if (!m_hdc) | |
| 197 return; | |
| 198 | |
| 199 XFORM xform = transform.toTransformationMatrix(); | |
| 200 SetWorldTransform(m_hdc, &xform); | |
| 201 } | |
| 202 #endif | |
| 203 | |
| 204 } | |
| OLD | NEW |