Browse files

Addign ArcText and spiralText

  • Loading branch information...
1 parent 29a13a1 commit ead49942761c026bd940e0086fdf134f8632a210 @patriciogonzalezvivo committed Apr 26, 2012
Showing with 264 additions and 131 deletions.
  1. +14 −0 src/ofxTextAlignment.h
  2. +9 −0 src/ofxTextArc.cpp
  3. +38 −0 src/ofxTextArc.h
  4. +129 −83 src/ofxTextBlock.cpp
  5. +24 −28 src/ofxTextBlock.h
  6. +14 −0 src/ofxTextFont.h
  7. +3 −13 src/ofxTextSequencer.cpp
  8. +0 −7 src/ofxTextSequencer.h
  9. +14 −0 src/ofxTextShape.h
  10. +14 −0 src/ofxTextSpiral.h
  11. +5 −0 src/ofxTextSuite.h
View
14 src/ofxTextAlignment.h
@@ -0,0 +1,14 @@
+//
+// ofxTextAlignment.h
+// ofxArcText-example
+//
+// Created by Patricio González Vivo on 4/24/12.
+// Copyright (c) 2012 PatricioGonzalezVivo.com. All rights reserved.
+//
+
+#ifndef ofxArcText_example_ofxTextAlignment_h
+#define ofxArcText_example_ofxTextAlignment_h
+
+
+
+#endif
View
9 src/ofxTextArc.cpp
@@ -0,0 +1,9 @@
+//
+// ofxTextArc.cpp
+// ofxArcText-example
+//
+// Created by Patricio González Vivo on 4/24/12.
+// Copyright (c) 2012 PatricioGonzalezVivo.com. All rights reserved.
+//
+
+#include <iostream>
View
38 src/ofxTextArc.h
@@ -9,13 +9,51 @@
#ifndef OFXTEXTARC_H
#define OFXTEXTARC_H
+#include "ofMain.h"
+
+#include "ofxTextFont.h"
+#include "ofxTextAlignment.h"
+
+typedef struct {
+ float angle;
+ float width;
+ float radius;
+ float scale;
+} letterArc;
+
class ofxTextArc {
public:
ofxTextArc();
+ virtual void loadFont(string _fontLocation, float _fontSize, int _dpi = 90);
+
+ virtual void setText(string _inputText);
+ virtual void setScale(float _scale){ scale = _scale; };
+ virtual void setRadius(float _radius){ radius = _radius; _loadWords(); };
+ virtual void setAngle(float _angle){ angle = _angle; _loadWords(); };
+ virtual void setAlignment(ofxHorizontalAlignment _hAlignment , ofxVerticalAlignment _vAlignment = OF_TEXT_ALIGN_TOP);
+
+ virtual int length(){return rawText.length();};
+ virtual void clear(){ rawText.clear();};
+ virtual ofxTextArc& operator += (string _text){ setText(rawText + _text);};
+ virtual ofxTextArc& operator = (string _text){ setText(_text);};
+ virtual ofxTextArc& operator -- (){ if( rawText.length()>0){ rawText.erase( rawText.begin() ); setText(rawText); }};
+ virtual ofxTextArc& operator -- (int){ if( rawText.length()>0){ rawText.erase(rawText.end()-1); setText(rawText); }};
+
+ virtual void draw(float _x, float _y);
+
+protected:
+ virtual void _loadWords();
+ ofxTextFont font;
+ ofxHorizontalAlignment hAlignment;
+ ofxVerticalAlignment vAlignment;
+ letterArc *letters;
+ string rawText;
+ float angle, totalAngle, radius;
+ float scale;
};
#endif
View
212 src/ofxTextBlock.cpp
@@ -20,40 +20,70 @@
#include "ofxTextBlock.h"
ofxTextBlock::ofxTextBlock(){
+ x = 0;
+ y = 0;
+ width = ofGetWidth();
+ height = ofGetHeight();
+
scale = 1.0f;
bWraping = false;
hAlignment = OF_TEXT_ALIGN_LEFT;
vAlignment = OF_TEXT_ALIGN_TOP;
}
-void ofxTextBlock::init(string fontLocation, float fontSize){
- font.loadFont(fontLocation, fontSize, true, true);
- font.setGlobalDpi(90);
+void ofxTextBlock::loadFont(string _fontLocation, float _fontSize, int _dpi){
+ font.loadFont(_fontLocation, _fontSize, true, true);
+ font.setGlobalDpi(_dpi);
//Set up the blank space word
+ //
blankSpaceWord.rawWord = " ";
blankSpaceWord.width = font.stringWidth ("x");
blankSpaceWord.height = font.stringHeight("i");
}
void ofxTextBlock::setText(string _inputText){
+
rawText = _inputText;
+
+ // Replace acent and other special characters for it ascii code
+ //
+ subsChars(rawText);
+
+ // Process words extractint width in order to arrange the lines in the specify format
+ //
_loadWords();
- _wrapTextForceLines(1);
+ //_wrapTextForceLines(1);
- if ( !bWraping ) {
- _wrapTextX(width);
- } else {
+ // If wrapping it´s on it try to force the sacale in order to fill all the space area
+ //
+ if ( bWraping )
_wrapTextArea(width, height);
- }
+ else
+ _wrapTextX(width);
}
-ofxTextBlock& ofxTextBlock::setAlignment(ofxHorizontalAlignment _hAlignment, ofxVerticalAlignment _vAlignment){
+void ofxTextBlock::setAlignment(ofxHorizontalAlignment _hAlignment, ofxVerticalAlignment _vAlignment){
hAlignment = _hAlignment;
vAlignment = _vAlignment;
+}
+
+// If the user specify a position and shape it will change the x, y, width and height of the based ofRectangle
+// variables.
+//
+void ofxTextBlock::draw(float _x, float _y, float _w, float _h){
+ if (_h == -1){
+ _h = height;
+
+ if (_w == -1)
+ _w = width;
+ }
- return * this;
+ set(_x,_y,_w,_h);
+ setText(rawText);
+
+ draw();
}
void ofxTextBlock::draw(){
@@ -63,7 +93,6 @@ void ofxTextBlock::draw(){
yAlig = y + height - getTextHeight();
} else if (vAlignment == OF_TEXT_ALIGN_MIDDLE){
yAlig = getCenter().y - getTextHeight()*0.5;
- cout << "seting middle" << endl;
}
if (hAlignment == OF_TEXT_ALIGN_LEFT){
@@ -218,14 +247,19 @@ void ofxTextBlock::draw(){
void ofxTextBlock::_trimLineSpaces(){
if (words.size() > 0) {
- //Now delete all leading or ending spaces on each line
+
+ // Now delete all leading or ending spaces on each line
+ //
for(int l=0;l < lines.size(); l++){
- //Delete the first word if it is a blank
+
+ // Delete the first word if it is a blank
+ //
if (lines[l].wordsID.size() > 0){
if (words[lines[l].wordsID[0]].rawWord == " ") lines[l].wordsID.erase(lines[l].wordsID.begin());
}
- //Delete the last word if it is a blank
+ // Delete the last word if it is a blank
+ //
if (lines[l].wordsID.size() > 0){
if (words[lines[l].wordsID[lines[l].wordsID.size() - 1]].rawWord == " ") lines[l].wordsID.erase(lines[l].wordsID.end() - 1);
}
@@ -234,6 +268,37 @@ void ofxTextBlock::_trimLineSpaces(){
}
+
+void ofxTextBlock::_loadWords(){
+
+ istringstream iss(rawText);
+
+ vector<string> tokens;
+ copy(istream_iterator<string>(iss),
+ istream_iterator<string>(),
+ back_inserter<vector<string> >(tokens));
+
+ words.clear();
+ wordBlock tmpWord;
+ for(int i = 0; i < tokens.size(); i++){
+ tmpWord.rawWord = tokens.at(i);
+ tmpWord.width = font.stringWidth(tmpWord.rawWord);
+ tmpWord.height = font.stringHeight(tmpWord.rawWord);
+ words.push_back(tmpWord);
+
+ // add spaces into the words vector if it is not the last word.
+ //
+ if (i != tokens.size())
+ words.push_back(blankSpaceWord);
+ }
+
+ for(int i=0;i < words.size(); i++){
+ ofLog(OF_LOG_VERBOSE, "Loaded word: %i, %s\n", i, words[i].rawWord.c_str());
+ }
+
+
+}
+
int ofxTextBlock::_getLinedWords(){
int wordCount = 0;
@@ -247,31 +312,56 @@ int ofxTextBlock::_getLinedWords(){
else return 0;
}
+bool ofxTextBlock::_wrapTextForceLines(int linesN){
+
+ if (words.size() > 0) {
+
+ if (linesN > words.size()) linesN = words.size();
+
+ float lineWidth = _getWidthOfWords() * (1.1f / (float)linesN);
+
+ int curLines = 0;
+ bool bGotLines;
+
+ // keep increasing the line width until we get the desired number of lines.
+ //
+ while (!bGotLines){
+ curLines = _wrapTextX(lineWidth);
+ if (curLines == linesN) return true;
+ if (curLines > linesN) return false;
+ lineWidth-=10;
+ }
+ }
+}
+
void ofxTextBlock::_wrapTextArea(float rWidth, float rHeight){
float tmpScale = 0.0f;
float maxIterations = _getLinedWords();
float scales[1000];
- scale = 1.0f; //Reset the scale for the height and width calculations.
+
+ // Reset the scale for the height and width calculations.
+ //
+ scale = 1.0f;
if (words.size() > 0) {
- //Check each possible line layout and check it will fit vertically
+ // Check each possible line layout and check it will fit vertically
+ //
for (int iteration=1; iteration <= maxIterations; iteration++){
-
- //printf("Iteration %i...\n", iteration);
+
_wrapTextForceLines(iteration);
-
tmpScale = rWidth / getTextWidth();
+
if ((tmpScale * getTextHeight()) < rHeight) {
scales[iteration] = tmpScale;
- }
- else {
+ } else {
scales[iteration] = -1;
}
}
- //Now see which is biggest
+ // Now see which is biggest
+ //
int maxIndex = 1;
bool bScaleAvailable = false;
@@ -284,7 +374,9 @@ void ofxTextBlock::_wrapTextArea(float rWidth, float rHeight){
}
}
- //When only one line is needed an appropriate on the Y scale can sometimes not be found. In these occasions scale the size to the Y dimension
+ // When only one line is needed an appropriate on the Y scale can sometimes not be found.
+ // In these occasions scale the size to the Y dimension
+ //
if (bScaleAvailable) {
scale = scales[maxIndex];
} else {
@@ -301,57 +393,40 @@ void ofxTextBlock::_wrapTextArea(float rWidth, float rHeight){
}
-
-bool ofxTextBlock::_wrapTextForceLines(int linesN){
-
- if (words.size() > 0) {
-
- if (linesN > words.size()) linesN = words.size();
-
- float lineWidth = _getWidthOfWords() * (1.1f / (float)linesN);
-
- int curLines = 0;
- bool bGotLines;
-
- //keep increasing the line width until we get the desired number of lines.
- while (!bGotLines){
- curLines = _wrapTextX(lineWidth);
- if (curLines == linesN) return true;
- if (curLines > linesN) return false;
- lineWidth-=10;
- }
- }
-}
-
-
int ofxTextBlock::_wrapTextX(float lineWidth){
-
+
+ // Reset the scale for the height and width calculations.
+ //
scale = 1.0f;
if (words.size() > 0) {
-
- float runningWidth = 0.0f;
-
+
lines.clear();
-
+ float runningWidth = 0.0f;
bool newLine = true;
lineBlock tmpLine;
tmpLine.wordsID.clear();
int activeLine = 0;
- for(int i=0;i < words.size(); i++){
+ for(int i = 0; i < words.size(); i++){
+
+ // Add words to each line until it fills the total amount of width
+ // available
+ //
runningWidth += words[i].width;
- if (runningWidth <= lineWidth) {
+ if ((runningWidth <= lineWidth) && ( words[i].rawWord.find("\n") )){
newLine = false;
} else {
-
newLine = true;
lines.push_back(tmpLine);
tmpLine.wordsID.clear();
runningWidth = 0.0f + words[i].width;;
activeLine++;
}
+
+ // Store in the line the id of the words
+ //
tmpLine.wordsID.push_back(i);
}
@@ -365,35 +440,6 @@ int ofxTextBlock::_wrapTextX(float lineWidth){
}
-void ofxTextBlock::_loadWords(){
-
- wordBlock tmpWord;
-
- istringstream iss(rawText);
-
- vector<string> tokens;
- copy(istream_iterator<string>(iss),
- istream_iterator<string>(),
- back_inserter<vector<string> >(tokens));
-
- words.clear();
-
- for(int i=0;i < tokens.size(); i++){
- tmpWord.rawWord = tokens.at(i);
- tmpWord.width = font.stringWidth(tmpWord.rawWord);
- tmpWord.height = font.stringHeight(tmpWord.rawWord);
- words.push_back(tmpWord);
- //add spaces into the words vector if it is not the last word.
- if (i != tokens.size()) words.push_back(blankSpaceWord);
- }
-
- for(int i=0;i < words.size(); i++){
- ofLog(OF_LOG_VERBOSE, "Loaded word: %i, %s\n", i, words[i].rawWord.c_str());
- }
-
-
-}
-
float ofxTextBlock::_getWidthOfWords(){
float widthTotal = 0.0f;
View
52 src/ofxTextBlock.h
@@ -23,49 +23,45 @@
#include "ofMain.h"
#include <iterator>
+#include "ofxTextFont.h"
+#include "ofxTextAlignment.h"
+
typedef struct {
- string rawWord;
- float width;
- float height;
+ string rawWord;
+ float width;
+ float height;
} wordBlock;
typedef struct {
- public:
- vector<int> wordsID;
- float width;
- float height;
+ vector<int> wordsID;
+ float width;
+ float height;
} lineBlock;
-//Just a helpful set of enumerated constants.
-enum ofxHorizontalAlignment {
- OF_TEXT_ALIGN_LEFT,
- OF_TEXT_ALIGN_RIGHT,
- OF_TEXT_ALIGN_JUSTIFIED,
- OF_TEXT_ALIGN_CENTER
-};
-
-enum ofxVerticalAlignment {
- OF_TEXT_ALIGN_TOP,
- OF_TEXT_ALIGN_BOTTOM,
- OF_TEXT_ALIGN_MIDDLE
-};
-
class ofxTextBlock: public ofRectangle {
public:
ofxTextBlock();
- void init(string fontLocation, float fontSize);
+ virtual void loadFont(string _fontLocation, float _fontSize, int _dpi = 90);
- void setText(string _inputText);
- ofxTextBlock& setScale(float _scale){ scale = _scale; return * this;};
- ofxTextBlock& setLineHeight(float lineHeight){ font.setLineHeight(lineHeight); return * this;};
- ofxTextBlock& setAlignment(ofxHorizontalAlignment _hAlignment , ofxVerticalAlignment _vAlignment = OF_TEXT_ALIGN_TOP);
- ofxTextBlock& setWrapping(bool _bWraping){ bWraping = _bWraping; return * this; };
+ virtual void setText(string _inputText);
+ virtual void setScale(float _scale){ scale = _scale; };
+ virtual void setLineHeight(float lineHeight){ font.setLineHeight(lineHeight);};
+ virtual void setAlignment(ofxHorizontalAlignment _hAlignment , ofxVerticalAlignment _vAlignment = OF_TEXT_ALIGN_TOP);
+ void setWrapping(bool _bWraping){ bWraping = _bWraping;};
+
float getTextWidth();
float getTextHeight();
+ int length(){return rawText.length();};
+ void clear(){ rawText.clear();};
+ ofxTextBlock& operator += (string _text){ setText(rawText + _text);};
+ ofxTextBlock& operator = (string _text){ setText(_text);};
+ ofxTextBlock& operator -- (int){ if( rawText.length()>0){ rawText.erase(rawText.end()-1); setText(rawText); } };
+
void draw();
+ void draw(float _x, float _y, float _w = -1, float _h = -1);
protected:
void _loadWords();
@@ -80,7 +76,7 @@ class ofxTextBlock: public ofRectangle {
void _forceScale(float _scale);
- ofTrueTypeFont font;
+ ofxTextFont font;
vector<wordBlock> words;
vector<lineBlock> lines;
View
14 src/ofxTextFont.h
@@ -0,0 +1,14 @@
+//
+// ofxTextFont.h
+// ofxArcText-example
+//
+// Created by Patricio González Vivo on 4/24/12.
+// Copyright (c) 2012 PatricioGonzalezVivo.com. All rights reserved.
+//
+
+#ifndef ofxArcText_example_ofxTextFont_h
+#define ofxArcText_example_ofxTextFont_h
+
+
+
+#endif
View
16 src/ofxTextSequencer.cpp
@@ -27,7 +27,7 @@ bool ofxTextSequencer::load(string _xmlFile){
secondsForChar = XML.getValue("secondForChar", 0.2);
waitingTime = XML.getValue("waitingTime", 2.0);
- textBlock.init( fontFile, fontSize);
+ textBlock.loadFont( fontFile, fontSize);
int totalLines = XML.getNumTags("phrase");
@@ -39,7 +39,7 @@ bool ofxTextSequencer::load(string _xmlFile){
newPhrase.seconds = newPhrase.text.length() * secondsForChar;
newPhrase.speed = XML.getValue("speed", 1.0);
- _subsChars(newPhrase.text);
+ subsChars(newPhrase.text);
string alignment = XML.getValue("hAlign", "LEFT");
if (alignment == "LEFT"){
@@ -84,7 +84,7 @@ void ofxTextSequencer::showMessage(string _message, ofxHorizontalAlignment _hAl
message.hAlign = _hAlign;
message.vAlign = _vAlign;
- _subsChars(message.text);
+ subsChars(message.text);
if (bWaiting){
countDown = 0;
@@ -146,14 +146,4 @@ void ofxTextSequencer::draw(){
countDown -= (1/ofGetFrameRate())*speed;
}
-}
-
-void ofxTextSequencer::_subsChars(string & origString){
- static charSubstitution chars[]={ {"à","\xE0"}, {"á","\xE1"}, {"â","\xE2"}, {"ã","\xE3"}, {"ä","\xE4"}, {"æ","\xE6"}, {"ò","\xF2"},{"ó","\xF3"}, {"ô","\xF4"}, {"õ","\xF5"}, {"ö","\xF6"}, {"ù","\xF9"}, {"ú","\xFA"}, {"û","\xFB"}, {"ü","\xFC"}, {"è","\xE8"}, {"é","\xE9"}, {"ê","\xEA"}, {"ë","\xEB"}, {"ì","\xEC"}, {"í","\xED"}, {"î","\xEE"}, {"ï","\xEF"}, {"ç","\xE7"}, {"Ç","\xC7"} };
-
- for(int i=0; i<24; i++){
- while(origString.find(chars[i].character) !=string::npos){
- origString = origString.substr(0,origString.find(chars[i].character)) + chars[i].code + origString.substr(origString.find(chars[i].character)+2);
- }
- };
}
View
7 src/ofxTextSequencer.h
@@ -19,11 +19,6 @@ typedef struct {
ofxVerticalAlignment vAlign;
} ofxTextPhrase;
-typedef struct{
- string character;
- string code;
-} charSubstitution;
-
class ofxTextSequencer: public ofRectangle {
public:
ofxTextSequencer();
@@ -42,8 +37,6 @@ class ofxTextSequencer: public ofRectangle {
void draw();
private:
- void _subsChars(string & origString);
-
vector<ofxTextPhrase> script;
ofxTextPhrase message;
View
14 src/ofxTextShape.h
@@ -0,0 +1,14 @@
+//
+// ofxTextShape.h
+// ofxArcText-example
+//
+// Created by Patricio González Vivo on 4/26/12.
+// Copyright (c) 2012 PatricioGonzalezVivo.com. All rights reserved.
+//
+
+#ifndef ofxArcText_example_ofxTextShape_h
+#define ofxArcText_example_ofxTextShape_h
+
+
+
+#endif
View
14 src/ofxTextSpiral.h
@@ -0,0 +1,14 @@
+//
+// ofxTextSpiral.h
+// ofxArcText-example
+//
+// Created by Patricio González Vivo on 4/25/12.
+// Copyright (c) 2012 PatricioGonzalezVivo.com. All rights reserved.
+//
+
+#ifndef ofxArcText_example_ofxTextSpiral_h
+#define ofxArcText_example_ofxTextSpiral_h
+
+
+
+#endif
View
5 src/ofxTextSuite.h
@@ -20,8 +20,13 @@
#ifndef OFXTEXTSUITE
#define OFXTEXTSUITE
+#include "ofxTextFont.h"
+#include "ofxTextAlignment.h"
+
#include "ofxTextArc.h"
+#include "ofxTextSpiral.h"
#include "ofxTextBlock.h"
+
#include "ofxTextSequencer.h"
#endif

0 comments on commit ead4994

Please sign in to comment.