summaryrefslogtreecommitdiff
path: root/plugins/NewStory/src/webpage.cpp
blob: a85d0587a915f6d58aa4cccf831f88bb824b117f (plain)
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
/*
Copyright (c) 2005 Victor Pavlychko (nullbyte@sotline.net.ua)
Copyright (C) 2012-24 Miranda NG team (https://miranda-ng.org)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 2
of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "stdafx.h"

/////////////////////////////////////////////////////////////////////////////////////////
// Litehtml interface

struct
{
	const char *name;
	int color_index;
}
static colors[] = {
	{ "ActiveBorder", COLOR_ACTIVEBORDER },
	{ "ActiveCaption", COLOR_ACTIVECAPTION },
	{ "AppWorkspace", COLOR_APPWORKSPACE },
	{ "Background", COLOR_BACKGROUND },
	{ "ButtonFace", COLOR_BTNFACE },
	{ "ButtonHighlight", COLOR_BTNHIGHLIGHT },
	{ "ButtonShadow", COLOR_BTNSHADOW },
	{ "ButtonText", COLOR_BTNTEXT },
	{ "CaptionText", COLOR_CAPTIONTEXT },
	{ "GrayText", COLOR_GRAYTEXT },
	{ "Highlight", COLOR_HIGHLIGHT },
	{ "HighlightText", COLOR_HIGHLIGHTTEXT },
	{ "InactiveBorder", COLOR_INACTIVEBORDER },
	{ "InactiveCaption", COLOR_INACTIVECAPTION },
	{ "InactiveCaptionText", COLOR_INACTIVECAPTIONTEXT },
	{ "InfoBackground", COLOR_INFOBK },
	{ "InfoText", COLOR_INFOTEXT },
	{ "Menu", COLOR_MENU },
	{ "MenuText", COLOR_MENUTEXT },
	{ "Scrollbar", COLOR_SCROLLBAR },
	{ "ThreeDDarkShadow", COLOR_3DDKSHADOW },
	{ "ThreeDFace", COLOR_3DFACE },
	{ "ThreeDHighlight", COLOR_3DHILIGHT },
	{ "ThreeDLightShadow", COLOR_3DLIGHT },
	{ "ThreeDShadow", COLOR_3DSHADOW },
	{ "Window", COLOR_WINDOW },
	{ "WindowFrame", COLOR_WINDOWFRAME },
	{ "WindowText", COLOR_WINDOWTEXT }
};

std::string NSWebPage::resolve_color(const std::string &color) const
{
	char buf[20];

	if (color == "NSText") {
		mir_snprintf(buf, "#%02X%02X%02X", GetRValue(clText), GetGValue(clText), GetBValue(clText));
		return buf;
	}

	for (auto &clr : colors) {
		if (!t_strcasecmp(color.c_str(), clr.name)) {
			char  str_clr[20];
			DWORD rgb_color = GetSysColor(clr.color_index);
			t_snprintf(str_clr, 20, "#%02X%02X%02X", GetRValue(rgb_color), GetGValue(rgb_color), GetBValue(rgb_color));
			return std::move(std::string(str_clr));
		}
	}
	return std::move(std::string());
}

/////////////////////////////////////////////////////////////////////////////////////////

NSWebPage::NSWebPage(NewstoryListData &_1) :
	ctrl(_1)
{
	m_hClipRgn = NULL;
	m_tmp_hdc = GetDC(NULL);

	GdiplusStartupInput gdiplusStartupInput;
	GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);

	EnumFonts(m_tmp_hdc, NULL, EnumFontsProc, (LPARAM)this);
	m_installed_fonts.insert(L"monospace");
	m_installed_fonts.insert(L"serif");
	m_installed_fonts.insert(L"sans-serif");
	m_installed_fonts.insert(L"fantasy");
	m_installed_fonts.insert(L"cursive");
}

NSWebPage::~NSWebPage()
{
	clear_images();
	GdiplusShutdown(m_gdiplusToken);

	if (m_hClipRgn) {
		DeleteObject(m_hClipRgn);
	}
	ReleaseDC(NULL, m_tmp_hdc);
}

/////////////////////////////////////////////////////////////////////////////////////////
// former win32_container

int CALLBACK NSWebPage::EnumFontsProc(const LOGFONT *lplf, const TEXTMETRIC *, DWORD, LPARAM lpData)
{
	NSWebPage *container = (NSWebPage *)lpData;
	container->m_installed_fonts.insert(lplf->lfFaceName);
	return 1;
}

static LPCWSTR get_exact_font_name(LPCWSTR facename)
{
	if (!lstrcmpi(facename, L"monospace"))		return L"Courier New";
	else if (!lstrcmpi(facename, L"serif"))			return L"Times New Roman";
	else if (!lstrcmpi(facename, L"sans-serif"))	return L"Arial";
	else if (!lstrcmpi(facename, L"fantasy"))		return L"Impact";
	else if (!lstrcmpi(facename, L"cursive"))		return L"Comic Sans MS";
	else											return facename;
}

static void trim_quotes(std::string &str)
{
	if (str.front() == '"' || str.front() == '\'')
		str.erase(0, 1);

	if (str.back() == '"' || str.back() == '\'')
		str.erase(str.length() - 1, 1);
}

uint_ptr NSWebPage::create_font(const char *font_list, int size, int weight, font_style italic, unsigned int decoration, font_metrics *fm)
{
	std::wstring font_name;
	string_vector fonts;
	split_string(font_list, fonts, ",");
	bool found = false;
	for (auto &name : fonts) {
		trim(name);
		trim_quotes(name);
		Utf2T wname(name.c_str());
		if (m_installed_fonts.count(wname.get())) {
			font_name = wname;
			found = true;
			break;
		}
	}
	if (!found) font_name = Utf2T(get_default_font_name());
	font_name = get_exact_font_name(font_name.c_str());

	LOGFONT lf = {};
	wcscpy_s(lf.lfFaceName, LF_FACESIZE, font_name.c_str());

	lf.lfHeight = -size;
	lf.lfWeight = weight;
	lf.lfItalic = (italic == font_style_italic) ? TRUE : FALSE;
	lf.lfCharSet = DEFAULT_CHARSET;
	lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
	lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
	lf.lfQuality = DEFAULT_QUALITY;
	lf.lfStrikeOut = (decoration & font_decoration_linethrough) ? TRUE : FALSE;
	lf.lfUnderline = (decoration & font_decoration_underline) ? TRUE : FALSE;
	HFONT hFont = CreateFontIndirect(&lf);

	if (fm) {
		SelectObject(m_tmp_hdc, hFont);
		TEXTMETRIC tm = {};
		GetTextMetrics(m_tmp_hdc, &tm);
		fm->ascent = tm.tmAscent;
		fm->descent = tm.tmDescent;
		fm->height = tm.tmHeight;
		fm->x_height = tm.tmHeight / 2;   // this is an estimate; call GetGlyphOutline to get the real value
		fm->draw_spaces = italic || decoration;
	}

	return (uint_ptr)hFont;
}

void NSWebPage::delete_font(uint_ptr hFont)
{
	DeleteObject((HFONT)hFont);
}

const char *NSWebPage::get_default_font_name() const
{
	return "Times New Roman";
}

int NSWebPage::get_default_font_size() const
{
	return 16;
}

int NSWebPage::text_width(const char *text, uint_ptr hFont)
{
	SIZE size = {};
	SelectObject(m_tmp_hdc, (HFONT)hFont);
	Utf2T wtext(text);
	GetTextExtentPoint32(m_tmp_hdc, wtext, (int)mir_wstrlen(wtext), &size);
	return size.cx;
}

void NSWebPage::draw_text(uint_ptr hdc, const char *text, uint_ptr hFont, web_color color, const position &pos)
{
	apply_clip((HDC)hdc);

	HFONT oldFont = (HFONT)SelectObject((HDC)hdc, (HFONT)hFont);

	SetBkMode((HDC)hdc, TRANSPARENT);

	SetTextColor((HDC)hdc, RGB(color.red, color.green, color.blue));

	RECT rcText = { pos.left(), pos.top(), pos.right(), pos.bottom() };
	DrawText((HDC)hdc, Utf2T(text), -1, &rcText, DT_SINGLELINE | DT_NOPREFIX | DT_BOTTOM | DT_NOCLIP);

	SelectObject((HDC)hdc, oldFont);

	release_clip((HDC)hdc);
}

int NSWebPage::pt_to_px(int pt) const
{
	return MulDiv(pt, GetDeviceCaps(m_tmp_hdc, LOGPIXELSY), 72);
}

void NSWebPage::draw_solid_fill(uint_ptr _hdc, const background_layer &bg, const web_color &color)
{
	HDC hdc = (HDC)_hdc;
	apply_clip(hdc);

	fill_rect(hdc, bg.border_box.x, bg.border_box.y, bg.border_box.width, bg.border_box.height, color);

	release_clip(hdc);
}

void NSWebPage::draw_linear_gradient(uint_ptr, const background_layer &, const background_layer::linear_gradient &)
{}

void NSWebPage::draw_radial_gradient(uint_ptr, const background_layer &, const background_layer::radial_gradient &)
{}

void NSWebPage::draw_conic_gradient(uint_ptr, const background_layer &, const background_layer::conic_gradient &)
{}

void NSWebPage::draw_list_marker(uint_ptr hdc, const list_marker &marker)
{
	apply_clip((HDC)hdc);

	int top_margin = marker.pos.height / 3;
	if (top_margin < 4)
		top_margin = 0;

	int draw_x = marker.pos.x;
	int draw_y = marker.pos.y + top_margin;
	int draw_width = marker.pos.height - top_margin * 2;
	int draw_height = marker.pos.height - top_margin * 2;

	switch (marker.marker_type) {
	case list_style_type_circle:
		{
			draw_ellipse((HDC)hdc, draw_x, draw_y, draw_width, draw_height, marker.color, 1);
		}
		break;
	case list_style_type_disc:
		{
			fill_ellipse((HDC)hdc, draw_x, draw_y, draw_width, draw_height, marker.color);
		}
		break;
	case list_style_type_square:
		{
			fill_rect((HDC)hdc, draw_x, draw_y, draw_width, draw_height, marker.color);
		}
		break;
	}
	release_clip((HDC)hdc);
}

void NSWebPage::make_url_utf8(const char *url, const char *basepath, std::wstring &out)
{
	make_url(Utf2T(url), Utf2T(basepath), out);
}

Bitmap* NSWebPage::load_image(const wchar_t *pwszUrl)
{
	mir_cslockfull lck(m_csImages);
	auto img = m_images.find(pwszUrl);
	if (img != m_images.end() && img->second)
		return (Bitmap *)img->second;

	if (uint_ptr newImg = get_image(pwszUrl, false)) {
		add_image(pwszUrl, newImg);
		return (Bitmap *)newImg;
	}
	return 0;
}

void NSWebPage::load_image(const char *src, const char *baseurl, bool redraw_on_ready)
{
	std::wstring url;
	make_url_utf8(src, baseurl, url);

	mir_cslockfull lck(m_csImages);
	if (m_images.count(url) == 0) {
		lck.unlock();

		if (uint_ptr img = get_image(url.c_str(), redraw_on_ready))
			add_image(url.c_str(), img);
	}
}

void NSWebPage::add_image(LPCWSTR url, uint_ptr img)
{
	mir_cslock lck(m_csImages);
	m_images[url] = img;
}

Bitmap* NSWebPage::find_image(const wchar_t *pwszUrl)
{
	mir_cslock lck(m_csImages);
	auto img = m_images.find(pwszUrl);
	if (img != m_images.end() && img->second)
		return (Bitmap *)img->second;

	return nullptr;
}

void NSWebPage::get_image_size(const char *src, const char *baseurl, size &sz)
{
	std::wstring url;
	make_url_utf8(src, baseurl, url);

	mir_cslock lck(m_csImages);
	images_map::iterator img = m_images.find(url);
	if (img != m_images.end() && img->second)
		get_img_size(img->second, sz);
	else
		sz.width = sz.height = 0;
}

void NSWebPage::clear_images()
{
	mir_cslock lck(m_csImages);
	for (auto &img : m_images) {
		if (img.second) {
			free_image(img.second);
		}
	}
	m_images.clear();
}

void NSWebPage::set_clip(const position &pos, const border_radiuses &)
{
	m_clips.push_back(pos);
}

void NSWebPage::del_clip()
{
	if (!m_clips.empty())
		m_clips.pop_back();
}

void NSWebPage::apply_clip(HDC hdc)
{
	if (m_hClipRgn) {
		DeleteObject(m_hClipRgn);
		m_hClipRgn = NULL;
	}

	if (!m_clips.empty()) {
		POINT ptView = { 0, 0 };
		GetWindowOrgEx(hdc, &ptView);

		position clip_pos = m_clips.back();
		m_hClipRgn = CreateRectRgn(clip_pos.left() - ptView.x, clip_pos.top() - ptView.y, clip_pos.right() - ptView.x, clip_pos.bottom() - ptView.y);
		SelectClipRgn(hdc, m_hClipRgn);
	}
}

void NSWebPage::release_clip(HDC hdc)
{
	SelectClipRgn(hdc, NULL);

	if (m_hClipRgn) {
		DeleteObject(m_hClipRgn);
		m_hClipRgn = NULL;
	}
}

element::ptr NSWebPage::create_element(const char *, const string_map &, const document::ptr &)
{
	return 0;
}

void NSWebPage::get_media_features(media_features &media)  const
{
	position client;
	get_client_rect(client);

	media.type = media_type_screen;
	media.width = client.width;
	media.height = client.height;
	media.color = 8;
	media.monochrome = 0;
	media.color_index = 256;
	media.resolution = GetDeviceCaps(m_tmp_hdc, LOGPIXELSX);
	media.device_width = GetDeviceCaps(m_tmp_hdc, HORZRES);
	media.device_height = GetDeviceCaps(m_tmp_hdc, VERTRES);
}

void NSWebPage::get_language(std::string &language, std::string &culture) const
{
	language = "en";
	culture = "";
}

void NSWebPage::transform_text(std::string &text, text_transform tt)
{
	if (text.empty()) return;

	LPWSTR txt = _wcsdup(Utf2T(text.c_str()));
	switch (tt) {
	case text_transform_capitalize:
		CharUpperBuff(txt, 1);
		break;
	case text_transform_uppercase:
		CharUpperBuff(txt, lstrlen(txt));
		break;
	case text_transform_lowercase:
		CharLowerBuff(txt, lstrlen(txt));
		break;
	}
	text = T2Utf(txt);
	free(txt);
}

void NSWebPage::link(const document::ptr &, const element::ptr &)
{}

/////////////////////////////////////////////////////////////////////////////////////////
// GDI+ part (former gdiplus_container)

static Color gdiplus_color(web_color color)
{
	return Color(color.alpha, color.red, color.green, color.blue);
}

void NSWebPage::draw_ellipse(HDC hdc, int x, int y, int width, int height, web_color color, int)
{
	Graphics graphics(hdc);

	graphics.SetCompositingQuality(CompositingQualityHighQuality);
	graphics.SetSmoothingMode(SmoothingModeAntiAlias);

	Pen pen(gdiplus_color(color));
	graphics.DrawEllipse(&pen, x, y, width, height);
}

void NSWebPage::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 NSWebPage::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 NSWebPage::get_img_size(uint_ptr img, size &sz)
{
	Bitmap *bmp = (Bitmap *)img;
	if (bmp) {
		sz.width = bmp->GetWidth();
		sz.height = bmp->GetHeight();
	}
}

void NSWebPage::free_image(uint_ptr img)
{
	Bitmap *bmp = (Bitmap *)img;
	delete bmp;
}

void NSWebPage::draw_image(uint_ptr _hdc, const background_layer &bg, const std::string &src, const std::string &base_url)
{
	if (src.empty() || (!bg.clip_box.width && !bg.clip_box.height))
		return;

	std::wstring url;
	make_url_utf8(src.c_str(), base_url.c_str(), url);
	
	Bitmap *bgbmp = find_image(url.c_str());
	if (!bgbmp)
		return;

	Graphics graphics((HDC)_hdc);
	graphics.SetInterpolationMode(Gdiplus::InterpolationModeNearestNeighbor);
	graphics.SetPixelOffsetMode(Gdiplus::PixelOffsetModeHalf);

	Region reg(Rect(bg.border_box.left(), bg.border_box.top(), bg.border_box.width, bg.border_box.height));
	graphics.SetClip(&reg);

	Bitmap *scaled_img = nullptr;
	if (bg.origin_box.width != (int)bgbmp->GetWidth() || bg.origin_box.height != (int)bgbmp->GetHeight()) {
		scaled_img = new Bitmap(bg.origin_box.width, bg.origin_box.height);
		Graphics gr(scaled_img);
		gr.SetPixelOffsetMode(Gdiplus::PixelOffsetModeHighQuality);
		gr.DrawImage(bgbmp, 0, 0, bg.origin_box.width, bg.origin_box.height);
		bgbmp = scaled_img;
	}

	switch (bg.repeat) {
	case background_repeat_no_repeat:
		{
			graphics.DrawImage(bgbmp, bg.origin_box.x, bg.origin_box.y, bgbmp->GetWidth(), bgbmp->GetHeight());
		}
		break;
	case background_repeat_repeat_x:
		{
			CachedBitmap bmp(bgbmp, &graphics);
			int x = bg.origin_box.x;
			while (x > bg.clip_box.left()) x -= bgbmp->GetWidth();
			for (; x < bg.clip_box.right(); x += bgbmp->GetWidth()) {
				graphics.DrawCachedBitmap(&bmp, x, bg.origin_box.y);
			}
		}
		break;
	case background_repeat_repeat_y:
		{
			CachedBitmap bmp(bgbmp, &graphics);
			int y = bg.origin_box.y;
			while (y > bg.clip_box.top()) y -= bgbmp->GetHeight();
			for (; y < bg.clip_box.bottom(); y += bgbmp->GetHeight()) {
				graphics.DrawCachedBitmap(&bmp, bg.origin_box.x, y);
			}
		}
		break;
	case background_repeat_repeat:
		{
			CachedBitmap bmp(bgbmp, &graphics);
			int x = bg.origin_box.x;
			while (x > bg.clip_box.left()) x -= bgbmp->GetWidth();
			int y0 = bg.origin_box.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 NSWebPage::draw_borders(uint_ptr hdc, const borders &borders, const position &draw_pos, bool)
{
	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);
}

/////////////////////////////////////////////////////////////////////////////////////////
// NSWebPage own functions

uint_ptr NSWebPage::get_image(LPCWSTR url_or_path, bool)
{
	if (!mir_wstrncmp(url_or_path, L"file://", 7))
		url_or_path += 7;

	auto *pImage = new Gdiplus::Bitmap(url_or_path);
	if (pImage->GetLastStatus() != Ok) {
		delete pImage;
		return 0;
	}
	return (uint_ptr)pImage;
}

void NSWebPage::get_client_rect(position &pos) const
{
	pos = size(ctrl.cachedWindowWidth, ctrl.cachedWindowHeight);
}

void NSWebPage::import_css(std::string &, const std::string &, std::string &)
{
}

void NSWebPage::make_url(LPCWSTR url, LPCWSTR, std::wstring &out)
{
	out = url;
}

void NSWebPage::on_anchor_click(const char *pszUtl, const element::ptr &)
{
	Utils_OpenUrl(pszUtl);
}

void NSWebPage::set_base_url(const char *)
{
}

void NSWebPage::set_caption(const char *)
{
}

void NSWebPage::set_cursor(const char *pszCursor)
{
	if (!mir_strcmp(pszCursor, "pointer"))
		SetCursor(LoadCursor(NULL, IDC_HAND));
	else
		SetCursor(LoadCursor(NULL, IDC_ARROW));
}