Fix em, ex, ch resolution in UIWebView elements.

This commit is contained in:
Martín Lucas Golini
2026-07-01 23:27:52 -03:00
parent 5ad166b648
commit d8a683ee99
2 changed files with 89 additions and 0 deletions

View File

@@ -1751,6 +1751,28 @@ Float UINode::lengthFromValue( const StyleSheetProperty& property,
res.setValue( 12, StyleSheetLength::Unit::Dp );
return convertLength( res, 0 );
}
if ( length.getUnit() != StyleSheetLength::Unit::Em &&
length.getUnit() != StyleSheetLength::Unit::Ex &&
length.getUnit() != StyleSheetLength::Unit::Ch )
return convertLength( length, 0 );
Float parentFontSize = 12.f * PixelDensity::getPixelDensity();
Node* parentNode = getParent();
while ( parentNode ) {
if ( parentNode->isWidget() ) {
parentFontSize = getAbsoluteFontSize( parentNode->asType<UIWidget>() );
break;
}
parentNode = parentNode->getParent();
}
Font* font = nullptr;
if ( getUISceneNode() && getUISceneNode()->getUIThemeManager() )
font = getUISceneNode()->getUIThemeManager()->getDefaultFont();
return length.asPixels( 0, Sizef::Zero, getSceneNode()->getDPI(), parentFontSize,
parentFontSize, font );
}
return lengthFromValue( property.getValue(),
property.getPropertyDefinition()->getRelativeTarget(), defaultValue,

View File

@@ -10,6 +10,7 @@
#include <eepp/ui/uiroot.hpp>
#include <eepp/ui/uiscenenode.hpp>
#include <eepp/ui/uiscrollbar.hpp>
#include <eepp/ui/uitextspan.hpp>
#include <eepp/ui/uithememanager.hpp>
#include <eepp/ui/uiwebview.hpp>
#include <eepp/ui/uiwidget.hpp>
@@ -170,6 +171,72 @@ UTEST( UIWebView, DocumentRootHitTestingTraversesScrollableExtent ) {
Engine::destroySingleton();
}
UTEST( UIWebView, FontSizeEmDoesNotCompoundOnViewportRelayout ) {
auto win = Engine::instance()->createWindow(
WindowSettings( 640, 480, "UIWebView Font Size Em Relayout", WindowStyle::Default,
WindowBackend::Default, 32, {}, 1, false, true ),
ContextSettings( false, 0, 0, GLv_default, true, false ) );
FileSystem::changeWorkingDirectory( Sys::getProcessPath() );
FontTrueType* font = FontTrueType::New( "NotoSans-Regular" );
font->loadFromFile( "../assets/fonts/NotoSans-Regular.ttf" );
ASSERT_TRUE( font != nullptr && font->loaded() );
FontFamily::loadFromRegular( font );
UISceneNode* sceneNode = UISceneNode::New();
SceneManager::instance()->add( sceneNode );
sceneNode->getUIThemeManager()->setDefaultFont( font );
UIWebView* webView = UIWebView::New();
webView->setParent( sceneNode->getRoot() );
webView->setPixelsSize( 420, 260 );
webView->setLayoutSizePolicy( SizePolicy::Fixed, SizePolicy::Fixed );
const std::string path = Sys::getTempPath() + "eepp_uiwebview_font_size_em.html";
FileSystem::fileWrite( path, R"html(
<!DOCTYPE html>
<html>
<head>
<style>
html, body { margin: 0; padding: 0; }
body { font-size: 16px; }
h1 { font-size: 2.5em; margin: 0.5em 0; }
</style>
</head>
<body>
<h1><span id="title-text">Title</span></h1>
<div style="height: 900px"></div>
</body>
</html>
)html" );
webView->loadURI( URI( "file://" + path ) );
UISceneNode* documentScene = webView->getDocumentSceneNode();
ASSERT_TRUE( documentScene != nullptr );
auto pump = [&]() {
for ( int i = 0; i < 30; i++ ) {
win->getInput()->update();
SceneManager::instance()->update( Seconds( 1.f / 60.f ) );
}
};
pump();
Node* title = documentScene->getRoot()->find( "title-text" );
ASSERT_TRUE( title != nullptr && title->isType( UI_TYPE_TEXTSPAN ) );
EXPECT_NEAR( title->asType<UITextSpan>()->getFontSize(), 40.f, 1.f );
for ( int i = 0; i < 4; i++ ) {
webView->setPixelsSize( 520 + i * 20, 320 + i * 10 );
pump();
webView->setPixelsSize( 420, 260 );
pump();
EXPECT_NEAR( title->asType<UITextSpan>()->getFontSize(), 40.f, 1.f );
}
Engine::destroySingleton();
}
UTEST( UIWebView, VerticalScrollbarViewportDoesNotCreateHorizontalScroll ) {
auto win = Engine::instance()->createWindow(
WindowSettings( 640, 480, "UIWebView Scrollbar Viewport Test", WindowStyle::Default,