0
4

1 回答 1

2

好吧,这是字符计数的蛮力策略。它遍历文档中的每个字符textStyleRange 并检查其应用的字体。编辑:更新为使用 textStyleRanges。比遍历每个角色要快得多。

var document = app.open(new File(Folder.desktop.fsName + "/test/test.indd"));
try {
    var fontMultiset = countCharsInFonts(document);

    // For each font, display its character count.
    var fonts = document.fonts.everyItem().getElements();
    for (var i = 0; i < fonts.length; i++) {
        var fontName = fonts[i].fullName;
        $.writeln(fontName + ": " + fontMultiset[fontName]);
    }
}
finally {
    document.close();
}

function countCharsInFonts(document) {
    // Create the font multiset.
    var fontMultiset = {
        add: function add(fontName, number) {
            if (this.hasOwnProperty(fontName)) {
                this[fontName] += number;
            }
            else {
                this[fontName] = number;
            }
        },
    };

    // For every textStyleRange in the document, add its applied font to the multiset.
    var stories = document.stories.everyItem().getElements();
    for (var i = 0; i < stories.length; i++) {
        var story = stories[i];
        var textStyleRanges = story.textStyleRanges.everyItem().getElements();
        for (var j = 0; j < textStyleRanges.length; j++) {
            fontMultiset.add(textStyleRanges[j].appliedFont.fullName, textStyleRanges[j].length);
        }
    }

    // For any fonts that aren't applied in the document, set the character count to 0.
    var fonts = document.fonts.everyItem().getElements();
    for (var i = 0; i < fonts.length; i++) {
        var fontName = fonts[i].fullName;
        if (!fontMultiset.hasOwnProperty(fontName)) {
            fontMultiset[fontName] = 0;
        }
    }

    return fontMultiset;
}
于 2013-05-21T17:07:23.017 回答