ruby_wordcram 1.0.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.mvn/extensions.xml +8 -0
  4. data/.mvn/wrapper/maven-wrapper.properties +1 -0
  5. data/Rakefile +28 -5
  6. data/docs/_posts/2017-03-07-getting_started.md +3 -2
  7. data/docs/_posts/2017-03-07-under_the_hood.md +33 -0
  8. data/lib/WordCram.jar +0 -0
  9. data/lib/jsoup-1.10.2.jar +0 -0
  10. data/lib/ruby_wordcram/version.rb +1 -1
  11. data/lib/ruby_wordcram.rb +1 -2
  12. data/pom.rb +53 -0
  13. data/pom.xml +87 -0
  14. data/ruby_wordcram.gemspec +1 -2
  15. data/src/cue/lang/Counter.java +141 -0
  16. data/src/cue/lang/IterableText.java +10 -0
  17. data/src/cue/lang/NGramIterator.java +151 -0
  18. data/src/cue/lang/SentenceIterator.java +86 -0
  19. data/src/cue/lang/WordIterator.java +60 -0
  20. data/src/cue/lang/stop/StopWords.java +114 -0
  21. data/src/cue/lang/stop/arabic +351 -0
  22. data/src/cue/lang/stop/armenian +45 -0
  23. data/src/cue/lang/stop/catalan +219 -0
  24. data/src/cue/lang/stop/croatian +2024 -0
  25. data/src/cue/lang/stop/czech +256 -0
  26. data/src/cue/lang/stop/danish +94 -0
  27. data/src/cue/lang/stop/dutch +107 -0
  28. data/src/cue/lang/stop/english +183 -0
  29. data/src/cue/lang/stop/esperanto +180 -0
  30. data/src/cue/lang/stop/farsi +966 -0
  31. data/src/cue/lang/stop/finnish +235 -0
  32. data/src/cue/lang/stop/french +543 -0
  33. data/src/cue/lang/stop/german +231 -0
  34. data/src/cue/lang/stop/greek +637 -0
  35. data/src/cue/lang/stop/hebrew +220 -0
  36. data/src/cue/lang/stop/hindi +97 -0
  37. data/src/cue/lang/stop/hungarian +202 -0
  38. data/src/cue/lang/stop/italian +279 -0
  39. data/src/cue/lang/stop/latin +1 -0
  40. data/src/cue/lang/stop/norwegian +176 -0
  41. data/src/cue/lang/stop/polish +138 -0
  42. data/src/cue/lang/stop/portuguese +204 -0
  43. data/src/cue/lang/stop/romanian +284 -0
  44. data/src/cue/lang/stop/russian +652 -0
  45. data/src/cue/lang/stop/slovak +110 -0
  46. data/src/cue/lang/stop/slovenian +448 -0
  47. data/src/cue/lang/stop/spanish +308 -0
  48. data/src/cue/lang/stop/swedish +114 -0
  49. data/src/cue/lang/stop/turkish +117 -0
  50. data/src/cue/lang/unicode/BlockUtil.java +103 -0
  51. data/src/cue/lang/unicode/Normalizer.java +55 -0
  52. data/src/cue/lang/unicode/Normalizer6.java +32 -0
  53. data/src/license.txt +201 -0
  54. data/src/wordcram/Anglers.java +137 -0
  55. data/src/wordcram/BBTree.java +133 -0
  56. data/src/wordcram/BBTreeBuilder.java +61 -0
  57. data/src/wordcram/Colorers.java +52 -0
  58. data/src/wordcram/EngineWord.java +73 -0
  59. data/src/wordcram/Fonters.java +17 -0
  60. data/src/wordcram/HsbWordColorer.java +28 -0
  61. data/src/wordcram/ImageShaper.java +91 -0
  62. data/src/wordcram/Observer.java +9 -0
  63. data/src/wordcram/PlacerHeatMap.java +134 -0
  64. data/src/wordcram/Placers.java +74 -0
  65. data/src/wordcram/PlottingWordNudger.java +38 -0
  66. data/src/wordcram/PlottingWordPlacer.java +36 -0
  67. data/src/wordcram/ProcessingWordRenderer.java +42 -0
  68. data/src/wordcram/RandomWordNudger.java +44 -0
  69. data/src/wordcram/RenderOptions.java +10 -0
  70. data/src/wordcram/ShapeBasedPlacer.java +66 -0
  71. data/src/wordcram/Sizers.java +54 -0
  72. data/src/wordcram/SketchCallbackObserver.java +70 -0
  73. data/src/wordcram/SpiralWordNudger.java +31 -0
  74. data/src/wordcram/SvgWordRenderer.java +110 -0
  75. data/src/wordcram/SwirlWordPlacer.java +25 -0
  76. data/src/wordcram/UpperLeftWordPlacer.java +27 -0
  77. data/src/wordcram/WaveWordPlacer.java +25 -0
  78. data/src/wordcram/Word.java +357 -0
  79. data/src/wordcram/WordAngler.java +20 -0
  80. data/src/wordcram/WordArray.java +18 -0
  81. data/src/wordcram/WordBag.java +31 -0
  82. data/src/wordcram/WordColorer.java +25 -0
  83. data/src/wordcram/WordCounter.java +96 -0
  84. data/src/wordcram/WordCram.java +920 -0
  85. data/src/wordcram/WordCramEngine.java +196 -0
  86. data/src/wordcram/WordFonter.java +24 -0
  87. data/src/wordcram/WordNudger.java +44 -0
  88. data/src/wordcram/WordPlacer.java +44 -0
  89. data/src/wordcram/WordRenderer.java +10 -0
  90. data/src/wordcram/WordShaper.java +78 -0
  91. data/src/wordcram/WordSizer.java +46 -0
  92. data/src/wordcram/WordSkipReason.java +42 -0
  93. data/src/wordcram/WordSorterAndScaler.java +31 -0
  94. data/src/wordcram/WordSource.java +5 -0
  95. data/src/wordcram/text/Html.java +15 -0
  96. data/src/wordcram/text/Html2Text.java +17 -0
  97. data/src/wordcram/text/Text.java +15 -0
  98. data/src/wordcram/text/TextFile.java +23 -0
  99. data/src/wordcram/text/TextSource.java +5 -0
  100. data/src/wordcram/text/WebPage.java +23 -0
  101. metadata +94 -5
  102. data/lib/cue.language.jar +0 -0
  103. data/lib/jsoup-1.7.2.jar +0 -0
  104. data/vendors/Rakefile +0 -51
