Center distiller results on desktop
[chromium-blink-merge.git] / ui / gfx / gdi_util.cc
blob996d01ee4a4cbfbf50e1ca29e6ca6d017a4972f0
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/gfx/gdi_util.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
10 namespace {
12 void CreateBitmapHeaderWithColorDepth(LONG width,
13 LONG height,
14 WORD color_depth,
15 BITMAPINFOHEADER* hdr) {
16 // These values are shared with gfx::PlatformDevice
17 hdr->biSize = sizeof(BITMAPINFOHEADER);
18 hdr->biWidth = width;
19 hdr->biHeight = -height; // minus means top-down bitmap
20 hdr->biPlanes = 1;
21 hdr->biBitCount = color_depth;
22 hdr->biCompression = BI_RGB; // no compression
23 hdr->biSizeImage = 0;
24 hdr->biXPelsPerMeter = 1;
25 hdr->biYPelsPerMeter = 1;
26 hdr->biClrUsed = 0;
27 hdr->biClrImportant = 0;
30 } // namespace
32 namespace gfx {
34 void CreateBitmapHeader(int width, int height, BITMAPINFOHEADER* hdr) {
35 CreateBitmapHeaderWithColorDepth(width, height, 32, hdr);
38 void CreateBitmapV4Header(int width, int height, BITMAPV4HEADER* hdr) {
39 // Because bmp v4 header is just an extension, we just create a v3 header and
40 // copy the bits over to the v4 header.
41 BITMAPINFOHEADER header_v3;
42 CreateBitmapHeader(width, height, &header_v3);
43 memset(hdr, 0, sizeof(BITMAPV4HEADER));
44 memcpy(hdr, &header_v3, sizeof(BITMAPINFOHEADER));
46 // Correct the size of the header and fill in the mask values.
47 hdr->bV4Size = sizeof(BITMAPV4HEADER);
48 hdr->bV4RedMask = 0x00ff0000;
49 hdr->bV4GreenMask = 0x0000ff00;
50 hdr->bV4BlueMask = 0x000000ff;
51 hdr->bV4AlphaMask = 0xff000000;
54 // Creates a monochrome bitmap header.
55 void CreateMonochromeBitmapHeader(int width,
56 int height,
57 BITMAPINFOHEADER* hdr) {
58 CreateBitmapHeaderWithColorDepth(width, height, 1, hdr);
61 void SubtractRectanglesFromRegion(HRGN hrgn,
62 const std::vector<gfx::Rect>& cutouts) {
63 if (cutouts.size()) {
64 HRGN cutout = ::CreateRectRgn(0, 0, 0, 0);
65 for (size_t i = 0; i < cutouts.size(); i++) {
66 ::SetRectRgn(cutout,
67 cutouts[i].x(),
68 cutouts[i].y(),
69 cutouts[i].right(),
70 cutouts[i].bottom());
71 ::CombineRgn(hrgn, hrgn, cutout, RGN_DIFF);
73 ::DeleteObject(cutout);
77 HRGN ConvertPathToHRGN(const gfx::Path& path) {
78 #if defined(USE_AURA)
79 int point_count = path.getPoints(NULL, 0);
80 scoped_ptr<SkPoint[]> points(new SkPoint[point_count]);
81 path.getPoints(points.get(), point_count);
82 scoped_ptr<POINT[]> windows_points(new POINT[point_count]);
83 for (int i = 0; i < point_count; ++i) {
84 windows_points[i].x = SkScalarRoundToInt(points[i].fX);
85 windows_points[i].y = SkScalarRoundToInt(points[i].fY);
88 return ::CreatePolygonRgn(windows_points.get(), point_count, ALTERNATE);
89 #elif defined(OS_WIN)
90 return path.CreateNativeRegion();
91 #endif
95 float CalculatePageScale(HDC dc, int page_width, int page_height) {
96 int dc_width = GetDeviceCaps(dc, HORZRES);
97 int dc_height = GetDeviceCaps(dc, VERTRES);
99 // If page fits DC - no scaling needed.
100 if (dc_width >= page_width && dc_height >= page_height)
101 return 1.0;
103 float x_factor =
104 static_cast<float>(dc_width) / static_cast<float>(page_width);
105 float y_factor =
106 static_cast<float>(dc_height) / static_cast<float>(page_height);
107 return std::min(x_factor, y_factor);
110 // Apply scaling to the DC.
111 bool ScaleDC(HDC dc, float scale_factor) {
112 SetGraphicsMode(dc, GM_ADVANCED);
113 XFORM xform = {0};
114 xform.eM11 = xform.eM22 = scale_factor;
115 return !!ModifyWorldTransform(dc, &xform, MWT_LEFTMULTIPLY);
118 void StretchDIBits(HDC hdc, int dest_x, int dest_y, int dest_w, int dest_h,
119 int src_x, int src_y, int src_w, int src_h, void* pixels,
120 const BITMAPINFO* bitmap_info) {
121 // When blitting a rectangle that touches the bottom, left corner of the
122 // bitmap, StretchDIBits looks at it top-down! For more details, see
123 // http://wiki.allegro.cc/index.php?title=StretchDIBits.
124 int rv;
125 int bitmap_h = -bitmap_info->bmiHeader.biHeight;
126 int bottom_up_src_y = bitmap_h - src_y - src_h;
127 if (bottom_up_src_y == 0 && src_x == 0 && src_h != bitmap_h) {
128 rv = ::StretchDIBits(hdc,
129 dest_x, dest_h + dest_y - 1, dest_w, -dest_h,
130 src_x, bitmap_h - src_y + 1, src_w, -src_h,
131 pixels, bitmap_info, DIB_RGB_COLORS, SRCCOPY);
132 } else {
133 rv = ::StretchDIBits(hdc,
134 dest_x, dest_y, dest_w, dest_h,
135 src_x, bottom_up_src_y, src_w, src_h,
136 pixels, bitmap_info, DIB_RGB_COLORS, SRCCOPY);
138 DCHECK(rv != GDI_ERROR);
141 } // namespace gfx