gfx/graphite2/src/inc/Bidi.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /*  GRAPHITE2 LICENSING
     3     Copyright 2013, SIL International
     4     All rights reserved.
     6     This library is free software; you can redistribute it and/or modify
     7     it under the terms of the GNU Lesser General Public License as published
     8     by the Free Software Foundation; either version 2.1 of License, or
     9     (at your option) any later version.
    11     This program is distributed in the hope that it will be useful,
    12     but WITHOUT ANY WARRANTY; without even the implied warranty of
    13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    14     Lesser General Public License for more details.
    16     You should also have received a copy of the GNU Lesser General Public
    17     License along with this library in the file named "LICENSE".
    18     If not, write to the Free Software Foundation, 51 Franklin Street,
    19     Suite 500, Boston, MA 02110-1335, USA or visit their web page on the
    20     internet at http://www.fsf.org/licenses/lgpl.html.
    22 Alternatively, the contents of this file may be used under the terms of the
    23 Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public
    24 License, as published by the Free Software Foundation, either version 2
    25 of the License or (at your option) any later version.
    26 */
    27 #pragma once
    29 namespace graphite2
    30 {
    32 class BracketPair
    33 {
    34 public:
    35     BracketPair(uint16 g, Slot *s, uint8 b, BracketPair *p, BracketPair *l) : _open(s), _close(0), _parent(p), _next(0), _prev(l), _gid(g), _mask(0), _before(b) {};
    36     uint16 gid() const { return _gid; }
    37     Slot *open() const { return _open; }
    38     Slot *close() const { return _close; }
    39     uint8 mask() const { return _mask; }
    40     int8 before() const { return _before; }
    41     BracketPair *parent() const { return _parent; }
    42     void close(Slot *s) { _close = s; }
    43     BracketPair *next() const { return _next; }
    44     BracketPair *prev() const { return _prev; }
    45     void next(BracketPair *n) { _next = n; }
    46     void orin(uint8 m) { _mask |= m; }
    47 private:
    48     Slot        *   _open;          // Slot of opening paren
    49     Slot        *   _close;         // Slot of closing paren
    50     BracketPair *   _parent;        // pair this pair is in or NULL
    51     BracketPair *   _next;          // next pair along the string
    52     BracketPair *   _prev;          // pair that closed last before we opened
    53     uint16          _gid;           // gid of closing paren
    54     uint8           _mask;          // bitmap (2 bits) of directions within the pair
    55     int8            _before;        // most recent strong type (L, R, OPP, CPP)
    56 };
    58 class BracketPairStack
    59 {
    60 public:
    61     BracketPairStack(int s) : _stack(grzeroalloc<BracketPair>(s)), _ip(_stack - 1), _top(0), _last(0), _lastclose(0), _size(s) {}
    62     ~BracketPairStack() { free(_stack); }
    64 public:
    65     BracketPair *scan(uint16 gid);
    66     void close(BracketPair *tos, Slot *s);
    67     BracketPair *push(uint16 gid, Slot *pos, uint8 before, int prevopen);
    68     void orin(uint8 mask);
    69     void clear() { _ip = _stack - 1; _top = 0; _last = 0; _lastclose = 0; }
    70     int size() const { return _size; }
    71     BracketPair *start() const { return _stack; }
    73     CLASS_NEW_DELETE
    75 private:
    77     BracketPair *_stack;        // start of storage
    78     BracketPair *_ip;           // where to add the next pair
    79     BracketPair *_top;          // current parent
    80     BracketPair *_last;         // end of next() chain
    81     BracketPair *_lastclose;    // last pair to close
    82     int          _size;         // capacity
    83 };
    85 inline BracketPair *BracketPairStack::scan(uint16 gid)
    86 {
    87     BracketPair *res = _top;
    88     while (res >= _stack)
    89     {
    90         if (res->gid() == gid)
    91             return res;
    92         res = res->parent();
    93     }
    94     return 0;
    95 }
    97 inline void BracketPairStack::close(BracketPair *tos, Slot *s) 
    98 {
    99     for ( ; _last && _last != tos && !_last->close(); _last = _last->parent())
   100     { }
   101     tos->close(s);
   102     _last->next(NULL);
   103     _lastclose = tos;
   104     _top = tos->parent();
   105 }
   107 inline BracketPair *BracketPairStack::push(uint16 gid, Slot *pos, uint8 before, int prevopen)
   108 {
   109     if (++_ip - _stack < _size && _stack)
   110     {
   111         ::new (_ip) BracketPair(gid, pos, before, _top, prevopen ? _last : _lastclose);
   112         if (_last) _last->next(_ip);
   113         _last = _ip;
   114     }
   115     _top = _ip;
   116     return _ip;
   117 }
   119 inline void BracketPairStack::orin(uint8 mask)
   120 {
   121     BracketPair *t = _top;
   122     for ( ; t; t = t->parent())
   123         t->orin(mask);
   124 }
   126 }

mercurial