summaryrefslogtreecommitdiffstats
path: root/tools/image_generator/ImageGenerator.java
diff options
context:
space:
mode:
Diffstat (limited to 'tools/image_generator/ImageGenerator.java')
-rw-r--r--tools/image_generator/ImageGenerator.java69
1 files changed, 48 insertions, 21 deletions
diff --git a/tools/image_generator/ImageGenerator.java b/tools/image_generator/ImageGenerator.java
index 19d187d24..fd8e54295 100644
--- a/tools/image_generator/ImageGenerator.java
+++ b/tools/image_generator/ImageGenerator.java
@@ -41,6 +41,7 @@ import java.io.IOException;
import java.text.AttributedString;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
@@ -92,7 +93,7 @@ public class ImageGenerator {
// Some localized font cannot draw the word "Android" and some PUNCTUATIONS; we need to fall
// back to use our default latin font instead.
- private static final char[] PUNCTUATIONS = {',', ';', '.', '!' };
+ private static final char[] PUNCTUATIONS = {',', ';', '.', '!', '?'};
private static final String ANDROID_STRING = "Android";
@@ -214,8 +215,9 @@ public class ImageGenerator {
index + ANDROID_STRING.length());
}
- // Adds the attribute to use default font to draw the PUNCTUATIONS ", . !"
+ // Adds the attribute to use default font to draw the PUNCTUATIONS ", . ; ! ?"
for (char punctuation : PUNCTUATIONS) {
+ // TODO (xunchang) handle the RTL language that has different directions for '?'
if (text.indexOf(punctuation) != -1 && !textFont.canDisplay(punctuation)) {
int index = 0;
while ((index = text.indexOf(punctuation, index)) != -1) {
@@ -229,6 +231,11 @@ public class ImageGenerator {
mWrappedLines.add(new LineInfo(attributedText, width));
}
+
+ /** Merges two WrappedTextInfo. */
+ public void addLines(WrappedTextInfo other) {
+ mWrappedLines.addAll(other.mWrappedLines);
+ }
}
/** Initailizes the fields of the image image. */
@@ -393,15 +400,7 @@ public class ImageGenerator {
"Can not find the font file " + fontName + " for language " + language);
}
- /**
- * Wraps the text with a maximum of mImageWidth pixels per line.
- *
- * @param text the string representation of text to wrap
- * @param metrics the metrics of the Font used to draw the text; it gives the width in pixels of
- * the text given its string representation
- * @return a WrappedTextInfo class with the width of each AttributedString smaller than
- * mImageWidth pixels
- */
+ /** Wraps the text with a maximum of mImageWidth pixels per line. */
private WrappedTextInfo wrapText(String text, FontMetrics metrics) {
WrappedTextInfo info = new WrappedTextInfo();
@@ -437,6 +436,29 @@ public class ImageGenerator {
}
/**
+ * Handles the special characters of the raw text embedded in the xml file; and wraps the text
+ * with a maximum of mImageWidth pixels per line.
+ *
+ * @param text the string representation of text to wrap
+ * @param metrics the metrics of the Font used to draw the text; it gives the width in pixels of
+ * the text given its string representation
+ * @return a WrappedTextInfo class with the width of each AttributedString smaller than
+ * mImageWidth pixels
+ */
+ private WrappedTextInfo processAndWrapText(String text, FontMetrics metrics) {
+ // Apostrophe is escaped in the xml file.
+ String processed = text.replace("\\'", "'");
+ // The separator "\n\n" indicates a new line in the text.
+ String[] lines = processed.split("\\\\n\\\\n");
+ WrappedTextInfo result = new WrappedTextInfo();
+ for (String line : lines) {
+ result.addLines(wrapText(line, metrics));
+ }
+
+ return result;
+ }
+
+ /**
* Encodes the information of the text image for |locale|. According to minui/resources.cpp, the
* width, height and locale of the image is decoded as: int w = (row[1] << 8) | row[0]; int h =
* (row[3] << 8) | row[2]; __unused int len = row[4]; char* loc =
@@ -477,7 +499,7 @@ public class ImageGenerator {
throws IOException, FontFormatException {
Graphics2D graphics = createGraphics(locale);
FontMetrics fontMetrics = graphics.getFontMetrics();
- WrappedTextInfo wrappedTextInfo = wrapText(text, fontMetrics);
+ WrappedTextInfo wrappedTextInfo = processAndWrapText(text, fontMetrics);
int textWidth = 0;
for (WrappedTextInfo.LineInfo lineInfo : wrappedTextInfo.mWrappedLines) {
@@ -512,7 +534,7 @@ public class ImageGenerator {
Graphics2D graphics = createGraphics(locale);
FontMetrics fontMetrics = graphics.getFontMetrics();
- WrappedTextInfo wrappedTextInfo = wrapText(text, fontMetrics);
+ WrappedTextInfo wrappedTextInfo = processAndWrapText(text, fontMetrics);
// Marks the start y offset for the text image of current locale; and reserves one line to
// encode the image metadata.
@@ -584,11 +606,15 @@ public class ImageGenerator {
mDefaultFont = defaultFontMetrics.getFont();
mAndroidStringWidth = defaultFontMetrics.stringWidth(ANDROID_STRING);
- Map<String, Integer> languageCount = new TreeMap<>();
+ // The last country variant should be the fallback locale for a given language.
+ Map<String, Locale> fallbackLocaleMap = new HashMap<>();
int textWidth = 0;
for (Locale locale : localizedTextMap.keySet()) {
- String language = locale.getLanguage();
- languageCount.put(language, languageCount.getOrDefault(language, 0) + 1);
+ // Updates the fallback locale if we have a new language variant. Don't do it for en-XC
+ // as it's a pseudo-locale.
+ if (!locale.toLanguageTag().equals("en-XC")) {
+ fallbackLocaleMap.put(locale.getLanguage(), locale);
+ }
textWidth = Math.max(textWidth, measureTextWidth(localizedTextMap.get(locale), locale));
}
@@ -596,15 +622,16 @@ public class ImageGenerator {
resize(textWidth, mImageHeight);
for (Locale locale : localizedTextMap.keySet()) {
- Integer count = languageCount.get(locale.getLanguage());
// Recovery expects en-US instead of en_US.
String languageTag = locale.toLanguageTag();
- if (count == 1) {
- // Make the last country variant for a given language be the catch-all for that
+ Locale fallbackLocale = fallbackLocaleMap.get(locale.getLanguage());
+ if (locale.equals(fallbackLocale)) {
+ // Makes the last country variant for a given language be the catch-all for that
// language.
languageTag = locale.getLanguage();
- } else {
- languageCount.put(locale.getLanguage(), count - 1);
+ } else if (localizedTextMap.get(locale).equals(localizedTextMap.get(fallbackLocale))) {
+ LOGGER.info("Skip parsing text for duplicate locale " + locale);
+ continue;
}
drawText(localizedTextMap.get(locale), locale, languageTag);