Skip to content

Commit 20544d7

Browse files
committed
Fixed test container
1 parent 62ed1d9 commit 20544d7

File tree

5 files changed

+89
-48
lines changed

5 files changed

+89
-48
lines changed

containers/test/Bitmap.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ void Bitmap::draw_rect(int x, int y, int _width, int _height, color color)
5252

5353
void Bitmap::fill_rect(rect rect, color color)
5454
{
55-
for (int y = rect.top(); y < rect.bottom(); y++)
56-
for (int x = rect.left(); x < rect.right(); x++)
55+
for (int y = (int) rect.top(); y < rect.bottom(); y++)
56+
for (int x = (int) rect.left(); x < rect.right(); x++)
5757
set_pixel(x, y, color);
5858
}
5959

containers/test/Font.cpp

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
#include "Bitmap.h"
2+
#include "litehtml/types.h"
13
#define _CRT_SECURE_NO_WARNINGS
24
#include "Font.h"
35
#include "canvas_ity.hpp"
46
using namespace canvas_ity;
57
string readfile(string filename);
6-
void draw_image(canvas& cvs, int x, int y, const Bitmap& bmp);
8+
void draw_image(canvas& cvs, pixel_t x, pixel_t y, const Bitmap& bmp);
79
bool set_font(canvas& cvs, const string& raw_font_data, int pixel_size);
810
void set_color(canvas& cvs, brush_type type, color c);
911

@@ -51,6 +53,12 @@ RasterFont::RasterFont(int size, int weight)
5153
}
5254

5355
load(get_font_dir() + name);
56+
57+
font_size = size;
58+
x_height = get_glyph('x', color(0, 0, 0, 255)).height;
59+
ch_width = get_glyph('0', color(0, 0, 0, 255)).width;
60+
sub_shift = (pixel_t) size / 5;
61+
super_shift = (pixel_t) size / 3;
5462
}
5563

5664
RasterFont* RasterFont::create(string name, int size, int weight)
@@ -65,8 +73,8 @@ Bitmap RasterFont::get_glyph(int ch, color color)
6573
{
6674
if (glyphs[ch].width == 0)
6775
{
68-
Bitmap bmp(width, height, transparent);
69-
bmp.draw_rect(1, 1, width - 2, height - 2, color);
76+
Bitmap bmp(width, (int) height, transparent);
77+
bmp.draw_rect(1, 1, width - 2, (int) height - 2, color);
7078
return bmp;
7179
}
7280
else if (color != black)
@@ -102,9 +110,20 @@ void RasterFont::load(string filename)
102110
auto val = line.substr(sep + 1); trim(val);
103111
if (val.empty()) break; // end of header
104112

105-
if (key == "cell-size") sscanf(val.c_str(), "%d %d", &width, &height);
106-
else if (key == "ascent") ascent = atoi(val.c_str());
107-
else if (key == "descent") descent = atoi(val.c_str());
113+
if (key == "cell-size")
114+
{
115+
int parsed_height;
116+
sscanf(val.c_str(), "%d %d", &width, &parsed_height);
117+
height = (pixel_t) parsed_height;
118+
}
119+
else if (key == "ascent")
120+
{
121+
ascent = atoi(val.c_str());
122+
}
123+
else if (key == "descent")
124+
{
125+
descent = atoi(val.c_str());
126+
}
108127
}
109128

110129
// parse glyphs
@@ -126,7 +145,7 @@ void RasterFont::load(string filename)
126145

