Skip to content
Permalink
Browse files

Merge pull request #2 from datablend/master

Save wordstorms as SVG
  • Loading branch information...
quimcastella committed May 22, 2014
2 parents 6d0a66c + 2826972 commit 8bace9f5d584e3ce0531916a335fe988226f48b1
@@ -0,0 +1,4 @@
*.iml
.idea/
WordStorm.iml
bin
@@ -5,7 +5,7 @@
*/
public class DataPath {
public static String dataPath() {
String path = "/Users/csutton/hg/projects/storm-www-demo/cluster-docs/d2";
String path = "examples/data";
return path;
}
}
@@ -70,7 +70,13 @@ public String getHTMLOutput(int maxWords){
public String getStormHTML(StormConf conf){
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){
return DataPath.dataPath()+"/output/"+folder+"/"+conf+".txt";
}
@@ -65,6 +65,10 @@ public String getHTMLOutput(int maxWords){
public String getStormHTML(StormConf conf){
return DataPath.dataPath()+"/output/"+folder+"/"+ conf+".html";
}
@Override
public String getStormSVG(StormConf conf){
return DataPath.dataPath()+"/output/"+folder+"/"+ conf+"-svg.html";
}
@Override
public String getStormLog(StormConf conf){
return DataPath.dataPath()+"/output/"+folder+"/"+conf+".txt";
@@ -15,8 +15,10 @@
public abstract String getOutputMovie( StormConf conf, int cloudIndex, int frameNum );
public abstract String getLocalOutput( StormConf conf, int cloudIndex );
public abstract String getHTMLFolder();

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

public abstract void loadText(PApplet parent,WordCram w, int index);
@@ -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());
}
}
}
@@ -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();
}
}
@@ -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);
}
}
@@ -355,4 +355,8 @@ public void printVocabByIdf(){
System.out.println(++i+" "+w.word+" "+index.get(w.word).getIdf());
}
}

public WordCram[] getClouds() {
return clouds;
}
}
@@ -1,5 +1,9 @@
package wordstorm;

import wordcram.WordStorm;

public interface Algorithm {
public void init();
public void initProcess();
public WordStorm getWordStorm();
}
@@ -37,5 +37,6 @@ else if(conf.coorPlacer == 3)
f.init();
f.initProcess();
HTMLSaver.singleStorm(load, conf, apConf);
SVGSaver.singleStorm(load, conf, apConf, f);
}
}
@@ -12,7 +12,7 @@
public class StormLauncher {

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

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

0 comments on commit 8bace9f

Please sign in to comment.
You can’t perform that action at this time.