diff options
-rw-r--r-- | README.md | 19 | ||||
-rw-r--r-- | minui/graphics_adf.h | 5 | ||||
-rw-r--r-- | minui/graphics_drm.h | 4 | ||||
-rw-r--r-- | minui/graphics_fbdev.cpp | 6 | ||||
-rw-r--r-- | minui/graphics_fbdev.h | 5 | ||||
-rw-r--r-- | minui/include/minui/minui.h | 25 | ||||
-rw-r--r-- | minui/resources.cpp | 22 | ||||
-rw-r--r-- | screen_ui.cpp | 4 | ||||
-rw-r--r-- | tests/testdata/battery_scale.png | bin | 0 -> 463 bytes | |||
-rw-r--r-- | tests/unit/minui_test.cpp | 32 | ||||
-rw-r--r-- | tests/unit/resources_test.cpp | 37 | ||||
-rw-r--r-- | tests/unit/screen_ui_test.cpp | 6 | ||||
-rw-r--r-- | tools/image_generator/ImageGenerator.java | 204 |
13 files changed, 285 insertions, 84 deletions
@@ -41,13 +41,6 @@ Running the manual tests contents of pmsg buffer into /data/misc/recovery/inject.txt. Test will pass if this file has expected contents. -`ResourceTest` validates whether the png files are qualified as background text -image under recovery. - - 1. `adb sync data` to make sure the test-dir has the images to test. - 2. The test will automatically pickup and verify all `_text.png` files in - the test dir. - Using `adb` under recovery -------------------------- @@ -60,10 +53,10 @@ allows `adb` communication. A device should be listed under `adb devices`, eithe List of devices attached 1234567890abcdef recovery -Although `/sbin/adbd` shares the same binary between normal boot and recovery images, only a subset -of `adb` commands are meaningful under recovery, such as `adb root`, `adb shell`, `adb push`, `adb -pull` etc. `adb shell` works only after manually mounting `/system` from recovery menu (assuming a -valid system image on device). +Although `/system/bin/adbd` is built from the same code base as the one in the normal boot, only a +subset of `adb` commands are meaningful under recovery, such as `adb root`, `adb shell`, `adb push`, +`adb pull` etc. Since Android Q, `adb shell` no longer requires manually mounting `/system` from +recovery menu. ## Troubleshooting @@ -74,8 +67,8 @@ valid system image on device). * Ensure `adbd` is built and running. -By default, `adbd` is always included into recovery image, as `/sbin/adbd`. `init` starts `adbd` -service automatically only in debuggable builds. This behavior is controlled by the recovery +By default, `adbd` is always included into recovery image, as `/system/bin/adbd`. `init` starts +`adbd` service automatically only in debuggable builds. This behavior is controlled by the recovery specific `/init.rc`, whose source code is at `bootable/recovery/etc/init.rc`. The best way to confirm a running `adbd` is by checking the serial output, which shows a service diff --git a/minui/graphics_adf.h b/minui/graphics_adf.h index bf9842878..79d8d2acb 100644 --- a/minui/graphics_adf.h +++ b/minui/graphics_adf.h @@ -16,6 +16,7 @@ #pragma once +#include <stddef.h> #include <stdint.h> #include <sys/types.h> @@ -40,8 +41,8 @@ class GRSurfaceAdf : public GRSurface { private: friend class MinuiBackendAdf; - GRSurfaceAdf(int width, int height, int row_bytes, int pixel_bytes, __u32 offset, __u32 pitch, - int fd) + GRSurfaceAdf(size_t width, size_t height, size_t row_bytes, size_t pixel_bytes, __u32 offset, + __u32 pitch, int fd) : GRSurface(width, height, row_bytes, pixel_bytes), offset(offset), pitch(pitch), fd(fd) {} const __u32 offset; diff --git a/minui/graphics_drm.h b/minui/graphics_drm.h index 6ba46e60b..57ba39b83 100644 --- a/minui/graphics_drm.h +++ b/minui/graphics_drm.h @@ -16,6 +16,7 @@ #pragma once +#include <stddef.h> #include <stdint.h> #include <memory> @@ -39,7 +40,8 @@ class GRSurfaceDrm : public GRSurface { private: friend class MinuiBackendDrm; - GRSurfaceDrm(int width, int height, int row_bytes, int pixel_bytes, int drm_fd, uint32_t handle) + GRSurfaceDrm(size_t width, size_t height, size_t row_bytes, size_t pixel_bytes, int drm_fd, + uint32_t handle) : GRSurface(width, height, row_bytes, pixel_bytes), drm_fd_(drm_fd), handle(handle) {} const int drm_fd_; diff --git a/minui/graphics_fbdev.cpp b/minui/graphics_fbdev.cpp index 93e4420d3..8d9c9741d 100644 --- a/minui/graphics_fbdev.cpp +++ b/minui/graphics_fbdev.cpp @@ -32,8 +32,8 @@ #include "minui/minui.h" -std::unique_ptr<GRSurfaceFbdev> GRSurfaceFbdev::Create(int width, int height, int row_bytes, - int pixel_bytes) { +std::unique_ptr<GRSurfaceFbdev> GRSurfaceFbdev::Create(size_t width, size_t height, + size_t row_bytes, size_t pixel_bytes) { // Cannot use std::make_unique to access non-public ctor. return std::unique_ptr<GRSurfaceFbdev>(new GRSurfaceFbdev(width, height, row_bytes, pixel_bytes)); } @@ -130,7 +130,7 @@ GRSurface* MinuiBackendFbdev::Init() { fb_fd = std::move(fd); SetDisplayedFramebuffer(0); - printf("framebuffer: %d (%d x %d)\n", fb_fd.get(), gr_draw->width, gr_draw->height); + printf("framebuffer: %d (%zu x %zu)\n", fb_fd.get(), gr_draw->width, gr_draw->height); Blank(true); Blank(false); diff --git a/minui/graphics_fbdev.h b/minui/graphics_fbdev.h index 016ab88bc..596ba74ea 100644 --- a/minui/graphics_fbdev.h +++ b/minui/graphics_fbdev.h @@ -17,6 +17,7 @@ #pragma once #include <linux/fb.h> +#include <stddef.h> #include <stdint.h> #include <memory> @@ -30,8 +31,8 @@ class GRSurfaceFbdev : public GRSurface { public: // Creates and returns a GRSurfaceFbdev instance, or nullptr on error. - static std::unique_ptr<GRSurfaceFbdev> Create(int width, int height, int row_bytes, - int pixel_bytes); + static std::unique_ptr<GRSurfaceFbdev> Create(size_t width, size_t height, size_t row_bytes, + size_t pixel_bytes); uint8_t* data() override { return buffer_; diff --git a/minui/include/minui/minui.h b/minui/include/minui/minui.h index 3231248a0..e49c6ac97 100644 --- a/minui/include/minui/minui.h +++ b/minui/include/minui/minui.h @@ -33,13 +33,16 @@ class GRSurface { public: + static constexpr size_t kSurfaceDataAlignment = 8; + virtual ~GRSurface() = default; // Creates and returns a GRSurface instance that's sufficient for storing an image of the given - // size. The starting address of the surface data is aligned to SURFACE_DATA_ALIGNMENT. Returns - // the created GRSurface instance (in std::unique_ptr), or nullptr on error. - static std::unique_ptr<GRSurface> Create(int width, int height, int row_bytes, int pixel_bytes, - size_t data_size); + // size (i.e. row_bytes * height). The starting address of the surface data is aligned to + // kSurfaceDataAlignment. Returns the created GRSurface instance (in std::unique_ptr), or nullptr + // on error. + static std::unique_ptr<GRSurface> Create(size_t width, size_t height, size_t row_bytes, + size_t pixel_bytes); // Clones the current GRSurface instance (i.e. an image). std::unique_ptr<GRSurface> Clone() const; @@ -52,13 +55,17 @@ class GRSurface { return const_cast<const uint8_t*>(const_cast<GRSurface*>(this)->data()); } - int width; - int height; - int row_bytes; - int pixel_bytes; + size_t data_size() const { + return data_size_; + } + + size_t width; + size_t height; + size_t row_bytes; + size_t pixel_bytes; protected: - GRSurface(int width, int height, int row_bytes, int pixel_bytes) + GRSurface(size_t width, size_t height, size_t row_bytes, size_t pixel_bytes) : width(width), height(height), row_bytes(row_bytes), pixel_bytes(pixel_bytes) {} private: diff --git a/minui/resources.cpp b/minui/resources.cpp index c7af1904d..069a49529 100644 --- a/minui/resources.cpp +++ b/minui/resources.cpp @@ -27,6 +27,7 @@ #include <sys/types.h> #include <unistd.h> +#include <limits> #include <memory> #include <regex> #include <string> @@ -39,11 +40,14 @@ static std::string g_resource_dir{ "/res/images" }; -std::unique_ptr<GRSurface> GRSurface::Create(int width, int height, int row_bytes, int pixel_bytes, - size_t data_size) { - static constexpr size_t kSurfaceDataAlignment = 8; +std::unique_ptr<GRSurface> GRSurface::Create(size_t width, size_t height, size_t row_bytes, + size_t pixel_bytes) { + if (width == 0 || row_bytes == 0 || height == 0 || pixel_bytes == 0) return nullptr; + if (std::numeric_limits<size_t>::max() / row_bytes < height) return nullptr; + // Cannot use std::make_unique to access non-public ctor. auto result = std::unique_ptr<GRSurface>(new GRSurface(width, height, row_bytes, pixel_bytes)); + size_t data_size = row_bytes * height; result->data_size_ = (data_size + kSurfaceDataAlignment - 1) / kSurfaceDataAlignment * kSurfaceDataAlignment; result->data_.reset( @@ -53,7 +57,7 @@ std::unique_ptr<GRSurface> GRSurface::Create(int width, int height, int row_byte } std::unique_ptr<GRSurface> GRSurface::Clone() const { - auto result = GRSurface::Create(width, height, row_bytes, pixel_bytes, data_size_); + auto result = GRSurface::Create(width, height, row_bytes, pixel_bytes); if (!result) return nullptr; memcpy(result->data(), data(), data_size_); return result; @@ -189,7 +193,7 @@ int res_create_display_surface(const char* name, GRSurface** pSurface) { png_uint_32 width = png_handler.width(); png_uint_32 height = png_handler.height(); - auto surface = GRSurface::Create(width, height, width * 4, 4, width * height * 4); + auto surface = GRSurface::Create(width, height, width * 4, 4); if (!surface) { return -8; } @@ -259,9 +263,7 @@ int res_create_multi_display_surface(const char* name, int* frames, int* fps, goto exit; } for (int i = 0; i < *frames; ++i) { - auto height_per_frame = height / *frames; - auto created_surface = - GRSurface::Create(width, height_per_frame, width * 4, 4, width * height_per_frame); + auto created_surface = GRSurface::Create(width, height / *frames, width * 4, 4); if (!created_surface) { result = -8; goto exit; @@ -309,7 +311,7 @@ int res_create_alpha_surface(const char* name, GRSurface** pSurface) { png_uint_32 width = png_handler.width(); png_uint_32 height = png_handler.height(); - auto surface = GRSurface::Create(width, height, width, 1, width * height); + auto surface = GRSurface::Create(width, height, width, 1); if (!surface) { return -8; } @@ -415,7 +417,7 @@ int res_create_localized_alpha_surface(const char* name, if (y + 1 + h >= height || matches_locale(loc, locale)) { printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y); - auto surface = GRSurface::Create(w, h, w, 1, w * h); + auto surface = GRSurface::Create(w, h, w, 1); if (!surface) { return -8; } diff --git a/screen_ui.cpp b/screen_ui.cpp index ed71888d1..765d2fe60 100644 --- a/screen_ui.cpp +++ b/screen_ui.cpp @@ -282,14 +282,14 @@ bool GraphicMenu::ValidateGraphicSurface(size_t max_width, size_t max_height, in } if (surface->pixel_bytes != 1 || surface->width != surface->row_bytes) { - fprintf(stderr, "Invalid graphic surface, pixel bytes: %d, width: %d row_bytes: %d", + fprintf(stderr, "Invalid graphic surface, pixel bytes: %zu, width: %zu row_bytes: %zu", surface->pixel_bytes, surface->width, surface->row_bytes); return false; } if (surface->width > max_width || surface->height > max_height - y) { fprintf(stderr, - "Graphic surface doesn't fit into the screen. width: %d, height: %d, max_width: %zu," + "Graphic surface doesn't fit into the screen. width: %zu, height: %zu, max_width: %zu," " max_height: %zu, vertical offset: %d\n", surface->width, surface->height, max_width, max_height, y); return false; diff --git a/tests/testdata/battery_scale.png b/tests/testdata/battery_scale.png Binary files differnew file mode 100644 index 000000000..2ae8f0fd7 --- /dev/null +++ b/tests/testdata/battery_scale.png diff --git a/tests/unit/minui_test.cpp b/tests/unit/minui_test.cpp index d68e5e3a1..c7d7f7eef 100644 --- a/tests/unit/minui_test.cpp +++ b/tests/unit/minui_test.cpp @@ -17,6 +17,7 @@ #include <stdint.h> #include <stdlib.h> +#include <limits> #include <vector> #include <gtest/gtest.h> @@ -24,21 +25,30 @@ #include "minui/minui.h" TEST(GRSurfaceTest, Create_aligned) { - static constexpr size_t kSurfaceDataAlignment = 8; - for (size_t data_size = 100; data_size < 128; data_size++) { - auto surface = GRSurface::Create(10, 1, 10, 1, data_size); - ASSERT_TRUE(surface); - ASSERT_EQ(0, reinterpret_cast<uintptr_t>(surface->data()) % kSurfaceDataAlignment); - } + auto surface = GRSurface::Create(9, 11, 9, 1); + ASSERT_TRUE(surface); + ASSERT_EQ(0, reinterpret_cast<uintptr_t>(surface->data()) % GRSurface::kSurfaceDataAlignment); + // data_size will be rounded up to the next multiple of GRSurface::kSurfaceDataAlignment. + ASSERT_EQ(0, surface->data_size() % GRSurface::kSurfaceDataAlignment); + ASSERT_GE(surface->data_size(), 11 * 9); +} + +TEST(GRSurfaceTest, Create_invalid_inputs) { + ASSERT_FALSE(GRSurface::Create(9, 11, 0, 1)); + ASSERT_FALSE(GRSurface::Create(9, 0, 9, 1)); + ASSERT_FALSE(GRSurface::Create(0, 11, 9, 1)); + ASSERT_FALSE(GRSurface::Create(9, 11, 9, 0)); + ASSERT_FALSE(GRSurface::Create(9, 101, std::numeric_limits<size_t>::max() / 100, 1)); } TEST(GRSurfaceTest, Clone) { - static constexpr size_t kImageSize = 10 * 50; - auto image = GRSurface::Create(50, 10, 50, 1, kImageSize); - for (auto i = 0; i < kImageSize; i++) { + auto image = GRSurface::Create(50, 10, 50, 1); + ASSERT_GE(image->data_size(), 10 * 50); + for (auto i = 0; i < image->data_size(); i++) { image->data()[i] = rand() % 128; } auto image_copy = image->Clone(); - ASSERT_EQ(std::vector(image->data(), image->data() + kImageSize), - std::vector(image_copy->data(), image_copy->data() + kImageSize)); + ASSERT_EQ(image->data_size(), image_copy->data_size()); + ASSERT_EQ(std::vector(image->data(), image->data() + image->data_size()), + std::vector(image_copy->data(), image_copy->data() + image->data_size())); } diff --git a/tests/unit/resources_test.cpp b/tests/unit/resources_test.cpp new file mode 100644 index 000000000..c3f72718f --- /dev/null +++ b/tests/unit/resources_test.cpp @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <string> + +#include <gtest/gtest.h> + +#include "common/test_constants.h" +#include "minui/minui.h" + +TEST(ResourcesTest, res_create_multi_display_surface) { + GRSurface** frames; + int frame_count; + int fps; + ASSERT_EQ(0, res_create_multi_display_surface(from_testdata_base("battery_scale.png").c_str(), + &frame_count, &fps, &frames)); + ASSERT_EQ(6, frame_count); + ASSERT_EQ(20, fps); + + for (auto i = 0; i < frame_count; i++) { + free(frames[i]); + } + free(frames); +} diff --git a/tests/unit/screen_ui_test.cpp b/tests/unit/screen_ui_test.cpp index 09c49977f..61a092551 100644 --- a/tests/unit/screen_ui_test.cpp +++ b/tests/unit/screen_ui_test.cpp @@ -231,7 +231,7 @@ TEST_F(ScreenUITest, WearMenuSelectItemsOverflow) { } TEST_F(ScreenUITest, GraphicMenuSelection) { - auto image = GRSurface::Create(50, 50, 50, 1, 50 * 50); + auto image = GRSurface::Create(50, 50, 50, 1); auto header = image->Clone(); std::vector<const GRSurface*> items = { image.get(), @@ -258,7 +258,7 @@ TEST_F(ScreenUITest, GraphicMenuSelection) { } TEST_F(ScreenUITest, GraphicMenuValidate) { - auto image = GRSurface::Create(50, 50, 50, 1, 50 * 50); + auto image = GRSurface::Create(50, 50, 50, 1); auto header = image->Clone(); std::vector<const GRSurface*> items = { image.get(), @@ -269,7 +269,7 @@ TEST_F(ScreenUITest, GraphicMenuValidate) { ASSERT_TRUE(GraphicMenu::Validate(200, 200, header.get(), items)); // Menu exceeds the horizontal boundary. - auto wide_surface = GRSurface::Create(300, 50, 300, 1, 300 * 50); + auto wide_surface = GRSurface::Create(300, 50, 300, 1); ASSERT_FALSE(GraphicMenu::Validate(299, 200, wide_surface.get(), items)); // Menu exceeds the vertical boundary. diff --git a/tools/image_generator/ImageGenerator.java b/tools/image_generator/ImageGenerator.java index 8730945b5..9d882678a 100644 --- a/tools/image_generator/ImageGenerator.java +++ b/tools/image_generator/ImageGenerator.java @@ -32,9 +32,11 @@ import java.awt.FontFormatException; import java.awt.FontMetrics; import java.awt.Graphics2D; import java.awt.RenderingHints; +import java.awt.font.TextAttribute; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; +import java.text.AttributedString; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; @@ -44,6 +46,8 @@ import java.util.Map; import java.util.Set; import java.util.StringTokenizer; import java.util.TreeMap; +import java.util.logging.Level; +import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.xml.parsers.DocumentBuilder; @@ -57,6 +61,8 @@ public class ImageGenerator { private static final float DEFAULT_FONT_SIZE = 40; + private static final Logger LOGGER = Logger.getLogger(ImageGenerator.class.getName()); + // This is the canvas we used to draw texts. private BufferedImage mBufferedImage; @@ -83,6 +89,20 @@ public class ImageGenerator { // Align the text in the center of the image. private final boolean mCenterAlignment; + // 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 String ANDROID_STRING = "Android"; + + // The width of the word "Android" when drawing with the default font. + private int mAndroidStringWidth; + + // The default Font to draw latin characters. It's loaded from DEFAULT_FONT_NAME. + private Font mDefaultFont; + // Cache of the loaded fonts for all languages. + private Map<String, Font> mLoadedFontMap; + // An explicit map from language to the font name to use. // The map is extracted from frameworks/base/data/fonts/fonts.xml. // And the language-subtag-registry is found in: @@ -160,6 +180,72 @@ public class ImageGenerator { } } + /** + * This class maintains the content of wrapped text, the attributes to draw these text, and + * the width of each wrapped lines. + */ + private class WrappedTextInfo { + /** LineInfo holds the AttributedString and width of each wrapped line. */ + private class LineInfo { + public AttributedString mLineContent; + public int mLineWidth; + + LineInfo(AttributedString text, int width) { + mLineContent = text; + mLineWidth = width; + } + } + + // Maintains the content of each line, as well as the width needed to draw these lines for + // a given language. + public List<LineInfo> mWrappedLines; + + WrappedTextInfo() { + mWrappedLines = new ArrayList<>(); + } + + /** + * Checks if the given text has words "Android" and some PUNCTUATIONS. If it does, and its + * associated textFont cannot display them correctly (e.g. for persian and hebrew); sets the + * attributes of these substrings to use our default font instead. + * + * @param text the input string to perform the check on + * @param width the pre-calculated width for the given text + * @param textFont the localized font to draw the input string + * @param fallbackFont our default font to draw latin characters + */ + public void addLine(String text, int width, Font textFont, Font fallbackFont) { + AttributedString attributedText = new AttributedString(text); + attributedText.addAttribute(TextAttribute.FONT, textFont); + attributedText.addAttribute(TextAttribute.SIZE, mFontSize); + + // Skips the check if we don't specify a fallbackFont. + if (fallbackFont != null) { + // Adds the attribute to use default font to draw the word "Android". + if (text.contains(ANDROID_STRING) + && textFont.canDisplayUpTo(ANDROID_STRING) != -1) { + int index = text.indexOf(ANDROID_STRING); + attributedText.addAttribute(TextAttribute.FONT, fallbackFont, index, + index + ANDROID_STRING.length()); + } + + // Adds the attribute to use default font to draw the PUNCTUATIONS ", . !" + for (char punctuation : PUNCTUATIONS) { + if (text.indexOf(punctuation) != -1 && !textFont.canDisplay(punctuation)) { + int index = 0; + while ((index = text.indexOf(punctuation, index)) != -1) { + attributedText.addAttribute(TextAttribute.FONT, fallbackFont, index, + index + 1); + index += 1; + } + } + } + } + + mWrappedLines.add(new LineInfo(attributedText, width)); + } + } + /** Initailizes the fields of the image image. */ public ImageGenerator( int initialImageWidth, @@ -177,6 +263,7 @@ public class ImageGenerator { mTextName = textName; mFontSize = fontSize; mFontDirPath = fontDirPath; + mLoadedFontMap = new TreeMap<>(); mCenterAlignment = centerAlignment; } @@ -239,12 +326,13 @@ public class ImageGenerator { * directory and collect the translated text. * * @param resourcePath the path to the resource directory + * @param localesSet a list of supported locales; resources of other locales will be omitted. * @return a map with the locale as key, and translated text as value * @throws LocalizedStringNotFoundException if we cannot find the translated text for the given * locale */ - public Map<Locale, String> readLocalizedStringFromXmls(String resourcePath) - throws IOException, LocalizedStringNotFoundException { + public Map<Locale, String> readLocalizedStringFromXmls(String resourcePath, + Set<String> localesSet) throws IOException, LocalizedStringNotFoundException { File resourceDir = new File(resourcePath); if (!resourceDir.isDirectory()) { throw new LocalizedStringNotFoundException(resourcePath + " is not a directory."); @@ -271,6 +359,12 @@ public class ImageGenerator { String[] nameList = resourceDir.list((File file, String name) -> name.startsWith("values-")); for (String name : nameList) { + String localeString = name.substring(7); + if (localesSet != null && !localesSet.contains(localeString)) { + LOGGER.info("Skip parsing text for locale " + localeString); + continue; + } + File textFile = new File(resourcePath, name + "/strings.xml"); String localizedText; try { @@ -295,12 +389,18 @@ public class ImageGenerator { * @throws FontFormatException if the font file doesn't have the expected format */ private Font loadFontsByLocale(String language) throws IOException, FontFormatException { + if (mLoadedFontMap.containsKey(language)) { + return mLoadedFontMap.get(language); + } + String fontName = LANGUAGE_TO_FONT_MAP.getOrDefault(language, DEFAULT_FONT_NAME); String[] suffixes = {".otf", ".ttf", ".ttc"}; for (String suffix : suffixes) { File fontFile = new File(mFontDirPath, fontName + suffix); if (fontFile.isFile()) { - return Font.createFont(Font.TRUETYPE_FONT, fontFile).deriveFont(mFontSize); + Font result = Font.createFont(Font.TRUETYPE_FONT, fontFile).deriveFont(mFontSize); + mLoadedFontMap.put(language, result); + return result; } } @@ -309,39 +409,53 @@ public class ImageGenerator { } /** Separates the text string by spaces and wraps it by words. */ - private List<String> wrapTextByWords(String text, FontMetrics metrics) { - List<String> wrappedText = new ArrayList<>(); + private WrappedTextInfo wrapTextByWords(String text, FontMetrics metrics) { + WrappedTextInfo info = new WrappedTextInfo(); StringTokenizer st = new StringTokenizer(text, " \n"); + int lineWidth = 0; // Width of the processed words of the current line. StringBuilder line = new StringBuilder(); while (st.hasMoreTokens()) { String token = st.nextToken(); - if (metrics.stringWidth(line + token + " ") > mImageWidth) { - wrappedText.add(line.toString()); + int tokenWidth = metrics.stringWidth(token + " "); + // Handles the width mismatch of the word "Android" between different fonts. + if (token.contains(ANDROID_STRING) + && metrics.getFont().canDisplayUpTo(ANDROID_STRING) != -1) { + tokenWidth = tokenWidth - metrics.stringWidth(ANDROID_STRING) + mAndroidStringWidth; + } + + if (lineWidth + tokenWidth > mImageWidth) { + info.addLine(line.toString(), lineWidth, metrics.getFont(), mDefaultFont); + line = new StringBuilder(); + lineWidth = 0; } line.append(token).append(" "); + lineWidth += tokenWidth; } - wrappedText.add(line.toString()); - return wrappedText; + info.addLine(line.toString(), lineWidth, metrics.getFont(), mDefaultFont); + + return info; } /** One character is a word for CJK. */ - private List<String> wrapTextByCharacters(String text, FontMetrics metrics) { - List<String> wrappedText = new ArrayList<>(); - + private WrappedTextInfo wrapTextByCharacters(String text, FontMetrics metrics) { + WrappedTextInfo info = new WrappedTextInfo(); + // TODO (xunchang) handle the text wrapping with logogram language mixed with latin. StringBuilder line = new StringBuilder(); for (char token : text.toCharArray()) { if (metrics.stringWidth(line + Character.toString(token)) > mImageWidth) { - wrappedText.add(line.toString()); + info.addLine(line.toString(), metrics.stringWidth(line.toString()), + metrics.getFont(), null); line = new StringBuilder(); } line.append(token); } - wrappedText.add(line.toString()); + info.addLine(line.toString(), metrics.stringWidth(line.toString()), metrics.getFont(), + null); - return wrappedText; + return info; } /** @@ -350,9 +464,10 @@ public class ImageGenerator { * @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 list of strings with their width smaller than mImageWidth pixels + * @return a WrappedTextInfo class with the width of each AttributedString smaller than + * mImageWidth pixels */ - private List<String> wrapText(String text, FontMetrics metrics, String language) { + private WrappedTextInfo wrapText(String text, FontMetrics metrics, String language) { if (LOGOGRAM_LANGUAGE.contains(language)) { return wrapTextByCharacters(text, metrics); } @@ -401,11 +516,11 @@ public class ImageGenerator { throws IOException, FontFormatException { Graphics2D graphics = createGraphics(locale); FontMetrics fontMetrics = graphics.getFontMetrics(); - List<String> wrappedText = wrapText(text, fontMetrics, locale.getLanguage()); + WrappedTextInfo wrappedTextInfo = wrapText(text, fontMetrics, locale.getLanguage()); int textWidth = 0; - for (String line : wrappedText) { - textWidth = Math.max(textWidth, fontMetrics.stringWidth(line)); + for (WrappedTextInfo.LineInfo lineInfo : wrappedTextInfo.mWrappedLines) { + textWidth = Math.max(textWidth, lineInfo.mLineWidth); } // This may happen if one single word is larger than the image width. @@ -432,21 +547,23 @@ public class ImageGenerator { */ private void drawText(String text, Locale locale, String languageTag) throws IOException, FontFormatException { - System.out.println("Encoding \"" + locale + "\" as \"" + languageTag + "\": " + text); + LOGGER.info("Encoding \"" + locale + "\" as \"" + languageTag + "\": " + text); Graphics2D graphics = createGraphics(locale); FontMetrics fontMetrics = graphics.getFontMetrics(); - List<String> wrappedText = wrapText(text, fontMetrics, locale.getLanguage()); + WrappedTextInfo wrappedTextInfo = wrapText(text, fontMetrics, locale.getLanguage()); // Marks the start y offset for the text image of current locale; and reserves one line to // encode the image metadata. int currentImageStart = mVerticalOffset; mVerticalOffset += 1; - for (String line : wrappedText) { + for (WrappedTextInfo.LineInfo lineInfo : wrappedTextInfo.mWrappedLines) { int lineHeight = fontMetrics.getHeight(); // Doubles the height of the image if we are short of space. if (mVerticalOffset + lineHeight >= mImageHeight) { resize(mImageWidth, mImageHeight * 2); + // Recreates the graphics since it's attached to the buffered image. + graphics = createGraphics(locale); } // Draws the text at mVerticalOffset and increments the offset with line space. @@ -455,12 +572,11 @@ public class ImageGenerator { // Draws from right if it's an RTL language. int x = mCenterAlignment - ? (mImageWidth - fontMetrics.stringWidth(line)) / 2 + ? (mImageWidth - lineInfo.mLineWidth) / 2 : RTL_LANGUAGE.contains(languageTag) - ? mImageWidth - fontMetrics.stringWidth(line) + ? mImageWidth - lineInfo.mLineWidth : 0; - - graphics.drawString(line, x, baseLine); + graphics.drawString(lineInfo.mLineContent.getIterator(), x, baseLine); mVerticalOffset += lineHeight; } @@ -502,6 +618,11 @@ public class ImageGenerator { */ public void generateImage(Map<Locale, String> localizedTextMap, String outputPath) throws FontFormatException, IOException { + FontMetrics defaultFontMetrics = + createGraphics(Locale.forLanguageTag("en")).getFontMetrics(); + mDefaultFont = defaultFontMetrics.getFont(); + mAndroidStringWidth = defaultFontMetrics.stringWidth(ANDROID_STRING); + Map<String, Integer> languageCount = new TreeMap<>(); int textWidth = 0; for (Locale locale : localizedTextMap.keySet()) { @@ -587,6 +708,19 @@ public class ImageGenerator { .hasArg(false) .create()); + options.addOption( + OptionBuilder.withLongOpt("verbose") + .withDescription("Output the logging above info level.") + .hasArg(false) + .create()); + + options.addOption( + OptionBuilder.withLongOpt("locales") + .withDescription("A list of android locales separated by ',' e.g." + + " 'af,en,zh-rTW'") + .hasArg(true) + .create()); + return options; } @@ -606,6 +740,12 @@ public class ImageGenerator { int imageWidth = Integer.parseUnsignedInt(cmd.getOptionValue("image_width")); + if (cmd.hasOption("verbose")) { + LOGGER.setLevel(Level.INFO); + } else { + LOGGER.setLevel(Level.WARNING); + } + ImageGenerator imageGenerator = new ImageGenerator( imageWidth, @@ -614,8 +754,16 @@ public class ImageGenerator { cmd.getOptionValue("font_dir"), cmd.hasOption("center_alignment")); + Set<String> localesSet = null; + if (cmd.hasOption("locales")) { + String[] localesList = cmd.getOptionValue("locales").split(","); + localesSet = new HashSet<>(Arrays.asList(localesList)); + // Ensures that we have the default locale, all english translations are identical. + localesSet.add("en-rAU"); + } Map<Locale, String> localizedStringMap = - imageGenerator.readLocalizedStringFromXmls(cmd.getOptionValue("resource_dir")); + imageGenerator.readLocalizedStringFromXmls(cmd.getOptionValue("resource_dir"), + localesSet); imageGenerator.generateImage(localizedStringMap, cmd.getOptionValue("output_file")); } } |