/// <summary> /// Creates an AttributedCharacterIterator with the contents of /// <code>iterator</code> and the additional attribute <code>key</code> /// <code>value</code>. /// </summary> /// <param name="iterator"> Initial AttributedCharacterIterator to add arg to </param> /// <param name="key"> Key for AttributedCharacterIterator </param> /// <param name="value"> Value associated with key in AttributedCharacterIterator </param> /// <returns> AttributedCharacterIterator wrapping args </returns> internal virtual AttributedCharacterIterator CreateAttributedCharacterIterator(AttributedCharacterIterator iterator, AttributedCharacterIterator_Attribute key, Object value) { AttributedString @as = new AttributedString(iterator); @as.AddAttribute(key, value); return(@as.Iterator); }
/** * Convert the underlying Set of rich text Runs into java.text.AttributedString */ public AttributedString GetAttributedString(TextRun txRun){ String text = txRun.GetText(); //TODO: properly process tabs text = text.Replace('\t', ' '); text = text.Replace((char)160, ' '); AttributedString at = new AttributedString(text); RichTextRun[] rt = txRun.GetRichTextRuns(); for (int i = 0; i < rt.Length; i++) { int start = rt[i].GetStartIndex(); int end = rt[i].GetEndIndex(); if(start == end) { logger.log(POILogger.INFO, "Skipping RichTextRun with zero length"); continue; } at.AddAttribute(TextAttribute.FAMILY, rt[i].GetFontName(), start, end); at.AddAttribute(TextAttribute.SIZE, new Float(rt[i].GetFontSize()), start, end); at.AddAttribute(TextAttribute.FOREGROUND, rt[i].GetFontColor(), start, end); if(rt[i].IsBold()) at.AddAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD, start, end); if(rt[i].IsItalic()) at.AddAttribute(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE, start, end); if(rt[i].IsUnderlined()) { at.AddAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, start, end); at.AddAttribute(TextAttribute.INPUT_METHOD_UNDERLINE, TextAttribute.UNDERLINE_LOW_TWO_PIXEL, start, end); } if(rt[i].IsStrikethrough()) at.AddAttribute(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON, start, end); int superScript = rt[i].GetSuperscript(); if(superScript != 0) at.AddAttribute(TextAttribute.SUPERSCRIPT, superScript > 0 ? TextAttribute.SUPERSCRIPT_SUPER : TextAttribute.SUPERSCRIPT_SUB, start, end); } return at; }
public virtual void Formatted(Format.Field attr, Object value, int start, int end, StringBuffer buffer) { if (start != end) { if (start < Size) { // Adjust attributes of existing runs int index = Size; int asIndex = AttributedStrings.Count - 1; while (start < index) { AttributedString @as = AttributedStrings[asIndex--]; int newIndex = index - @as.Length(); int aStart = System.Math.Max(0, start - newIndex); @as.AddAttribute(attr, value, aStart, System.Math.Min(end - start, @as.Length() - aStart) + aStart); index = newIndex; } } if (Size < start) { // Pad attributes AttributedStrings.Add(new AttributedString(buffer.Substring(Size, start - Size))); Size = start; } if (Size < end) { // Add new string int aStart = System.Math.Max(start, Size); AttributedString @string = new AttributedString(buffer.Substring(aStart, end - aStart)); @string.AddAttribute(attr, value); AttributedStrings.Add(@string); Size = end; } } }
public TextElement[] GetTextElements(float textWidth, FontRenderContext frc){ TextRun run = _shape.GetTextRun(); if (run == null) return null; String text = Run.GetText(); if (text == null || text.Equals("")) return null; AttributedString at = GetAttributedString(Run); AttributedCharacterIterator it = at.GetIterator(); int paragraphStart = it.GetBeginIndex(); int paragraphEnd = it.GetEndIndex(); List<TextElement> lines = new List<TextElement>(); LineBreakMeasurer measurer = new LineBreakMeasurer(it, frc); measurer.SetPosition(paragraphStart); while (measurer.GetPosition() < paragraphEnd) { int startIndex = measurer.GetPosition(); int nextBreak = text.IndexOf('\n', measurer.GetPosition() + 1); bool prStart = text[startIndex] == '\n'; if(prStart) measurer.SetPosition(startIndex++); RichTextRun rt = Run.GetRichTextRunAt(startIndex == text.Length ? (startIndex-1) : startIndex); if(rt == null) { logger.log(POILogger.WARN, "RichTextRun not found at pos" + startIndex + "; text.Length: " + text.Length); break; } float wrappingWidth = textWidth - _shape.GetMarginLeft() - _shape.GetMarginRight(); int bulletOffset = rt.GetBulletOffset(); int textOffset = rt.GetTextOffset(); int indent = rt.GetIndentLevel(); TextRulerAtom ruler = Run.GetTextRuler(); if(ruler != null) { int bullet_val = ruler.GetBulletOffsets()[indent]*Shape.POINT_DPI/Shape.MASTER_DPI; int text_val = ruler.GetTextOffsets()[indent]*Shape.POINT_DPI/Shape.MASTER_DPI; if(bullet_val > text_val){ int a = bullet_val; bullet_val = text_val; text_val = a; } if(bullet_val != 0 ) bulletOffset = bullet_val; if(text_val != 0) textOffset = text_val; } if(bulletOffset > 0 || prStart || startIndex == 0) wrappingWidth -= textOffset; if (_shape.GetWordWrap() == TextShape.WrapNone) { wrappingWidth = _shape.Sheet.GetSlideShow().GetPageSize().width; } TextLayout textLayout = measurer.nextLayout(wrappingWidth + 1, nextBreak == -1 ? paragraphEnd : nextBreak, true); if (textLayout == null) { textLayout = measurer.nextLayout(textWidth, nextBreak == -1 ? paragraphEnd : nextBreak, false); } if(textLayout == null){ logger.log(POILogger.WARN, "Failed to break text into lines: wrappingWidth: "+wrappingWidth+ "; text: " + rt.GetText()); measurer.SetPosition(rt.GetEndIndex()); continue; } int endIndex = measurer.GetPosition(); float lineHeight = (float)textLayout.GetBounds().Height; int linespacing = rt.GetLineSpacing(); if(linespacing == 0) linespacing = 100; TextElement el = new TextElement(); if(linespacing >= 0){ el.ascent = textLayout.GetAscent()*linespacing/100; } else { el.ascent = -linespacing*Shape.POINT_DPI/Shape.MASTER_DPI; } el._align = rt.GetAlignment(); el.advance = textLayout.GetAdvance(); el._textOffset = textOffset; el._text = new AttributedString(it, startIndex, endIndex); el.textStartIndex = startIndex; el.textEndIndex = endIndex; if (prStart){ int sp = rt.GetSpaceBefore(); float spaceBefore; if(sp >= 0){ spaceBefore = lineHeight * sp/100; } else { spaceBefore = -sp*Shape.POINT_DPI/Shape.MASTER_DPI; } el.ascent += spaceBefore; } float descent; if(linespacing >= 0){ descent = (textLayout.GetDescent() + textLayout.GetLeading())*linespacing/100; } else { descent = -linespacing*Shape.POINT_DPI/Shape.MASTER_DPI; } if (prStart){ int sp = rt.GetSpaceAfter(); float spaceAfter; if(sp >= 0){ spaceAfter = lineHeight * sp/100; } else { spaceAfter = -sp*Shape.POINT_DPI/Shape.MASTER_DPI; } el.ascent += spaceAfter; } el.descent = descent; if(rt.IsBullet() && (prStart || startIndex == 0)){ it.SetIndex(startIndex); AttributedString bat = new AttributedString(Character.ToString(rt.GetBulletChar())); Color clr = rt.GetBulletColor(); if (clr != null) bat.AddAttribute(TextAttribute.FOREGROUND, clr); else bat.AddAttribute(TextAttribute.FOREGROUND, it.GetAttribute(TextAttribute.FOREGROUND)); int fontIdx = rt.GetBulletFont(); if(fontIdx == -1) fontIdx = rt.GetFontIndex(); PPFont bulletFont = _shape.Sheet.GetSlideShow().GetFont(fontIdx); bat.AddAttribute(TextAttribute.FAMILY, bulletFont.GetFontName()); int bulletSize = rt.GetBulletSize(); int fontSize = rt.GetFontSize(); if(bulletSize != -1) fontSize = Math.round(fontSize*bulletSize*0.01f); bat.AddAttribute(TextAttribute.SIZE, new Float(fontSize)); if(!new Font(bulletFont.GetFontName(), Font.PLAIN, 1).canDisplay(rt.GetBulletChar())){ bat.AddAttribute(TextAttribute.FAMILY, "Arial"); bat = new AttributedString("" + DEFAULT_BULLET_CHAR, bat.GetIterator().GetAttributes()); } if(text.Substring(startIndex, endIndex).Length > 1){ el._bullet = bat; el._bulletOffset = bulletOffset; } } lines.Add(el); } //finally Draw the text fragments TextElement[] elems = new TextElement[lines.Count]; return lines.ToArray(elems); }
public void TestCompatibility2() { // This test case does not work well on Java 1.4/1.4.1 environment, // because of insufficient Bidi implementation in these versions. /*String javaVersion = System.Environment.GetEnvironmentVariable("java.version"); * if (javaVersion.StartsWith("1.4.0") || javaVersion.StartsWith("1.4.1")) { * Logln("\nSkipping TestCompatibility. The test case is known to fail on Java " + javaVersion + "\n"); + return; + }*/ Logln("\nEntering TestCompatibility\n"); /* check constant field values */ int val; val = IBM.ICU.Text.Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT; val = IBM.ICU.Text.Bidi.DIRECTION_DEFAULT_RIGHT_TO_LEFT; val = IBM.ICU.Text.Bidi.DIRECTION_LEFT_TO_RIGHT; val = IBM.ICU.Text.Bidi.DIRECTION_RIGHT_TO_LEFT; Logln("last val = " + val); String[] data = { "", /* * the following 2 cases are skipped, because java.text.Bidi has bugs * which cause discrepancies "abc", "ABC", */ "abc def", "ABC DEF", "abc 123 def", "ABC 123 DEF", "abc DEF ghi", "abc DEF 123 xyz","abc GHIJ 12345 def KLM" }; int dataCnt = data.Length; Bidi bidi; Bidi jbidi; for (int i = 0; i < dataCnt; i++) { String src = IBM.ICU.Charset.BidiTest.PseudoToU16(data[i]); bidi = new Bidi(src, IBM.ICU.Text.Bidi.DIRECTION_LEFT_TO_RIGHT); jbidi = new Bidi(src, Bidi.DIRECTION_LEFT_TO_RIGHT); CompareBidi(bidi, jbidi); bidi = new Bidi(src, IBM.ICU.Text.Bidi.DIRECTION_RIGHT_TO_LEFT); jbidi = new Bidi(src, Bidi.DIRECTION_RIGHT_TO_LEFT); CompareBidi(bidi, jbidi); char[] chars = src.ToCharArray(); bidi = new Bidi(chars, 0, null, 0, chars.Length, IBM.ICU.Text.Bidi.DIRECTION_LEFT_TO_RIGHT); jbidi = new Bidi(chars, 0, null, 0, chars.Length, Bidi.DIRECTION_LEFT_TO_RIGHT); CompareBidi(bidi, jbidi); } /* check bogus flags */ bidi = new Bidi("abc", 999); AssertEquals("\nDirection should be LTR", IBM.ICU.Text.Bidi.LTR, bidi.GetDirection()); /* check constructor with overriding embeddings */ bidi = new Bidi(new char[] { 's', 's', 's' }, 0, new sbyte[] { (sbyte)-7, (sbyte)-2, (sbyte)-3 }, 0, 3, IBM.ICU.Text.Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT); jbidi = new Bidi(new char[] { 's', 's', 's' }, 0, new sbyte[] { (sbyte)-7, (sbyte)-2, (sbyte)-3 }, 0, 3, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT); AttributedString al = new AttributedString( "HEBREW 123 english MOREHEB"); al.AddAttribute(TextAttribute.RUN_DIRECTION, TextAttribute.RUN_DIRECTION_RTL); al.AddAttribute(TextAttribute.NUMERIC_SHAPING, NumericShaper.GetShaper(NumericShaper.ARABIC)); al.AddAttribute(TextAttribute.BIDI_EMBEDDING, 1, 0, 26); al.AddAttribute(TextAttribute.BIDI_EMBEDDING, -1, 0, 6); al.AddAttribute(TextAttribute.BIDI_EMBEDDING, -1, 19, 26); AttributedCharacterIterator aci = al.GetIterator(); bidi = new Bidi(aci); jbidi = new Bidi(aci); CompareBidi(bidi, jbidi); String xout = bidi.WriteReordered(0); Logln("Output #1 of Bidi(AttributedCharacterIterator): " + xout); al = new AttributedString("HEBREW 123 english MOREHEB"); al.AddAttribute(TextAttribute.RUN_DIRECTION, TextAttribute.RUN_DIRECTION_RTL); al.AddAttribute(TextAttribute.BIDI_EMBEDDING, 0, 0, 26); aci = al.GetIterator(); bidi = new Bidi(aci); jbidi = new Bidi(aci); CompareBidi(bidi, jbidi); xout = bidi.WriteReordered(0); Logln("Output #2 of Bidi(AttributedCharacterIterator): " + xout); al = new AttributedString("HEBREW 123 english MOREHEB"); aci = al.GetIterator(); bidi = new Bidi(aci); jbidi = new Bidi(aci); CompareBidi(bidi, jbidi); xout = bidi.WriteReordered(0); Logln("Output #3 of Bidi(AttributedCharacterIterator): " + xout); char[] text = "abc==(123)==>def".ToCharArray(); bidi = new Bidi(text, 3, null, 0, 10, IBM.ICU.Text.Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT); jbidi = new Bidi(text, 3, null, 0, 10, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT); CompareBidi(bidi, jbidi); xout = bidi.WriteReordered(0); Logln("Output of Bidi(abc==(123)==>def,3,null,0,10, DEFAULT_LTR): " + xout); bidi = new Bidi(text, 3, null, 0, 10, IBM.ICU.Text.Bidi.DIRECTION_DEFAULT_RIGHT_TO_LEFT); jbidi = new Bidi(text, 3, null, 0, 10, Bidi.DIRECTION_DEFAULT_RIGHT_TO_LEFT); CompareBidi(bidi, jbidi); xout = bidi.WriteReordered(0); Logln("Output of Bidi(abc==(123)==>def,3,null,0,10, DEFAULT_RTL): " + xout); sbyte[] levels = new sbyte[] { 0, 0, 0, -1, -1, -1, 0, 0, 0, 0 }; bidi = new Bidi(text, 3, levels, 0, 10, IBM.ICU.Text.Bidi.DIRECTION_LEFT_TO_RIGHT); jbidi = new Bidi(text, 3, levels, 0, 10, Bidi.DIRECTION_LEFT_TO_RIGHT); CompareBidi(bidi, jbidi); xout = bidi.WriteReordered(0); Logln("Output of Bidi(abc==(123)==>def,3,levels,0,10, LTR): " + xout); bidi = new Bidi(text, 3, levels, 0, 10, IBM.ICU.Text.Bidi.DIRECTION_DEFAULT_RIGHT_TO_LEFT); jbidi = new Bidi(text, 3, levels, 0, 10, Bidi.DIRECTION_DEFAULT_RIGHT_TO_LEFT); CompareBidi(bidi, jbidi); xout = bidi.WriteReordered(0); Logln("Output of Bidi(abc==(123)==>def,3,levels,0,10, DEFAULT_RTL): " + xout); /* test reorderVisually */ sbyte[] myLevels = new sbyte[] { 1, 2, 0, 1, 2, 1, 2, 0, 1, 2 }; object[] objects = new object[10]; levels = new sbyte[objects.Length]; for (int i_0 = 0; i_0 < objects.Length; i_0++) { objects[i_0] = (char)((char)('a' + i_0)); levels[i_0] = myLevels[i_0]; } IBM.ICU.Text.Bidi.ReorderVisually(levels, 3, objects, 3, 7); String strbidi = ""; for (int i_1 = 0; i_1 < objects.Length; i_1++) { strbidi += objects[i_1].ToString(); } for (int i_2 = 0; i_2 < objects.Length; i_2++) { objects[i_2] = (char)((char)('a' + i_2)); levels[i_2] = myLevels[i_2]; } Bidi.ReorderVisually(levels, 3, objects, 3, 7); String strjbidi = ""; for (int i_3 = 0; i_3 < objects.Length; i_3++) { strjbidi += objects[i_3].ToString(); } if (!strjbidi.Equals(strbidi)) { Errln("Discrepancy in reorderVisually " + "\n bidi: " + strbidi + "\n jbidi: " + strjbidi); } else { Logln("Correct match in reorderVisually " + "\n bidi: " + strbidi + "\n jbidi: " + strjbidi); } Logln("\nExiting TestCompatibility\n"); }