-
-
Notifications
You must be signed in to change notification settings - Fork 355
/
textlayer.c
executable file
·1744 lines (1628 loc) · 49.3 KB
/
textlayer.c
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ***************************************************************************
* textlayer.c -- text bitmap layer processing module.
*
* Copyright (C) 2012-2013 by
* Liu Chao
*
* This file is part of the LCUI project, and may only be used, modified, and
* distributed under the terms of the GPLv2.
*
* (GPLv2 is abbreviation of GNU General Public License Version 2)
*
* By continuing to use, modify, or distribute this file you indicate that you
* have read the license and understand and accept it fully.
*
* The LCUI project 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 GPL v2 for more details.
*
* You should have received a copy of the GPLv2 along with this file. It is
* usually in the LICENSE.TXT file, If not, see <http://www.gnu.org/licenses/>.
* ****************************************************************************/
/* ****************************************************************************
* textlayer.c -- 文本图层处理模块
*
* 版权所有 (C) 2013 归属于
* 刘超
*
* 这个文件是LCUI项目的一部分,并且只可以根据GPLv2许可协议来使用、更改和发布。
*
* (GPLv2 是 GNU通用公共许可证第二版 的英文缩写)
*
* 继续使用、修改或发布本文件,表明您已经阅读并完全理解和接受这个许可协议。
*
* LCUI 项目是基于使用目的而加以散布的,但不负任何担保责任,甚至没有适销性或特
* 定用途的隐含担保,详情请参照GPLv2许可协议。
*
* 您应已收到附随于本文件的GPLv2许可协议的副本,它通常在LICENSE.TXT文件中,如果
* 没有,请查看:<http://www.gnu.org/licenses/>.
* ****************************************************************************/
//#define DEBUG
//#define DEBUG1
//#define DEBUG2
#include <LCUI_Build.h>
#include LC_LCUI_H
#include LC_GRAPH_H
#include LC_WIDGET_H
#include LC_FONT_H
#include <wchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _Special_KeyWord Special_KeyWord;
/*************************** 特殊关键词 *****************************/
struct _Special_KeyWord
{
LCUI_String keyword; /* 关键词 */
LCUI_TextStyle *data; /* 该关键词使用的字体数据 */
LCUI_Queue *text_source_data; /* 关键词所属的源文本 */
};
/******************************************************************/
static void
Destroy_Special_KeyWord( void *arg )
{
/* 在该关键词所属的原文本中查找组成该关键词的字,并修改字所使用的字体数据 */
}
static void
Destroy_CharData( void *arg )
{
}
static void
Destroy_Text_RowData( void *arg )
{
Text_RowData *data;
data = (Text_RowData *)arg;
Queue_Destroy ( &data->string );
}
static void
TextLayer_Clear( LCUI_TextLayer *layer, LCUI_Pos pos,
int max_h, LCUI_CharData *char_ptr )
{
static LCUI_Rect area;
//printf("pos: %d,%d, max_h: %d\n", pos.x, pos.y, max_h);
/* 计算区域范围 */
area.x = pos.x + char_ptr->bitmap->left;
area.y = pos.y + max_h-1;
area.y -= char_ptr->bitmap->top;
area.width = char_ptr->bitmap->width;
area.height = char_ptr->bitmap->rows;
/* 记录需刷新的区域 */
RectQueue_AddToValid( &layer->clear_area, area );
//printf("record area: %d,%d,%d,%d\n", area.x, area.y, area.width, area.height);
}
static void
TextLayer_GetCharBMP ( LCUI_TextStyle *default_style, LCUI_CharData *data )
/* 获取字体位图,字体的样式由文本图层中记录的字体样式决定 */
{
int pixel_size;
/* 获取字体尺寸 */
if( !data->data ) {
pixel_size = default_style->pixel_size;
} else {
/* 如果已经自定义字体尺寸 */
if( data->data->_pixel_size ) {
pixel_size = data->data->pixel_size;
} else {
pixel_size = default_style->pixel_size;
}
}
data->bitmap = Get_ExistFontBMP( default_style->font_id,
data->char_code, pixel_size );
}
static int
TextLayer_Text_Add_NewRow ( LCUI_TextLayer *layer )
/* 添加新行 */
{
Text_RowData data;
/* 单整行最大尺寸改变时,需要移动整行,目前还未支持此功能 */
data.pos = Pos(0,0);
data.max_size = Size(0,0);
data.last_char = NULL;
Queue_Init( &data.string, sizeof(LCUI_CharData), NULL );
/* 使用链表模式,方便数据的插入 */
Queue_SetDataMode( &data.string, QUEUE_DATA_MODE_LINKED_LIST );
/* 队列成员使用指针,主要是引用text_source_data里面的数据 */
Queue_UsingPointer( &data.string );
return Queue_Add( &layer->rows_data, &data );
}
/* 对目标行进行断行处理,也就是将目标行指定位置后面的全部文字转移到另一行 */
static void
TextLayer_Text_RowBreak (
LCUI_TextLayer *layer, Text_RowData *src,
int break_point, Text_RowData *des )
{
static int i, total;
static LCUI_CharData *char_ptr;
total = Queue_GetTotal( &src->string );
for(i=break_point; i<total; ++i ) {
char_ptr = Queue_Get( &src->string, break_point );
Queue_AddPointer( &des->string, char_ptr );
char_ptr->need_update = TRUE;
Queue_DeletePointer( &src->string, break_point );
}
}
static int
TextLayer_Text_Insert_NewRow ( LCUI_TextLayer *layer, int row )
/* 在插入新行至指定位置 */
{
Text_RowData data;
data.pos = Pos(0,0);
data.max_size = Size(0,0);
data.last_char = NULL;
Queue_Init( &data.string, sizeof(LCUI_CharData), NULL );
Queue_SetDataMode( &data.string, QUEUE_DATA_MODE_LINKED_LIST );
Queue_UsingPointer( &data.string );
return Queue_Insert( &layer->rows_data, row, &data );
}
static Text_RowData *
TextLayer_GetCurRowData ( LCUI_TextLayer *layer )
/* 获取指向当前行的指针 */
{
return Queue_Get( &layer->rows_data, layer->current_des_pos.y );
}
static void
TextLayer_Update_RowSize (LCUI_TextLayer *layer, int row )
/* 更新指定行文本位图的尺寸 */
{
int total, i;
LCUI_Size size;
LCUI_CharData *char_data;
Text_RowData *row_data;
LCUI_TextStyle *style;
row_data = Queue_Get( &layer->rows_data, row );
total = Queue_GetTotal( &row_data->string );
style = StyleTag_GetCurrentStyle( &layer->tag_buff );
size = Size(0,14);
if( !style ) {
if(layer->default_data.pixel_size > 0) {
size = Size(0, layer->default_data.pixel_size+2);
}
} else {
if(style->pixel_size > 0) {
size = Size(0, style->pixel_size+2);
}
}
free( style );
for( i=0; i<total; ++i ) {
/* 如果屏蔽字符有效,则使用该字符的数据 */
if( layer->password_char.char_code > 0 ) {
char_data = &layer->password_char;
} else {
char_data = Queue_Get( &row_data->string, i );
if( !char_data ) {
continue;
}
}
size.w += char_data->bitmap->advance.x;
if( char_data->data ) {
if( char_data->data->_pixel_size ) {
if( size.h < char_data->data->pixel_size + 2) {
size.h = char_data->data->pixel_size + 2;
}
} else {
if( size.h < 14) {
size.h = 14;
}
}
} else {
if( size.h < 14) {
size.h = 14;
}
}
}
row_data->max_size = size;
}
LCUI_API void
TextLayer_Init( LCUI_TextLayer *layer )
/* 初始化文本图层相关数据 */
{
layer->read_only = FALSE;
layer->using_code_mode = FALSE;
layer->using_style_tags = FALSE;
layer->enable_word_wrap = FALSE;
layer->enable_multiline = FALSE;
layer->need_proc_buff = FALSE;
layer->need_scroll_layer = FALSE;
layer->have_select = FALSE;
layer->start = 0;
layer->end = 0;
layer->offset_pos = Pos(0,0);
layer->old_offset_pos = Pos(0,0);
Queue_Init( &layer->color_keyword, sizeof(Special_KeyWord), Destroy_Special_KeyWord );
/* 队列中使用链表储存这些数据 */
Queue_Init( &layer->text_source_data, sizeof(LCUI_CharData), Destroy_CharData );
Queue_SetDataMode( &layer->text_source_data, QUEUE_DATA_MODE_LINKED_LIST );
Queue_Init( &layer->rows_data, sizeof(Text_RowData), Destroy_Text_RowData );
StyleTag_Init( &layer->tag_buff );
Queue_Init( &layer->style_data, sizeof(LCUI_TextStyle), NULL );
RectQueue_Init( &layer->clear_area );
/* 初始化屏蔽符的数据 */
layer->password_char.display = TRUE;
layer->password_char.need_update = FALSE;
layer->password_char.data = NULL;
layer->password_char.char_code = 0;
layer->password_char.bitmap = NULL;
layer->default_data.pixel_size = 12;
layer->current_src_pos = 0;
layer->current_des_pos = Pos(0,0);
layer->max_text_len = 512000;
TextStyle_Init ( &layer->default_data );
LCUIWString_Init( &layer->text_buff );
//TextLayer_Text_Add_NewRow ( layer );/* 添加新行 */
}
LCUI_API void
Destroy_TextLayer( LCUI_TextLayer *layer )
/* 销毁文本图层占用的资源 */
{
Queue_Destroy( &layer->text_source_data );
Queue_Destroy( &layer->rows_data );
Queue_Destroy( &layer->tag_buff );
Queue_Destroy( &layer->style_data );
RectQueue_Destroy( &layer->clear_area );
LCUIWString_Free( &layer->text_buff );
}
/* 设定整个文本图层中需显示的文本,原有选中文本被删除 */
static int
__TextLayer_Text( LCUI_TextLayer *layer )
{
if( !layer ) {
return -1;
}
TextLayer_Text_Clear( layer );
TextLayer_Text_AddW( layer, layer->text_buff.string );
return 0;
}
static void
__TextLayer_OldArea_Erase( LCUI_TextLayer *layer, LCUI_Graph *graph )
/*
* 功能:擦除文本图层的老区域
* 说明:根据记录的旧偏移坐标,刷新部件区域内的文字所在区域。
* */
{
static int i, j, x, y, rows, len;
static Text_RowData *row_ptr;
static LCUI_CharData *char_ptr;
static LCUI_Rect area;
rows = Queue_GetTotal( &layer->rows_data );
/* 除去y轴偏移量,计算处于显示区域内的起始行的y轴坐标 */
for(y=layer->old_offset_pos.y,i=0; y<0 && i<rows; ++i) {
row_ptr = Queue_Get( &layer->rows_data, i );
if( !row_ptr ) {
continue;
}
if( y + row_ptr->max_size.h >= 0 ) {
break;
}
y += row_ptr->max_size.h;
}
for(; i<rows; ++i) {
row_ptr = Queue_Get( &layer->rows_data, i );
if( !row_ptr ) {
continue;
}
len = Queue_GetTotal( &row_ptr->string );
/* 除去x轴偏移量,计算处于显示区域内的起始列的x轴坐标 */
for(x=layer->old_offset_pos.x,j=0; x<0 && j<len; ++j) {
char_ptr = Queue_Get( &row_ptr->string, j );
if( !char_ptr ) {
continue;
}
if( x+char_ptr->bitmap->advance.x >= 0 ) {
break;
}
x += char_ptr->bitmap->advance.x;
}
/* 计算当前行的区域高度 */
area.height = layer->default_data.pixel_size+2;
for( ; j<len; ++j ) {
char_ptr = Queue_Get( &row_ptr->string, j );
if( !char_ptr ) {
continue;
}
area.y = row_ptr->max_size.h-1;
area.y -= char_ptr->bitmap->top;
if( area.y + char_ptr->bitmap->rows > area.height ) {
area.height = area.y + char_ptr->bitmap->rows;
}
/* 累加当前行的宽度(已减去x轴偏移量) */
x += char_ptr->bitmap->advance.x;
if( x > graph->width ) {
break;
}
}
/* 计算相对于图层内的区域 */
area.x = 0-layer->offset_pos.x;
area.y = y-layer->offset_pos.y;
area.width = x;
RectQueue_AddToValid( &layer->clear_area, area );
//_DEBUG_MSG("area: %d,%d,%d,%d\n", area.x, area.y, area.width, area.height);
y += row_ptr->max_size.h;
if( y > graph->height ) {
break;
}
}
}
LCUI_API void
TextLayer_Draw( LCUI_Widget *widget, LCUI_TextLayer *layer, int mode )
/* 将文本图层绘制到目标部件的图层上 */
{
LCUI_Rect area;
LCUI_Pos pos, mix_pos;
LCUI_BOOL draw_all = FALSE, redraw_row;
int i, j, n, rows, size;
LCUI_RGB color;
LCUI_Graph slot, *graph;
LCUI_CharData *p_data;
Text_RowData *p_row;
//clock_t start;
//start = clock();
//_DEBUG_MSG("enter\n");
if( widget == NULL || layer == NULL ) {
return;
}
graph = Widget_GetSelfGraph( widget );
if( graph == NULL ) {
return;
}
/* 如果文本缓存区内有数据 */
if( layer->need_proc_buff ) {
__TextLayer_Text( layer );
layer->need_proc_buff = FALSE;
}
/* 如果需要滚动图层 */
if( layer->need_scroll_layer ) {
layer->need_scroll_layer = FALSE;
//_DEBUG_MSG("layer->need_scroll_layer\n");
/* 根据之前记录的偏移坐标,刷新文本图层 */
__TextLayer_OldArea_Erase( layer, Widget_GetSelfGraph( widget ) );
draw_all = TRUE;
}
//_DEBUG_MSG("1, use time: %ld\n", clock() - start );
//start = clock();
Graph_Init( &slot );
/* 切换可用队列为当前占用的队列 */
RectQueue_Switch( &layer->clear_area );
/* 先处理需要清空的区域 */
while( RectQueue_GetFromCurrent(&layer->clear_area, &area) ) {
area.x += layer->offset_pos.x;
area.y += layer->offset_pos.y;
Graph_Quote( &slot, graph, area );
/* 将该区域的alpha通道填充为0 */
Graph_FillAlpha( &slot, 0 );
Widget_InvalidArea( widget, area );
//printf("refresh area: %d,%d,%d,%d\n",
//area.x, area.y, area.width, area.height);
}
//_DEBUG_MSG("2, use time: %ld\n", clock() - start );
//start = clock();
/* 开始绘制文本位图至目标图层上 */
rows = Queue_GetTotal( &layer->rows_data );
for(pos.y=layer->offset_pos.y,i=0; i<rows; ++i) {
redraw_row = FALSE;
p_row = Queue_Get( &layer->rows_data, i );
if( !p_row ) {
continue;
}
n = Queue_GetTotal( &p_row->string );
/* 如果当前字的位图的Y轴跨距不在有效绘制区域内 */
if( pos.y + p_row->max_size.h <= 0 ) {
pos.y += p_row->max_size.h;
continue;
}
for(pos.x=layer->offset_pos.x,j=0; j<n; ++j) {
/* 如果设置了屏蔽符 */
p_data = Queue_Get( &p_row->string, j );
if( !p_data ) {
continue;
}
if( layer->password_char.char_code > 0 ) {
layer->password_char.need_update = p_data->need_update;
p_data = &layer->password_char;
}
/* 如果当前字的位图的X轴跨距不在有效绘制区域内 */
if( pos.x + p_data->bitmap->advance.x <= 0) {
pos.x += p_data->bitmap->advance.x;
continue;
}
/* 获取该字体位图的大致尺寸 */
if( p_data->data ) {
size = p_data->data->pixel_size;
size += 2;
color = p_data->data->fore_color;
} else {
size = layer->default_data.pixel_size + 2;
color = layer->default_data.fore_color;
}
/* 如果字体位图已标记更新,则绘制它 */
if( p_data->need_update || draw_all ) {
p_data->need_update = FALSE;
if( !redraw_row ) {
area.x = pos.x;
area.height = p_row->max_size.h;
redraw_row = TRUE;
}
mix_pos.x = pos.x + p_data->bitmap->left;
mix_pos.y = pos.y + p_row->max_size.h-1;
mix_pos.y -= p_data->bitmap->top;
/* 贴上字体位图 */
FontBMP_Mix( graph, mix_pos,
p_data->bitmap, color, mode );
}
pos.x += p_data->bitmap->advance.x;
if( pos.x > widget->size.w ) {
break;
}
}
if(redraw_row) {
area.y = pos.y;
area.width = pos.x - area.x;
//_DEBUG_MSG("area:%d,%d,%d,%d\n",
//area.x, area.y, area.width, area.height);
Widget_InvalidArea( widget, area );
}
pos.y += p_row->max_size.h;
if( pos.y > widget->size.h ) {
break;
}
}
//_DEBUG_MSG("3, use time: %ld\n", clock() - start );
//_DEBUG_MSG("quit\n");
}
LCUI_API void
TextLayer_Refresh( LCUI_TextLayer *layer )
/* 标记文本图层中每个字的位图,等待绘制文本图层时进行更新 */
{
int i, j;
int rows, len;
Text_RowData *row_ptr;
LCUI_CharData *char_ptr;
LCUI_Rect area;
rows = Queue_GetTotal( &layer->rows_data );
for(area.y=0,area.x=0,i=0; i<rows; ++i) {
row_ptr = Queue_Get( &layer->rows_data, i );
len = Queue_GetTotal( &row_ptr->string );
for(j=0; j<len; ++j) {
char_ptr = Queue_Get( &row_ptr->string, j );
if( !char_ptr ) {
continue;
}
char_ptr->need_update = TRUE;
}
area.height = row_ptr->max_size.h;
area.width = row_ptr->max_size.w;
RectQueue_AddToValid( &layer->clear_area, area );
area.y += row_ptr->max_size.h;
}
}
/*
* 功能:设定文本图层的偏移位置
* 返回值:需要对图层进行重绘时返回0,否则返回1
* */
LCUI_API int
TextLayer_SetOffset( LCUI_TextLayer *layer, LCUI_Pos offset_pos )
{
/* 如果之前已经标记不需要滚动,那么就记录当前的偏移位置 */
if( !layer->need_scroll_layer ) {
layer->old_offset_pos = layer->offset_pos;
}
if( layer->offset_pos.x == offset_pos.x
&& layer->offset_pos.y == offset_pos.y ) {
return 1;
}
layer->offset_pos = offset_pos;
layer->need_scroll_layer = TRUE;
return 0;
}
/* 剪切板 */
//static LCUI_String clip_board;
LCUI_API LCUI_Size
TextLayer_GetSize ( LCUI_TextLayer *layer )
/* 获取文本图层的实际尺寸 */
{
int i, rows;
LCUI_Size size;
Text_RowData *p_row;
rows = Queue_GetTotal( &layer->rows_data );
for(size.w=0,size.h=0,i=0; i<rows; ++i) {
p_row = Queue_Get( &layer->rows_data, i );
if( !p_row ) {
continue;
}
if( size.w < p_row->max_size.w ) {
size.w = p_row->max_size.w;
}
size.h += p_row->max_size.h;
}
/* 尺寸稍微搞大一点,因为显示文本光标需要一定空间 */
size.w += 2;
size.h += 2;
return size;
}
/* 获取文本图层中的文本内容 */
LCUI_API size_t
TextLayer_GetText( LCUI_TextLayer *layer, wchar_t *buff, size_t max_len )
{
unsigned int i, text_len;
LCUI_CharData *char_p;
if( max_len == 0 ) {
return 0;
}
text_len = Queue_GetTotal( &layer->text_source_data );
if( text_len <= 0 ) {
return 0;
}
max_len -= 1; /* 留一个位置保存结束符 */
if( max_len > text_len ) {
max_len = text_len;
}
for( i=0; i<max_len; ++i ) {
char_p = (LCUI_CharData *)Queue_Get( &layer->text_source_data, i );
buff[i] = char_p->char_code;
}
buff[i] = '\0';
return i;
}
LCUI_API void
TextLayer_CharLater_Refresh( LCUI_TextLayer *layer, LCUI_Pos char_pos )
/* 刷新指定行中指定字以及后面的字的区域 */
{
LCUI_Pos pos;
int i, len;
Text_RowData *row_ptr;
LCUI_CharData *char_ptr;
/* 获取该行文字的起点Y轴坐标 */
for( pos.y=0,i=0; i<char_pos.y; ++i ) {
row_ptr = Queue_Get( &layer->rows_data, i );
if( !row_ptr ) {
continue;
}
pos.y += row_ptr->max_size.h;
}
row_ptr = Queue_Get( &layer->rows_data, char_pos.y );
len = Queue_GetTotal( &row_ptr->string );
/* 获取该字的起点X轴坐标 */
for( pos.x=0,i=0; i<char_pos.x; ++i ) {
char_ptr = Queue_Get( &row_ptr->string, i );
if( !char_ptr ) {
continue;
}
pos.x += char_ptr->bitmap->advance.x;
}
for( i=char_pos.x; i<len; ++i ) {
char_ptr = Queue_Get( &row_ptr->string, i );
if( !char_ptr ) {
continue;
}
TextLayer_Clear( layer, pos, row_ptr->max_size.h, char_ptr );
/* 标记该字的位图需要重绘 */
char_ptr->need_update = TRUE;
pos.x += char_ptr->bitmap->advance.x;
}
}
LCUI_API void
TextLayer_Text_SetDefaultStyle( LCUI_TextLayer *layer, LCUI_TextStyle style )
/* 设定默认的文本样式,需要调用TextLayer_Draw函数进行文本位图更新 */
{
LCUI_Pos pos;
LCUI_CharData *char_ptr;
LCUI_TextStyle *old_style;
Text_RowData *row_ptr;
int rows, len, i, j;
layer->default_data = style;
rows = Queue_GetTotal( &layer->rows_data );
for(pos.y=0,i=0; i<rows; ++i) {
row_ptr = Queue_Get( &layer->rows_data, i );
len = Queue_GetTotal( &row_ptr->string );
for(pos.x=0,j=0; j<len; ++j) {
char_ptr = Queue_Get( &row_ptr->string, j );
if( !char_ptr ) {
continue;
}
old_style = char_ptr->data;
if( !old_style ) {
char_ptr->need_update = TRUE;
goto skip_style_cmp;
}
/* 若有属性是缺省的 */
if(!old_style->_pixel_size) {
old_style->pixel_size = style.pixel_size;
char_ptr->need_update = TRUE;
}
if(!old_style->_style) {
old_style->style = style.style;
char_ptr->need_update = TRUE;
}
if(!old_style->_family) {
old_style->font_id = style.font_id;
char_ptr->need_update = TRUE;
}
if(!old_style->_weight) {
old_style->weight = style.weight;
char_ptr->need_update = TRUE;
}
if(!old_style->_back_color) {
old_style->back_color = style.back_color;
char_ptr->need_update = TRUE;
}
if(!old_style->_fore_color) {
old_style->fore_color = style.fore_color;
char_ptr->need_update = TRUE;
}
if(!old_style->_decoration) {
old_style->decoration = style.decoration;
char_ptr->need_update = TRUE;
}
skip_style_cmp:;
if(char_ptr->need_update) {
TextLayer_Clear( layer, pos, row_ptr->max_size.h, char_ptr );
pos.x += char_ptr->bitmap->advance.x;
TextLayer_GetCharBMP ( &layer->default_data, char_ptr );
} else {
pos.x += char_ptr->bitmap->advance.x;
}
}
pos.y += row_ptr->max_size.h;
TextLayer_Update_RowSize( layer, i );
}
}
LCUI_API void
TextLayer_ReadOnly( LCUI_TextLayer *layer, LCUI_BOOL flag )
/* 指定文本图层中的文本是否为只读 */
{
layer->read_only = flag;
}
LCUI_API void
TextLayer_Text_Clear( LCUI_TextLayer *layer )
/* 清空文本内容 */
{
if( layer == NULL ) {
return;
}
TextLayer_Refresh( layer );
Queue_Destroy( &layer->text_source_data );
Queue_Destroy( &layer->rows_data );
Queue_Destroy( &layer->style_data );
layer->current_src_pos = 0;
layer->current_des_pos = Pos(0,0);
}
LCUI_API void
TextLayer_SetRowEnd( LCUI_TextLayer *layer, uint_t row, uint_t start_cols )
/* 为指定行设定结束点,结束点及后面的数据将被删除,但不记录残余文本位图区域 */
{
uint_t total, i;
Text_RowData *row_data;
row_data = Queue_Get( &layer->rows_data, row );
total = Queue_GetTotal( &row_data->string );
/* 移除多余的数据 */
for(i=start_cols; i<total; ++i) {
Queue_Delete( &row_data->string, start_cols );
}
}
LCUI_API int
TextLayer_Text_GetTotalLength( LCUI_TextLayer *layer )
/* 获取文本位图中的文本长度 */
{
return Queue_GetTotal( &layer->text_source_data );
}
LCUI_API void
TextLayer_Text_Set_MaxLength( LCUI_TextLayer *layer, int max )
/* 设定文本位图中的文本长度 */
{
if( max > 0 ) {
layer->max_text_len = max;
}
// 对现有文本进行截断处理,暂不添加添加
}
LCUI_API void
TextLayer_Text_SetPasswordChar( LCUI_TextLayer *layer, wchar_t ch )
/*
* 设置屏蔽字符,设置后,文本框内的文本都会显示成该字符
* 如果ch的值为0,则不对文本框里的文本进行屏蔽
* */
{
layer->password_char.char_code = ch;
TextLayer_GetCharBMP( &layer->default_data, &layer->password_char );
//暂时不进行其它处理
}
/* 对文本进行预处理,处理后的数据保存至layer里 */
LCUI_API void
TextLayer_Text_Process( LCUI_TextLayer *layer,
int pos_type,
wchar_t *new_text )
{
LCUI_BOOL refresh = TRUE;
LCUI_Pos cur_pos, des_pos;
int total, cur_len, row, src_pos, total_row, n_ignore = 0;
wchar_t *finish, *p, *q;
LCUI_Pos tmp_pos;
LCUI_CharData *char_ptr, char_data;
Text_RowData *cur_row_ptr, *tmp_row_ptr;
if( new_text == NULL ) {
return;
}
/* 如果有选中的文本,那就删除 */
//......
DEBUG_MSG1("enter\n");
/* 如果是将文本追加至文本末尾 */
if( pos_type == AT_TEXT_LAST ) {
cur_pos.y = Queue_GetTotal( &layer->rows_data );
if( cur_pos.y > 0 ) {
--cur_pos.y;
}
cur_row_ptr = Queue_Get( &layer->rows_data, cur_pos.y );
if( !cur_row_ptr ) {
TextLayer_Text_Add_NewRow( layer );
cur_row_ptr = Queue_Get( &layer->rows_data, cur_pos.y );
}
cur_pos.x = Queue_GetTotal( &cur_row_ptr->string );
src_pos = Queue_GetTotal( &layer->text_source_data );
des_pos = cur_pos;
} else {/* 否则,是将文本插入至光标所在位置 */
cur_pos = TextLayer_Cursor_GetPos( layer );
DEBUG_MSG1( "cur_pos: %d,%d\n", cur_pos.x, cur_pos.y );
cur_row_ptr = Queue_Get( &layer->rows_data, cur_pos.y );
DEBUG_MSG1( "cur_row_ptr: %p\n", cur_row_ptr );
if( !cur_row_ptr ) {
TextLayer_Text_Add_NewRow( layer );
cur_row_ptr = Queue_Get( &layer->rows_data, cur_pos.y );
}
src_pos = layer->current_src_pos;
des_pos = layer->current_des_pos;
DEBUG_MSG1( "src_pos: %d\n", src_pos );
DEBUG_MSG1( "des_pos: %d,%d\n", des_pos.x, des_pos.y );
}
row = cur_pos.y;
total = wcslen( new_text );
total_row = TextLayer_GetRows( layer );
/* 判断当前要添加的字符的总数是否超出最大限制 */
cur_len = Queue_GetTotal( &layer->text_source_data );
if( total + cur_len > layer->max_text_len ) {
total = layer->max_text_len - cur_len;
}
if( total < 0 ) {
total = 0;
}
//_DEBUG_MSG( "layer: %p, cur total: %d, max_len: %d\n",
// layer, cur_len, layer->max_text_len );
DEBUG_MSG1( "total char: %d\n", total );
DEBUG_MSG1( "total row: %d\n", total_row );
char_data.bitmap = NULL;
/* 先记录这一行需要刷新的区域,起点为光标所在位置 */
TextLayer_CharLater_Refresh( layer, cur_pos );
for(p=new_text, finish=new_text+total; p<finish; ++p) {
DEBUG_MSG2( "1, char: %c\n", *p );
if( layer->using_style_tags ) {
/* 处理样式的结束标签 */
q = StyleTag_ProcessEndingTag( &layer->tag_buff, p );
if( q ) {
/* 计算需忽略的字符数 */
n_ignore = q-p+1;
} else {
/* 处理样式标签 */
q = StyleTag_ProcessTag( &layer->tag_buff, p );
if( q ) {
n_ignore = q-p+1;
}
}
}
/* 针对换行符模式为Win(CR/LF)的文本,进行处理 */
if(*p == '\n' || *p == '\r') {
/* 计算需要忽略的换行符的数量 */
for( n_ignore=0,q=p; *q == '\n' || *q == '\r';
++q,++n_ignore);
}
if(n_ignore > 0) {
/* 被忽略的字符的属性都一样,所以只需赋一次值 */
char_data.data = NULL;
char_data.display = FALSE;
char_data.need_update = FALSE;
char_data.bitmap = NULL;
}
while(n_ignore > 0) {
DEBUG_MSG2( "ignore = %d\n", n_ignore );
char_data.char_code = *p++;
Queue_Insert( &layer->text_source_data, src_pos, &char_data );
char_ptr = Queue_Get( &layer->text_source_data, src_pos );
/* 如果启用多行显示,并遇到换行符,那就增加新行 */
if( layer->enable_multiline
&& char_data.char_code == '\n' ) {
++row;
if( refresh ) {
tmp_pos.x = 0;
tmp_pos.y = row;
/* 刷新该行后面所有行的字符 */
for( ; tmp_pos.y<total_row; ++tmp_pos.y ) {
TextLayer_CharLater_Refresh( layer, tmp_pos );
}
refresh = FALSE;
}
/* 换行符的数据单独保存 */
cur_row_ptr->last_char = char_ptr;
/* 在该行插入新行 */
TextLayer_Text_Insert_NewRow( layer, row );
/* 获取新行的行指针 */
tmp_row_ptr = Queue_Get( &layer->rows_data, row );
/* 当前位置后面的字符转移到新行里 */
TextLayer_Text_RowBreak( layer, cur_row_ptr,
des_pos.x, tmp_row_ptr );
/* 行指针切换到下一行 */
cur_row_ptr = tmp_row_ptr;
/* 更新当前字符坐标 */
des_pos.x = 0;
des_pos.y = row;
/* 更新总行数 */
total_row = TextLayer_GetRows( layer );
}
++src_pos;
--n_ignore;
if(n_ignore == 0) {
n_ignore = -1;
break;
}
}
if(n_ignore == -1) {
--p; n_ignore = 0;
continue;
}
DEBUG_MSG2( "2, char: %c\n", *p );
char_data.char_code = *p;
char_data.display = TRUE;
char_data.need_update = TRUE;
char_data.data = StyleTag_GetCurrentStyle( &layer->tag_buff );
/* 插入至源文本中 */
Queue_Insert( &layer->text_source_data, src_pos, &char_data );
/* 获取源文本中的字符数据的指针 */
char_ptr = Queue_Get( &layer->text_source_data, src_pos );
/* 将该指针添加至行数据队列中 */
cur_row_ptr = Queue_Get( &layer->rows_data, des_pos.y );
Queue_InsertPointer( &cur_row_ptr->string, des_pos.x, char_ptr );
++src_pos; ++des_pos.x;
}
if( pos_type == AT_CURSOR_POS ) {
layer->current_des_pos = des_pos;
layer->current_src_pos = src_pos;
}
DEBUG_MSG1("quit\n");
}
LCUI_API void
TextLayer_Text_GenerateBMP( LCUI_TextLayer *layer )
/* 为文本图层中的文本生成位图,已存在位图的文字将不重新生成 */
{
LCUI_BOOL refresh = FALSE;
LCUI_Pos pos;
int i, j, len, rows;
Text_RowData *row_ptr;
LCUI_CharData *char_ptr;
DEBUG_MSG1("enter\n");
DEBUG_MSG1("thread: %lu\n", thread_self());
rows = Queue_GetTotal( &layer->rows_data );
for( pos.y=0,j=0; j<rows; ++j ) {
row_ptr = Queue_Get( &layer->rows_data, j );
len = Queue_GetTotal( &row_ptr->string );
DEBUG_MSG1("row %d, len: %d\n", j, len);
for( pos.x=0,i=0; i<len; ++i) {
char_ptr = Queue_Get( &row_ptr->string, i );
DEBUG_MSG1("generate FontBMP, get char_ptr: %p, char: %c\n",
char_ptr, char_ptr->char_code );
if( !char_ptr || !char_ptr->display ) {
DEBUG_MSG1("no display\n");
continue;
}
if( FontBMP_Valid( char_ptr->bitmap ) ) {
DEBUG_MSG1("have FontBMP\n");
if( !refresh ) {
pos.x += char_ptr->bitmap->advance.x;
continue;
}
} else {
refresh = TRUE;
DEBUG_MSG1( "generate FontBMP, char code: %d\n", char_ptr->char_code );
TextLayer_GetCharBMP ( &layer->default_data, char_ptr );
}
DEBUG_MSG1( "char_data->bitmap->advance.x: %d\n", char_ptr->bitmap->advance.x );
TextLayer_Clear( layer, pos, row_ptr->max_size.h, char_ptr );
char_ptr->need_update = TRUE;
pos.x += char_ptr->bitmap->advance.x;
}
refresh = FALSE;
/* 更新当前行的尺寸 */
TextLayer_Update_RowSize( layer, j );
DEBUG_MSG1("row size: %d,%d\n", row_ptr->max_size.w, row_ptr->max_size.h);
pos.y += row_ptr->max_size.h;
}
DEBUG_MSG1("quit\n");
}
LCUI_API void
TextLayer_PrintInfo( LCUI_TextLayer *layer )
/* 打印文本图层信息 */
{
int32_t i, j, len, rows;