File: aux.cc

package info (click to toggle)
gtkmathview 0.8.0-8
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 7,904 kB
  • sloc: cpp: 53,699; xml: 12,660; sh: 8,979; makefile: 1,722; ansic: 1,149; perl: 88
file content (282 lines) | stat: -rw-r--r-- 6,260 bytes parent folder | download | duplicates (6)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Copyright (C) 2000-2007, Luca Padovani <padovani@sti.uniurb.it>.
// 
// This file is part of GtkMathView, a flexible, high-quality rendering
// engine for MathML documents.
// 
// GtkMathView is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
// 
// GtkMathView 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
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include <config.h>
#include <cstdlib>
#include <assert.h>

#include <sstream>

#include <gdome.h>
#include <gdome-util.h>

#include "defs.h"
#include "gmetadom.hh"

static unsigned
getDepth(const DOM::Element& elem)
{
  unsigned length = 0;
  DOM::Element p = elem;

  while (p)
    {
      p = p.get_parentNode();
      length++;
    }

  return length;
}

static DOM::Element
findCommonAncestor(const DOM::Element& first, const DOM::Element& last)
{
  if (!first || !last) return DOM::Element(0);

  DOM::Element p(first);
  DOM::Element q(last);

  if (p != q)
    {
      unsigned pDepth = getDepth(p);
      unsigned qDepth  = getDepth(q);

      while (p && pDepth > qDepth)
	{
	  p = p.get_parentNode();
	  pDepth--;
	}

      while (q && qDepth > pDepth)
	{
	  q = q.get_parentNode();
	  qDepth--;
	}

      assert(pDepth == qDepth);

      while (p && q && p != q)
	{
	  p = p.get_parentNode();
	  q = q.get_parentNode();
	}
    }
  
  return p;
}

static void
findCommonSiblings(const DOM::Element& first, const DOM::Element& last,
		   DOM::Element& firstS, DOM::Element& lastS)
{
  DOM::Element p(first);
  DOM::Element q(last);

  if (p != q)
    {
      unsigned pDepth = getDepth(p);
      unsigned qDepth  = getDepth(q);

      while (p && pDepth > qDepth)
	{
	  p = p.get_parentNode();
	  pDepth--;
	}

      while (q && qDepth > pDepth)
	{
	  q = q.get_parentNode();
	  qDepth--;
	}

      assert(pDepth == qDepth);

      while (p && q && p.get_parentNode() != q.get_parentNode())
	{
	  p = p.get_parentNode();
	  q = q.get_parentNode();
	}
    }

  firstS = p;
  lastS = q;
}

static DOM::Node
leftmostChild(const DOM::Node& node)
{
  if (!node) return node;

  DOM::Node firstChild = node.get_firstChild();
  if (!firstChild) return node;

  return leftmostChild(firstChild);
}

static DOM::Node
rightmostChild(const DOM::Node& node)
{
  if (!node) return node;

  DOM::Node lastChild = node.get_lastChild();
  if (!lastChild) return node;

  return rightmostChild(lastChild);
}

static DOM::Node
leftSibling(const DOM::Node& node)
{
  DOM::Node p = node;

  if (!p) return p;

  while (p.get_parentNode() && p.get_parentNode().get_firstChild() == p)
    p = p.get_parentNode();

  if (!p.get_parentNode()) return DOM::Node(0);

  DOM::Node prevSibling = p.get_previousSibling();
  assert(prevSibling);

  return rightmostChild(prevSibling);
}

static DOM::Node
rightSibling(const DOM::Node& node)
{
  DOM::Node p = node;

  if (!p) return p;

  DOM::Node firstChild = p.get_firstChild();
  if (firstChild) return firstChild;

  while (p.get_parentNode() && p.get_parentNode().get_lastChild() == p)
    p = p.get_parentNode();

  if (!p.get_parentNode()) return DOM::Node(0);

  DOM::Node nextSibling = p.get_nextSibling();
  assert(nextSibling);

  return leftmostChild(nextSibling);
}

extern "C" GdomeElement*
find_common_ancestor(GdomeElement* first, GdomeElement* last)
{
  DOM::Element p(first);
  DOM::Element q(last);
  return gdome_cast_el(findCommonAncestor(p, q).gdome_object());
}

extern "C" GdomeElement*
find_self_or_ancestor(GdomeElement* elem, const char* uri, const char* name)
{
  DOM::Element el(elem);

  while (el && (el.get_namespaceURI() != uri || el.get_localName() != name))
    el = el.get_parentNode();

  return gdome_cast_el(el.gdome_object());
}

extern "C" GdomeElement*
find_action_element(GdomeElement* elem)
{
  for (DOM::Element el(elem); el; el = el.get_parentNode())
    if ((el.get_namespaceURI() == MATHML_NS_URI && el.get_localName() == "maction") ||
	(el.get_namespaceURI() == BOXML_NS_URI && el.get_localName() == "action"))
      return gdome_cast_el(el.gdome_object());

  return NULL;
}

extern "C" void
action_toggle(GdomeElement* elem)
{
  DOM::Element el(elem);

  guint idx;
  if (el.hasAttribute("selection"))
    idx = atoi(std::string(el.getAttribute("selection")).c_str());
  else idx = 1;

  idx++;

  std::ostringstream os;
  os << idx;
  el.setAttribute("selection", os.str());
}

extern "C" void
find_common_siblings(GdomeElement* first, GdomeElement* last,
		     GdomeElement** firstS, GdomeElement** lastS)
{
  DOM::Element fs(0);
  DOM::Element ls(0);

  findCommonSiblings(DOM::Element(first), DOM::Element(last), fs, ls);

  if (firstS != NULL) *firstS = gdome_cast_el(fs.gdome_object());
  if (lastS != NULL) *lastS = gdome_cast_el(ls.gdome_object());
}

static DOM::Element
findElementWithAttribute(const DOM::Element& elem, const std::string& name)
{
  DOM::Element el(elem);
  while (el && !el.hasAttribute(name)) el = el.get_parentNode();
  return el;
}

static DOM::Element
findElementWithAttributeNS(const DOM::Element& elem, const std::string& ns_uri, const std::string& name)
{
  DOM::Element el(elem);
  while (el && !el.hasAttributeNS(ns_uri, name)) el = el.get_parentNode();
  return el;
}

extern "C" GdomeElement*
find_xref_element(GdomeElement* elem)
{
  DOM::Element el = findElementWithAttribute(DOM::Element(elem), "xref");
  return gdome_cast_el(el.gdome_object());
}

extern "C" GdomeDOMString*
find_hyperlink(GdomeElement* elem, const char* ns_uri, const char* name)
{
  DOM::Element el = findElementWithAttributeNS(DOM::Element(elem), ns_uri, name);
  if (el) return el.getAttributeNS(ns_uri, name).gdome_str();
  else return NULL;
}

extern "C" void
delete_element(GdomeElement* elem)
{
  DOM::Element p(elem);

  DOM::Element parent = p.get_parentNode();
  assert(parent);

  parent.removeChild(p);
}