Skip to content

Commit

Permalink
Added bounds to SdfTextMesh. Updated MeshPages samples to include bou…
Browse files Browse the repository at this point in the history
…nds drawing.
  • Loading branch information
Hai Nguyen committed Oct 7, 2016
1 parent 4f4e56e commit 1c89bdb
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 11 deletions.
5 changes: 5 additions & 0 deletions include/cinder/gl/SdfTextMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class SdfTextMesh {
void setWrapped( bool value ) { mOptions.setWrapped( value ); }
const Rectf& getFitRect() const { return mOptions.getFitRect(); }
void setFitRect( const Rectf &value ) { mOptions.setFitRect( value ); }
const Rectf& getBounds() const { return mBounds; }
private:
Run( SdfTextMesh *sdfTextMesh, const std::string& utf8, const SdfTextRef& sdfText, const vec2 &baseline, const Run::Options &drawOptions );
Run( SdfTextMesh *sdfTextMesh, const std::string& utf8, const SdfTextRef& sdfText, const Rectf &fitRect, const Run::Options &drawOptions );
Expand All @@ -150,6 +151,7 @@ class SdfTextMesh {
uint32_t mDirty = Feature::NONE;
std::string mUtf8;
Run::Options mOptions;
Rectf mBounds = Rectf( 0, 0, 0, 0 );
};

virtual ~SdfTextMesh() {}
Expand All @@ -162,6 +164,9 @@ class SdfTextMesh {
SdfTextMesh::RunRef appendTextWrapped( const std::string &utf8, const SdfTextRef &sdfText, const Rectf &fitRect, const Run::Options &options = Run::Options() );
SdfTextMesh::RunRef appendTextWrapped( const std::string &utf8, const SdfText::Font &font, const Rectf &fitRect, const Run::Options &options = Run::Options() );

//! Returns the runs associated with \a sdfText. If \a sdfText is null, all runs gets returned.
std::vector<RunRef> getRuns( const SdfTextRef &sdfText = SdfTextRef() ) const;

void cache();

void draw( bool premultiply = true, float gamma = 2.2f );
Expand Down
Binary file modified samples/MeshPages/assets/Alike.sdft
Binary file not shown.
21 changes: 20 additions & 1 deletion samples/MeshPages/src/MeshPagesApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ class MeshPagesApp : public App {
gl::SdfText::Font mFont;
gl::SdfTextRef mSdfText;
gl::SdfTextMeshRef mSdfTextMesh;
bool mPremultiply = false;
bool mPremultiply = true;
gl::SdfText::Alignment mAlignment = gl::SdfText::Alignment::LEFT;
bool mJustify = true;
bool mDrawBounds = false;

std::vector<gl::SdfTextMesh::RunRef> mPageNumRuns;
std::vector<gl::SdfTextMesh::RunRef> mPageTextRuns;
Expand Down Expand Up @@ -134,6 +135,11 @@ void MeshPagesApp::keyDown( KeyEvent event )
}
break;

case 'b':
case 'B':
mDrawBounds = ! mDrawBounds;
break;

case '1': moveToPage( 1 ); break;
case '2': moveToPage( 2 ); break;
case '3': moveToPage( 3 ); break;
Expand Down Expand Up @@ -163,6 +169,19 @@ void MeshPagesApp::draw()

auto drawOptions = gl::SdfText::DrawOptions().premultiply( mPremultiply );

if( mDrawBounds ) {
gl::ScopedModelMatrix scopedModel;
gl::translate( mOffset.value() );

auto runs = mSdfTextMesh->getRuns();
gl::lineWidth( 1.0f );
gl::color( Color( 0.1f, 0.8f, 0.9f ) );
for( const auto &run : runs ) {
const auto &bounds = run->getBounds();
gl::drawStrokedRect( bounds );
}
}

gl::color( Color( 0.1f, 0.1f, 0.1f ) );
drawOptions.scale( 2.0f );
mSdfText->drawString( "Gulliver's Travels into Several Remote Nations of the World", vec2( 1.5f * kPageBorder, 2.5f * kPageBorder ), drawOptions );
Expand Down
2 changes: 1 addition & 1 deletion samples/MeshPages/vc2013/MeshPages.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{1DBAE4DA-7CC4-4BDD-904D-971A2901C3DE}</ProjectGuid>
<ProjectGuid>{022DEB01-A1F9-438F-9A33-3204F8C04816}</ProjectGuid>
<RootNamespace>Basic</RootNamespace>
<Keyword>Win32Proj</Keyword>
</PropertyGroup>
Expand Down
44 changes: 35 additions & 9 deletions src/cinder/gl/SdfTextMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,23 @@ void SdfTextMesh::appendText( const SdfTextMesh::RunRef &run )
updateDirty( run.get() );
}

std::vector<SdfTextMesh::RunRef> SdfTextMesh::getRuns( const SdfTextRef &sdfText ) const
{
std::vector<SdfTextMesh::RunRef> result;
if( sdfText ) {
auto it = mRunMaps.find( sdfText );
if( mRunMaps.end() != it ) {
result = it->second;
}
}
else {
for( const auto &it : mRunMaps ) {
std::copy( std::begin( it.second ), std::end( it.second ), std::back_inserter( result ) );
}
}
return result;
}

void SdfTextMesh::updateFeatures( const Run *run )
{
const auto& sdfText = run->getSdfText();
Expand Down Expand Up @@ -253,9 +270,9 @@ void SdfTextMesh::cache()
return;
}

for( auto& runMapIt : mRunMaps ) {
auto& sdfText = runMapIt.first;
auto& runs = runMapIt.second;
for( auto &runMapIt : mRunMaps ) {
auto &sdfText = runMapIt.first;
auto &runs = runMapIt.second;

// Get textures
std::vector<Texture2dRef> textures;
Expand All @@ -269,24 +286,33 @@ void SdfTextMesh::cache()

std::unordered_map<RunRef, std::pair<uint32_t, uint32_t>> runVertRanges;
std::unordered_map<Texture2dRef, ClientMesh> texToMesh;
for( const auto& run : runs ) {
for( const auto &run : runs ) {
std::pair<uint32_t, uint32_t> vertRange = std::make_pair( 0, 0 );
const auto& options = run->getOptions();
const auto &options = run->getOptions();
auto &bounds = run->mBounds;
std::vector<std::pair<uint8_t, std::vector<SdfText::CharPlacement>>> placements;
if( run->getWrapped() ) {
placements = sdfText->placeStringWrapped( run->getUtf8(), run->getFitRect(), vec2( run->getPosition() ), options.getDrawOptions() );
const Rectf &fitRect = run->getFitRect();
vec2 offset = vec2( run->getPosition() );
placements = sdfText->placeStringWrapped( run->getUtf8(), fitRect, offset, options.getDrawOptions() );
bounds = sdfText->measureStringBoundsWrapped( run->getUtf8(), fitRect, options.getDrawOptions() );
bounds += vec2( fitRect.x1, fitRect.y1 );
bounds += offset;
}
else {
placements = sdfText->placeString( run->getUtf8(), vec2( run->getBaseline() ), options.getDrawOptions() );
vec2 baseline = vec2( run->getBaseline() );
placements = sdfText->placeString( run->getUtf8(), baseline, options.getDrawOptions() );
bounds = sdfText->measureStringBounds( run->getUtf8(),options.getDrawOptions() );
bounds += baseline;
}
for( const auto& placementsIt : placements ) {
for( const auto &placementsIt : placements ) {
Texture2dRef tex = textures[placementsIt.first];
const auto& charPlacements = placementsIt.second;
if( charPlacements.empty() ) {
continue;
}

auto& mesh = texToMesh[tex];
auto &mesh = texToMesh[tex];
vertRange.first = static_cast<uint32_t>( mesh.getNumIndices() );
for( const auto& place : charPlacements ) {
const auto& srcTexCoords = place.mSrcTexCoords;
Expand Down

0 comments on commit 1c89bdb

Please sign in to comment.