1 /* 2 * Copyright (C) 2000 Lars Knoll (knoll (at) kde.org) 3 * Copyright (C) 2003, 2004, 2006, 2007, 2009, 2010 Apple Inc. All right reserved. 4 * 5 * This library is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Library General Public 7 * License as published by the Free Software Foundation; either 8 * version 2 of the License, or (at your option) any later version. 9 * 10 * This library is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * Library General Public License for more details. 14 * 15 * You should have received a copy of the GNU Library General Public License 16 * along with this library; see the file COPYING.LIB. If not, write to 17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 18 * Boston, MA 02110-1301, USA. 19 * 20 */ 21 22 #include "config.h" 23 #include "platform/text/BidiContext.h" 24 25 #include "wtf/StdLibExtras.h" 26 #include "wtf/Vector.h" 27 28 namespace WebCore { 29 30 using namespace WTF::Unicode; 31 32 struct SameSizeAsBidiContext : public RefCounted<SameSizeAsBidiContext> { 33 uint32_t bitfields : 16; 34 void* parent; 35 }; 36 37 COMPILE_ASSERT(sizeof(BidiContext) == sizeof(SameSizeAsBidiContext), BidiContext_should_stay_small); 38 39 inline PassRefPtr<BidiContext> BidiContext::createUncached(unsigned char level, Direction direction, bool override, BidiEmbeddingSource source, BidiContext* parent) 40 { 41 return adoptRef(new BidiContext(level, direction, override, source, parent)); 42 } 43 44 PassRefPtr<BidiContext> BidiContext::create(unsigned char level, Direction direction, bool override, BidiEmbeddingSource source, BidiContext* parent) 45 { 46 ASSERT(direction == (level % 2 ? RightToLeft : LeftToRight)); 47 48 if (parent) 49 return createUncached(level, direction, override, source, parent); 50 51 ASSERT(level <= 1); 52 if (!level) { 53 if (!override) { 54 DEFINE_STATIC_REF(BidiContext, ltrContext, (createUncached(0, LeftToRight, false, FromStyleOrDOM, 0))); 55 return ltrContext; 56 } 57 58 DEFINE_STATIC_REF(BidiContext, ltrOverrideContext, (createUncached(0, LeftToRight, true, FromStyleOrDOM, 0))); 59 return ltrOverrideContext; 60 } 61 62 if (!override) { 63 DEFINE_STATIC_REF(BidiContext, rtlContext, (createUncached(1, RightToLeft, false, FromStyleOrDOM, 0))); 64 return rtlContext; 65 } 66 67 DEFINE_STATIC_REF(BidiContext, rtlOverrideContext, (createUncached(1, RightToLeft, true, FromStyleOrDOM, 0))); 68 return rtlOverrideContext; 69 } 70 71 static inline PassRefPtr<BidiContext> copyContextAndRebaselineLevel(BidiContext* context, BidiContext* parent) 72 { 73 ASSERT(context); 74 unsigned char newLevel = parent ? parent->level() : 0; 75 if (context->dir() == RightToLeft) 76 newLevel = nextGreaterOddLevel(newLevel); 77 else if (parent) 78 newLevel = nextGreaterEvenLevel(newLevel); 79 80 return BidiContext::create(newLevel, context->dir(), context->override(), context->source(), parent); 81 } 82 83 // The BidiContext stack must be immutable -- they're re-used for re-layout after 84 // DOM modification/editing -- so we copy all the non-unicode contexts, and 85 // recalculate their levels. 86 PassRefPtr<BidiContext> BidiContext::copyStackRemovingUnicodeEmbeddingContexts() 87 { 88 Vector<BidiContext*, 64> contexts; 89 for (BidiContext* iter = this; iter; iter = iter->parent()) { 90 if (iter->source() != FromUnicode) 91 contexts.append(iter); 92 } 93 ASSERT(contexts.size()); 94 95 RefPtr<BidiContext> topContext = copyContextAndRebaselineLevel(contexts.last(), 0); 96 for (int i = contexts.size() - 1; i > 0; --i) 97 topContext = copyContextAndRebaselineLevel(contexts[i - 1], topContext.get()); 98 99 return topContext.release(); 100 } 101 102 bool operator==(const BidiContext& c1, const BidiContext& c2) 103 { 104 if (&c1 == &c2) 105 return true; 106 if (c1.level() != c2.level() || c1.override() != c2.override() || c1.dir() != c2.dir() || c1.source() != c2.source()) 107 return false; 108 if (!c1.parent()) 109 return !c2.parent(); 110 return c2.parent() && *c1.parent() == *c2.parent(); 111 } 112 113 } // namespace WebCore 114