1 using System.Collections.Generic;
7 using Tizen.NUI.BaseComponents;
8 using Tizen.NUI.Components;
9 using Tizen.NUI.Wearable;
11 namespace Tizen.NUI.Samples
15 public string previousTag {get; set;}
16 public string endTag {get; set;}
17 public int startOffset {get; set;}
18 public int endOffset {get; set;}
23 public string tagName {get; set;}
24 public string attributeName {get; set;}
25 public bool isEndTag {get; set;}
28 public class TextPageUtil
30 private static char LESS_THAN = '<';
31 private static char GREATER_THAN = '>';
32 private static char EQUAL = '=';
33 private static char QUOTATION_MARK = '\'';
34 private static char SLASH = '/';
35 private static char BACK_SLASH = '\\';
36 private static char AMPERSAND = '&';
37 private static char HASH = '#';
38 private static char SEMI_COLON = ';';
39 private static char CHAR_ARRAY_END = '\0';
40 private static char HEX_CODE = 'x';
41 private static byte WHITE_SPACE = 0x20;
43 private static int TEXTPAGE_TEXT_CHUNK = 20000;
45 private int totalPageCnt;
47 private List<PageData> pageList;
48 private List<TagData> tagList;
49 private StringReader stream;
50 private List<char> characterList;
51 private string pageString;
54 public int SetText(TextLabel label, string str)
56 if(str == null) return 0;
58 // perform this operation to match the utf32 character used in native Dali.
59 bool previousMarkup = label.EnableMarkup;
60 label.EnableMarkup = false;
62 pageString = label.Text;
63 label.EnableMarkup = previousMarkup;
64 label.MultiLine = true;
65 label.Ellipsis = false;
67 int length = pageString.Length;
68 int remainLength = length;
73 pageList = new List<PageData>();
74 tagList = new List<TagData>();
75 characterList = new List<char>();
77 stream = new StringReader(pageString);
79 RendererParameters textParameters = new RendererParameters();
80 textParameters.Text = pageString;
81 textParameters.HorizontalAlignment = label.HorizontalAlignment;
82 textParameters.VerticalAlignment = label.VerticalAlignment;
83 textParameters.FontFamily = label.FontFamily;
84 textParameters.FontWeight = "";
85 textParameters.FontWidth = "";
86 textParameters.FontSlant = "";
87 textParameters.Layout = TextLayout.MultiLine;
88 textParameters.TextColor = Color.Black;
89 textParameters.FontSize = label.PointSize;
90 textParameters.TextWidth = (uint)label.Size.Width;
91 textParameters.TextHeight = (uint)label.Size.Height;
92 textParameters.EllipsisEnabled = true;
93 textParameters.MarkupEnabled = previousMarkup;
96 Tizen.NUI.PropertyArray cutOffIndexArray = TextUtils.GetLastCharacterIndex( textParameters );
97 uint count = cutOffIndexArray.Count();
98 for(uint i=0; i < count; i++)
100 cutOffIndexArray.GetElementAt(i).Get(out cutOffIndex); // Gets the last index of text shown on the actual screen.
102 // If markup is enabled, It should parse markup
103 if(label.EnableMarkup)
105 int preOffset = offset;
106 offset = MarkupProcess( offset, cutOffIndex - preOffset );
107 remainLength -= (offset - preOffset);
109 //If markup is not enabled, parsing is not required.
112 PageData pageData = new PageData();
113 pageData.startOffset = offset;
114 int cnt = (cutOffIndex - offset ) < remainLength ? (cutOffIndex - offset ) : remainLength;
117 pageData.endOffset = offset;
118 pageList.Add(pageData);
121 if(offset <= 0 ) break;
128 public string GetText(int pageNum)
130 if( pageNum > totalPageCnt || pageNum < 1 ) {
131 Tizen.Log.Error("NUI", $"Out of Range total page count : {totalPageCnt}, input page number : {pageNum}\n");
135 List<PageData> dataList = pageList.GetRange(pageNum-1, 1);
136 foreach(PageData data in dataList)
138 int cnt = data.endOffset - data.startOffset;
139 char[] charArray = new char[cnt];
140 pageString.CopyTo(data.startOffset, charArray, 0, cnt);
141 string pageText = data.previousTag+new String(charArray)+data.endTag;
147 private void SkipWhiteSpace(ref int offset)
150 while( ( character = stream.Read()) != -1)
153 if( character == WHITE_SPACE) continue;
158 private bool IsTag(TagData tag, ref int offset)
160 List<char> tagChaList = new List<char>();
163 bool isQuotationOpen = false;
164 bool attributesFound = false;
165 tag.isEndTag = false;
166 bool isPreviousLessThan = true;
167 bool isPreviousSlash = false;
172 tag.attributeName = "";
173 // SkipWhiteSpace(ref offset);
174 while((!isTag) && ((character = stream.Read()) != -1)) {
176 characterList.Add((char)character);
177 if( !isQuotationOpen && ( SLASH == character ) ) // '/'
179 if (isPreviousLessThan)
185 // if the tag has a '/' it may be an end tag.
186 isPreviousSlash = true;
188 isPreviousLessThan = false;
189 // SkipWhiteSpace(ref offset);
191 else if( GREATER_THAN == character ) // '>'
199 if(!attributesFound) {
200 tag.tagName = new String(tagChaList.ToArray());
202 tag.attributeName = new String(tagChaList.ToArray());
205 isPreviousSlash = false;
206 isPreviousLessThan = false;
208 else if( QUOTATION_MARK == character )
210 tagChaList.Add((char)character);
211 isQuotationOpen = !isQuotationOpen;
213 isPreviousSlash = false;
214 isPreviousLessThan = false;
216 else if( WHITE_SPACE >= character || EQUAL == character ) // ' ', '='
218 // Let's save tag name.
219 if(!attributesFound) {
220 tag.tagName = new String(tagChaList.ToArray());
223 tagChaList.Add((char)character);
224 // If the tag contains white spaces then it may have attributes.
225 if( !isQuotationOpen )
227 attributesFound = true;
232 tagChaList.Add((char)character);
233 isPreviousSlash = false;
234 isPreviousLessThan = false;
241 private int MarkupProcess(int startOffset, int cutOffIndex)
245 int offset = startOffset;
247 characterList.Clear();
248 PageData pageData = new PageData();
250 pageData.startOffset = offset;
252 bool isPreviousLessThan = false;
253 bool isPreviousSlash = false;
255 // If the markup was previously open, the markup tag should be attached to the front.
257 foreach (TagData data in tagList)
259 tag += "<"+data.tagName+data.attributeName+">";
261 pageData.previousTag = tag;
265 while( (character = stream.Read()) != -1 )
268 characterList.Add((char)character);
270 TagData tagData = new TagData();
272 if( LESS_THAN == character ) // '<'
274 isTag = IsTag(tagData, ref offset);
278 if(tagData.isEndTag) {
279 int lastIndex = tagList.Count;
280 tagList.RemoveAt(lastIndex-1);
282 tagList.Add(tagData);
287 if(count >= cutOffIndex) break;
291 // If the markup was previously open, you should attach the label tag.
293 foreach (TagData data in tagList)
295 tag = "</"+data.tagName+">" + tag;
297 pageData.endTag = tag;
299 pageData.endOffset = offset;
300 pageList.Add(pageData);
302 if(character == -1) offset = -1;