10 Replies Latest reply: Mar 28, 2008 2:28 AM by (jxswm) RSS

    [JS] [CS2] removing baseline shift - Performance problem

    Community Member
      Hi,

      A few days ago, I posted a small problem regarding the removal of baseline shift. The solution added to the forum worked great....

      I've used it like this ...

      for (var j = app.activeDocument.stories.length - 1; j >= 0; j--) {
      for (var k = app.activeDocument.stories[j].paragraphs.length- 1; k >= 0;
      for (var h = app.activeDocument.stories[j].paragraphs[k].characters.length- 1; h >= 0; h--) {
      if (app.activeDocument.stories[j].paragraphs[k].characters[h].pageItems.length < 1){
      if (app.activeDocument.stories[j].paragraphs[k].characters[h].baselineShift != 0){
      app.activeDocument.stories[j].paragraphs[k].characters[h].baselineShift = 0 ;
      }
      }
      }
      }
      }

      What I'm trying to do, is to remove the baseline shift, when the character is not an inline graphic....

      I've runned the script and it works great on small documents. However, when running it on a 60 page document, it needs 31 minutes to do it's thing...
      This is to slow... But then again, it checks every character...

      I've tried using a search, but this doesn't work. I used the code below ...

      app.findPreferences = app.changePreferences = null;
      app.findPreferences.pageItems.length = 0;
      app.changePreferences.baselineShift = 0;
      app.activeDocument.search('',undefined,undefined,'');

      Does anyone have a solution?

      Thanx

      Tim
        • 1. Re: [JS] [CS2] removing baseline shift - Performance problem
          Fred Goldman Community Member
          Try this:

          for (var j = app.activeDocument.stories.length - 1; j >= 0; j--) {
          myCharacters=app.activeDocument.stories[j].characters.everyItem().getElements()
          for (var h = myCharacters.length- 1; h >= 0; h--) {
          if (myCharacters[h].pageItems.length < 1){
          if (myCharacters[h].baselineShift != 0){
          myCharacters[h].baselineShift = 0;
          }
          }
          }
          }
          • 2. Re: [JS] [CS2] removing baseline shift - Performance problem
            Community Member
            Hi Fred,

            Thanx for your input ...

            I think your code is somewhat faster...

            However, It still takes more than 20 minutes to be executed...

            Tim
            • 3. Re: [JS] [CS2] removing baseline shift - Performance problem
              [Jongware] Community Member
              A tiny tiny tiny optimization is removing one single "if".

              >if (myCharacters[h].baselineShift != 0){
              >myCharacters[h].baselineShift = 0;
              >}

              If it isn't zero set it to zero ... You can remove the test! That's, assuming plain
              i setting
              the value when it didn't change (i.e., when it's already 0), takes less time than first testing, then setting if !0. But it's worth a try as this removes one instruction in your inner loop.

              There are plenty scenarios where testing takes up a lot of time and setting values don't, and others that work the other way around.
              • 4. Re: [JS] [CS2] removing baseline shift - Performance problem
                Fred Goldman Community Member
                Ok, the problem with doing a search is that we don't know what to search
                for. Are all the baseline shifts the same value? There is no way to
                search for anything that has > 0 baseline shift.
                • 5. Re: [JS] [CS2] removing baseline shift - Performance problem
                  Fred Goldman Community Member
                  What kind of processor do you have? I just tried it on a 33 page
                  document and it took a little over a minute.

                  It might be a CS2 thing, I tried in CS3.
                  • 6. Re: [JS] [CS2] removing baseline shift - Performance problem
                    Community Member
                    Hi,

                    I removed the extra if loop...

                    I also tried cs3...

                    No visible performance gain ...

                    I use a 2 Ghz PowerPC G5 (2x) with 2 GB DDR SDRAM...
                    Running os X 10.4.11

                    I checked the documents... the baseline shift can be every value but 0...

                    I did a small test ...

                    var customValue = -50;

                    for (var z = 0; z < 100; z++){
                    customValue = customValue + 1;
                    app.findPreferences = app.changePreferences = null;
                    app.findPreferences.baselineShift = customValue;
                    app.changePreferences.baselineShift = 0;
                    app.activeDocument.search('',undefined,undefined,'');
                    }

                    This works very fast but also applies the baselineshift to inline graphics, which have to stay the same...

                    tim
                    • 7. Re: [JS] [CS2] removing baseline shift - Performance problem
                      Community Member
                      will be faster - much faster ;) if you will check TextStyleRanges not every Character ;)<br /><br />but I don't know (I can't check this now) what will be with InLineGraphics ...<br /><br />do InLineGraphics have applied any CharacterStyle ?? if yes - you can skip TextStyleRange if you find this CharStyle ...<br /><br />or you can use Search and search for BaselineShift = 0 and process all text "not found"<br /><br />[VB example]<br />' ################<br />set myFind=myStory.Search<br />for a=1 to myFind.Count-1<br /> myTextStart=myFind(a).Characters.item(-1).index+1<br /> myTextEnd=myFind(a+1).Characters.item(1).index-1<br /><br /> set myText=myStory.Texts.ItemByRange( _<br />     myStory.Characters.item(myTextStart), _<br />     myStory.Characters.item(myTextEnd)<br /><br /> myText.BaselineShift=0<br />next a<br />myTextStart=myFind(myFind.count).Characters.item(-1).index+1<br />myTextEnd=myStory.Characters.Count<br />if myTextStart<myTextEnd then<br /> set myText=myStory.Texts.ItemByRange( _<br />     myStory.Characters.item(myTextStart), _<br />     myStory.Characters.item(myTextEnd)<br /> myText.BaselineShift=0<br />end if<br />' ################<br /><br /><br />[edited]<br /><br />or you can check your TextStyleRange for PageItems.Count :)<br />and start processing Char-by-Char<br /><br /> if PageItems.Count>0 and CharCount>1 then<br />   ' check all chars to find and bypass InLineGraphics<br /> elseif PageItems.Count>1 and CharCount=1 then<br />   ' skip - TextStyleRange contains only InLineGraphics<br /> end if<br /><br /><br />robin<br /><br />-- <br />www.adobescripts.com
                      • 8. Re: [JS] [CS2] removing baseline shift - Performance problem
                        Fred Goldman Community Member
                        Hi John,

                        All you did there was a regular find/change. If you knew the values of
                        the baseline shift we wouldn't have to check every character, we would
                        first do a find (no change) and then loop through the finds and set the
                        baseline shift to 0 if it's not an inline.

                        I am very surprised it's taking such a long time. I am using a 3.0Ghz
                        Core2Duo, but I can't imagine that the processor would be the reason for
                        it taking 10x as long.

                        Try this. This is the best I think we can do:

                        myStories = app.activeDocument.stories.everyItem().getElements()
                        for (var j = myStories.length - 1; j >= 0; j--) {
                        myCharacters=myStories[j].characters.everyItem().getElements()
                        for (var h = myCharacters.length- 1; h >= 0; h--) {
                        if (myCharacters[h].pageItems.length < 1){
                        if (myCharacters[h].baselineShift != 0){
                        myCharacters[h].baselineShift = 0;
                        }
                        }
                        }
                        }
                        • 9. Re: [JS] [CS2] removing baseline shift - Performance problem
                          Harbs. CommunityMVP
                          How about this?

                          This uses Roberts idea of using textStyleRanges...

                          myStories = app.activeDocument.stories.everyItem().getElements();
                          for (var j = myStories.length - 1; j >= 0; j--) {
                          ranges=myStories[j].textStyleRanges.everyItem().getElements();
                          for (var h = ranges.length- 1; h >= 0; h--) {
                          if (ranges[h].pageItems.length < 1){
                          if (ranges[h].baselineShift != 0){
                          ranges[h].baselineShift = 0;
                          }
                          }
                          }
                          }
                          • 10. Re: [JS] [CS2] removing baseline shift - Performance problem
                            Community Member
                            this will be very quick.

                            ///////////////////////////////
                            //baselineShift !== 0 and exclude inline
                            function timer(){this.start = new Date();}
                            timer.prototype.restart = function(){this.start = new Date();}
                            timer.prototype.elapsed = function(){return (new Date().getTime() - this.start.getTime());}

                            var isID5 = parseInt(String(app.version))==5;

                            function baselineText(myText){
                            var chs, chsLen, chsBaselines, begin, end, isTrue, bFlag;
                            chs = myText.characters, chsLen = chs.length-1;
                            //firstNoInline == 0;
                            if(chsLen > -1){
                            //if(chsLen > 20000){return;}
                            if(chs[0].pageItems.length == 0 && chs[0].baselineShift != 0){
                            chs[0].baselineShift = 0; param.doCount += 1;
                            }
                            if(chsLen > 0){
                            //getAll baselineShift
                            //chsBaselines = chs.everyItem().baselineShift; //restrict
                            begin = end = chsLen, isTrue = false,
                            bFlag = chs[chsLen].baselineShift != 0 && chs[chsLen].pageItems.length == 0;
                            //one for get range
                            for(j = chsLen; j >= 0; j--){
                            //isTrue = chsBaselines[j] != 0 && chs[j].pageItems.length == 0; //restrict
                            isTrue = j != 0 && chs[j].pageItems.length == 0;
                            if(!isTrue){
                            if(bFlag && j < end){
                            begin = j+1;
                            //dst = chs.itemByRange(begin, end);
                            //dst.select(SelectionOptions.replaceWith);alert([begin, end].join("\n"));
                            //dst.baselineShift = 0;
                            chs.itemByRange(begin, end).baselineShift = 0;
                            param.doCount += 1;
                            bFlag = false;
                            }
                            end = j-1;
                            continue;
                            }
                            bFlag = true;
                            }
                            }
                            }
                            }

                            var myDoc, param;

                            myDoc = app.documents[0];

                            param = {};
                            param.doCount = 0;
                            param.t = new timer();

                            var myStories, myStoriesLen, foundsLen;
                            var i, j, myStory, founds;
                            myStories = myDoc.stories;
                            myStoriesLen = myStories.length-1;

                            if(isID5){
                            app.findTextPreferences = NothingEnum.nothing;
                            app.changeTextPreferences = NothingEnum.nothing;
                            app.findChangeTextOptions.properties = {wholeWord :false,caseSensitive:true, kanaSensitive:false,widthSensitive:true,includeMasterPages:true};
                            app.findTextPreferences.baselineShift = 0;
                            }else{
                            app.findPreferences = NothingEnum.nothing;
                            app.changePreferences = NothingEnum.nothing;
                            app.findPreferences.properties = {baselineShift:0, wholeWord :false,caseSensitive:true, kanaSensitive:false,widthSensitive:true};
                            }

                            if(isID5){if(app.scriptPreferences.enableRedraw){app.scriptPreferences.enableRedraw = !true;}}

                            for(i = myStoriesLen; i >=0; i--){
                            myStory = myStories[i];
                            if(isID5){
                            founds = myStory.findText(0);
                            }else{
                            founds = myStory.search();
                            }

                            foundsLen = founds.length-1;
                            //if not found and the story characters length > 20000, will very slow. by jxswm 2008-3-28 18:14:03
                            if(foundsLen == -1){
                            baselineText(myStory);
                            }else{
                            //bug: if(the cell has no baselineShift == 0, all >0 || <0, this cell will not cover. by jxswm 2008-3-28 17:19:20
                            var this_start, this_end, this_Parent, prev_start, prev_end, prev_Parent, endIndex, myText;
                            //from end: reverse order
                            for(var n = foundsLen; n >=0; n--){
                            if(this_Parent == prev_Parent){
                            if(n == foundsLen){
                            this_start = founds[n].index;
                            this_end = founds[n].insertionPoints[-1].index;
                            this_Parent = founds[n].parent;
                            }else{
                            this_start = prev_start;
                            this_end = prev_end;
                            this_Parent = prev_Parent;
                            }

                            if(n != 0){
                            prev_start = founds[n-1].index;
                            prev_end = founds[n-1].insertionPoints[-1].index;
                            prev_Parent = founds[n-1].parent;
                            }else{
                            prev_start = this_start;
                            prev_end = this_end;
                            prev_Parent = this_Parent;
                            }

                            if(prev_end >= this_start){continue;}

                            if(n == foundsLen){
                            endIndex = this_Parent.insertionPoints.length-1;
                            if(this_end != endIndex){
                            myText = this_Parent.insertionPoints.itemByRange(this_end, endIndex);
                            baselineText(myText);
                            }
                            }else if(n == 0){
                            if(this_start != 0){
                            myText = this_Parent.insertionPoints.itemByRange(0, this_start);
                            baselineText(myText);
                            }
                            }

                            myText = this_Parent.insertionPoints.itemByRange(prev_end, this_start);
                            baselineText(myText);
                            }else{
                            //alert(founds[n].texts[0].contents);
                            if(prev_start != 0){
                            myText = prev_Parent.insertionPoints.itemByRange(0, prev_start);
                            baselineText(myText);
                            }

                            this_start = founds[n].index;
                            this_end = founds[n].insertionPoints[-1].index;
                            this_Parent = founds[n].parent;

                            endIndex = this_Parent.insertionPoints.length-1;
                            if(this_end != endIndex){
                            myText = this_Parent.insertionPoints.itemByRange(this_end, endIndex);
                            baselineText(myText);
                            }

                            prev_start = this_start;
                            prev_end = this_end;
                            prev_Parent = this_Parent;;
                            }
                            }
                            }
                            }

                            if(isID5){
                            app.findTextPreferences = NothingEnum.nothing;
                            app.changeTextPreferences = NothingEnum.nothing;
                            }else{
                            app.findPreferences = NothingEnum.nothing;
                            app.changePreferences = NothingEnum.nothing;
                            }

                            if(isID5){if(!app.scriptPreferences.enableRedraw){app.scriptPreferences.enableRedraw = true;}}

                            param.t = param.t.elapsed();

                            alert([param.t/1000, param.doCount].join("\n"));

                            jxswm