Skip to content

Truly independent web browser

3.4 KB
Raw Blame
/*
 * Copyright (c) 2026-present, the Ladybird developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/String.h>
#include <LibWebCommon/WebView/DictionaryLookup.h>

#import <CoreText/CoreText.h>
#import <UI/Qt/DictionaryLookup.h>

namespace Ladybird {

static NSString* ns_string_from_ak_string(String const& string)
{
    auto view = string.bytes_as_string_view();
    return [[NSString alloc] initWithBytes:view.characters_without_null_termination()
                                    length:view.length()
                                  encoding:NSUTF8StringEncoding];
}

static NSFontWeight ns_font_weight_from_css_weight(u16 weight)
{
    if (weight >= 900)
        return NSFontWeightBlack;
    if (weight >= 800)
        return NSFontWeightHeavy;
    if (weight >= 700)
        return NSFontWeightBold;
    if (weight >= 600)
        return NSFontWeightSemibold;
    if (weight >= 500)
        return NSFontWeightMedium;
    return NSFontWeightRegular;
}

static NSFont* ns_font_from_dictionary_lookup_text_style(WebView::DictionaryLookupTextStyle const& style)
{
    if (style.ui_point_size <= 0)
        return nil;

    auto traits = style.slope == 0 ? 0 : NSFontItalicTrait;
    auto* family = ns_string_from_ak_string(style.font_family);
    auto* font = [[NSFontManager sharedFontManager] fontWithFamily:family
                                                            traits:traits
                                                            weight:style.weight / 100
                                                              size:style.ui_point_size];
    if (font)
        return font;

    font = [NSFont systemFontOfSize:style.ui_point_size weight:ns_font_weight_from_css_weight(style.weight)];
    if (style.slope != 0)
        font = [[NSFontManager sharedFontManager] convertFont:font toHaveTrait:NSItalicFontMask];
    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);
    if (!lookup.style.has_value())
        return [[NSAttributedString alloc] initWithString:ns_text];

    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 }];
}

void show_dictionary_lookup(NSView* view, WebView::DictionaryLookup const& lookup, NSPoint position)
{
    if (!view || lookup.text.is_empty())
        return;

    [view showDefinitionForAttributedString:attributed_string_for_dictionary_lookup(lookup) atPoint:position];
}

}