GLine.cs
上传用户:szltgg
上传日期:2019-05-16
资源大小:604k
文件大小:28k
- /*
- * Copyright (c) 2005 Poderosa Project, All Rights Reserved.
- * $Id: GLine.cs,v 1.3 2005/04/27 08:48:50 okajima Exp $
- */
- using System;
- using System.Collections;
- using System.Drawing;
- using System.Diagnostics;
- using System.Text;
- using Poderosa.Forms;
- using Poderosa.Terminal;
- using Poderosa.UI;
- namespace Poderosa.Text
- {
- internal class RenderParameter {
- private int _linefrom;
- private int _linecount;
- private Rectangle _targetRect;
- public int LineFrom {
- get {
- return _linefrom;
- }
- set {
- _linefrom=value;
- }
- }
- public int LineCount {
- get {
- return _linecount;
- }
- set {
- _linecount=value;
- }
- }
- public Rectangle TargetRect {
- get {
- return _targetRect;
- }
- set {
- _targetRect=value;
- }
- }
- }
- /// <summary>
- /// 峴枛偺庬椶
- /// </summary>
- public enum EOLType {
- Continue,
- CRLF,
- CR,
- LF
- }
- public enum CharGroup {
- SingleByte, //unicode偱0x100枹枮偺暥帤
- TwoBytes //0x100埲忋偺暥帤
- }
- /// <summary>
- /// GLine偺峔惉梫慺丅GWord偼TextDecoration側偳傪嫟桳偡傞丅
- /// </summary>
- public class GWord
- {
- private TextDecoration _decoration;
- private int _offset;
- private CharGroup _charGroup;
- private GWord _next;
- //偟偽偟偽嶲徠偡傞偺偱僉儍僢僔儏偡傞抣
- internal int nextOffsetCache;
- internal int displayLengthCache;
- /// <summary>
- /// 昞帵梡偺憰忺
- /// </summary>
- internal TextDecoration Decoration {
- get {
- return _decoration;
- }
- }
- /// <summary>
- /// 強懏偡傞GLine偺拞偱壗暥帤栚偐傜巒傑偭偰偄傞偐
- /// </summary>
- public int Offset {
- get {
- return _offset;
- }
- }
- ///師偺Word
- public GWord Next {
- get {
- return _next;
- }
- set {
- _next = value;
- }
- }
- public CharGroup CharGroup {
- get {
- return _charGroup;
- }
- set {
- _charGroup = value;
- }
- }
- /// <summary>
- /// 暥帤楍丄僨僐儗乕僔儑儞丄僆僼僙僢僩傪巜掕偡傞僐儞僗僩儔僋僞丅Type偼Normal偵側傞丅
- /// </summary>
- internal GWord(TextDecoration d, int o, CharGroup chargroup) {
- Debug.Assert(d!=null);
- _offset = o;
- _decoration = d;
- _next = null;
- _charGroup = chargroup;
- nextOffsetCache = -1;
- displayLengthCache = -1;
- }
- //Next偺抣埲奜傪僐僺乕偡傞
- public GWord StandAloneClone() {
- return new GWord(_decoration, _offset, _charGroup);
- }
- public GWord DeepClone() {
- GWord w = new GWord(_decoration, _offset, _charGroup);
- if(_next!=null)
- w._next = _next.DeepClone();
- return w;
- }
- }
- /// <summary>
- /// 侾峴偺僨乕僞
- /// GWord傊偺暘夝偼抶墑昡壙偝傟傞丅
- /// </summary>
- public class GLine {
- static GLine() {
- InitLengthMap();
- }
- public const char WIDECHAR_PAD = 'uFFFF';
- private char[] _text;
- private EOLType _eolType;
- private int _id;
- private GWord _firstWord;
- private GLine _nextLine;
- private GLine _prevLine;
- public GLine(int length) {
- Debug.Assert(length>0);
- _text = new char[length];
- _firstWord = new GWord(TextDecoration.ClonedDefault(), 0, CharGroup.SingleByte);
- _id = -1;
- }
- public GLine(char[] data, GWord firstWord) {
- _text = (char[])data.Clone();
- _firstWord = firstWord;
- _id = -1;
- }
- public GLine Clone() {
- GLine nl = new GLine(_text, _firstWord.DeepClone());
- nl._eolType = _eolType;
- nl._id = _id;
- return nl;
- }
- public void Clear() {
- for(int i=0; i<_text.Length; i++)
- _text[i] = ' ';
- _firstWord = new GWord(TextDecoration.ClonedDefault(), 0, CharGroup.SingleByte);
- }
- internal void Clear(TextDecoration dec) {
- for(int i=0; i<_text.Length; i++)
- _text[i] = ' ';
- _firstWord = new GWord(dec, 0, CharGroup.SingleByte);
- }
- public int Length {
- get {
- return _text.Length;
- }
- }
- /// <summary>
- /// 僀儞僨僋僗傪巜掕偟偰GWord傪曉偡丅儗儞僟儕儞僌嵪傒偐偳偆偐偼峫椂偟偰偄側偄丅
- /// </summary>
- public GWord FirstWord {
- get {
- return _firstWord;
- }
- }
- public char[] Text {
- get {
- return _text;
- }
- }
- public int DisplayLength {
- get {
- int i = 0;
- int m = _text.Length;
- for(i=0; i<m; i++) {
- if(_text[i]==' ') break;
- }
- return i;
- }
- }
- public int CharLength {
- get {
- int n = _text.Length-1;
- while(n>=0 && _text[n]==' ') n--;
- return n+1;
- }
- }
- //慜屻偺扨岅嬫愗傝傪尒偮偗傞丅曉偡埵抲偼丄pos偲GetWordBreakGroup偺抣偑堦抳偡傞拞偱墦偄抧揰
- public int FindPrevWordBreak(int pos) {
- int v = ToCharGroupForWordBreak(_text[pos]);
- while(pos>=0) {
- if(v!=ToCharGroupForWordBreak(_text[pos])) return pos;
- pos--;
- }
- return -1;
- }
- public int FindNextWordBreak(int pos) {
- int v = ToCharGroupForWordBreak(_text[pos]);
- while(pos<_text.Length) {
- if(v!=ToCharGroupForWordBreak(_text[pos])) return pos;
- pos++;
- }
- return _text.Length;
- }
- private static int ToCharGroupForWordBreak(char ch) {
- if(('0'<=ch && ch<='9') || ('a'<=ch && ch<='z') || ('A'<=ch && ch<='Z') || ch=='_' || GEnv.Options.AdditionalWordElement.IndexOf(ch)!=-1)
- return 1;
- else if(ch<=0x20 || ch==0x40)
- return 2;
- else if(ch<=0x100)
- return 3;
- else //偝傜偵偙偙傪UnicodeCategory摍傪傒偰揔摉偵偙偟傜偊傞偙偲傕偱偒傞偑
- return 4;
- }
- public int ID {
- get {
- return _id;
- }
- set {
- _id = value;
- }
- }
- //椬愙峴偺愝掕丂偙偺曄峏偼TerminalDocument偐傜偺傒峴偆偙偲両
- public GLine NextLine {
- get {
- return _nextLine;
- }
- set {
- _nextLine = value;
- }
- }
- public GLine PrevLine {
- get {
- return _prevLine;
- }
- set {
- _prevLine = value;
- }
- }
- public EOLType EOLType {
- get {
- return _eolType;
- }
- set {
- _eolType = value;
- }
- }
- internal void ExpandBuffer(int length) {
- if(length<=_text.Length) return;
- char[] current = _text;
- _text = new char[length];
- Array.Copy(current, 0, _text, 0, current.Length);
- }
- internal void Render(IntPtr hdc, RenderParameter param, RenderProfile prof, int y) {
- if(_text[0]==' ') return; //壗傕昤偐側偔偰傛偄
- float fx = (float)param.TargetRect.Left;
- RectangleF rect = new RectangleF();
- rect.Y = param.TargetRect.Top+y;
- rect.Height = prof.Pitch.Height;
- GWord word = _firstWord;
- while(word != null) {
-
- rect.X = fx /*- prof.CharGap*/; //Native側昤夋偱偼晄梫丠
- rect.Width = param.TargetRect.Right - rect.X;
- int ix = (int)rect.X;
- int iy = (int)rect.Y;
- TextDecoration dec = word.Decoration;
- //Brush brush = prof.CalcTextBrush(dec);
- uint forecolorref = DrawUtil.ToCOLORREF(prof.CalcTextColor(dec));
- Color bkcolor = prof.CalcBackColor(dec);
- uint bkcolorref = DrawUtil.ToCOLORREF(bkcolor);
- IntPtr hfont = prof.CalcHFONT_NoUnderline(dec, word.CharGroup);
- Win32.SelectObject(hdc, hfont);
- Win32.SetTextColor(hdc, forecolorref);
- Win32.SetBkColor(hdc, bkcolorref);
- Win32.SetBkMode(hdc, bkcolor==prof.BackColor? 1 : 2); //婎杮攚宨怓偲堦弿側傜TRANSPARENT, 堎側傟偽OPAQUE
- IntPtr bkbrush = bkcolor==prof.BackColor? IntPtr.Zero : Win32.CreateSolidBrush(bkcolorref);
-
- int display_length = WordDisplayLength(word);
- if(word.Decoration==null) { //憰忺側偟
- //g.DrawString(WordText(word), font, brush, rect);
- DrawWord(hdc, ix, iy, word);
- }
- else {
- //if(dec.Bold || (!prof.UsingIdenticalFont && word.CharGroup==CharGroup.TwoBytes))
- if(dec.Bold || word.CharGroup==CharGroup.TwoBytes) //摨偠僼僅儞僩巜掕偱傕擔杮岅偑敿妏偺俀攞偱側偄応崌偁傝丅僷僼僅乕儅儞僗栤戣偼僋儕傾偝傟偮偮偁傞偺偱妋幚偵侾暥帤偢偮昤夋
- DrawStringByOneChar2(hdc, word, display_length, bkbrush, rect.X, iy, prof);
- else
- DrawWord(hdc, ix, iy, word); //偄傑傗傾儂側昤夋僄儞僕儞偺栤戣偐傜偼夝曻偝傟偨両
- }
- //Debug.WriteLine("PW="+p.Pitch.Width+",TL="+(pb.Text.Length*p.Pitch.Width)+", real="+g.MeasureString(pb.Text, p.Font).Width);
- if(dec.Underline)
- DrawUnderline(hdc, forecolorref, ix, iy+(int)prof.Pitch.Height-1, (int)(prof.Pitch.Width * display_length));
- fx += prof.Pitch.Width * display_length;
- word = word.Next;
- if(bkbrush!=IntPtr.Zero) Win32.DeleteObject(bkbrush);
- }
- }
- private void DrawUnderline(IntPtr hdc, uint col, int x, int y, int length) {
- //Underline偑偮偔偙偲偼偁傑傝側偄偩傠偆偐傜枅夞Pen傪嶌傞丅栤戣偵側傝偦偆偩偭偨傜偦偺偲偒偵峫偊傛偆
- IntPtr pen = Win32.CreatePen(0, 1, col);
- IntPtr prev = Win32.SelectObject(hdc, pen);
- Win32.POINT pt = new Win32.POINT();
- Win32.MoveToEx(hdc, x, y, out pt);
- Win32.LineTo(hdc, x+length, y);
- Win32.SelectObject(hdc, prev);
- Win32.DeleteObject(pen);
- }
- private void DrawWord(IntPtr hdc, int x, int y, GWord word) {
- unsafe {
- int len;
- if(word.CharGroup==CharGroup.SingleByte) {
- fixed(char* p = &_text[0]) {
- len = WordNextOffset(word) - word.Offset;
- Win32.TextOut(hdc, x, y, p+word.Offset, len);
- //Win32.ExtTextOut(hdc, x, y, 4, null, p+word.Offset, len, null);
- }
- }
- else {
- string t = WordText(word);
- fixed(char* p = t) {
- len = t.Length;
- Win32.TextOut(hdc, x, y, p, len);
- //Win32.ExtTextOut(hdc, x, y, 4, null, p, len, null);
- }
- }
-
- }
- }
- private void DrawStringByOneChar2(IntPtr hdc, GWord word, int display_length, IntPtr bkbrush, float fx, int y, RenderProfile prof) {
- float pitch = prof.Pitch.Width;
- int nextoffset = WordNextOffset(word);
- if(bkbrush!=IntPtr.Zero) { //偙傟偑側偄偲擔杮岅暥帤僺僢僠偑彫偝偄偲偒慖戰帪偺偡偒傑偑偱偒傞応崌偑偁傞
- Win32.RECT rect = new Win32.RECT();
- rect.left = (int)fx;
- rect.top = y;
- rect.right = (int)(fx + pitch*display_length);
- rect.bottom = y + (int)prof.Pitch.Height;
- Win32.FillRect(hdc, ref rect, bkbrush);
- }
- for(int i=word.Offset; i<nextoffset; i++) {
- char ch = _text[i];
- if(ch==' ') break;
- if(ch==GLine.WIDECHAR_PAD) continue;
- unsafe {
- Win32.TextOut(hdc, (int)fx, y, &ch, 1);
- }
- fx += pitch * CalcDisplayLength(ch);
- }
- }
- /*
- * //!!.NET偺昤夋僶僌
- * Graphics#DrawString偵搉偡暥帤楍偼丄僗儁乕僗偺傒偱峔惉偝傟偰偄傞偲柍帇偝傟偰偟傑偆傛偆偩丅
- * 偙傟偩偲傾儞僟乕儔僀儞偩偗傪堷偒偨偄偲偒側偳偵崲傞丅挷傋偨偲偙傠丄枛旜偵僞僽傪偮偗傞偲偙偺巇慻傒傪偩傑偡偙偲偑偱偒傞偙偲偑敾柧
- * .NET偺師偺僶乕僕儑儞偱偼捈偭偰偄傞偙偲傪婜懸
- */
- private string WordTextForFuckingDotNet(GWord word) {
- int nextoffset = WordNextOffset(word);
- if(nextoffset==0)
- return "";
- else {
- bool last_is_space = false;
- Debug.Assert(nextoffset-word.Offset >= 0);
- if(word.CharGroup==CharGroup.SingleByte) {
- last_is_space = _text[nextoffset-1]==' ';
- if(last_is_space)
- return new string(_text, word.Offset, nextoffset-word.Offset)+'t';
- else
- return new string(_text, word.Offset, nextoffset-word.Offset);
- }
- else {
- char[] buf = new char[256];
- int o = word.Offset, i=0;
- while(o < nextoffset) {
- char ch = _text[o];
- if(ch!=GLine.WIDECHAR_PAD) {
- last_is_space = ch==' ';
- buf[i++] = ch;
- }
- o++;
- }
- if(last_is_space)
- buf[i++] = (char)'t';
- return new string(buf, 0, i);
- }
- }
- }
- private string WordText(GWord word) {
- int nextoffset = WordNextOffset(word);
- if(nextoffset==0)
- return "";
- else {
- Debug.Assert(nextoffset-word.Offset >= 0);
- if(word.CharGroup==CharGroup.SingleByte)
- return new string(_text, word.Offset, nextoffset-word.Offset);
- else {
- char[] buf = new char[256];
- int o = word.Offset, i=0;
- while(o < nextoffset) {
- char ch = _text[o];
- if(ch!=GLine.WIDECHAR_PAD)
- buf[i++] = ch;
- o++;
- }
- return new string(buf, 0, i);
- }
- }
- }
- private int WordDisplayLength(GWord word) {
- //偙偙偼屇偽傟傞偙偲偑偲偰傕懡偄偺偱僉儍僢僔儏傪愝偗傞
- int cache = word.displayLengthCache;
- if(cache < 0) {
- int nextoffset = WordNextOffset(word);
- int l = nextoffset - word.Offset;
- word.displayLengthCache = l;
- return l;
- }
- else
- return cache;
- }
- internal int WordNextOffset(GWord word) {
- //偙偙偼屇偽傟傞偙偲偑偲偰傕懡偄偺偱僉儍僢僔儏傪愝偗傞
- int cache = word.nextOffsetCache;
- if(cache < 0) {
- if(word.Next==null) {
- int i = _text.Length-1;
- while(i>=0 && _text[i]==' ')
- i--;
- word.nextOffsetCache = i+1;
- return i+1;
- }
- else {
- word.nextOffsetCache = word.Next.Offset;
- return word.Next.Offset;
- }
- }
- else
- return cache;
- }
- internal void Append(GWord w) {
- if(_firstWord==null)
- _firstWord = w;
- else
- this.LastWord.Next = w;
- }
- public GWord LastWord {
- get {
- GWord w = _firstWord;
- while(w.Next!=null)
- w = w.Next;
- return w;
- }
- }
- //index偺埵抲偺昞帵傪斀揮偟偨怴偟偄GLine傪曉偡
- //inverse偑false偩偲丄GWord偺暘妱偼偡傞偑Decoration偺斀揮偼偟側偄丅僸僋僸僋栤戣偺懳張偲偟偰幚憰丅
- internal GLine InverseCaret(int index, bool inverse, bool underline) {
- ExpandBuffer(index+1);
- if(_text[index]==WIDECHAR_PAD) index--;
- GLine ret = new GLine(_text, null);
- ret.ID = _id;
- ret.EOLType = _eolType;
-
- GWord w = _firstWord;
- int nextoffset = 0;
- while(w!=null) {
- nextoffset = WordNextOffset(w);
- if(w.Offset<=index && index<nextoffset) {
- //!!tail偐傜弴偵楢寢偟偨曽偑岠棪偼傛偄
- if(w.Offset<index) {
- GWord head = new GWord(w.Decoration, w.Offset, w.CharGroup);
- ret.Append(head);
- }
- TextDecoration dec = (TextDecoration)w.Decoration.Clone();
- if(inverse) {
- //怓偮偒僉儍儗僢僩偺僒億乕僩
- dec.ToCaretStyle();
- }
- if(underline) dec.Underline = true;
- GWord mid = new GWord(dec, index, w.CharGroup);
- ret.Append(mid);
- if(index+CalcDisplayLength(_text[index]) < nextoffset) {
- GWord tail = new GWord(w.Decoration, index+CalcDisplayLength(_text[index]), w.CharGroup);
- ret.Append(tail);
- }
- }
- else
- ret.Append(w.StandAloneClone());
- w = w.Next;
- }
- //!!偙偺丄僉儍儗僢僩埵抲偵僗儁乕僗傪擖傟傞偺偼Inverse偲偼堘偆張棟偱偁傞偐傜暘棧偡傞偙偲
- if(nextoffset<=index) {
- while(nextoffset<=index) {
- Debug.Assert(nextoffset < ret.Text.Length);
- ret.Text[nextoffset++] = ' ';
- }
- TextDecoration dec = TextDecoration.ClonedDefault();
- if(inverse) {
- dec.ToCaretStyle();
- }
- if(underline) dec.Underline = true;
- ret.Append(new GWord(dec, index, CharGroup.SingleByte));
- }
- return ret;
- }
- internal GLine InverseRange(int from, int to) {
- ExpandBuffer(Math.Max(from+1,to)); //寖偟偔儕僒僀僘偟偨偲偒側偳偵偙偺忦審偑枮偨偣側偄偙偲偑偁傞
- Debug.Assert(from>=0 && from<_text.Length);
- if(from<_text.Length && _text[from]==WIDECHAR_PAD) from--;
- if(to>0 && to-1<_text.Length && _text[to-1]==WIDECHAR_PAD) to--;
- GLine ret = new GLine(_text, null);
- ret.ID = _id;
- ret.EOLType = _eolType;
- //憰忺偺攝楍傪僙僢僩
- TextDecoration[] dec = new TextDecoration[_text.Length];
- GWord w = _firstWord;
- while(w!=null) {
- Debug.Assert(w.Decoration!=null);
- dec[w.Offset] = w.Decoration;
- w = w.Next;
- }
- //斀揮奐巒揰
- TextDecoration original = null;
- TextDecoration inverse = null;
- for(int i=from; i>=0; i--) {
- if(dec[i]!=null) {
- original = dec[i];
- break;
- }
- }
- Debug.Assert(original!=null);
- inverse = (TextDecoration)original.Clone();
- inverse.Inverse();
- dec[from] = inverse;
- //斖埻偵搉偭偰斀揮嶌嬈
- for(int i=from+1; i<to; i++) {
- if(i<dec.Length && dec[i]!=null) {
- original = dec[i];
- inverse = (TextDecoration)original.Clone();
- inverse.Inverse();
- dec[i] = inverse;
- }
- }
- if(to<dec.Length && dec[to]==null) dec[to] = original;
- //偙傟偵廬偭偰GWord傪嶌傞
- w = null;
- for(int i=dec.Length-1; i>=0; i--) {
- char ch = _text[i];
- if(dec[i]!=null && ch!=' ') {
- int j = i;
- if(ch==WIDECHAR_PAD) j++;
- GWord ww = new GWord(dec[i], j, CalcCharGroup(ch));
- ww.Next = w;
- w = ww;
- }
- }
- ret.Append(w);
- return ret;
- }
- //偙偺椞堟偺暥帤偺暆偼杮摉偼僼僅儞僩埶懚偩偑丄懡偔偺擔杮岅娐嫬偱偼慡妏偲偟偰埖傢傟傞柾條丅BS偱徚偡偲俀屄棃偨傝偡傞
- private static byte[] _length_map_0x80_0xFF;
- private static byte[] _length_map_0x2500_0x25FF;
- private static void InitLengthMap() {
- _length_map_0x80_0xFF = new byte[0x80];
- for(int i=0; i<_length_map_0x80_0xFF.Length; i++) {
- int t = i+0x80;
- // 仒 丯 亱 亇 丩 侘 亊 亐
- if(t==0xA7 || t==0xA8 || t==0xB0 || t==0xB1 || t==0xB4 || t==0xB6 || t==0xD7 || t==0xF7)
- _length_map_0x80_0xFF[i] = 2;
- else
- _length_map_0x80_0xFF[i] = 1;
- }
- //慡妏敿妏崿嵼僝乕儞
- _length_map_0x2500_0x25FF = new byte[] {
- //劅 劒 劆 劔 劇 劕
- 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, //2500-0F
- //劉 劖 劋 劘 劊 劗 劌 労
- 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 2, 1, 1, //2510-1F
- //劦 劙 劎 劶 劮 劜 劍 劧
- 2, 1, 1, 2, 2, 2, 1, 1, 2, 1, 1, 2, 2, 1, 1, 2, //2520-2F
- //劵 劚 劏 劯 劷 劤 劑 劰
- 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, //2530-3F
- // 劸 劥
- 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, //2540-4F
- //
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, //2550-5F
- //
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, //2560-6F
- //
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, //2570-7F
- //
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, //2580-8F
- //
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, //2590-9F
- //仭 仩
- 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, //25A0-AF
- // 仯 仮 仴 仱
- 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, //25B0-BF
- // 仧 仦 仜 仢 仠
- 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 2, 1, 1, 2, 2, //25C0-CF
- //
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, //25D0-DF
- // 侟
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, //25E0-EF
- //
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 //25F0-FF
- };
- Debug.Assert(_length_map_0x2500_0x25FF.Length==256);
-
- }
- //暥帤偵傛偭偰昤夋暆傪寛傔傞
- internal static int CalcDisplayLength(char ch) {
- if(ch >= 0x100) {
- if(0xFF61<=ch && ch<=0xFF9F) //敿妏僇僫
- return 1;
- else if(0x2500<=ch && ch<=0x25FF) //宺慄摍摿庩婰崋
- return _length_map_0x2500_0x25FF[ch-0x2500];
- else
- return 2;
- }
- else if(ch >= 0x80) {
- return _length_map_0x80_0xFF[ch-0x80];
- }
- else
- return 1; //杮摉偼tab側偳偁傞偐傕偟傟側偄偺偱傕偆彮偟恀柺栚偵寁嶼偡傋偒
- }
- //ASCII偐擔杮岅暥帤偐 僼僅儞僩偺慖戰偵巊偆
- internal static CharGroup CalcCharGroup(char ch) {
- if(ch < 0x80)
- return CharGroup.SingleByte;
- else if(ch < 0x100)
- return _length_map_0x80_0xFF[ch-0x80]==1? CharGroup.SingleByte : CharGroup.TwoBytes;
- else {
- if(0x2500 <= ch && ch <= 0x25FF) //宺慄偼擔杮岅僼僅儞僩偼巊傢側偄
- return _length_map_0x2500_0x25FF[ch-0x2500]==1? CharGroup.SingleByte : CharGroup.TwoBytes;
- else
- return CharGroup.TwoBytes;
- }
- }
- }
- /// <summary>
- /// 暥帤偺捛壛嶍彍側偳傪偟偰GLine傪憖嶌偡傞丅椺偊偽僞乕儈僫儖偑偙偺僋儔僗傪宱桼偟偰僪僉儏儊儞僩偺摿掕偺GLine傪彂偒姺偊傞偺偵巊偆丅
- /// </summary>
- internal class GLineManipulator {
- private char[] _text;
- private TextDecoration[] _decorations;
- private int _caretColumn;
- private TextDecoration _defaultDecoration;
- private EOLType _eolType;
- /// <summary>
- /// 嬻偱峔抸
- /// </summary>
- public GLineManipulator(int length) {
- _decorations = new TextDecoration[length];
- _text = new char[length];
- Clear(length);
- }
- /// <summary>
- /// 慡撪梕傪攋婞偡傞
- /// </summary>
- public void Clear(int length) {
- if(length!=_text.Length) {
- _decorations = new TextDecoration[length];
- _text = new char[length];
- }
- else {
- for(int i=0; i<_decorations.Length; i++) _decorations[i] = null;
- for(int i=0; i<_text.Length; i++) _text[i] = ' ';
- }
- _caretColumn = 0;
- _eolType = EOLType.Continue;
- }
- public int CaretColumn {
- get {
- return _caretColumn;
- }
- set {
- Debug.Assert(value>=0 && value<=_text.Length);
- _caretColumn = value;
- value--;
- while(value>=0 && _text[value]==' ')
- _text[value--] = ' ';
- }
- }
- public void CarriageReturn() {
- _caretColumn = 0;
- _eolType = EOLType.CR;
- }
- public bool IsEmpty {
- get {
- //_text傪慡晹尒傞昁梫偼側偄偩傠偆
- return _caretColumn==0 && _text[0]==' ';
- }
- }
- public TextDecoration DefaultDecoration {
- get {
- return _defaultDecoration;
- }
- set {
- _defaultDecoration = value;
- }
- }
- /// <summary>
- /// 堷悢偲摨偠撪梕偱弶婜壔偡傞丅line偺撪梕偼攋夡偝傟側偄丅
- /// 堷悢偑null偺偲偒偼堷悢側偟偺僐儞僗僩儔僋僞偲摨偠寢壥偵側傞丅
- /// </summary>
- public void Load(GLine line, int cc) {
- if(line==null) { //偙傟偑null偵側偭偰偄傞偲偟偐巚偊側偄僋儔僢僔儏儗億乕僩偑偁偭偨丅杮棃偼側偄偼偢側傫偩偑...
- Clear(80);
- return;
- }
- Clear(line.Length);
- GWord w = line.FirstWord;
- Debug.Assert(line.Text.Length==_text.Length);
- Array.Copy(line.Text, 0, _text, 0, _text.Length);
-
- int n = 0;
- while(w != null) {
- int nextoffset = line.WordNextOffset(w);
- while(n < nextoffset)
- _decorations[n++] = w.Decoration;
- w = w.Next;
- }
- _eolType = line.EOLType;
- ExpandBuffer(cc+1);
- this.CaretColumn = cc; //' '偱杽傔傞偙偲傕偁傞偺偱僾儘僷僥傿僙僢僩傪巊偆
- }
- public int BufferSize {
- get {
- return _text.Length;
- }
- }
- public void ExpandBuffer(int length) {
- if(length<=_text.Length) return;
- char[] current = _text;
- _text = new char[length];
- Array.Copy(current, 0, _text, 0, current.Length);
- TextDecoration[] current2 = _decorations;
- _decorations = new TextDecoration[length];
- Array.Copy(current2, 0, _decorations, 0, current2.Length);
- }
- public void PutChar(char ch, TextDecoration dec) {
- Debug.Assert(dec!=null);
- Debug.Assert(_caretColumn>=0);
- Debug.Assert(_caretColumn<_text.Length);
- //埲壓傢偐傝偵偔偄偑丄梫偼応崌暘偗丅偙傟偺巇條傪彂偄偨帒椏偁傝
- bool onZenkakuRight = (_text[_caretColumn] == GLine.WIDECHAR_PAD);
- bool onZenkaku = onZenkakuRight || (_text.Length>_caretColumn+1 && _text[_caretColumn+1] == GLine.WIDECHAR_PAD);
- if(onZenkaku) {
- //慡妏偺忋偵彂偔
- if(!onZenkakuRight) {
- _text[_caretColumn] = ch;
- _decorations[_caretColumn] = dec;
- if(GLine.CalcDisplayLength(ch)==1) {
- //慡妏偺忋偵敿妏傪彂偄偨応崌丄椬偵僗儁乕僗傪擖傟側偄偲昞帵偑棎傟傞
- if(_caretColumn+1<_text.Length) _text[_caretColumn+1] = ' ';
- _caretColumn++;
- }
- else {
- _decorations[_caretColumn+1] = dec;
- _caretColumn+=2;
- }
- }
- else {
- _text[_caretColumn-1] = ' ';
- _text[_caretColumn] = ch;
- _decorations[_caretColumn] = dec;
- if(GLine.CalcDisplayLength(ch)==2) {
- if(GLine.CalcDisplayLength(_text[_caretColumn+1])==2)
- if(_caretColumn+2<_text.Length) _text[_caretColumn+2] = ' ';
- _text[_caretColumn+1] = GLine.WIDECHAR_PAD;
- _decorations[_caretColumn+1] = _decorations[_caretColumn];
- _caretColumn += 2;
- }
- else
- _caretColumn++;
- }
- }
- else { //敿妏偺忋偵彂偔
- _text[_caretColumn] = ch;
- _decorations[_caretColumn] = dec;
- if(GLine.CalcDisplayLength(ch)==2) {
- if(GLine.CalcDisplayLength(_text[_caretColumn+1])==2) //敿妏丄慡妏偲側偭偰偄傞偲偙傠偵慡妏傪彂偄偨傜
- if(_caretColumn+2<_text.Length) _text[_caretColumn+2] = ' ';
- _text[_caretColumn+1] = GLine.WIDECHAR_PAD;
- _decorations[_caretColumn+1] = _decorations[_caretColumn];
- _caretColumn += 2;
- }
- else
- _caretColumn++; //偙傟偑嵟傕common側働乕僗偩偑
- }
- }
- public void SetDecoration(TextDecoration dec) {
- if(_caretColumn<_decorations.Length)
- _decorations[_caretColumn] = dec;
- }
-
- public char CharAt(int index) {
- return _text[index];
- }
- public void BackCaret() {
- if(_caretColumn>0) { //嵟嵍抂偵偁傞偲偒偼壗傕偟側偄
- _caretColumn--;
- }
- }
- public void RemoveAfterCaret() {
- for(int i=_caretColumn; i<_text.Length; i++) {
- _text[i] = ' ';
- _decorations[i] = null;
- }
- }
- public void FillSpace(int from, int to) {
- if(to>_text.Length) to = _text.Length;
- for(int i=from; i<to; i++) {
- _text[i] = ' ';
- _decorations[i] = null;
- }
- }
- public void FillSpace(int from, int to, TextDecoration dec) {
- if(to>_text.Length) to = _text.Length;
- for(int i=from; i<to; i++) {
- _text[i] = ' ';
- _decorations[i] = dec;
- }
- }
- //start偐傜count暥帤傪徚嫀偟偰媗傔傞丅塃抂偵偼null傪擖傟傞
- public void DeleteChars(int start, int count) {
- for(int i = start; i<_text.Length; i++) {
- int j = i + count;
- if(j < _text.Length) {
- _text[i] = _text[j];
- _decorations[i] = _decorations[j];
- }
- else {
- _text[i] = ' ';
- _decorations[i] = null;
- }
- }
- }
- public void InsertBlanks(int start, int count) {
- for(int i=_text.Length-1; i>=_caretColumn; i--) {
- int j = i - count;
- if(j >= _caretColumn) {
- _text[i] = _text[j];
- _decorations[i] = _decorations[j];
- }
- else {
- _text[i] = ' ';
- _decorations[i] = null;
- }
- }
- }
- public GLine Export() {
- GWord w = new GWord(_decorations[0]==null? TextDecoration.ClonedDefault() : _decorations[0], 0, GLine.CalcCharGroup(_text[0]));
- GLine line = new GLine(_text, w);
- line.EOLType = _eolType;
- int m = _text.Length;
- for(int offset=1; offset<m; offset++) {
- char ch = _text[offset];
- if(ch==' ') break;
- else if(ch==GLine.WIDECHAR_PAD) continue;
- TextDecoration dec = _decorations[offset];
- if(_decorations[offset-1]!=dec || w.CharGroup!=GLine.CalcCharGroup(ch)) {
- if(dec==null) dec = TextDecoration.ClonedDefault(); //!!杮摉偼偙偙偑null偵側偭偰偄傞偺偼偁傝偊側偄偼偢丅屻偱挷嵏偡傞偙偲
- GWord ww = new GWord(dec, offset, GLine.CalcCharGroup(ch));
- w.Next = ww;
- w = ww;
- }
- }
- return line;
- }
- public override string ToString() {
- StringBuilder b = new StringBuilder();
- b.Append(_text);
- //傾僩儕價儏乕僩傑傢傝偺昞帵偼傑偩偟偰偄側偄
- return b.ToString();
- }
- /*
- public static void TestPutChar() {
- TestPutChar(1, "aaaaz ", 0, '偄');
- TestPutChar(2, "a偁uFFFFaz ", 0, '偄');
- TestPutChar(3, "偁uFFFF偁uFFFFz ", 0, 'b');
- TestPutChar(4, "偁uFFFF偁uFFFFz ", 0, '偄');
- TestPutChar(5, "偁uFFFF偁uFFFFz ", 1, 'b');
- TestPutChar(6, "偁uFFFFaaz ", 1, '偄');
- TestPutChar(7, "偁uFFFF偁uFFFFz ", 1, '偄');
- }*/
- private static void TestPutChar(int num, string initial, int col, char ch) {
- GLineManipulator m = new GLineManipulator(10);
- m._text = initial.ToCharArray();
- m.CaretColumn = col;
- Debug.WriteLine(String.Format("Test{0} [{1}] col={2} char={3}", num, SafeString(m._text), m.CaretColumn, ch));
- m.PutChar(ch, TextDecoration.Default);
- Debug.WriteLine(String.Format("Result [{0}] col={1}", SafeString(m._text), m.CaretColumn));
- }
- public static string SafeString(char[] d) {
- StringBuilder bld = new StringBuilder();
- for(int i=0; i<d.Length; i++) {
- char ch = d[i];
- if(ch==' ') break;
- if(ch!=GLine.WIDECHAR_PAD) bld.Append(ch);
- }
- return bld.ToString();
- }
- }
- }