aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/apache/fop/fo/FOText.java
blob: ac18fb1c7fedd75bb1c9dd1d2a8879ea876357b7 (plain)
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
/*
 * $Id$
 * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
 * For details on use and redistribution please refer to the
 * LICENSE file included with these sources."
 */

package org.apache.fop.fo;

// FOP
import org.apache.fop.layout.Area;
import org.apache.fop.layout.BlockArea;
import org.apache.fop.layout.FontState;
import org.apache.fop.layout.*;
import org.apache.fop.datatypes.*;
import org.apache.fop.fo.properties.*;
import org.apache.fop.apps.FOPException;
import org.apache.fop.layoutmgr.LayoutManager;
import org.apache.fop.layoutmgr.TextLayoutManager;

import java.util.NoSuchElementException;

/**
 * a text node in the formatting object tree
 *
 * Modified by Mark Lillywhite, mark-fop@inomial.com.
 * Unfortunately the BufferManager implementatation holds
 * onto references to the character data in this object
 * longer than the lifetime of the object itself, causing
 * excessive memory consumption and OOM errors.
 */
public class FOText extends FObj {

    protected char[] ca;
    protected int start;
    protected int length;
    TextInfo textInfo;
    TextState ts;

    public FOText(char[] chars, int s, int e, TextInfo ti) {
        super(null);
        this.start = 0;
        this.ca = new char[e - s];
	System.arraycopy(chars, s, ca, 0, e-s);
        this.length = e - s;
        textInfo = ti;
    }

    public boolean willCreateArea() {
        if (textInfo.whiteSpaceCollapse == WhiteSpaceCollapse.FALSE
                && length > 0) {
            return true;
        }

        for (int i = start; i < start + length; i++) {
            char ch = ca[i];
            if (!((ch == ' ') || (ch == '\n') || (ch == '\r')
                    || (ch == '\t'))) {    // whitespace
                return true;
            }
        }
        return false;
    }

    // Just to keep PageNumber and PageNumber citation happy for now.
    // The real code is moved to TextLayoutManager!

    public static int addText(BlockArea ba, FontState fontState, float red,
                              float green, float blue, int wrapOption,
                              LinkSet ls, int whiteSpaceCollapse,
                              char data[], int start, int end,
                              TextState textState, int vAlign) {
	return 0;
    }

    public LayoutManager getLayoutManager() {
	// What if nothing left (length=0)?
	if (length < ca.length) {
	    char[] tmp = ca;
	    ca  = new char[length];
	    System.arraycopy(tmp, 0, ca, 0, length);
	}
	return new TextLayoutManager(this, ca, textInfo);
    }

    public CharIterator charIterator() {
	return new TextCharIterator();
    }

    private class TextCharIterator extends AbstractCharIterator {
	int curIndex = 0;
	public boolean hasNext() {
	    return (curIndex < length);
	}

	public char nextChar() {
	    if (curIndex < length) {
		// Just a char class? Don't actually care about the value!
		return ca[curIndex++];
	    }
	    else throw new NoSuchElementException();
	}

	public void remove() {
	    if (curIndex>0 && curIndex < length) {
		// copy from curIndex to end to curIndex-1
		System.arraycopy(ca, curIndex, ca, curIndex-1,
				 length-curIndex);
		length--;
		curIndex--;
	    }
	    else if (curIndex == length) {
		curIndex = --length;
	    }
	}


	public void replaceChar(char c) {
	    if (curIndex>0 && curIndex <= length) {
		ca[curIndex-1]=c;
	    }
	}


    }
}