/**
   * Updates the interal bitset from <code>iterator</code>. This will set <code>validMask</code> to
   * true if <code>iterator</code> is non-null.
   */
  private void updateMask(AttributedCharacterIterator iterator) {
    if (iterator != null) {
      validMask = true;
      this.iterator = iterator;

      // Update the literal mask
      if (literalMask == null) {
        literalMask = new BitSet();
      } else {
        for (int counter = literalMask.length() - 1; counter >= 0; counter--) {
          literalMask.clear(counter);
        }
      }

      iterator.first();
      while (iterator.current() != CharacterIterator.DONE) {
        Map attributes = iterator.getAttributes();
        boolean set = isLiteral(attributes);
        int start = iterator.getIndex();
        int end = iterator.getRunLimit();

        while (start < end) {
          if (set) {
            literalMask.set(start);
          } else {
            literalMask.clear(start);
          }
          start++;
        }
        iterator.setIndex(start);
      }
    }
  }
Example #2
0
  /** @see Graphics2D#drawString(AttributedCharacterIterator, float, float) */
  public void drawString(AttributedCharacterIterator iter, float x, float y) {
    /*
            StringBuffer sb = new StringBuffer();
            for(char c = iter.first(); c != AttributedCharacterIterator.DONE; c = iter.next()) {
                sb.append(c);
            }
            drawString(sb.toString(),x,y);
    */
    StringBuffer stringbuffer = new StringBuffer(iter.getEndIndex());
    for (char c = iter.first(); c != '\uFFFF'; c = iter.next()) {
      if (iter.getIndex() == iter.getRunStart()) {
        if (stringbuffer.length() > 0) {
          drawString(stringbuffer.toString(), x, y);
          FontMetrics fontmetrics = getFontMetrics();
          x = (float) (x + fontmetrics.getStringBounds(stringbuffer.toString(), this).getWidth());
          stringbuffer.delete(0, stringbuffer.length());
        }
        doAttributes(iter);
      }
      stringbuffer.append(c);
    }

    drawString(stringbuffer.toString(), x, y);
    underline = false;
  }
Example #3
0
 private List<Integer> getNewlineLocations(AttributedCharacterIterator styledTextIterator) {
   List<Integer> newlineLocations = new ArrayList<Integer>();
   for (char c = styledTextIterator.first();
       c != AttributedCharacterIterator.DONE;
       c = styledTextIterator.next()) {
     if (c == '\n') newlineLocations.add(styledTextIterator.getIndex());
   }
   return newlineLocations;
 }
  /**
   * Returns the start of the first run that contains the attribute <code>id</code>. This will
   * return <code>-1</code> if the attribute can not be found.
   */
  int getAttributeStart(AttributedCharacterIterator.Attribute id) {
    if (isValidMask()) {
      AttributedCharacterIterator iterator = getIterator();

      iterator.first();
      while (iterator.current() != CharacterIterator.DONE) {
        if (iterator.getAttribute(id) != null) {
          return iterator.getIndex();
        }
        iterator.next();
      }
    }
    return -1;
  }
