Skip to content

Enable processing pixel lengths as float #415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e69755b
Fixed some existing float conversion warnings
DRKV333 Jun 29, 2025
4a9128f
Added special type for pixel lengths
DRKV333 Jun 30, 2025
268cd4b
Merge remote-tracking branch 'origin/master' into FloatPixels
DRKV333 Jun 30, 2025
e077405
Use float as pixel type
DRKV333 Jul 3, 2025
a3a4ed0
Renamed int_int_cache and typed_int
DRKV333 Jul 3, 2025
8c859eb
Removed some unnecessary rounding
DRKV333 Jul 3, 2025
30e5f5e
Updated cairo container to use pixel_t
DRKV333 Jul 4, 2025
ebb69c3
Fixed windows_container not working
DRKV333 Jul 5, 2025
7342cf1
Enabled float conversion warnings
DRKV333 Jul 5, 2025
bf70ed9
Fixed some warnings
DRKV333 Jul 7, 2025
687928f
Fixed windows container to compile with float pixels
DRKV333 Jul 7, 2025
a73d209
Fixed GDI+ container
DRKV333 Jul 8, 2025
b757ca9
Fixed haiku container
DRKV333 Jul 13, 2025
62ed1d9
Fixed ch_width comment
DRKV333 Jul 14, 2025
20544d7
Fixed test container
DRKV333 Jul 14, 2025
d24b177
Don't use pixel_t for pt
DRKV333 Jul 17, 2025
1361d93
Removed unnecessary SetHighColor
DRKV333 Jul 17, 2025
6ce5401
Removed completed TODOs
DRKV333 Jul 17, 2025
9586de2
Removed unnecessary int/float/double casts
DRKV333 Jul 19, 2025
66f6525
Fixed some gcc specific conversion warnings
DRKV333 Jul 19, 2025
10a27a8
Updated docs
DRKV333 Jul 20, 2025
2fd778c
Made list marker width calculation simpler
DRKV333 Jul 21, 2025
4b5aaf2
Implemented "snap as border width" rule
DRKV333 Aug 5, 2025
ab16b1c
Improved cm/mm to pixel conversion precision
DRKV333 Aug 5, 2025
416c34b
Round font sizes, image, text and background position before drawing …
DRKV333 Aug 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ if (MSVC)
add_compile_options(/permissive- /utf-8)
else()
# additional warnings
add_compile_options(-Wall -Wextra -Wpedantic)
add_compile_options(-Wall -Wextra -Wpedantic -Wfloat-conversion)
endif()

add_library(${PROJECT_NAME} ${HEADER_LITEHTML} ${SOURCE_LITEHTML})
Expand Down
16 changes: 8 additions & 8 deletions containers/cairo/cairo_borders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void cairo::border::draw_border()
{
cairo_save(cr);

if(radius_top_x && radius_top_y)
if(radius_top_x != 0 && radius_top_y != 0)
{
double start_angle = M_PI;
double end_angle = start_angle + M_PI / 2.0 / ((double) top_border_width / (double) border_width + 1);
Expand All @@ -69,7 +69,7 @@ void cairo::border::draw_border()
cairo_line_to(cr, left, top);
}

if(radius_bottom_x && radius_bottom_y)
if(radius_bottom_x != 0 && radius_bottom_y != 0)
{
cairo_line_to(cr, left, bottom - radius_bottom_y);

Expand Down Expand Up @@ -131,7 +131,7 @@ void cairo::border::draw_border()

void cairo::border::draw_line(double line_offset, double top_line_offset, double bottom_line_offset)
{
if(radius_top_x && radius_top_y)
if(radius_top_x != 0 && radius_top_y != 0)
{
double end_angle = M_PI;
double start_angle = end_angle + M_PI / 2.0 / ((double) top_border_width / (double) border_width + 1);
Expand All @@ -148,7 +148,7 @@ void cairo::border::draw_line(double line_offset, double top_line_offset, double
cairo_move_to(cr, left + line_offset, top);
}

if(radius_bottom_x && radius_bottom_y)
if(radius_bottom_x != 0 && radius_bottom_y != 0)
{
cairo_line_to(cr, left + line_offset, bottom - radius_bottom_y);

Expand Down Expand Up @@ -224,15 +224,15 @@ void cairo::border::draw_double()

void cairo::border::draw_dashed()
{
int line_length = std::abs(bottom - top);
if(!line_length) return;
litehtml::pixel_t line_length = std::abs(bottom - top);
if(line_length == 0) return;

draw_line(border_width / 2.0,
top_border_width / 2.0,
bottom_border_width / 2.0);

int segment_length = border_width * 3;
int seg_nums = line_length / segment_length;
litehtml::pixel_t segment_length = border_width * 3;
int seg_nums = (int) (line_length / segment_length);
if(seg_nums < 2)
{
seg_nums = 2;
Expand Down
22 changes: 11 additions & 11 deletions containers/cairo/cairo_borders.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ namespace cairo
litehtml::web_color color;
litehtml::border_style style;

int border_width;
int top_border_width;
int bottom_border_width;
litehtml::pixel_t border_width;
litehtml::pixel_t top_border_width;
litehtml::pixel_t bottom_border_width;

int radius_top_x;
int radius_top_y;
int radius_bottom_x;
int radius_bottom_y;
litehtml::pixel_t radius_top_x;
litehtml::pixel_t radius_top_y;
litehtml::pixel_t radius_bottom_x;
litehtml::pixel_t radius_bottom_y;

border(cairo_t* _cr, int _left, int _top, int _bottom) :
border(cairo_t* _cr, litehtml::pixel_t _left, litehtml::pixel_t _top, litehtml::pixel_t _bottom) :
real_side(left_side),
color(),
style(litehtml::border_style_none),
Expand All @@ -54,9 +54,9 @@ namespace cairo

private:
cairo_t* cr;
int left;
int top;
int bottom;
litehtml::pixel_t left;
litehtml::pixel_t top;
litehtml::pixel_t bottom;
void draw_line(double line_offset, double top_line_offset, double bottom_line_offset);
void draw_solid();
void draw_dotted();
Expand Down
8 changes: 4 additions & 4 deletions containers/cairo/conic_gradient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ cairo_pattern_t* create_conic_gradient_pattern(double angle, double radius, cons
points.push_back(color_points[0]);
bg_color_point cp;
cp.offset = 0.5f;
cp.color.red = INTERPOLATE_COLOR(color_points[0].color.red, color_points[1].color.red, 0.5f);
cp.color.green = INTERPOLATE_COLOR(color_points[0].color.green, color_points[1].color.green, 0.5f);
cp.color.blue = INTERPOLATE_COLOR(color_points[0].color.blue, color_points[1].color.blue, 0.5f);
cp.color.alpha = INTERPOLATE_COLOR(color_points[0].color.alpha, color_points[1].color.alpha, 0.5f);
cp.color.red = (litehtml::byte) INTERPOLATE_COLOR(color_points[0].color.red, color_points[1].color.red, 0.5f);
cp.color.green = (litehtml::byte) INTERPOLATE_COLOR(color_points[0].color.green, color_points[1].color.green, 0.5f);
cp.color.blue = (litehtml::byte) INTERPOLATE_COLOR(color_points[0].color.blue, color_points[1].color.blue, 0.5f);
cp.color.alpha = (litehtml::byte) INTERPOLATE_COLOR(color_points[0].color.alpha, color_points[1].color.alpha, 0.5f);
points.push_back(cp);
points.push_back(color_points[1]);
return create_conic_gradient_pattern(angle, radius, points);
Expand Down
96 changes: 50 additions & 46 deletions containers/cairo/container_cairo.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
#include "container_cairo.h"
#include "cairo_borders.h"
#include "conic_gradient.h"
#include "litehtml/html.h"
#include <cmath>

#ifndef M_PI
# define M_PI 3.14159265358979323846
#endif


int container_cairo::pt_to_px(int pt ) const
litehtml::pixel_t container_cairo::pt_to_px(float pt ) const
{
double dpi = get_screen_dpi();

return (int) ((double) pt * dpi / 72.0);
return pt * dpi / 72.0;
}

int container_cairo::get_default_font_size() const
litehtml::pixel_t container_cairo::get_default_font_size() const
{
return pt_to_px(12);
}
Expand Down Expand Up @@ -98,7 +99,7 @@ void container_cairo::clip_background_layer(cairo_t* cr, const litehtml::backgro

void container_cairo::draw_image(litehtml::uint_ptr hdc, const litehtml::background_layer& layer, const std::string& url, const std::string& base_url)
{
if(url.empty() || (!layer.clip_box.width && !layer.clip_box.height) )
if(url.empty() || (layer.clip_box.width == 0 && layer.clip_box.height == 0) )
{
return;
}
Expand All @@ -115,10 +116,13 @@ void container_cairo::draw_image(litehtml::uint_ptr hdc, const litehtml::backgro
auto bgbmp = get_image(img_url);
if (bgbmp)
{
if (layer.origin_box.width != cairo_image_surface_get_width(bgbmp) ||
layer.origin_box.height != cairo_image_surface_get_height(bgbmp))
int image_width = litehtml::round_f(layer.origin_box.width);
int image_height = litehtml::round_f(layer.origin_box.height);

if (image_width != cairo_image_surface_get_width(bgbmp) ||
image_height != cairo_image_surface_get_height(bgbmp))
{
auto new_img = scale_surface(bgbmp, layer.origin_box.width, layer.origin_box.height);
auto new_img = scale_surface(bgbmp, image_width, image_height);
cairo_surface_destroy(bgbmp);
bgbmp = new_img;
}
Expand Down Expand Up @@ -198,17 +202,17 @@ void container_cairo::draw_solid_fill(litehtml::uint_ptr hdc, const litehtml::ba
*/
static void draw_pattern(cairo_t* cr, cairo_pattern_t* pattern,
const litehtml::background_layer& layer,
const std::function<void(cairo_t* cr, cairo_pattern_t* pattern, int x, int y, int width, int height)>& draw)
const std::function<void(cairo_t* cr, cairo_pattern_t* pattern, litehtml::pixel_t x, litehtml::pixel_t y, litehtml::pixel_t width, litehtml::pixel_t height)>& draw)
{
int start_x = layer.origin_box.x;
litehtml::pixel_t start_x = layer.origin_box.x;
int num_x = 1;
int start_y = layer.origin_box.y;
litehtml::pixel_t start_y = layer.origin_box.y;
int num_y = 1;
if(layer.repeat == litehtml::background_repeat_repeat_x || layer.repeat == litehtml::background_repeat_repeat)
{
if(layer.origin_box.left() > layer.clip_box.left())
{
int num_left = (layer.origin_box.left() - layer.clip_box.left()) / layer.origin_box.width;
int num_left = (int) ((layer.origin_box.left() - layer.clip_box.left()) / layer.origin_box.width);
if(layer.origin_box.left() - num_left * layer.origin_box.width > layer.clip_box.left())
{
num_left++;
Expand All @@ -218,7 +222,7 @@ static void draw_pattern(cairo_t* cr, cairo_pattern_t* pattern,
}
if(layer.origin_box.right() < layer.clip_box.right())
{
int num_right = (layer.clip_box.right() - layer.origin_box.right()) / layer.origin_box.width;
int num_right = (int) ((layer.clip_box.right() - layer.origin_box.right()) / layer.origin_box.width);
if(layer.origin_box.left() + num_right * layer.origin_box.width < layer.clip_box.right())
{
num_right++;
Expand All @@ -230,7 +234,7 @@ static void draw_pattern(cairo_t* cr, cairo_pattern_t* pattern,
{
if(layer.origin_box.top() > layer.clip_box.top())
{
int num_top = (layer.origin_box.top() - layer.clip_box.top()) / layer.origin_box.height;
int num_top = (int) ((layer.origin_box.top() - layer.clip_box.top()) / layer.origin_box.height);
if(layer.origin_box.top() - num_top * layer.origin_box.height > layer.clip_box.top())
{
num_top++;
Expand All @@ -240,7 +244,7 @@ static void draw_pattern(cairo_t* cr, cairo_pattern_t* pattern,
}
if(layer.origin_box.bottom() < layer.clip_box.bottom())
{
int num_bottom = (layer.clip_box.bottom() - layer.origin_box.bottom()) / layer.origin_box.height;
int num_bottom = (int) ((layer.clip_box.bottom() - layer.origin_box.bottom()) / layer.origin_box.height);
if(layer.origin_box.bottom() + num_bottom * layer.origin_box.height < layer.clip_box.bottom())
{
num_bottom++;
Expand Down Expand Up @@ -289,7 +293,7 @@ void container_cairo::draw_linear_gradient(litehtml::uint_ptr hdc, const litehtm
color_stop.color.alpha / 255.0);
}

draw_pattern(cr, pattern, layer, [](cairo_t* cr, cairo_pattern_t* pattern, int x, int y, int width, int height)
draw_pattern(cr, pattern, layer, [](cairo_t* cr, cairo_pattern_t* pattern, litehtml::pixel_t x, litehtml::pixel_t y, litehtml::pixel_t width, litehtml::pixel_t height)
{
cairo_set_source(cr, pattern);
cairo_rectangle(cr, x, y, width, height);
Expand Down Expand Up @@ -340,30 +344,30 @@ void container_cairo::draw_borders(litehtml::uint_ptr hdc, const litehtml::borde

cairo_new_path(cr);

int bdr_top = 0;
int bdr_bottom = 0;
int bdr_left = 0;
int bdr_right = 0;
litehtml::pixel_t bdr_top = 0;
litehtml::pixel_t bdr_bottom = 0;
litehtml::pixel_t bdr_left = 0;
litehtml::pixel_t bdr_right = 0;

if(borders.top.width != 0 && borders.top.style > litehtml::border_style_hidden)
{
bdr_top = (int) borders.top.width;
bdr_top = borders.top.width;
}
if(borders.bottom.width != 0 && borders.bottom.style > litehtml::border_style_hidden)
{
bdr_bottom = (int) borders.bottom.width;
bdr_bottom = borders.bottom.width;
}
if(borders.left.width != 0 && borders.left.style > litehtml::border_style_hidden)
{
bdr_left = (int) borders.left.width;
bdr_left = borders.left.width;
}
if(borders.right.width != 0 && borders.right.style > litehtml::border_style_hidden)
{
bdr_right = (int) borders.right.width;
bdr_right = borders.right.width;
}

// draw right border
if(bdr_right)
if(bdr_right != 0)
{
cairo_matrix_t save_matrix;
cairo_get_matrix(cr, &save_matrix);
Expand All @@ -388,7 +392,7 @@ void container_cairo::draw_borders(litehtml::uint_ptr hdc, const litehtml::borde
}

// draw bottom border
if(bdr_bottom)
if(bdr_bottom != 0)
{
cairo_matrix_t save_matrix;
cairo_get_matrix(cr, &save_matrix);
Expand All @@ -413,7 +417,7 @@ void container_cairo::draw_borders(litehtml::uint_ptr hdc, const litehtml::borde
}

// draw top border
if(bdr_top)
if(bdr_top != 0)
{
cairo_matrix_t save_matrix;
cairo_get_matrix(cr, &save_matrix);
Expand All @@ -438,7 +442,7 @@ void container_cairo::draw_borders(litehtml::uint_ptr hdc, const litehtml::borde
}

// draw left border
if(bdr_left)
if(bdr_left != 0)
{
cairo::border border(cr, draw_pos.left(), draw_pos.top(), draw_pos.bottom());
border.real_side = cairo::border::left_side;
Expand Down Expand Up @@ -483,9 +487,9 @@ void container_cairo::apply_clip(cairo_t* cr )
}
}

void container_cairo::draw_ellipse(cairo_t* cr, int x, int y, int width, int height, const litehtml::web_color& color, int line_width )
void container_cairo::draw_ellipse(cairo_t* cr, litehtml::pixel_t x, litehtml::pixel_t y, litehtml::pixel_t width, litehtml::pixel_t height, const litehtml::web_color& color, litehtml::pixel_t line_width )
{
if(!cr || !width || !height) return;
if(!cr || width == 0 || height == 0) return;
cairo_save(cr);

apply_clip(cr);
Expand All @@ -503,9 +507,9 @@ void container_cairo::draw_ellipse(cairo_t* cr, int x, int y, int width, int hei
cairo_restore(cr);
}

void container_cairo::fill_ellipse(cairo_t* cr, int x, int y, int width, int height, const litehtml::web_color& color )
void container_cairo::fill_ellipse(cairo_t* cr, litehtml::pixel_t x, litehtml::pixel_t y, litehtml::pixel_t width, litehtml::pixel_t height, const litehtml::web_color& color )
{
if(!cr || !width || !height) return;
if(!cr || width == 0 || height == 0) return;
cairo_save(cr);

apply_clip(cr);
Expand Down Expand Up @@ -550,7 +554,7 @@ std::shared_ptr<litehtml::element> container_cairo::create_element(const char */
void container_cairo::rounded_rectangle(cairo_t* cr, const litehtml::position &pos, const litehtml::border_radiuses &radius )
{
cairo_new_path(cr);
if(radius.top_left_x && radius.top_left_y)
if(radius.top_left_x != 0 && radius.top_left_y != 0)
{
add_path_arc(cr,
pos.left() + radius.top_left_x,
Expand All @@ -566,7 +570,7 @@ void container_cairo::rounded_rectangle(cairo_t* cr, const litehtml::position &p

cairo_line_to(cr, pos.right() - radius.top_right_x, pos.top());

if(radius.top_right_x && radius.top_right_y)
if(radius.top_right_x != 0 && radius.top_right_y != 0)
{
add_path_arc(cr,
pos.right() - radius.top_right_x,
Expand All @@ -579,7 +583,7 @@ void container_cairo::rounded_rectangle(cairo_t* cr, const litehtml::position &p

cairo_line_to(cr, pos.right(), pos.bottom() - radius.bottom_right_x);

if(radius.bottom_right_x && radius.bottom_right_y)
if(radius.bottom_right_x != 0 && radius.bottom_right_y != 0)
{
add_path_arc(cr,
pos.right() - radius.bottom_right_x,
Expand All @@ -592,7 +596,7 @@ void container_cairo::rounded_rectangle(cairo_t* cr, const litehtml::position &p

cairo_line_to(cr, pos.left() - radius.bottom_left_x, pos.bottom());

if(radius.bottom_left_x && radius.bottom_left_y)
if(radius.bottom_left_x != 0 && radius.bottom_left_y != 0)
{
add_path_arc(cr,
pos.left() + radius.bottom_left_x,
Expand Down Expand Up @@ -622,7 +626,7 @@ cairo_surface_t* container_cairo::scale_surface(cairo_surface_t* surface, int wi
return result;
}

void container_cairo::draw_pixbuf(cairo_t* cr, cairo_surface_t* bmp, int x, int y, int cx, int cy)
void container_cairo::draw_pixbuf(cairo_t* cr, cairo_surface_t* bmp, litehtml::pixel_t x, litehtml::pixel_t y, int cx, int cy)
{
cairo_save(cr);

Expand Down Expand Up @@ -703,7 +707,7 @@ void container_cairo::draw_radial_gradient(litehtml::uint_ptr hdc, const litehtm
color_stop.color.alpha / 255.0);
}

draw_pattern(cr, pattern, layer, [&gradient, &position](cairo_t* cr, cairo_pattern_t* pattern, int x, int y, int w, int h)
draw_pattern(cr, pattern, layer, [&gradient, &position](cairo_t* cr, cairo_pattern_t* pattern, litehtml::pixel_t x, litehtml::pixel_t y, litehtml::pixel_t w, litehtml::pixel_t h)
{
cairo_matrix_t save_matrix;
cairo_get_matrix(cr, &save_matrix);
Expand Down Expand Up @@ -755,16 +759,16 @@ void container_cairo::draw_conic_gradient(litehtml::uint_ptr hdc, const litehtml
position.x -= (float) layer.origin_box.x;
position.y -= (float) layer.origin_box.y;

draw_pattern(cr, pattern, layer, [&position](cairo_t* cr, cairo_pattern_t* pattern, int x, int y, int w, int h)
{
cairo_matrix_t flib_m;
cairo_matrix_init_translate(&flib_m, -(position.x + (float ) x), -(position.y + (float ) y));
cairo_pattern_set_matrix(pattern, &flib_m);
draw_pattern(cr, pattern, layer, [&position](cairo_t* cr, cairo_pattern_t* pattern, litehtml::pixel_t x, litehtml::pixel_t y, litehtml::pixel_t w, litehtml::pixel_t h)
{
cairo_matrix_t flib_m;
cairo_matrix_init_translate(&flib_m, -(position.x + (float ) x), -(position.y + (float ) y));
cairo_pattern_set_matrix(pattern, &flib_m);

cairo_set_source(cr, pattern);
cairo_rectangle(cr, x, y, w, h);
cairo_fill(cr);
}
cairo_set_source(cr, pattern);
cairo_rectangle(cr, x, y, w, h);
cairo_fill(cr);
}
);

cairo_pattern_destroy(pattern);
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy