Skip to content

Commit

Permalink
Fix #17: let a WordCram reset() all the words. Not sure I'll keep thi…
Browse files Browse the repository at this point in the history
…s, yet.
  • Loading branch information
danbernier committed Feb 25, 2013
1 parent 3461ab3 commit e1fafb5
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 10 deletions.
3 changes: 3 additions & 0 deletions RELEASENOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ Release notes for WordCram 0.5.7, date TBD:
* Fix for issue #14: support filtering HTML by CSS selectors. (Maybe we'll add
xpath filtering, if there's demand for it.)

* Fix for issue #17: add WordCram#reset(), so you can re-render a WordCram
without loading all the text over and over again.

Release notes for WordCram 0.5.6, 2012-08-25:

* Bug fix for issue #6: clean up old SVN references in javadoc & wiki
Expand Down
27 changes: 19 additions & 8 deletions src/wordcram/Word.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public class Word implements Comparable<Word> {
private PVector targetPlace;
private PVector renderedPlace;
private Integer skippedBecause;
private EngineWord engineWord;

private HashMap<String,Object> properties = new HashMap<String,Object>();

Expand Down Expand Up @@ -338,20 +339,19 @@ else if (w.weight > weight) {
}
else return 0;
}

// Note: these are only so we can delegate to EngineWord for getShape().
private EngineWord engineWord;

// Note: this is only so we can delegate to EngineWord for getShape().
void setEngineWord(EngineWord engineWord) {
this.engineWord = engineWord;
}


// These are down here, because it comes from the EngineWord, *not* from the Fonter, Angler, etc.
/**
* Gets the width, in pixels, of the java.awt.Shape that will be rendered for
* Gets the width, in pixels, of the java.awt.Shape that will be rendered for
* this Word, based on the Font, Angle, and Size for this Word.
* If that all hasn't been figured out yet, then this returns 0.
* @return The width in pixels of this Word's Shape, or 0, if it hasn't
* @return The width in pixels of this Word's Shape, or 0, if it hasn't
* been rendered yet.
* @see #getRenderedHeight()
*/
Expand All @@ -361,15 +361,26 @@ public float getRenderedWidth() {
}

/**
* Gets the height, in pixels, of the java.awt.Shape that will be rendered for
* Gets the height, in pixels, of the java.awt.Shape that will be rendered for
* this Word, based on the Font, Angle, and Size for this Word.
* If that all hasn't been figured out yet, then this returns 0.
* @return The height in pixels of this Word's Shape, or 0, if it hasn't
* @return The height in pixels of this Word's Shape, or 0, if it hasn't
* been rendered yet.
* @see #getRenderedWidth()
*/
public float getRenderedHeight() {
if (engineWord == null) return 0.0f;
return (float)engineWord.getShape().getBounds2D().getHeight();
}

void reset() {
renderedSize = null;
renderedAngle = null;
renderedFont = null;
renderedColor = null;
targetPlace = null;
renderedPlace = null;
skippedBecause = null;
engineWord = null;
}
}
4 changes: 4 additions & 0 deletions src/wordcram/WordCram.java
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,10 @@ public void drawAll() {
getWordCramEngine().drawAll();
}

public void reset() {
getWordCramEngine().reset();
}

/**
* Get the Words that WordCram is drawing. This can be useful
* if you want to inspect exactly how the words were weighted,
Expand Down
20 changes: 19 additions & 1 deletion src/wordcram/WordCramEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class WordCramEngine {
private int eWordIndex = -1;

private RenderOptions renderOptions;
private WordShaper shaper;
private BBTreeBuilder bbTreeBuilder;

WordCramEngine(PGraphics destination, Word[] words, WordFonter fonter, WordSizer sizer, WordColorer colorer, WordAngler angler, WordPlacer placer, WordNudger nudger, WordShaper shaper, BBTreeBuilder bbTreeBuilder, RenderOptions renderOptions) {
this.destination = destination;
Expand All @@ -52,7 +54,9 @@ class WordCramEngine {

this.renderOptions = renderOptions;
this.words = words;
this.eWords = wordsIntoEngineWords(words, shaper, bbTreeBuilder);

this.shaper = shaper;
this.bbTreeBuilder = bbTreeBuilder;
}

private EngineWord[] wordsIntoEngineWords(Word[] words, WordShaper wordShaper, BBTreeBuilder bbTreeBuilder) {
Expand Down Expand Up @@ -95,10 +99,15 @@ private void skipWord(Word word, int reason) {
}

boolean hasMore() {
// If you're sketching with hasMore() & drawNext(), this will probably
// be called first, so make sure reset() has been called.
if (eWords == null) { reset(); }

return eWordIndex < eWords.length-1;
}

void drawAll() {
reset();
while(hasMore()) {
drawNext();
}
Expand All @@ -115,6 +124,15 @@ void drawNext() {
}
}

void reset() {
for (Word word : words) {
word.reset();
}

this.eWordIndex = -1;
this.eWords = wordsIntoEngineWords(words, shaper, bbTreeBuilder);
}

private boolean placeWord(EngineWord eWord) {
Word word = eWord.word;
Rectangle2D rect = eWord.getShape().getBounds2D(); // TODO can we move these into EngineWord.setDesiredLocation? Does that make sense?
Expand Down
4 changes: 3 additions & 1 deletion test/wordcram/AWordCramEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ public void willNotGoPastRenderOptionsMaxNumberOfWords() {
}

private WordCramEngine getEngine(Word... words) {
return new WordCramEngine(destination, words, fonter, sizer, colorer, angler, placer, nudger, shaper, bbTreeBuilder, renderOptions);
WordCramEngine wce = new WordCramEngine(destination, words, fonter, sizer, colorer, angler, placer, nudger, shaper, bbTreeBuilder, renderOptions);
wce.reset();
return wce;
}
}

0 comments on commit e1fafb5

Please sign in to comment.