Example #5
0
  protected String style(final AttributedCharacterIterator aci) {
    final StringBuilder builder = new StringBuilder();

    Map<AttributedCharacterIterator.Attribute, Object> map = null;
    char chr = aci.current();

    while (aci.getIndex() < aci.getEndIndex()) {
      if (!aci.getAttributes().equals(map)) {
        style(aci.getAttributes(), builder);
        map = aci.getAttributes();
      }

      builder.append(StringEscapeUtils.escapeHtml(String.valueOf(chr)));
      chr = aci.next();
    }

    return builder.toString();
  }
  protected void exportStyledText(JRStyledText styledText, Locale locale, boolean startedHyperlink)
      throws IOException {
    String text = styledText.getText();

    int runLimit = 0;

    AttributedCharacterIterator iterator = styledText.getAttributedString().getIterator();

    while (runLimit < styledText.length()
        && (runLimit = iterator.getRunLimit()) <= styledText.length()) {
      exportStyledTextRun(
          iterator.getAttributes(),
          text.substring(iterator.getIndex(), runLimit),
          locale,
          startedHyperlink);

      iterator.setIndex(runLimit);
    }
  }
  /**
   * finds attributes with regards to char index in this AttributedCharacterIterator, and puts them
   * in a vector
   *
   * @param iterator
   * @return a vector, each entry in this vector are of type FieldContainer , which stores start and
   *     end indexes and an attribute this range has
   */
  private static List<FieldContainer> findFields(AttributedCharacterIterator iterator) {
    List<FieldContainer> result = new ArrayList<FieldContainer>();
    while (iterator.getIndex() != iterator.getEndIndex()) {
      int start = iterator.getRunStart();
      int end = iterator.getRunLimit();

      Iterator it = iterator.getAttributes().keySet().iterator();
      while (it.hasNext()) {
        AttributedCharacterIterator.Attribute attribute =
            (AttributedCharacterIterator.Attribute) it.next();
        Object value = iterator.getAttribute(attribute);
        result.add(new FieldContainer(start, end, attribute, value));
        // System.out.println(start + " " + end + ": " + attribute + ",
        // " + value );
        // System.out.println("v.add(new FieldContainer(" + start +"," +
        // end +"," + attribute+ "," + value+ "));");
      }
      iterator.setIndex(end);
    }
    return result;
  }
 private TextLayout createAndCacheTextLayout(
     int fragmentIndex, Font basefont, FontRenderContext fontRenderContext) {
   String text = myFragments.get(fragmentIndex);
   AttributedString string = new AttributedString(text);
   int start = 0;
   int end = text.length();
   AttributedCharacterIterator it =
       string.getIterator(new AttributedCharacterIterator.Attribute[0], start, end);
   Font currentFont = basefont;
   int currentIndex = start;
   for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) {
     Font font = basefont;
     if (!font.canDisplay(c)) {
       for (SuitableFontProvider provider : SuitableFontProvider.EP_NAME.getExtensions()) {
         font =
             provider.getFontAbleToDisplay(
                 c, basefont.getSize(), basefont.getStyle(), basefont.getFamily());
         if (font != null) break;
       }
     }
     int i = it.getIndex();
     if (!Comparing.equal(currentFont, font)) {
       if (i > currentIndex) {
         string.addAttribute(TextAttribute.FONT, currentFont, currentIndex, i);
       }
       currentFont = font;
       currentIndex = i;
     }
   }
   if (currentIndex < end) {
     string.addAttribute(TextAttribute.FONT, currentFont, currentIndex, end);
   }
   TextLayout layout = new TextLayout(string.getIterator(), fontRenderContext);
   if (fragmentIndex >= myLayouts.size()) {
     myLayouts.addAll(Collections.nCopies(fragmentIndex - myLayouts.size() + 1, null));
   }
   myLayouts.set(fragmentIndex, layout);
   myLayoutFont = getBaseFont();
   return layout;
 }
  /**
   * Returns the number of occurences of <code>f</code> before the location <code>start</code> in
   * the current <code>AttributedCharacterIterator</code>.
   */
  private int getFieldTypeCountTo(Object f, int start) {
    AttributedCharacterIterator iterator = getIterator();
    int count = 0;

    if (iterator != null && (f instanceof AttributedCharacterIterator.Attribute)) {
      AttributedCharacterIterator.Attribute field = (AttributedCharacterIterator.Attribute) f;
      int index = 0;

      iterator.first();
      while (iterator.getIndex() < start) {
        while (iterator.getAttribute(field) == null && iterator.next() != CharacterIterator.DONE) ;
        if (iterator.current() != CharacterIterator.DONE) {
          iterator.setIndex(iterator.getRunLimit(field));
          iterator.next();
          count++;
        } else {
          break;
        }
      }
    }
    return count;
  }
  /** Selects the fields identified by <code>attributes</code>. */
  void selectField(Object f, int count) {
    AttributedCharacterIterator iterator = getIterator();

    if (iterator != null && (f instanceof AttributedCharacterIterator.Attribute)) {
      AttributedCharacterIterator.Attribute field = (AttributedCharacterIterator.Attribute) f;

      iterator.first();
      while (iterator.current() != CharacterIterator.DONE) {
        while (iterator.getAttribute(field) == null && iterator.next() != CharacterIterator.DONE) ;
        if (iterator.current() != CharacterIterator.DONE) {
          int limit = iterator.getRunLimit(field);

          if (--count <= 0) {
            getFormattedTextField().select(iterator.getIndex(), limit);
            break;
          }
          iterator.setIndex(limit);
          iterator.next();
        }
      }
    }
  }
