Coverage Report - net.sourceforge.jeuclid.elements.presentation.token.AbstractTokenWithTextLayout
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractTokenWithTextLayout
95%
20/21
50%
2/4
1,6
 
 1  
 /*
 2  
  * Copyright 2007 - 2009 JEuclid, http://jeuclid.sf.net
 3  
  * 
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 
 17  
 /* $Id: AbstractTokenWithTextLayout.java,v 923658a2b249 2009/09/11 09:57:31 max $ */
 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  
  * Common functionality for all tokens based on a text layout.
 44  
  * 
 45  
  * @version $Revision: 923658a2b249 $
 46  
  */
 47  
 public abstract class AbstractTokenWithTextLayout extends
 48  
         AbstractJEuclidElement implements MathMLPresentationToken {
 49  
 
 50  
     private static final long serialVersionUID = 1L;
 51  
 
 52  
     /**
 53  
      * Default constructor. Sets MathML Namespace.
 54  
      * 
 55  
      * @param qname
 56  
      *            Qualified name.
 57  
      * @param odoc
 58  
      *            Owner Document.
 59  
      */
 60  
     public AbstractTokenWithTextLayout(final String qname,
 61  
             final AbstractDocument odoc) {
 62  73319
         super(qname, odoc);
 63  73319
     }
 64  
 
 65  
     /** {@inheritDoc} */
 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  
     /** {@inheritDoc} */
 104  
     @Override
 105  
     public List<LayoutableNode> getChildrenToLayout() {
 106  25506
         return Collections.emptyList();
 107  
     }
 108  
 
 109  
     /** {@inheritDoc} */
 110  
     @Override
 111  
     public List<LayoutableNode> getChildrenToDraw() {
 112  23826
         return Collections.emptyList();
 113  
     }
 114  
 
 115  
 }