summaryrefslogtreecommitdiff
path: root/Source/WebCore/rendering/TextAutosizer.cpp
blob: 0824aa336bec31e7a4ca91e4b3b718b4d3dbccad (plain)
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
/*
 * Copyright (C) 2012 Google Inc. All rights reserved.
 * Copyright (C) 2012 Apple Inc. All rights reserved.
 *
 * 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"

#if ENABLE(TEXT_AUTOSIZING)

#include "TextAutosizer.h"

#include "Document.h"
#include "InspectorInstrumentation.h"
#include "RenderObject.h"
#include "RenderText.h"
#include "RenderView.h"
#include "Settings.h"

namespace WebCore {

TextAutosizer::TextAutosizer(Document* document)
    : m_document(document)
{
}

TextAutosizer::~TextAutosizer()
{
}

bool TextAutosizer::processSubtree(RenderObject* layoutRoot)
{
    // FIXME: Text Autosizing should only be enabled when m_document->page()->mainFrame()->view()->useFixedLayout()
    // is true, but for now it's useful to ignore this so that it can be tested on desktop.
    if (!m_document->settings() || !m_document->settings()->textAutosizingEnabled() || layoutRoot->view()->printing() || !m_document->page())
        return false;

    IntSize windowSize = m_document->settings()->textAutosizingWindowSizeOverride();
    if (windowSize.isEmpty()) {
        Frame* mainFrame = m_document->page()->mainFrame();
        bool includeScrollbars = !InspectorInstrumentation::shouldApplyScreenWidthOverride(mainFrame);
        windowSize = mainFrame->view()->visibleContentRect(includeScrollbars).size(); // FIXME: Check that this is always in logical (density-independent) pixels (see wkbug.com/87440).
    }

    for (RenderObject* descendant = traverseNext(layoutRoot, layoutRoot); descendant; descendant = traverseNext(descendant, layoutRoot)) {
        if (!treatAsInline(descendant))
            processBlock(toRenderBlock(descendant), windowSize);
    }

    return true;
}

void TextAutosizer::processBlock(RenderBlock* block, const IntSize& windowSize)
{
    int windowLogicalWidth = block->isHorizontalWritingMode() ? windowSize.width() : windowSize.height();
    float multiplier = static_cast<float>(block->logicalWidth()) / windowLogicalWidth; // FIXME: This is overly simplistic.
    if (multiplier < 1)
        return;
    for (RenderObject* descendant = traverseNext(block, block, treatAsInline); descendant; descendant = traverseNext(descendant, block, treatAsInline)) {
        if (descendant->isText())
            processText(toRenderText(descendant), multiplier);
    }
}

void TextAutosizer::processText(RenderText* text, float multiplier)
{
    float specifiedSize = text->style()->fontDescription().specifiedSize();
    float newSize = specifiedSize * multiplier; // FIXME: This is overly simplistic.

    RefPtr<RenderStyle> style = RenderStyle::clone(text->style());
    FontDescription fontDescription(style->fontDescription());
    fontDescription.setComputedSize(newSize);
    style->setFontDescription(fontDescription);
    style->font().update(style->font().fontSelector());
    text->setStyle(style.release());

    // FIXME: Increase computed line height proportionately.
    // FIXME: Increase list marker size proportionately.
}

bool TextAutosizer::treatAsInline(const RenderObject* renderer)
{
    return !renderer->isRenderBlock() || renderer->isListItem() || renderer->isInlineBlockOrInlineTable();
}

// FIXME: Consider making this a method on RenderObject if it remains this generic.
RenderObject* TextAutosizer::traverseNext(RenderObject* current, const RenderObject* stayWithin, RenderObjectFilter filter)
{
    for (RenderObject* child = current->firstChild(); child; child = child->nextSibling()) {
        if (!filter || filter(child)) {
            ASSERT(!stayWithin || child->isDescendantOf(stayWithin));
            return child;
        }
    }

    for (RenderObject* ancestor = current; ancestor; ancestor = ancestor->parent()) {
        if (ancestor == stayWithin)
            return 0;
        for (RenderObject* sibling = ancestor->nextSibling(); sibling; sibling = sibling->nextSibling()) {
            if (!filter || filter(sibling)) {
                ASSERT(!stayWithin || sibling->isDescendantOf(stayWithin));
                return sibling;
            }
        }
    }

    return 0;
}

} // namespace WebCore

#endif // ENABLE(TEXT_AUTOSIZING)