Repository navigation
Expand file tree
/
Copy pathSizer.cpp
More file actions
143 lines (124 loc) · 4.95 KB
/
Copy pathSizer.cpp
File metadata and controls
143 lines (124 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Sizer.cpp
* xwall
*
* Created by Tim Medcalf on 16/10/2010.
* Copyright 2010 ErgoThis Ltd. All rights reserved.
*
*/
#include "Sizer.h"
#include "Global.h"
#include <iostream>
#include <math.h>
#include "Rect.h"
using namespace std;
Sizer::Sizer() : surface(cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 0, 0)),
cr(cairo_create(surface)),word_te(),letter_te(),tmp_te()
{
cairo_select_font_face(cr, settings.font.c_str(), settings.font_style, settings.font_weight);
}
Sizer::~Sizer()
{
cairo_destroy (cr);
cairo_surface_destroy(surface);
}
void Sizer::size_word(CloudWord *word)
{
cairo_set_font_size(cr,word->font_size);
cairo_text_extents(cr,word->text.c_str(),&word_te);
if (word_te.width <= 0)
{
cout << "ERROR - NO TEXT EXTENTS FOR WORD: '" << word->text.c_str() << "'" << endl;
}
word->text_extents(word_te);
string tmpword = "";
string letter;
double tmpadvance = -word_te.x_bearing;
word->area = 0;
for (int i = 0; i < word->text.length(); i++)
{
//cout << word->text[i];
letter = word->text[i];
cairo_text_extents(cr,letter.c_str(),&letter_te);
cairo_text_extents(cr,tmpword.c_str(),&tmp_te);
//Glyph* glyph = new Glyph(word,letter,letter_te,tmp_te.width - letter_te.width,0 - word->text_extents().y_bearing + letter_te.y_bearing);
Glyph* glyph = new Glyph(word,letter,letter_te,tmpadvance + letter_te.x_bearing,0 - word->text_extents().y_bearing + letter_te.y_bearing);
tmpadvance += (letter_te.x_advance);
word->letters->push_back(glyph);
//if word has been flagged as large
if (word->large_word)
{
//glyphmap_manager.add_glyph(letter);
//get a map of the glyph we've currently got
GlyphMap *map = glyphmap_manager.glyph_map(letter);
double x_adjust = letter_te.width / (map->width() * 10);
double y_adjust = letter_te.height / (map->height() * 10);
//loop through the map to create rects for each active bit
/*
for (int col = 0; col < map.width(); col ++)
{
for (int row = 0; row < map.height(); row ++)
{
//is this segment active?
if (map.val(col,row))
{
//yes - create a rect that matches that area
Rect* rect;
if (word->rotated())
{
rect = new Rect(glyph->rect()->left + (row * y_adjust), glyph->rect()->top + (col * x_adjust), y_adjust, x_adjust, true, settings.word_border);
rect->inletter_yoffset = (col * x_adjust);
rect->inletter_xoffset = (row * y_adjust);
}
else
{
rect = new Rect(glyph->rect()->left + (col * x_adjust), glyph->rect()->top + (row * y_adjust), x_adjust, y_adjust, true, settings.word_border);
rect->inletter_xoffset = (col * x_adjust);
rect->inletter_yoffset = (row * y_adjust);
}
glyph->letter_rects()->push_back(rect);
word->area += rect->width() * rect->height();
}
}
}
*/
//copy rects from the glyph manager
glyph->letter_rects()->reserve(map->rects->size());
if (word->rotated())
{
for(vector<Rect*>::iterator itG = map->rects->begin(); itG < map->rects->end(); ++itG)
{
Rect *newRect = new Rect(glyph->rect()->left + ((*itG)->y() * y_adjust),glyph->rect()->top + ((*itG)->x() * x_adjust), (*itG)->height() * y_adjust, (*itG)->width() * x_adjust,true, settings.word_border);
newRect->inletter_xoffset = (*itG)->y() * y_adjust;
newRect->inletter_yoffset = (*itG)->x() * x_adjust;
glyph->letter_rects()->push_back(newRect);
word->area += newRect->width() * newRect->height();
}
}
else
{
for(vector<Rect*>::iterator itG = map->rects->begin(); itG < map->rects->end(); ++itG)
{
Rect *newRect = new Rect(glyph->rect()->left + ((*itG)->x() * x_adjust),glyph->rect()->top + ((*itG)->y() * y_adjust), (*itG)->width() * x_adjust, (*itG)->height() * y_adjust,true, settings.word_border);
newRect->inletter_xoffset = (*itG)->x() * x_adjust;
newRect->inletter_yoffset = (*itG)->y() * y_adjust;
glyph->letter_rects()->push_back(newRect);
word->area += newRect->width() * newRect->height();
}
}
}
else //if not a large word then just give one single rect that covers the whole letter
{
Rect* rect = new Rect(glyph->rect()->left, glyph->rect()->top, glyph->rect()->width(), glyph->rect()->height(), true, settings.word_border);
rect->inletter_yoffset = 0;
rect->inletter_xoffset = 0;
glyph->letter_rects()->push_back(rect);
word->area += rect->width() * rect->height();
}
}
//adjust the word are to try to include the border
double tmpheight = word->area / word->width();
word->area = (word->width() + settings.word_border) * (tmpheight + settings.word_border);
word->analyze_core_rect();
word->sized(true);
}