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.documentinterchange.prepress; 18 19 import org.apache.pdfbox.cos.COSArray; 20 import org.apache.pdfbox.cos.COSBase; 21 import org.apache.pdfbox.cos.COSDictionary; 22 import org.apache.pdfbox.cos.COSInteger; 23 import org.apache.pdfbox.pdmodel.common.COSObjectable; 24 import org.apache.pdfbox.pdmodel.graphics.PDLineDashPattern; 25 import org.apache.pdfbox.pdmodel.graphics.color.PDColorState; 26 import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB; 27 28 /** 29 * The Box Style specifies visual characteristics for displaying box areas. 30 * 31 * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a> 32 * @version $Revision: 1.3 $ 33 */ 34 public class PDBoxStyle implements COSObjectable 35 { 36 /** 37 * Style for guideline. 38 */ 39 public static final String GUIDELINE_STYLE_SOLID = "S"; 40 /** 41 * Style for guideline. 42 */ 43 public static final String GUIDELINE_STYLE_DASHED = "D"; 44 45 private COSDictionary dictionary; 46 47 /** 48 * Default Constructor. 49 * 50 */ 51 public PDBoxStyle() 52 { 53 dictionary = new COSDictionary(); 54 } 55 56 /** 57 * Constructor for an existing BoxStyle element. 58 * 59 * @param dic The existing dictionary. 60 */ 61 public PDBoxStyle( COSDictionary dic ) 62 { 63 dictionary = dic; 64 } 65 66 /** 67 * Convert this standard java object to a COS object. 68 * 69 * @return The cos object that matches this Java object. 70 */ 71 public COSBase getCOSObject() 72 { 73 return dictionary; 74 } 75 76 /** 77 * Convert this standard java object to a COS object. 78 * 79 * @return The cos object that matches this Java object. 80 */ 81 public COSDictionary getDictionary() 82 { 83 return dictionary; 84 } 85 86 /** 87 * Get the color to be used for the guidelines. This is guaranteed to 88 * not return null. The color space will always be DeviceRGB and the 89 * default color is [0,0,0]. 90 * 91 *@return The guideline color. 92 */ 93 public PDColorState getGuidelineColor() 94 { 95 COSArray colorValues = (COSArray)dictionary.getDictionaryObject( "C" ); 96 if( colorValues == null ) 97 { 98 colorValues = new COSArray(); 99 colorValues.add( COSInteger.ZERO ); 100 colorValues.add( COSInteger.ZERO ); 101 colorValues.add( COSInteger.ZERO ); 102 dictionary.setItem( "C", colorValues ); 103 } 104 PDColorState instance = new PDColorState( colorValues ); 105 instance.setColorSpace( PDDeviceRGB.INSTANCE ); 106 return instance; 107 } 108 109 /** 110 * Set the color space instance for this box style. This must be a 111 * PDDeviceRGB! 112 * 113 * @param color The new colorspace value. 114 */ 115 public void setGuideLineColor( PDColorState color ) 116 { 117 COSArray values = null; 118 if( color != null ) 119 { 120 values = color.getCOSColorSpaceValue(); 121 } 122 dictionary.setItem( "C", values ); 123 } 124 125 /** 126 * Get the width of the of the guideline in default user space units. 127 * The default is 1. 128 * 129 * @return The width of the guideline. 130 */ 131 public float getGuidelineWidth() 132 { 133 return dictionary.getFloat( "W", 1 ); 134 } 135 136 /** 137 * Set the guideline width. 138 * 139 * @param width The width in default user space units. 140 */ 141 public void setGuidelineWidth( float width ) 142 { 143 dictionary.setFloat( "W", width ); 144 } 145 146 /** 147 * Get the style for the guideline. The default is "S" for solid. 148 * 149 * @return The guideline style. 150 * @see PDBoxStyle#GUIDELINE_STYLE_DASHED 151 * @see PDBoxStyle#GUIDELINE_STYLE_SOLID 152 */ 153 public String getGuidelineStyle() 154 { 155 return dictionary.getNameAsString( "S", GUIDELINE_STYLE_SOLID ); 156 } 157 158 /** 159 * Set the style for the box. 160 * 161 * @param style The style for the box line. 162 * @see PDBoxStyle#GUIDELINE_STYLE_DASHED 163 * @see PDBoxStyle#GUIDELINE_STYLE_SOLID 164 */ 165 public void setGuidelineStyle( String style ) 166 { 167 dictionary.setName( "S", style ); 168 } 169 170 /** 171 * Get the line dash pattern for this box style. This is guaranteed to not 172 * return null. The default is [3],0. 173 * 174 * @return The line dash pattern. 175 */ 176 public PDLineDashPattern getLineDashPattern() 177 { 178 PDLineDashPattern pattern = null; 179 COSArray d = (COSArray)dictionary.getDictionaryObject( "D" ); 180 if( d == null ) 181 { 182 d = new COSArray(); 183 d.add( COSInteger.THREE ); 184 dictionary.setItem( "D", d ); 185 } 186 COSArray lineArray = new COSArray(); 187 lineArray.add( d ); 188 //dash phase is not specified and assumed to be zero. 189 lineArray.add( COSInteger.ZERO ); 190 pattern = new PDLineDashPattern( lineArray ); 191 return pattern; 192 } 193 194 /** 195 * Set the line dash pattern associated with this box style. 196 * 197 * @param pattern The patter for this box style. 198 */ 199 public void setLineDashPattern( PDLineDashPattern pattern ) 200 { 201 COSArray array = null; 202 if( pattern != null ) 203 { 204 array = pattern.getCOSDashPattern(); 205 } 206 dictionary.setItem( "D", array ); 207 } 208 }