| 1 | /* |
| 2 | * Copyright (C) 2014 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "InlineTextBoxStyle.h" |
| 28 | |
| 29 | #include "FontCascade.h" |
| 30 | #include "InlineTextBox.h" |
| 31 | #include "RootInlineBox.h" |
| 32 | #include "TextUnderlineOffset.h" |
| 33 | |
| 34 | namespace WebCore { |
| 35 | |
| 36 | float computeUnderlineOffset(TextUnderlinePosition underlinePosition, TextUnderlineOffset underlineOffset, const FontMetrics& fontMetrics, const InlineTextBox* inlineTextBox, float defaultGap) |
| 37 | { |
| 38 | // This represents the gap between the baseline and the closest edge of the underline. |
| 39 | float gap = std::max<int>(1, std::ceil(defaultGap / 2.0f)); |
| 40 | |
| 41 | // FIXME: The code for visual overflow detection passes in a null inline text box. This means it is now |
| 42 | // broken for the case where auto needs to behave like "under". |
| 43 | |
| 44 | // According to the specification TextUnderlinePosition::Auto should avoid drawing through glyphs in |
| 45 | // scripts where it would not be appropriate (e.g., ideographs). |
| 46 | // Strictly speaking this can occur whenever the line contains ideographs |
| 47 | // even if it is horizontal, but detecting this has performance implications. For now we only work with |
| 48 | // vertical text, since we already determined the baseline type to be ideographic in that |
| 49 | // case. |
| 50 | |
| 51 | auto resolvedUnderlinePosition = underlinePosition; |
| 52 | if (resolvedUnderlinePosition == TextUnderlinePosition::Auto && underlineOffset.isAuto()) { |
| 53 | if (inlineTextBox) |
| 54 | resolvedUnderlinePosition = inlineTextBox->root().baselineType() == IdeographicBaseline ? TextUnderlinePosition::Under : TextUnderlinePosition::Auto; |
| 55 | else |
| 56 | resolvedUnderlinePosition = TextUnderlinePosition::Auto; |
| 57 | } |
| 58 | |
| 59 | switch (resolvedUnderlinePosition) { |
| 60 | case TextUnderlinePosition::Auto: |
| 61 | if (underlineOffset.isAuto()) |
| 62 | return fontMetrics.ascent() + gap; |
| 63 | return fontMetrics.ascent() + std::max(0.0f, underlineOffset.lengthValue()); |
| 64 | case TextUnderlinePosition::FromFont: |
| 65 | return fontMetrics.ascent() + std::max(0.0f, fontMetrics.underlinePosition() + underlineOffset.lengthOr(0)); |
| 66 | case TextUnderlinePosition::Under: { |
| 67 | ASSERT(inlineTextBox); |
| 68 | // Position underline relative to the bottom edge of the lowest element's content box. |
| 69 | const RootInlineBox& rootBox = inlineTextBox->root(); |
| 70 | const RenderElement* decorationRenderer = inlineTextBox->parent()->renderer().enclosingRendererWithTextDecoration(TextDecoration::Underline, inlineTextBox->isFirstLine()); |
| 71 | |
| 72 | float offset; |
| 73 | if (inlineTextBox->renderer().style().isFlippedLinesWritingMode()) { |
| 74 | offset = inlineTextBox->logicalTop(); |
| 75 | rootBox.minLogicalTopForTextDecorationLine(offset, decorationRenderer, TextDecoration::Underline); |
| 76 | offset = inlineTextBox->logicalTop() - offset; |
| 77 | } else { |
| 78 | offset = inlineTextBox->logicalBottom(); |
| 79 | rootBox.maxLogicalBottomForTextDecorationLine(offset, decorationRenderer, TextDecoration::Underline); |
| 80 | offset -= inlineTextBox->logicalBottom(); |
| 81 | } |
| 82 | auto desiredOffset = inlineTextBox->logicalHeight() + gap + std::max(offset, 0.0f) + underlineOffset.lengthOr(0); |
| 83 | return std::max<float>(desiredOffset, fontMetrics.ascent()); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | ASSERT_NOT_REACHED(); |
| 88 | return fontMetrics.ascent() + gap; |
| 89 | } |
| 90 | |
| 91 | WavyStrokeParameters getWavyStrokeParameters(float fontSize) |
| 92 | { |
| 93 | WavyStrokeParameters result; |
| 94 | // More information is in the WavyStrokeParameters definition. |
| 95 | result.controlPointDistance = fontSize * 1.5 / 16; |
| 96 | result.step = fontSize / 4.5; |
| 97 | return result; |
| 98 | } |
| 99 | |
| 100 | static inline void extendIntToFloat(int& extendMe, float extendTo) |
| 101 | { |
| 102 | extendMe = std::max(extendMe, static_cast<int>(ceilf(extendTo))); |
| 103 | } |
| 104 | |
| 105 | GlyphOverflow visualOverflowForDecorations(const RenderStyle& lineStyle, const InlineTextBox* inlineTextBox) |
| 106 | { |
| 107 | ASSERT(!inlineTextBox || inlineTextBox->lineStyle() == lineStyle); |
| 108 | |
| 109 | auto decoration = lineStyle.textDecorationsInEffect(); |
| 110 | if (decoration.isEmpty()) |
| 111 | return GlyphOverflow(); |
| 112 | |
| 113 | float strokeThickness = lineStyle.textDecorationThickness().resolve(lineStyle.computedFontSize(), lineStyle.fontMetrics()); |
| 114 | WavyStrokeParameters wavyStrokeParameters; |
| 115 | float wavyOffset = 0; |
| 116 | |
| 117 | TextDecorationStyle decorationStyle = lineStyle.textDecorationStyle(); |
| 118 | float height = lineStyle.fontCascade().fontMetrics().floatHeight(); |
| 119 | GlyphOverflow overflowResult; |
| 120 | |
| 121 | if (decorationStyle == TextDecorationStyle::Wavy) { |
| 122 | wavyStrokeParameters = getWavyStrokeParameters(lineStyle.computedFontPixelSize()); |
| 123 | wavyOffset = wavyOffsetFromDecoration(); |
| 124 | overflowResult.left = strokeThickness; |
| 125 | overflowResult.right = strokeThickness; |
| 126 | } |
| 127 | |
| 128 | // These metrics must match where underlines get drawn. |
| 129 | // FIXME: Share the code in TextDecorationPainter::paintTextDecoration() so we can just query it for the painted geometry. |
| 130 | if (decoration & TextDecoration::Underline) { |
| 131 | // Compensate for the integral ceiling in GraphicsContext::computeLineBoundsAndAntialiasingModeForText() |
| 132 | int underlineOffset = 1; |
| 133 | float textDecorationBaseFontSize = 16; |
| 134 | auto defaultGap = lineStyle.computedFontSize() / textDecorationBaseFontSize; |
| 135 | underlineOffset += computeUnderlineOffset(lineStyle.textUnderlinePosition(), lineStyle.textUnderlineOffset(), lineStyle.fontMetrics(), inlineTextBox, defaultGap); |
| 136 | if (decorationStyle == TextDecorationStyle::Wavy) { |
| 137 | extendIntToFloat(overflowResult.bottom, underlineOffset + wavyOffset + wavyStrokeParameters.controlPointDistance + strokeThickness - height); |
| 138 | extendIntToFloat(overflowResult.top, -(underlineOffset + wavyOffset - wavyStrokeParameters.controlPointDistance - strokeThickness)); |
| 139 | } else { |
| 140 | extendIntToFloat(overflowResult.bottom, underlineOffset + strokeThickness - height); |
| 141 | extendIntToFloat(overflowResult.top, -underlineOffset); |
| 142 | } |
| 143 | } |
| 144 | if (decoration & TextDecoration::Overline) { |
| 145 | FloatRect rect(FloatPoint(), FloatSize(1, strokeThickness)); |
| 146 | float autoTextDecorationThickness = TextDecorationThickness::createWithAuto().resolve(lineStyle.computedFontSize(), lineStyle.fontMetrics()); |
| 147 | rect.move(0, autoTextDecorationThickness - strokeThickness - wavyOffset); |
| 148 | if (decorationStyle == TextDecorationStyle::Wavy) { |
| 149 | FloatBoxExtent wavyExpansion; |
| 150 | wavyExpansion.setTop(wavyStrokeParameters.controlPointDistance); |
| 151 | wavyExpansion.setBottom(wavyStrokeParameters.controlPointDistance); |
| 152 | rect.expand(wavyExpansion); |
| 153 | } |
| 154 | extendIntToFloat(overflowResult.top, -rect.y()); |
| 155 | extendIntToFloat(overflowResult.bottom, rect.maxY() - height); |
| 156 | } |
| 157 | if (decoration & TextDecoration::LineThrough) { |
| 158 | FloatRect rect(FloatPoint(), FloatSize(1, strokeThickness)); |
| 159 | float autoTextDecorationThickness = TextDecorationThickness::createWithAuto().resolve(lineStyle.computedFontSize(), lineStyle.fontMetrics()); |
| 160 | auto center = 2 * lineStyle.fontMetrics().floatAscent() / 3 + autoTextDecorationThickness / 2; |
| 161 | rect.move(0, center - strokeThickness / 2); |
| 162 | if (decorationStyle == TextDecorationStyle::Wavy) { |
| 163 | FloatBoxExtent wavyExpansion; |
| 164 | wavyExpansion.setTop(wavyStrokeParameters.controlPointDistance); |
| 165 | wavyExpansion.setBottom(wavyStrokeParameters.controlPointDistance); |
| 166 | rect.expand(wavyExpansion); |
| 167 | } |
| 168 | extendIntToFloat(overflowResult.top, -rect.y()); |
| 169 | extendIntToFloat(overflowResult.bottom, rect.maxY() - height); |
| 170 | } |
| 171 | return overflowResult; |
| 172 | } |
| 173 | |
| 174 | } |
| 175 | |