Trim whitespace from parsed filename in Android v21.
authorbungeman <bungeman@google.com>
Fri, 8 May 2015 15:31:54 +0000 (08:31 -0700)
committerCommit bot <commit-bot@chromium.org>
Fri, 8 May 2015 15:31:54 +0000 (08:31 -0700)
The entire text content of the 'font' element is currently used as the
file name. This wasn't an issue in earlier versions, as the 'file'
element was dedicated to only containing the file name. The new 'font'
element also contains a number of attributes and potentially other tags.
This means that a 'font' element can become quite long, making it
desireable to be able to split it across multiple lines.

However, splitting the 'font' element across multiple lines is currently
difficult and awkward as any whitespace outside of tags will be
considered part of the file name. This change means that any leading or
trailing whitespace will not be considered part of the file name.

This only applies to v21 and later files, so while this restricts font
file names from beginning and ending with whitespace, it is unlikely to
break any users in practice. It is probably also undesireable to have
font files with names that begin or end with whitespace in any event.

Review URL: https://codereview.chromium.org/1125413003

resources/android_fonts/v22/fonts.xml
src/ports/SkFontConfigParser_android.cpp

index 180d5f7..b9ea87f 100644 (file)
@@ -2,7 +2,9 @@
 <familyset version="21">
     <!-- first font is default -->
     <family name="sans-serif">
-        <font weight="100" style="normal">Roboto-Thin.ttf</font>
+        <font weight="100" style="normal">
+            Roboto-Thin.ttf
+        </font>
         <font weight="100" style="italic">Roboto-ThinItalic.ttf</font>
         <font weight="300" style="normal">Roboto-Light.ttf</font>
         <font weight="300" style="italic">Roboto-LightItalic.ttf</font>
index e60f4e4..3f0ebbf 100644 (file)
@@ -263,6 +263,25 @@ static void XMLCALL start_element_handler(void* data, const char* tag, const cha
     }
 }
 
+static bool is_whitespace(char c) {
+    return c == ' ' || c == '\n'|| c == '\r' || c == '\t';
+}
+
+static void trim_string(SkString* s) {
+    char* str = s->writable_str();
+    const char* start = str;  // start is inclusive
+    const char* end = start + s->size();  // end is exclusive
+    while (is_whitespace(*start)) { ++start; }
+    if (start != end) {
+        --end;  // make end inclusive
+        while (is_whitespace(*end)) { --end; }
+        ++end;  // make end exclusive
+    }
+    size_t len = end - start;
+    memmove(str, start, len);
+    s->resize(len);
+}
+
 static void XMLCALL end_element_handler(void* data, const char* tag) {
     FamilyData* self = static_cast<FamilyData*>(data);
     size_t len = strlen(tag);
@@ -270,6 +289,7 @@ static void XMLCALL end_element_handler(void* data, const char* tag) {
         *self->fFamilies.append() = self->fCurrentFamily.detach();
     } else if (MEMEQ("font", tag, len)) {
         XML_SetCharacterDataHandler(self->fParser, NULL);
+        trim_string(&self->fCurrentFontInfo->fFileName);
     }
 }