1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
|
/*
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* (C) 1999 Antti Koivisto (koivisto@kde.org)
* Copyright (C) 2003-2022 Apple Inc. All rights reserved.
* Copyright (C) 2006 Andrew Wellington (proton@wiretapped.net)
* Copyright (C) 2010 Daniel Bates (dbates@intudata.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include "RenderListMarker.h"
#include "CSSCounterStyleRegistry.h"
#include "Document.h"
#include "FontCascade.h"
#include "GraphicsContext.h"
#include "ListStyleType.h"
#include "RenderBlockInlines.h"
#include "RenderBoxInlines.h"
#include "RenderLayer.h"
#include "RenderListItem.h"
#include "RenderMultiColumnFlow.h"
#include "RenderMultiColumnSpannerPlaceholder.h"
#include "RenderView.h"
#include "StyleScope.h"
#include <wtf/StackStats.h>
#include <wtf/TZoneMallocInlines.h>
#include <wtf/text/MakeString.h>
#include <wtf/unicode/CharacterNames.h>
namespace WebCore {
WTF_MAKE_TZONE_OR_ISO_ALLOCATED_IMPL(RenderListMarker);
RenderListMarker::RenderListMarker(RenderListItem& listItem, RenderStyle&& style)
: RenderBox(Type::ListMarker, listItem.document(), WTFMove(style))
, m_listItem(listItem)
{
setInline(true);
setReplacedOrAtomicInline(true); // pretend to be replaced
ASSERT(isRenderListMarker());
}
// Do not add any code in below destructor. Add it to willBeDestroyed() instead.
RenderListMarker::~RenderListMarker() = default;
void RenderListMarker::willBeDestroyed()
{
if (m_image)
m_image->removeClient(*this);
RenderBox::willBeDestroyed();
}
static StyleDifference adjustedStyleDifference(StyleDifference diff, const RenderStyle& oldStyle, const RenderStyle& newStyle)
{
if (diff >= StyleDifference::Layout)
return diff;
// FIXME: Preferably we do this at RenderStyle::changeRequiresLayout but checking against pseudo(::marker) is not sufficient.
auto needsLayout = oldStyle.listStylePosition() != newStyle.listStylePosition() || oldStyle.listStyleType() != newStyle.listStyleType() || oldStyle.isDisplayInlineType() != newStyle.isDisplayInlineType();
return needsLayout ? StyleDifference::Layout : diff;
}
void RenderListMarker::styleWillChange(StyleDifference diff, const RenderStyle& newStyle)
{
RenderBox::styleWillChange(adjustedStyleDifference(diff, style(), newStyle), newStyle);
}
void RenderListMarker::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
{
if (oldStyle)
diff = adjustedStyleDifference(diff, *oldStyle, style());
RenderBox::styleDidChange(diff, oldStyle);
if (m_image != style().listStyleImage()) {
if (m_image)
m_image->removeClient(*this);
m_image = style().listStyleImage();
if (m_image)
m_image->addClient(*this);
}
}
bool RenderListMarker::isImage() const
{
return m_image && !m_image->errorOccurred();
}
LayoutRect RenderListMarker::localSelectionRect()
{
return LayoutRect(LayoutPoint(), size());
}
static String reversed(StringView string)
{
auto length = string.length();
if (length <= 1)
return string.toString();
std::span<UChar> characters;
auto result = String::createUninitialized(length, characters);
for (unsigned i = 0; i < length; ++i)
characters[i] = string[length - i - 1];
return result;
}
struct TextRunWithUnderlyingString {
TextRun textRun;
String underlyingString;
operator const TextRun&() const { return textRun; }
};
static auto textRunForContent(ListMarkerTextContent textContent, const RenderStyle& style) -> TextRunWithUnderlyingString
{
ASSERT(!textContent.isEmpty());
// Since the bidi algorithm doesn't run on this text, we instead reorder the characters here.
// We use u_charDirection to figure out if the marker text is RTL and assume the suffix matches the surrounding direction.
String textForRun;
if (textContent.textDirection == TextDirection::LTR) {
if (style.writingMode().isBidiLTR())
textForRun = textContent.textWithSuffix;
else
textForRun = makeString(reversed(textContent.suffix()), textContent.textWithoutSuffix());
} else {
if (!style.writingMode().isBidiLTR())
textForRun = reversed(textContent.textWithSuffix);
else
textForRun = makeString(reversed(textContent.textWithoutSuffix()), textContent.suffix());
}
auto textRun = RenderBlock::constructTextRun(textForRun, style);
return { WTFMove(textRun), WTFMove(textForRun) };
}
void RenderListMarker::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
if (paintInfo.phase != PaintPhase::Foreground && paintInfo.phase != PaintPhase::Accessibility)
return;
if (style().usedVisibility() != Visibility::Visible)
return;
LayoutPoint boxOrigin(paintOffset + location());
LayoutRect overflowRect(visualOverflowRect());
overflowRect.moveBy(boxOrigin);
if (!paintInfo.rect.intersects(overflowRect))
return;
LayoutRect box(boxOrigin, size());
auto markerRect = relativeMarkerRect();
markerRect.moveBy(boxOrigin);
if (paintInfo.phase == PaintPhase::Accessibility) {
paintInfo.accessibilityRegionContext()->takeBounds(*this, markerRect);
return;
}
if (markerRect.isEmpty())
return;
GraphicsContext& context = paintInfo.context();
if (isImage()) {
if (RefPtr markerImage = m_image->image(this, markerRect.size()))
context.drawImage(*markerImage, markerRect);
if (selectionState() != HighlightState::None) {
LayoutRect selectionRect = localSelectionRect();
selectionRect.moveBy(boxOrigin);
context.fillRect(snappedIntRect(selectionRect), m_listItem->selectionBackgroundColor());
}
return;
}
if (selectionState() != HighlightState::None) {
LayoutRect selectionRect = localSelectionRect();
selectionRect.moveBy(boxOrigin);
context.fillRect(snappedIntRect(selectionRect), m_listItem->selectionBackgroundColor());
}
auto color = style().visitedDependentColorWithColorFilter(CSSPropertyColor);
context.setStrokeColor(color);
context.setStrokeStyle(StrokeStyle::SolidStroke);
context.setStrokeThickness(1.0f);
context.setFillColor(color);
auto listStyleType = style().listStyleType();
if (listStyleType.isDisc()) {
context.fillEllipse(markerRect);
return;
}
if (listStyleType.isCircle()) {
context.strokeEllipse(markerRect);
return;
}
if (listStyleType.isSquare()) {
context.fillRect(markerRect);
return;
}
if (m_textContent.isEmpty())
return;
GraphicsContextStateSaver stateSaver(context, false);
if (!writingMode().isHorizontal()) {
markerRect.moveBy(-boxOrigin);
markerRect = markerRect.transposedRect();
markerRect.moveBy(FloatPoint(box.x(), box.y() - logicalHeight()));
stateSaver.save();
context.translate(markerRect.x(), markerRect.maxY());
context.rotate(static_cast<float>(deg2rad(90.)));
context.translate(-markerRect.x(), -markerRect.maxY());
}
FloatPoint textOrigin = FloatPoint(markerRect.x(), markerRect.y() + style().metricsOfPrimaryFont().intAscent());
textOrigin = roundPointToDevicePixels(LayoutPoint(textOrigin), document().deviceScaleFactor(), writingMode().isLogicalLeftInlineStart());
context.drawText(style().fontCascade(), textRunForContent(m_textContent, style()), textOrigin);
}
RenderBox* RenderListMarker::parentBox(RenderBox& box)
{
ASSERT(m_listItem);
CheckedPtr multiColumnFlow = dynamicDowncast<RenderMultiColumnFlow>(m_listItem->enclosingFragmentedFlow());
if (!multiColumnFlow)
return box.parentBox();
auto* placeholder = multiColumnFlow->findColumnSpannerPlaceholder(&box);
return placeholder ? placeholder->parentBox() : box.parentBox();
};
void RenderListMarker::layout()
{
StackStats::LayoutCheckPoint layoutCheckPoint;
ASSERT(needsLayout());
LayoutUnit blockOffset;
for (auto* ancestor = parentBox(*this); ancestor && ancestor != m_listItem.get(); ancestor = parentBox(*ancestor))
blockOffset += ancestor->logicalTop();
m_lineLogicalOffsetForListItem = m_listItem->logicalLeftOffsetForLine(blockOffset);
m_lineOffsetForListItem = writingMode().isLogicalLeftInlineStart() ? m_lineLogicalOffsetForListItem : m_listItem->logicalRightOffsetForLine(blockOffset);
if (isImage()) {
updateInlineMarginsAndContent();
setWidth(m_image->imageSize(this, style().usedZoom()).width());
setHeight(m_image->imageSize(this, style().usedZoom()).height());
} else {
setLogicalWidth(minPreferredLogicalWidth());
setLogicalHeight(style().metricsOfPrimaryFont().intHeight());
}
setMarginStart(0);
setMarginEnd(0);
Length startMargin = style().marginStart();
Length endMargin = style().marginEnd();
if (startMargin.isFixed())
setMarginStart(LayoutUnit(startMargin.value()));
if (endMargin.isFixed())
setMarginEnd(LayoutUnit(endMargin.value()));
clearNeedsLayout();
}
void RenderListMarker::imageChanged(WrappedImagePtr o, const IntRect* rect)
{
if (parent()) {
if (m_image && o == m_image->data()) {
if (width() != m_image->imageSize(this, style().usedZoom()).width() || height() != m_image->imageSize(this, style().usedZoom()).height() || m_image->errorOccurred())
setNeedsLayoutAndPrefWidthsRecalc();
else
repaint();
}
}
RenderBox::imageChanged(o, rect);
}
void RenderListMarker::updateInlineMarginsAndContent()
{
// FIXME: It's messy to use the preferredLogicalWidths dirty bit for this optimization, also unclear if this is premature optimization.
if (preferredLogicalWidthsDirty())
updateContent();
updateInlineMargins();
}
void RenderListMarker::updateContent()
{
if (isImage()) {
// FIXME: This is a somewhat arbitrary width.
LayoutUnit bulletWidth = style().metricsOfPrimaryFont().intAscent() / 2_lu;
LayoutSize defaultBulletSize(bulletWidth, bulletWidth);
LayoutSize imageSize = calculateImageIntrinsicDimensions(m_image.get(), defaultBulletSize, ScaleByUsedZoom::No);
m_image->setContainerContextForRenderer(*this, imageSize, style().usedZoom());
m_textContent = {
.textWithSuffix = emptyString(),
.textWithoutSuffixLength = 0,
.textDirection = TextDirection::LTR,
};
return;
}
auto contentTextDirection = [&](auto content) {
if (!content.length())
return TextDirection::LTR;
// FIXME: Depending on the string value, we may need the real bidi algorithm. (rdar://106139180)
// Also we may need to start checking for the entire content for directionality (and whether we need to check for additional
// directionality characters like U_RIGHT_TO_LEFT_EMBEDDING).
auto bidiCategory = u_charDirection(content[0]);
if (bidiCategory != U_RIGHT_TO_LEFT && bidiCategory != U_RIGHT_TO_LEFT_ARABIC)
return TextDirection::LTR;
return TextDirection::RTL;
};
auto styleType = style().listStyleType();
switch (styleType.type) {
case ListStyleType::Type::String: {
m_textContent = {
.textWithSuffix = styleType.identifier,
.textWithoutSuffixLength = styleType.identifier.length(),
.textDirection = contentTextDirection(StringView { styleType.identifier }),
};
break;
}
case ListStyleType::Type::CounterStyle: {
auto counter = counterStyle();
ASSERT(counter);
auto text = makeString(counter->prefix().text, counter->text(m_listItem->value(), writingMode()));
m_textContent = {
.textWithSuffix = makeString(text, counter->suffix().text),
.textWithoutSuffixLength = text.length(),
.textDirection = contentTextDirection(text),
};
break;
}
case ListStyleType::Type::None:
m_textContent = {
.textWithSuffix = " "_s,
.textWithoutSuffixLength = 0,
.textDirection = TextDirection::LTR,
};
break;
}
}
void RenderListMarker::computePreferredLogicalWidths()
{
ASSERT(preferredLogicalWidthsDirty());
updateContent();
if (isImage()) {
LayoutSize imageSize = LayoutSize(m_image->imageSize(this, style().usedZoom()));
m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = writingMode().isHorizontal() ? imageSize.width() : imageSize.height();
setPreferredLogicalWidthsDirty(false);
updateInlineMargins();
return;
}
const FontCascade& font = style().fontCascade();
LayoutUnit logicalWidth;
if (widthUsesMetricsOfPrimaryFont())
logicalWidth = (font.metricsOfPrimaryFont().intAscent() * 2 / 3 + 1) / 2 + 2;
else if (!m_textContent.isEmpty())
logicalWidth = font.width(textRunForContent(m_textContent, style()));
m_minPreferredLogicalWidth = logicalWidth;
m_maxPreferredLogicalWidth = logicalWidth;
setPreferredLogicalWidthsDirty(false);
updateInlineMargins();
}
void RenderListMarker::updateInlineMargins()
{
constexpr int markerPadding = 7;
const FontMetrics& fontMetrics = style().metricsOfPrimaryFont();
auto marginsForInsideMarker = [&]() -> std::pair<LayoutUnit, LayoutUnit> {
if (isImage())
return { 0, markerPadding };
if (widthUsesMetricsOfPrimaryFont())
return { -1, fontMetrics.intAscent() - minPreferredLogicalWidth() + 1 };
return { };
};
auto marginsForOutsideMarker = [&]() -> std::pair<LayoutUnit, LayoutUnit> {
if (isImage())
return { -minPreferredLogicalWidth() - markerPadding, markerPadding };
int offset = fontMetrics.intAscent() * 2 / 3;
if (widthUsesMetricsOfPrimaryFont())
return { -offset - markerPadding - 1, offset + markerPadding + 1 - minPreferredLogicalWidth() };
if (m_textContent.isEmpty())
return { };
if (style().listStyleType().type == ListStyleType::Type::String)
return { -minPreferredLogicalWidth(), 0 };
return { -minPreferredLogicalWidth() - offset / 2, offset / 2 };
};
auto [marginStart, marginEnd] = isInside() ? marginsForInsideMarker() : marginsForOutsideMarker();
mutableStyle().setMarginStart(Length(marginStart, LengthType::Fixed));
mutableStyle().setMarginEnd(Length(marginEnd, LengthType::Fixed));
}
LayoutUnit RenderListMarker::lineHeight(bool firstLine, LineDirectionMode direction, LinePositionMode linePositionMode) const
{
if (!isImage())
return m_listItem->lineHeight(firstLine, direction, PositionOfInteriorLineBoxes);
return RenderBox::lineHeight(firstLine, direction, linePositionMode);
}
LayoutUnit RenderListMarker::baselinePosition(FontBaseline baselineType, bool firstLine, LineDirectionMode direction, LinePositionMode linePositionMode) const
{
if (!isImage())
return m_listItem->baselinePosition(baselineType, firstLine, direction, PositionOfInteriorLineBoxes);
return RenderBox::baselinePosition(baselineType, firstLine, direction, linePositionMode);
}
bool RenderListMarker::isInside() const
{
return style().listStylePosition() == ListStylePosition::Inside;
}
const RenderListItem* RenderListMarker::listItem() const
{
return m_listItem.get();
}
FloatRect RenderListMarker::relativeMarkerRect()
{
if (isImage())
return FloatRect(0, 0, m_image->imageSize(this, style().usedZoom()).width(), m_image->imageSize(this, style().usedZoom()).height());
FloatRect relativeRect;
if (widthUsesMetricsOfPrimaryFont()) {
// FIXME: Are these particular rounding rules necessary?
const FontMetrics& fontMetrics = style().metricsOfPrimaryFont();
int ascent = fontMetrics.intAscent();
int bulletWidth = (ascent * 2 / 3 + 1) / 2;
relativeRect = FloatRect(1, 3 * (ascent - ascent * 2 / 3) / 2, bulletWidth, bulletWidth);
} else {
if (m_textContent.isEmpty())
return FloatRect();
auto& font = style().fontCascade();
relativeRect = FloatRect(0, 0, font.width(textRunForContent(m_textContent, style())), font.metricsOfPrimaryFont().intHeight());
}
if (!writingMode().isHorizontal()) {
relativeRect = relativeRect.transposedRect();
relativeRect.setX(width() - relativeRect.x() - relativeRect.width());
}
return relativeRect;
}
LayoutRect RenderListMarker::selectionRectForRepaint(const RenderLayerModelObject*, bool)
{
ASSERT(!needsLayout());
return LayoutRect();
}
RefPtr<CSSCounterStyle> RenderListMarker::counterStyle() const
{
return document().counterStyleRegistry().resolvedCounterStyle(style().listStyleType());
}
bool RenderListMarker::widthUsesMetricsOfPrimaryFont() const
{
auto listType = style().listStyleType();
return listType.isCircle() || listType.isDisc() || listType.isSquare();
}
} // namespace WebCore
|