sort the glyphID arrays

add head/tail code

git-svn-id: http://skia.googlecode.com/svn/trunk@9376 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/tools/lua/glyph-usage.lua b/tools/lua/glyph-usage.lua
index 2b0c0e1..8715f72 100644
--- a/tools/lua/glyph-usage.lua
+++ b/tools/lua/glyph-usage.lua
@@ -52,6 +52,15 @@
     return math.floor(x * mul + 0.5) / mul
 end
 
+dump_glyph_array_p = false
+
+function dump_array_as_C(array)
+    for k, v in next, array do
+        io.write(tostring(v), ", ");
+    end
+    io.write("-1,\n")
+end
+
 local strikes = {}  -- [fontID_pointsize] = [] unique glyphs
 
 function make_strike_key(paint)
@@ -66,6 +75,16 @@
     end
 end
 
+-- take a table of bools, indexed by values, and return a sorted table of values
+function bools_to_values(t)
+    local array = {}
+    for k, v in next, t do
+        array[#array + 1] = k
+    end
+    table.sort(array)
+    return array
+end
+
 function array_count(array)
     local n = 0
     for k in next, array do
@@ -81,6 +100,10 @@
             local key = make_strike_key(t.paint)
             strikes[key] = strikes[key] or {}
             array_union(strikes[key], t.glyphs)
+            
+            if dump_glyph_array_p then
+                dump_array_as_C(t.glyphs)
+            end
         end
     end
 end
@@ -94,20 +117,41 @@
     local strikeCount = 0
     local min, max = 0, 0
 
+    local histogram = {}
+
     for k, v in next, strikes do
         local fontID = round(k / 1000)
         local size = k - fontID * 1000
         local count = array_count(v)
 
-        io.write("fontID = ", fontID, ", size = ", size, ", entries = ", count, "\n");
+--        io.write("fontID,", fontID, ", size,", size, ", entries,", count, "\n");
         
         min = math.min(min, count)
         max = math.max(max, count)
         totalCount = totalCount + count
         strikeCount = strikeCount + 1
+        
+        histogram[count] = (histogram[count] or 0) + 1
     end
     local ave = round(totalCount / strikeCount)
 
     io.write("\n", "unique glyphs: min = ", min, ", max = ", max, ", ave = ", ave, "\n");
+    
+    for k, v in next, histogram do
+        io.write("glyph_count,", k, ",frequency,", v, "\n")
+    end
+end
+
+function test_summary()
+    io.write("just testing test_summary\n")
+end
+
+function summarize_unique_glyphIDs()
+    io.write("/* runs of unique glyph IDs, with a -1 sentinel between different runs */\n")
+    io.write("static const int gUniqueGlyphIDs[] = {\n");
+    for k, v in next, strikes do
+        dump_array_as_C(bools_to_values(v))
+    end
+    io.write("-1 };\n")
 end
 
diff --git a/tools/lua/lua_pictures.cpp b/tools/lua/lua_pictures.cpp
index ae4e1ca..ca28f24 100644
--- a/tools/lua/lua_pictures.cpp
+++ b/tools/lua/lua_pictures.cpp
@@ -30,10 +30,10 @@
 // PictureRenderingFlags.cpp
 extern bool lazy_decode_bitmap(const void* buffer, size_t size, SkBitmap*);
 
-
-// Flags used by this file, alphabetically:
 DEFINE_string2(skpPath, r, "", "Read .skp files from this dir");
 DEFINE_string2(luaFile, l, "", "File containing lua script to run");
+DEFINE_string2(headCode, s, "", "Optional lua code to call at beginning");
+DEFINE_string2(tailFunc, s, "", "Optional lua function to call at end");
 
 static SkPicture* load_picture(const char path[]) {
     SkAutoTUnref<SkStream> stream(SkStream::NewFromFile(path));
@@ -91,8 +91,13 @@
         exit(-1);
     }
 
+    const char* summary = gSummarizeFunc;
+    if (!FLAGS_tailFunc.isEmpty()) {
+        summary = FLAGS_tailFunc[0];
+    }
+
     SkAutoGraphics ag;
-    SkLua L(gSummarizeFunc);
+    SkLua L(summary);
 
     for (int i = 0; i < FLAGS_luaFile.count(); ++i) {
         SkAutoDataUnref data(read_into_data(FLAGS_luaFile[i]));
@@ -103,6 +108,10 @@
         }
     }
 
+    if (!FLAGS_headCode.isEmpty()) {
+        L.runCode(FLAGS_headCode[0]);
+    }
+    
     for (int i = 0; i < FLAGS_skpPath.count(); i ++) {
         SkOSFile::Iter iter(FLAGS_skpPath[i], "skp");
         SkString inputFilename;