Skip to content

Commit

Permalink
Merge pull request #2 from datablend/master
Browse files Browse the repository at this point in the history
Save wordstorms as SVG
  • Loading branch information
quimcastella committed May 22, 2014
2 parents 6d0a66c + 2826972 commit 8bace9f
Show file tree
Hide file tree
Showing 12 changed files with 185 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
*.iml
.idea/
WordStorm.iml
bin
2 changes: 1 addition & 1 deletion src/io/DataPath.java
Expand Up @@ -5,7 +5,7 @@
*/ */
public class DataPath { public class DataPath {
public static String dataPath() { public static String dataPath() {
String path = "/Users/csutton/hg/projects/storm-www-demo/cluster-docs/d2"; String path = "examples/data";
return path; return path;
} }
} }
8 changes: 7 additions & 1 deletion src/io/FileLoader.java
Expand Up @@ -70,7 +70,13 @@ public String getHTMLOutput(int maxWords){
public String getStormHTML(StormConf conf){ public String getStormHTML(StormConf conf){
return DataPath.dataPath()+"/output/"+folder+"/"+ conf+".html"; return DataPath.dataPath()+"/output/"+folder+"/"+ conf+".html";
} }
@Override
@Override
public String getStormSVG(StormConf conf){
return DataPath.dataPath()+"/output/"+folder+"/"+ conf+"-svg.html";
}

@Override
public String getStormLog(StormConf conf){ public String getStormLog(StormConf conf){
return DataPath.dataPath()+"/output/"+folder+"/"+conf+".txt"; return DataPath.dataPath()+"/output/"+folder+"/"+conf+".txt";
} }
Expand Down
4 changes: 4 additions & 0 deletions src/io/FileLoaderWeight.java
Expand Up @@ -65,6 +65,10 @@ public String getHTMLOutput(int maxWords){
public String getStormHTML(StormConf conf){ public String getStormHTML(StormConf conf){
return DataPath.dataPath()+"/output/"+folder+"/"+ conf+".html"; return DataPath.dataPath()+"/output/"+folder+"/"+ conf+".html";
} }
@Override
public String getStormSVG(StormConf conf){
return DataPath.dataPath()+"/output/"+folder+"/"+ conf+"-svg.html";
}
@Override @Override
public String getStormLog(StormConf conf){ public String getStormLog(StormConf conf){
return DataPath.dataPath()+"/output/"+folder+"/"+conf+".txt"; return DataPath.dataPath()+"/output/"+folder+"/"+conf+".txt";
Expand Down
2 changes: 2 additions & 0 deletions src/io/Loader.java
Expand Up @@ -15,8 +15,10 @@ public abstract class Loader {
public abstract String getOutputMovie( StormConf conf, int cloudIndex, int frameNum ); public abstract String getOutputMovie( StormConf conf, int cloudIndex, int frameNum );
public abstract String getLocalOutput( StormConf conf, int cloudIndex ); public abstract String getLocalOutput( StormConf conf, int cloudIndex );
public abstract String getHTMLFolder(); public abstract String getHTMLFolder();

public abstract String getHTMLOutput(int maxWords); public abstract String getHTMLOutput(int maxWords);
public abstract String getStormHTML(StormConf conf); public abstract String getStormHTML(StormConf conf);
public abstract String getStormSVG(StormConf conf);
public abstract String getStormLog(StormConf conf); public abstract String getStormLog(StormConf conf);


public abstract void loadText(PApplet parent,WordCram w, int index); public abstract void loadText(PApplet parent,WordCram w, int index);
Expand Down
45 changes: 45 additions & 0 deletions src/io/SVGSaver.java
@@ -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());
}
}
}
36 changes: 36 additions & 0 deletions src/io/svg/SVGGenerator.java
@@ -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();
}
}
75 changes: 75 additions & 0 deletions src/io/svg/SVGTextElement.java
@@ -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);
}
}
4 changes: 4 additions & 0 deletions src/wordcram/WordStorm.java
Expand Up @@ -355,4 +355,8 @@ public void printVocabByIdf(){
System.out.println(++i+" "+w.word+" "+index.get(w.word).getIdf()); System.out.println(++i+" "+w.word+" "+index.get(w.word).getIdf());
} }
} }

public WordCram[] getClouds() {
return clouds;
}
} }
4 changes: 4 additions & 0 deletions src/wordstorm/Algorithm.java
@@ -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();
} }
1 change: 1 addition & 0 deletions src/wslauncher/IndepLauncher.java
Expand Up @@ -37,5 +37,6 @@ else if(conf.coorPlacer == 3)
f.init(); f.init();
f.initProcess(); f.initProcess();
HTMLSaver.singleStorm(load, conf, apConf); HTMLSaver.singleStorm(load, conf, apConf);
SVGSaver.singleStorm(load, conf, apConf, f);
} }
} }
3 changes: 2 additions & 1 deletion src/wslauncher/StormLauncher.java
Expand Up @@ -12,7 +12,7 @@
public class StormLauncher { public class StormLauncher {


public static void main(String[] args) { public static void main(String[] args) {
String folder = ""; String folder = "PresidentialDebate";
AppletConf apConf = new AppletConf(640,480,255); //cloud width, height and bk color AppletConf apConf = new AppletConf(640,480,255); //cloud width, height and bk color


StormConf conf = new StormConf(); //Storm Parameters StormConf conf = new StormConf(); //Storm Parameters
Expand All @@ -38,5 +38,6 @@ else if(conf.coorPlacer == 3)
f.init(); f.init();
f.initProcess(); f.initProcess();
HTMLSaver.singleStorm(load, conf, apConf); HTMLSaver.singleStorm(load, conf, apConf);
SVGSaver.singleStorm(load, conf, apConf, f);
} }
} }

0 comments on commit 8bace9f

Please sign in to comment.