Skip to content

Commit

Permalink
Adjust logic for positioning the filename label in RenderFileUploadCo…
Browse files Browse the repository at this point in the history
…ntrol to use the font ascent

https://bugs.webkit.org/show_bug.cgi?id=262494
rdar://116356078

Reviewed by Alan Baradlay.

In preparation for vertical writing mode support for `RenderFileUploadControl`,
adjust the way that the filename label is positioned to use the font ascent
rather than the `baselinePosition` of the associated button.

This change is necessary since, the baseline position returns the center baseline
in vertical writing mode, making it impossible to line up the filename label
with the button text. Instead, the font ascent can be used similar to its use
to draw the button text using `TextBoxPainter`.

For now, there is no behavior change as only horizontal writing mode is supported.

* Source/WebCore/rendering/RenderButton.cpp:
* Source/WebCore/rendering/RenderButton.h:
* Source/WebCore/rendering/RenderFileUploadControl.cpp:
(WebCore::RenderFileUploadControl::paintControl):

Obtain the visual rect of the first textbox in the file upload button, move
it to the desired painting position, and adjust its position using the ascent
to ensure the text is painted at the correct location.

Canonical link: https://commits.webkit.org/268752@main
  • Loading branch information
pxlcoder committed Oct 2, 2023
1 parent 006900d commit a073a3d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
1 change: 0 additions & 1 deletion Source/WebCore/rendering/RenderButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include "RenderBoxModelObjectInlines.h"
#include "RenderElementInlines.h"
#include "RenderStyleSetters.h"
#include "RenderTextFragment.h"
#include "RenderTheme.h"
#include "RenderTreeBuilder.h"
#include "StyleInheritedData.h"
Expand Down
3 changes: 3 additions & 0 deletions Source/WebCore/rendering/RenderButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#pragma once

#include "RenderFlexibleBox.h"
#include "RenderTextFragment.h"
#include <memory>

namespace WebCore {
Expand Down Expand Up @@ -59,6 +60,8 @@ class RenderButton final : public RenderFlexibleBox {
void layout() override;
#endif

RenderTextFragment* textRenderer() const { return m_buttonText.get(); }

RenderBlock* innerRenderer() const { return m_inner.get(); }
void setInnerRenderer(RenderBlock&);

Expand Down
23 changes: 17 additions & 6 deletions Source/WebCore/rendering/RenderFileUploadControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "HTMLInputElement.h"
#include "HTMLNames.h"
#include "Icon.h"
#include "InlineIteratorInlineBox.h"
#include "LocalizedStrings.h"
#include "PaintInfo.h"
#include "RenderBoxInlines.h"
Expand Down Expand Up @@ -168,18 +169,28 @@ void RenderFileUploadControl::paintControl(PaintInfo& paintInfo, const LayoutPoi
else
textX = contentLeft + contentWidth() - buttonAndIconWidth - font.width(textRun);

LayoutUnit textY;
// We want to match the button's baseline
// FIXME: Make this work with transforms.
if (RenderButton* buttonRenderer = downcast<RenderButton>(button->renderer()))
textY = paintOffset.y() + borderTop() + paddingTop() + buttonRenderer->baselinePosition(AlphabeticBaseline, true, HorizontalLine, PositionOnContainingLine);
else
textY = baselinePosition(AlphabeticBaseline, true, HorizontalLine, PositionOnContainingLine);
auto textY = [&]() -> int {
if (auto* buttonRenderer = downcast<RenderButton>(button->renderer())) {
if (auto* buttonTextRenderer = buttonRenderer->textRenderer()) {
if (auto textBox = InlineIterator::firstTextBoxFor(*buttonTextRenderer)) {
auto textVisualRect = textBox->visualRectIgnoringBlockDirection();
textVisualRect.setLocation(buttonTextRenderer->localToContainerPoint(textVisualRect.location(), this));
textVisualRect.moveBy(roundPointToDevicePixels(paintOffset, document().deviceScaleFactor()));
textVisualRect.move(0, textBox->style().fontCascade().metricsOfPrimaryFont().ascent());
return std::round(textVisualRect.y());
}
}
}

return roundToInt(baselinePosition(AlphabeticBaseline, true, HorizontalLine, PositionOnContainingLine));
}();

paintInfo.context().setFillColor(style().visitedDependentColorWithColorFilter(CSSPropertyColor));

// Draw the filename
paintInfo.context().drawBidiText(font, textRun, IntPoint(roundToInt(textX), roundToInt(textY)));
paintInfo.context().drawBidiText(font, textRun, IntPoint(roundToInt(textX), textY));

if (inputElement().icon()) {
// Determine where the icon should be placed
Expand Down

0 comments on commit a073a3d

Please sign in to comment.