haikuwebkit/Source/WebCore/rendering/TextBoxSelectableRange.h

63 lines
2.2 KiB
C
Raw Permalink Normal View History

Factor selection clamping into a type https://bugs.webkit.org/show_bug.cgi?id=226697 Reviewed by Sam Weinig. TextBoxSelectableRange will be helpful for moving selection code out of the legacy inline boxes. It also allows some code sharing already in this patch. * Headers.cmake: * WebCore.xcodeproj/project.pbxproj: * layout/integration/LayoutIntegrationRunIteratorLegacyPath.h: (WebCore::LayoutIntegration::RunIteratorLegacyPath::isSelectable const): * layout/integration/LayoutIntegrationRunIteratorModernPath.h: (WebCore::LayoutIntegration::RunIteratorModernPath::positionForOffset const): (WebCore::LayoutIntegration::RunIteratorModernPath::isSelectable const): (WebCore::LayoutIntegration::RunIteratorModernPath::selectionRect const): (WebCore::LayoutIntegration::RunIteratorModernPath::selectableRange const): (WebCore::LayoutIntegration::RunIteratorModernPath::clampedOffset const): Deleted. * rendering/LegacyInlineTextBox.cpp: (WebCore::LegacyInlineTextBox::isSelectable const): (WebCore::LegacyInlineTextBox::localSelectionRect const): (WebCore::LegacyInlineTextBox::paint): (WebCore::LegacyInlineTextBox::selectableRange const): (WebCore::LegacyInlineTextBox::clampedStartEndForState const): (WebCore::LegacyInlineTextBox::calculateDocumentMarkerBounds const): (WebCore::LegacyInlineTextBox::collectMarkedTextsForDraggedContent): (WebCore::LegacyInlineTextBox::collectMarkedTextsForDocumentMarkers const): (WebCore::LegacyInlineTextBox::paintCompositionBackground): (WebCore::LegacyInlineTextBox::positionForOffset const): (WebCore::LegacyInlineTextBox::isSelected const): Deleted. (WebCore::LegacyInlineTextBox::clampedOffset const): Deleted. * rendering/LegacyInlineTextBox.h: * rendering/RenderBlockFlow.cpp: (WebCore::RenderBlockFlow::inlineSelectionGaps): * rendering/TextBoxSelectableRange.h: Added. (WebCore::TextBoxSelectableRange::clamp const): (WebCore::TextBoxSelectableRange::intersects const): * rendering/svg/SVGInlineTextBox.cpp: (WebCore::SVGInlineTextBox::localSelectionRect const): Canonical link: https://commits.webkit.org/238543@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@278545 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2021-06-07 06:19:03 +00:00
/*
* Copyright (C) 2021 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <optional>
namespace WebCore {
struct TextBoxSelectableRange {
const unsigned start;
const unsigned length;
const unsigned additionalLengthAtEnd { 0 };
const bool isLineBreak { false };
Factor selection clamping into a type https://bugs.webkit.org/show_bug.cgi?id=226697 Reviewed by Sam Weinig. TextBoxSelectableRange will be helpful for moving selection code out of the legacy inline boxes. It also allows some code sharing already in this patch. * Headers.cmake: * WebCore.xcodeproj/project.pbxproj: * layout/integration/LayoutIntegrationRunIteratorLegacyPath.h: (WebCore::LayoutIntegration::RunIteratorLegacyPath::isSelectable const): * layout/integration/LayoutIntegrationRunIteratorModernPath.h: (WebCore::LayoutIntegration::RunIteratorModernPath::positionForOffset const): (WebCore::LayoutIntegration::RunIteratorModernPath::isSelectable const): (WebCore::LayoutIntegration::RunIteratorModernPath::selectionRect const): (WebCore::LayoutIntegration::RunIteratorModernPath::selectableRange const): (WebCore::LayoutIntegration::RunIteratorModernPath::clampedOffset const): Deleted. * rendering/LegacyInlineTextBox.cpp: (WebCore::LegacyInlineTextBox::isSelectable const): (WebCore::LegacyInlineTextBox::localSelectionRect const): (WebCore::LegacyInlineTextBox::paint): (WebCore::LegacyInlineTextBox::selectableRange const): (WebCore::LegacyInlineTextBox::clampedStartEndForState const): (WebCore::LegacyInlineTextBox::calculateDocumentMarkerBounds const): (WebCore::LegacyInlineTextBox::collectMarkedTextsForDraggedContent): (WebCore::LegacyInlineTextBox::collectMarkedTextsForDocumentMarkers const): (WebCore::LegacyInlineTextBox::paintCompositionBackground): (WebCore::LegacyInlineTextBox::positionForOffset const): (WebCore::LegacyInlineTextBox::isSelected const): Deleted. (WebCore::LegacyInlineTextBox::clampedOffset const): Deleted. * rendering/LegacyInlineTextBox.h: * rendering/RenderBlockFlow.cpp: (WebCore::RenderBlockFlow::inlineSelectionGaps): * rendering/TextBoxSelectableRange.h: Added. (WebCore::TextBoxSelectableRange::clamp const): (WebCore::TextBoxSelectableRange::intersects const): * rendering/svg/SVGInlineTextBox.cpp: (WebCore::SVGInlineTextBox::localSelectionRect const): Canonical link: https://commits.webkit.org/238543@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@278545 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2021-06-07 06:19:03 +00:00
const std::optional<unsigned> truncation { };
unsigned clamp(unsigned offset) const
{
auto clampedOffset = std::clamp(offset, start, start + length) - start;
if (truncation)
return std::min<unsigned>(clampedOffset, *truncation);
if (clampedOffset == length)
clampedOffset += additionalLengthAtEnd;
return clampedOffset;
}
std::pair<unsigned, unsigned> clamp(unsigned startOffset, unsigned endOffset) const
{
return { clamp(startOffset), clamp(endOffset) };
}
bool intersects(unsigned startOffset, unsigned endOffset) const
{
return clamp(startOffset) < clamp(endOffset);
}
};
}