private static void UpdateTagFormatting(StringColorType stringColorType, ICollection <SegmentSection> sections) { if (stringColorType.Black != string.Empty) { sections.Add(new SegmentSection(SegmentSection.ContentType.Text, string.Empty, stringColorType.Black)); } var tags = Core.Processor.SeperateTags(stringColorType.Red); foreach (var tag in tags) { switch (tag.Type) { case TagUnit.TagUnitType.IsPlaceholder: { sections.Add(new SegmentSection(SegmentSection.ContentType.Placeholder, tag.Id, tag.Content)); break; } case TagUnit.TagUnitType.IsTag: { switch (tag.State) { case TagUnit.TagUnitState.IsOpening: { sections.Add(new SegmentSection(SegmentSection.ContentType.Tag, tag.Id, tag.Content)); break; } case TagUnit.TagUnitState.IsClosing: { sections.Add(new SegmentSection(SegmentSection.ContentType.TagClosing, tag.Id, tag.Content)); break; } case TagUnit.TagUnitState.IsEmpty: { sections.Add(new SegmentSection(SegmentSection.ContentType.Placeholder, tag.Id, tag.Content)); break; } } break; } } } }
public void DrawString(string str, double x, double y, StringAlignment align, Size layoutRect, StringColorType colorType = StringColorType.Forground) { System.Drawing.StringAlignment old = this.sf.Alignment; this.sf.Alignment = System.Drawing.StringAlignment.Center; g.DrawString(str, this.font, new SolidBrush(this.Foreground), new RectangleF((float)x, (float)y, (float)layoutRect.Width, (float)layoutRect.Height), this.sf); this.sf.Alignment = old; }
internal Dictionary <string, ParagraphUnit> ReadRtf(string rtfFile) { var rtb = new RichTextBox { Enabled = false, LanguageOption = RichTextBoxLanguageOptions.AutoFont, ReadOnly = true, Visible = false, WordWrap = false, ShowSelectionMargin = false }; rtb.SendToBack(); rtb.SuspendLayout(); rtb.LoadFile(rtfFile, RichTextBoxStreamType.RichText); rtb.Rtf = rtb.Rtf.Replace("\\v\\", "\\v0\\"); rtb.AppendText("\n"); var indexProcessingCurrent = 0; OnProgress(1000, indexProcessingCurrent, "Initialzing..."); var paragraphUnits = new Dictionary <string, ParagraphUnit>(); var file = rtfFile; var fileAttributes = new FileAttributes(); var iTagId = 10000; var innerFileName = string.Empty; var regexFile = new Regex(@"\<file source\=""(?<x1>[^""]*)""\s+target\=""(?<x2>[^""]*)""\s+segmentCount\=""(?<x3>[^""]*)""\s+path\=""(?<x4>[^""]*)""[^\>]*\>", RegexOptions.IgnoreCase | RegexOptions.Singleline); var regexSeg = new Regex(@"\<seg id\=""(?<x1>[^""]*)""\s+pid\=""(?<x2>[^""]*)""\s+status\=""(?<x3>[^""]*)""\s+locked\=""(?<x4>[^""]*)""\s+match\=""(?<x5>[^""]*)""[^\>]*\>" + "\n" + @"(?<x6>.*?)" + "\n" + @"\<\/seg\>" + "\n", RegexOptions.IgnoreCase | RegexOptions.Singleline); var regexSource = new Regex(@"\{0\>(?<xSource>.*?)\<\}(?<xPercentage>[^\{]*)\{\>", RegexOptions.IgnoreCase | RegexOptions.Singleline); var matchFile = regexFile.Match(rtb.Text); if (matchFile.Success) { fileAttributes.FileSource = matchFile.Groups["x1"].Value; fileAttributes.FileTarget = matchFile.Groups["x2"].Value; fileAttributes.FileSegmentCount = matchFile.Groups["x3"].Value; fileAttributes.FilePath = matchFile.Groups["x4"].Value; } var matchesSeg = regexSeg.Matches(rtb.Text); foreach (Match matchSeg in matchesSeg) { #region | init | var isSource = false; var colorFormat = new StringColorType(); var sourceSections = new List <SegmentSection>(); var targetSections = new List <SegmentSection>(); var segmentPairs = new List <SegmentPair>(); var comments = new List <Comment>(); #endregion var segmentPair = new SegmentPair { Id = matchSeg.Groups["x1"].Value, ParagraphId = matchSeg.Groups["x2"].Value, SegmentStatus = Core.Processor.GetSegmentStatusFromVisual(matchSeg.Groups["x3"].Value), IsLocked = Convert.ToBoolean(matchSeg.Groups["x4"].Value), MatchTypeId = matchSeg.Groups["x5"].Value, Comments = comments }; var importTranslation = ImportTranslation(segmentPair); if (!importTranslation) { continue; } var content = matchSeg.Groups["x6"].Value; var paragraphUnit = new ParagraphUnit(segmentPair.ParagraphId, segmentPairs, innerFileName); var startIndex = matchSeg.Groups["x6"].Index; var endIndex = startIndex + content.Length; segmentPair.TranslationOrigin = new TranslationOrigin();; var matchSource = regexSource.Match(content); if (matchSource.Success) { var text = matchSource.Groups["xSource"].Value; var percentage = matchSource.Groups["xPercentage"].Value; if (colorFormat.Black != string.Empty) { sourceSections.Add(new SegmentSection(SegmentSection.ContentType.Text, string.Empty, text)); } segmentPair.Source = Core.Processor.GetSectionsToText(sourceSections); segmentPair.SourceSections = sourceSections; startIndex = startIndex + matchSource.Groups["xPercentage"].Index + percentage.Length + 2; var isTarget = true; for (var i = startIndex; i <= endIndex; i++) { rtb.SelectionStart = i; rtb.SelectionLength = 1; text = rtb.Text[i].ToString(); var color = rtb.SelectionColor; if (color == Color.Black) { if (colorFormat.Red != string.Empty) { UpdateTagFormatting(colorFormat, isSource ? sourceSections : targetSections); colorFormat.Red = string.Empty; colorFormat.Black = string.Empty; colorFormat.Purple = string.Empty; colorFormat.Gray = string.Empty; colorFormat.Black += text; } else { colorFormat.Black += text; } } else if (color == Color.Red) { colorFormat.Red += text; } else if (color == Color.Purple) { if (colorFormat.Red != string.Empty) { UpdateTagFormatting(colorFormat, isSource ? sourceSections : targetSections); colorFormat.Red = string.Empty; colorFormat.Black = string.Empty; colorFormat.Red = string.Empty; } colorFormat.Purple += text; if (!isTarget) { continue; } if (!colorFormat.Purple.Trim().EndsWith("<0}")) { continue; } if (colorFormat.Black != string.Empty) { targetSections.Add(new SegmentSection(SegmentSection.ContentType.Text, string.Empty, colorFormat.Black)); } isSource = false; isTarget = false; colorFormat.Black = string.Empty; colorFormat.Purple = string.Empty; colorFormat.Red = string.Empty; colorFormat.Gray = string.Empty; } else { //outside the scope of the markup; error colorFormat.Misc += text; } } if (colorFormat.Red != string.Empty) { UpdateTagFormatting(colorFormat, isSource ? sourceSections : targetSections); colorFormat.Red = string.Empty; colorFormat.Black = string.Empty; colorFormat.Red = string.Empty; } isSource = false; isTarget = false; colorFormat.Black = string.Empty; colorFormat.Purple = string.Empty; colorFormat.Red = string.Empty; MarkupTagContent(targetSections, ref iTagId); segmentPair.Target = Core.Processor.GetSectionsToText(targetSections); segmentPair.TargetSections = targetSections; paragraphUnit.SegmentPairs.Add(segmentPair); if (segmentPair.ParagraphId != string.Empty) { if (!paragraphUnits.ContainsKey(segmentPair.ParagraphId)) { paragraphUnits.Add(segmentPair.ParagraphId, paragraphUnit); } else { paragraphUnits[segmentPair.ParagraphId].SegmentPairs.AddRange(paragraphUnit.SegmentPairs); } } } indexProcessingCurrent++; OnProgress(matchesSeg.Count, indexProcessingCurrent, string.Format("Processing segment id: {0}", segmentPair.Id)); } return(paragraphUnits); }
public void DrawString(string str, double x, double y, StringAlignment align, Size layoutRect, StringColorType colorType = StringColorType.Forground) { TextAlignment TextAlign = TextAlignment.Left; switch (align) { case StringAlignment.Left: TextAlign = TextAlignment.Left; break; case StringAlignment.Center: TextAlign = TextAlignment.Center; break; case StringAlignment.Right: TextAlign = TextAlignment.Right; break; } TextLayout layout = new TextLayout(str, this.FontFamily, this.FontSize, this.Brushes[this.ForegroundColor], layoutRect.Width, TextAlign); layout.FlowDirection = this.RightToLeft ? FlowDirection.RightToLeft : FlowDirection.LeftToRight; layout.Draw(this.Context, x, y); layout.Dispose(); }
public void DrawString(string str, double x, double y, StringAlignment align, Size layoutRect, StringColorType type) { throw new NotImplementedException(); }
public void DrawString(string str, double x, double y, StringAlignment align, Size layoutRect, StringColorType colorType = StringColorType.Forground) { if (this.render == null || this.render.IsDisposed) { return; } float dpix, dpiy; D2D.SolidColorBrush brush; switch (colorType) { case StringColorType.Forground: brush = this._factory.GetSolidColorBrush(this.Foreground); break; case StringColorType.LineNumber: brush = this._factory.GetSolidColorBrush(this.LineNumber); break; default: throw new ArgumentOutOfRangeException(); } this.GetDpi(out dpix, out dpiy); MyTextLayout layout = this._factory.GetTextLayout(str, this.format, (float)layoutRect.Width, (float)layoutRect.Height, dpix, false); layout.StringAlignment = align; layout.Draw(this.render, (float)x, (float)y, brush); layout.Dispose(); }