aboutsummaryrefslogtreecommitdiffstats
path: root/mainwindow.cpp
blob: 5173fda5c0c184a73144e620d2b70624c7f69571 (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
** Arabtype Copyright 2012-2015(c) Wael El Oraiby. All Rights Reserved
**
** This file is in public domain
*/

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtGui>
#include <cassert>
#include <iostream>
#include <vector>
#include <string>
#include <fstream>

#include "utf8.h"
#include "arabtype.h"

FT_Library MainWindow::ftlib__	= NULL;



uint32_t	get_arabic_form(const std::vector<uint32_t>& code_point, uint32_t idx)
{
	uint	ch	= code_point[idx];
	uint	prev	= 0;
	uint	next	= 0;

	if( idx )
		prev	= code_point[idx - 1];
	if( idx < code_point.size() - 1 )
		next	= code_point[idx + 1];

	return get_presentation_form_b(prev, next, ch);
}

std::vector<uint32_t> decode(const std::vector<uint8_t>& in)
{
	std::vector<uint>	ret;
	uint codep	= 0;
	uint state	= 0;

	for( size_t i = 0; i < in.size(); ++i )
	{
		if( decode(&state, &codep, in[i]) == UTF8_ACCEPT )
			ret.push_back(codep);
	}
	if( state != UTF8_ACCEPT )
		std::cout << "The string is not well-formed" << std::endl;

	return ret;
}

MainWindow::MainWindow(QWidget *parent) :
	QMainWindow(parent),
	ui__(new Ui::MainWindow)
{
	ui__->setupUi(this);

	int	width	= 480;
	int	height	= 320;
	int	font_size	= 24;

	std::ifstream	ifs("arabic_test.txt");
	assert( ifs.is_open() );
	std::vector<uchar>	arabic_string;

	while( ifs.good() )
	{
		uchar ch = ifs.get();
		if( ifs.good() )
			arabic_string.push_back(ch);
	}

	std::cout << "size: " << arabic_string.size() << std::endl;
	ifs.close();

	std::vector<uint> arabic_cp	= decode(arabic_string);

	for( size_t i = 0; i < arabic_cp.size(); ++i )
		std::cout << "0x" << std::hex << arabic_cp[i] << std::endl;

	std::cout << std::dec;

	if( !ftlib__ )
	{
		FT_Error	error = FT_Init_FreeType(&ftlib__);
		assert( !error && "Error loading freetype" );
	}

	// load the font
	FT_Face		face;
	FT_Error	error;

	error		= FT_New_Face(ftlib__, "ae_Tholoth.ttf", 0, &face);
	assert( !error && "unable to load font file" );

	// set font size
	error		= FT_Set_Pixel_Sizes(face, 0, font_size);
	assert( !error && "Error setting font size" );

	// setup glyph
	// render chars
	FT_GlyphSlot	slot		= face->glyph;

	FT_Int32	load_flags	= FT_LOAD_DEFAULT | FT_LOAD_FORCE_AUTOHINT;
	FT_Render_Mode	render_flags	= FT_RENDER_MODE_NORMAL;


	data__	= new uchar[width * height * 4];
	memset(data__, 0, width * height * 4);
	int	col	= width - font_size;
	int	line	= font_size;
	QImage	img(data__, width, height, QImage::Format_RGB32);
	// render the arabic glyphs
	for( size_t idx = 0; idx < arabic_cp.size(); ++idx )
	{
		uint ch	= get_arabic_form(arabic_cp, idx);
		if( ch == 0xA ) {
			line	+= font_size;
			col	= width - font_size;
			continue;
		}

		if ( ch == 0xC ) {
			continue;
		}

		if ( ch == -1 ) {
			continue;
		}

		assert(ch != 0);

		int glyph_index	= FT_Get_Char_Index(face, ch);
		assert( glyph_index && "invalid character" );

		error	= FT_Load_Glyph(face, glyph_index, load_flags);
		assert( !error && "couldn't load glyph" );

		error = FT_Render_Glyph(slot, render_flags);
		assert( !error && "couldn't render glyph" );

		FT_Glyph glyph;
		FT_Get_Glyph(slot, &glyph);

		FT_BBox	box;
		FT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_TRUNCATE, &box);

		int	temp_col	= col - (slot->advance.x >> 6);

		for( int y = 0; y < slot->bitmap.rows; y++ )
		{
			for( int x = 0; x < slot->bitmap.width; x++ )
			{
				uchar	co	= slot->bitmap.buffer[x + y * slot->bitmap.pitch];
				if( co && x + temp_col < width && x + temp_col >= 0 )
				{
					uint rgb = img.pixel(x + temp_col + slot->bitmap_left, y - slot->bitmap_top + line);
					img.setPixel(x + temp_col + slot->bitmap_left, y - slot->bitmap_top + line, rgb | qRgb(co, co, co));
				}

				//std::cout << uint(co) << " ";
			}
			//std::cout << std::endl;
		}

		//std::cout << std::endl;

		col -= slot->advance.x >> 6;

		FT_Done_Glyph(glyph);
	}


	QPixmap	pixmap = QPixmap::fromImage(img);
	ui__->label->setPixmap(pixmap);

	FT_Done_Face(face);
}

MainWindow::~MainWindow()
{
	FT_Done_FreeType(ftlib__);
	delete ui__;
	delete data__;
}

void MainWindow::paintEvent ( QPaintEvent * event )
{
}