mirror of
				https://github.com/SimpleMobileTools/Simple-Draw.git
				synced 2025-06-05 21:59:17 +02:00 
			
		
		
		
	Consider the background color when saving as .svg
This commit is contained in:
		| @@ -1,5 +1,7 @@ | |||||||
| package com.simplemobiletools.draw; | package com.simplemobiletools.draw; | ||||||
|  |  | ||||||
|  | import android.graphics.drawable.ColorDrawable; | ||||||
|  |  | ||||||
| import com.simplemobiletools.draw.actions.Action; | import com.simplemobiletools.draw.actions.Action; | ||||||
|  |  | ||||||
| import java.io.BufferedWriter; | import java.io.BufferedWriter; | ||||||
| @@ -13,14 +15,18 @@ import java.util.Map; | |||||||
| public class Svg { | public class Svg { | ||||||
|  |  | ||||||
|     public static void saveSvg(File output, MyCanvas canvas) |     public static void saveSvg(File output, MyCanvas canvas) | ||||||
|             throws IOException { |             throws Exception { | ||||||
|  |         // This might throw ClassCastException | ||||||
|  |         int backgroundColor = ((ColorDrawable) canvas.getBackground()).getColor(); | ||||||
|  |  | ||||||
|  |         // This might throw IOException | ||||||
|         FileOutputStream out = new FileOutputStream(output); |         FileOutputStream out = new FileOutputStream(output); | ||||||
|         BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out)); |         BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out)); | ||||||
|         writeSvg(writer, canvas.getPaths(), canvas.getWidth(), canvas.getHeight()); |         writeSvg(writer, backgroundColor, canvas.getPaths(), canvas.getWidth(), canvas.getHeight()); | ||||||
|         writer.close(); |         writer.close(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private static void writeSvg(Writer writer, |     private static void writeSvg(Writer writer, int backgroundColor, | ||||||
|                                  Map<MyPath, PaintOptions> paths, int width, int height) |                                  Map<MyPath, PaintOptions> paths, int width, int height) | ||||||
|             throws IOException { |             throws IOException { | ||||||
|         writer.write("<svg width=\""); |         writer.write("<svg width=\""); | ||||||
| @@ -28,6 +34,17 @@ public class Svg { | |||||||
|         writer.write("\" height=\""); |         writer.write("\" height=\""); | ||||||
|         writer.write(String.valueOf(height)); |         writer.write(String.valueOf(height)); | ||||||
|         writer.write("\" xmlns=\"http://www.w3.org/2000/svg\">"); |         writer.write("\" xmlns=\"http://www.w3.org/2000/svg\">"); | ||||||
|  |  | ||||||
|  |         // Background color (use a rect) | ||||||
|  |         writer.write("<rect width=\""); | ||||||
|  |         writer.write(String.valueOf(width)); | ||||||
|  |         writer.write("\" height=\""); | ||||||
|  |         writer.write(String.valueOf(height)); | ||||||
|  |         writer.write("\" fill=\"#"); | ||||||
|  |         writer.write(Integer.toHexString(backgroundColor).substring(2)); // Skip the alpha FF | ||||||
|  |         writer.write("\"/>"); | ||||||
|  |  | ||||||
|  |         // Write the paths | ||||||
|         for (Map.Entry<MyPath, PaintOptions> entry : paths.entrySet()) { |         for (Map.Entry<MyPath, PaintOptions> entry : paths.entrySet()) { | ||||||
|             writePath(writer, entry.getKey(), entry.getValue()); |             writePath(writer, entry.getKey(), entry.getValue()); | ||||||
|         } |         } | ||||||
|   | |||||||
| @@ -234,7 +234,7 @@ public class MainActivity extends SimpleActivity implements MyCanvas.PathsChange | |||||||
|             case ".svg": |             case ".svg": | ||||||
|                 try { |                 try { | ||||||
|                     Svg.saveSvg(file, mMyCanvas); |                     Svg.saveSvg(file, mMyCanvas); | ||||||
|                 } catch (IOException e) { |                 } catch (Exception e) { | ||||||
|                     Log.e(TAG, "MainActivity SaveFile (.svg) " + e.getMessage()); |                     Log.e(TAG, "MainActivity SaveFile (.svg) " + e.getMessage()); | ||||||
|                     return false; |                     return false; | ||||||
|                 } |                 } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user