summaryrefslogtreecommitdiffstats
path: root/ksvg/impl/libs/libtext2path/src/Converter.cpp
blob: 2085e0038a62311c5a587b64146b72af6b714e4f (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*
    Copyright (C) 2003 Nikolas Zimmermann <wildfox@kde.org>
    This file is part of the KDE project

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    aint with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "config.h"

#include <math.h>

#include "myboost/shared_ptr.hpp"
#include <fontconfig/fontconfig.h>
#include <fribidi/fribidi.h>

#ifdef FRIBIDI_NEW_FILENAME
#include <fribidi/fribidi-types.h>
#else
#include <fribidi/fribidi_types.h>
#endif

#include "Font.h"
#include "Glyph.h"
#include "Tools.h"
#include "Rectangle.h"
#include "Converter.h"
#include "QtUnicode.h"
#include "GlyphTracer.h"

// Macros
#define FT_TRUNC(x)		((x) >> 6)
#define FT_TOFLOAT(x)	((x) * (1.0 / 64.0))
#define FT_FROMFLOAT(x)	((int) floor ((x) * 64.0 + 0.5))

const double deg2rad = 0.017453292519943295769; // pi/180

using namespace T2P;

// Font engine. The core component.
Converter::Converter(GlyphTracer *tracer) : m_glyphTracer(tracer)
{
	m_init = false;
	m_kerning = true;
	m_library = 0;
}

Converter::~Converter()
{
	if(m_glyphTracer)
		delete m_glyphTracer;

	m_fontCache.clear();
	m_glyphCache.clear();

	// Close FreeType2 library
	if(m_library)
		FT_Done_FreeType(m_library);
}

void Converter::init()
{
	// Initialize FreeType2
	m_init = (FT_Init_FreeType(&m_library) == 0);
}

bool Converter::ready()
{
	return m_init;
}

void Converter::setKerning(bool mode)
{
	m_kerning = mode;
}

// Lookup font in cache or create new font
SharedFont Converter::requestFont(const FontVisualParams *params)
{
	std::string cacheKey = cacheFontKey(params);
	SharedFont cached = m_fontCache.tqfind(cacheKey);

	// If not available in cache, create new one and cache it :)
	if(cached)
	{
		delete params;
		return cached;
	}
	else
	{
		// Create a new Font
		SharedFont newFont(new Font(this));

		// Load the font
		if(!newFont->load(params))
		{
			delete params;
			return SharedFont();
		}

		// Append to cache
		m_fontCache.insert(cacheKey, newFont);
		return newFont;
	}
}

GlyphAffinePair *Converter::requestGlyph(GlyphRenderParams *params, Rectangle &bbox, Affine &affine, bool onlyLatin)
{
	// 1. Select glyph to have glyphIndex information, 
	// 	  needed to generate the cache lookup key
	selectGlyph(params);

	SharedGlyph cached = m_glyphCache.tqfind(cacheGlyphKey(params));

	// If not available in cache, render new one and cache it :)
	// If we're mixing ie. japanese and latin characters (TTB tqlayout),
	// then we also have to re-calculate the glyph again with the appropriate rotation matrix (Niko)
	if(!cached || !onlyLatin)
		cached = calcGlyph(params, affine, onlyLatin);

	// Correct bezier path by setting up the correct scaling matrix
	// and multiplying with the current glyph affine
	double fontSize = params->font()->fontParams()->size();

	T2P::Affine correctAffine;
	correctAffine.scale(0.001 * fontSize, -0.001 * fontSize);
	correctAffine *= affine;

	// Get bbox information
	Point p1(FT_TRUNC(cached->ftBbox()->xMin), FT_TRUNC(cached->ftBbox()->yMin));
	Point p2(FT_TRUNC(cached->ftBbox()->xMax), FT_TRUNC(cached->ftBbox()->yMax));

	bbox.setA(affine.mapPoint(p1));
	bbox.setB(affine.mapPoint(p2));

	return new GlyphAffinePair(cached.get(), correctAffine);
}

void Converter::selectGlyph(GlyphRenderParams *params)
{
	// 1. Select glyph + test if it exists
	//		|
	//		|-> if it doesn't exist, replace by a '?' and check if exists
	//		|-> if it does exist, use it!
	params->setGlyphIndex(FT_Get_Char_Index(params->font()->fontFace(), (FT_ULong) params->character()));

	if(params->glyphIndex() == 0)
		params->setGlyphIndex(FT_Get_Char_Index(params->font()->fontFace(), '?'));

	// 2. Load glyph into font's glyphSlot, according to the GlyphLayoutParams
	int flags = FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING | FT_LOAD_NO_BITMAP;

	// 3. Don't pass FT_LOAD_VERTICAL_LAYOUT on TTB layouts when rendering
	// 	  a latin glyph because it needs to be rotated...
	if(params->tqlayout()->tb())
	{
		Script script;
		SCRIPT_FOR_CHAR(script, params->character())
		if(script != Latin || params->tqlayout()->glyphOrientationVertical() == 0)
			flags |= FT_LOAD_VERTICAL_LAYOUT;
	}

	FT_Error error = FT_Load_Glyph(params->font()->fontFace(), params->glyphIndex(), flags);
	if(error)
		params->setGlyphIndex(0);
}
	
SharedGlyph Converter::calcGlyph(const GlyphRenderParams *params, Affine &affine, bool onlyLatin)
{
	// 2. Apply kerning if needed
	if(m_kerning && params->lastGlyph() != 0 && params->glyphIndex() != 0)
	{
		FT_Vector kerningVector;
		double kx, ky;

		FT_Get_Kerning(params->font()->fontFace(), params->lastGlyph(), params->glyphIndex(), ft_kerning_default, &kerningVector);

		kx = FT_TRUNC(kerningVector.x);
		ky = FT_TRUNC(kerningVector.y);

		affine.dx() += kx + affine.m21() * ky;

		// Only apply y kerning in TB mode
		if(params->tqlayout()->tb())
			affine.dy() += kx + affine.m22() * ky;
	}

	// 3. Prepare path tracing
	FT_OutlineGlyph ftGlyph;
	FT_Get_Glyph(params->font()->fontFace()->glyph, reinterpret_cast<FT_Glyph *>(&ftGlyph));

	// 3a. Prepare tracing matrix
	Affine traceAffine;
	traceAffine.scale(1000.0 / params->font()->fontFace()->units_per_EM);

	// 3b. Enable character rotation, if needed
	if(params->tqlayout()->tb())
	{
		Script script;
		SCRIPT_FOR_CHAR(script, params->character())
		if(!onlyLatin && script == Latin)
		{
			FT_Matrix matrix;

			double angle = deg2rad * params->tqlayout()->glyphOrientationVertical();
			matrix.xx = (FT_Fixed)( cos(angle) * 0x10000L);
			matrix.xy = (FT_Fixed)(-sin(angle) * 0x10000L);
			matrix.yx = (FT_Fixed)( sin(angle) * 0x10000L);
			matrix.yy = (FT_Fixed)( cos(angle) * 0x10000L);

			FT_Glyph_Transform(reinterpret_cast<FT_Glyph>(ftGlyph), &matrix, 0);
		}
	}

	// 4. Begin path tracing
	//    - Setup glyphOutlineing glyph
	//    - Start tracing
	//    - Close path
	FT_Outline *ftOut = &ftGlyph->outline;

	SharedGlyph glyphOutline(new Glyph());
	glyphOutline->setBezierPath(m_glyphTracer->allocBezierPath(ftOut->n_points * 2 + ftOut->n_contours + 1));
	glyphOutline->setAffine(traceAffine);

	FT_Outline_Decompose(ftOut, m_glyphTracer->outlineFuncs(), glyphOutline.get());

	m_glyphTracer->closePath(glyphOutline.get());

	// 5. Determine bounding box (aka "cbox")
	FT_Glyph_Get_CBox(reinterpret_cast<FT_Glyph>(ftGlyph), ft_glyph_bbox_unscaled, glyphOutline->ftBbox());

	// 6. Cache glyph!
	m_glyphCache.insert(cacheGlyphKey(params), glyphOutline);

	FT_Done_Glyph(reinterpret_cast<FT_Glyph>(ftGlyph));

	return glyphOutline;
}

GlyphSet *Converter::calcString(Font *font, const unsigned short *text, unsigned int length, Affine &affine, const GlyphLayoutParams *params, BezierPath *bpath)
{
	unsigned short *bidi = 0;

	// 0. Apply BiDi Rules
	if(params->useBidi())
	{
		FriBidiCharType baseDir = FRIBIDI_TYPE_N;
		FriBidiChar *tqunicodeIn = new FriBidiChar[length + 1];
		FriBidiChar *tqunicodeOut = new FriBidiChar[length + 1];
		bidi = new unsigned short[length + 1];	

		for(unsigned int i = 0; i < length; i++)
			tqunicodeIn[i] = text[i];
	
		fribidi_log2vis(tqunicodeIn, length, &baseDir, tqunicodeOut, 0, 0, 0); 
	
		for(unsigned int i = 0; i < length; i++)
			bidi[i] = tqunicodeOut[i];

		bidi[length] = 0;
		delete []tqunicodeIn;
		delete []tqunicodeOut;
	}
	else
		bidi = const_cast<unsigned short *>(text);

	// Detect character rotation if needed,
	// probably working in 0.1% of the cases :/
	Script script;	
	bool onlyLatin = true;
	for(unsigned int i = 0; i < length; i++)
	{
		SCRIPT_FOR_CHAR(script, bidi[i])
		if(script != Latin)
		{
			onlyLatin = false;
			break;
		}
	}

	// 1. Set initial font size
	double fontSize = font->fontParams()->size();
	FT_Set_Char_Size(font->fontFace(), FT_FROMFLOAT(fontSize), FT_FROMFLOAT(fontSize), 0, 0);

	// 2. Compute positioning metrics
	//	  - See http://www.freetype.org/freetype2/docs/tutorial/stepX.html (X = 1 or 2)
	int pixelHeight = (int) (FT_TOFLOAT(font->fontFace()->size->metrics.ascender - font->fontFace()->size->metrics.descender) * affine.m22());
	int pixelBaseline = (int) (FT_TOFLOAT(font->fontFace()->size->metrics.ascender) * affine.m22());
	int pixelUnderlinePosition = (int) (((font->fontFace()->ascender - font->fontFace()->underline_position - font->fontFace()->underline_thickness / 2) * fontSize / font->fontFace()->units_per_EM) * affine.m22());
	int pixelUnderlineThickness = T2PMAX(1, (int) ((font->fontFace()->underline_thickness * fontSize / font->fontFace()->units_per_EM) * affine.m22()));

	// 3. Prepare needed variables for the rendering loop
	// 	  - rendering params (tqlayout, font...)
	// 	  - bounding box (per glyph, overall)
	// 	  - glyph matrix (overall)
	// 	  - resulting glyph sets
	GlyphRenderParams *renderParams = new GlyphRenderParams();
	renderParams->setFont(font);
	renderParams->setLayout(params);
	renderParams->setLastGlyph(0);
	renderParams->setGlyphIndex(0);

	Affine glyphAffine(affine);
	Rectangle bbox, currentBbox;
	GlyphSet *result = new GlyphSet();

	// Align to path start, if bpath != 0
	Point pathPoint, pathTangent, pathNormal;
	double pathLength = 0, pathAdvance = 0;
	double startOffset = params->textPathStartOffset();

	if(bpath)
	{
		pathLength = bpath->length();
		bpath->pointTangentNormalAt(startOffset, &pathPoint, &pathTangent);

		glyphAffine.dx() = pathPoint.x();
		glyphAffine.dy() = pathPoint.y();
		glyphAffine.rotate(atan2(pathTangent.y(), pathTangent.x()));

		std::cout << "[T2P] Starting textPath at: " << pathPoint.x() << ", " << pathPoint.y() << std::endl;
	}

	// <tspan> elements can have different baseline-shift's
	// baseline-shift support (Niko)
	double baseline = 0;
	if(!params->baselineShift().empty())
	{
		if(params->baselineShift() == "sub")
			baseline += fontSize / 5 + 1;
		else if(params->baselineShift() == "super")
			baseline -= fontSize / 3 + 1;
		else
		{
			std::string temp = params->baselineShift();
			if(temp.find("%") > 0)
				baseline -= (atof(temp.c_str()) / 100.0) * fontSize;
			else
				baseline -= atoi(temp.c_str());
		}

		if(!params->tb())
			glyphAffine.translate(0, baseline);
		else
			glyphAffine.translate(-baseline, 0);
	}

	// 4. Enter main rendering loop
	for(unsigned int i = 0; i < length; i++)
	{
		// 4a. Modify character to render
		renderParams->setCharacter(bidi[i]);

		// 4c. Get glyph outline
		GlyphAffinePair *glyphOutline = requestGlyph(renderParams, currentBbox, glyphAffine, onlyLatin);
		m_glyphTracer->correctGlyph(glyphOutline);

		// 4d. Save advance values
		result->m_glyphXAdvance.push_back((font->fontFace()->glyph->advance.x * fontSize / font->fontFace()->units_per_EM) * affine.m11());
		result->m_glyphYAdvance.push_back((font->fontFace()->glyph->advance.y * fontSize / font->fontFace()->units_per_EM) * affine.m22());

		// 4e. Misc
		//	   - union current + overall bounding box
		//	   - add glyph to list of glyphs
		//	   - apply letter & word spacing
		if(renderParams->glyphIndex() != 0)
		{
			// Remember last glyph for kerning
			renderParams->setLastGlyph(renderParams->glyphIndex());

			// Union current + overall bounding box
			bbox.bboxUnion(bbox, currentBbox);

			// Move glyph locations for the next glyph to be
			// rendered apply letter & word spacing
			bool addGlyph = true;
			if(!params->tb())
			{
				if(bpath)
				{
					double advance = startOffset + (pathAdvance / pathLength);
					if(advance < 0.0 || advance > 1.0) // dont render off-path glyphs
						addGlyph = false;

					pathAdvance += (result->m_glyphXAdvance.back() + params->letterSpacing() * affine.m11()) + ((bidi[(i + 1)] == ' ') ? params->wordSpacing() * affine.m11() : 0);

					std::cout << "[T2P] Adjusting textPath advance: " << pathAdvance << " Path Length: " << pathLength << " StartOffset: " << startOffset << " Position in Path (relative 0-1): " << pathAdvance/pathLength << " Adjusted by offset: " << startOffset + (pathAdvance / pathLength) << std::endl;

					bpath->pointTangentNormalAt(startOffset + (pathAdvance / pathLength), &pathPoint, &pathTangent, &pathNormal);

					glyphAffine.m11() = affine.m11();
					glyphAffine.m12() = affine.m12();
					glyphAffine.m21() = affine.m21();
					glyphAffine.m22() = affine.m22();
					glyphAffine.dx() = pathPoint.x();
					glyphAffine.dy() = pathPoint.y();

					glyphAffine.rotateAround(atan2(pathTangent.y(), pathTangent.x()), Point(result->m_glyphXAdvance.back() / 2, result->m_glyphYAdvance.back() / 2)); 
		
					std::cout << "[T2P] Aligning textPath to: " << pathPoint.x() << ", " << pathPoint.y() << std::endl;
					if(!params->tb())
						glyphAffine.translate(0, baseline);
					else
						glyphAffine.translate(-baseline, 0);
				}
				else
					glyphAffine.dx() += (result->m_glyphXAdvance.back() + params->letterSpacing() * affine.m11()) + ((bidi[(i + 1)] == ' ') ? params->wordSpacing() * affine.m11() : 0);
			}
			else
			{
				double advy = result->m_glyphYAdvance.back();

				Script script;
				SCRIPT_FOR_CHAR(script, bidi[i])
				if(!onlyLatin && script == Latin && params->glyphOrientationVertical() != 0)
					advy = result->m_glyphXAdvance.back();

				glyphAffine.dy() += (advy + params->letterSpacing() * affine.m22()) + ((bidi[(i + 1)] == ' ') ? params->wordSpacing() * affine.m22() : 0);
			}

			// Copy bezier path/affine pair
			if(addGlyph)
			{
				result->m_set.push_back(glyphOutline);
				result->m_glyphCount++;
			}
			else
				delete glyphOutline;
		}
	}

	// 5. Fill out resulting data structures
	result->m_underlinePosition = pixelUnderlinePosition;
	result->m_overlinePosition = pixelHeight - pixelUnderlinePosition;
	result->m_strikeThroughPosition = (result->m_underlinePosition + result->m_overlinePosition) / 2;
	result->m_pixelBaseline = pixelBaseline;
	result->m_underlineThickness = pixelUnderlineThickness;

	result->m_xpen = glyphAffine.dx() - affine.dx();
	result->m_ypen = glyphAffine.dy() - affine.dy();

	result->m_bboxX = T2PMAX(1, int(bbox.a().x()));
	result->m_bboxY = T2PMAX(1, int(bbox.a().y() - int(pixelHeight / 2)));
	result->m_height = T2PMAX(1, int(bbox.b().y() - bbox.a().y()));

	// Correct bounding box information also on
	// vertical layouts! (Niko)
	if(!params->tb())
	{
		if(bpath)
			result->m_width = int(pathAdvance);
		else
			result->m_width = T2PMAX(1, int(bbox.b().x() - bbox.a().x()));
	}
	else
		result->m_width = T2PMAX(1, pixelHeight);

	// 6. Cleanup memory
	if(text != bidi)
		delete []bidi;

	delete renderParams;

	// 7. Eventually, dump cache statistics
	// m_glyphCache.dump();
	// m_fontCache.dump();

	return result;
}

std::string Converter::cacheFontKey(const FontVisualParams *params) const
{
	// TODO: Tune the key
	std::string key;

	key += Tools::joinList('|', const_cast<FontVisualParams *>(params)->fontList());
	key += Tools::a2str(params->weight());
	key += Tools::a2str(params->slant());
	key += Tools::a2str(params->size());

	// std::cout << "Font cache key: " << key << std::endl;
	return key;
}

std::string Converter::cacheGlyphKey(const GlyphRenderParams *params) const
{
	// TODO: Tune the key
	std::string key;

	key += params->font()->fontFile();
	key += Tools::a2str(params->character());
	key += Tools::a2str(params->glyphIndex());
	key += Tools::a2str(params->font()->fontParams()->size());
	key += Tools::a2str(params->font()->fontParams()->weight());
	key += Tools::a2str(params->font()->fontParams()->slant());

	// std::cout << "Glyph cache key: " << key << std::endl;
	return key;
}
// vim:ts=4:noet