KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > PdfChunk


1 /*
2  * $Id: PdfChunk.java 2742 2007-05-08 13:04:56Z blowagie $
3  * $Name$
4  *
5  * Copyright 1999, 2000, 2001, 2002 Bruno Lowagie
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
28  * provisions of LGPL are applicable instead of those above. If you wish to
29  * allow use of your version of this file only under the terms of the LGPL
30  * License and not to allow others to use your version of this file under
31  * the MPL, indicate your decision by deleting the provisions above and
32  * replace them with the notice and other provisions required by the LGPL.
33  * If you do not delete the provisions above, a recipient may use your version
34  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Library General Public License as published by the Free Software Foundation;
39  * either version 2 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
44  * details.
45  *
46  * If you didn't download this code from the following link, you should check if
47  * you aren't using an obsolete version:
48  * http://www.lowagie.com/iText/
49  */

50
51 package com.lowagie.text.pdf;
52
53 import java.awt.Color JavaDoc;
54 import java.util.HashMap JavaDoc;
55 import java.util.Iterator JavaDoc;
56 import java.util.Map JavaDoc;
57
58 import com.lowagie.text.Chunk;
59 import com.lowagie.text.Font;
60 import com.lowagie.text.Image;
61 import com.lowagie.text.SplitCharacter;
62 import com.lowagie.text.Utilities;
63
64 /**
65  * A <CODE>PdfChunk</CODE> is the PDF translation of a <CODE>Chunk</CODE>.
66  * <P>
67  * A <CODE>PdfChunk</CODE> is a <CODE>PdfString</CODE> in a certain
68  * <CODE>PdfFont</CODE> and <CODE>Color</CODE>.
69  *
70  * @see PdfString
71  * @see com.lowagie.text.Chunk
72  * @see com.lowagie.text.Font
73  */

