Skip to content

Commit

Permalink
Allow text to be set to a transparency.
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=273602
rdar://127309911

Reviewed by Wenson Hsieh and Richard Robinson.

We need a way to mark text as transparent, so add
a document marker to set the transparency. This could
be combined with the DraggedContent document markers in the
future.

* Source/WebCore/dom/DocumentMarker.h:
(WebCore::DocumentMarker::allMarkers):
* Source/WebCore/dom/DocumentMarkerController.cpp:
(WebCore::shouldInsertAsSeparateMarker):
* Source/WebCore/rendering/MarkedText.cpp:
(WebCore::MarkedText::collectForDocumentMarkers):
* Source/WebCore/rendering/MarkedText.h:
* Source/WebCore/rendering/RenderReplaced.cpp:
(WebCore::contentContainsReplacedElement):
(WebCore::RenderReplaced::paint):
(WebCore::draggedContentContainsReplacedElement): Deleted.
* Source/WebCore/rendering/StyledMarkedText.cpp:
(WebCore::resolveStyleForMarkedText):
* Source/WebCore/rendering/TextBoxPainter.cpp:
(WebCore::TextBoxPainter<TextBoxPath>::paintForeground):
(WebCore::TextBoxPainter<TextBoxPath>::createDecorationPainter):
* Source/WebKit/WebProcess/WebPage/Cocoa/WebPageCocoa.mm:
(WebKit::WebPage::getRangeforUUID):
(WebKit::WebPage::getTextIndicatorForID):
(WebKit::WebPage::updateTextIndicatorStyleVisibilityForID):
* Source/WebKit/WebProcess/WebPage/WebPage.h:
* Tools/TestWebKitAPI/Tests/WebCore/MarkedText.cpp:
(WebCore::operator<<):

Canonical link: https://commits.webkit.org/278276@main
  • Loading branch information
megangardner committed May 2, 2024
1 parent c97a08d commit 8ba4bc7
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 15 deletions.
2 changes: 2 additions & 0 deletions Source/WebCore/dom/DocumentMarker.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class DocumentMarker : public CanMakeWeakPtr<DocumentMarker> {
#if ENABLE(UNIFIED_TEXT_REPLACEMENT)
UnifiedTextReplacement = 1 << 16,
#endif
TransparentContent = 1 << 17,
};

static constexpr OptionSet<Type> allMarkers();
Expand Down Expand Up @@ -192,6 +193,7 @@ constexpr auto DocumentMarker::allMarkers() -> OptionSet<Type>
#if ENABLE(UNIFIED_TEXT_REPLACEMENT)
Type::UnifiedTextReplacement,
#endif
Type::TransparentContent,
};
}

Expand Down
1 change: 1 addition & 0 deletions Source/WebCore/dom/DocumentMarkerController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ static bool shouldInsertAsSeparateMarker(const DocumentMarker& marker)
return true;
#endif

case DocumentMarker::Type::TransparentContent:
case DocumentMarker::Type::DraggedContent:
return is<RenderReplaced>(std::get<RefPtr<Node>>(marker.data())->renderer());

