Skip to content

Instantly share code, notes, and snippets.

@pmiddend
Created June 27, 2015 08:49
Show Gist options
  • Save pmiddend/f5666b8ad1437868e6b5 to your computer and use it in GitHub Desktop.
Save pmiddend/f5666b8ad1437868e6b5 to your computer and use it in GitHub Desktop.
/**
* Copyright (c) 2010-2010 Andrey AndryBlack Kunitsyn
* email:support.andryblack@gmail.com
*
* Report bugs and download new versions at http://code.google.com/p/fontbuilder
*
* This software is distributed under the MIT License.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include "monoboxlayouter.h"
#include "../layoutdata.h"
#include <cmath>
#include <iostream>
#include <algorithm>
MonoBoxLayouter::MonoBoxLayouter(QObject *parent) :
AbstractLayouter(parent)
{
}
struct Line {
int min_y;
int max_y;
int max_h;
int y;
Line() : min_y(0),max_y(0),max_h(0),y(0) {}
Line(const LayoutChar& c,int const max_h) : max_h(max_h),y(0) {
std::cout << "in line: " << this->y << "\n";
min_y = c.y;
max_y = c.y + max_h;
//chars.push_back(&c);
}
int h() const { return max_y - min_y;}
int gety() const { std::cout << "in getter: " << this->y << "\n"; return this->y; }
void append(const LayoutChar& c) {
if (c.y < min_y) {
min_y = c.y;
max_y = c.y+max_h;
}
chars.push_back(&c);
}
QVector<const LayoutChar*> chars;
};
void MonoBoxLayouter::PlaceImages(const QVector<LayoutChar>& chars) {
int h = 0;
int w = 0;
if (chars.isEmpty()) return;
/// speed up
int area = 0;
int maxw = -1;
int maxh = -1;
foreach (const LayoutChar& c, chars) {
maxw = std::max(c.w,maxw);
maxh = std::max(c.h,maxh);
}
foreach (const LayoutChar& c, chars)
area+=maxw*maxh;
int dim = ::sqrt(area);
resize(dim,dim);
w = width();
h = height();
QVector<Line> lines;
bool iteration = true;
while (iteration) {
int x = 0;
lines.clear();
lines.push_back(Line(chars.front(),maxh));
std::cout << "line size: " << lines.size() << "\n";
std::cout << "initial line y: " << lines[0].gety() << "\n";
iteration = false;
foreach (const LayoutChar& c, chars) {
if ((x+maxw)>w) {
x = 0;
int y = lines.back().y;
int h = lines.back().h();
lines.push_back(Line(c,maxh));
lines.back().y = y + h;
std::cout << "new line y: " << lines.back().y << "\n";
}
if ( (lines.back().y+maxh)>h ) {
std::cout << "line y " << (lines.back().y+maxh) << " greater than height " << h << "\n";
if (w>h) {
resize(width(),lines.back().y+maxh);
h=height();
}
else {
resize(width()+maxw,height());
w=width();
}
iteration = true;
break;
}
/// place
lines.back().append(c);
x+=maxw;
}
}
w = width();
h = height();
int x = 0;
foreach (const Line& line, lines) {
x = 0;
foreach (const LayoutChar* c , line.chars ) {
LayoutChar l = *c;
l.x = x;
l.y = line.y + (c->y-line.min_y);
place(l);
x+=maxw;
}
}
}
AbstractLayouter* MonoBoxLayouterFactoryFunc (QObject* parent) {
return new MonoBoxLayouter(parent);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment