コード例 #1
0
ファイル: TableSprmCompressor.cs プロジェクト: 89sos98/npoi
        public static byte[] CompressTableProperty(TableProperties newTAP)
        {
            int size = 0;
            ArrayList sprmList = new ArrayList();

            if (newTAP.GetJc() != 0)
            {
                size += SprmUtils.AddSprm((short)0x5400, newTAP.GetJc(), null, sprmList);
            }
            if (newTAP.GetFCantSplit())
            {
                size += SprmUtils.AddSprm((short)0x3403, 1, null, sprmList);
            }
            if (newTAP.GetFTableHeader())
            {
                size += SprmUtils.AddSprm((short)0x3404, 1, null, sprmList);
            }
            byte[] brcBuf = new byte[6 * BorderCode.SIZE];
            int offset = 0;
            newTAP.GetBrcTop().Serialize(brcBuf, offset);
            offset += BorderCode.SIZE;
            newTAP.GetBrcLeft().Serialize(brcBuf, offset);
            offset += BorderCode.SIZE;
            newTAP.GetBrcBottom().Serialize(brcBuf, offset);
            offset += BorderCode.SIZE;
            newTAP.GetBrcRight().Serialize(brcBuf, offset);
            offset += BorderCode.SIZE;
            newTAP.GetBrcHorizontal().Serialize(brcBuf, offset);
            offset += BorderCode.SIZE;
            newTAP.GetBrcVertical().Serialize(brcBuf, offset);
            byte[] compare = new byte[6 * BorderCode.SIZE];
            if (!Arrays.Equals(brcBuf, compare))
            {
                size += SprmUtils.AddSprm(unchecked((short)0xD605), 0, brcBuf, sprmList);
            }
            if (newTAP.GetDyaRowHeight() != 0)
            {
                size += SprmUtils.AddSprm(unchecked((short)0x9407), newTAP.GetDyaRowHeight(), null, sprmList);
            }
            if (newTAP.GetItcMac() > 0)
            {
                int itcMac = newTAP.GetItcMac();
                byte[] buf = new byte[1 + (LittleEndianConsts.SHORT_SIZE * (itcMac + 1)) + (TableCellDescriptor.SIZE * itcMac)];
                buf[0] = (byte)itcMac;

                short[] dxaCenters = newTAP.GetRgdxaCenter();
                for (int x = 0; x < dxaCenters.Length; x++)
                {
                    LittleEndian.PutShort(buf, 1 + (x * LittleEndianConsts.SHORT_SIZE),
                                          dxaCenters[x]);
                }

                TableCellDescriptor[] cellDescriptors = newTAP.GetRgtc();
                for (int x = 0; x < cellDescriptors.Length; x++)
                {
                    cellDescriptors[x].Serialize(buf,
                      1 + ((itcMac + 1) * LittleEndianConsts.SHORT_SIZE) + (x * TableCellDescriptor.SIZE));
                }
                size += SprmUtils.AddSpecialSprm(unchecked((short)0xD608), buf, sprmList);

                //      buf = new byte[(itcMac * ShadingDescriptor.SIZE) + 1];
                //      buf[0] = (byte)itcMac;
                //      ShadingDescriptor[] shds = newTAP.GetRgshd();
                //      for (int x = 0; x < itcMac; x++)
                //      {
                //        shds[x].Serialize(buf, 1 + (x * ShadingDescriptor.SIZE));
                //      }
                //      size += SprmUtils.AddSpecialSprm((short)0xD609, buf, sprmList);
            }
            if (newTAP.GetTlp() != 0)
            {
                size += SprmUtils.AddSprm((short)0x740a, newTAP.GetTlp(), null, sprmList);
            }

            return SprmUtils.GetGrpprl(sprmList, size);
        }
コード例 #2
0
        /**
         * Used to uncompress a table property. Performs an operation defined
         * by a sprm stored in a tapx.
         *
         * @param newTAP The TableProperties object to perform the operation on.
         * @param operand The operand that defines this operation.
         * @param param The parameter for this operation.
         * @param varParam Variable length parameter for this operation.
         */
        static void UncompressTAPOperation(TableProperties newTAP, SprmOperation sprm)
        {
            switch (sprm.Operation)
            {
                case 0:
                    newTAP.SetJc((short)sprm.Operand);
                    break;
                case 0x01:
                    {
                        short[] rgdxaCenter = newTAP.GetRgdxaCenter();
                        short itcMac = newTAP.GetItcMac();
                        int adjust = sprm.Operand - (rgdxaCenter[0] + newTAP.GetDxaGapHalf());
                        for (int x = 0; x < itcMac; x++)
                        {
                            rgdxaCenter[x] += (short)adjust;
                        }
                        break;
                    }
                case 0x02:
                    {
                        short[] rgdxaCenter = newTAP.GetRgdxaCenter();
                        if (rgdxaCenter != null)
                        {
                            int adjust = newTAP.GetDxaGapHalf() - sprm.Operand;
                            rgdxaCenter[0] += (short)adjust;
                        }
                        newTAP.SetDxaGapHalf(sprm.Operand);
                        break;
                    }
                case 0x03:
                    newTAP.SetFCantSplit(GetFlag(sprm.Operand));
                    break;
                case 0x04:
                    newTAP.SetFTableHeader(GetFlag(sprm.Operand));
                    break;
                case 0x05:
                    {
                        byte[] buf = sprm.Grpprl;
                        int offset = sprm.GrpprlOffset;
                        newTAP.SetBrcTop(new BorderCode(buf, offset));
                        offset += BorderCode.SIZE;
                        newTAP.SetBrcLeft(new BorderCode(buf, offset));
                        offset += BorderCode.SIZE;
                        newTAP.SetBrcBottom(new BorderCode(buf, offset));
                        offset += BorderCode.SIZE;
                        newTAP.SetBrcRight(new BorderCode(buf, offset));
                        offset += BorderCode.SIZE;
                        newTAP.SetBrcHorizontal(new BorderCode(buf, offset));
                        offset += BorderCode.SIZE;
                        newTAP.SetBrcVertical(new BorderCode(buf, offset));
                        break;
                    }
                case 0x06:

                    //obsolete, used in word 1.x
                    break;
                case 0x07:
                    newTAP.SetDyaRowHeight(sprm.Operand);
                    break;
                case 0x08:
                    {
                        byte[] grpprl = sprm.Grpprl;
                        int offset = sprm.GrpprlOffset;
                        short itcMac = grpprl[offset];
                        short[] rgdxaCenter = new short[itcMac + 1];
                        TableCellDescriptor[] rgtc = new TableCellDescriptor[itcMac];
                        //I use varParam[0] and newTAP._itcMac interchangably
                        newTAP.SetItcMac(itcMac);
                        newTAP.SetRgdxaCenter(rgdxaCenter);
                        newTAP.SetRgtc(rgtc);

                        // get the rgdxaCenters
                        for (int x = 0; x < itcMac; x++)
                        {
                            rgdxaCenter[x] = LittleEndian.GetShort(grpprl, offset + (1 + (x * 2)));
                        }

                        // only try to get the TC entries if they exist...
                        int endOfSprm = offset + sprm.Size - 6; // -2 bytes for sprm - 2 for size short - 2 to correct Offsets being 0 based
                        int startOfTCs = offset + (1 + (itcMac + 1) * 2);

                        bool hasTCs = startOfTCs < endOfSprm;

                        for (int x = 0; x < itcMac; x++)
                        {
                            // Sometimes, the grpprl does not contain data at every Offset. I have no idea why this happens.
                            if (hasTCs && offset + (1 + ((itcMac + 1) * 2) + (x * 20)) < grpprl.Length)
                                rgtc[x] = TableCellDescriptor.ConvertBytesToTC(grpprl,
                                   offset + (1 + ((itcMac + 1) * 2) + (x * 20)));
                            else
                                rgtc[x] = new TableCellDescriptor();
                        }

                        rgdxaCenter[itcMac] = LittleEndian.GetShort(grpprl, offset + (1 + (itcMac * 2)));
                        break;
                    }
                case 0x09:

                    /** @todo handle cell shading*/
                    break;
                case 0x0a:

                    /** @todo handle word defined table styles*/
                    break;
                case 0x20:
                    //      {
                    //        TableCellDescriptor[] rgtc = newTAP.GetRgtc();
                    //
                    //        for (int x = varParam[0]; x < varParam[1]; x++)
                    //        {
                    //
                    //          if ((varParam[2] & 0x08) > 0)
                    //          {
                    //            short[] brcRight = rgtc[x].GetBrcRight ();
                    //            brcRight[0] = LittleEndian.Getshort (varParam, 6);
                    //            brcRight[1] = LittleEndian.Getshort (varParam, 8);
                    //          }
                    //          else if ((varParam[2] & 0x04) > 0)
                    //          {
                    //            short[] brcBottom = rgtc[x].GetBrcBottom ();
                    //            brcBottom[0] = LittleEndian.Getshort (varParam, 6);
                    //            brcBottom[1] = LittleEndian.Getshort (varParam, 8);
                    //          }
                    //          else if ((varParam[2] & 0x02) > 0)
                    //          {
                    //            short[] brcLeft = rgtc[x].GetBrcLeft ();
                    //            brcLeft[0] = LittleEndian.Getshort (varParam, 6);
                    //            brcLeft[1] = LittleEndian.Getshort (varParam, 8);
                    //          }
                    //          else if ((varParam[2] & 0x01) > 0)
                    //          {
                    //            short[] brcTop = rgtc[x].GetBrcTop ();
                    //            brcTop[0] = LittleEndian.Getshort (varParam, 6);
                    //            brcTop[1] = LittleEndian.Getshort (varParam, 8);
                    //          }
                    //        }
                    //        break;
                    //      }
                    break;
                case 0x21:
                    {
                        int param = sprm.Operand;
                        int index = (int)(param & 0xff000000) >> 24;
                        int count = (param & 0x00ff0000) >> 16;
                        int width = (param & 0x0000ffff);
                        int itcMac = newTAP.GetItcMac();

                        short[] rgdxaCenter = new short[itcMac + count + 1];
                        TableCellDescriptor[] rgtc = new TableCellDescriptor[itcMac + count];
                        if (index >= itcMac)
                        {
                            index = itcMac;
                            Array.Copy(newTAP.GetRgdxaCenter(), 0, rgdxaCenter, 0,
                                             itcMac + 1);
                            Array.Copy(newTAP.GetRgtc(), 0, rgtc, 0, itcMac);
                        }
                        else
                        {
                            //copy rgdxaCenter
                            Array.Copy(newTAP.GetRgdxaCenter(), 0, rgdxaCenter, 0,
                                             index + 1);
                            Array.Copy(newTAP.GetRgdxaCenter(), index + 1, rgdxaCenter,
                                             index + count, itcMac - (index));
                            //copy rgtc
                            Array.Copy(newTAP.GetRgtc(), 0, rgtc, 0, index);
                            Array.Copy(newTAP.GetRgtc(), index, rgtc, index + count,
                                             itcMac - index);
                        }

                        for (int x = index; x < index + count; x++)
                        {
                            rgtc[x] = new TableCellDescriptor();
                            rgdxaCenter[x] = (short)(rgdxaCenter[x - 1] + width);
                        }
                        rgdxaCenter[index +
                          count] = (short)(rgdxaCenter[(index + count) - 1] + width);
                        break;
                    }
                /**@todo handle table sprms from complex files*/
                case 0x22:
                case 0x23:
                case 0x24:
                case 0x25:
                case 0x26:
                case 0x27:
                case 0x28:
                case 0x29:
                case 0x2a:
                case 0x2b:
                case 0x2c:
                    break;
                default:
                    break;
            }
        }
コード例 #3
0
        private void initCells()
        {
            if (_cellsFound)
            {
                return;
            }

            short expectedCellsCount = _tprops.GetItcMac();

            int lastCellStart      = 0;
            List <TableCell> cells = new List <TableCell>(
                expectedCellsCount + 1);

            for (int p = 0; p < NumParagraphs; p++)
            {
                Paragraph paragraph = GetParagraph(p);
                String    s         = paragraph.Text;

                if (((s.Length > 0 && s[s.Length - 1] == TABLE_CELL_MARK) || paragraph
                     .IsEmbeddedCellMark()) &&
                    paragraph.GetTableLevel() == _levelNum)
                {
                    TableCellDescriptor tableCellDescriptor = _tprops.GetRgtc() != null &&
                                                              _tprops.GetRgtc().Length > cells.Count ? _tprops
                                                              .GetRgtc()[cells.Count] : new TableCellDescriptor();
                    short leftEdge = (_tprops.GetRgdxaCenter() != null &&
                                      _tprops.GetRgdxaCenter().Length > cells.Count) ? (short)_tprops
                                     .GetRgdxaCenter()[cells.Count] : (short)0;
                    short rightEdge = (_tprops.GetRgdxaCenter() != null &&
                                       _tprops.GetRgdxaCenter().Length > cells.Count + 1) ? (short)_tprops
                                      .GetRgdxaCenter()[cells.Count + 1] : (short)0;

                    TableCell tableCell = new TableCell(GetParagraph(
                                                            lastCellStart).StartOffset, GetParagraph(p)
                                                        .EndOffset, this, _levelNum, tableCellDescriptor,
                                                        leftEdge, rightEdge - leftEdge);
                    cells.Add(tableCell);
                    lastCellStart = p + 1;
                }
            }

            if (lastCellStart < (NumParagraphs - 1))
            {
                TableCellDescriptor tableCellDescriptor = _tprops.GetRgtc() != null &&
                                                          _tprops.GetRgtc().Length > cells.Count ? _tprops
                                                          .GetRgtc()[cells.Count] : new TableCellDescriptor();
                short leftEdge = _tprops.GetRgdxaCenter() != null &&
                                 _tprops.GetRgdxaCenter().Length > cells.Count ? (short)_tprops
                                 .GetRgdxaCenter()[cells.Count] : (short)0;
                short rightEdge = _tprops.GetRgdxaCenter() != null &&
                                  _tprops.GetRgdxaCenter().Length > cells.Count + 1 ? (short)_tprops
                                  .GetRgdxaCenter()[cells.Count + 1] : (short)0;

                TableCell tableCell = new TableCell(lastCellStart,
                                                    (NumParagraphs - 1), this, _levelNum,
                                                    tableCellDescriptor, leftEdge, rightEdge - leftEdge);
                cells.Add(tableCell);
            }

            if (cells.Count > 0)
            {
                TableCell lastCell = cells[cells.Count - 1];
                if (lastCell.NumParagraphs == 1 &&
                    (lastCell.GetParagraph(0).IsTableRowEnd()))
                {
                    // remove "fake" cell
                    cells.RemoveAt(cells.Count - 1);
                }
            }

            if (cells.Count != expectedCellsCount)
            {
                _tprops.SetItcMac((short)cells.Count);
            }

            _cells      = cells.ToArray();
            _cellsFound = true;
        }