Skip to content

Commit

Permalink
Code restructuring.
Browse files Browse the repository at this point in the history
Add text_layout class.
  • Loading branch information
herm committed Jun 28, 2012
1 parent 48dcac1 commit b8ccbe5
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 69 deletions.
6 changes: 4 additions & 2 deletions harfbuzz-test.pro
Expand Up @@ -6,9 +6,11 @@ INCLUDEPATH = /usr/include/freetype2
LIBS += -lharfbuzz
SOURCES += main.cpp scrptrun.cpp \
text_itemizer.cpp \
text_shaping.cpp
text_shaping.cpp \
text_layout.cpp

HEADERS += \
scrptrun.h \
text_itemizer.hpp \
text_shaping.hpp
text_shaping.hpp \
text_layout.hpp
62 changes: 10 additions & 52 deletions main.cpp
@@ -1,64 +1,22 @@
#include "text_itemizer.hpp"
#include "text_layout.hpp"
#include "text_shaping.hpp"

#define HAVE_FREETYPE
#include <iostream>
#include <string>
#include <list>

#include <boost/iostreams/device/mapped_file.hpp>

using namespace std;
namespace mapnik
{


class text_layout
{
public:
text_layout(double text_ratio, double wrap_width);
inline void add_text(UnicodeString const& str, char_properties const& format)
{
itemizer.add_text(str, format);
}

void break_lines();


private:
text_itemizer itemizer;
double text_ratio_;
double wrap_width_;
};

text_layout::text_layout(double text_ratio, double wrap_width) : text_ratio_(text_ratio), wrap_width_(wrap_width)
{
}

} //ns mapnik

int main()
{
mapnik::text_itemizer itemizer;
mapnik::text_layout layout(0.0, 0.0);
mapnik::char_properties dummy;
itemizer.add_text("Hello ", dummy);
itemizer.add_text("World", dummy);
itemizer.add_text("किகே", dummy);
itemizer.add_text("وگرىmixed", dummy);
itemizer.add_text("وگرى", dummy);
std::list<mapnik::text_item> const& list = itemizer.itemize();
std::list<mapnik::text_item>::const_iterator itr = list.begin(), end = list.end();
for (;itr!=end; itr++)
{
std::string s;
itr->str.toUTF8String(s);
std::cout << "Text item: text: " << s << " rtl: " << itr->rtl << "script: " << uscript_getName(itr->script) << "\n";
}
mapnik::text_shaping unifont;
std::cout << "Hello World!\n";
unifont.process_text("Hello World!");
std::cout << "Complex text:\n";
unifont.process_text("कि கே કિ ฐู");
layout.add_text("Hello World", dummy);
layout.add_text("किகே", dummy);
layout.add_text("وگرىmixed", dummy);
layout.add_text("وگرى", dummy);
layout.add_text("German umlauts: äöüß", dummy);
layout.add_text("äöüß", dummy);
layout.shape_text();

return 0;
}

46 changes: 46 additions & 0 deletions text_layout.cpp
@@ -0,0 +1,46 @@
#include "text_layout.hpp"
#include "text_shaping.hpp"

//stl
#include <iostream>

// harf-buzz
#include <harfbuzz/hb.h>

namespace mapnik
{
text_layout::text_layout(double text_ratio, double wrap_width) : text_ratio_(text_ratio), wrap_width_(wrap_width)
{
}

void text_layout::break_lines()
{
}

void text_layout::shape_text()
{
std::list<text_item> const& list = itemizer.itemize();
std::list<text_item>::const_iterator itr = list.begin(), end = list.end();
for (;itr!=end; itr++)
{
std::string s;
text_shaping shaper;
shaper.process_text(itr->str);
hb_buffer_t *buffer = shaper.get_buffer();

unsigned num_glyphs = hb_buffer_get_length(buffer);

hb_glyph_info_t *glyphs = hb_buffer_get_glyph_infos(buffer, NULL);
hb_glyph_position_t *positions = hb_buffer_get_glyph_positions(buffer, NULL);
std::cout << "Processing item '" << itr->str.toUTF8String(s) << "' (" << uscript_getName(itr->script) << "," << itr->str.length() << "," << num_glyphs << ")\n";

for (unsigned i=0; i<num_glyphs; i++)
{
std::cout << "glyph codepoint:" << glyphs[i].codepoint <<
" cluster: " << glyphs[i].cluster <<
" x_advance: "<< positions[i].x_advance << "\n";
}
}
}

} //ns mapnik
28 changes: 28 additions & 0 deletions text_layout.hpp
@@ -0,0 +1,28 @@
#ifndef MAPNIK_TEXT_LAYOUT_HPP
#define MAPNIK_TEXT_LAYOUT_HPP

#include "text_itemizer.hpp"

namespace mapnik
{
class text_layout
{
public:
text_layout(double text_ratio, double wrap_width);
inline void add_text(UnicodeString const& str, char_properties const& format)
{
itemizer.add_text(str, format);
}

void break_lines();
void shape_text();


private:
text_itemizer itemizer;
double text_ratio_;
double wrap_width_;
};
}

#endif // TEXT_LAYOUT_HPP
18 changes: 3 additions & 15 deletions text_shaping.cpp
Expand Up @@ -6,6 +6,7 @@
#include <fstream>

//harf-buzz
#define HAVE_FREETYPE
#include <harfbuzz/hb.h>
#include <harfbuzz/hb-ft.h>

Expand Down Expand Up @@ -41,20 +42,6 @@ void text_shaping::process_text(const UnicodeString &text)
hb_buffer_set_language(buffer, hb_language_from_string (language, -1));
#endif
hb_shape(font_, buffer_, 0 /*features*/, 0 /*num_features*/);
int num_glyphs = hb_buffer_get_length(buffer_);
hb_glyph_info_t *hb_glyph = hb_buffer_get_glyph_infos(buffer_, NULL);
hb_glyph_position_t *hb_position = hb_buffer_get_glyph_positions(buffer_, NULL);
for (int i=0; i<num_glyphs; i++)
{
std::cout << "glyph codepoint:" << hb_glyph[i].codepoint <<
" cluster: " << hb_glyph[i].cluster <<
" mask: " << hb_glyph[i].mask <<
" x_advance: "<< hb_position[i].x_advance <<
" x_advance: "<< hb_position[i].y_advance <<
" x_offset: "<< hb_position[i].x_offset <<
" y_offset: "<< hb_position[i].y_offset << "\n";
// std::cout << "glyph:" << hb_glyph->codepoint << "\n";
}
}

void text_shaping::free_data(void *data)
Expand All @@ -71,7 +58,8 @@ void text_shaping::load_font()
char *font_data;
unsigned int size;

std::ifstream file("./unifont-5.1.20080907.ttf" /*TODO*/, std::ios::in|std::ios::binary|std::ios::ate);
// std::ifstream file("./unifont-5.1.20080907.ttf" /*TODO*/, std::ios::in|std::ios::binary|std::ios::ate);
std::ifstream file("./DejaVuSans.ttf" /*TODO*/, std::ios::in|std::ios::binary|std::ios::ate);
if (file.is_open())
{
size = file.tellg();
Expand Down
2 changes: 2 additions & 0 deletions text_shaping.hpp
Expand Up @@ -7,6 +7,7 @@
#include <unicode/unistr.h>
class hb_font_t;
class hb_buffer_t;
class hb_glyph_info_t;

namespace mapnik
{
Expand All @@ -19,6 +20,7 @@ class text_shaping
~text_shaping();

void process_text(UnicodeString const& text);
hb_buffer_t *get_buffer() { return buffer_; }

protected:
static void free_data(void *data);
Expand Down

0 comments on commit b8ccbe5

Please sign in to comment.