Skip to content

Truly independent web browser

LibWebCommon+WebContent+UI/Qt: Look up words with the page's opsz

Jelle Raaijmakers56734365e95b
diff --git a/Libraries/LibWebCommon/WebView/DictionaryLookup.cpp b/Libraries/LibWebCommon/WebView/DictionaryLookup.cpp
index 3d477175a02..af684648467 100644
--- a/Libraries/LibWebCommon/WebView/DictionaryLookup.cpp
+++ b/Libraries/LibWebCommon/WebView/DictionaryLookup.cpp
@@ -13,6 +13,7 @@ ErrorOr<void> IPC::encode(Encoder& encoder, WebView::DictionaryLookupTextStyle c
{
TRY(encoder.encode(style.font_family));
TRY(encoder.encode(style.ui_point_size));
+ TRY(encoder.encode(style.optical_size));
TRY(encoder.encode(style.weight));
TRY(encoder.encode(style.slope));
return {};
@@ -23,10 +24,11 @@ ErrorOr<WebView::DictionaryLookupTextStyle> IPC::decode(Decoder& decoder)
{
auto font_family = TRY(decoder.decode<String>());
auto ui_point_size = TRY(decoder.decode<float>());
+ auto optical_size = TRY(decoder.decode<Optional<float>>());
auto weight = TRY(decoder.decode<u16>());
auto slope = TRY(decoder.decode<u8>());
- return WebView::DictionaryLookupTextStyle { move(font_family), ui_point_size, weight, slope };
+ return WebView::DictionaryLookupTextStyle { move(font_family), ui_point_size, optical_size, weight, slope };
}
template<>
diff --git a/Libraries/LibWebCommon/WebView/DictionaryLookup.h b/Libraries/LibWebCommon/WebView/DictionaryLookup.h
index cb4be08d743..029809c2247 100644
--- a/Libraries/LibWebCommon/WebView/DictionaryLookup.h
+++ b/Libraries/LibWebCommon/WebView/DictionaryLookup.h
@@ -18,6 +18,7 @@ namespace WebView {
struct WEBCOMMON_API DictionaryLookupTextStyle {
String font_family;
float ui_point_size { 0 };
+ Optional<float> optical_size;
u16 weight { 0 };
u8 slope { 0 };
};
diff --git a/Services/WebContent/ConnectionFromClient.cpp b/Services/WebContent/ConnectionFromClient.cpp
index 3c6af019d4c..c7a29b40179 100644
--- a/Services/WebContent/ConnectionFromClient.cpp
+++ b/Services/WebContent/ConnectionFromClient.cpp
@@ -25,6 +25,8 @@
#include <LibGC/Heap.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/Color.h>
+#include <LibGfx/Font/Font.h>
+#include <LibGfx/FourCC.h>
#include <LibGfx/Palette.h>
#include <LibIPC/Transport.h>
#include <LibJS/Runtime/ConsoleObject.h>
@@ -2808,6 +2810,7 @@ static WebView::DictionaryLookupTextStyle dictionary_lookup_text_style_from_layo
return {
.font_family = font.family().to_string(),
.ui_point_size = font.pixel_size() * static_cast<float>(scale),
+ .optical_size = font.variation_settings().axes.get(Gfx::FourCC("opsz")),
.weight = font.weight(),
.slope = font.slope(),
};
diff --git a/UI/Qt/DictionaryLookup.mm b/UI/Qt/DictionaryLookup.mm
index 53e850db290..88fa86796b3 100644
--- a/UI/Qt/DictionaryLookup.mm
+++ b/UI/Qt/DictionaryLookup.mm
@@ -7,6 +7,7 @@
#include <AK/String.h>
#include <LibWebCommon/WebView/DictionaryLookup.h>
+#import <CoreText/CoreText.h>
#import <UI/Qt/DictionaryLookup.h>
namespace Ladybird {
@@ -54,6 +55,16 @@ static NSFont* ns_font_from_dictionary_lookup_text_style(WebView::DictionaryLook
return font;
}
+// AppKit picks a font's optical size from its point size, which grows with zoom, and the glyph shapes and tracking of
+// that optical size would no longer cover the word as the page draws it. So use the optical size the page renders with.
+static NSFont* ns_font_with_optical_size(NSFont* font, float optical_size)
+{
+ auto* descriptor = [font.fontDescriptor fontDescriptorByAddingAttributes:@{(NSString*)kCTFontOpticalSizeAttribute : @(optical_size)}];
+ if (auto* font_with_optical_size = [NSFont fontWithDescriptor:descriptor size:font.pointSize])
+ return font_with_optical_size;
+ return font;
+}
+
static NSAttributedString* attributed_string_for_dictionary_lookup(WebView::DictionaryLookup const& lookup)
{
auto* ns_text = ns_string_from_ak_string(lookup.text);
@@ -63,6 +74,8 @@ static NSAttributedString* attributed_string_for_dictionary_lookup(WebView::Dict
auto* font = ns_font_from_dictionary_lookup_text_style(*lookup.style);
if (!font)
return [[NSAttributedString alloc] initWithString:ns_text];
+ if (lookup.style->optical_size.has_value())
+ font = ns_font_with_optical_size(font, *lookup.style->optical_size);
return [[NSAttributedString alloc] initWithString:ns_text attributes:@ { NSFontAttributeName : font }];
}