127146
auto parse_glyph = [&](int ch) {
128147
int glyph_width = (int)trim(lines[i]).size();
129-
Bitmap& glyph = glyphs[ch] = Bitmap(glyph_width, height, transparent);
148+
Bitmap& glyph = glyphs[ch] = Bitmap(glyph_width, (int) height, transparent);
130149
for (int y = 0; i < (int)lines.size() && y < height; i++, y++)
131150
{
132151
string line = trim(lines[i]);
@@ -148,13 +167,13 @@ void RasterFont::load(string filename)
148167
x_height = glyphs[(int)'x'].find_picture(transparent).height;
149168
}
150169

151-
int RasterFont::text_width(string text)
170+
pixel_t RasterFont::text_width(string text)
152171
{
153172
utf8_to_utf32 utf32(text);
154173
int width_ = 0;
155174
for (const char32_t* p = utf32; *p; p++)
156175
width_ += get_glyph(*p, black).width;
157-
return width_;
176+
return (pixel_t) width_;
158177
}
159178

160179
void RasterFont::draw_text(canvas& cvs, string text, color color, int x, int y)
@@ -191,14 +210,26 @@ OutlineFont::OutlineFont(string name, int size) : name(name), size(size)
191210

192211
canvas cvs;
193212
set_font(cvs, data, size);
194-
cvs.get_font_metrics(ascent, descent, height, x_height);
213+
214+
int metric_ascent;
215+
int metric_descent;
216+
int metric_height;
217+
int metric_x_height;
218+
cvs.get_font_metrics(metric_ascent, metric_descent, metric_height, metric_x_height);
219+
220+
ascent = (pixel_t) metric_ascent;
221+
descent = (pixel_t) metric_descent;
222+
height = (pixel_t) metric_height;
223+
x_height = (pixel_t) metric_x_height;
224+
sub_shift = (pixel_t) size / 5;
225+
super_shift = (pixel_t) size / 3;
195226
}
196227

197-
int OutlineFont::text_width(string text)
228+
pixel_t OutlineFont::text_width(string text)
198229
{
199230
canvas cvs;
200231
set_font(cvs, data, size);
201-
return (int)ceil(cvs.measure_text(text.c_str()));
232+
return cvs.measure_text(text.c_str());
202233
}
203234

204235
void OutlineFont::draw_text(canvas& cvs, string text, color color, int x, int y)

containers/test/Font.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class Font : public font_metrics
44
{
55
public:
66
static Font* create(string face, int size, int weight);
7-
virtual int text_width(string text) = 0;
7+
virtual pixel_t text_width(string text) = 0;
88
virtual void draw_text(canvas& canvas, string text, color color, int x, int y) = 0;
99
};
1010

@@ -21,7 +21,7 @@ class RasterFont : public Font
2121
Bitmap get_glyph(int ch, color color);
2222
void load(string filename);
2323

24-
int text_width(string text) override;
24+
pixel_t text_width(string text) override;
2525
void draw_text(canvas& canvas, string text, color color, int x, int y) override;
2626
};
2727

@@ -36,6 +36,6 @@ class OutlineFont : public Font
3636
static string_map installed_fonts;
3737
static OutlineFont* create(string name, int size);
3838

39-
int text_width(string text) override;
39+
pixel_t text_width(string text) override;
4040
void draw_text(canvas& canvas, string text, color color, int x, int y) override;
4141
};

containers/test/test_container.cpp

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
#include "test_container.h"
22
#include "Font.h"
3+
#include "litehtml/types.h"
4+
#include <fstream>
5+
#include <sstream>
36
#define CANVAS_ITY_IMPLEMENTATION
47
#include "canvas_ity.hpp"
5-
string readfile(string filename);
8+
9+
string readfile(string filename)
10+
{
11+
std::stringstream ss;
12+
std::ifstream(filename) >> ss.rdbuf();
13+
return ss.str();
14+
}
615

716
//
817
// canvas_ity adapters
@@ -54,7 +63,7 @@ void clip_rect(canvas& cvs, rect r)
5463
}
5564

5665
// without scaling
57-
void draw_image(canvas& cvs, int x, int y, const Bitmap& bmp)
66+
void draw_image(canvas& cvs, pixel_t x, pixel_t y, const Bitmap& bmp)
5867
{
5968
cvs.draw_image((byte*)bmp.data.data(), bmp.width, bmp.height, bmp.width * 4, (float)x, (float)y, (float)bmp.width, (float)bmp.height);
6069
}
@@ -87,24 +96,24 @@ void fill_polygon(canvas& cvs, vector<xy> points, color color)
8796
// test_container implementation
8897
//
8998

90-
uint_ptr test_container::create_font(const char* font_families, int size, int weight, font_style /*italic*/, unsigned int /*decoration*/, font_metrics* fm)
99+
uint_ptr test_container::create_font(const font_description& descr, const document*, litehtml::font_metrics* fm)
91100
{
92101
Font* font = 0;
93-
string_vector fonts = split_string(font_families, ",", "", "");
102+
string_vector fonts = split_string(descr.family, ",", "", "");
94103
for (auto name : fonts)
95104
{
96-
font = Font::create(name, size, weight);
105+
font = Font::create(name, (int) descr.size, descr.weight);
97106
if (font) break;
98107
}
99108
if (!font)
100-
font = Font::create(get_default_font_name(), size, weight);
109+
font = Font::create(get_default_font_name(), (int) descr.size, descr.weight);
101110

102111
if (fm) *fm = *font;
103112

104113
return (uint_ptr)font;
105114
}
106115

107-
int test_container::text_width(const char* text, uint_ptr hFont)
116+
pixel_t test_container::text_width(const char* text, uint_ptr hFont)
108117
{
109118
Font* font = (Font*)hFont;
110119
return font->text_width(text);
@@ -113,11 +122,11 @@ int test_container::text_width(const char* text, uint_ptr hFont)
113122
void test_container::draw_text(uint_ptr hdc, const char* text, uint_ptr hFont, web_color color, const position& pos)
114123
{
115124
Font* font = (Font*)hFont;
116-
font->draw_text(*(canvas*)hdc, text, color, pos.x, pos.y);
125+
font->draw_text(*(canvas*)hdc, text, color, (int) pos.x, (int) pos.y);
117126
}
118127

119-
int test_container::pt_to_px(int pt) const { return pt * 96 / 72; }
120-
int test_container::get_default_font_size() const { return 16; }
128+
pixel_t test_container::pt_to_px(pixel_t pt) const { return pt * 96 / 72; }
129+
pixel_t test_container::get_default_font_size() const { return 16; }
121130
const char* test_container::get_default_font_name() const { return "Terminus"; }
122131

123132
void test_container::draw_solid_fill(uint_ptr hdc, const background_layer& layer, const web_color& color)
@@ -128,7 +137,7 @@ void test_container::draw_solid_fill(uint_ptr hdc, const background_layer& layer
128137

129138
void test_container::draw_borders(uint_ptr hdc, const borders& borders, const position& pos, bool /*root*/)
130139
{
131-
canvas img(pos.width, pos.height);
140+
canvas img((int) pos.width, (int) pos.height);
132141
img.global_composite_operation = lighter;
133142

134143
/*
@@ -223,15 +232,15 @@ void test_container::import_css(string& text, const string& url, string& baseurl
223232
text = readfile(baseurl);
224233
}
225234

226-
void test_container::get_client_rect(position& client) const
235+
void test_container::get_viewport(position& client) const
227236
{
228-
client = {0, 0, width, height};
237+
client = {0, 0, (pixel_t) width, (pixel_t) height};
229238
}
230239

231240
void test_container::get_media_features(media_features& media) const
232241
{
233242
position client;
234-
get_client_rect(client);
243+
get_viewport(client);
235244
media.type = media_type_screen;
236245
media.width = client.width;
237246
media.height = client.height;
@@ -251,18 +260,18 @@ void test_container::get_image_size(const char* src, const char* baseurl, size&
251260
{
252261
string url = make_url(src, baseurl);
253262
auto& img = images[url];
254-
sz = {img.width, img.height};
263+
sz = {(pixel_t) img.width, (pixel_t) img.height};
255264
}
256265

257266
void draw_image_pattern(canvas& cvs, const background_layer& bg, const Bitmap& img)
258267
{
259268
cvs.save();
260269
clip_rect(cvs, bg.clip_box);
261270

262-
int x = bg.origin_box.x;
263-
int y = bg.origin_box.y;
264-
int w = bg.origin_box.width;
265-
int h = bg.origin_box.height;
271+
pixel_t x = bg.origin_box.x;
272+
pixel_t y = bg.origin_box.y;
273+
pixel_t w = bg.origin_box.width;
274+
pixel_t h = bg.origin_box.height;
266275

267276
switch (bg.repeat)
268277
{
@@ -286,7 +295,7 @@ void draw_image_pattern(canvas& cvs, const background_layer& bg, const Bitmap& i
286295
while (x > bg.clip_box.left()) x -= w;
287296
while (y > bg.clip_box.top()) y -= h;
288297
for (; x < bg.clip_box.right(); x += w)
289-
for (int _y = y; _y < bg.clip_box.bottom(); _y += h)
298+
for (pixel_t _y = y; _y < bg.clip_box.bottom(); _y += h)
290299
draw_image(cvs, {x, _y, w, h}, img);
291300
break;
292301
}
@@ -330,14 +339,14 @@ void set_gradient(canvas& cvs, const background_layer::conic_gradient& gradient,
330339
template<class Gradient>
331340
void draw_gradient(uint_ptr hdc, const background_layer& bg, const Gradient& gradient)
332341
{
333-
int x = bg.origin_box.x;
334-
int y = bg.origin_box.y;
335-
int w = bg.origin_box.width;
336-
int h = bg.origin_box.height;
342+
pixel_t x = bg.origin_box.x;
343+
pixel_t y = bg.origin_box.y;
344+
pixel_t w = bg.origin_box.width;
345+
pixel_t h = bg.origin_box.height;
337346

338-
canvas img(w, h);
347+
canvas img((int) w, (int) h);
339348

340-
set_gradient(img, gradient, x, y);
349+
set_gradient(img, gradient, (int) x, (int) y);
341350

342351
for (auto cs : gradient.color_points)
343352
add_color_stop(img, fill_style, cs.offset, cs.color, cs.hint);

containers/test/test_container.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <litehtml.h>
22
#include "Bitmap.h"
33
#include "canvas_ity.hpp"
4+
#include "litehtml/types.h"
45
using namespace litehtml;
56
using namespace canvas_ity;
67

@@ -16,12 +17,12 @@ class test_container : public document_container
1617

1718
string make_url(const char* src, const char* baseurl);
1819

19-
uint_ptr create_font(const char* faceName, int size, int weight, font_style italic, unsigned int decoration, font_metrics* fm) override;
20+
uint_ptr create_font(const font_description& descr, const document* doc, litehtml::font_metrics* fm) override;
2021
void delete_font(uint_ptr /*hFont*/) override {}
21-
int text_width(const char* text, uint_ptr hFont) override;
22+
pixel_t text_width(const char* text, uint_ptr hFont) override;
2223
void draw_text(uint_ptr hdc, const char* text, uint_ptr hFont, web_color color, const position& pos) override;
23-
int pt_to_px(int pt) const override;
24-
int get_default_font_size() const override;
24+
pixel_t pt_to_px(pixel_t pt) const override;
25+
pixel_t get_default_font_size() const override;
2526
const char* get_default_font_name() const override;
2627
void load_image(const char* src, const char* baseurl, bool redraw_on_ready) override;
2728
void get_image_size(const char* src, const char* baseurl, size& sz) override;
@@ -49,5 +50,5 @@ class test_container : public document_container
4950
void on_mouse_event(const element::ptr& /*el*/, mouse_event /*event*/) override {};
5051
void set_cursor(const char* /*cursor*/) override {}
5152
void import_css(string& text, const string& url, string& baseurl) override;
52-
void get_client_rect(position& client) const override;
53+
void get_viewport(position& client) const override;
5354
};

0 commit comments

Comments
 (0)