01 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
02 *
03 * Copyright 2000-2011, Michael Meyling <mime@qedeq.org>.
04 *
05 * "Hilbert II" is free software; you can redistribute
06 * it and/or modify it under the terms of the GNU General Public
07 * License as published by the Free Software Foundation; either
08 * version 2 of the License, or (at your option) any later version.
09 *
10 * This program 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
13 * GNU General Public License for more details.
14 */
15
16 package org.qedeq.kernel.bo.service;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.qedeq.base.utility.StringUtility;
22 import org.qedeq.kernel.bo.module.Element2Utf8;
23 import org.qedeq.kernel.bo.service.unicode.Latex2UnicodeParser;
24 import org.qedeq.kernel.se.base.list.Element;
25
26
27 /**
28 * Transfer a QEDEQ formulas into UTF-8 text.
29 *
30 * @author Michael Meyling
31 */
32 public final class Element2Utf8Impl implements Element2Utf8 {
33
34 /** We use this converter. */
35 private Element2LatexImpl converter;
36
37 /**
38 * Constructor.
39 *
40 * @param converter This converter can produce at least LaTeX.
41 */
42 public Element2Utf8Impl(final Element2LatexImpl converter) {
43 this.converter = converter;
44 }
45
46 public String getUtf8(final Element element) {
47 return getUtf8(element, 0)[0];
48 }
49
50 public String[] getUtf8(final Element element, final int maxCols) {
51 final String result = Latex2UnicodeParser.transform(null, converter.getLatex(element), 0);
52 if (maxCols <= 0 || result.length() < maxCols) {
53 return new String[] {result};
54 }
55 final List list = new ArrayList();
56 int index = 0;
57 while (index < result.length()) {
58 list.add(StringUtility.substring(result, index, maxCols));
59 index += maxCols;
60 }
61 return (String[]) list.toArray(new String[] {});
62 }
63
64 }
|