/** * Process this text Token to optimize tokens and evaluate the token bounding box. * * @param g Graphics to get the font metrics. * @param text Text to add to the current textTokan. * @param textTok Current TextToken. * @param isText True if the previous textToken was a Text Token so we can merge it with this. */ private void updateText(Graphics g, String text, TextToken textTok, boolean isText) { // The text exists! if (text.length() > 0) { FontMetrics fm = g.getFontMetrics(); int a = fm.getAscent(), d = fm.getDescent(), w = fm.stringWidth(text), h = fm.getHeight(); // The previous token was a text too so we must merge it with this new one. if (isText) { textTok = (TextToken) m_tokens.lastElement(); textTok.m_text += text; textTok.m_bounds.width += w; if (textTok.m_bounds.height < h) textTok.m_bounds.height = h; } // The previous token was a formating one. else { m_tokens.addElement(textTok); textTok.m_text = text; textTok.m_bounds = new Rectangle(0, 0, w, h); } if (a > m_body.m_aMax) m_body.m_aMax = a; if (d > m_body.m_dMax) m_body.m_dMax = d; m_wCur += w; } }
/** * Creates a new TextToken by parsing a pseudo-HTML tag. * * @param g Graphics used to retrieve the font metric. * @param tag A pseudo HTML tag without '<' and '>'. * @return a new TextToken initialized according to the tag. */ private TextToken updateTag(Graphics g, String tag) { String tempTag; TextToken textTok = null; char begChar; if (tag.length() > 0) { tag = tag.toLowerCase(); begChar = tag.charAt(0); // End of Tag, we returns except for the case </p> if (begChar == '/') { char nxtChar = tag.charAt(1); tempTag = (String) m_heap.lastElement(); if (tempTag.charAt(0) == nxtChar) // ! very simple verification ! { textTok = closeTag(g, tempTag); if (nxtChar != 'p') return textTok; } else { System.out.println("[updateTag] no corresponding opened Tag : " + tag); return null; } } FormatToken prevTok = m_curTok; Insets prevMrg = prevTok.m_margin, margin = m_body.m_margin; int flags = m_body.m_flags, width = m_wCur + (prevMrg != null ? prevMrg.left + prevMrg.right : 0); // Start of Tag + </p> if (tag.equals("br") || begChar == 'p' || tag.equals("/p")) { if (tag.equals("br")) { if (prevMrg != null) margin = prevMrg; flags = prevTok.m_flags; } // We specify new margins if (begChar == 'p') { String alignStr = readAtt(tag, "a"); if (alignStr != null) { char align = Character.toLowerCase(alignStr.charAt(0)); flags = align == 'r' ? RIGHT_BIT : (align == 'c' ? CENTER_BIT : 0); } margin = readMargin(tag); if (tag.length() > 1 && alignStr == null && margin == null) { flags = m_body.m_flags; System.out.println("[updateTag] syntax error Tag : " + tag); return null; } else { m_heap.addElement(tag); } } // update pr�vious format Token prevTok.m_aMax = m_body.m_aMax; prevTok.m_dMax = m_body.m_dMax; prevTok.m_width = width; // reset current vars m_body.m_aMax = 0; m_body.m_dMax = 0; m_wCur = 0; // Stores the max width of all lines including its margins if (width > m_body.m_width) m_body.m_width = width; m_curTok = new FormatToken(); m_curTok.m_flags = flags; m_curTok.m_margin = margin == null ? m_body.m_margin : margin; m_tokens.addElement(m_curTok); textTok = new TextToken(); textTok.m_color = new Color(m_color); textTok.m_font = new Font(m_name, m_style, m_size); } else if (isGfx(begChar)) { textTok = updateGfx(g, tag); m_heap.addElement(tag); } else { System.out.println("[updateTag] Unknown Tag : " + tag); textTok = null; } } return textTok; }