• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.eclipse.org/org/documents/epl-v10.php
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.ide.eclipse.adt.internal.assetstudio;
17 
18 import static com.android.ide.eclipse.adt.internal.wizards.templates.NewProjectWizard.DEFAULT_LAUNCHER_ICON;
19 
20 import com.android.annotations.NonNull;
21 import com.android.assetstudiolib.GraphicGenerator;
22 import com.android.assetstudiolib.GraphicGenerator.Shape;
23 import com.android.assetstudiolib.GraphicGeneratorContext;
24 import com.android.ide.eclipse.adt.AdtPlugin;
25 import com.android.ide.eclipse.adt.internal.wizards.templates.TemplateManager;
26 
27 import org.eclipse.core.resources.IProject;
28 import org.eclipse.swt.graphics.RGB;
29 
30 import java.awt.Font;
31 import java.awt.GraphicsEnvironment;
32 import java.awt.image.BufferedImage;
33 import java.io.File;
34 import java.io.IOException;
35 import java.util.HashMap;
36 import java.util.Map;
37 
38 import javax.imageio.ImageIO;
39 
40 /**
41  * Value object for the AssetStudio wizard. These values are both set by the
42  * wizard as well as read by the wizard initially, so passing in a configured
43  * {@link CreateAssetSetWizardState} to the icon generator is possible.
44  */
45 public class CreateAssetSetWizardState implements GraphicGeneratorContext {
46     /**
47      * The type of asset being created. This field is static such that when you
48      * bring up the wizard repeatedly (for example to create multiple
49      * notification icons) you don't have to keep selecting the same type over
50      * and over.
51      */
52     public static AssetType sLastType = AssetType.LAUNCHER;
53 
54     /** The type of asset to be created */
55     public AssetType type = sLastType;
56 
57     /** The base name to use for the created icons */
58     public String outputName;
59 
60     /** The minimum SDK being targeted */
61     public int minSdk = -1;
62 
63     /** The project to create the icons into */
64     public IProject project;
65 
66     /** Whether empty space around the source image should be trimmed */
67     public boolean trim = true;
68 
69     /** The type of source the icon is being created from */
70     public SourceType sourceType = SourceType.TEXT;
71 
72     /** If {@link #sourceType} is a {@link SourceType#CLIPART}, the name of the clipart image */
73     public String clipartName;
74 
75     /** If {@link #sourceType} is a {@link SourceType#IMAGE}, the path to the input image */
76     public File imagePath;
77 
78     /** If {@link #sourceType} is a {@link SourceType#TEXT}, the text to render */
79     public String text = "aA";
80 
81     /** The amount of padding to add around the source image */
82     public int padding = 15;
83 
84     /** The background shape */
85     public Shape shape = Shape.SQUARE;
86 
87     /** Whether the image should be cropped */
88     public boolean crop;
89 
90     /** Whether to use Holo Dark for action bar icons */
91     public boolean holoDark;
92 
93     /** The background color to use for the shape (unless the shape is {@link Shape#NONE} */
94     public RGB background = new RGB(0xff, 0x00, 0x00);
95 
96     /** The background color to use for the text or clipart (unless shape is {@link Shape#NONE} */
97     public RGB foreground = new RGB(0x00, 0x00, 0x00);
98 
99     /** If {@link #sourceType} is a {@link SourceType#TEXT}, the font of the text to render */
100     private Font mTextFont;
101 
102     private Map<String, BufferedImage> mImageCache = null;
103 
104     /**
105      * Gets the text font to be used for text rendering if the
106      * {@link #sourceType} is a {@link SourceType#TEXT}
107      *
108      * @return the text font
109      */
110     @NonNull
getTextFont()111     public Font getTextFont() {
112         if (mTextFont == null) {
113             GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
114             String[] fontNames = env.getAvailableFontFamilyNames();
115             for (String familyName : fontNames) {
116                 if (familyName.equals("Helvetica")) {
117                     mTextFont = new java.awt.Font(familyName, java.awt.Font.BOLD, 512);
118                     break;
119                 }
120             }
121             if (mTextFont == null) {
122                 for (String familyName : fontNames) {
123                     if (familyName.equals("Arial")) {
124                         mTextFont = new java.awt.Font(familyName, java.awt.Font.BOLD, 512);
125                         break;
126                     }
127                 }
128 
129                 if (mTextFont == null) {
130                     mTextFont = new java.awt.Font("SansSerif", java.awt.Font.BOLD, 512);
131                 }
132             }
133         }
134 
135         return mTextFont;
136     }
137 
138     /**
139      * Sets the text font to be used for text rendering if the
140      * {@link #sourceType} is a {@link SourceType#TEXT}
141      *
142      * @param textFont the font to use
143      */
setTextFont(@onNull Font textFont)144     public void setTextFont(@NonNull Font textFont) {
145         mTextFont = textFont;
146     }
147 
148     /** Types of sources that the asset studio can use to generate icons from */
149     public enum SourceType {
150         /** Generate the icon using the image pointed to by {@link #imagePath} */
151         IMAGE,
152 
153         /** Generate the icon using the clipart named by {@link #clipartName} */
154         CLIPART,
155 
156         /** Generate the icon using the text in {@link #text} */
157         TEXT
158     }
159 
160     // ---- Implements GraphicGeneratorContext ----
161 
162     @Override
loadImageResource(String relativeName)163     public BufferedImage loadImageResource(String relativeName) {
164         try {
165             return getCachedImage(relativeName, true);
166         } catch (IOException e) {
167             AdtPlugin.log(e, null);
168             return null;
169         }
170     }
171 
getCachedImage(String path, boolean isPluginRelative)172     BufferedImage getCachedImage(String path, boolean isPluginRelative)
173             throws IOException {
174         BufferedImage image = mImageCache != null ? mImageCache.get(path) : null;
175         if (image == null) {
176             image = getImage(path, isPluginRelative);
177             if (mImageCache == null) {
178                 mImageCache = new HashMap<String, BufferedImage>();
179             }
180             mImageCache.put(path, image);
181         }
182 
183         return image;
184     }
185 
186     @NonNull
getImage(@onNull String path, boolean isPluginRelative)187     static BufferedImage getImage(@NonNull String path, boolean isPluginRelative)
188             throws IOException {
189         BufferedImage image = null;
190         if (isPluginRelative) {
191             image = GraphicGenerator.getStencilImage(path);
192         } else {
193             if (path.equals(DEFAULT_LAUNCHER_ICON)) {
194                 File file = TemplateManager.getTemplateLocation(
195                   "projects/NewAndroidApplication/root/res/drawable-xhdpi/ic_launcher.png"); //$NON-NLS-1$
196                 if (file != null) {
197                     path = file.getPath();
198                 } else {
199                     image = GraphicGenerator.getStencilImage("user.png");
200                 }
201             }
202 
203             File file = new File(path);
204 
205             // Requires Batik
206             //if (file.getName().endsWith(DOT_SVG)) {
207             //    image = loadSvgImage(file);
208             //}
209 
210             if (image == null) {
211                 image = ImageIO.read(file);
212             }
213         }
214 
215         if (image == null) {
216             image = new BufferedImage(1,1, BufferedImage.TYPE_INT_ARGB);
217         }
218 
219         return image;
220     }
221 
222     // This requires Batik for SVG rendering
223     //
224     //public static BufferedImage loadSvgImage(File file) {
225     //    BufferedImageTranscoder transcoder = new BufferedImageTranscoder();
226     //
227     //    String svgURI = file.toURI().toString();
228     //    TranscoderInput input = new TranscoderInput(svgURI);
229     //
230     //    try {
231     //        transcoder.transcode(input, null);
232     //    } catch (TranscoderException e) {
233     //        e.printStackTrace();
234     //        return null;
235     //    }
236     //
237     //    return transcoder.decodedImage;
238     //}
239     //
240     ///**
241     // * A dummy implementation of an {@link ImageTranscoder} that simply stores the {@link
242     // * BufferedImage} generated by the SVG library.
243     // */
244     //private static class BufferedImageTranscoder extends ImageTranscoder {
245     //    public BufferedImage decodedImage;
246     //
247     //    @Override
248     //    public BufferedImage createImage(int w, int h) {
249     //        return new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
250     //    }
251     //
252     //    @Override
253     //    public void writeImage(BufferedImage image, TranscoderOutput output)
254     //            throws TranscoderException {
255     //        this.decodedImage = image;
256     //    }
257     //}
258 }
259