forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttachmentLayout.mm
More file actions
409 lines (336 loc) · 18.9 KB
/
Copy pathAttachmentLayout.mm
File metadata and controls
409 lines (336 loc) · 18.9 KB
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
/*
* Copyright (C) 2022-2023 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.
*/
#include "config.h"
#include "AttachmentLayout.h"
#if ENABLE(ATTACHMENT_ELEMENT) && PLATFORM(COCOA)
#include "ColorCocoa.h"
#include "ElementInlines.h"
#include "FontCacheCoreText.h"
#include "FrameSelection.h"
#include "GeometryUtilities.h"
#include "RenderTheme.h"
#include <pal/spi/cf/CoreTextSPI.h>
namespace WebCore {
#if PLATFORM(MAC)
constexpr CGFloat attachmentIconSize = 48;
constexpr CGFloat attachmentIconBackgroundPadding = 6;
constexpr CGFloat attachmentIconBackgroundSize = attachmentIconSize + attachmentIconBackgroundPadding;
constexpr CGFloat attachmentIconSelectionBorderThickness = 1;
constexpr CGFloat attachmentIconBackgroundRadius = 3;
constexpr CGFloat attachmentIconToTitleMargin = 2;
constexpr auto attachmentIconBackgroundColor = Color::black.colorWithAlphaByte(30);
constexpr auto attachmentIconBorderColor = Color::white.colorWithAlphaByte(125);
constexpr CGFloat attachmentTitleFontSize = 12;
constexpr CGFloat attachmentTitleBackgroundRadius = 3;
constexpr CGFloat attachmentTitleBackgroundPadding = 3;
constexpr CGFloat attachmentTitleMaximumWidth = 100 - (attachmentTitleBackgroundPadding * 2);
constexpr CFIndex attachmentTitleMaximumLineCount = 2;
constexpr auto attachmentTitleInactiveBackgroundColor = SRGBA<uint8_t> { 204, 204, 204 };
constexpr auto attachmentTitleInactiveTextColor = SRGBA<uint8_t> { 100, 100, 100 };
constexpr CGFloat attachmentSubtitleFontSize = 10;
constexpr int attachmentSubtitleWidthIncrement = 10;
constexpr auto attachmentSubtitleTextColor = SRGBA<uint8_t> { 82, 145, 214 };
constexpr CGFloat attachmentProgressBarWidth = 30;
constexpr CGFloat attachmentProgressBarHeight = 5;
constexpr CGFloat attachmentProgressBarOffset = -9;
constexpr CGFloat attachmentProgressBarBorderWidth = 1;
constexpr auto attachmentProgressBarBackgroundColor = Color::black.colorWithAlphaByte(89);
constexpr auto attachmentProgressBarFillColor = Color::white;
constexpr auto attachmentProgressBarBorderColor = Color::black.colorWithAlphaByte(128);
constexpr CGFloat attachmentPlaceholderBorderRadius = 5;
constexpr auto attachmentPlaceholderBorderColor = Color::black.colorWithAlphaByte(56);
constexpr CGFloat attachmentPlaceholderBorderWidth = 2;
constexpr CGFloat attachmentPlaceholderBorderDashLength = 6;
constexpr CGFloat attachmentMargin = 3;
static Color titleTextColorForAttachment(const RenderAttachment& attachment, AttachmentLayoutStyle style)
{
Color result = RenderTheme::singleton().systemColor(CSSValueCanvastext, attachment.styleColorOptions());
if (style == AttachmentLayoutStyle::Selected) {
if (attachment.frame().selection().isFocusedAndActive())
result = RenderTheme::singleton().systemColor(CSSValueAppleSystemAlternateSelectedText, attachment.styleColorOptions());
else
result = attachmentTitleInactiveTextColor;
}
return result;
}
void AttachmentLayout::layOutTitle(const RenderAttachment& attachment)
{
CFStringRef language = nullptr; // By not specifying a language we use the system language.
auto font = adoptCF(CTFontCreateUIFontForLanguage(kCTFontUIFontSystem, attachmentTitleFontSize, language));
baseline = CGRound(attachmentIconBackgroundSize + attachmentIconToTitleMargin + CTFontGetAscent(font.get()));
wrappingWidth = attachmentTitleMaximumWidth;
widthPadding = attachmentIconBackgroundSize;
String title = attachment.attachmentElement().attachmentTitleForDisplay();
if (title.isEmpty())
return;
NSDictionary *textAttributes = @{
(__bridge id)kCTFontAttributeName: (__bridge id)font.get(),
(__bridge id)kCTForegroundColorAttributeName: (__bridge id)cachedCGColor(titleTextColorForAttachment(attachment, style)).get()
};
buildWrappedLines(title, font.get(), textAttributes, attachmentTitleMaximumLineCount);
CGFloat yOffset = attachmentIconBackgroundSize + attachmentIconToTitleMargin;
unsigned i = 0;
if (!lines.isEmpty()) {
for (auto& line : lines) {
if (i)
yOffset += origins[i - 1].y - origins[i].y;
line.rect.setY(yOffset);
line.backgroundRect = LayoutRect(line.rect);
line.rect.setY(yOffset - origins.last().y);
line.backgroundRect.inflateX(attachmentTitleBackgroundPadding);
line.backgroundRect = encloseRectToDevicePixels(line.backgroundRect, attachment.document().deviceScaleFactor());
// If the text rects are close in size, the curved enclosing background won't
// look right, so make them the same exact size.
if (i) {
float previousBackgroundRectWidth = lines[i-1].backgroundRect.width();
if (std::abs(line.backgroundRect.width() - previousBackgroundRectWidth) < attachmentTitleBackgroundRadius * 4) {
float newBackgroundRectWidth = std::max(previousBackgroundRectWidth, line.backgroundRect.width());
line.backgroundRect.inflateX((newBackgroundRectWidth - line.backgroundRect.width()) / 2);
lines[i-1].backgroundRect.inflateX((newBackgroundRectWidth - previousBackgroundRectWidth) / 2);
}
}
i++;
}
}
}
void AttachmentLayout::layOutSubtitle(const RenderAttachment& attachment)
{
String subtitleText = attachment.attachmentElement().attachmentSubtitleForDisplay();
if (subtitleText.isEmpty())
return;
auto subtitleColor = attachment.style().colorByApplyingColorFilter(attachmentSubtitleTextColor);
CFStringRef language = nullptr; // By not specifying a language we use the system language.
auto font = adoptCF(CTFontCreateUIFontForLanguage(kCTFontUIFontSystem, attachmentSubtitleFontSize, language));
NSDictionary *textAttributes = @{
(__bridge id)kCTFontAttributeName: (__bridge id)font.get(),
(__bridge id)kCTForegroundColorAttributeName: (__bridge id)cachedCGColor(subtitleColor).get()
};
CGFloat yOffset = 0;
if (!lines.isEmpty())
yOffset = lines.last().backgroundRect.maxY();
else
yOffset = attachmentIconBackgroundSize + attachmentIconToTitleMargin;
buildSingleLine(subtitleText, font.get(), textAttributes);
lines.last().rect.setY(yOffset);
subtitleTextRect = LayoutRect(lines.last().rect);
lines.last().rect.setLocation(subtitleTextRect.minXMaxYCorner());
lines.last().rect.setSize(FloatSize(0, 0));
}
AttachmentLayout::AttachmentLayout(const RenderAttachment& attachment, AttachmentLayoutStyle layoutStyle)
: style(layoutStyle)
{
excludeTypographicLeading = false;
layOutTitle(attachment);
layOutSubtitle(attachment);
iconBackgroundRect = FloatRect(0, 0, attachmentIconBackgroundSize, attachmentIconBackgroundSize);
iconRect = iconBackgroundRect;
iconRect.setSize(FloatSize(attachmentIconSize, attachmentIconSize));
iconRect.move(attachmentIconBackgroundPadding / 2, attachmentIconBackgroundPadding / 2);
attachmentRect = iconBackgroundRect;
for (const auto& line : lines)
attachmentRect.unite(line.backgroundRect);
if (!subtitleTextRect.isEmpty()) {
FloatRect roundedSubtitleTextRect = subtitleTextRect;
roundedSubtitleTextRect.inflateX(attachmentSubtitleWidthIncrement - clampToInteger(ceilf(subtitleTextRect.width())) % attachmentSubtitleWidthIncrement);
attachmentRect.unite(roundedSubtitleTextRect);
}
attachmentRect.inflate(attachmentMargin);
attachmentRect = encloseRectToDevicePixels(attachmentRect, attachment.document().deviceScaleFactor());
}
#endif // PLATFORM(MAC)
#if PLATFORM(IOS_FAMILY)
constexpr CGSize attachmentSize = { 160, 119 };
constexpr CGFloat attachmentBorderRadius = 16;
constexpr auto attachmentBorderColor = SRGBA<uint8_t> { 204, 204, 204 };
static CGFloat attachmentBorderThickness = 1;
constexpr auto attachmentProgressColor = SRGBA<uint8_t> { 222, 222, 222 };
constexpr CGFloat attachmentProgressBorderThickness = 3;
constexpr CGFloat attachmentProgressSize = 36;
constexpr CGFloat attachmentIconSize = 48;
constexpr CGFloat attachmentItemMargin = 8;
constexpr CGFloat attachmentWrappingTextMaximumWidth = 140;
constexpr CFIndex attachmentWrappingTextMaximumLineCount = 2;
static BOOL getAttachmentProgress(const RenderAttachment& attachment, float& progress)
{
auto& progressString = attachment.attachmentElement().attributeWithoutSynchronization(HTMLNames::progressAttr);
if (progressString.isEmpty())
return NO;
bool validProgress;
progress = std::max<float>(std::min<float>(progressString.toFloat(&validProgress), 1), 0);
return validProgress;
}
static RetainPtr<CTFontRef> attachmentActionFont()
{
auto style = kCTUIFontTextStyleFootnote;
auto size = contentSizeCategory();
auto attributes = static_cast<CFDictionaryRef>(@{ (id)kCTFontTraitsAttribute: @{ (id)kCTFontSymbolicTrait: @(kCTFontTraitTightLeading | kCTFontTraitEmphasized) } });
auto emphasizedFontDescriptor = adoptCF(CTFontDescriptorCreateWithTextStyleAndAttributes(style, size, attributes));
return adoptCF(CTFontCreateWithFontDescriptor(emphasizedFontDescriptor.get(), 0, nullptr));
}
static RetainPtr<UIColor> attachmentActionColor(const RenderAttachment& attachment)
{
return cocoaColor(attachment.style().visitedDependentColor(CSSPropertyColor));
}
static RetainPtr<CTFontRef> attachmentTitleFont()
{
auto fontDescriptor = adoptCF(CTFontDescriptorCreateWithTextStyle(kCTUIFontTextStyleShortCaption1, contentSizeCategory(), 0));
return adoptCF(CTFontCreateWithFontDescriptor(fontDescriptor.get(), 0, nullptr));
}
static UIColor *attachmentTitleColor(const RenderAttachment& renderer)
{
return cocoaColor(RenderTheme::singleton().systemColor(CSSValueAppleSystemGray, renderer.styleColorOptions())).autorelease();
}
static RetainPtr<CTFontRef> attachmentSubtitleFont() { return attachmentTitleFont(); }
static UIColor *attachmentSubtitleColor(const RenderAttachment& renderer) { return attachmentTitleColor(renderer); }
static CGFloat shortCaptionPointSizeWithContentSizeCategory(CFStringRef contentSizeCategory)
{
auto descriptor = adoptCF(CTFontDescriptorCreateWithTextStyle(kCTUIFontTextStyleShortCaption1, contentSizeCategory, 0));
auto pointSize = adoptCF(CTFontDescriptorCopyAttribute(descriptor.get(), kCTFontSizeAttribute));
return [dynamic_objc_cast<NSNumber>((__bridge id)pointSize.get()) floatValue];
}
static CGFloat attachmentDynamicTypeScaleFactor()
{
CGFloat fixedPointSize = shortCaptionPointSizeWithContentSizeCategory(kCTFontContentSizeCategoryL);
CGFloat dynamicPointSize = shortCaptionPointSizeWithContentSizeCategory(contentSizeCategory());
if (!dynamicPointSize || !fixedPointSize)
return 1;
return std::max<CGFloat>(1, dynamicPointSize / fixedPointSize);
}
AttachmentLayout::AttachmentLayout(const RenderAttachment& attachment, AttachmentLayoutStyle)
{
excludeTypographicLeading = true;
attachmentRect = FloatRect(0, 0, attachment.width().toFloat(), attachment.height().toFloat());
wrappingWidth = attachmentWrappingTextMaximumWidth * attachmentDynamicTypeScaleFactor();
widthPadding = attachmentRect.width();
hasProgress = getAttachmentProgress(attachment, progress);
String title = attachment.attachmentElement().attachmentTitleForDisplay();
String action = attachment.attachmentElement().attachmentActionForDisplay();
String subtitle = attachment.attachmentElement().attachmentSubtitleForDisplay();
CGFloat yOffset = 0;
if (hasProgress) {
progressRect = FloatRect((attachmentRect.width() / 2) - (attachmentProgressSize / 2), 0, attachmentProgressSize, attachmentProgressSize);
yOffset += attachmentProgressSize + attachmentItemMargin;
}
if (action.isEmpty() && !hasProgress) {
FloatSize iconSize = attachment.attachmentElement().iconSize();
icon = attachment.attachmentElement().icon();
if (!icon)
attachment.attachmentElement().requestIconWithSize(FloatSize());
thumbnailIcon = attachment.attachmentElement().thumbnail();
if (thumbnailIcon)
iconSize = largestRectWithAspectRatioInsideRect(thumbnailIcon->size().aspectRatio(), FloatRect(0, 0, attachmentIconSize, attachmentIconSize)).size();
if (thumbnailIcon || icon) {
iconRect = FloatRect(FloatPoint((attachmentRect.width() / 2) - (iconSize.width() / 2), 0), iconSize);
yOffset += iconRect.height() + attachmentItemMargin;
}
} else {
NSDictionary *textAttributesTitle = @{
(id)kCTFontAttributeName: (id)attachmentActionFont().get(),
(id)kCTForegroundColorAttributeName: attachmentActionColor(attachment).get()
};
buildWrappedLines(action, attachmentActionFont().get(), textAttributesTitle, attachmentWrappingTextMaximumLineCount);
}
bool forceSingleLineTitle = !action.isEmpty() || !subtitle.isEmpty() || hasProgress;
NSDictionary *textAttributesTitle = @{
(id)kCTFontAttributeName: (id)attachmentTitleFont().get(),
(id)kCTForegroundColorAttributeName: attachmentTitleColor(attachment)
};
buildWrappedLines(title, attachmentTitleFont().get(), textAttributesTitle, forceSingleLineTitle ? 1 : attachmentWrappingTextMaximumLineCount);
NSDictionary *textAttributesSubTitle = @{
(id)kCTFontAttributeName: (id)attachmentSubtitleFont().get(),
(id)kCTForegroundColorAttributeName: attachmentSubtitleColor(attachment)
};
buildSingleLine(subtitle, attachmentSubtitleFont().get(), textAttributesSubTitle);
if (!lines.isEmpty()) {
for (auto& line : lines) {
line.rect.setY(yOffset);
yOffset += line.rect.height() + attachmentItemMargin;
}
}
yOffset -= attachmentItemMargin;
contentYOrigin = (attachmentRect.height() / 2) - (yOffset / 2);
}
#endif // PLATFORM(IOS_FAMILY)
void AttachmentLayout::addLine(CTFontRef font, CTLineRef line, bool isSubtitle = false)
{
CGRect lineBounds = CTLineGetBoundsWithOptions(line, excludeTypographicLeading ? kCTLineBoundsExcludeTypographicLeading : 0);
CGFloat trailingWhitespaceWidth = CTLineGetTrailingWhitespaceWidth(line);
CGFloat lineWidthIgnoringTrailingWhitespace = lineBounds.size.width - trailingWhitespaceWidth;
CGFloat lineHeight = lineBounds.size.height + (excludeTypographicLeading ? lineBounds.origin.y : 0);
lineHeight = isSubtitle ? lineHeight : CGCeiling(lineHeight);
CGFloat xOffset = (widthPadding / 2) - (lineWidthIgnoringTrailingWhitespace / 2);
LabelLine labelLine;
labelLine.font = font;
labelLine.line = line;
labelLine.rect = FloatRect(xOffset, 0, lineWidthIgnoringTrailingWhitespace, lineHeight);
lines.append(labelLine);
}
void AttachmentLayout::buildWrappedLines(String& text, CTFontRef font, NSDictionary *textAttributes, unsigned maximumLineCount)
{
if (text.isEmpty())
return;
auto attributedText = adoptNS([[NSAttributedString alloc] initWithString:text attributes:textAttributes]);
auto framesetter = adoptCF(CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attributedText.get()));
CFRange fitRange;
auto textSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter.get(), CFRangeMake(0, 0), nullptr, CGSizeMake(wrappingWidth, CGFLOAT_MAX), &fitRange);
auto textPath = adoptCF(CGPathCreateWithRect(CGRectMake(0, 0, textSize.width, textSize.height), nullptr));
auto textFrame = adoptCF(CTFramesetterCreateFrame(framesetter.get(), fitRange, textPath.get(), nullptr));
auto ctLines = CTFrameGetLines(textFrame.get());
auto lineCount = CFArrayGetCount(ctLines);
if (!lineCount)
return;
origins.resize(lineCount);
CTFrameGetLineOrigins(textFrame.get(), CFRangeMake(0, 0), origins.data());
// Lay out and record the first (maximumLineCount - 1) lines.
CFIndex lineIndex = 0;
auto nonTruncatedLineCount = std::min<CFIndex>(maximumLineCount - 1, lineCount);
for (; lineIndex < nonTruncatedLineCount; ++lineIndex)
addLine(font, (CTLineRef)CFArrayGetValueAtIndex(ctLines, lineIndex));
if (lineIndex == lineCount)
return;
// We had text that didn't fit in the first (maximumLineCount - 1) lines.
// Combine it into one last line, and center-truncate it.
auto firstRemainingLine = (CTLineRef)CFArrayGetValueAtIndex(ctLines, lineIndex);
auto remainingRangeStart = CTLineGetStringRange(firstRemainingLine).location;
auto remainingRange = CFRangeMake(remainingRangeStart, [attributedText length] - remainingRangeStart);
auto remainingPath = adoptCF(CGPathCreateWithRect(CGRectMake(0, 0, CGFLOAT_MAX, CGFLOAT_MAX), nullptr));
auto remainingFrame = adoptCF(CTFramesetterCreateFrame(framesetter.get(), remainingRange, remainingPath.get(), nullptr));
auto ellipsisString = adoptNS([[NSAttributedString alloc] initWithString:@"\u2026" attributes:textAttributes]);
auto ellipsisLine = adoptCF(CTLineCreateWithAttributedString((CFAttributedStringRef)ellipsisString.get()));
auto remainingLine = (CTLineRef)CFArrayGetValueAtIndex(CTFrameGetLines(remainingFrame.get()), 0);
auto truncatedLine = adoptCF(CTLineCreateTruncatedLine(remainingLine, wrappingWidth, kCTLineTruncationMiddle, ellipsisLine.get()));
if (!truncatedLine)
truncatedLine = remainingLine;
addLine(font, truncatedLine.get());
}
void AttachmentLayout::buildSingleLine(const String& text, CTFontRef font, NSDictionary *textAttributes)
{
if (text.isEmpty())
return;
RetainPtr<NSAttributedString> attributedText = adoptNS([[NSAttributedString alloc] initWithString:text attributes:textAttributes]);
addLine(font, adoptCF(CTLineCreateWithAttributedString((CFAttributedStringRef)attributedText.get())).get(), true);
}
} // namespace WebCore
#endif // ENABLE(ATTACHMENT_ELEMENT) && PLATFORM(COCOA)