1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
#include <windows.h>
#include <gdiplus.h>
#include "gdiplus_container.h"
#pragma comment(lib, "gdiplus.lib")
using namespace Gdiplus;
using namespace litehtml;
gdiplus_container::gdiplus_container()
{
GdiplusStartupInput gdiplusStartupInput;
GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
}
gdiplus_container::~gdiplus_container()
{
clear_images();
GdiplusShutdown(m_gdiplusToken);
}
static Color gdiplus_color(web_color color)
{
return Color(color.alpha, color.red, color.green, color.blue);
}
void gdiplus_container::draw_ellipse(HDC hdc, int x, int y, int width, int height, web_color color, int line_width)
{
Graphics graphics(hdc);
graphics.SetCompositingQuality(CompositingQualityHighQuality);
graphics.SetSmoothingMode(SmoothingModeAntiAlias);
Pen pen(gdiplus_color(color));
graphics.DrawEllipse(&pen, x, y, width, height);
}
void gdiplus_container::fill_ellipse(HDC hdc, int x, int y, int width, int height, web_color color)
{
Graphics graphics(hdc);
graphics.SetCompositingQuality(CompositingQualityHighQuality);
graphics.SetSmoothingMode(SmoothingModeAntiAlias);
SolidBrush brush(gdiplus_color(color));
graphics.FillEllipse(&brush, x, y, width, height);
}
void gdiplus_container::fill_rect(HDC hdc, int x, int y, int width, int height, web_color color)
{
Graphics graphics(hdc);
SolidBrush brush(gdiplus_color(color));
graphics.FillRectangle(&brush, x, y, width, height);
}
void gdiplus_container::get_img_size(uint_ptr img, size& sz)
{
Bitmap* bmp = (Bitmap*)img;
if (bmp)
{
sz.width = bmp->GetWidth();
sz.height = bmp->GetHeight();
}
}
void gdiplus_container::free_image(uint_ptr img)
{
Bitmap* bmp = (Bitmap*)img;
delete bmp;
}
void gdiplus_container::draw_img_bg(HDC hdc, uint_ptr img, const background_paint& bg)
{
Bitmap* bgbmp = (Bitmap*)img;
Graphics graphics(hdc);
graphics.SetInterpolationMode(InterpolationModeNearestNeighbor);
graphics.SetPixelOffsetMode(PixelOffsetModeHalf);
Region reg(Rect(bg.border_box.left(), bg.border_box.top(), bg.border_box.width, bg.border_box.height));
graphics.SetClip(®);
Bitmap* scaled_img = nullptr;
if (bg.image_size.width != bgbmp->GetWidth() || bg.image_size.height != bgbmp->GetHeight())
{
scaled_img = new Bitmap(bg.image_size.width, bg.image_size.height);
Graphics gr(scaled_img);
gr.SetPixelOffsetMode(PixelOffsetModeHighQuality);
gr.DrawImage(bgbmp, 0, 0, bg.image_size.width, bg.image_size.height);
bgbmp = scaled_img;
}
switch (bg.repeat)
{
case background_repeat_no_repeat:
{
graphics.DrawImage(bgbmp, bg.position_x, bg.position_y, bgbmp->GetWidth(), bgbmp->GetHeight());
}
break;
case background_repeat_repeat_x:
{
CachedBitmap bmp(bgbmp, &graphics);
int x = bg.position_x;
while(x > bg.clip_box.left()) x -= bgbmp->GetWidth();
for(; x < bg.clip_box.right(); x += bgbmp->GetWidth())
{
graphics.DrawCachedBitmap(&bmp, x, bg.position_y);
}
}
break;
case background_repeat_repeat_y:
{
CachedBitmap bmp(bgbmp, &graphics);
int y = bg.position_y;
while(y > bg.clip_box.top()) y -= bgbmp->GetHeight();
for(; y < bg.clip_box.bottom(); y += bgbmp->GetHeight())
{
graphics.DrawCachedBitmap(&bmp, bg.position_x, y);
}
}
break;
case background_repeat_repeat:
{
CachedBitmap bmp(bgbmp, &graphics);
int x = bg.position_x;
while(x > bg.clip_box.left()) x -= bgbmp->GetWidth();
int y0 = bg.position_y;
while(y0 > bg.clip_box.top()) y0 -= bgbmp->GetHeight();
for(; x < bg.clip_box.right(); x += bgbmp->GetWidth())
{
for(int y = y0; y < bg.clip_box.bottom(); y += bgbmp->GetHeight())
{
graphics.DrawCachedBitmap(&bmp, x, y);
}
}
}
break;
}
delete scaled_img;
}
// length of dash and space for "dashed" style, in multiples of pen width
const float dash = 3;
const float space = 2;
static void draw_horz_border(Graphics& graphics, const border& border, int y, int left, int right)
{
if (border.style != border_style_double || border.width < 3)
{
if (border.width == 1) right--; // 1px-wide lines are longer by one pixel in GDI+ (the endpoint is also drawn)
Pen pen(gdiplus_color(border.color), (float)border.width);
if (border.style == border_style_dotted)
{
float dashValues[2] = { 1, 1 };
pen.SetDashPattern(dashValues, 2);
}
else if (border.style == border_style_dashed)
{
float dashValues[2] = { dash, space };
pen.SetDashPattern(dashValues, 2);
}
graphics.DrawLine(&pen,
Point(left, y + border.width / 2),
Point(right, y + border.width / 2));
}
else
{
int single_line_width = (int)round(border.width / 3.);
if (single_line_width == 1) right--;
Pen pen(gdiplus_color(border.color), (float)single_line_width);
graphics.DrawLine(&pen,
Point(left, y + single_line_width / 2),
Point(right, y + single_line_width / 2));
graphics.DrawLine(&pen,
Point(left, y + border.width - 1 - single_line_width / 2),
Point(right, y + border.width - 1 - single_line_width / 2));
}
}
static void draw_vert_border(Graphics& graphics, const border& border, int x, int top, int bottom)
{
if (border.style != border_style_double || border.width < 3)
{
if (border.width == 1) bottom--;
Pen pen(gdiplus_color(border.color), (float)border.width);
if (border.style == border_style_dotted)
{
float dashValues[2] = { 1, 1 };
pen.SetDashPattern(dashValues, 2);
}
else if (border.style == border_style_dashed)
{
float dashValues[2] = { dash, space };
pen.SetDashPattern(dashValues, 2);
}
graphics.DrawLine(&pen,
Point(x + border.width / 2, top),
Point(x + border.width / 2, bottom));
}
else
{
int single_line_width = (int)round(border.width / 3.);
if (single_line_width == 1) bottom--;
Pen pen(gdiplus_color(border.color), (float)single_line_width);
graphics.DrawLine(&pen,
Point(x + single_line_width / 2, top),
Point(x + single_line_width / 2, bottom));
graphics.DrawLine(&pen,
Point(x + border.width - 1 - single_line_width / 2, top),
Point(x + border.width - 1 - single_line_width / 2, bottom));
}
}
void gdiplus_container::draw_borders(uint_ptr hdc, const borders& borders, const position& draw_pos, bool root)
{
apply_clip((HDC) hdc);
Graphics graphics((HDC)hdc);
if (borders.left.width != 0)
{
draw_vert_border(graphics, borders.left, draw_pos.left(), draw_pos.top(), draw_pos.bottom());
}
if (borders.right.width != 0)
{
draw_vert_border(graphics, borders.right, draw_pos.right() - borders.right.width, draw_pos.top(), draw_pos.bottom());
}
if (borders.top.width != 0)
{
draw_horz_border(graphics, borders.top, draw_pos.top(), draw_pos.left(), draw_pos.right());
}
if (borders.bottom.width != 0)
{
draw_horz_border(graphics, borders.bottom, draw_pos.bottom() - borders.bottom.width, draw_pos.left(), draw_pos.right());
}
release_clip((HDC) hdc);
}
|