1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.pdfbox.pdmodel.graphics; 18 19 import org.apache.pdfbox.cos.COSArray; 20 import org.apache.pdfbox.cos.COSBase; 21 import org.apache.pdfbox.cos.COSInteger; 22 import org.apache.pdfbox.cos.COSNumber; 23 24 import org.apache.pdfbox.pdmodel.common.COSArrayList; 25 import org.apache.pdfbox.pdmodel.common.COSObjectable; 26 27 import java.util.List; 28 29 /** 30 * This class represents the line dash pattern for a graphics state. See PDF 31 * Reference 1.5 section 4.3.2 32 * 33 * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a> 34 * @version $Revision: 1.7 $ 35 */ 36 public class PDLineDashPattern implements COSObjectable, Cloneable 37 { 38 private COSArray lineDashPattern = null; 39 40 /** 41 * Creates a blank line dash pattern. With no dashes and a phase of 0. 42 */ 43 public PDLineDashPattern() 44 { 45 lineDashPattern = new COSArray(); 46 lineDashPattern.add( new COSArray() ); 47 lineDashPattern.add( COSInteger.ZERO ); 48 } 49 50 /** 51 * Constructs a line dash pattern from an existing array. 52 * 53 * @param ldp The existing line dash pattern. 54 */ 55 public PDLineDashPattern( COSArray ldp ) 56 { 57 lineDashPattern = ldp; 58 } 59 60 /** 61 * Constructs a line dash pattern from an existing array. 62 * 63 * @param ldp The existing line dash pattern. 64 * @param phase The phase for the line dash pattern. 65 */ 66 public PDLineDashPattern( COSArray ldp, int phase ) 67 { 68 lineDashPattern = new COSArray(); 69 lineDashPattern.add( ldp ); 70 lineDashPattern.add( COSInteger.get( phase ) ); 71 } 72 73 /** 74 * {@inheritDoc} 75 */ 76 public Object clone() 77 { 78 PDLineDashPattern pattern = null; 79 try 80 { 81 pattern = (PDLineDashPattern)super.clone(); 82 pattern.setDashPattern(getDashPattern()); 83 pattern.setPhaseStart(getPhaseStart()); 84 } 85 catch(CloneNotSupportedException exception) 86 { 87 exception.printStackTrace(); 88 } 89 return pattern; 90 } 91 92 /** 93 * {@inheritDoc} 94 */ 95 public COSBase getCOSObject() 96 { 97 return lineDashPattern; 98 } 99 100 /** 101 * This will get the line dash pattern phase. The dash phase specifies the 102 * distance into the dash pattern at which to start the dash. 103 * 104 * @return The line dash pattern phase. 105 */ 106 public int getPhaseStart() 107 { 108 COSNumber phase = (COSNumber)lineDashPattern.get( 1 ); 109 return phase.intValue(); 110 } 111 112 /** 113 * This will set the line dash pattern phase. 114 * 115 * @param phase The new line dash patter phase. 116 */ 117 public void setPhaseStart( int phase ) 118 { 119 lineDashPattern.set( 1, phase ); 120 } 121 122 /** 123 * This will return a list of java.lang.Integer objects that represent the line 124 * dash pattern appearance. 125 * 126 * @return The line dash pattern. 127 */ 128 public List getDashPattern() 129 { 130 COSArray dashPatterns = (COSArray)lineDashPattern.get( 0 ); 131 return COSArrayList.convertIntegerCOSArrayToList( dashPatterns ); 132 } 133 134 /** 135 * Get the line dash pattern as a COS object. 136 * 137 * @return The cos array line dash pattern. 138 */ 139 public COSArray getCOSDashPattern() 140 { 141 return (COSArray)lineDashPattern.get( 0 ); 142 } 143 144 /** 145 * This will replace the existing line dash pattern. 146 * 147 * @param dashPattern A list of java.lang.Integer objects. 148 */ 149 public void setDashPattern( List dashPattern ) 150 { 151 lineDashPattern.set( 0, COSArrayList.converterToCOSArray( dashPattern ) ); 152 } 153 154 /** 155 * Checks if the dashPattern is empty or all values equals 0. 156 * 157 * @return true if the dashPattern is empty or all values equals 0 158 */ 159 public boolean isDashPatternEmpty() 160 { 161 float[] dashPattern = getCOSDashPattern().toFloatArray(); 162 boolean dashPatternEmpty = true; 163 if (dashPattern != null) 164 { 165 int arraySize = dashPattern.length; 166 for(int i=0;i<arraySize;i++) 167 { 168 if (dashPattern[i] > 0) 169 { 170 dashPatternEmpty = false; 171 break; 172 } 173 } 174 } 175 return dashPatternEmpty; 176 } 177 178 }