PageRenderTime 34ms CodeModel.GetById 20ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/src/ftk_text_layout_normal.c

http://ftk.googlecode.com/
C | 173 lines | 107 code | 36 blank | 30 comment | 36 complexity | a0b3cf1839ec38f5ca56b3bec822cbb6 MD5 | raw file
  1/*
  2 * File: ftk_text_layout_normal.c
  3 *
  4 * Author:  Li XianJing <xianjimli@hotmail.com>
  5 * Brief: simple implementation for text layout interface.
  6 *
  7 * Copyright (c) 2009 - 2010  Li XianJing <xianjimli@hotmail.com>
  8 *
  9 * Licensed under the Academic Free License version 2.1
 10 *
 11 * This program is free software; you can redistribute it and/or modify
 12 * it under the terms of the GNU General Public License as published by
 13 * the Free Software Foundation; either version 2 of the License, or
 14 * (at your option) any later version.
 15 *
 16 * This program is distributed in the hope that it will be useful,
 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 19 * GNU General Public License for more details.
 20 *
 21 * You should have received a copy of the GNU General Public License
 22 * along with this program; if not, write to the Free Software
 23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 24 */
 25
 26/*
 27 * History:
 28 * ================================================================
 29 * 2010-07-18 Li XianJing <xianjimli@hotmail.com> created
 30 *
 31 */
 32#include "ftk_util.h"
 33#include "ftk_text_layout.h"
 34
 35struct _FtkTextLayout
 36{
 37	int pos;
 38	int len;
 39	size_t width;
 40	FtkCanvas* canvas;
 41	const char* text;
 42	FtkWrapMode wrap_mode;
 43	int pos_v2l[FTK_LINE_CHAR_NR+1];
 44};
 45
 46FtkTextLayout* ftk_text_layout_create(void)
 47{
 48	FtkTextLayout* thiz = FTK_NEW(FtkTextLayout);
 49
 50	return thiz;
 51}
 52
 53Ret ftk_text_layout_set_canvas(FtkTextLayout* thiz, FtkCanvas* canvas)
 54{
 55	return_val_if_fail(thiz != NULL && canvas != NULL, RET_FAIL);
 56
 57	thiz->canvas = canvas;
 58
 59	return RET_OK;
 60}
 61
 62Ret ftk_text_layout_set_width(FtkTextLayout* thiz, size_t width)
 63{
 64	return_val_if_fail(thiz != NULL && width > 0, RET_FAIL);
 65
 66	thiz->width = width;
 67
 68	return RET_OK;
 69}
 70
 71Ret ftk_text_layout_set_text(FtkTextLayout* thiz, const char* text, int len)
 72{
 73	return_val_if_fail(thiz != NULL && text != NULL, RET_FAIL);
 74
 75	thiz->pos   = 0;
 76	thiz->text = text;
 77	thiz->len   = len < 0 ? strlen(text) : len;
 78
 79	return RET_OK;
 80}
 81
 82Ret ftk_text_layout_set_wrap_mode(FtkTextLayout* thiz, FtkWrapMode wrap_mode)
 83{
 84	return_val_if_fail(thiz != NULL, RET_FAIL);
 85
 86	thiz->wrap_mode = wrap_mode;
 87
 88	return RET_OK;
 89}
 90
 91Ret ftk_text_layout_init(FtkTextLayout* thiz, const char* text, int len, FtkCanvas* canvas, size_t width)
 92{
 93	return_val_if_fail(thiz != NULL && text != NULL && canvas != NULL && width > 0, RET_FAIL);
 94
 95	thiz->pos   = 0;
 96	thiz->text  = text;
 97	thiz->canvas  = canvas;
 98	thiz->width = width;
 99	thiz->wrap_mode = FTK_WRAP_NONE;
100	thiz->len   = len < 0 ? strlen(text) : len;
101
102	return RET_OK;
103}
104
105Ret ftk_text_layout_skip_to(FtkTextLayout* thiz, int pos)
106{
107	return_val_if_fail(thiz != NULL && pos >= 0 && pos < thiz->len, RET_FAIL);
108
109	thiz->pos = pos;
110
111	return RET_OK;
112}
113
114
115Ret ftk_text_layout_get_visual_line(FtkTextLayout* thiz, FtkTextLine* line)
116{
117	int i = 0;
118	int extent = 0;
119	const char* end = NULL;
120	return_val_if_fail(thiz != NULL && line != NULL, RET_FAIL);
121
122	if(thiz->pos >= thiz->len)
123	{
124		return RET_FAIL;
125	}
126
127	line->pos_v2l = thiz->pos_v2l;
128	line->attr = FTK_TEXT_ATTR_NORMAL;
129	line->text = thiz->text + thiz->pos;
130	end = ftk_canvas_calc_str_visible_range(thiz->canvas, thiz->text, thiz->pos, -1, thiz->width, &extent);
131
132	if(thiz->wrap_mode == FTK_WRAP_WORD)
133	{
134		const char* start = thiz->text + thiz->pos;
135		const char* new_end = ftk_line_break(start, end);
136
137		if(end != new_end && new_end > (start + 2))
138		{
139			end = new_end;
140			extent = ftk_canvas_get_str_extent(thiz->canvas, start, end - start);
141		}
142	}
143
144	line->len = end - line->text;
145	line->xoffset = 0;
146	line->extent = extent;
147
148	for(i = 0; i < line->len && i < FTK_LINE_CHAR_NR; i++)
149	{
150		line->pos_v2l[i] = i + thiz->pos;
151	}
152	thiz->pos += line->len;
153
154	for(i = line->len - 1; i >= 0; i--)
155	{
156		if(line->text[i] == '\r' || line->text[i] == '\n')
157			line->len--;
158	}
159
160	return RET_OK;
161}
162
163void ftk_text_layout_destroy(FtkTextLayout* thiz)
164{
165	if(thiz != NULL)
166	{
167		FTK_FREE(thiz);
168	}
169
170	return;
171}
172
173