Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from datablend/master
Save wordstorms as SVG
- Loading branch information
Showing
12 changed files
with
185 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,4 @@ | |||
*.iml | |||
.idea/ | |||
WordStorm.iml | |||
bin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,45 @@ | |||
package io; | |||
|
|||
import io.svg.SVGGenerator; | |||
import wordstorm.Algorithm; | |||
import wordstorm.AppletConf; | |||
import wordstorm.StormConf; | |||
|
|||
import java.io.*; | |||
|
|||
/** | |||
* Created by stijnbe on 22/05/14. | |||
*/ | |||
public class SVGSaver { | |||
public static void singleStorm(Loader load, StormConf conf, AppletConf apConf, Algorithm algorithm) { | |||
SVGGenerator svgGenerator = new SVGGenerator(apConf.getWidth(), apConf.getHeight()); | |||
String svg = svgGenerator.generateSvg(algorithm.getWordStorm()); | |||
try{ | |||
File f = new File(load.getHTMLFolder()); | |||
f.mkdirs(); | |||
FileOutputStream fstream = new FileOutputStream(load.getStormSVG(conf)); | |||
DataOutputStream out = new DataOutputStream(fstream); | |||
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out)); | |||
bw.write("<!DOCTYPE html>\n<html>\n<body>\n"); | |||
bw.write("<h3>\n"); | |||
bw.write("Word Storm"); | |||
bw.write("</h3>"); | |||
bw.write(svg); | |||
bw.write("<p>"); | |||
bw.write("folder: "+load.getFolder()); | |||
bw.write("</p>"); | |||
bw.write("<p>"); | |||
bw.write("Clouds: "+load.numClouds); | |||
bw.write("</p>"); | |||
bw.write("<p>"); | |||
bw.write("parameters: "+conf); | |||
bw.write("</p>"); | |||
bw.write("</body>\n</html>"); | |||
bw.close(); | |||
out.close(); | |||
fstream.close(); | |||
}catch (Exception e){ | |||
System.err.println("Error: output" + e.getMessage()); | |||
} | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,36 @@ | |||
package io.svg; | |||
|
|||
import wordcram.Word; | |||
import wordcram.WordCram; | |||
import wordcram.WordStorm; | |||
|
|||
/** | |||
* Created by stijnbe on 02/05/14. | |||
*/ | |||
public class SVGGenerator { | |||
private int width; | |||
private int height; | |||
|
|||
public SVGGenerator(int width, int height) { | |||
this.width = width; | |||
this.height = height; | |||
} | |||
|
|||
public String generateSvg(WordStorm wordStorm) { | |||
String svg = ""; | |||
WordCram[] wordCrams = wordStorm.getClouds(); | |||
for (WordCram wordCram : wordCrams) { | |||
svg += "<svg height=\"" + height + "px\" width=\"" + width + "px\" viewBox=\"0 0 " + width + " " + height + " xmlns=\"http://www.w3.org/2000/svg\"> \n"; | |||
Word[] words = wordCram.getWords(); | |||
for (Word word : words) { | |||
svg += renderSvgWordElement(word) + "\n"; | |||
} | |||
svg += "</svg>"; | |||
} | |||
return svg; | |||
} | |||
|
|||
private String renderSvgWordElement(Word word) { | |||
return new SVGTextElement(word).toString(); | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,75 @@ | |||
package io.svg; | |||
|
|||
import processing.core.PConstants; | |||
import wordcram.Word; | |||
|
|||
import java.awt.*; | |||
|
|||
/** | |||
* Created by stijnbe on 02/05/14. | |||
*/ | |||
public class SVGTextElement { | |||
/* | |||
<text | |||
text-anchor="middle" | |||
transform="translate(-18,55)rotate(60)" | |||
style="font-size: 99px; opacity: 1; | |||
font-family: Impact; fill: rgb(57, 59, 121);"> | |||
Cat | |||
</text> | |||
*/ | |||
|
|||
private Float x; | |||
private Float y; | |||
private Float size; | |||
private String text; | |||
private String svgClass; | |||
private String color; | |||
private Integer rotate; | |||
|
|||
public SVGTextElement(Word word){ | |||
this.x = word.getRenderedPlace().x; | |||
this.y = word.getRenderedPlace().y; | |||
this.size = word.getRenderedSize(); | |||
this.text = word.word; | |||
this.svgClass = "word-" + text.replaceAll("[^a-zA-Z0-9]", "_"); | |||
|
|||
this.color = getColor(new Color(word.getRenderedColor(), true)); | |||
|
|||
float angle = word.getRenderedAngle(); | |||
|
|||
this.rotate = 0; | |||
if(angle == PConstants.HALF_PI){ | |||
this.rotate = 90; | |||
} else if(angle == -PConstants.HALF_PI){ | |||
this.rotate = -90; | |||
this.y = this.y + (float)word.getBounds().getHeight(); | |||
this.x = this.x + (float)word.getBounds().getWidth(); | |||
} else{ | |||
this.y = this.y + (float)word.getBounds().getHeight(); | |||
} | |||
} | |||
|
|||
public String toString(){ | |||
return "<text " + | |||
"transform=\"translate(" + x +"," + y + ")rotate(" + rotate + ")\" " + | |||
"style=\"font-family: ChunkFive; " + | |||
"font-size: " + size +"; " + | |||
"fill: " + color + ";\" " + | |||
"class=\"" + svgClass + "\"" + | |||
">" + | |||
text + | |||
"</text>"; | |||
} | |||
|
|||
private String getColor(Color color) { | |||
// rgb(30, 200, 90) | |||
// #ff0023 | |||
return "#" + decToHex(color.getRed()) + decToHex(color.getGreen()) + decToHex(color.getBlue()); | |||
} | |||
|
|||
private String decToHex(int dec) { | |||
return dec > 16 ? Integer.toHexString(dec) : | |||
"0" + Integer.toHexString(dec); | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,5 +1,9 @@ | |||
package wordstorm; | package wordstorm; | ||
|
|||
import wordcram.WordStorm; | |||
|
|||
public interface Algorithm { | public interface Algorithm { | ||
public void init(); | public void init(); | ||
public void initProcess(); | public void initProcess(); | ||
public WordStorm getWordStorm(); | |||
} | } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters