KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > font > Type3StreamParser


1 /**
2  * Copyright (c) 2003-2004, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.pdmodel.font;
32
33 import java.awt.Image JavaDoc;
34
35 import java.io.IOException JavaDoc;
36
37 import java.util.List JavaDoc;
38
39 import org.fontbox.util.BoundingBox;
40
41 import org.pdfbox.cos.COSNumber;
42 import org.pdfbox.cos.COSStream;
43
44 import org.pdfbox.pdmodel.graphics.xobject.PDInlinedImage;
45
46 import org.pdfbox.util.ImageParameters;
47 import org.pdfbox.util.PDFOperator;
48 import org.pdfbox.util.PDFStreamEngine;
49
50 /**
51  * This class will handle creating an image for a type 3 glyph.
52  *
53  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
54  * @version $Revision: 1.10 $
55  */

56 public class Type3StreamParser extends PDFStreamEngine
57 {
58     private PDInlinedImage image = null;
59     private BoundingBox box = null;
60
61
62     /**
63      * This will parse a type3 stream and create an image from it.
64      *
65      * @param type3Stream The stream containing the operators to draw the image.
66      *
67      * @return The image that was created.
68      *
69      * @throws IOException If there is an error processing the stream.
70      */

71     public Image JavaDoc createImage( COSStream type3Stream ) throws IOException JavaDoc
72     {
73         processStream( null, null, type3Stream );
74         return image.createImage();
75     }
76
77     /**
78      * This is used to handle an operation.
79      *
80      * @param operator The operation to perform.
81      * @param arguments The list of arguments.
82      *
83      * @throws IOException If there is an error processing the operation.
84      */

85     protected void processOperator( PDFOperator operator, List JavaDoc arguments ) throws IOException JavaDoc
86     {
87         super.processOperator( operator, arguments );
88         String JavaDoc operation = operator.getOperation();
89         /**
90         if( operation.equals( "b" ) )
91         {
92             //Close, fill, and stroke path using nonzero winding number rule
93         }
94         else if( operation.equals( "B" ) )
95         {
96             //Fill and stroke path using nonzero winding number rule
97         }
98         else if( operation.equals( "b*" ) )
99         {
100             //Close, fill, and stroke path using even-odd rule
101         }
102         else if( operation.equals( "B*" ) )
103         {
104             //Fill and stroke path using even-odd rule
105         }
106         else if( operation.equals( "BDC" ) )
107         {
108             //(PDF 1.2) Begin marked-content sequence with property list
109         }
110         else **/
if( operation.equals( "BI" ) )
111         {
112             ImageParameters params = operator.getImageParameters();
113             image = new PDInlinedImage();
114             image.setImageParameters( params );
115             image.setImageData( operator.getImageData() );
116             //begin inline image object
117
}/**
118         else if( operation.equals( "BMC" ) )
119         {
120             //(PDF 1.2) Begin marked-content sequence
121         }
122         else if( operation.equals( "BT" ) )
123         {
124             log.debug( "<BT>" );
125             textMatrix = new Matrix();
126             textLineMatrix = new Matrix();
127         }
128         else if( operation.equals( "BX" ) )
129         {
130             //(PDF 1.1) Begin compatibility section
131         }
132         else if( operation.equals( "c" ) )
133         {
134             //Append curved segment to path (three control points)
135         }
136         else if( operation.equals( "cm" ) )
137         {
138         }
139         else if( operation.equals( "cs" ) )
140         {
141         }
142         else if( operation.equals( "CS" ) )
143         {
144         }
145         else if( operation.equals( "d" ) )
146         {
147             //Set the line dash pattern in the graphics state
148         }
149         else */
if( operation.equals( "d0" ) )
150         {
151             //set glyph with for a type3 font
152
//COSNumber horizontalWidth = (COSNumber)arguments.get( 0 );
153
//COSNumber verticalWidth = (COSNumber)arguments.get( 1 );
154
//width = horizontalWidth.intValue();
155
//height = verticalWidth.intValue();
156
}
157         else if( operation.equals( "d1" ) )
158         {
159             //set glyph with and bounding box for type 3 font
160
//COSNumber horizontalWidth = (COSNumber)arguments.get( 0 );
161
//COSNumber verticalWidth = (COSNumber)arguments.get( 1 );
162
COSNumber llx = (COSNumber)arguments.get( 2 );
163             COSNumber lly = (COSNumber)arguments.get( 3 );
164             COSNumber urx = (COSNumber)arguments.get( 4 );
165             COSNumber ury = (COSNumber)arguments.get( 5 );
166
167             //width = horizontalWidth.intValue();
168
//height = verticalWidth.intValue();
169
box = new BoundingBox();
170             box.setLowerLeftX( llx.floatValue() );
171             box.setLowerLeftY( lly.floatValue() );
172             box.setUpperRightX( urx.floatValue() );
173             box.setUpperRightY( ury.floatValue() );
174         }/*
175         else if( operation.equals( "Do" ) )
176         {
177             //invoke named object.
178         }
179         else if( operation.equals( "DP" ) )
180         {
181             //(PDF 1.2) De.ne marked-content point with property list
182         }
183         else if( operation.equals( "EI" ) )
184         {
185             //end inline image object
186         }
187         else if( operation.equals( "EMC" ) )
188         {
189             //End inline image object
190         }
191         else if( operation.equals( "ET" ) )
192         {
193             log.debug( "<ET>" );
194             textMatrix = null;
195             textLineMatrix = null;
196         }
197         else if( operation.equals( "EX" ) )
198         {
199             //(PDF 1.1) End compatibility section
200         }
201         else if( operation.equals( "f" ) )
202         {
203             //Fill the path, using the nonzero winding number rule to determine the region to .ll
204         }
205         else if( operation.equals( "F" ) )
206         {
207         }
208         else if( operation.equals( "f*" ) )
209         {
210             //Fill path using even-odd rule
211         }
212         else if( operation.equals( "g" ) )
213         {
214         }
215         else if( operation.equals( "G" ) )
216         {
217         }
218         else if( operation.equals( "gs" ) )
219         {
220         }
221         else if( operation.equals( "h" ) )
222         {
223             //close subpath
224         }
225         else if( operation.equals( "i" ) )
226         {
227             //set flatness tolerance, not sure what this does
228         }
229         else if( operation.equals( "ID" ) )
230         {
231             //begin inline image data
232         }
233         else if( operation.equals( "j" ) )
234         {
235             //Set the line join style in the graphics state
236             //System.out.println( "<j>" );
237         }
238         else if( operation.equals( "J" ) )
239         {
240             //Set the line cap style in the graphics state
241             //System.out.println( "<J>" );
242         }
243         else if( operation.equals( "k" ) )
244         {
245             //Set CMYK color for nonstroking operations
246         }
247         else if( operation.equals( "K" ) )
248         {
249             //Set CMYK color for stroking operations
250         }
251         else if( operation.equals( "l" ) )
252         {
253             //append straight line segment from the current point to the point.
254             COSNumber x = (COSNumber)arguments.get( 0 );
255             COSNumber y = (COSNumber)arguments.get( 1 );
256             linePath.lineTo( x.floatValue(), pageSize.getHeight()-y.floatValue() );
257         }
258         else if( operation.equals( "m" ) )
259         {
260             COSNumber x = (COSNumber)arguments.get( 0 );
261             COSNumber y = (COSNumber)arguments.get( 1 );
262             linePath.reset();
263             linePath.moveTo( x.floatValue(), pageSize.getHeight()-y.floatValue() );
264             //System.out.println( "<m x=\"" + x.getValue() + "\" y=\"" + y.getValue() + "\" >" );
265         }
266         else if( operation.equals( "M" ) )
267         {
268             //System.out.println( "<M>" );
269         }
270         else if( operation.equals( "MP" ) )
271         {
272             //(PDF 1.2) Define marked-content point
273         }
274         else if( operation.equals( "n" ) )
275         {
276             //End path without .lling or stroking
277             //System.out.println( "<n>" );
278         }
279         else if( operation.equals( "q" ) )
280         {
281             //save graphics state
282             if( log.isDebugEnabled() )
283             {
284                 log.debug( "<" + operation + "> - save state" );
285             }
286             graphicsStack.push(graphicsState.clone());
287         }
288         else if( operation.equals( "Q" ) )
289         {
290             //restore graphics state
291             if( log.isDebugEnabled() )
292             {
293                 log.debug( "<" + operation + "> - restore state" );
294             }
295             graphicsState = (PDGraphicsState)graphicsStack.pop();
296         }
297         else if( operation.equals( "re" ) )
298         {
299         }
300         else if( operation.equals( "rg" ) )
301         {
302             //Set RGB color for nonstroking operations
303         }
304         else if( operation.equals( "RG" ) )
305         {
306             //Set RGB color for stroking operations
307         }
308         else if( operation.equals( "ri" ) )
309         {
310             //Set color rendering intent
311         }
312         else if( operation.equals( "s" ) )
313         {
314             //Close and stroke path
315         }
316         else if( operation.equals( "S" ) )
317         {
318             graphics.draw( linePath );
319         }
320         else if( operation.equals( "sc" ) )
321         {
322             //set color for nonstroking operations
323             //System.out.println( "<sc>" );
324         }
325         else if( operation.equals( "SC" ) )
326         {
327             //set color for stroking operations
328             //System.out.println( "<SC>" );
329         }
330         else if( operation.equals( "scn" ) )
331         {
332             //set color for nonstroking operations special
333         }
334         else if( operation.equals( "SCN" ) )
335         {
336             //set color for stroking operations special
337         }
338         else if( operation.equals( "sh" ) )
339         {
340             //(PDF 1.3) Paint area de.ned by shading pattern
341         }
342         else if( operation.equals( "T*" ) )
343         {
344             if (log.isDebugEnabled())
345             {
346                 log.debug("<T* graphicsState.getTextState().getLeading()=\"" +
347                     graphicsState.getTextState().getLeading() + "\">");
348             }
349             //move to start of next text line
350             if( graphicsState.getTextState().getLeading() == 0 )
351             {
352                 graphicsState.getTextState().setLeading( -.01f );
353             }
354             Matrix td = new Matrix();
355             td.setValue( 2, 1, -1 * graphicsState.getTextState().getLeading() * textMatrix.getValue(1,1));
356             textLineMatrix = textLineMatrix.multiply( td );
357             textMatrix = textLineMatrix.copy();
358         }
359         else if( operation.equals( "Tc" ) )
360         {
361             //set character spacing
362             COSNumber characterSpacing = (COSNumber)arguments.get( 0 );
363             if (log.isDebugEnabled())
364             {
365                 log.debug("<Tc characterSpacing=\"" + characterSpacing.floatValue() + "\" />");
366             }
367             graphicsState.getTextState().setCharacterSpacing( characterSpacing.floatValue() );
368         }
369         else if( operation.equals( "Td" ) )
370         {
371             COSNumber x = (COSNumber)arguments.get( 0 );
372             COSNumber y = (COSNumber)arguments.get( 1 );
373             if (log.isDebugEnabled())
374             {
375                 log.debug("<Td x=\"" + x.floatValue() + "\" y=\"" + y.floatValue() + "\">");
376             }
377             Matrix td = new Matrix();
378             td.setValue( 2, 0, x.floatValue() * textMatrix.getValue(0,0) );
379             td.setValue( 2, 1, y.floatValue() * textMatrix.getValue(1,1) );
380             //log.debug( "textLineMatrix before " + textLineMatrix );
381             textLineMatrix = textLineMatrix.multiply( td );
382             //log.debug( "textLineMatrix after " + textLineMatrix );
383             textMatrix = textLineMatrix.copy();
384         }
385         else if( operation.equals( "TD" ) )
386         {
387             //move text position and set leading
388             COSNumber x = (COSNumber)arguments.get( 0 );
389             COSNumber y = (COSNumber)arguments.get( 1 );
390             if (log.isDebugEnabled())
391             {
392                 log.debug("<TD x=\"" + x.floatValue() + "\" y=\"" + y.floatValue() + "\">");
393             }
394             graphicsState.getTextState().setLeading( -1 * y.floatValue() );
395             Matrix td = new Matrix();
396             td.setValue( 2, 0, x.floatValue() * textMatrix.getValue(0,0) );
397             td.setValue( 2, 1, y.floatValue() * textMatrix.getValue(1,1) );
398             //log.debug( "textLineMatrix before " + textLineMatrix );
399             textLineMatrix = textLineMatrix.multiply( td );
400             //log.debug( "textLineMatrix after " + textLineMatrix );
401             textMatrix = textLineMatrix.copy();
402         }
403         else if( operation.equals( "Tf" ) )
404         {
405             //set font and size
406             COSName fontName = (COSName)arguments.get( 0 );
407             graphicsState.getTextState().setFontSize( ((COSNumber)arguments.get( 1 ) ).floatValue() );
408
409             if (log.isDebugEnabled())
410             {
411                 log.debug("<Tf font=\"" + fontName.getName() + "\" size=\"" +
412                     graphicsState.getTextState().getFontSize() + "\">");
413             }
414
415             //old way
416             //graphicsState.getTextState().getFont() = (COSObject)stream.getDictionaryObject( fontName );
417             //if( graphicsState.getTextState().getFont() == null )
418             //{
419             // graphicsState.getTextState().getFont() = (COSObject)graphicsState.getTextState().getFont()
420             // Dictionary.getItem( fontName );
421             //}
422             graphicsState.getTextState().setFont( (PDFont)fonts.get( fontName.getName() ) );
423             if( graphicsState.getTextState().getFont() == null )
424             {
425                 throw new IOException( "Error: Could not find font(" + fontName + ") in map=" + fonts );
426             }
427             //log.debug( "Font Resource=" + fontResource );
428             //log.debug( "Current Font=" + graphicsState.getTextState().getFont() );
429             //log.debug( "graphicsState.getTextState().getFontSize()=" + graphicsState.getTextState().getFontSize() );
430         }
431         else if( operation.equals( "Tj" ) )
432         {
433             COSString string = (COSString)arguments.get( 0 );
434             TextPosition pos = showString( string.getBytes() );
435             if (log.isDebugEnabled())
436             {
437                 log.debug("<Tj string=\"" + string.getString() + "\">");
438             }
439         }
440         else if( operation.equals( "TJ" ) )
441         {
442             Matrix td = new Matrix();
443
444             COSArray array = (COSArray)arguments.get( 0 );
445             for( int i=0; i<array.size(); i++ )
446             {
447                 COSBase next = array.get( i );
448                 if( next instanceof COSNumber )
449                 {
450                     float value = -1*
451                                   (((COSNumber)next).floatValue()/1000) *
452                                   graphicsState.getTextState().getFontSize() *
453                                   textMatrix.getValue(1,1);
454
455                     if (log.isDebugEnabled())
456                     {
457                         log.debug( "<TJ(" + i + ") value=\"" + value +
458                                    "\", param=\"" + ((COSNumber)next).floatValue() +
459                                    "\", fontsize=\"" + graphicsState.getTextState().getFontSize() + "\">" );
460                     }
461                     td.setValue( 2, 0, value );
462                     textMatrix = textMatrix.multiply( td );
463                 }
464                 else if( next instanceof COSString )
465                 {
466                     TextPosition pos = showString( ((COSString)next).getBytes() );
467                     if (log.isDebugEnabled())
468                     {
469                         log.debug("<TJ(" + i + ") string=\"" + pos.getString() + "\">");
470                     }
471                 }
472                 else
473                 {
474                     throw new IOException( "Unknown type in array for TJ operation:" + next );
475                 }
476             }
477         }
478         else if( operation.equals( "TL" ) )
479         {
480             COSNumber leading = (COSNumber)arguments.get( 0 );
481             graphicsState.getTextState().setLeading( leading.floatValue() );
482             if (log.isDebugEnabled())
483             {
484                 log.debug("<TL leading=\"" + leading.floatValue() + "\" >");
485             }
486         }
487         else if( operation.equals( "Tm" ) )
488         {
489             //Set text matrix and text line matrix
490             COSNumber a = (COSNumber)arguments.get( 0 );
491             COSNumber b = (COSNumber)arguments.get( 1 );
492             COSNumber c = (COSNumber)arguments.get( 2 );
493             COSNumber d = (COSNumber)arguments.get( 3 );
494             COSNumber e = (COSNumber)arguments.get( 4 );
495             COSNumber f = (COSNumber)arguments.get( 5 );
496
497             if (log.isDebugEnabled())
498             {
499                 log.debug("<Tm " +
500                           "a=\"" + a.floatValue() + "\" " +
501                           "b=\"" + b.floatValue() + "\" " +
502                           "c=\"" + c.floatValue() + "\" " +
503                           "d=\"" + d.floatValue() + "\" " +
504                           "e=\"" + e.floatValue() + "\" " +
505                           "f=\"" + f.floatValue() + "\" >");
506             }
507
508             textMatrix = new Matrix();
509             textMatrix.setValue( 0, 0, a.floatValue() );
510             textMatrix.setValue( 0, 1, b.floatValue() );
511             textMatrix.setValue( 1, 0, c.floatValue() );
512             textMatrix.setValue( 1, 1, d.floatValue() );
513             textMatrix.setValue( 2, 0, e.floatValue() );
514             textMatrix.setValue( 2, 1, f.floatValue() );
515             textLineMatrix = textMatrix.copy();
516         }
517         else if( operation.equals( "Tr" ) )
518         {
519             //Set text rendering mode
520             //System.out.println( "<Tr>" );
521         }
522         else if( operation.equals( "Ts" ) )
523         {
524             //Set text rise
525             //System.out.println( "<Ts>" );
526         }
527         else if( operation.equals( "Tw" ) )
528         {
529             //set word spacing
530             COSNumber wordSpacing = (COSNumber)arguments.get( 0 );
531             if (log.isDebugEnabled())
532             {
533                 log.debug("<Tw wordSpacing=\"" + wordSpacing.floatValue() + "\" />");
534             }
535             graphicsState.getTextState().setWordSpacing( wordSpacing.floatValue() );
536         }
537         else if( operation.equals( "Tz" ) )
538         {
539             //Set horizontal text scaling
540         }
541         else if( operation.equals( "v" ) )
542         {
543             //Append curved segment to path (initial point replicated)
544         }
545         else if( operation.equals( "w" ) )
546         {
547             //Set the line width in the graphics state
548             //System.out.println( "<w>" );
549         }
550         else if( operation.equals( "W" ) )
551         {
552             //Set clipping path using nonzero winding number rule
553             //System.out.println( "<W>" );
554         }
555         else if( operation.equals( "W*" ) )
556         {
557             //Set clipping path using even-odd rule
558         }
559         else if( operation.equals( "y" ) )
560         {
561             //Append curved segment to path (final point replicated)
562         }
563         else if( operation.equals( "'" ) )
564         {
565             // Move to start of next text line, and show text
566             //
567             COSString string = (COSString)arguments.get( 0 );
568             if (log.isDebugEnabled())
569             {
570                 log.debug("<' string=\"" + string.getString() + "\">");
571             }
572
573             Matrix td = new Matrix();
574             td.setValue( 2, 1, -1 * graphicsState.getTextState().getLeading() * textMatrix.getValue(1,1));
575             textLineMatrix = textLineMatrix.multiply( td );
576             textMatrix = textLineMatrix.copy();
577
578             showString( string.getBytes() );
579         }
580         else if( operation.equals( "\"" ) )
581         {
582             //Set word and character spacing, move to next line, and show text
583             //
584             COSNumber wordSpacing = (COSNumber)arguments.get( 0 );
585             COSNumber characterSpacing = (COSNumber)arguments.get( 1 );
586             COSString string = (COSString)arguments.get( 2 );
587
588             if (log.isDebugEnabled())
589             {
590                 log.debug("<\" wordSpacing=\"" + wordSpacing +
591                           "\", characterSpacing=\"" + characterSpacing +
592                           "\", string=\"" + string.getString() + "\">");
593             }
594
595             graphicsState.getTextState().setCharacterSpacing( characterSpacing.floatValue() );
596             graphicsState.getTextState().setWordSpacing( wordSpacing.floatValue() );
597
598             Matrix td = new Matrix();
599             td.setValue( 2, 1, -1 * graphicsState.getTextState().getLeading() * textMatrix.getValue(1,1));
600             textLineMatrix = textLineMatrix.multiply( td );
601             textMatrix = textLineMatrix.copy();
602
603             showString( string.getBytes() );
604         }*/

605     }
606
607
608 }
Popular Tags