001/* 002 * $Id: TextRenderInfo.java 4784 2011-03-15 08:33:00Z blowagie $ 003 * 004 * This file is part of the iText (R) project. 005 * Copyright (c) 1998-2011 1T3XT BVBA 006 * Authors: Kevin Day, Bruno Lowagie, Paulo Soares, et al. 007 * 008 * This program is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU Affero General Public License version 3 010 * as published by the Free Software Foundation with the addition of the 011 * following permission added to Section 15 as permitted in Section 7(a): 012 * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT, 013 * 1T3XT DISCLAIMS THE WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS. 014 * 015 * This program is distributed in the hope that it will be useful, but 016 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 017 * or FITNESS FOR A PARTICULAR PURPOSE. 018 * See the GNU Affero General Public License for more details. 019 * You should have received a copy of the GNU Affero General Public License 020 * along with this program; if not, see http://www.gnu.org/licenses or write to 021 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 022 * Boston, MA, 02110-1301 USA, or download the license from the following URL: 023 * http://itextpdf.com/terms-of-use/ 024 * 025 * The interactive user interfaces in modified source and object code versions 026 * of this program must display Appropriate Legal Notices, as required under 027 * Section 5 of the GNU Affero General Public License. 028 * 029 * In accordance with Section 7(b) of the GNU Affero General Public License, 030 * a covered work must retain the producer line in every PDF that is created 031 * or manipulated using iText. 032 * 033 * You can be released from the requirements of the license by purchasing 034 * a commercial license. Buying such a license is mandatory as soon as you 035 * develop commercial activities involving the iText software without 036 * disclosing the source code of your own applications. 037 * These activities include: offering paid services to customers as an ASP, 038 * serving PDFs on the fly in a web application, shipping iText with a closed 039 * source product. 040 * 041 * For more information, please contact iText Software Corp. at this 042 * address: sales@itextpdf.com 043 */ 044package com.itextpdf.text.pdf.parser; 045 046import java.util.ArrayList; 047import java.util.Collection; 048 049import com.itextpdf.text.pdf.BaseFont; 050import com.itextpdf.text.pdf.DocumentFont; 051 052/** 053 * Provides information and calculations needed by render listeners 054 * to display/evaluate text render operations. 055 * <br><br> 056 * This is passed between the {@link PdfContentStreamProcessor} and 057 * {@link RenderListener} objects as text rendering operations are 058 * discovered 059 */ 060public class TextRenderInfo { 061 062 private final String text; 063 private final Matrix textToUserSpaceTransformMatrix; 064 private final GraphicsState gs; 065 /** 066 * Array containing marked content info for the text. 067 * @since 5.0.2 068 */ 069 private final Collection<MarkedContentInfo> markedContentInfos; 070 071 /** 072 * Creates a new TextRenderInfo object 073 * @param text the text that should be displayed 074 * @param gs the graphics state (note: at this time, this is not immutable, so don't cache it) 075 * @param textMatrix the text matrix at the time of the render operation 076 * @param markedContentInfo the marked content sequence, if available 077 */ 078 TextRenderInfo(String text, GraphicsState gs, Matrix textMatrix, Collection<MarkedContentInfo> markedContentInfo) { 079 this.text = text; 080 this.textToUserSpaceTransformMatrix = textMatrix.multiply(gs.ctm); 081 this.gs = gs; 082 this.markedContentInfos = new ArrayList<MarkedContentInfo>(markedContentInfo); 083 } 084 085 /** 086 * @return the text to render 087 */ 088 public String getText(){ 089 return text; 090 } 091 092 /** 093 * Checks if the text belongs to a marked content sequence 094 * with a given mcid. 095 * @param mcid a marked content id 096 * @return true if the text is marked with this id 097 * @since 5.0.2 098 */ 099 public boolean hasMcid(int mcid) { 100 for (MarkedContentInfo info : markedContentInfos) { 101 if (info.hasMcid()) 102 if(info.getMcid() == mcid) 103 return true; 104 } 105 106 return false; 107 } 108 109 /** 110 * @return the unscaled (i.e. in Text space) width of the text 111 */ 112 float getUnscaledWidth(){ 113 return getStringWidth(text); 114 } 115 116 /** 117 * Gets the baseline for the text (i.e. the line that the text 'sits' on) 118 * @return the baseline line segment 119 * @since 5.0.2 120 */ 121 public LineSegment getBaseline(){ 122 return getUnscaledBaselineWithOffset(0).transformBy(textToUserSpaceTransformMatrix); 123 } 124 125 /** 126 * Gets the ascentline for the text (i.e. the line that represents the topmost extent that a string of the current font could have) 127 * @return the ascentline line segment 128 * @since 5.0.2 129 */ 130 public LineSegment getAscentLine(){ 131 float ascent = gs.getFont().getFontDescriptor(BaseFont.ASCENT, gs.getFontSize()); 132 return getUnscaledBaselineWithOffset(ascent).transformBy(textToUserSpaceTransformMatrix); 133 } 134 135 /** 136 * Gets the descentline for the text (i.e. the line that represents the bottom most extent that a string of the current font could have) 137 * @return the descentline line segment 138 * @since 5.0.2 139 */ 140 public LineSegment getDescentLine(){ 141 // per getFontDescription() API, descent is returned as a negative number, so we apply that as a normal vertical offset 142 float descent = gs.getFont().getFontDescriptor(BaseFont.DESCENT, gs.getFontSize()); 143 return getUnscaledBaselineWithOffset(descent).transformBy(textToUserSpaceTransformMatrix); 144 } 145 146 private LineSegment getUnscaledBaselineWithOffset(float yOffset){ 147 return new LineSegment(new Vector(0, yOffset, 1), new Vector(getUnscaledWidth(), yOffset, 1)); 148 } 149 150 /** 151 * Getter for the font 152 * @return the font 153 * @since iText 5.0.2 154 */ 155 public DocumentFont getFont() { 156 return gs.getFont(); 157 } 158 159 /** 160 * @return The width, in user space units, of a single space character in the current font 161 */ 162 public float getSingleSpaceWidth(){ 163 LineSegment textSpace = new LineSegment(new Vector(0, 0, 1), new Vector(getUnscaledFontSpaceWidth(), 0, 1)); 164 LineSegment userSpace = textSpace.transformBy(textToUserSpaceTransformMatrix); 165 return userSpace.getLength(); 166 } 167 168 /** 169 * @return the text render mode that should be used for the text. From the 170 * PDF specification, this means: 171 * <ul> 172 * <li>0 = Fill text</li> 173 * <li>1 = Stroke text</li> 174 * <li>2 = Fill, then stroke text</li> 175 * <li>3 = Invisible</li> 176 * <li>4 = Fill text and add to path for clipping</li> 177 * <li>5 = Stroke text and add to path for clipping</li> 178 * <li>6 = Fill, then stroke text and add to path for clipping</li> 179 * <li>7 = Add text to padd for clipping</li> 180 * </ul> 181 * @since iText 5.0.1 182 */ 183 public int getTextRenderMode(){ 184 return gs.renderMode; 185 } 186 187 /** 188 * Calculates the width of a space character. If the font does not define 189 * a width for a standard space character \u0020, we also attempt to use 190 * the width of \u00A0 (a non-breaking space in many fonts) 191 * @return the width of a single space character in text space units 192 */ 193 private float getUnscaledFontSpaceWidth(){ 194 char charToUse = ' '; 195 if (gs.font.getWidth(charToUse) == 0) 196 charToUse = '\u00A0'; 197 return getStringWidth(String.valueOf(charToUse)); 198 } 199 200 /** 201 * Gets the width of a String in text space units 202 * @param string the string that needs measuring 203 * @return the width of a String in text space units 204 */ 205 private float getStringWidth(String string){ 206 DocumentFont font = gs.font; 207 char[] chars = string.toCharArray(); 208 float totalWidth = 0; 209 for (int i = 0; i < chars.length; i++) { 210 float w = font.getWidth(chars[i]) / 1000.0f; 211 float wordSpacing = chars[i] == 32 ? gs.wordSpacing : 0f; 212 totalWidth += (w * gs.fontSize + gs.characterSpacing + wordSpacing) * gs.horizontalScaling; 213 } 214 215 return totalWidth; 216 } 217 218}