| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package net.sourceforge.jeuclid.elements.presentation.token; |
| 20 | |
|
| 21 | |
import java.awt.Color; |
| 22 | |
import java.awt.Graphics2D; |
| 23 | |
import java.awt.font.TextLayout; |
| 24 | |
import java.text.AttributedCharacterIterator; |
| 25 | |
import java.text.AttributedString; |
| 26 | |
import java.util.Collections; |
| 27 | |
import java.util.List; |
| 28 | |
|
| 29 | |
import net.sourceforge.jeuclid.LayoutContext; |
| 30 | |
import net.sourceforge.jeuclid.context.Parameter; |
| 31 | |
import net.sourceforge.jeuclid.elements.AbstractJEuclidElement; |
| 32 | |
import net.sourceforge.jeuclid.elements.support.text.StringUtil; |
| 33 | |
import net.sourceforge.jeuclid.layout.LayoutInfo; |
| 34 | |
import net.sourceforge.jeuclid.layout.LayoutStage; |
| 35 | |
import net.sourceforge.jeuclid.layout.LayoutView; |
| 36 | |
import net.sourceforge.jeuclid.layout.LayoutableNode; |
| 37 | |
import net.sourceforge.jeuclid.layout.TextObject; |
| 38 | |
|
| 39 | |
import org.apache.batik.dom.AbstractDocument; |
| 40 | |
import org.w3c.dom.mathml.MathMLPresentationToken; |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
public abstract class AbstractTokenWithTextLayout extends |
| 48 | |
AbstractJEuclidElement implements MathMLPresentationToken { |
| 49 | |
|
| 50 | |
private static final long serialVersionUID = 1L; |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
public AbstractTokenWithTextLayout(final String qname, |
| 61 | |
final AbstractDocument odoc) { |
| 62 | 73319 | super(qname, odoc); |
| 63 | 73319 | } |
| 64 | |
|
| 65 | |
|
| 66 | |
@Override |
| 67 | |
public void layoutStageInvariant(final LayoutView view, |
| 68 | |
final LayoutInfo info, final LayoutStage stage, |
| 69 | |
final LayoutContext context) { |
| 70 | 25506 | final Graphics2D g = view.getGraphics(); |
| 71 | 25506 | final TextLayout t = this.produceTextLayout(g, context); |
| 72 | 25506 | if (t != null) { |
| 73 | 25506 | final StringUtil.TextLayoutInfo tli = StringUtil.getTextLayoutInfo( |
| 74 | |
t, false); |
| 75 | 25506 | info.setAscentHeight(tli.getAscent(), stage); |
| 76 | 25506 | info.setDescentHeight(tli.getDescent(), stage); |
| 77 | 25506 | final float width = tli.getWidth(); |
| 78 | 25506 | info.setHorizontalCenterOffset(width / 2.0f, stage); |
| 79 | 25506 | info.setWidth(width, stage); |
| 80 | 25506 | info.setGraphicsObject(new TextObject(t, tli.getOffset(), |
| 81 | |
(Color) this.applyLocalAttributesToContext(context) |
| 82 | |
.getParameter(Parameter.MATHCOLOR))); |
| 83 | |
} |
| 84 | 25506 | } |
| 85 | |
|
| 86 | |
private TextLayout produceTextLayout(final Graphics2D g2d, |
| 87 | |
final LayoutContext context) { |
| 88 | |
TextLayout layout; |
| 89 | 25506 | final LayoutContext now = this.applyLocalAttributesToContext(context); |
| 90 | |
|
| 91 | 25506 | final AttributedCharacterIterator aci = StringUtil |
| 92 | |
.textContentAsAttributedCharacterIterator(now, this, this, 1.0f); |
| 93 | |
|
| 94 | 25506 | if (aci.getBeginIndex() == aci.getEndIndex()) { |
| 95 | 0 | return null; |
| 96 | |
} else { |
| 97 | 25506 | layout = StringUtil.createTextLayoutFromAttributedString(g2d, |
| 98 | |
new AttributedString(aci), now); |
| 99 | 25506 | return layout; |
| 100 | |
} |
| 101 | |
} |
| 102 | |
|
| 103 | |
|
| 104 | |
@Override |
| 105 | |
public List<LayoutableNode> getChildrenToLayout() { |
| 106 | 25506 | return Collections.emptyList(); |
| 107 | |
} |
| 108 | |
|
| 109 | |
|
| 110 | |
@Override |
| 111 | |
public List<LayoutableNode> getChildrenToDraw() { |
| 112 | 23826 | return Collections.emptyList(); |
| 113 | |
} |
| 114 | |
|
| 115 | |
} |