Expand Down
1 change: 1 addition & 0 deletions Source/WebCore/rendering/MarkedText.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ Vector<MarkedText> MarkedText::collectForDocumentMarkers(const RenderText& rende

BFALLTHROUGH;
#endif
case DocumentMarker::Type::TransparentContent:
case DocumentMarker::Type::DictationAlternatives:
case DocumentMarker::Type::Grammar:
#if PLATFORM(IOS_FAMILY)
Expand Down
1 change: 1 addition & 0 deletions Source/WebCore/rendering/MarkedText.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ struct MarkedText : public CanMakeCheckedPtr<MarkedText> {
#endif
Selection,
DraggedContent,
TransparentContent,
};

enum class PaintPhase {
Expand Down
15 changes: 11 additions & 4 deletions Source/WebCore/rendering/RenderReplaced.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ bool RenderReplaced::shouldDrawSelectionTint() const
return selectionState() != HighlightState::None && !document().printing();
}

inline static bool draggedContentContainsReplacedElement(const Vector<WeakPtr<RenderedDocumentMarker>>& markers, const Element& element)
inline static bool contentContainsReplacedElement(const Vector<WeakPtr<RenderedDocumentMarker>>& markers, const Element& element)
{
for (auto& marker : markers) {
if (std::get<RefPtr<Node>>(marker->data()) == &element)
Expand Down Expand Up @@ -246,9 +246,16 @@ void RenderReplaced::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
if (element() && element()->parentOrShadowHostElement()) {
auto* parentContainer = element()->parentOrShadowHostElement();
ASSERT(parentContainer);
if (CheckedPtr markers = document().markersIfExists(); markers && draggedContentContainsReplacedElement(markers->markersFor(*parentContainer, DocumentMarker::Type::DraggedContent), *element())) {
savedGraphicsContext.save();
paintInfo.context().setAlpha(0.25);
CheckedPtr markers = document().markersIfExists();
if (markers) {
if (contentContainsReplacedElement(markers->markersFor(*parentContainer, DocumentMarker::Type::DraggedContent), *element())) {
savedGraphicsContext.save();
paintInfo.context().setAlpha(0.25);
}
if (contentContainsReplacedElement(markers->markersFor(*parentContainer, DocumentMarker::Type::TransparentContent), *element())) {
savedGraphicsContext.save();
paintInfo.context().setAlpha(0.0);
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions Source/WebCore/rendering/StyledMarkedText.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ static StyledMarkedText resolveStyleForMarkedText(const MarkedText& markedText,
case MarkedText::Type::DraggedContent:
style.alpha = 0.25;
break;
case MarkedText::Type::TransparentContent:
style.alpha = 0.0;
break;
case MarkedText::Type::Selection: {
style.textStyles = computeTextSelectionPaintStyle(style.textStyles, renderer, lineStyle, paintInfo, style.textShadow);

Expand Down
11 changes: 6 additions & 5 deletions Source/WebCore/rendering/TextBoxPainter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,9 @@ void TextBoxPainter<TextBoxPath>::paintForeground(const StyledMarkedText& marked
if (auto* debugShadow = debugTextShadow())
textPainter.setShadow(debugShadow);

GraphicsContextStateSaver stateSaver(context, markedText.style.textStyles.strokeWidth > 0 || markedText.type == MarkedText::Type::DraggedContent);
if (markedText.type == MarkedText::Type::DraggedContent)
bool isTransparentMarkedText = markedText.type == MarkedText::Type::DraggedContent || markedText.type == MarkedText::Type::TransparentContent;
GraphicsContextStateSaver stateSaver(context, markedText.style.textStyles.strokeWidth > 0 || isTransparentMarkedText);
if (isTransparentMarkedText)
context.setAlpha(markedText.style.alpha);
updateGraphicsContext(context, markedText.style.textStyles);

Expand All @@ -539,10 +540,10 @@ TextDecorationPainter TextBoxPainter<TextBoxPath>::createDecorationPainter(const
// Note that if the text is truncated, we let the thing being painted in the truncation
// draw its own decoration.
GraphicsContextStateSaver stateSaver { context, false };
bool isDraggedContent = markedText.type == MarkedText::Type::DraggedContent;
if (isDraggedContent || !clipOutRect.isEmpty()) {
bool isTransparentContent = markedText.type == MarkedText::Type::DraggedContent || markedText.type == MarkedText::Type::TransparentContent;
if (isTransparentContent || !clipOutRect.isEmpty()) {
stateSaver.save();
if (isDraggedContent)
if (isTransparentContent)
context.setAlpha(markedText.style.alpha);
if (!clipOutRect.isEmpty())
context.clipOut(clipOutRect);
Expand Down
42 changes: 36 additions & 6 deletions Source/WebKit/WebProcess/WebPage/Cocoa/WebPageCocoa.mm
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,19 @@
}

#if ENABLE(UNIFIED_TEXT_REPLACEMENT)
std::optional<WebCore::SimpleRange> WebPage::getRangeforUUID(const WTF::UUID& uuid)
{
auto range = m_unifiedTextReplacementController->contextRangeForSessionWithUUID(uuid);
if (range)
return range;

RefPtr liveRange = m_textIndicatorStyleEnablementRanges.get(uuid);
return WebCore::makeSimpleRange(liveRange);
}

void WebPage::getTextIndicatorForID(const WTF::UUID& uuid, CompletionHandler<void(std::optional<WebCore::TextIndicatorData>&&)>&& completionHandler)
{
auto sessionRange = m_unifiedTextReplacementController->contextRangeForSessionWithUUID(uuid);
if (!sessionRange) {
if (RefPtr liveRange = m_textIndicatorStyleEnablementRanges.get(uuid))
sessionRange = WebCore::makeSimpleRange(liveRange);
}
auto sessionRange = getRangeforUUID(uuid);

if (!sessionRange) {
completionHandler(std::nullopt);
Expand All @@ -341,7 +347,31 @@

void WebPage::updateTextIndicatorStyleVisibilityForID(const WTF::UUID uuid, bool visible, CompletionHandler<void()>&& completionHandler)
{
// FIXME: Turn on/off the visibility.
RefPtr frame = m_page->checkedFocusController()->focusedOrMainFrame();
if (!frame) {
ASSERT_NOT_REACHED();
completionHandler();
return;
}

RefPtr document = frame->document();
if (!document) {
ASSERT_NOT_REACHED();
completionHandler();
return;
}

auto sessionRange = getRangeforUUID(uuid);

if (!sessionRange) {
completionHandler();
return;
}

if (visible)
document->markers().removeMarkers(*sessionRange, { DocumentMarker::Type::TransparentContent });
else
document->markers().addMarker(*sessionRange, DocumentMarker::Type::TransparentContent);

completionHandler();
}
Expand Down
2 changes: 2 additions & 0 deletions Source/WebKit/WebProcess/WebPage/WebPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -2244,6 +2244,8 @@ class WebPage : public API::ObjectImpl<API::Object::Type::BundlePage>, public IP

void textReplacementSessionDidReceiveEditAction(const WTF::UUID&, WebKit::WebTextReplacementDataEditAction);

std::optional<WebCore::SimpleRange> getRangeforUUID(const WTF::UUID&);

void getTextIndicatorForID(const WTF::UUID&, CompletionHandler<void(std::optional<WebCore::TextIndicatorData>&&)>&&);

void updateTextIndicatorStyleVisibilityForID(const WTF::UUID, bool, CompletionHandler<void()>&&);
Expand Down
2 changes: 2 additions & 0 deletions Tools/TestWebKitAPI/Tests/WebCore/MarkedText.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ std::ostream& operator<<(std::ostream& os, MarkedText::Type type)
case MarkedText::Type::UnifiedTextReplacement:
return os << "UnifiedTextReplacement";
#endif
case MarkedText::Type::TransparentContent:
return os << "TransparentContent";
case MarkedText::Type::Unmarked:
return os << "Unmarked";
}
Expand Down

0 comments on commit 8ba4bc7

Please sign in to comment.