@@ -0,0 +1,103 @@
1
+ /*
2
+ Copyright 2009 IBM Corp
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+ package cue.lang.unicode;
17
+
18
+ import java.lang.Character.UnicodeBlock;
19
+ import java.util.Collection;
20
+ import java.util.List;
21
+ import java.util.regex.Pattern;
22
+
23
+ import cue.lang.Counter;
24
+ import cue.lang.WordIterator;
25
+
26
+ public class BlockUtil {
27
+ static final Pattern EXTENDED_LATIN = Pattern
28
+ .compile("\\p{InLatin-1Supplement}");
29
+ static final Pattern HYPEREXTENDED_LATIN = Pattern.compile("[" //
30
+ + "\\p{InLatinExtended-A}\\p{InLatinExtended-B}" //
31
+ + "\\p{InSpacingModifierLetters}" //
32
+ + "\\p{InIPAExtensions}" //
33
+ + "\\p{InCombiningDiacriticalMarks}]");
34
+
35
+ private static UnicodeBlock getBlock(final String word) {
36
+ final int c = word.codePointAt(0);
37
+ final UnicodeBlock block = UnicodeBlock.of(c);
38
+ if (block == UnicodeBlock.BASIC_LATIN
39
+ && HYPEREXTENDED_LATIN.matcher(word).find()) {
40
+ return UnicodeBlock.LATIN_EXTENDED_A;
41
+ }
42
+ if (block == UnicodeBlock.BASIC_LATIN
43
+ && EXTENDED_LATIN.matcher(word).find()) {
44
+ return UnicodeBlock.LATIN_1_SUPPLEMENT;
45
+ }
46
+ return block;
47
+ }
48
+
49
+ public static UnicodeBlock guessUnicodeBlock(final String text) {
50
+ return guessUnicodeBlock(new Counter<>(new WordIterator(text)));
51
+ }
52
+
53
+ public static UnicodeBlock guessUnicodeBlock(
54
+ final Counter<String> wordCounter) {
55
+ return guessUnicodeBlock(wordCounter.getMostFrequent(50));
56
+ }
57
+
58
+ /**
59
+ * This method is for helping you guess, e.g., what kind of font you'll need
60
+ * in order to represent some text. In particular, if a lot of the
61
+ * characters in your text are Latin, but there are a few LATIN_EXTENDED_A
62
+ * characters lurking in there, then we say the result is LATIN_EXTENDED_A,
63
+ * because you'll need to be able to handle those characters.
64
+ *
65
+ * @param words
66
+ * @return The most representative UnicodeBlock for the given words.
67
+ */
68
+ public static UnicodeBlock guessUnicodeBlock(final Collection<String> words) {
69
+ boolean hasExtendedLatin = false;
70
+ boolean hasHyperextendedLatin = false;
71
+ final Counter<UnicodeBlock> counter = new Counter<>();
72
+ for (final String word : words) {
73
+ final UnicodeBlock block = getBlock(word);
74
+ if (block == UnicodeBlock.LATIN_1_SUPPLEMENT) {
75
+ hasExtendedLatin = true;
76
+ }
77
+ if (block == UnicodeBlock.LATIN_EXTENDED_A) {
78
+ hasHyperextendedLatin = true;
79
+ }
80
+ counter.note(block);
81
+ }
82
+ final List<UnicodeBlock> mostFrequent = counter.getMostFrequent(1);
83
+ if (mostFrequent.isEmpty()) {
84
+ return null;
85
+ }
86
+ UnicodeBlock b = mostFrequent.get(0);
87
+ /*
88
+ * If we've seen *any* extended latin, and we're mostly latin, then
89
+ * treat the whole thing as extended.
90
+ */
91
+ if (b == UnicodeBlock.BASIC_LATIN
92
+ || b == UnicodeBlock.LATIN_1_SUPPLEMENT) {
93
+ if (hasHyperextendedLatin) {
94
+ return UnicodeBlock.LATIN_EXTENDED_A;
95
+ }
96
+ if (hasExtendedLatin) {
97
+ return UnicodeBlock.LATIN_1_SUPPLEMENT;
98
+ }
99
+ }
100
+ return b;
101
+ }
102
+
103
+ }
@@ -0,0 +1,55 @@
1
+ /*
2
+ Copyright 2009 IBM Corp
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+ package cue.lang.unicode;
17
+
18
+ import java.lang.reflect.InvocationTargetException;
19
+ import java.util.logging.Level;
20
+ import java.util.logging.Logger;
21
+
22
+ /**
23
+ *
24
+ * @author Jonathan Feinberg <jdf@us.ibm.com>
25
+ *
26
+ */
27
+ public abstract class Normalizer {
28
+
29
+ public static Normalizer getInstance() {
30
+ return INSTANCE;
31
+ }
32
+
33
+ abstract public String normalize(final String s);
34
+
35
+ private static final Normalizer INSTANCE;
36
+
37
+ static {
38
+ try {
39
+ INSTANCE = (Normalizer) Class.forName(getNormalizerClass())
40
+ .getConstructor().newInstance();
41
+ } catch (final ClassNotFoundException | IllegalAccessException | IllegalArgumentException | InstantiationException | NoSuchMethodException | SecurityException | InvocationTargetException e) {
42
+ throw new RuntimeException(e);
43
+ }
44
+ }
45
+
46
+ private static String getNormalizerClass() {
47
+
48
+ try {
49
+ Class.forName("java.text.Normalizer");
50
+ } catch (ClassNotFoundException ex) {
51
+ Logger.getLogger(Normalizer.class.getName()).log(Level.SEVERE, null, ex);
52
+ }
53
+ return "cue.lang.unicode.Normalizer6";
54
+ }
55
+ }
@@ -0,0 +1,32 @@
1
+ /*
2
+ Copyright 2009 IBM Corp
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+ package cue.lang.unicode;
17
+
18
+ /**
19
+ *
20
+ * @author Jonathan Feinberg <jdf@us.ibm.com>
21
+ *
22
+ */
23
+ class Normalizer6 extends Normalizer {
24
+ public Normalizer6() {
25
+ }
26
+
27
+ @Override
28
+ public String normalize(final String s) {
29
+ return java.text.Normalizer
30
+ .normalize(s, java.text.Normalizer.Form.NFKD);
31
+ }
32
+ }
data/src/license.txt ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,137 @@
1
+ package wordcram;
2
+
3
+ import java.util.Random;
4
+
5
+ import processing.core.PApplet;
6
+ import processing.core.PConstants;
7
+
8
+ /**
9
+ * Some pre-fab WordAnglers.
10
+ * <p>
11
+ * If you want a pretty typical WordAngler, it's probably in here; if you want
12
+ * to know how to build your own WordAngler, you can learn from the source for
13
+ * these.
14
+ *
15
+ * @author Dan Bernier
16
+ */
17
+ public class Anglers {
18
+
19
+ /**
20
+ * @return a WordAngler that gives a random angle every time it's called.
21
+ */
22
+ public static WordAngler random() {
23
+ final Random r = new Random();
24
+ return (Word w) -> r.nextFloat() * PConstants.TWO_PI;
25
+ }
26
+
27
+ /**
28
+ * @param min
29
+ * the lower-bound of the angle range
30
+ * @param max
31
+ * the upper-bound of the angle range
32
+ * @return a WordAngler that gives a random angle between min and max, every
33
+ * time it's called.
34
+ */
35
+ public static WordAngler randomBetween(final float min, final float max) {
36
+ final Random r = new Random();
37
+ final float difference = max - min;
38
+ return (Word w) -> (r.nextFloat() * difference) + min;
39
+ }
40
+
41
+ /**
42
+ * @return a WordAngler that angles all words between -7 degrees and 7
43
+ * degrees, for a "heaped" effect.
44
+ */
45
+ public static WordAngler heaped() {
46
+ final float angle = PApplet.radians(7);
47
+ return randomBetween(-angle, angle);
48
+ }
49
+
50
+ /**
51
+ * If you want all your words to be drawn at the same angle, use this. For
52
+ * example, {@link #horiz()} is basically implemented as
53
+ * <code>return alwaysUse(0f);</code>.
54
+ *
55
+ * @see #horiz()
56
+ * @param angle
57
+ * The angle all words should be rotated at.
58
+ * @return a WordAngler that always returns the given angle parameter.
59
+ */
60
+ public static WordAngler alwaysUse(final float angle) {
61
+ return (Word w) -> angle;
62
+ }
63
+
64
+ /**
65
+ * Just like {@link #alwaysUse(float)}, but it takes multiple angles. If you
66
+ * want all your words to be drawn at the same N angles, pass those angles
67
+ * to {@link #alwaysUse(float)}. You can pass as many angles as you like.
68
+ * <p>
69
+ * For example, if you want all your words drawn on 45&deg; and 135&deg;
70
+ * angles, use <code>Anglers.pickFrom(radians(45), radians(135))</code>.
71
+ * {@link #hexes()} is a similar example.
72
+ *
73
+ * @see #alwaysUse(float)
74
+ * @see #hexes()
75
+ * @param angles
76
+ * The angles all words should be rotated at.
77
+ * @return A WordAngler that will pick one of the angles, at random, for
78
+ * each word.
79
+ */
80
+ public static WordAngler pickFrom(final float... angles) {
81
+ final Random r = new Random();
82
+ return (Word w) -> angles[r.nextInt(angles.length)];
83
+ }
84
+
85
+ /**
86
+ * A WordAngler that draws all words at hexagonal angles, or (if you're a
87
+ * bit more mathy) 0&pi;/6, 1&pi;/6, 2&pi;/6, 3&pi;/6, 4&pi;/6, and 5&pi;/6.
88
+ * It gives a vaguely snow-flake look.
89
+ * <p>
90
+ * It's implemented with {@link #pickFrom(float...)}.
91
+ * <p>
92
+ * (In retrospect, this is probably not one you'll use very often, so it
93
+ * might not merit a place in Anglers. But whatever.)
94
+ *
95
+ * @see #pickFrom(float...)
96
+ * @return a WordAngler that draws all words at hexagonal angles.
97
+ */
98
+ public static WordAngler hexes() {
99
+ float oneSixth = PConstants.TWO_PI / 6f;
100
+ return pickFrom(0f, oneSixth, 2 * oneSixth, 3 * oneSixth, 4 * oneSixth,
101
+ 5 * oneSixth);
102
+ }
103
+
104
+ /**
105
+ * A WordAngler that draws all words horizontally. It uses
106
+ * {@link #alwaysUse(float)}.
107
+ *
108
+ * @return a WordAngler that draws all words horizontally.
109
+ */
110
+ public static WordAngler horiz() {
111
+ return alwaysUse(0f);
112
+ }
113
+
114
+ /**
115
+ * A WordAngler that draws all words vertically, pointing both up and down.
116
+ * It uses {@link #pickFrom(float...)}.
117
+ *
118
+ * @return a WordAngler that draws all words vertically, pointing both up
119
+ * and down.
120
+ */
121
+ public static WordAngler upAndDown() {
122
+ return pickFrom(PConstants.HALF_PI, -PConstants.HALF_PI);
123
+ }
124
+
125
+ /**
126
+ * A WordAngler that draws 5/7 words horizontally, and the rest going up and
127
+ * down. It makes for a pretty nice effect. A WordAngler that draws all
128
+ * words vertically, pointing both up and down. It uses
129
+ * {@link #pickFrom(float...)}.
130
+ *
131
+ * @return a WordAngler that draws most of the words horizontally, and the
132
+ * rest vertically.
133
+ */
134
+ public static WordAngler mostlyHoriz() {
135
+ return pickFrom(0f, 0f, 0f, 0f, 0f, PConstants.HALF_PI, -PConstants.HALF_PI);
136
+ }
137
+ }