diff options
author | 2018-03-05 13:26:16 -0500 | |
---|---|---|
committer | 2018-03-05 18:48:15 +0000 | |
commit | 1a8d762a18d6f6494408a3a5e06a80097f8b85f7 (patch) | |
tree | d4ccb89175936ecfeceb205bc60a2af536d96de1 /tools/bookmaker/definition.cpp | |
parent | f6188425c7501ee0d7485d933f0c93b25972e58b (diff) |
work in imageinfo and phrase substitution
This adds the ability to define long phrases
in one place and refer to those phrases in
many places.
Bookmaker has new syntax to support phrase substitution.
When it encounters
#some_phrase_reference#
It substitutes the body of
#PhraseDef some_phrase_reference
text to substitute when encountering the phrase
##
The phrase label must start with a lowercase letter,
and be bracketed by single hash marks, without spaces
between the label and the hash marks.
Docs-Preview: https://skia.org/?cl=111224
TBR=caryclark@google.com
Bug: skia:6898
Change-Id: I12c57d916ccedbd86b421377d117399150ada72a
Reviewed-on: https://skia-review.googlesource.com/111224
Reviewed-by: Cary Clark <caryclark@skia.org>
Commit-Queue: Cary Clark <caryclark@skia.org>
Diffstat (limited to 'tools/bookmaker/definition.cpp')
-rw-r--r-- | tools/bookmaker/definition.cpp | 254 |
1 files changed, 0 insertions, 254 deletions
diff --git a/tools/bookmaker/definition.cpp b/tools/bookmaker/definition.cpp index 68e001aebb..14f6daab81 100644 --- a/tools/bookmaker/definition.cpp +++ b/tools/bookmaker/definition.cpp @@ -6,51 +6,6 @@ */ #include "bookmaker.h" -#include "SkOSPath.h" - -static size_t count_indent(const string& text, size_t test, size_t end) { - size_t result = test; - while (test < end) { - if (' ' != text[test]) { - break; - } - ++test; - } - return test - result; -} - -static void add_code(const string& text, int pos, int end, - size_t outIndent, size_t textIndent, string& example) { - do { - // fix this to move whole paragraph in, out, but preserve doc indent - int nextIndent = count_indent(text, pos, end); - size_t len = text.find('\n', pos); - if (string::npos == len) { - len = end; - } - if ((size_t) (pos + nextIndent) < len) { - size_t indent = outIndent + nextIndent; - SkASSERT(indent >= textIndent); - indent -= textIndent; - for (size_t index = 0; index < indent; ++index) { - example += ' '; - } - pos += nextIndent; - while ((size_t) pos < len) { - example += '"' == text[pos] ? "\\\"" : - '\\' == text[pos] ? "\\\\" : - text.substr(pos, 1); - ++pos; - } - example += "\\n"; - } else { - pos += nextIndent; - } - if ('\n' == text[pos]) { - ++pos; - } - } while (pos < end); -} #ifdef CONST #undef CONST @@ -487,215 +442,6 @@ void Definition::setCanonicalFiddle() { fFiddle = Definition::NormalizedName(result); } -void Definition::setWrapper() { - const char drawWrapper[] = "void draw(SkCanvas* canvas) {"; - const char drawNoCanvas[] = "void draw(SkCanvas* ) {"; - string text = this->extractText(Definition::TrimExtract::kNo); - size_t nonSpace = 0; - while (nonSpace < text.length() && ' ' >= text[nonSpace]) { - ++nonSpace; - } - bool hasFunc = !text.compare(nonSpace, sizeof(drawWrapper) - 1, drawWrapper); - bool noCanvas = !text.compare(nonSpace, sizeof(drawNoCanvas) - 1, drawNoCanvas); - bool hasCanvas = string::npos != text.find("SkCanvas canvas"); - SkASSERT(!hasFunc || !noCanvas); - bool preprocessor = text[0] == '#'; - bool wrapCode = !hasFunc && !noCanvas && !preprocessor; - if (wrapCode) { - fWrapper = hasCanvas ? string(drawNoCanvas) : string(drawWrapper); - } -} - -bool Definition::exampleToScript(string* result, ExampleOptions exampleOptions) const { - bool hasFiddle = true; - const Definition* platform = this->hasChild(MarkType::kPlatform); - if (platform) { - TextParser platParse(platform); - hasFiddle = !platParse.strnstr("!fiddle", platParse.fEnd); - } - if (!hasFiddle) { - *result = ""; - return true; - } - string text = this->extractText(Definition::TrimExtract::kNo); - bool textOut = string::npos != text.find("SkDebugf(") - || string::npos != text.find("dump(") - || string::npos != text.find("dumpHex("); - string heightStr = "256"; - string widthStr = "256"; - string normalizedName(fFiddle); - string code; - string imageStr = "0"; - string srgbStr = "false"; - string durationStr = "0"; - for (auto const& iter : fChildren) { - switch (iter->fMarkType) { - case MarkType::kDuration: - durationStr = string(iter->fContentStart, iter->fContentEnd - iter->fContentStart); - break; - case MarkType::kHeight: - heightStr = string(iter->fContentStart, iter->fContentEnd - iter->fContentStart); - break; - case MarkType::kWidth: - widthStr = string(iter->fContentStart, iter->fContentEnd - iter->fContentStart); - break; - case MarkType::kDescription: - // ignore for now - break; - case MarkType::kFunction: { - // emit this, but don't wrap this in draw() - string funcText(iter->fContentStart, iter->fContentEnd - iter->fContentStart); - size_t pos = 0; - while (pos < funcText.length() && ' ' > funcText[pos]) { - ++pos; - } - size_t indent = count_indent(funcText, pos, funcText.length()); - add_code(funcText, pos, funcText.length(), 0, indent, code); - code += "\\n"; - } break; - case MarkType::kComment: - break; - case MarkType::kImage: - imageStr = string(iter->fContentStart, iter->fContentEnd - iter->fContentStart); - break; - case MarkType::kToDo: - break; - case MarkType::kBug: - case MarkType::kMarkChar: - case MarkType::kPlatform: - // ignore for now - break; - case MarkType::kSet: - if ("sRGB" == string(iter->fContentStart, - iter->fContentEnd - iter->fContentStart)) { - srgbStr = "true"; - } else { - SkASSERT(0); // more work to do - return false; - } - break; - case MarkType::kStdOut: - textOut = true; - break; - default: - SkASSERT(0); // more coding to do - } - } - string animatedStr = "0" != durationStr ? "true" : "false"; - string textOutStr = textOut ? "true" : "false"; - size_t pos = 0; - while (pos < text.length() && ' ' > text[pos]) { - ++pos; - } - size_t end = text.length(); - size_t outIndent = 0; - size_t textIndent = count_indent(text, pos, end); - if (fWrapper.length() > 0) { - code += fWrapper; - code += "\\n"; - outIndent = 4; - } - add_code(text, pos, end, outIndent, textIndent, code); - if (fWrapper.length() > 0) { - code += "}"; - } - string example = "\"" + normalizedName + "\": {\n"; - size_t nameStart = fFileName.find(SkOSPath::SEPARATOR, 0); - SkASSERT(string::npos != nameStart); - string baseFile = fFileName.substr(nameStart + 1, fFileName.length() - nameStart - 5); - if (ExampleOptions::kText == exampleOptions) { - example += " \"code\": \"" + code + "\",\n"; - example += " \"hash\": \"" + fHash + "\",\n"; - example += " \"file\": \"" + baseFile + "\",\n"; - example += " \"name\": \"" + fName + "\","; - } else { - example += " \"code\": \"" + code + "\",\n"; - if (ExampleOptions::kPng == exampleOptions) { - example += " \"width\": " + widthStr + ",\n"; - example += " \"height\": " + heightStr + ",\n"; - example += " \"hash\": \"" + fHash + "\",\n"; - example += " \"file\": \"" + baseFile + "\",\n"; - example += " \"name\": \"" + fName + "\"\n"; - example += "}"; - } else { - example += " \"options\": {\n"; - example += " \"width\": " + widthStr + ",\n"; - example += " \"height\": " + heightStr + ",\n"; - example += " \"source\": " + imageStr + ",\n"; - example += " \"srgb\": " + srgbStr + ",\n"; - example += " \"f16\": false,\n"; - example += " \"textOnly\": " + textOutStr + ",\n"; - example += " \"animated\": " + animatedStr + ",\n"; - example += " \"duration\": " + durationStr + "\n"; - example += " },\n"; - example += " \"fast\": true"; - } - } - *result = example; - return true; -} - -string Definition::extractText(TrimExtract trimExtract) const { - string result; - TextParser parser(fFileName, fContentStart, fContentEnd, fLineCount); - int childIndex = 0; - char mc = '#'; - while (parser.fChar < parser.fEnd) { - if (TrimExtract::kYes == trimExtract && !parser.skipWhiteSpace()) { - break; - } - if (parser.next() == mc) { - if (parser.next() == mc) { - if (parser.next() == mc) { - mc = parser.next(); - } - } else { - // fixme : more work to do if # style comment is in text - // if in method definition, could be alternate method name - --parser.fChar; - if (' ' < parser.fChar[0]) { - if (islower(parser.fChar[0])) { - result += '\n'; - parser.skipLine(); - } else { - SkASSERT(isupper(parser.fChar[0])); - parser.skipTo(fChildren[childIndex]->fTerminator); - if (mc == parser.fChar[0] && mc == parser.fChar[1]) { - parser.next(); - parser.next(); - } - childIndex++; - } - } else { - parser.skipLine(); - } - continue; - } - } else { - --parser.fChar; - } - const char* end = parser.fEnd; - const char* mark = parser.strnchr(mc, end); - if (mark) { - end = mark; - } - string fragment(parser.fChar, end - parser.fChar); - trim_end(fragment); - if (TrimExtract::kYes == trimExtract) { - trim_start(fragment); - if (result.length()) { - result += '\n'; - result += '\n'; - } - } - if (TrimExtract::kYes == trimExtract || has_nonwhitespace(fragment)) { - result += fragment; - } - parser.skipTo(end); - } - return result; -} - static void space_pad(string* str) { size_t len = str->length(); if (len == 0) { |