74
75 public class PdfChunk implements SplitCharacter{
76
77     private static final char singleSpace[] = {' '};
78     private static final PdfChunk thisChunk[] = new PdfChunk[1];
79     private static final float ITALIC_ANGLE = 0.21256f;
80 /** The allowed attributes in variable <CODE>attributes</CODE>. */
81     private static final HashMap JavaDoc keysAttributes = new HashMap JavaDoc();
82     
83 /** The allowed attributes in variable <CODE>noStroke</CODE>. */
84     private static final HashMap JavaDoc keysNoStroke = new HashMap JavaDoc();
85     
86     static {
87         keysAttributes.put(Chunk.ACTION, null);
88         keysAttributes.put(Chunk.UNDERLINE, null);
89         keysAttributes.put(Chunk.REMOTEGOTO, null);
90         keysAttributes.put(Chunk.LOCALGOTO, null);
91         keysAttributes.put(Chunk.LOCALDESTINATION, null);
92         keysAttributes.put(Chunk.GENERICTAG, null);
93         keysAttributes.put(Chunk.NEWPAGE, null);
94         keysAttributes.put(Chunk.IMAGE, null);
95         keysAttributes.put(Chunk.BACKGROUND, null);
96         keysAttributes.put(Chunk.PDFANNOTATION, null);
97         keysAttributes.put(Chunk.SKEW, null);
98         keysAttributes.put(Chunk.HSCALE, null);
99         keysNoStroke.put(Chunk.SUBSUPSCRIPT, null);
100         keysNoStroke.put(Chunk.SPLITCHARACTER, null);
101         keysNoStroke.put(Chunk.HYPHENATION, null);
102         keysNoStroke.put(Chunk.TEXTRENDERMODE, null);
103     }
104     
105     // membervariables
106

107     /** The value of this object. */
108     protected String JavaDoc value = PdfObject.NOTHING;
109     
110     /** The encoding. */
111     protected String JavaDoc encoding = BaseFont.WINANSI;
112     
113     
114 /** The font for this <CODE>PdfChunk</CODE>. */
115     protected PdfFont font;
116     
117     protected BaseFont baseFont;
118     
119     protected SplitCharacter splitCharacter;
120 /**
121  * Metric attributes.
122  * <P>
123  * This attributes require the mesurement of characters widths when rendering
124  * such as underline.
125  */

126     protected HashMap JavaDoc attributes = new HashMap JavaDoc();
127     
128 /**
129  * Non metric attributes.
130  * <P>
131  * This attributes do not require the mesurement of characters widths when rendering
132  * such as Color.
133  */

134     protected HashMap JavaDoc noStroke = new HashMap JavaDoc();
135     
136 /** <CODE>true</CODE> if the chunk split was cause by a newline. */
137     protected boolean newlineSplit;
138     
139 /** The image in this <CODE>PdfChunk</CODE>, if it has one */
140     protected Image image;
141     
142 /** The offset in the x direction for the image */
143     protected float offsetX;
144     
145 /** The offset in the y direction for the image */
146     protected float offsetY;
147
148 /** Indicates if the height and offset of the Image has to be taken into account */
149     protected boolean changeLeading = false;
150
151     // constructors
152

153 /**
154  * Constructs a <CODE>PdfChunk</CODE>-object.
155  *
156  * @param string the content of the <CODE>PdfChunk</CODE>-object
157  * @param other Chunk with the same style you want for the new Chunk
158  */

159     
160     PdfChunk(String JavaDoc string, PdfChunk other) {
161         thisChunk[0] = this;
162         value = string;
163         this.font = other.font;
164         this.attributes = other.attributes;
165         this.noStroke = other.noStroke;
166         this.baseFont = other.baseFont;
167         Object JavaDoc obj[] = (Object JavaDoc[])attributes.get(Chunk.IMAGE);
168         if (obj == null)
169             image = null;
170         else {
171             image = (Image)obj[0];
172             offsetX = ((Float JavaDoc)obj[1]).floatValue();
173             offsetY = ((Float JavaDoc)obj[2]).floatValue();
174             changeLeading = ((Boolean JavaDoc)obj[3]).booleanValue();
175         }
176         encoding = font.getFont().getEncoding();
177         splitCharacter = (SplitCharacter)noStroke.get(Chunk.SPLITCHARACTER);
178         if (splitCharacter == null)
179             splitCharacter = this;
180     }
181     
182 /**
183  * Constructs a <CODE>PdfChunk</CODE>-object.
184  *
185  * @param chunk the original <CODE>Chunk</CODE>-object
186  * @param action the <CODE>PdfAction</CODE> if the <CODE>Chunk</CODE> comes from an <CODE>Anchor</CODE>
187  */

188     
189     PdfChunk(Chunk chunk, PdfAction action) {
190         thisChunk[0] = this;
191         value = chunk.getContent();
192         
193         Font f = chunk.getFont();
194         float size = f.getSize();
195         if (size == Font.UNDEFINED)
196             size = 12;
197         baseFont = f.getBaseFont();
198         int style = f.getStyle();
199         if (style == Font.UNDEFINED) {
200             style = Font.NORMAL;
201         }
202         if (baseFont == null) {
203             // translation of the font-family to a PDF font-family
204
baseFont = f.getCalculatedBaseFont(false);
205         }
206         else {
207             // bold simulation
208
if ((style & Font.BOLD) != 0)
209                 attributes.put(Chunk.TEXTRENDERMODE, new Object JavaDoc[]{new Integer JavaDoc(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE), new Float JavaDoc(size / 30f), null});
210             // italic simulation
211
if ((style & Font.ITALIC) != 0)
212                 attributes.put(Chunk.SKEW, new float[]{0, ITALIC_ANGLE});
213         }
214         font = new PdfFont(baseFont, size);
215         // other style possibilities
216
HashMap JavaDoc attr = chunk.getAttributes();
217         if (attr != null) {
218             for (Iterator JavaDoc i = attr.entrySet().iterator(); i.hasNext();) {
219                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
220                 Object JavaDoc name = entry.getKey();
221                 if (keysAttributes.containsKey(name)) {
222                     attributes.put(name, entry.getValue());
223                 }
224                 else if (keysNoStroke.containsKey(name)) {
225                     noStroke.put(name, entry.getValue());
226                 }
227             }
228             if ("".equals(attr.get(Chunk.GENERICTAG))) {
229                 attributes.put(Chunk.GENERICTAG, chunk.getContent());
230             }
231         }
232         if (f.isUnderlined()) {
233             Object JavaDoc obj[] = {null, new float[]{0, 1f / 15, 0, -1f / 3, 0}};
234             Object JavaDoc unders[][] = Utilities.addToArray((Object JavaDoc[][])attributes.get(Chunk.UNDERLINE), obj);
235             attributes.put(Chunk.UNDERLINE, unders);
236         }
237         if (f.isStrikethru()) {
238             Object JavaDoc obj[] = {null, new float[]{0, 1f / 15, 0, 1f / 3, 0}};
239             Object JavaDoc unders[][] = Utilities.addToArray((Object JavaDoc[][])attributes.get(Chunk.UNDERLINE), obj);
240             attributes.put(Chunk.UNDERLINE, unders);
241         }
242         if (action != null)
243             attributes.put(Chunk.ACTION, action);
244         // the color can't be stored in a PdfFont
245
noStroke.put(Chunk.COLOR, f.getColor());
246         noStroke.put(Chunk.ENCODING, font.getFont().getEncoding());
247         Object JavaDoc obj[] = (Object JavaDoc[])attributes.get(Chunk.IMAGE);
248         if (obj == null) {
249             image = null;
250         }
251         else {
252             attributes.remove(Chunk.HSCALE); // images are scaled in other ways
253
image = (Image)obj[0];
254             offsetX = ((Float JavaDoc)obj[1]).floatValue();
255             offsetY = ((Float JavaDoc)obj[2]).floatValue();
256             changeLeading = ((Boolean JavaDoc)obj[3]).booleanValue();
257         }
258         font.setImage(image);
259         Float JavaDoc hs = (Float JavaDoc)attributes.get(Chunk.HSCALE);
260         if (hs != null)
261             font.setHorizontalScaling(hs.floatValue());
262         encoding = font.getFont().getEncoding();
263         splitCharacter = (SplitCharacter)noStroke.get(Chunk.SPLITCHARACTER);
264         if (splitCharacter == null)
265             splitCharacter = this;
266     }
267     
268     // methods
269

270     /** Gets the Unicode equivalent to a CID.
271      * The (inexistent) CID <FF00> is translated as '\n'.
272      * It has only meaning with CJK fonts with Identity encoding.
273      * @param c the CID code
274      * @return the Unicode equivalent
275      */

276     public char getUnicodeEquivalent(char c) {
277         return baseFont.getUnicodeEquivalent(c);
278     }
279
280     protected int getWord(String JavaDoc text, int start) {
281         int len = text.length();
282         while (start < len) {
283             if (!Character.isLetter(text.charAt(start)))
284                 break;
285             ++start;
286         }
287         return start;
288     }
289     
290 /**
291  * Splits this <CODE>PdfChunk</CODE> if it's too long for the given width.
292  * <P>
293  * Returns <VAR>null</VAR> if the <CODE>PdfChunk</CODE> wasn't truncated.
294  *
295  * @param width a given width
296  * @return the <CODE>PdfChunk</CODE> that doesn't fit into the width.
297  */

298     
299     PdfChunk split(float width) {
300         newlineSplit = false;
301         if (image != null) {
302             if (image.getScaledWidth() > width) {
303                 PdfChunk pc = new PdfChunk(Chunk.OBJECT_REPLACEMENT_CHARACTER, this);
304                 value = "";
305                 attributes = new HashMap JavaDoc();
306                 image = null;
307                 font = PdfFont.getDefaultFont();
308                 return pc;
309             }
310             else
311                 return null;
312         }
313         HyphenationEvent hyphenationEvent = (HyphenationEvent)noStroke.get(Chunk.HYPHENATION);
314         int currentPosition = 0;
315         int splitPosition = -1;
316         float currentWidth = 0;
317         
318         // loop over all the characters of a string
319
// or until the totalWidth is reached
320
int lastSpace = -1;
321         float lastSpaceWidth = 0;
322         int length = value.length();
323         char valueArray[] = value.toCharArray();
324         char character = 0;
325         BaseFont ft = font.getFont();
326         if (ft.getFontType() == BaseFont.FONT_TYPE_CJK && ft.getUnicodeEquivalent(' ') != ' ') {
327             while (currentPosition < length) {
328                 // the width of every character is added to the currentWidth
329
char cidChar = valueArray[currentPosition];
330                 character = ft.getUnicodeEquivalent(cidChar);
331                 // if a newLine or carriageReturn is encountered
332
if (character == '\n') {
333                     newlineSplit = true;
334                     String JavaDoc returnValue = value.substring(currentPosition + 1);
335                     value = value.substring(0, currentPosition);
336                     if (value.length() < 1) {
337                         value = "\u0001";
338                     }
339                     PdfChunk pc = new PdfChunk(returnValue, this);
340                     return pc;
341                 }
342                 currentWidth += font.width(cidChar);
343                 if (character == ' ') {
344                     lastSpace = currentPosition + 1;
345                     lastSpaceWidth = currentWidth;
346                 }
347                 if (currentWidth > width)
348                     break;
349                 // if a split-character is encountered, the splitPosition is altered
350
if (splitCharacter.isSplitCharacter(0, currentPosition, length, valueArray, thisChunk))
351                     splitPosition = currentPosition + 1;
352                 currentPosition++;
353             }
354         }
355         else {
356             while (currentPosition < length) {
357                 // the width of every character is added to the currentWidth
358
character = valueArray[currentPosition];
359                 // if a newLine or carriageReturn is encountered
360
if (character == '\r' || character == '\n') {
361                     newlineSplit = true;
362                     int inc = 1;
363                     if (character == '\r' && currentPosition + 1 < length && valueArray[currentPosition + 1] == '\n')
364                         inc = 2;
365                     String JavaDoc returnValue = value.substring(currentPosition + inc);
366                     value = value.substring(0, currentPosition);
367                     if (value.length() < 1) {
368                         value = " ";
369                     }
370                     PdfChunk pc = new PdfChunk(returnValue, this);
371                     return pc;
372                 }
373                 currentWidth += font.width(character);
374                 if (character == ' ') {
375                     lastSpace = currentPosition + 1;
376                     lastSpaceWidth = currentWidth;
377                 }
378                 if (currentWidth > width)
379                     break;
380                 // if a split-character is encountered, the splitPosition is altered
381
if (splitCharacter.isSplitCharacter(0, currentPosition, length, valueArray, null))
382                     splitPosition = currentPosition + 1;
383                 currentPosition++;
384             }
385         }
386         
387         // if all the characters fit in the total width, null is returned (there is no overflow)
388
if (currentPosition == length) {
389             return null;
390         }
391         // otherwise, the string has to be truncated
392
if (splitPosition < 0) {
393             String JavaDoc returnValue = value;
394             value = "";
395             PdfChunk pc = new PdfChunk(returnValue, this);
396             return pc;
397         }
398         if (lastSpace > splitPosition && splitCharacter.isSplitCharacter(0, 0, 1, singleSpace, null))
399             splitPosition = lastSpace;
400         if (hyphenationEvent != null && lastSpace >= 0 && lastSpace < currentPosition) {
401             int wordIdx = getWord(value, lastSpace);
402             if (wordIdx > lastSpace) {
403                 String JavaDoc pre = hyphenationEvent.getHyphenatedWordPre(value.substring(lastSpace, wordIdx), font.getFont(), font.size(), width - lastSpaceWidth);
404                 String JavaDoc post = hyphenationEvent.getHyphenatedWordPost();
405                 if (pre.length() > 0) {
406                     String JavaDoc returnValue = post + value.substring(wordIdx);
407                     value = trim(value.substring(0, lastSpace) + pre);
408                     PdfChunk pc = new PdfChunk(returnValue, this);
409                     return pc;
410                 }
411             }
412         }
413         String JavaDoc returnValue = value.substring(splitPosition);
414         value = trim(value.substring(0, splitPosition));
415         PdfChunk pc = new PdfChunk(returnValue, this);
416         return pc;
417     }
418     
419 /**
420  * Truncates this <CODE>PdfChunk</CODE> if it's too long for the given width.
421  * <P>
422  * Returns <VAR>null</VAR> if the <CODE>PdfChunk</CODE> wasn't truncated.
423  *
424  * @param width a given width
425  * @return the <CODE>PdfChunk</CODE> that doesn't fit into the width.
426  */

427     
428     PdfChunk truncate(float width) {
429         if (image != null) {
430             if (image.getScaledWidth() > width) {
431                 PdfChunk pc = new PdfChunk("", this);
432                 value = "";
433                 attributes.remove(Chunk.IMAGE);
434                 image = null;
435                 font = PdfFont.getDefaultFont();
436                 return pc;
437             }
438             else
439                 return null;
440         }
441         
442         int currentPosition = 0;
443         float currentWidth = 0;
444         
445         // it's no use trying to split if there isn't even enough place for a space
446
if (width < font.width()) {
447             String JavaDoc returnValue = value.substring(1);
448             value = value.substring(0, 1);
449             PdfChunk pc = new PdfChunk(returnValue, this);
450             return pc;
451         }
452         
453         // loop over all the characters of a string
454
// or until the totalWidth is reached
455
int length = value.length();
456         char character;
457         while (currentPosition < length) {
458             // the width of every character is added to the currentWidth
459
character = value.charAt(currentPosition);
460             currentWidth += font.width(character);
461             if (currentWidth > width)
462                 break;
463             currentPosition++;
464         }
465         
466         // if all the characters fit in the total width, null is returned (there is no overflow)
467
if (currentPosition == length) {
468             return null;
469         }
470         
471         // otherwise, the string has to be truncated
472
//currentPosition -= 2;
473
// we have to chop off minimum 1 character from the chunk
474
if (currentPosition == 0) {
475             currentPosition = 1;
476         }
477         String JavaDoc returnValue = value.substring(currentPosition);
478         value = value.substring(0, currentPosition);
479         PdfChunk pc = new PdfChunk(returnValue, this);
480         return pc;
481     }
482     
483     // methods to retrieve the membervariables
484

485 /**
486  * Returns the font of this <CODE>Chunk</CODE>.
487  *
488  * @return a <CODE>PdfFont</CODE>
489  */

490     
491     PdfFont font() {
492         return font;
493     }
494     
495 /**
496  * Returns the color of this <CODE>Chunk</CODE>.
497  *
498  * @return a <CODE>Color</CODE>
499  */

500     
501     Color JavaDoc color() {
502         return (Color JavaDoc)noStroke.get(Chunk.COLOR);
503     }
504     
505 /**
506  * Returns the width of this <CODE>PdfChunk</CODE>.
507  *
508  * @return a width
509  */

510     
511     float width() {
512         return font.width(value);
513     }
514     
515 /**
516  * Checks if the <CODE>PdfChunk</CODE> split was caused by a newline.
517  * @return <CODE>true</CODE> if the <CODE>PdfChunk</CODE> split was caused by a newline.
518  */

519     
520     public boolean isNewlineSplit()
521     {
522         return newlineSplit;
523     }
524     
525 /**
526  * Gets the width of the <CODE>PdfChunk</CODE> taking into account the
527  * extra character and word spacing.
528  * @param charSpacing the extra character spacing
529  * @param wordSpacing the extra word spacing
530  * @return the calculated width
531  */

532     
533     public float getWidthCorrected(float charSpacing, float wordSpacing)
534     {
535         if (image != null) {
536             return image.getScaledWidth() + charSpacing;
537         }
538         int numberOfSpaces = 0;
539         int idx = -1;
540         while ((idx = value.indexOf(' ', idx + 1)) >= 0)
541             ++numberOfSpaces;
542         return width() + (value.length() * charSpacing + numberOfSpaces * wordSpacing);
543     }
544     
545     /**
546      * Gets the text displacement relatiev to the baseline.
547      * @return a displacement in points
548      */

549     public float getTextRise() {
550         Float JavaDoc f = (Float JavaDoc) getAttribute(Chunk.SUBSUPSCRIPT);
551         if (f != null) {
552             return f.floatValue();
553         }
554         return 0.0f;
555     }
556     
557 /**
558  * Trims the last space.
559  * @return the width of the space trimmed, otherwise 0
560  */

561     
562     public float trimLastSpace()
563     {
564         BaseFont ft = font.getFont();
565         if (ft.getFontType() == BaseFont.FONT_TYPE_CJK && ft.getUnicodeEquivalent(' ') != ' ') {
566             if (value.length() > 1 && value.endsWith("\u0001")) {
567                 value = value.substring(0, value.length() - 1);
568                 return font.width('\u0001');
569             }
570         }
571         else {
572             if (value.length() > 1 && value.endsWith(" ")) {
573                 value = value.substring(0, value.length() - 1);
574                 return font.width(' ');
575             }
576         }
577         return 0;
578     }
579     public float trimFirstSpace()
580     {
581         BaseFont ft = font.getFont();
582         if (ft.getFontType() == BaseFont.FONT_TYPE_CJK && ft.getUnicodeEquivalent(' ') != ' ') {
583             if (value.length() > 1 && value.startsWith("\u0001")) {
584                 value = value.substring(1);
585                 return font.width('\u0001');
586             }
587         }
588         else {
589             if (value.length() > 1 && value.startsWith(" ")) {
590                 value = value.substring(1);
591                 return font.width(' ');
592             }
593         }
594         return 0;
595     }
596     
597 /**
598  * Gets an attribute. The search is made in <CODE>attributes</CODE>
599  * and <CODE>noStroke</CODE>.
600  * @param name the attribute key
601  * @return the attribute value or null if not found
602  */

603     
604     Object JavaDoc getAttribute(String JavaDoc name)
605     {
606         if (attributes.containsKey(name))
607             return attributes.get(name);
608         return noStroke.get(name);
609     }
610     
611 /**
612  *Checks if the attribute exists.
613  * @param name the attribute key
614  * @return <CODE>true</CODE> if the attribute exists
615  */

616     
617     boolean isAttribute(String JavaDoc name)
618     {
619         if (attributes.containsKey(name))
620             return true;
621         return noStroke.containsKey(name);
622     }
623     
624 /**
625  * Checks if this <CODE>PdfChunk</CODE> needs some special metrics handling.
626  * @return <CODE>true</CODE> if this <CODE>PdfChunk</CODE> needs some special metrics handling.
627  */

628     
629     boolean isStroked()
630     {
631         return (!attributes.isEmpty());
632     }
633     
634 /**
635  * Checks if there is an image in the <CODE>PdfChunk</CODE>.
636  * @return <CODE>true</CODE> if an image is present
637  */

638     
639     boolean isImage()
640     {
641         return image != null;
642     }
643     
644 /**
645  * Gets the image in the <CODE>PdfChunk</CODE>.
646  * @return the image or <CODE>null</CODE>
647  */

648     
649     Image getImage()
650     {
651         return image;
652     }
653     
654 /**
655  * Sets the image offset in the x direction
656  * @param offsetX the image offset in the x direction
657  */

658     
659     void setImageOffsetX(float offsetX)
660     {
661         this.offsetX = offsetX;
662     }
663     
664 /**
665  * Gets the image offset in the x direction
666  * @return the image offset in the x direction
667  */

668     
669     float getImageOffsetX()
670     {
671         return offsetX;
672     }
673     
674 /**
675  * Sets the image offset in the y direction
676  * @param offsetY the image offset in the y direction
677  */

678     
679     void setImageOffsetY(float offsetY)
680     {
681         this.offsetY = offsetY;
682     }
683     
684 /**
685  * Gets the image offset in the y direction
686  * @return Gets the image offset in the y direction
687  */

688     
689     float getImageOffsetY()
690     {
691         return offsetY;
692     }
693     
694 /**
695  * sets the value.
696  * @param value content of the Chunk
697  */

698     
699     void setValue(String JavaDoc value)
700     {
701         this.value = value;
702     }
703
704     /**
705      * @see java.lang.Object#toString()
706      */

707     public String JavaDoc toString() {
708         return value;
709     }
710
711     /**
712      * Tells you if this string is in Chinese, Japanese, Korean or Identity-H.
713      * @return true if the Chunk has a special encoding
714      */

715     
716     boolean isSpecialEncoding() {
717         return encoding.equals(CJKFont.CJK_ENCODING) || encoding.equals(BaseFont.IDENTITY_H);
718     }
719     
720     /**
721      * Gets the encoding of this string.
722      *
723      * @return a <CODE>String</CODE>
724      */

725     
726     String JavaDoc getEncoding() {
727         return encoding;
728     }
729
730     int length() {
731         return value.length();
732     }
733 /**
734  * Checks if a character can be used to split a <CODE>PdfString</CODE>.
735  * <P>
736  * for the moment every character less than or equal to SPACE and the character '-' are 'splitCharacters'.
737  *
738  * @param start start position in the array
739  * @param current current position in the array
740  * @param end end position in the array
741  * @param cc the character array that has to be checked
742  * @param ck chunk array
743  * @return <CODE>true</CODE> if the character can be used to split a string, <CODE>false</CODE> otherwise
744  */

745     public boolean isSplitCharacter(int start, int current, int end, char[] cc, PdfChunk[] ck) {
746         char c;
747         if (ck == null)
748             c = cc[current];
749         else
750             c = ck[Math.min(current, ck.length - 1)].getUnicodeEquivalent(cc[current]);
751         if (c <= ' ' || c == '-') {
752             return true;
753         }
754         if (c < 0x2e80)
755             return false;
756         return ((c >= 0x2e80 && c < 0xd7a0)
757         || (c >= 0xf900 && c < 0xfb00)
758         || (c >= 0xfe30 && c < 0xfe50)
759         || (c >= 0xff61 && c < 0xffa0));
760     }
761     
762     boolean isExtSplitCharacter(int start, int current, int end, char[] cc, PdfChunk[] ck) {
763         return splitCharacter.isSplitCharacter(start, current, end, cc, ck);
764     }
765     
766 /**
767  * Removes all the <VAR>' '</VAR> and <VAR>'-'</VAR>-characters on the right of a <CODE>String</CODE>.
768  * <P>
769  * @param string the <CODE>String<CODE> that has to be trimmed.
770  * @return the trimmed <CODE>String</CODE>
771  */

772     String JavaDoc trim(String JavaDoc string) {
773         BaseFont ft = font.getFont();
774         if (ft.getFontType() == BaseFont.FONT_TYPE_CJK && ft.getUnicodeEquivalent(' ') != ' ') {
775             while (string.endsWith("\u0001")) {
776                 string = string.substring(0, string.length() - 1);
777             }
778         }
779         else {
780             while (string.endsWith(" ") || string.endsWith("\t")) {
781                 string = string.substring(0, string.length() - 1);
782             }
783         }
784         return string;
785     }
786
787     public boolean changeLeading() {
788         return changeLeading;
789     }
790     
791     float getCharWidth(char c) {
792         if (noPrint(c))
793             return 0;
794         return font.width(c);
795     }
796     
797     public static boolean noPrint(char c) {
798         return ((c >= 0x200b && c <= 0x200f) || (c >= 0x202a && c <= 0x202e));
799     }
800     
801 }
802
Popular Tags