Skip to content

Truly independent web browser

UI/Qt: Redraw the chrome icons and fit them to the pixel grid

Andreas Kling5a293de53e50
diff --git a/UI/Qt/Icon.cpp b/UI/Qt/Icon.cpp
index f65696578f2..3fcff201a8b 100644
--- a/UI/Qt/Icon.cpp
+++ b/UI/Qt/Icon.cpp
@@ -6,6 +6,8 @@
#include <AK/HashMap.h>
#include <AK/Math.h>
+#include <AK/QuickSort.h>
+#include <AK/Vector.h>
#include <LibCore/Resource.h>
#include <UI/Qt/ChromeStyle.h>
#include <UI/Qt/Icon.h>
@@ -269,48 +271,57 @@ static QPainterPath parse_svg_path(StringView data)
return path;
}
-// The icon set of the chrome: 16 px on a 16 px grid, stroked 1.5 px wide with round caps and joins.
-static constexpr auto BACK_ICON_PATH = "M10 3.5L5.5 8l4.5 4.5"sv;
-static constexpr auto FORWARD_ICON_PATH = "M6 3.5L10.5 8 6 12.5"sv;
-static constexpr auto RELOAD_ICON_PATH = "M12.89 9.04A5 5 0 1 1 8 3c1.4 0 2.74.56 3.74 1.52L13 5.8M13 2.8v3h-2.8"sv;
-static constexpr auto STOP_ICON_PATH = "M4 4l8 8M12 4l-8 8"sv;
-static constexpr auto SITE_SETTINGS_ICON_PATH = "M2.5 5h6M12.5 5h1M2.5 11h1M7.5 11h6M12.1 5a1.6 1.6 0 1 1-3.2 0a1.6 1.6 0 1 1 3.2 0M7.1 11a1.6 1.6 0 1 1-3.2 0a1.6 1.6 0 1 1 3.2 0"sv;
-static constexpr auto MICROPHONE_ICON_PATH = "M8 2.5a2 2 0 0 1 2 2v3.5a2 2 0 0 1-4 0V4.5a2 2 0 0 1 2-2zM4.5 7.5a3.5 3.5 0 0 0 7 0M8 11v2.5"sv;
-static constexpr auto LOCATION_ICON_PATH = "M8 14.2C5.1 10.9 3.6 8.6 3.6 6.7a4.4 4.4 0 0 1 8.8 0c0 1.9-1.5 4.2-4.4 7.5zM9.6 6.7a1.6 1.6 0 1 1-3.2 0a1.6 1.6 0 1 1 3.2 0"sv;
-static constexpr auto NOTIFICATIONS_ICON_PATH = "M8 2.2a3.8 3.8 0 0 1 3.8 3.8v2.6l1.4 2.4H2.8L4.2 8.6V6A3.8 3.8 0 0 1 8 2.2zM6.6 13.2a1.4 1.4 0 0 0 2.8 0"sv;
+// The icon set of the chrome, drawn on a 16 px grid. Straight lines sit on half pixels, where a one pixel line fills
+// whole pixels, and 45 degree lines run through the corners of pixels, so that a 1x screen needs no fitting at all. A
+// shape with a line through its middle is centered on 7.5 for the same reason. Lines keep a pixel between them.
+static constexpr auto BACK_ICON_PATH = "M10.5 2.5l-5 5 5 5"sv;
+static constexpr auto FORWARD_ICON_PATH = "M5.5 2.5l5 5-5 5"sv;
+static constexpr auto RELOAD_ICON_PATH = "M13.41 9a5.5 5.5 0 0 1-5.41 4.5a5.5 5.5 0 1 1 3.89-9.39L13.5 5.5M13.5 2v3.5H10"sv;
+static constexpr auto SITE_SETTINGS_ICON_PATH = "M1.5 4.5h7M12.5 4.5h1M1.5 11.5h1M6.5 11.5h7M12.5 4.5a2 2 0 1 1-4 0a2 2 0 1 1 4 0M6.5 11.5a2 2 0 1 1-4 0a2 2 0 1 1 4 0"sv;
+static constexpr auto MICROPHONE_ICON_PATH = "M7.5 1.5a2 2 0 0 1 2 2v4a2 2 0 0 1-4 0v-4a2 2 0 0 1 2-2zM2.5 7.5a5 5 0 0 0 10 0M7.5 12.5v2M5.5 14.5h4"sv;
+static constexpr auto LOCATION_ICON_PATH = "M7.5 14.5C4.5 11.3 2.5 8.8 2.5 6.5a5 5 0 0 1 10 0c0 2.3-2 4.8-5 8zM9.5 6.5a2 2 0 1 1-4 0a2 2 0 1 1 4 0"sv;
+static constexpr auto NOTIFICATIONS_ICON_PATH = "M7.5 1.5a4 4 0 0 1 4 4v3l2 3h-13l2-3v-3a4 4 0 0 1 4-4zM5.5 14.5h4"sv;
static constexpr auto CHECK_CIRCLE_ICON_PATH = "M14.5 8a6.5 6.5 0 1 1-13 0a6.5 6.5 0 1 1 13 0M5.2 8.2l2 2 3.8-4.2"sv;
-static constexpr auto STAR_ICON_PATH = "M8 2.3l1.75 3.55 3.9.57-2.83 2.75.67 3.88L8 11.22l-3.49 1.83.67-3.88L2.35 6.42l3.9-.57z"sv;
-static constexpr auto CONTENT_BLOCKING_ICON_PATH = "M8 1.8l5 1.9v4c0 3.1-2.1 5.3-5 6.5-2.9-1.2-5-3.4-5-6.5v-4z"sv;
-static constexpr auto NEW_TAB_ICON_PATH = "M8 3v10M3 8h10"sv;
+static constexpr auto STAR_ICON_PATH = "M7.5 1l2 4.4 4.8.6-3.5 3.3.9 4.8-4.2-2.4-4.2 2.4.9-4.8L.7 6l4.8-.6z"sv;
+static constexpr auto CONTENT_BLOCKING_ICON_PATH = "M7.5 1.5l5 2v4.5c0 3-2 5-5 6.5-3-1.5-5-3.5-5-6.5V3.5z"sv;
+static constexpr auto NEW_TAB_ICON_PATH = "M7.5 2.5v10M2.5 7.5h10"sv;
// A stack of cards: a wide one in front, with the edges of two shorter ones above it.
-static constexpr auto ALL_TABS_ICON_PATH = "M3 7h10a1.5 1.5 0 0 1 1.5 1.5v3.5A1.5 1.5 0 0 1 13 13.5H3A1.5 1.5 0 0 1 1.5 12V8.5A1.5 1.5 0 0 1 3 7zM3.25 4.75h9.5M5 2.5h6"sv;
-static constexpr auto CLOSE_ICON_PATH = "M4.5 4.5l7 7M11.5 4.5l-7 7"sv;
-static constexpr auto TAB_CLOSE_ICON_PATH = "M4 4l8 8M12 4l-8 8"sv;
-static constexpr auto CHEVRON_DOWN_ICON_PATH = "M4.5 6.5L8 10l3.5-3.5"sv;
-static constexpr auto CHEVRON_UP_ICON_PATH = "M4.5 9.5L8 6l3.5 3.5"sv;
-static constexpr auto SIDEBAR_ICON_PATH = "M4 3h8a2 2 0 0 1 2 2v6a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2zM6 3v10"sv;
-static constexpr auto DOWNLOAD_ICON_PATH = "M8 3v6.5M5 6.8L8 9.8l3-3M3.5 12.5h9"sv;
-static constexpr auto AUDIO_ICON_PATH = "M2.5 6h2.5L8.5 3v10L5 10H2.5zM11 5.5a3.5 3.5 0 0 1 0 5"sv;
-static constexpr auto MUTED_ICON_PATH = "M2.5 6h2.5L8.5 3v10L5 10H2.5zM11 6l3.5 4M14.5 6L11 10"sv;
-static constexpr auto WARNING_ICON_PATH = "M8 2.5l6 11H2zM8 6.8v3M8 11.7v.1"sv;
-static constexpr auto SEARCH_ICON_PATH = "M7 2.5a4.5 4.5 0 1 1 0 9a4.5 4.5 0 1 1 0-9zM10.5 10.5L14 14"sv;
-static constexpr auto ZOOM_OUT_ICON_PATH = "M3 8h10"sv;
-static constexpr auto PAUSE_ICON_PATH = "M6 4v8M10 4v8"sv;
-static constexpr auto GLOBE_ICON_PATH = "M8 2.5a5.5 5.5 0 1 1 0 11a5.5 5.5 0 1 1 0-11zM2.5 8h11M8 2.5c-1.6 1.6-2.4 3.4-2.4 5.5s.8 3.9 2.4 5.5M8 2.5c1.6 1.6 2.4 3.4 2.4 5.5s-.8 3.9-2.4 5.5"sv;
-static constexpr auto DOCUMENT_ICON_PATH = "M3.5 2.5h6l3 3v8h-9zM9.5 2.5v3h3"sv;
-static constexpr auto NETWORK_ICON_PATH = "M2.5 8a5.5 5.5 0 1 0 11 0a5.5 5.5 0 1 0-11 0M2.5 8h11M8 2.5c1.6 1.6 2.4 3.4 2.4 5.5S9.6 11.9 8 13.5M8 2.5C6.4 4.1 5.6 5.9 5.6 8s.8 3.9 2.4 5.5"sv;
-static constexpr auto COMPOSITOR_ICON_PATH = "M2.5 4h7v5h-7zM6.5 7h7v5h-7z"sv;
-static constexpr auto IMAGE_ICON_PATH = "M2.5 3.5h11v9h-11zM2.5 10.5l3-3 3 3 2-2 3 3M10.5 6.2v.1"sv;
-static constexpr auto FOLDER_ICON_PATH = "M2.5 4.5a1 1 0 0 1 1-1h3l1.5 1.5h4.5a1 1 0 0 1 1 1v6a1 1 0 0 1-1 1h-9a1 1 0 0 1-1-1z"sv;
+static constexpr auto ALL_TABS_ICON_PATH = "M2.5 6.5h11a1 1 0 0 1 1 1v5a1 1 0 0 1-1 1h-11a1 1 0 0 1-1-1v-5a1 1 0 0 1 1-1zM3.5 4.5h9M5.5 2.5h5"sv;
+static constexpr auto CLOSE_ICON_PATH = "M3.5 3.5l8 8M11.5 3.5l-8 8"sv;
+static constexpr auto CHEVRON_DOWN_ICON_PATH = "M3.5 5.5l4 4 4-4"sv;
+static constexpr auto CHEVRON_UP_ICON_PATH = "M3.5 9.5l4-4 4 4"sv;
+static constexpr auto SIDEBAR_ICON_PATH = "M3.5 2.5h9a2 2 0 0 1 2 2v7a2 2 0 0 1-2 2h-9a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2zM5.5 2.5v11"sv;
+static constexpr auto DOWNLOAD_ICON_PATH = "M7.5 1.5v9M3.5 6.5l4 4 4-4M2.5 13.5h10"sv;
+static constexpr auto AUDIO_ICON_PATH = "M1.5 5.5h2l4-3v10l-4-3h-2zM10.5 4.5c1.3.8 2 1.8 2 3s-.7 2.2-2 3"sv;
+static constexpr auto MUTED_ICON_PATH = "M1.5 5.5h2l4-3v10l-4-3h-2zM10.5 5.5l4 4M14.5 5.5l-4 4"sv;
+static constexpr auto WARNING_ICON_PATH = "M7.5 1.5l6.5 12H1zM7.5 5.5v4M7.5 11.5v.01"sv;
+static constexpr auto SEARCH_ICON_PATH = "M6.5 1.5a5 5 0 1 1 0 10a5 5 0 1 1 0-10zM10.5 10.5l4 4"sv;
+static constexpr auto ZOOM_OUT_ICON_PATH = "M2.5 7.5h10"sv;
+static constexpr auto PAUSE_ICON_PATH = "M5.5 3.5v9M10.5 3.5v9"sv;
+static constexpr auto GLOBE_ICON_PATH = "M7.5 1.5a6 6 0 1 1 0 12a6 6 0 1 1 0-12zM1.5 7.5h12M7.5 1.5c-2 1.6-3 3.6-3 6s1 4.4 3 6M7.5 1.5c2 1.6 3 3.6 3 6s-1 4.4-3 6"sv;
+static constexpr auto MOON_ICON_PATH = "M13.39 9.15A6 6 0 1 1 6.54 2.08a5 5 0 0 0 6.85 7.07z"sv;
+static constexpr auto DOCUMENT_ICON_PATH = "M3.5 1.5h5l4 4v9h-9zM8.5 1.5v4h4"sv;
+static constexpr auto COMPOSITOR_ICON_PATH = "M5.5 6.5h9v7h-9zM5.5 9.5h-4v-7h9v4"sv;
+static constexpr auto IMAGE_ICON_PATH = "M2.5 2.5h11a1 1 0 0 1 1 1v9a1 1 0 0 1-1 1h-11a1 1 0 0 1-1-1v-9a1 1 0 0 1 1-1zM1.5 11.5l3.5-3.5 5.5 5.5M9.5 9.5l2-2 3 3M10.5 5.5v.01"sv;
+static constexpr auto FOLDER_ICON_PATH = "M2.5 2.5h3l2 2h6a1 1 0 0 1 1 1v7a1 1 0 0 1-1 1h-11a1 1 0 0 1-1-1v-9a1 1 0 0 1 1-1z"sv;
// The window controls of Windows and Linux are drawn on a 10 px box in the middle of the icon.
-static constexpr auto WINDOW_MINIMIZE_ICON_PATH = "M3 8.5h10"sv;
-static constexpr auto WINDOW_MAXIMIZE_ICON_PATH = "M5 3.5h6a1.5 1.5 0 0 1 1.5 1.5v6a1.5 1.5 0 0 1-1.5 1.5H5A1.5 1.5 0 0 1 3.5 11V5A1.5 1.5 0 0 1 5 3.5z"sv;
-static constexpr auto WINDOW_RESTORE_ICON_PATH = "M4.5 6h5a1 1 0 0 1 1 1v5a1 1 0 0 1-1 1h-5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1zM6 3.5h5.5a1 1 0 0 1 1 1V10"sv;
+static constexpr auto WINDOW_MINIMIZE_ICON_PATH = "M3.5 8.5h9"sv;
+static constexpr auto WINDOW_MAXIMIZE_ICON_PATH = "M4.5 3.5h7a1 1 0 0 1 1 1v7a1 1 0 0 1-1 1h-7a1 1 0 0 1-1-1v-7a1 1 0 0 1 1-1z"sv;
+static constexpr auto WINDOW_RESTORE_ICON_PATH = "M4.5 5.5h5a1 1 0 0 1 1 1v5a1 1 0 0 1-1 1h-5a1 1 0 0 1-1-1v-5a1 1 0 0 1 1-1zM5.5 3.5h6a1 1 0 0 1 1 1v6"sv;
static constexpr auto WINDOW_CLOSE_ICON_PATH = "M3.5 3.5l9 9M12.5 3.5l-9 9"sv;
+// The line of the icons, and the finer one of the window controls.
static constexpr qreal CHROME_ICON_STROKE_WIDTH = 1.5;
-static constexpr qreal TOOLBAR_ICON_STROKE_WIDTH = 1.4;
+static constexpr qreal WINDOW_CONTROL_STROKE_WIDTH = 1;
+// Icons between these sizes all keep the same line, so that they look alike side by side. A smaller one needs a finer
+// line to keep its shape, and a larger one is shown as a feature, with its line growing along with it.
+static constexpr int SMALLEST_ICON_SIZE_WITH_FIXED_STROKE = 14;
+static constexpr int LARGEST_ICON_SIZE_WITH_FIXED_STROKE = 18;
+// How wide a one pixel line is drawn where it doesn't run along the pixels.
+static constexpr qreal SLANTED_ONE_PIXEL_LINE_WIDTH = 1.3;
+// How far across a curve can reach, in device pixels, and still be a rounded corner rather than a line of its own.
+static constexpr qreal LONGEST_ROUNDED_CORNER = 5;
static StringView chrome_icon_path(ChromeIcon icon)
{
@@ -321,16 +332,14 @@ static StringView chrome_icon_path(ChromeIcon icon)
return FORWARD_ICON_PATH;
case ChromeIcon::Reload:
return RELOAD_ICON_PATH;
- case ChromeIcon::Stop:
- return STOP_ICON_PATH;
case ChromeIcon::NewTab:
return NEW_TAB_ICON_PATH;
case ChromeIcon::AllTabs:
return ALL_TABS_ICON_PATH;
+ case ChromeIcon::Stop:
case ChromeIcon::Close:
- return CLOSE_ICON_PATH;
case ChromeIcon::TabClose:
- return TAB_CLOSE_ICON_PATH;
+ return CLOSE_ICON_PATH;
case ChromeIcon::Star:
case ChromeIcon::StarFilled:
return STAR_ICON_PATH;
@@ -343,7 +352,7 @@ static StringView chrome_icon_path(ChromeIcon icon)
case ChromeIcon::Document:
return DOCUMENT_ICON_PATH;
case ChromeIcon::Network:
- return NETWORK_ICON_PATH;
+ return GLOBE_ICON_PATH;
case ChromeIcon::Compositor:
return COMPOSITOR_ICON_PATH;
case ChromeIcon::Image:
@@ -361,7 +370,7 @@ static StringView chrome_icon_path(ChromeIcon icon)
case ChromeIcon::ContentBlocking:
return CONTENT_BLOCKING_ICON_PATH;
case ChromeIcon::Moon:
- return "M13.5 9.5A5.8 5.8 0 0 1 6.5 2.5A5.8 5.8 0 1 0 13.5 9.5Z"sv;
+ return MOON_ICON_PATH;
case ChromeIcon::SiteSettings:
return SITE_SETTINGS_ICON_PATH;
case ChromeIcon::Microphone:
@@ -412,107 +421,307 @@ static QPainterPath const& chrome_icon_painter_path(ChromeIcon icon)
});
}
-// On a 1x screen a 1.5 px line cannot land on whole pixels and smears, so the small icons of the chrome have a second
-// drawing for it: 1 px lines on half pixels, so that their straight edges are crisp, and a 1.25 px line for the diagonals
-// of a cross. The drawings are on the same 16 px canvas.
-struct PixelSnappedChromeIcon {
- StringView path;
- qreal stroke_width { 1 };
+static QPixmap create_transparent_icon_pixmap(QSize logical_size, qreal device_pixel_ratio)
+{
+ QPixmap pixmap(physical_size_for_device_pixel_ratio(logical_size, device_pixel_ratio));
+ pixmap.setDevicePixelRatio(device_pixel_ratio);
+ pixmap.fill(Qt::transparent);
+ return pixmap;
+}
+
+static bool coordinates_match(qreal a, qreal b)
+{
+ return abs(a - b) < 0.001;
+}
+
+// The lines of an icon that fitting should put on the pixel grid, as positions along each axis.
+struct IconStems {
+ Vector<qreal> vertical;
+ Vector<qreal> horizontal;
+ // Whether the icon has nothing but 45 degree lines, as a cross or a chevron does.
+ bool is_diagonal { true };
+};
+
+static IconStems find_icon_stems(QPainterPath const& path)
+{
+ IconStems stems;
+ auto add = [](Vector<qreal>& positions, qreal position) {
+ if (!positions.find_if([&](auto other) { return coordinates_match(other, position); }).is_end())
+ return;
+ positions.append(position);
+ };
+ auto point_at = [&](int index) -> QPointF { return path.elementAt(index); };
+
+ auto element_count = path.elementCount();
+ for (int index = 0; index < element_count; ++index) {
+ auto const& element = path.elementAt(index);
+
+ if (element.isLineTo()) {
+ auto from = point_at(index - 1);
+ auto to = point_at(index);
+ auto is_vertical = coordinates_match(from.x(), to.x());
+ auto is_horizontal = coordinates_match(from.y(), to.y());
+ if (is_vertical)
+ add(stems.vertical, to.x());
+ if (is_horizontal)
+ add(stems.horizontal, to.y());
+ if (!coordinates_match(abs(from.x() - to.x()), abs(from.y() - to.y())))
+ stems.is_diagonal = false;
+
+ // The ends of a line that stops in the open are put on the grid too, so that it ends on a whole pixel.
+ // NB: A dot is a line too short to see, and has to keep its length.
+ auto is_dot = abs(from.x() - to.x()) + abs(from.y() - to.y()) < 0.5;
+ auto starts_open = path.elementAt(index - 1).isMoveTo();
+ auto ends_open = index + 1 == element_count || path.elementAt(index + 1).isMoveTo();
+ if (is_vertical) {
+ if (starts_open)
+ add(stems.horizontal, from.y());
+ if (ends_open && !is_dot)
+ add(stems.horizontal, to.y());
+ }
+ if (is_horizontal) {
+ if (starts_open)
+ add(stems.vertical, from.x());
+ if (ends_open && !is_dot)
+ add(stems.vertical, to.x());
+ }
+ } else if (element.isCurveTo()) {
+ // The turning points of a curve count too: the top of a circle should be as crisp as the top of a square.
+ stems.is_diagonal = false;
+ auto from = point_at(index - 1);
+ auto first_control = point_at(index);
+ auto second_control = point_at(index + 1);
+ auto to = point_at(index + 2);
+ if (coordinates_match(from.x(), first_control.x()))
+ add(stems.vertical, from.x());
+ if (coordinates_match(from.y(), first_control.y()))
+ add(stems.horizontal, from.y());
+ if (coordinates_match(to.x(), second_control.x()))
+ add(stems.vertical, to.x());
+ if (coordinates_match(to.y(), second_control.y()))
+ add(stems.horizontal, to.y());
+ index += 2;
+ }
+ }
+
+ quick_sort(stems.vertical);
+ quick_sort(stems.horizontal);
+ return stems;
+}
+
+// A place on one axis of an icon, and where fitting moves it to.
+struct FittedPosition {
+ qreal from { 0 };
+ qreal to { 0 };
};
-// The 1x drawings are used for icons shown within this much of the canvas's size.
-static constexpr int PIXEL_SNAPPED_ICON_SIZE_TOLERANCE = 2;
+// Lines of an odd width need their center on a half pixel to fill whole pixels, and lines of an even width on a whole
+// one.
+static qreal pixel_phase(int stroke_width)
+{
+ return stroke_width % 2 == 0 ? 0 : 0.5;
+}
-static Optional<PixelSnappedChromeIcon> pixel_snapped_chrome_icon(ChromeIcon icon)
+static qreal nearest_on_pixel_grid(qreal position, int stroke_width)
{
- switch (icon) {
- case ChromeIcon::Close:
- case ChromeIcon::TabClose:
- return PixelSnappedChromeIcon { "M4 4L12 12M12 4L4 12"sv, 1.25 };
- case ChromeIcon::NewTab:
- return PixelSnappedChromeIcon { "M7.5 2.5v10M2.5 7.5h10"sv };
- case ChromeIcon::Volume:
- return PixelSnappedChromeIcon { "M2.5 5.5H5l3.5-3v11L5 10.5H2.5zM10.9 5.3Q12.6 6.4 12.6 8T10.9 10.7"sv };
- case ChromeIcon::VolumeMuted:
- return PixelSnappedChromeIcon { "M2.5 5.5H5l3.5-3v11L5 10.5H2.5zM10 6l4 4M14 6l-4 4"sv };
- case ChromeIcon::Download:
- return PixelSnappedChromeIcon { "M7.5 2v7.5M4.5 6.5l3 3 3-3M2.5 12.5h10"sv };
- default:
- return {};
+ auto phase = pixel_phase(stroke_width);
+ return floor(position - phase + 0.5) + phase;
+}
+
+static bool lies_between_pixel_grid_positions(qreal position, int stroke_width)
+{
+ return coordinates_match(position - pixel_phase(stroke_width) + 0.5, round(position - pixel_phase(stroke_width) + 0.5));
+}
+
+static Vector<FittedPosition> fit_stems_to_pixels(Vector<qreal> const& stems, qreal scale, int stroke_width, int canvas_size)
+{
+ Vector<FittedPosition> fitted;
+ if (stems.is_empty())
+ return fitted;
+
+ // The grid of an icon on half pixels falls exactly between the pixels at 2x. Moving the whole icon, toward the
+ // middle of the canvas, keeps its lines evenly spaced, where moving each one to its nearest place would not.
+ size_t lines_between_positions = 0;
+ for (auto stem : stems) {
+ if (lies_between_pixel_grid_positions(stem * scale, stroke_width))
+ ++lines_between_positions;
}
+ qreal shift = 0;
+ if (lines_between_positions * 2 >= stems.size()) {
+ auto middle = (stems.first() + stems.last()) * scale / 2;
+ shift = middle < canvas_size / 2.0 ? 0.5 : -0.5;
+ }
+
+ for (auto stem : stems) {
+ auto position = stem * scale;
+ auto target = nearest_on_pixel_grid(position + shift, stroke_width);
+ // Lines that had room between them keep at least a pixel of it.
+ if (!fitted.is_empty()) {
+ auto const& previous = fitted.last();
+ if (position - previous.from >= stroke_width + 0.5 && target - previous.to < stroke_width + 1)
+ target = previous.to + stroke_width + 1;
+ target = max(target, previous.to);
+ }
+ fitted.append({ position, target });
+ }
+ return fitted;
}
-static QPainterPath const* chrome_icon_pixel_snapped_painter_path(ChromeIcon icon)
+// Everything between two fitted places moves along with them, and whatever is beyond them moves as the nearest does.
+static qreal apply_fitted_positions(Vector<FittedPosition> const& fitted, qreal position)
{
- static HashMap<ChromeIcon, Optional<QPainterPath>> s_paths;
- auto const& path = s_paths.ensure(icon, [&]() -> Optional<QPainterPath> {
- auto snapped = pixel_snapped_chrome_icon(icon);
- if (!snapped.has_value())
- return {};
- return mirrored_for_right_sidebar(icon, parse_svg_path(snapped->path));
- });
- return path.has_value() ? &path.value() : nullptr;
+ if (fitted.is_empty())
+ return position;
+ if (position <= fitted.first().from)
+ return position + fitted.first().to - fitted.first().from;
+ if (position >= fitted.last().from)
+ return position + fitted.last().to - fitted.last().from;
+ for (size_t index = 1; index < fitted.size(); ++index) {
+ auto const& start = fitted[index - 1];
+ auto const& end = fitted[index];
+ if (position > end.from)
+ continue;
+ return start.to + ((position - start.from) / (end.from - start.from) * (end.to - start.to));
+ }
+ VERIFY_NOT_REACHED();
}
-static QPixmap create_transparent_icon_pixmap(QSize logical_size, qreal device_pixel_ratio)
+// A 45 degree line is only clean if it runs through the corners of pixels, and it only stays at 45 degrees if the icon
+// is scaled evenly. So an icon of nothing else is resized as a whole, to a length that keeps its corners on the grid.
+static QTransform transform_for_diagonal_icon(QPainterPath const& path, qreal scale, int stroke_width, int canvas_size)
{
- QPixmap pixmap(physical_size_for_device_pixel_ratio(logical_size, device_pixel_ratio));
- pixmap.setDevicePixelRatio(device_pixel_ratio);
- pixmap.fill(Qt::transparent);
- return pixmap;
+ auto bounds = path.boundingRect();
+ auto length = max(bounds.width(), bounds.height());
+
+ // An even length puts the middle of a cross on one spot, so that its lines cross in a single place.
+ auto fitted_length = max(2.0, round(length * scale / 2) * 2);
+ auto fitted_scale = fitted_length / length;
+
+ auto left = nearest_on_pixel_grid((canvas_size - (bounds.width() * fitted_scale)) / 2, stroke_width);
+ auto top = nearest_on_pixel_grid((canvas_size - (bounds.height() * fitted_scale)) / 2, stroke_width);
+
+ QTransform transform;
+ transform.translate(left - (bounds.left() * fitted_scale), top - (bounds.top() * fitted_scale));
+ transform.scale(fitted_scale, fitted_scale);
+ return transform;
}
-// The icon is drawn from its vector at the size and device pixel ratio it is shown at, never scaled from a picture of it.
-// NB: The size the icon was made for sets its line: the toolbar's larger icons are drawn with a finer line on their
-// canvas, so that they keep about the weight of the smaller ones.
-static QPixmap create_chrome_icon_pixmap(ChromeIcon icon, QPainterPath const& path, QColor color, qreal device_pixel_ratio, int logical_size, int design_size)
+// Puts the lines of an icon on the pixel grid of the size it is shown at, the way a font's hints do for its letters.
+static QPainterPath fit_icon_path_to_pixels(QPainterPath const& path, qreal scale, int stroke_width, int canvas_size)
+{
+ auto stems = find_icon_stems(path);
+ if (stems.is_diagonal)
+ return transform_for_diagonal_icon(path, scale, stroke_width, canvas_size).map(path);
+
+ auto vertical = fit_stems_to_pixels(stems.vertical, scale, stroke_width, canvas_size);
+ auto horizontal = fit_stems_to_pixels(stems.horizontal, scale, stroke_width, canvas_size);
+
+ QPainterPath fitted = path;
+ for (int index = 0; index < fitted.elementCount(); ++index) {
+ auto const& element = fitted.elementAt(index);
+ fitted.setElementPositionAt(index, apply_fitted_positions(vertical, element.x * scale), apply_fitted_positions(horizontal, element.y * scale));
+ }
+ return fitted;
+}
+
+static qreal chrome_icon_stroke_width(ChromeIcon icon, int logical_size)
+{
+ if (icon == ChromeIcon::WindowMinimize || icon == ChromeIcon::WindowMaximize || icon == ChromeIcon::WindowRestore || icon == ChromeIcon::WindowClose)
+ return WINDOW_CONTROL_STROKE_WIDTH;
+ if (logical_size > LARGEST_ICON_SIZE_WITH_FIXED_STROKE)
+ return CHROME_ICON_STROKE_WIDTH * logical_size / CHROME_ICON_CANVAS_SIZE;
+ if (logical_size < SMALLEST_ICON_SIZE_WITH_FIXED_STROKE)
+ return CHROME_ICON_STROKE_WIDTH * logical_size / SMALLEST_ICON_SIZE_WITH_FIXED_STROKE;
+ return CHROME_ICON_STROKE_WIDTH;
+}
+
+// The straight lines of an icon that run along the pixel grid, and everything else.
+struct IconLines {
+ QPainterPath straight;
+ QPainterPath slanted;
+};
+
+static IconLines split_icon_lines(QPainterPath const& path)
+{
+ IconLines lines;
+ auto start_at = [](QPainterPath& part, QPointF point) {
+ if (part.elementCount() == 0 || part.currentPosition() != point)
+ part.moveTo(point);
+ };
+
+ for (int index = 1; index < path.elementCount(); ++index) {
+ auto const& element = path.elementAt(index);
+ QPointF from = path.elementAt(index - 1);
+ QPointF to = element;
+ if (element.isLineTo()) {
+ auto& part = coordinates_match(from.x(), to.x()) || coordinates_match(from.y(), to.y()) ? lines.straight : lines.slanted;
+ start_at(part, from);
+ part.lineTo(to);
+ } else if (element.isCurveTo()) {
+ // NB: A small rounded corner belongs to the lines on either side of it. It is too short to look thin.
+ QPointF end = path.elementAt(index + 2);
+ auto& part = abs(from.x() - end.x()) + abs(from.y() - end.y()) < LONGEST_ROUNDED_CORNER ? lines.straight : lines.slanted;
+ start_at(part, from);
+ part.cubicTo(to, path.elementAt(index + 1), end);
+ index += 2;
+ }
+ }
+ return lines;
+}
+
+// The outline of an icon's lines. A one pixel line fills its pixels when it runs along them, but it spreads thin over a
+// slant or a curve, so these are drawn a little wider to match.
+static QPainterPath icon_outline(QPainterPath const& path, int stroke_width)
+{
+ QPainterPathStroker stroker;
+ stroker.setWidth(stroke_width);
+ stroker.setCapStyle(Qt::RoundCap);
+ stroker.setJoinStyle(Qt::RoundJoin);
+ if (stroke_width > 1)
+ return stroker.createStroke(path);
+
+ auto lines = split_icon_lines(path);
+
+ // NB: These lines end on the grid, so a rounded end or corner would leave a soft pixel behind.
+ stroker.setCapStyle(Qt::SquareCap);
+ stroker.setJoinStyle(Qt::MiterJoin);
+ auto outline = stroker.createStroke(lines.straight);
+
+ stroker.setWidth(SLANTED_ONE_PIXEL_LINE_WIDTH);
+ stroker.setCapStyle(Qt::RoundCap);
+ stroker.setJoinStyle(Qt::RoundJoin);
+ outline.addPath(stroker.createStroke(lines.slanted));
+ return outline;
+}
+
+// The icon is drawn from its vector at the size and device pixel ratio it is shown at, never scaled from a picture.
+static QPixmap create_chrome_icon_pixmap(ChromeIcon icon, QPainterPath const& path, QColor color, qreal device_pixel_ratio, int logical_size)
{
auto pixmap = create_transparent_icon_pixmap({ logical_size, logical_size }, device_pixel_ratio);
QPainter painter(&pixmap);
painter.setRenderHint(QPainter::Antialiasing, true);
+ // NB: Everything below is measured in pixels of the screen.
+ painter.scale(1 / device_pixel_ratio, 1 / device_pixel_ratio);
+
// An icon is drawn in one opaque color, then made as translucent as its color as a whole, so that where its lines
// cross they don't add up to a brighter spot.
auto opaque_color = color;
opaque_color.setAlpha(255);
- // NB: The 1x drawing keeps the size of the 16 px canvas, so it is only used for icons shown at about that size. A
- // smaller icon, such as a badge's, is scaled from its vector.
- auto has_pixel_snapped_size = device_pixel_ratio == 1 && abs(logical_size - CHROME_ICON_CANVAS_SIZE) <= PIXEL_SNAPPED_ICON_SIZE_TOLERANCE;
- auto const* pixel_snapped_path = has_pixel_snapped_size ? chrome_icon_pixel_snapped_painter_path(icon) : nullptr;
- if (pixel_snapped_path) {
- // NB: The 1x drawing keeps its own pixels, so it is centered on whole pixels rather than scaled.
- auto offset = (logical_size - CHROME_ICON_CANVAS_SIZE) / 2;
- painter.translate(offset, offset);
- painter.setPen(QPen(opaque_color, pixel_snapped_chrome_icon(icon)->stroke_width, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin));
- painter.setBrush(Qt::NoBrush);
- painter.drawPath(*pixel_snapped_path);
- } else {
- painter.scale(static_cast<qreal>(logical_size) / CHROME_ICON_CANVAS_SIZE, static_cast<qreal>(logical_size) / CHROME_ICON_CANVAS_SIZE);
-
- auto stroke_width = CHROME_ICON_STROKE_WIDTH;
- // NB: The window controls of Windows are drawn with hairlines, as the system draws its own.
- if (icon == ChromeIcon::WindowMinimize || icon == ChromeIcon::WindowMaximize || icon == ChromeIcon::WindowRestore || icon == ChromeIcon::WindowClose)
- stroke_width = 1.0;
- else if (design_size == TOOLBAR_ICON_SIZE)
- stroke_width = TOOLBAR_ICON_STROKE_WIDTH;
-
- if (icon == ChromeIcon::Sidebar || icon == ChromeIcon::SidebarRight) {
- // Fill the combined outline once so antialiased edges do not accumulate where the divider meets the frame.
- QPainterPathStroker stroker;
- stroker.setWidth(stroke_width);
- stroker.setCapStyle(Qt::RoundCap);
- stroker.setJoinStyle(Qt::RoundJoin);
- auto stroke = stroker.createStroke(path);
- stroke.setFillRule(Qt::WindingFill);
- painter.fillPath(stroke.simplified(), opaque_color);
- } else {
- painter.setPen(QPen(opaque_color, stroke_width, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
- painter.setBrush(icon == ChromeIcon::StarFilled ? QBrush(opaque_color) : QBrush(Qt::NoBrush));
- painter.drawPath(path);
- }
- }
+ // A line of a whole number of pixels can fill them exactly. Rounding down keeps a 1x line at one pixel, the most
+ // that leaves room inside an icon of this size.
+ auto stroke_width = max(1, static_cast<int>(floor((chrome_icon_stroke_width(icon, logical_size) * device_pixel_ratio) + 0.25)));
+ auto scale = logical_size * device_pixel_ratio / CHROME_ICON_CANVAS_SIZE;
+ auto fitted_path = fit_icon_path_to_pixels(path, scale, stroke_width, pixmap.width());
+
+ // Fill the combined outline once so antialiased edges do not accumulate where lines meet.
+ auto outline = icon_outline(fitted_path, stroke_width);
+ outline.setFillRule(Qt::WindingFill);
+ if (icon == ChromeIcon::StarFilled)
+ outline.addPath(fitted_path);
+ painter.fillPath(outline.simplified(), opaque_color);
if (color.alpha() != 255) {
painter.resetTransform();
@@ -535,15 +744,15 @@ public:
QColor selected;
};
- ChromeIconEngine(ChromeIcon icon, QPainterPath path, Colors colors, int design_size)
+ ChromeIconEngine(ChromeIcon icon, QPainterPath path, Colors colors, int logical_size)
: m_icon(icon)
, m_path(AK::move(path))
, m_colors(AK::move(colors))
- , m_design_size(design_size)
+ , m_logical_size(logical_size)
{
}
- virtual QIconEngine* clone() const override { return new ChromeIconEngine(m_icon, m_path, m_colors, m_design_size); }
+ virtual QIconEngine* clone() const override { return new ChromeIconEngine(m_icon, m_path, m_colors, m_logical_size); }
virtual QSize actualSize(QSize const& size, QIcon::Mode, QIcon::State) override
{
@@ -551,7 +760,7 @@ public:
return { side, side };
}
- virtual QList<QSize> availableSizes(QIcon::Mode, QIcon::State) override { return { QSize(m_design_size, m_design_size) }; }
+ virtual QList<QSize> availableSizes(QIcon::Mode, QIcon::State) override { return { QSize(m_logical_size, m_logical_size) }; }
virtual QPixmap pixmap(QSize const& size, QIcon::Mode mode, QIcon::State state) override
{
@@ -563,7 +772,16 @@ public:
auto side = min(size.width(), size.height());
if (side <= 0)
return {};
- return create_chrome_icon_pixmap(m_icon, m_path, color_for_mode(mode), scale, side, m_design_size);
+
+ // OPTIMIZATION: Fitting an icon to the pixels costs far more than drawing the result, and the same few pictures
+ // are asked for on every repaint.
+ for (auto const& entry : m_pixmaps) {
+ if (entry.side == side && entry.scale == scale && entry.mode == mode)
+ return entry.pixmap;
+ }
+ auto pixmap = create_chrome_icon_pixmap(m_icon, m_path, color_for_mode(mode), scale, side);
+ m_pixmaps.append({ side, scale, mode, pixmap });
+ return pixmap;
}
virtual void paint(QPainter* painter, QRect const& rect, QIcon::Mode mode, QIcon::State state) override
@@ -591,15 +809,23 @@ private:
return m_colors.normal;
}
+ struct CachedPixmap {
+ int side { 0 };
+ qreal scale { 1 };
+ QIcon::Mode mode { QIcon::Normal };
+ QPixmap pixmap;
+ };
+
ChromeIcon m_icon;
QPainterPath m_path;
Colors m_colors;
- int m_design_size { CHROME_ICON_CANVAS_SIZE };
+ int m_logical_size { CHROME_ICON_CANVAS_SIZE };
+ Vector<CachedPixmap> m_pixmaps;
};
QPixmap create_chrome_icon_pixmap(ChromeIcon icon, QColor color, qreal device_pixel_ratio)
{
- return create_chrome_icon_pixmap(icon, chrome_icon_painter_path(icon), color, device_pixel_ratio, CHROME_ICON_CANVAS_SIZE, CHROME_ICON_CANVAS_SIZE);
+ return create_chrome_icon_pixmap(icon, chrome_icon_painter_path(icon), color, device_pixel_ratio, CHROME_ICON_CANVAS_SIZE);
}
static QIcon create_chrome_icon(ChromeIcon icon, QPainterPath const& path, QPalette const& palette, ChromeStyle::WindowActivity activity, int logical_size)
@@ -642,26 +868,25 @@ QIcon create_chrome_icon(ChromeIcon icon, QPalette const& palette, ChromeStyle::
// slide past each other as the travel goes from 0 to 1, and each line breaks around its knob.
QIcon create_site_settings_icon(QPalette const& palette, qreal knob_travel)
{
- static constexpr qreal LINE_START = 2.5;
+ static constexpr qreal LINE_START = 1.5;
static constexpr qreal LINE_END = 13.5;
- static constexpr qreal KNOB_RADIUS = 1.6;
- static constexpr qreal KNOB_GAP = 2;
- static constexpr qreal KNOB_TRAVEL = 5;
+ static constexpr qreal KNOB_RADIUS = 2;
+ static constexpr qreal KNOB_TRAVEL = 6;
QPainterPath path;
auto add_line_with_knob = [&](qreal y, qreal knob_x) {
- if (knob_x - KNOB_GAP > LINE_START) {
+ if (knob_x - KNOB_RADIUS > LINE_START) {
path.moveTo(LINE_START, y);
- path.lineTo(knob_x - KNOB_GAP, y);
+ path.lineTo(knob_x - KNOB_RADIUS, y);
}
- if (knob_x + KNOB_GAP < LINE_END) {
- path.moveTo(knob_x + KNOB_GAP, y);
+ if (knob_x + KNOB_RADIUS < LINE_END) {
+ path.moveTo(knob_x + KNOB_RADIUS, y);
path.lineTo(LINE_END, y);
}
path.addEllipse(QPointF(knob_x, y), KNOB_RADIUS, KNOB_RADIUS);
};
- add_line_with_knob(5, 10.5 - (KNOB_TRAVEL * knob_travel));
- add_line_with_knob(11, 5.5 + (KNOB_TRAVEL * knob_travel));
+ add_line_with_knob(4.5, 10.5 - (KNOB_TRAVEL * knob_travel));
+ add_line_with_knob(11.5, 4.5 + (KNOB_TRAVEL * knob_travel));
return create_chrome_icon(ChromeIcon::SiteSettings, path, palette, ChromeStyle::WindowActivity::Active, CHROME_ICON_CANVAS_SIZE);
}