/** * Applies a font to the specified characters of a string. * * @param startIndex The start index to apply the font to (inclusive) * @param endIndex The end index to apply to font to (exclusive) * @param font The index of the font to use. */ public void ApplyFont(int startIndex, int endIndex, IFont font) { if (startIndex > endIndex) { throw new ArgumentException("Start index must be less than end index, but had " + startIndex + " and " + endIndex); } if (startIndex < 0 || endIndex > Length) { throw new ArgumentException("Start and end index not in range, but had " + startIndex + " and " + endIndex); } if (startIndex == endIndex) { return; } if (st.sizeOfRArray() == 0 && st.IsSetT()) { //convert <t>string</t> into a text Run: <r><t>string</t></r> st.AddNewR().t = (st.t); st.unsetT(); } String text = this.String; XSSFFont xssfFont = (XSSFFont)font; SortedDictionary <int, CT_RPrElt> formats = GetFormatMap(st); CT_RPrElt fmt = new CT_RPrElt(); SetRunAttributes(xssfFont.GetCTFont(), fmt); ApplyFont(formats, startIndex, endIndex, fmt); CT_Rst newSt = buildCTRst(text, formats); st.Set(newSt); }
CT_Rst buildCTRst(String text, SortedDictionary <int, CT_RPrElt> formats) { if (text.Length != GetLastKey(formats.Keys)) { throw new ArgumentException("Text length was " + text.Length + " but the last format index was " + GetLastKey(formats.Keys)); } CT_Rst st = new CT_Rst(); int runStartIdx = 0; for (SortedDictionary <int, CT_RPrElt> .KeyCollection.Enumerator it = formats.Keys.GetEnumerator(); it.MoveNext();) { int runEndIdx = it.Current; CT_RElt run = st.AddNewR(); String fragment = text.Substring(runStartIdx, runEndIdx - runStartIdx); run.t = (fragment); PreserveSpaces(run.t); CT_RPrElt fmt = formats[runEndIdx]; if (fmt != null) { run.rPr = (fmt); } runStartIdx = runEndIdx; } return(st); }
CT_Rst BuildCTRst(String text, SortedDictionary <int, CT_RPrElt> formats) { if (text.Length != GetLastKey(formats.Keys)) { throw new ArgumentException("Text length was " + text.Length + " but the last format index was " + GetLastKey(formats.Keys)); } CT_Rst st = new CT_Rst(); int runStartIdx = 0; foreach (KeyValuePair <int, CT_RPrElt> kv in formats) { int runEndIdx = kv.Key; CT_RElt run = st.AddNewR(); String fragment = text.Substring(runStartIdx, runEndIdx - runStartIdx); run.t = (fragment); PreserveSpaces(run.t); CT_RPrElt fmt = kv.Value; if (fmt != null) { run.rPr = fmt; } runStartIdx = runEndIdx; } return(st); }
private CT_Rst buildCTRst(string text, SortedDictionary <int, CT_RPrElt> formats) { if (text.Length != this.GetLastKey(formats.Keys)) { throw new ArgumentException("Text length was " + (object)text.Length + " but the last format index was " + (object)this.GetLastKey(formats.Keys)); } CT_Rst ctRst = new CT_Rst(); int startIndex = 0; SortedDictionary <int, CT_RPrElt> .KeyCollection.Enumerator enumerator = formats.Keys.GetEnumerator(); while (enumerator.MoveNext()) { int current = enumerator.Current; CT_RElt ctRelt = ctRst.AddNewR(); string str = text.Substring(startIndex, current - startIndex); ctRelt.t = str; XSSFRichTextString.PreserveSpaces(ctRelt.t); CT_RPrElt format = formats[current]; if (format != null) { ctRelt.rPr = format; } startIndex = current; } return(ctRst); }
public void TestCreateNew() { SharedStringsTable sst = new SharedStringsTable(); CT_Rst st; int idx; // Check defaults Assert.IsNotNull(sst.Items); Assert.AreEqual(0, sst.Items.Count); Assert.AreEqual(0, sst.Count); Assert.AreEqual(0, sst.UniqueCount); st = new CT_Rst(); st.t = ("Hello, World!"); idx = sst.AddEntry(st); Assert.AreEqual(0, idx); Assert.AreEqual(1, sst.Count); Assert.AreEqual(1, sst.UniqueCount); //add the same entry again idx = sst.AddEntry(st); Assert.AreEqual(0, idx); Assert.AreEqual(2, sst.Count); Assert.AreEqual(1, sst.UniqueCount); //and again idx = sst.AddEntry(st); Assert.AreEqual(0, idx); Assert.AreEqual(3, sst.Count); Assert.AreEqual(1, sst.UniqueCount); st = new CT_Rst(); st.t = ("Second string"); idx = sst.AddEntry(st); Assert.AreEqual(1, idx); Assert.AreEqual(4, sst.Count); Assert.AreEqual(2, sst.UniqueCount); //add the same entry again idx = sst.AddEntry(st); Assert.AreEqual(1, idx); Assert.AreEqual(5, sst.Count); Assert.AreEqual(2, sst.UniqueCount); st = new CT_Rst(); CT_RElt r = st.AddNewR(); CT_RPrElt pr = r.AddNewRPr(); pr.AddNewColor().SetRgb(new byte[] { (byte)0xFF, 0, 0 }); //red pr.AddNewI().val = (true); //bold pr.AddNewB().val = (true); //italic r.t = ("Second string"); idx = sst.AddEntry(st); Assert.AreEqual(2, idx); Assert.AreEqual(6, sst.Count); Assert.AreEqual(3, sst.UniqueCount); idx = sst.AddEntry(st); Assert.AreEqual(2, idx); Assert.AreEqual(7, sst.Count); Assert.AreEqual(3, sst.UniqueCount); //OK. the sst table is Filled, check the contents Assert.AreEqual(3, sst.Items.Count); Assert.AreEqual("Hello, World!", new XSSFRichTextString(sst.GetEntryAt(0)).ToString()); Assert.AreEqual("Second string", new XSSFRichTextString(sst.GetEntryAt(1)).ToString()); Assert.AreEqual("Second string", new XSSFRichTextString(sst.GetEntryAt(2)).ToString()); }
public void TestApplyFont_lowlevel() { CT_Rst st = new CT_Rst(); String text = "Apache Software Foundation"; XSSFRichTextString str = new XSSFRichTextString(text); Assert.AreEqual(26, text.Length); st.AddNewR().t = (text); //SortedDictionary<int, CT_RPrElt> formats = str.GetFormatMap(st); //Assert.AreEqual(1, formats.Count); //Assert.AreEqual(26, (int)formats.Keys[0]); //Assert.IsNull(formats.Get(formats.firstKey())); //CT_RPrElt fmt1 = new CT_RPrElt(); //str.ApplyFont(formats, 0, 6, fmt1); //Assert.AreEqual(2, formats.Count); //Assert.AreEqual("[6, 26]", formats.Keys.ToString()); //Object[] Runs1 = formats.Values.ToArray(); //Assert.AreSame(fmt1, Runs1[0]); //Assert.AreSame(null, Runs1[1]); //CT_RPrElt fmt2 = new CT_RPrElt(); //str.ApplyFont(formats, 7, 15, fmt2); //Assert.AreEqual(4, formats.Count); //Assert.AreEqual("[6, 7, 15, 26]", formats.Keys.ToString()); //Object[] Runs2 = formats.Values.ToArray(); //Assert.AreSame(fmt1, Runs2[0]); //Assert.AreSame(null, Runs2[1]); //Assert.AreSame(fmt2, Runs2[2]); //Assert.AreSame(null, Runs2[3]); //CT_RPrElt fmt3 = new CT_RPrElt(); //str.ApplyFont(formats, 6, 7, fmt3); //Assert.AreEqual(4, formats.Count); //Assert.AreEqual("[6, 7, 15, 26]", formats.Keys.ToString()); //Object[] Runs3 = formats.Values.ToArray(); //Assert.AreSame(fmt1, Runs3[0]); //Assert.AreSame(fmt3, Runs3[1]); //Assert.AreSame(fmt2, Runs3[2]); //Assert.AreSame(null, Runs3[3]); //CT_RPrElt fmt4 = new CT_RPrElt(); //str.ApplyFont(formats, 0, 7, fmt4); //Assert.AreEqual(3, formats.Count); //Assert.AreEqual("[7, 15, 26]", formats.Keys.ToString()); //Object[] Runs4 = formats.Values.ToArray(); //Assert.AreSame(fmt4, Runs4[0]); //Assert.AreSame(fmt2, Runs4[1]); //Assert.AreSame(null, Runs4[2]); //CT_RPrElt fmt5 = new CT_RPrElt(); //str.ApplyFont(formats, 0, 26, fmt5); //Assert.AreEqual(1, formats.Count); //Assert.AreEqual("[26]", formats.Keys.ToString()); //Object[] Runs5 = formats.Values.ToArray(); //Assert.AreSame(fmt5, Runs5[0]); //CT_RPrElt fmt6 = new CT_RPrElt(); //str.ApplyFont(formats, 15, 26, fmt6); //Assert.AreEqual(2, formats.Count); //Assert.AreEqual("[15, 26]", formats.Keys.ToString()); //Object[] Runs6 = formats.Values.ToArray(); //Assert.AreSame(fmt5, Runs6[0]); //Assert.AreSame(fmt6, Runs6[1]); //str.ApplyFont(formats, 0, 26, null); //Assert.AreEqual(1, formats.Count); //Assert.AreEqual("[26]", formats.Keys.ToString()); //Object[] Runs7 = formats.Values.ToArray(); //Assert.AreSame(null, Runs7[0]); //str.ApplyFont(formats, 15, 26, fmt6); //Assert.AreEqual(2, formats.Count); //Assert.AreEqual("[15, 26]", formats.Keys.ToString()); //Object[] Runs8 = formats.Values.ToArray(); //Assert.AreSame(null, Runs8[0]); //Assert.AreSame(fmt6, Runs8[1]); //str.ApplyFont(formats, 15, 26, fmt5); //Assert.AreEqual(2, formats.Count); //Assert.AreEqual("[15, 26]", formats.Keys.ToString()); //Object[] Runs9 = formats.Values.ToArray(); //Assert.AreSame(null, Runs9[0]); //Assert.AreSame(fmt5, Runs9[1]); //str.ApplyFont(formats, 2, 20, fmt6); //Assert.AreEqual(3, formats.Count); //Assert.AreEqual("[2, 20, 26]", formats.Keys.ToString()); //Object[] Runs10 = formats.Values.ToArray(); //Assert.AreSame(null, Runs10[0]); //Assert.AreSame(fmt6, Runs10[1]); //Assert.AreSame(fmt5, Runs10[2]); //str.ApplyFont(formats, 22, 24, fmt4); //Assert.AreEqual(5, formats.Count); //Assert.AreEqual("[2, 20, 22, 24, 26]", formats.Keys.ToString()); //Object[] Runs11 = formats.Values.ToArray(); //Assert.AreSame(null, Runs11[0]); //Assert.AreSame(fmt6, Runs11[1]); //Assert.AreSame(fmt5, Runs11[2]); //Assert.AreSame(fmt4, Runs11[3]); //Assert.AreSame(fmt5, Runs11[4]); //str.ApplyFont(formats, 0, 10, fmt1); //Assert.AreEqual(5, formats.Count); //Assert.AreEqual("[10, 20, 22, 24, 26]", formats.Keys.ToString()); //Object[] Runs12 = formats.Values.ToArray(); //Assert.AreSame(fmt1, Runs12[0]); //Assert.AreSame(fmt6, Runs12[1]); //Assert.AreSame(fmt5, Runs12[2]); //Assert.AreSame(fmt4, Runs12[3]); //Assert.AreSame(fmt5, Runs12[4]); Assert.Fail("implement STXString"); }