diff --git a/examples/HFontPool/HFontPool_001/HFontPool_001.pde b/examples/HFontPool/HFontPool_001/HFontPool_001.pde new file mode 100644 index 00000000..42198950 --- /dev/null +++ b/examples/HFontPool/HFontPool_001/HFontPool_001.pde @@ -0,0 +1,52 @@ +import hype.*; +import hype.extended.colorist.*; +import hype.extended.layout.HGridLayout; + +HColorPool colors; +HDrawablePool pool; +HFontPool fonts; + +void setup() { + size(900, 900, P2D); + H.init(this).background(#202020).use3D(false); + colors = new HColorPool(#FFFFFF, #F7F7F7, #ECECEC, #333333, #0095a8, #00616f, #FF3300, #FF6600); + noLoop(); + + PFont font1, font2, font3, font4; + font1 = createFont("DroidSerifRegular.ttf", 18); + font2 = createFont("DroidSerifItalic.ttf", 48); + font3 = createFont("DroidSerifBold.ttf", 48); + font4 = createFont("DroidSerifBoldItalic.ttf", 48); + + fonts = new HFontPool(font1, font2, font3, font4); + + pool = new HDrawablePool(4); + pool.autoAddToStage() + .add(new HText("", 24, null)) + .layout( + new HGridLayout() + .startX(40) + .startY(40) + .spacing(36, 36) + .cols(1) + ) + .onCreate( + new HCallback() { + public void run(Object obj) { + int i = pool.currentIndex(); + HText t = (HText) obj; + t.fill( colors.getColor() ); + t.font(fonts.getFontAt(i)); + t.text(i+1 + ". " + fonts.currentName()); + t.anchorAt(H.LEFT); + } + } + ) + .requestAll() + ; + + H.drawStage(); +} + +void draw() { +} \ No newline at end of file diff --git a/examples/HFontPool/HFontPool_001/data/DroidSerifBold.ttf b/examples/HFontPool/HFontPool_001/data/DroidSerifBold.ttf new file mode 100644 index 00000000..b6add0e4 Binary files /dev/null and b/examples/HFontPool/HFontPool_001/data/DroidSerifBold.ttf differ diff --git a/examples/HFontPool/HFontPool_001/data/DroidSerifBoldItalic.ttf b/examples/HFontPool/HFontPool_001/data/DroidSerifBoldItalic.ttf new file mode 100644 index 00000000..921434f5 Binary files /dev/null and b/examples/HFontPool/HFontPool_001/data/DroidSerifBoldItalic.ttf differ diff --git a/examples/HFontPool/HFontPool_001/data/DroidSerifItalic.ttf b/examples/HFontPool/HFontPool_001/data/DroidSerifItalic.ttf new file mode 100644 index 00000000..1ef1f75e Binary files /dev/null and b/examples/HFontPool/HFontPool_001/data/DroidSerifItalic.ttf differ diff --git a/examples/HFontPool/HFontPool_001/data/DroidSerifRegular.ttf b/examples/HFontPool/HFontPool_001/data/DroidSerifRegular.ttf new file mode 100644 index 00000000..239ba382 Binary files /dev/null and b/examples/HFontPool/HFontPool_001/data/DroidSerifRegular.ttf differ diff --git a/examples/HFontPool/HFontPool_002/HFontPool_002.pde b/examples/HFontPool/HFontPool_002/HFontPool_002.pde new file mode 100644 index 00000000..fbb85083 --- /dev/null +++ b/examples/HFontPool/HFontPool_002/HFontPool_002.pde @@ -0,0 +1,53 @@ +import hype.*; +import hype.extended.colorist.*; +import hype.extended.layout.HGridLayout; + +HColorPool colors; +HDrawablePool pool; +HFontPool fonts; + +void setup() { + size(900, 900, P2D); + H.init(this).background(#202020).use3D(false); + colors = new HColorPool(#FFFFFF, #F7F7F7, #ECECEC, #333333, #0095a8, #00616f, #FF3300, #FF6600); + noLoop(); + + PFont font1, font2, font3, font4; + font1 = createFont("DroidSerifRegular.ttf", 18); + font2 = createFont("DroidSerifItalic.ttf", 48); + font3 = createFont("DroidSerifBold.ttf", 48); + font4 = createFont("DroidSerifBoldItalic.ttf", 48); + + fonts = new HFontPool().add(font1).add(font2).add(font3).add(font4,8); + //weighted so that font 4 (DroidSerifBoldItalic) will be added 8 times + + pool = new HDrawablePool(20); + pool.autoAddToStage() + .add(new HText("", 24, null)) + .layout( + new HGridLayout() + .startX(40) + .startY(40) + .spacing(36, 36) + .cols(1) + ) + .onCreate( + new HCallback() { + public void run(Object obj) { + int i = pool.currentIndex()+1; + HText t = (HText) obj; + t.fill( colors.getColor() ); + t.font(fonts.getRandomFont()); //pick random font + t.text(i + ". " + fonts.currentName()); + t.anchorAt(H.LEFT); + } + } + ) + .requestAll() + ; + + H.drawStage(); +} + +void draw() { +} \ No newline at end of file diff --git a/examples/HFontPool/HFontPool_002/data/DroidSerifBold.ttf b/examples/HFontPool/HFontPool_002/data/DroidSerifBold.ttf new file mode 100644 index 00000000..b6add0e4 Binary files /dev/null and b/examples/HFontPool/HFontPool_002/data/DroidSerifBold.ttf differ diff --git a/examples/HFontPool/HFontPool_002/data/DroidSerifBoldItalic.ttf b/examples/HFontPool/HFontPool_002/data/DroidSerifBoldItalic.ttf new file mode 100644 index 00000000..921434f5 Binary files /dev/null and b/examples/HFontPool/HFontPool_002/data/DroidSerifBoldItalic.ttf differ diff --git a/examples/HFontPool/HFontPool_002/data/DroidSerifItalic.ttf b/examples/HFontPool/HFontPool_002/data/DroidSerifItalic.ttf new file mode 100644 index 00000000..1ef1f75e Binary files /dev/null and b/examples/HFontPool/HFontPool_002/data/DroidSerifItalic.ttf differ diff --git a/examples/HFontPool/HFontPool_002/data/DroidSerifRegular.ttf b/examples/HFontPool/HFontPool_002/data/DroidSerifRegular.ttf new file mode 100644 index 00000000..239ba382 Binary files /dev/null and b/examples/HFontPool/HFontPool_002/data/DroidSerifRegular.ttf differ diff --git a/examples/HFontPool/HFontPool_003/HFontPool_003.pde b/examples/HFontPool/HFontPool_003/HFontPool_003.pde new file mode 100644 index 00000000..8ee3951b --- /dev/null +++ b/examples/HFontPool/HFontPool_003/HFontPool_003.pde @@ -0,0 +1,46 @@ +import hype.*; +import hype.extended.colorist.*; +import hype.extended.layout.HGridLayout; + +HColorPool colors; +HDrawablePool pool; +HFontPool fonts; + +void setup() { + size(900, 900, P2D); + H.init(this).background(#202020).use3D(false); + colors = new HColorPool(#FFFFFF, #F7F7F7, #ECECEC, #333333, #0095a8, #00616f, #FF3300, #FF6600); + noLoop(); + + fonts = new HFontPool().addSystemFonts(); //load all available system fonts... + + pool = new HDrawablePool(20); + pool.autoAddToStage() + .add(new HText("", 24, null)) + .layout( + new HGridLayout() + .startX(40) + .startY(40) + .spacing(36, 36) + .cols(1) + ) + .onCreate( + new HCallback() { + public void run(Object obj) { + int i = pool.currentIndex()+1; + HText t = (HText) obj; + t.fill( colors.getColor() ); + t.font(fonts.getRandomFont()); //pick random font + t.text(i + ". " + fonts.currentName()); + t.anchorAt(H.LEFT); + } + } + ) + .requestAll() + ; + + H.drawStage(); +} + +void draw() { +} \ No newline at end of file diff --git a/examples/HFontPool/HFontPool_004/HFontPool_004.pde b/examples/HFontPool/HFontPool_004/HFontPool_004.pde new file mode 100644 index 00000000..8ee3951b --- /dev/null +++ b/examples/HFontPool/HFontPool_004/HFontPool_004.pde @@ -0,0 +1,46 @@ +import hype.*; +import hype.extended.colorist.*; +import hype.extended.layout.HGridLayout; + +HColorPool colors; +HDrawablePool pool; +HFontPool fonts; + +void setup() { + size(900, 900, P2D); + H.init(this).background(#202020).use3D(false); + colors = new HColorPool(#FFFFFF, #F7F7F7, #ECECEC, #333333, #0095a8, #00616f, #FF3300, #FF6600); + noLoop(); + + fonts = new HFontPool().addSystemFonts(); //load all available system fonts... + + pool = new HDrawablePool(20); + pool.autoAddToStage() + .add(new HText("", 24, null)) + .layout( + new HGridLayout() + .startX(40) + .startY(40) + .spacing(36, 36) + .cols(1) + ) + .onCreate( + new HCallback() { + public void run(Object obj) { + int i = pool.currentIndex()+1; + HText t = (HText) obj; + t.fill( colors.getColor() ); + t.font(fonts.getRandomFont()); //pick random font + t.text(i + ". " + fonts.currentName()); + t.anchorAt(H.LEFT); + } + } + ) + .requestAll() + ; + + H.drawStage(); +} + +void draw() { +} \ No newline at end of file diff --git a/src/main/java/hype/HFontPool.java b/src/main/java/hype/HFontPool.java new file mode 100644 index 00000000..b252e82b --- /dev/null +++ b/src/main/java/hype/HFontPool.java @@ -0,0 +1,197 @@ +/* + * HFontPool + * + * Helper class for HYPE framework - creates a pool of fonts + * + * HYPE_Processing + * http://www.hypeframework.org/ & https://github.com/hype/HYPE_Processing + * + * Copyright (c) 2013-2017 Joshua Davis + * + * Distributed under the BSD License. See LICENSE.txt for details. + * + * All rights reserved. + * + */ + +package hype; + +import java.util.ArrayList; +import processing.core.PApplet; +import processing.core.PFont; +import static processing.core.PApplet.max; +import static processing.core.PApplet.min; + +public class HFontPool { + private int currentIndex = -1; + private String currentName = ""; + private ArrayList fontList; + PApplet app = H.app(); + + public HFontPool() { + } + + public HFontPool(Object... fonts) { + PFont type; + for (int i = 0; i < fonts.length; ++i) { + if (fonts[i] instanceof String) { + type = app.createFont((String) fonts[i], 24); + add(type); + } + if (fonts[i] instanceof PFont) { + add(fonts[i]); + } + if (fonts[i] instanceof HText) { + HText temp = (HText) fonts[i]; + add(temp.font()); + } + } + } + + public HFontPool addSystemFonts() { + String[] systemFontList; + systemFontList = PFont.list(); //get all available system fonts + PFont type; + for (int i = 0; i < systemFontList.length; ++i) { + type = app.createFont(systemFontList[i], 24); + add(type); + } + return this; + } + + public int setIndex(int i) { + //ensure index within bounds + if (i >= size() || i < 0) { + currentIndex = 0; + if (i < 0) { + currentIndex = size() - 1; + } + } else { + currentIndex = i; + } + return currentIndex; + } + + public int currentIndex() { + return currentIndex; + } + + public String currentName() { + PFont font; + if (size() <= 0) { + font = defaultFont(); + } else { + font = fontList.get(currentIndex()); + } + currentName = font.getName(); + return currentName; + } + + public int size() { + int num; + if (fontList != null) { + num = fontList.size(); + } else { + num = 0; + } + return num; + } + + public HFontPool add(Object font) { + + if (size() == 0) fontList = new ArrayList(); + + if (font instanceof PFont) { + fontList.add((PFont) font); + } + if (font instanceof String) { + PFont type; + type = app.createFont((String) font, 24); + fontList.add(type); + } + if (font instanceof HText) { + HText temp = (HText) font; + add(temp.font()); + } + return this; + } + + public HFontPool add(Object font, int freq) { + if (font instanceof PFont) { + while (freq-- > 0) add((PFont) font); + } + if (font instanceof String) { + PFont type; + while (freq-- > 0) { + type = app.createFont((String) font, 24); + add(type); + } + } + if (font instanceof HText) { + HText temp = (HText) font; + while (freq-- > 0) { + add(temp.font()); + } + } + + return this; + } + + public PFont getRandomFont() { + //select a random font + if (size() <= 0) { + return defaultFont(); + } else { + int rndIndex = HMath.randomInt(0, size()); + return getFontAt(rndIndex); + } + } + + //select subset of available fonts + public PFont getRandomFont(int lower, int upper) { + int lbound = max(0, lower); + int ubound = min(upper, size()); + //select a random font + if (size() <= 0) { + return defaultFont(); + } else { + int rndIndex = HMath.randomInt(lbound, ubound); + return getFontAt(rndIndex); + } + } + + public PFont getNextFont() { + // cycles forward through all available fonts + if (size() <= 0) { + return defaultFont(); + } else { + currentIndex = setIndex(currentIndex + 1); + return fontList.get(currentIndex); + } + } + + public PFont getPrevFont() { + // cycles through all available fonts (in reverse order) + if (size() <= 0) { + return defaultFont(); + } else { + currentIndex = setIndex(currentIndex - 1); + return fontList.get(currentIndex); + } + } + + public PFont getFontAt(int index) { + //extract a specific font by index + if (size() <= 0) { + return defaultFont(); + } else { + currentIndex = setIndex(index); + return fontList.get(currentIndex); + } + } + + private PFont defaultFont() { + return app.createFont("SansSerif", 64); + } + +}