| 1 | /* |
| 2 | * Copyright (C) 2009 Google Inc. All rights reserved. |
| 3 | * Copyright (C) 2011 Apple Inc. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are |
| 7 | * met: |
| 8 | * |
| 9 | * * Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * * Redistributions in binary form must reproduce the above |
| 12 | * copyright notice, this list of conditions and the following disclaimer |
| 13 | * in the documentation and/or other materials provided with the |
| 14 | * distribution. |
| 15 | * * Neither the name of Google Inc. nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived from |
| 17 | * this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #include "config.h" |
| 33 | |
| 34 | #include "RenderRubyText.h" |
| 35 | #include "RenderRubyRun.h" |
| 36 | #include <wtf/IsoMallocInlines.h> |
| 37 | |
| 38 | namespace WebCore { |
| 39 | |
| 40 | WTF_MAKE_ISO_ALLOCATED_IMPL(RenderRubyText); |
| 41 | |
| 42 | RenderRubyText::RenderRubyText(Element& element, RenderStyle&& style) |
| 43 | : RenderBlockFlow(element, WTFMove(style)) |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | RenderRubyText::~RenderRubyText() = default; |
| 48 | |
| 49 | RenderRubyRun* RenderRubyText::rubyRun() const |
| 50 | { |
| 51 | ASSERT(parent()); |
| 52 | return downcast<RenderRubyRun>(parent()); |
| 53 | } |
| 54 | |
| 55 | bool RenderRubyText::isChildAllowed(const RenderObject& child, const RenderStyle&) const |
| 56 | { |
| 57 | return child.isInline(); |
| 58 | } |
| 59 | |
| 60 | TextAlignMode RenderRubyText::textAlignmentForLine(bool endsWithSoftBreak) const |
| 61 | { |
| 62 | TextAlignMode textAlign = style().textAlign(); |
| 63 | // FIXME: This check is bogus since user can set the initial value. |
| 64 | if (textAlign != RenderStyle::initialTextAlign()) |
| 65 | return RenderBlockFlow::textAlignmentForLine(endsWithSoftBreak); |
| 66 | |
| 67 | // The default behavior is to allow ruby text to expand if it is shorter than the ruby base. |
| 68 | return TextAlignMode::Justify; |
| 69 | } |
| 70 | |
| 71 | void RenderRubyText::adjustInlineDirectionLineBounds(int expansionOpportunityCount, float& logicalLeft, float& logicalWidth) const |
| 72 | { |
| 73 | TextAlignMode textAlign = style().textAlign(); |
| 74 | // FIXME: This check is bogus since user can set the initial value. |
| 75 | if (textAlign != RenderStyle::initialTextAlign()) |
| 76 | return RenderBlockFlow::adjustInlineDirectionLineBounds(expansionOpportunityCount, logicalLeft, logicalWidth); |
| 77 | |
| 78 | int maxPreferredLogicalWidth = this->maxPreferredLogicalWidth(); |
| 79 | if (maxPreferredLogicalWidth >= logicalWidth) |
| 80 | return; |
| 81 | |
| 82 | // Inset the ruby text by half the inter-ideograph expansion amount, but no more than a full-width |
| 83 | // ruby character on each side. |
| 84 | float inset = (logicalWidth - maxPreferredLogicalWidth) / (expansionOpportunityCount + 1); |
| 85 | if (expansionOpportunityCount) |
| 86 | inset = std::min<float>(2 * style().computedFontPixelSize(), inset); |
| 87 | |
| 88 | logicalLeft += inset / 2; |
| 89 | logicalWidth -= inset; |
| 90 | } |
| 91 | |
| 92 | bool RenderRubyText::avoidsFloats() const |
| 93 | { |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | bool RenderRubyText::canBreakBefore(const LazyLineBreakIterator& iterator) const |
| 98 | { |
| 99 | // FIXME: It would be nice to improve this so that it isn't just hard-coded, but lookahead in this |
| 100 | // case is particularly problematic. |
| 101 | |
| 102 | if (!iterator.priorContextLength()) |
| 103 | return true; |
| 104 | UChar ch = iterator.lastCharacter(); |
| 105 | ULineBreak lineBreak = (ULineBreak)u_getIntPropertyValue(ch, UCHAR_LINE_BREAK); |
| 106 | // UNICODE LINE BREAKING ALGORITHM |
| 107 | // http://www.unicode.org/reports/tr14/ |
| 108 | // And Requirements for Japanese Text Layout, 3.1.7 Characters Not Starting a Line |
| 109 | // http://www.w3.org/TR/2012/NOTE-jlreq-20120403/#characters_not_starting_a_line |
| 110 | switch (lineBreak) { |
| 111 | case U_LB_NONSTARTER: |
| 112 | case U_LB_CLOSE_PARENTHESIS: |
| 113 | case U_LB_CLOSE_PUNCTUATION: |
| 114 | case U_LB_EXCLAMATION: |
| 115 | case U_LB_BREAK_SYMBOLS: |
| 116 | case U_LB_INFIX_NUMERIC: |
| 117 | case U_LB_ZWSPACE: |
| 118 | case U_LB_WORD_JOINER: |
| 119 | return false; |
| 120 | default: |
| 121 | break; |
| 122 | } |
| 123 | // Special care for Requirements for Japanese Text Layout |
| 124 | switch (ch) { |
| 125 | case 0x2019: // RIGHT SINGLE QUOTATION MARK |
| 126 | case 0x201D: // RIGHT DOUBLE QUOTATION MARK |
| 127 | case 0x00BB: // RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK |
| 128 | case 0x2010: // HYPHEN |
| 129 | case 0x2013: // EN DASH |
| 130 | case 0x300C: // LEFT CORNER BRACKET |
| 131 | return false; |
| 132 | default: |
| 133 | break; |
| 134 | } |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | } // namespace WebCore |
| 139 | |