Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Steps towards compatibility with Android #78

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import com.beust.jcommander.ParameterException;
import com.kennycason.kumo.CollisionMode;
import com.kennycason.kumo.PolarBlendMode;
import com.kennycason.kumo.font.FontWeight;
import com.kennycason.kumo.draw.Color;
import com.kennycason.kumo.draw.FontFace;

import java.awt.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -59,7 +59,7 @@ public class CliParameters {
private List<String> backgrounds = new ArrayList<>();

@Parameter(names = "--background-color", description = "Background color. Default is Black.", converter = ColorConverter.class)
private Color backgroundColor = Color.BLACK;
private Color backgroundColor = new Color(0, 0, 0);

@Parameter(names = { "--color", "-c" }, description = "A comma separated list of colors to use for the word cloud text. Values most be provided in one of the below formats. Refer to CLI.md for usage examples.")
// perform actual parsing in the getter, the commas in our color format cause issues with jCommander
Expand All @@ -77,8 +77,8 @@ public class CliParameters {
@Parameter(names = "--font-size-max", description = "Maximum font size, default is 40px.")
private int fontSizeMax = 40;

@Parameter(names = "--font-weight", description = "A font weight. Default is Bold.", converter = FontWeightConverter.class)
private FontWeight fontWeight = FontWeight.BOLD;
@Parameter(names = "--font-weight", description = "A font weight. Default is Bold.", converter = FontFaceConverter.class)
private FontFace fontWeight = FontFace.BOLD;

@Parameter(names = "--font-type", description = "The name of the font to use. The system must have the font loaded already. Default is \"Comic Sans MS\".")
private String fontType = "Comic Sans MS";
Expand Down Expand Up @@ -145,7 +145,7 @@ public String getFontType() {
return fontType;
}

public FontWeight getFontWeight() {
public FontFace getFontFace() {
return fontWeight;
}

Expand Down Expand Up @@ -297,10 +297,10 @@ public FontScalarType convert(final String input) {
return new EnumConverter<>(FontScalarType.class).convert(input);
}
}
public static class FontWeightConverter implements IStringConverter<FontWeight> {
public static class FontFaceConverter implements IStringConverter<FontFace> {
@Override
public FontWeight convert(final String input) {
return new EnumConverter<>(FontWeight.class).convert(input);
public FontFace convert(final String input) {
return new EnumConverter<>(FontFace.class).convert(input);
}
}
public static class WordStartConverter implements IStringConverter<WordStartType> {
Expand Down
12 changes: 6 additions & 6 deletions kumo-cli/src/main/java/com/kennycason/kumo/cli/KumoCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import com.kennycason.kumo.PolarWordCloud;
import com.kennycason.kumo.WordCloud;
import com.kennycason.kumo.WordFrequency;
import com.kennycason.kumo.draw.Dimension;
import com.kennycason.kumo.bg.Background;
import com.kennycason.kumo.bg.PixelBoundryBackground;
import com.kennycason.kumo.cli.CliParameters.FontScalarType;
import com.kennycason.kumo.cli.CliParameters.NormalizerType;
import com.kennycason.kumo.cli.CliParameters.WordStartType;
import com.kennycason.kumo.font.FontWeight;
import com.kennycason.kumo.draw.FontFace;
import com.kennycason.kumo.font.KumoFont;
import com.kennycason.kumo.font.scale.FontScalar;
import com.kennycason.kumo.font.scale.LinearFontScalar;
Expand All @@ -27,7 +28,6 @@
import com.kennycason.kumo.wordstart.RandomWordStart;
import com.kennycason.kumo.wordstart.WordStartStrategy;

import java.awt.*;
import java.io.*;
import java.net.URL;
import java.util.Arrays;
Expand Down Expand Up @@ -110,7 +110,7 @@ private void buildLayeredWordCloud() {
wordCloud.setColorPalette(i, new ColorPalette(cliParameters.getLayeredColors().get(i)));
wordCloud.setFontScalar(i, buildFontScalar(cliParameters.getFontScalarType()));
wordCloud.setPadding(i, cliParameters.getPadding());
wordCloud.setKumoFont(i, buildKumoFont(cliParameters.getFontWeight()));
wordCloud.setKumoFont(i, buildKumoFont(cliParameters.getFontFace()));
wordCloud.build(i, loadFrequencies(cliParameters.getInputSources().get(i)));
}
wordCloud.writeToFile(cliParameters.getOutputSource());
Expand Down Expand Up @@ -138,7 +138,7 @@ private void buildPolarWordCloud() {
wordCloud.setFontScalar(buildFontScalar(cliParameters.getFontScalarType()));
wordCloud.setPadding(cliParameters.getPadding());
wordCloud.setWordStartStrategy(buildWordStart(cliParameters.getWordStartType()));
wordCloud.setKumoFont(buildKumoFont(cliParameters.getFontWeight()));
wordCloud.setKumoFont(buildKumoFont(cliParameters.getFontFace()));
wordCloud.build(loadFrequencies(cliParameters.getInputSources().get(0)), loadFrequencies(cliParameters.getInputSources().get(1)));
wordCloud.writeToFile(cliParameters.getOutputSource());
}
Expand All @@ -158,7 +158,7 @@ private void buildStandardWordCloud() {
wordCloud.setFontScalar(buildFontScalar(cliParameters.getFontScalarType()));
wordCloud.setPadding(cliParameters.getPadding());
wordCloud.setWordStartStrategy(buildWordStart(cliParameters.getWordStartType()));
wordCloud.setKumoFont(buildKumoFont(cliParameters.getFontWeight()));
wordCloud.setKumoFont(buildKumoFont(cliParameters.getFontFace()));
wordCloud.build(loadFrequencies(cliParameters.getInputSources().get(0)));
wordCloud.writeToFile(cliParameters.getOutputSource());
}
Expand Down Expand Up @@ -208,7 +208,7 @@ private Normalizer buildNormalizer(final NormalizerType normalizer) {
throw new IllegalStateException("Unknown normalizer: " + normalizer);
}

private KumoFont buildKumoFont(final FontWeight fontWeight) {
private KumoFont buildKumoFont(final FontFace fontWeight) {
return new KumoFont(cliParameters.getFontType(), fontWeight);
}

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>android</groupId>
<artifactId>android-framework</artifactId>
<version>19</version>
<description>POM was created from install:install-file</description>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>android</groupId>
<artifactId>android-framework</artifactId>
<versioning>
<release>19</release>
<versions>
<version>19</version>
</versions>
<lastUpdated>20190120101650</lastUpdated>
</versioning>
</metadata>
15 changes: 15 additions & 0 deletions kumo-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>kumo-core</artifactId>

<repositories>
<repository>
<id>android-framework-stub-repo</id>
<url>file://${basedir}/android-framework-stub</url>
</repository>
</repositories>

<dependencies>
<!-- kumo -->
<dependency>
Expand Down Expand Up @@ -44,6 +51,14 @@
<artifactId>rtree</artifactId>
</dependency>

<!-- Android framework stubs -->
<dependency>
<groupId>android</groupId>
<artifactId>android-framework</artifactId>
<version>19</version>
<scope>provided</scope>
</dependency>

<!-- test -->
<dependency>
<groupId>junit</groupId>
Expand Down
22 changes: 10 additions & 12 deletions kumo-core/src/main/java/com/kennycason/kumo/LayeredWordCloud.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.kennycason.kumo;

import com.kennycason.kumo.bg.Background;
import com.kennycason.kumo.draw.*;
import com.kennycason.kumo.exception.KumoException;
import com.kennycason.kumo.font.KumoFont;
import com.kennycason.kumo.font.scale.FontScalar;
Expand All @@ -11,10 +12,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -30,7 +29,7 @@ public class LayeredWordCloud {

private final List<WordCloud> wordClouds = new ArrayList<>();

private Color backgroundColor = Color.BLACK;
private Color backgroundColor = new Color(0, 0, 0);

public LayeredWordCloud(final int layers, final Dimension dimension, final CollisionMode collisionMode) {
this.dimension = dimension;
Expand Down Expand Up @@ -78,17 +77,16 @@ public void setBackgroundColor(final Color backgroundColor) {
this.backgroundColor = backgroundColor;
}

public BufferedImage getBufferedImage() {
final BufferedImage bufferedImage = new BufferedImage(dimension.width, dimension.height, BufferedImage.TYPE_INT_ARGB);
final Graphics graphics = bufferedImage.getGraphics();
graphics.setColor(backgroundColor);
graphics.fillRect(0, 0, dimension.width, dimension.height);
public Image getImage() {
final Image image = new Image(dimension.getWidth(), dimension.getHeight());
final Graphics graphics = new Graphics(image);
graphics.drawRect(backgroundColor, 0, 0, dimension.getWidth(), dimension.getHeight());

for (final WordCloud wordCloud : wordClouds) {
graphics.drawImage(wordCloud.getBufferedImage(), 0, 0, null);
graphics.drawImg(wordCloud.getImage(), 0, 0);
}

return bufferedImage;
return image;
}

public WordCloud getLayer(final int layer) {
Expand All @@ -111,7 +109,7 @@ public void writeToFile(final String outputFileName) {
}
try {
LOGGER.info("Saving Layered WordCloud to: {}", outputFileName);
ImageIO.write(getBufferedImage(), extension, new File(outputFileName));
new ImageWriter().write(getImage(), extension, new FileOutputStream(new File(outputFileName)));

} catch (final IOException e) {
throw new KumoException(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.kennycason.kumo;

import com.kennycason.kumo.draw.Dimension;
import com.kennycason.kumo.exception.KumoException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
Expand Down
28 changes: 15 additions & 13 deletions kumo-core/src/main/java/com/kennycason/kumo/PolarWordCloud.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.kennycason.kumo;

import com.kennycason.kumo.draw.Color;
import com.kennycason.kumo.draw.Dimension;
import com.kennycason.kumo.draw.Point;
import com.kennycason.kumo.palette.ColorPalette;

import java.awt.*;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
Expand All @@ -14,9 +16,9 @@
public class PolarWordCloud extends WordCloud {
private static final Random RANDOM = new Random();

private static final ColorPalette DEFAULT_POSITIVE_COLORS = new ColorPalette(new Color(0x1BE000), new Color(0x1AC902), new Color(0x15B000), new Color(0x129400), new Color(0x0F7A00), new Color(0x0B5E00));
private static final ColorPalette DEFAULT_POSITIVE_COLORS = new ColorPalette(new Color(27,224,0), new Color(26,201,2), new Color(21,176,0), new Color(18,148,0), new Color(15,122,0), new Color(11,94,0));

private static final ColorPalette DEFAULT_NEGATIVE_COLORS = new ColorPalette(new Color(0xF50000), new Color(0xDE0000), new Color(0xC90202), new Color(0xB50202), new Color(0x990202), new Color(0x800101));
private static final ColorPalette DEFAULT_NEGATIVE_COLORS = new ColorPalette(new Color(245,0,0), new Color(222,0,0), new Color(201,2,2), new Color(181,2,2), new Color(153,2,2), new Color(128,1,1));

private final PolarBlendMode polarBlendMode;

Expand Down Expand Up @@ -50,10 +52,10 @@ public void build(final List<WordFrequency> wordFrequencies, final List<WordFreq
final Point[] poles = getRandomPoles();
final Point pole1 = poles[0];
final Point pole2 = poles[1];

// the background masks all none usable pixels and we can only check this raster
background.mask(backgroundCollidable);

while (wordIterator.hasNext() || wordIterator2.hasNext()) {

if (wordIterator.hasNext()) {
Expand All @@ -76,11 +78,11 @@ public void build(final List<WordFrequency> wordFrequencies, final List<WordFreq
private Point getStartPosition(final Point pole) {
switch (polarBlendMode) {
case BLUR:
final int blurX = dimension.width / 2;
final int blurY = dimension.height / 2;
final int blurX = dimension.getWidth() / 2;
final int blurY = dimension.getHeight() / 2;
return new Point(
pole.x + -blurX + RANDOM.nextInt(blurX * 2),
pole.y + -blurY + RANDOM.nextInt(blurY * 2)
pole.getX() + -blurX + RANDOM.nextInt(blurX * 2),
pole.getY() + -blurY + RANDOM.nextInt(blurY * 2)
);

case EVEN:
Expand All @@ -93,10 +95,10 @@ private Point[] getRandomPoles() {
final Point[] max = new Point[2];
double maxDistance = 0.0;
for (int i = 0; i < 100; i++) {
final int x = RANDOM.nextInt(dimension.width);
final int y = RANDOM.nextInt(dimension.height);
final int x2 = RANDOM.nextInt(dimension.width);
final int y2 = RANDOM.nextInt(dimension.height);
final int x = RANDOM.nextInt(dimension.getWidth());
final int y = RANDOM.nextInt(dimension.getHeight());
final int x2 = RANDOM.nextInt(dimension.getWidth());
final int y2 = RANDOM.nextInt(dimension.getHeight());
final double distance = Math.sqrt(Math.pow(x - x2, 2) + Math.pow(y - y2, 2));
if (distance > maxDistance) {
maxDistance = distance;
Expand Down
Loading