Add SkDrawPosTextHCommand JSON, fix skiaserve build.

Adds SkDrawPosTextHCommand ::fromJSON and ::toJSON.

Both SkDrawPosTextCommand and SkDrawPosTextHCommand's
::toJSON write the correct number of positions, preventing reading
uninitialized memory.

The microhttpd build is now done in the build tree as opposed to in a
temporary directory.

The microhttpd build script uses os.path.join so that absolute paths
do not confuse the build. This allows compatibility with the cmake
gyp generator as CMake likes to pass absolute paths.

The microhttpd gyp target is now marked as 'none' since it is not
a 'static_library' target (which directs gyp to compile sources into
a static library). The dependencies to the action are updated to the
minimum required for sane re-building.

The everything gyp target now depends on the skiaserve gyp target.
This means that when using skia_build_server=1, building 'most' will
build skiaserve, but when skia_build_server is not defined the
skiaserve target will still be available if specified manually.

The old json.gyp is removed as it currently does not build anything.
All of the files currently referenced by it as sources no longer exist.

Review URL: https://codereview.chromium.org/1775203002
diff --git a/gyp/everything.gyp b/gyp/everything.gyp
index 61909e4..47db978 100644
--- a/gyp/everything.gyp
+++ b/gyp/everything.gyp
@@ -18,6 +18,7 @@
       'type': 'none',
       'dependencies': [
         'most.gyp:most',
+        'skiaserve.gyp:skiaserve',
       ],
       'conditions': [
         ['skia_os in ("ios", "android", "chromeos")', {
diff --git a/gyp/json.gyp b/gyp/json.gyp
deleted file mode 100644
index 707e225..0000000
--- a/gyp/json.gyp
+++ /dev/null
@@ -1,29 +0,0 @@
-# Copyright 2015 Google Inc.
-#
-# Use of this source code is governed by a BSD-style license that can be
-# found in the LICENSE file.
-{
-  'targets': [
-    {
-      'target_name': 'json',
-      'product_name': 'skia_json',
-      'type': 'static_library',
-      'standalone_static_library': 1,
-      'dependencies': [
-        'core.gyp:core',
-        'jsoncpp.gyp:jsoncpp',
-      ],
-      'include_dirs': [
-        '../include/core',
-        '../include/effects',
-        '../include/private',
-        '../include/utils',
-        '../src/core',
-      ],
-      'sources': [
-        '../tools/json/SkJSONCanvas.cpp',
-        '../tools/json/SkJSONRenderer.cpp',
-      ],
-    },
-  ],
-}
diff --git a/gyp/microhttpd.gyp b/gyp/microhttpd.gyp
index 58ea0ba..5254c02 100644
--- a/gyp/microhttpd.gyp
+++ b/gyp/microhttpd.gyp
@@ -8,17 +8,17 @@
   'targets': [
     {
       'target_name': 'microhttpd',
-      'type': 'static_library',
+      'type': 'none',
       'variables': {
         'base_dir%': '../third_party/libmicrohttpd',
+        'out_dir%': '<(INTERMEDIATE_DIR)/build',
         'src_dir%': '../third_party/externals/microhttpd',
       },
       'direct_dependent_settings': {
         'include_dirs': [
           '<(src_dir)/src/include',
         ],
-        # for reasons I can't quite fathom, we need the below line to trigger
-        # a link
+        # Link the built library to dependents.
         'libraries': [
           'libmicrohttpd.a',
         ],
@@ -27,13 +27,15 @@
         {
           'action_name': 'configure_and_build',
           'inputs': [
-            '<(PRODUCT_DIR)/',
+            '<(base_dir)/build.py',
+            '<(src_dir)/.git/HEAD', # This does not support local changes, but does support DEPS.
           ],
           'outputs': [ '<(PRODUCT_DIR)/libmicrohttpd.a' ],
           'action': [
             'python',
             '<(base_dir)/build.py', 
             '--src', '<(src_dir)',
+            '--out', '<(out_dir)',
             '--dst', '<(PRODUCT_DIR)',
           ],
         },
diff --git a/gyp/most.gyp b/gyp/most.gyp
index 22be29b..dac0685 100644
--- a/gyp/most.gyp
+++ b/gyp/most.gyp
@@ -82,7 +82,6 @@
         [ 'skia_build_server', {
           'dependencies': [
             'skiaserve.gyp:skiaserve',
-            'json.gyp:json',
           ],
         }],
       ],
diff --git a/third_party/libmicrohttpd/build.py b/third_party/libmicrohttpd/build.py
index cafcbea..f09a360 100644
--- a/third_party/libmicrohttpd/build.py
+++ b/third_party/libmicrohttpd/build.py
@@ -13,13 +13,19 @@
 
 parser = argparse.ArgumentParser()
 parser.add_argument("--src", help="microhttpd src directory")
-parser.add_argument("--dst", help="output for build files")
+parser.add_argument("--out", help="build directory")
+parser.add_argument("--dst", help="output for final build products")
 args = parser.parse_args()
 
-temp_dir = tempfile.mkdtemp()
+out_dir = args.out
 cwd = os.getcwd()
-os.chdir(temp_dir)
-call([cwd + "/" + args.src + "/configure",
+try:
+  os.makedirs(out_dir)
+except OSError as e:
+  pass
+
+os.chdir(out_dir)
+call([os.path.join(cwd, args.src, "configure"),
       "--disable-doc",
       "--disable-examples",
       "--enable-https=no",
@@ -28,7 +34,5 @@
       "--enable-shared=no"])
 call(["make", "--silent"])
 call(["cp",
-      temp_dir + "/src/microhttpd/.libs/libmicrohttpd.a",
-      cwd + "/" + args.dst])
-shutil.rmtree(temp_dir)
-
+      "src/microhttpd/.libs/libmicrohttpd.a",
+      os.path.join(cwd, args.dst)])
diff --git a/tools/debugger/SkDrawCommand.cpp b/tools/debugger/SkDrawCommand.cpp
index df33824..e342c44 100644
--- a/tools/debugger/SkDrawCommand.cpp
+++ b/tools/debugger/SkDrawCommand.cpp
@@ -245,6 +245,7 @@
         INSTALL_FACTORY(DrawPoints);
         INSTALL_FACTORY(DrawText);
         INSTALL_FACTORY(DrawPosText);
+        INSTALL_FACTORY(DrawPosTextH);
         INSTALL_FACTORY(DrawTextOnPath);
         INSTALL_FACTORY(DrawTextBlob);
 
@@ -2371,7 +2372,8 @@
     result[SKDEBUGCANVAS_ATTRIBUTE_TEXT] = Json::Value((const char*) fText, 
                                                        ((const char*) fText) + fByteLength);
     Json::Value coords(Json::arrayValue);
-    for (size_t i = 0; i < fByteLength; i++) {
+    size_t numCoords = fPaint.textToGlyphs(fText, fByteLength, nullptr);
+    for (size_t i = 0; i < numCoords; i++) {
         coords.append(make_json_point(fPos[i]));
     }
     result[SKDEBUGCANVAS_ATTRIBUTE_COORDS] = coords;
@@ -2419,6 +2421,36 @@
     canvas->drawPosTextH(fText, fByteLength, fXpos, fConstY, fPaint);
 }
 
+Json::Value SkDrawPosTextHCommand::toJSON(UrlDataManager& urlDataManager) const {
+    Json::Value result = INHERITED::toJSON(urlDataManager);
+    result[SKDEBUGCANVAS_ATTRIBUTE_TEXT] = Json::Value((const char*) fText,
+                                                       ((const char*) fText) + fByteLength);
+    result[SKDEBUGCANVAS_ATTRIBUTE_Y] = Json::Value(fConstY);
+    Json::Value xpos(Json::arrayValue);
+    size_t numXpos = fPaint.textToGlyphs(fText, fByteLength, nullptr);
+    for (size_t i = 0; i < numXpos; i++) {
+        xpos.append(Json::Value(fXpos[i]));
+    }
+    result[SKDEBUGCANVAS_ATTRIBUTE_POSITIONS] = xpos;
+    result[SKDEBUGCANVAS_ATTRIBUTE_PAINT] = make_json_paint(fPaint, urlDataManager);
+    return result;
+}
+
+SkDrawPosTextHCommand* SkDrawPosTextHCommand::fromJSON(Json::Value& command,
+                                                       UrlDataManager& urlDataManager) {
+    const char* text = command[SKDEBUGCANVAS_ATTRIBUTE_TEXT].asCString();
+    SkPaint paint;
+    extract_json_paint(command[SKDEBUGCANVAS_ATTRIBUTE_PAINT], urlDataManager, &paint);
+    Json::Value jsonXpos = command[SKDEBUGCANVAS_ATTRIBUTE_POSITIONS];
+    int count = (int) jsonXpos.size();
+    SkScalar* xpos = (SkScalar*) sk_malloc_throw(count * sizeof(SkScalar));
+    for (int i = 0; i < count; i++) {
+        xpos[i] = jsonXpos[i].asFloat();
+    }
+    SkScalar y = command[SKDEBUGCANVAS_ATTRIBUTE_Y].asFloat();
+    return new SkDrawPosTextHCommand(text, strlen(text), xpos, y, paint);
+}
+
 static const char* gPositioningLabels[] = {
     "kDefault_Positioning",
     "kHorizontal_Positioning",
diff --git a/tools/debugger/SkDrawCommand.h b/tools/debugger/SkDrawCommand.h
index 44682b5..0a365c0 100644
--- a/tools/debugger/SkDrawCommand.h
+++ b/tools/debugger/SkDrawCommand.h
@@ -497,6 +497,8 @@
                           SkScalar constY, const SkPaint& paint);
     virtual ~SkDrawPosTextHCommand() { delete [] fXpos; delete [] fText; }
     void execute(SkCanvas* canvas) const override;
+    Json::Value toJSON(UrlDataManager& urlDataManager) const override;
+    static SkDrawPosTextHCommand* fromJSON(Json::Value& command, UrlDataManager& urlDataManager);
 
 private:
     SkScalar* fXpos;