RefPtr<cairo_t> context = adoptRef(cairo_create(surface.get()));
OwnPtr<WebCore::GraphicsContext> graphicsContext = adoptPtr(new GraphicsContext(context.get()));
- graphicsContext->scale(FloatSize((float)m_size.width() / other->m_size.width(), (float)m_size.height() / other->m_size.height()));
+ float xScale = static_cast<float>(m_size.width()) / other->m_size.width();
+ float yScale = static_cast<float>(m_size.height()) / other->m_size.height();
+
+ // Fit to width. (scale using xScale.)
+ // Copy to center of destination surface.
+ if (xScale != yScale) {
+ float yPosition = (m_size.height() - other->m_size.height() * xScale) / 2;
+ graphicsContext->translate(0, yPosition);
+ }
+ graphicsContext->scale(FloatSize(xScale, xScale));
other->paintCurrentFrameInContext(graphicsContext.get(), IntRect());
}