1/*
2 * Copyright (C) 2013 Google 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 are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32#include "core/rendering/TextRunConstructor.h"
33
34#include "core/rendering/RenderText.h"
35#include "core/rendering/style/RenderStyle.h"
36#include "core/rendering/svg/SVGTextRunRenderingContext.h"
37#include "platform/text/BidiTextRun.h"
38
39namespace blink {
40
41template <typename CharacterType>
42static inline TextRun constructTextRunInternal(RenderObject* context, const Font& font, const CharacterType* characters, int length, RenderStyle* style, TextDirection direction, TextRun::ExpansionBehavior expansion)
43{
44    ASSERT(style);
45
46    bool directionalOverride = style->rtlOrdering() == VisualOrder;
47    TextRun run(characters, length, 0, 0, expansion, direction, directionalOverride);
48    if (textRunNeedsRenderingContext(font))
49        run.setRenderingContext(SVGTextRunRenderingContext::create(context));
50
51    return run;
52}
53
54template <typename CharacterType>
55static inline TextRun constructTextRunInternal(RenderObject* context, const Font& font, const CharacterType* characters, int length, RenderStyle* style, TextDirection direction, TextRun::ExpansionBehavior expansion, TextRunFlags flags)
56{
57    ASSERT(style);
58
59    TextDirection textDirection = direction;
60    bool directionalOverride = style->rtlOrdering() == VisualOrder;
61    if (flags != DefaultTextRunFlags) {
62        if (flags & RespectDirection)
63            textDirection = style->direction();
64        if (flags & RespectDirectionOverride)
65            directionalOverride |= isOverride(style->unicodeBidi());
66    }
67
68    TextRun run(characters, length, 0, 0, expansion, textDirection, directionalOverride);
69    if (textRunNeedsRenderingContext(font))
70        run.setRenderingContext(SVGTextRunRenderingContext::create(context));
71
72    return run;
73}
74
75TextRun constructTextRun(RenderObject* context, const Font& font, const LChar* characters, int length, RenderStyle* style, TextDirection direction, TextRun::ExpansionBehavior expansion)
76{
77    return constructTextRunInternal(context, font, characters, length, style, direction, expansion);
78}
79
80TextRun constructTextRun(RenderObject* context, const Font& font, const UChar* characters, int length, RenderStyle* style, TextDirection direction, TextRun::ExpansionBehavior expansion)
81{
82    return constructTextRunInternal(context, font, characters, length, style, direction, expansion);
83}
84
85TextRun constructTextRun(RenderObject* context, const Font& font, const RenderText* text, RenderStyle* style, TextDirection direction, TextRun::ExpansionBehavior expansion)
86{
87    if (text->is8Bit())
88        return constructTextRunInternal(context, font, text->characters8(), text->textLength(), style, direction, expansion);
89    return constructTextRunInternal(context, font, text->characters16(), text->textLength(), style, direction, expansion);
90}
91
92TextRun constructTextRun(RenderObject* context, const Font& font, const RenderText* text, unsigned offset, unsigned length, RenderStyle* style, TextDirection direction, TextRun::ExpansionBehavior expansion)
93{
94    ASSERT(offset + length <= text->textLength());
95    if (text->is8Bit())
96        return constructTextRunInternal(context, font, text->characters8() + offset, length, style, direction, expansion);
97    return constructTextRunInternal(context, font, text->characters16() + offset, length, style, direction, expansion);
98}
99
100TextRun constructTextRun(RenderObject* context, const Font& font, const String& string, RenderStyle* style, TextDirection direction, TextRun::ExpansionBehavior expansion, TextRunFlags flags)
101{
102    unsigned length = string.length();
103    if (!length)
104        return constructTextRunInternal(context, font, static_cast<const LChar*>(0), length, style, direction, expansion, flags);
105    if (string.is8Bit())
106        return constructTextRunInternal(context, font, string.characters8(), length, style, direction, expansion, flags);
107    return constructTextRunInternal(context, font, string.characters16(), length, style, direction, expansion, flags);
108}
109
110TextRun constructTextRun(RenderObject* context, const Font& font, const String& string, RenderStyle* style, TextRun::ExpansionBehavior expansion, TextRunFlags flags)
111{
112    bool hasStrongDirectionality;
113    return constructTextRun(context, font, string, style, determineDirectionality(string, hasStrongDirectionality), expansion, flags);
114}
115
116TextRun constructTextRun(RenderObject* context, const Font& font, const RenderText* text, unsigned offset, unsigned length, RenderStyle* style, TextRun::ExpansionBehavior expansion)
117{
118    ASSERT(offset + length <= text->textLength());
119    TextRun run = text->is8Bit()
120        ? constructTextRunInternal(context, font, text->characters8() + offset, length, style, LTR, expansion)
121        : constructTextRunInternal(context, font, text->characters16() + offset, length, style, LTR, expansion);
122    bool hasStrongDirectionality;
123    run.setDirection(directionForRun(run, hasStrongDirectionality));
124    return run;
125}
126
127} // namespace blink
128