Example #11
0
  /** Initialize state, including fChars array, direction, and fBidi. */
  private void initAll(AttributedCharacterIterator text) {

    fStart = text.getBeginIndex();

    // extract chars
    fChars = new char[text.getEndIndex() - fStart];

    int n = 0;
    for (char c = text.first(); c != CharacterIterator.DONE; c = text.next()) {
      fChars[n++] = c;
    }

    text.first();

    fBidi = new Bidi(text);
    if (fBidi.isLeftToRight()) {
      fBidi = null;
    }

    text.first();
    Map<? extends Attribute, ?> paragraphAttrs = text.getAttributes();
    NumericShaper shaper = AttributeValues.getNumericShaping(paragraphAttrs);
    if (shaper != null) {
      shaper.shape(fChars, 0, fChars.length);
    }

    fParagraph = new StyledParagraph(text, fChars);

    // set paragraph attributes
    {
      // If there's an embedded graphic at the start of the
      // paragraph, look for the first non-graphic character
      // and use it and its font to initialize the paragraph.
      // If not, use the first graphic to initialize.
      fJustifyRatio = AttributeValues.getJustification(paragraphAttrs);

      boolean haveFont = TextLine.advanceToFirstFont(text);

      if (haveFont) {
        Font defaultFont = TextLine.getFontAtCurrentPos(text);
        int charsStart = text.getIndex() - text.getBeginIndex();
        LineMetrics lm = defaultFont.getLineMetrics(fChars, charsStart, charsStart + 1, fFrc);
        fBaseline = (byte) lm.getBaselineIndex();
        fBaselineOffsets = lm.getBaselineOffsets();
      } else {
        // hmmm what to do here?  Just try to supply reasonable
        // values I guess.

        GraphicAttribute graphic =
            (GraphicAttribute) paragraphAttrs.get(TextAttribute.CHAR_REPLACEMENT);
        fBaseline = TextLayout.getBaselineFromGraphic(graphic);
        Hashtable<Attribute, ?> fmap = new Hashtable<>(5, (float) 0.9);
        Font dummyFont = new Font(fmap);
        LineMetrics lm = dummyFont.getLineMetrics(" ", 0, 1, fFrc);
        fBaselineOffsets = lm.getBaselineOffsets();
      }
      fBaselineOffsets = TextLine.getNormalizedOffsets(fBaselineOffsets, fBaseline);
    }

    invalidateComponents();
  }
Example #12
0
  /** Shows the current selected <tt>TextNode</tt>. */
  protected void showSelectedGraphicsNode() {
    GraphicsNode gn = walker.getCurrentGraphicsNode();
    if (!(gn instanceof TextNode)) {
      return;
    }
    TextNode textNode = (TextNode) gn;
    // mark the selection of the substring found
    String text = textNode.getText();
    String pattern = search.getText();
    if (!caseSensitive.isSelected()) {
      text = text.toLowerCase();
      pattern = pattern.toLowerCase();
    }
    int end = text.indexOf(pattern, currentIndex);

    AttributedCharacterIterator aci = textNode.getAttributedCharacterIterator();
    aci.first();
    for (int i = 0; i < end; ++i) {
      aci.next();
    }
    Mark startMark = textNode.getMarkerForChar(aci.getIndex(), true);

    for (int i = 0; i < pattern.length() - 1; ++i) {
      aci.next();
    }
    Mark endMark = textNode.getMarkerForChar(aci.getIndex(), false);
    svgCanvas.select(startMark, endMark);

    // zoom on the TextNode if needed
    if (highlightButton.isSelected()) {
      return;
    }

    // get the highlight shape in GVT root (global) coordinate sytem
    Shape s = textNode.getHighlightShape();
    AffineTransform at;
    if (highlightCenterZoomButton.isSelected()) {
      at = svgCanvas.getInitialTransform();
    } else {
      at = svgCanvas.getRenderingTransform();
    }
    // get the bounds of the highlight shape in the canvas coordinate system
    Rectangle2D gnb = at.createTransformedShape(s).getBounds();

    Dimension canvasSize = svgCanvas.getSize();
    // translate the highlight region to (0, 0) in the canvas coordinate
    // system
    AffineTransform Tx =
        AffineTransform.getTranslateInstance(
            -gnb.getX() - gnb.getWidth() / 2, -gnb.getY() - gnb.getHeight() / 2);

    if (highlightCenterZoomButton.isSelected()) {
      // zoom on the highlight shape such as the shape takes x% of the
      // canvas size
      double sx = canvasSize.width / gnb.getWidth();
      double sy = canvasSize.height / gnb.getHeight();
      double scale = Math.min(sx, sy) / 8;
      if (scale > 1) {
        Tx.preConcatenate(AffineTransform.getScaleInstance(scale, scale));
      }
    }
    Tx.preConcatenate(
        AffineTransform.getTranslateInstance(canvasSize.width / 2, canvasSize.height / 2));
    // take into account the initial transform
    AffineTransform newRT = new AffineTransform(at);
    newRT.preConcatenate(Tx);
    // change the rendering transform
    svgCanvas.setRenderingTransform(newRT);
  }