Created
May 11, 2012 22:39
-
-
Save ElectricJack/2662880 to your computer and use it in GitHub Desktop.
Converts a text file to a printable PDF of custom moving labels for easier packing & sorting.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Example Moving_Labels File: | |
- Must be created in sketch folder before running script. | |
Home: | |
- Sheets & Cloth | |
- (Decor) Chinese Lantern Spheres | |
Office: | |
- Assorted Computer & Game Peripherals | |
- | |
Electronics: | |
- | |
Shop: | |
- Socket Wrenches (METRIC) | |
- Socket Wrenches (STANDARD) | |
- Circular Saw | |
- Jig Saw | |
- Cordless Drill & Screwdriver | |
- Drill Bits | |
- Dremel Tool & Accessories | |
- Cutting Tools, Blades & Xacto | |
- Nails, Hammers, Staple Gun & Impact Tools |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
MovingLabels.pde | |
Converts a text file to a printable PDF of custom moving labels | |
for easier packing and sorting. | |
Copyright (c) 2011, Jack W. Kern | |
Tested with: Processing 1.5 (osx), 2.0 beta (win) | |
Contact: jack.w.kern@gmail.com (jackkern.com) | |
No dependencies other than processing's standard libraries. | |
---- | |
This library is free software; you can redistribute it and/or | |
modify it under the terms of the GNU Lesser General Public | |
License as published by the Free Software Foundation; either | |
version 2.1 of the License, or (at your option) any later version. | |
This library is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
Lesser General Public License for more details. | |
You should have received a copy of the GNU Lesser General | |
Public License along with this library; if not, write to the | |
Free Software Foundation, Inc., 59 Temple Place, Suite 330, | |
Boston, MA 02111-1307 USA | |
*/ | |
// | |
// Moving Labels parses a text file in the sketch folder named | |
// "moving_labels.txt" with the following format (example): | |
// " | |
// | |
//[Major category name]: | |
// - [Container contents / Description] | |
//Home: | |
// - Sheets & Cloth | |
// - (Decor) Chinese Lantern Spheres | |
// | |
//Office: | |
// - Assorted Computer & Game Peripherals | |
// - | |
//Electronics: | |
// - | |
//Shop: | |
// - Socket Wrenches (METRIC) | |
// - Socket Wrenches (STANDARD) | |
// - Circular Saw | |
// - Jig Saw | |
// - Cordless Drill & Screwdriver | |
// - Drill Bits | |
// - Dremel Tool & Accessories | |
// - Cutting Tools, Blades & Xacto | |
// - Nails, Hammers, Staple Gun & Impact Tools | |
// | |
//" | |
// This file must be created to run the script. | |
import processing.pdf.*; | |
float margin = 10; | |
int rows = 8; | |
int cols = 2; | |
int s = 26; | |
// -------------------------------------------------------------------- // | |
float getLabelY( int r ) { return margin + r*(margin + getLabelH()); } | |
float getLabelX( int c ) { return margin + c*(margin + getLabelW()); } | |
float getLabelW( ) { return (width - (cols+1)*margin) / cols; } | |
float getLabelH( ) { return (height - (rows+1)*margin) / rows; } | |
class Label { | |
String name; | |
String description; | |
Label(String name, String description) { | |
this.name = name; | |
this.description = description; | |
} | |
} | |
List<Label> labels = new ArrayList<Label>(); | |
int currentLabel = 0; | |
// -------------------------------------------------------------------- // | |
void setup() { | |
size( 612, 792, PDF, "moving_labels.pdf" ); | |
// Parse the labels text file | |
String active_name = ""; | |
String[] lines = loadStrings("moving_labels.txt"); | |
for( String line : lines ) { | |
line = line.trim(); | |
if( line.length() == 0 ) continue; | |
if( line.contains(":") ) { | |
active_name = line.substring( 0, line.indexOf(":") ); | |
} else if ( line.contains("-") ) { | |
String desc = line.substring( line.indexOf("-") + 1 ).trim(); | |
println( active_name + ": " + " \"" + desc + "\"" ); | |
labels.add( new Label( active_name, desc ) ); | |
} | |
} | |
// Set the font for the PDF | |
textFont( createFont( "Ariel", 72 ) ); | |
} | |
// -------------------------------------------------------------------- // | |
void draw() { | |
background(255); | |
fill(0); | |
drawPage(); | |
if( currentLabel >= labels.size() ) | |
exit(); | |
} | |
// -------------------------------------------------------------------- // | |
void drawPage() { | |
for( int r=0;r<rows;++r) | |
for( int c=0;c<cols;++c) { | |
if( currentLabel < labels.size() ) { | |
Label l = labels.get(currentLabel); | |
++currentLabel; | |
drawLabel(c,r,l.name,l.description); | |
} | |
} | |
if( currentLabel < labels.size() ) { | |
PGraphicsPDF pdf = (PGraphicsPDF) g; | |
pdf.nextPage(); | |
} | |
} | |
// -------------------------------------------------------------------- // | |
void drawLabel( int c, int r, String label, String description ) { | |
float x = getLabelX(c); | |
float y = getLabelY(r); | |
float w = getLabelW(); | |
float h = getLabelH(); | |
stroke( 0 ); | |
noFill(); | |
rect( x, y, w, h ); | |
textSize( s ); | |
textAlign( CENTER, TOP ); | |
text( label, x+w*0.5f, y+margin ); | |
textSize( s / 2 ); | |
text( description, x+margin, y+s+2*margin, w-2*margin, h-2*margin ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment