Пример #1
0
TEST(LocaleTest, Misc) {
  EXPECT_TRUE(matches_locale("zh-CN", "zh-Hans-CN"));
  EXPECT_TRUE(matches_locale("zh", "zh-Hans-CN"));
  EXPECT_FALSE(matches_locale("zh-HK", "zh-Hans-CN"));
  EXPECT_TRUE(matches_locale("en-GB", "en-GB"));
  EXPECT_TRUE(matches_locale("en", "en-GB"));
  EXPECT_FALSE(matches_locale("en-GB", "en"));
  EXPECT_FALSE(matches_locale("en-GB", "en-US"));
  EXPECT_FALSE(matches_locale("en-US", ""));
  // Empty locale prefix in the PNG file will match the input locale.
  EXPECT_TRUE(matches_locale("", "en-US"));
  EXPECT_TRUE(matches_locale("sr-Latn", "sr-Latn-BA"));
}
Пример #2
0
int res_create_localized_surface(const char* name, gr_surface* pSurface) {
    char resPath[256];
    GGLSurface* surface = NULL;
    int result = 0;
    unsigned char header[8];
    png_structp png_ptr = NULL;
    png_infop info_ptr = NULL;

    *pSurface = NULL;

    snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name);
    resPath[sizeof(resPath)-1] = '\0';
    FILE* fp = fopen(resPath, "rb");
    if (fp == NULL) {
        result = -1;
        goto exit;
    }

    size_t bytesRead = fread(header, 1, sizeof(header), fp);
    if (bytesRead != sizeof(header)) {
        result = -2;
        goto exit;
    }

    if (png_sig_cmp(header, 0, sizeof(header))) {
        result = -3;
        goto exit;
    }

    png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    if (!png_ptr) {
        result = -4;
        goto exit;
    }

    info_ptr = png_create_info_struct(png_ptr);
    if (!info_ptr) {
        result = -5;
        goto exit;
    }

    if (setjmp(png_jmpbuf(png_ptr))) {
        result = -6;
        goto exit;
    }

    png_init_io(png_ptr, fp);
    png_set_sig_bytes(png_ptr, sizeof(header));
    png_read_info(png_ptr, info_ptr);

    int color_type, bit_depth;
    size_t width, height;

    png_get_IHDR(png_ptr, info_ptr, width, height, &bit_depth,
                 &color_type, NULL, NULL, NULL);

    png_byte* channels = png_get_channels(png_ptr, info_ptr);
    size_t stride = 4 * width;

    if (!(bit_depth == 8 &&
            (channels == 1 && color_type == PNG_COLOR_TYPE_GRAY))) {
        return -7;
        goto exit;
    }

    unsigned char* row = malloc(width);
    int y;
    for (y = 0; y < height; ++y) {
        png_read_row(png_ptr, row, NULL);
        int w = (row[1] << 8) | row[0];
        int h = (row[3] << 8) | row[2];
        int len = row[4];
        char* loc = row+5;

        if (y+1+h >= height || matches_locale(loc)) {
            printf("  %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y);

            surface = malloc(sizeof(GGLSurface));
            if (surface == NULL) {
                result = -8;
                goto exit;
            }
            unsigned char* pData = malloc(w*h);

            surface->version = sizeof(GGLSurface);
            surface->width = w;
            surface->height = h;
            surface->stride = w; /* Yes, pixels, not bytes */
            surface->data = pData;
            surface->format = GGL_PIXEL_FORMAT_A_8;

            int i;
            for (i = 0; i < h; ++i, ++y) {
                png_read_row(png_ptr, row, NULL);
                memcpy(pData + i*w, row, w);
            }

            *pSurface = (gr_surface) surface;
            break;
        } else {
            int i;
            for (i = 0; i < h; ++i, ++y) {
                png_read_row(png_ptr, row, NULL);
            }
        }
    }

exit:
    png_destroy_read_struct(&png_ptr, &info_ptr, NULL);

    if (fp != NULL) {
        fclose(fp);
    }
    if (result < 0) {
        if (surface) {
            free(surface);
        }
    }
    return result;
}
int res_create_theme_localized_alpha_surface(const char* name,
                                             const char* themename,
                                             const char* locale,
                                             gr_surface* pSurface) {
    gr_surface surface = NULL;
    int result = 0;
    png_structp png_ptr = NULL;
    png_infop info_ptr = NULL;
    png_uint_32 width, height;
    png_byte channels;

    *pSurface = NULL;

    if (locale == NULL) {
        surface = malloc_surface(0);
        surface->width = 0;
        surface->height = 0;
        surface->row_bytes = 0;
        surface->pixel_bytes = 1;
        goto exit;
    }

    result = open_theme_png(name, themename, &png_ptr, &info_ptr, &width, &height, &channels);
    if (result < 0) return result;

    if (channels != 1) {
        result = -7;
        goto exit;
    }

    unsigned char* row = malloc(width);
    png_uint_32 y;
    for (y = 0; y < height; ++y) {
        png_read_row(png_ptr, row, NULL);
        int w = (row[1] << 8) | row[0];
        int h = (row[3] << 8) | row[2];
        int len = row[4];
        char* loc = (char*)row+5;

        if (y+1+h >= height || matches_locale(loc, locale)) {
            printf("  %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y);

            surface = malloc_surface(w*h);
            if (surface == NULL) {
                result = -8;
                goto exit;
            }
            surface->width = w;
            surface->height = h;
            surface->row_bytes = w;
            surface->pixel_bytes = 1;

            int i;
            for (i = 0; i < h; ++i, ++y) {
                png_read_row(png_ptr, row, NULL);
                memcpy(surface->data + i*w, row, w);
            }

            *pSurface = (gr_surface) surface;
            break;
        } else {
            int i;
            for (i = 0; i < h; ++i, ++y) {
                png_read_row(png_ptr, row, NULL);
            }
        }
    }

    exit:
    png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
    if (result < 0 && surface != NULL) free(surface);
    return result;
}
int
res_create_localized_alpha_surface(const char *name, const char *locale,
				   gr_surface *pSurface)
{
	int result = 0;
	unsigned char *row;
	gr_surface surface = NULL;
	png_structp png_ptr = NULL;
	png_infop info_ptr = NULL;
	png_uint_32 width, height, y;
	png_byte channels;
	FILE *fp = NULL;

	*pSurface = NULL;

	if (!locale) {
		surface = malloc_surface(0);
		surface->width = 0;
		surface->height = 0;
		surface->row_bytes = 0;
		surface->pixel_bytes = 1;
		goto exit;
	}

	result = open_png(name, &png_ptr, &info_ptr, &fp, &width, &height,
			  &channels);
	if (result < 0)
		return result;

	if (channels != 1) {
		result = -7;
		goto exit;
	}

	/* TODO: check for error */
	row = malloc(width);
	for (y = 0; y < height; y++) {
		int h, w;
		char *loc;

		png_read_row(png_ptr, row, NULL);
		w = (row[1] << 8) | row[0];
		h = (row[3] << 8) | row[2];
		loc = (char *)row + 5;

		if (y + 1 + h >= height || matches_locale(loc, locale)) {
			int i;

			printf("  %20s: %s (%d x %d @ %ld)\n", name, loc, w,
			       h, (long)y);

			if (!(surface = malloc_surface(w * h))) {
				result = -8;
				goto exit;
			}

			surface->width = w;
			surface->height = h;
			surface->row_bytes = w;
			surface->pixel_bytes = 1;

			for (i = 0; i < h; i++, y++) {
				png_read_row(png_ptr, row, NULL);
				memcpy(surface->data + i * w, row, w);
			}

			*pSurface = (gr_surface)surface;
			break;
		} else {
			int i;

			for (i = 0; i < h; i++, y++)
				png_read_row(png_ptr, row, NULL);
		}
	}

exit:
	close_png(&png_ptr, &info_ptr, fp);
	if (result < 0 && surface)
		free(surface);

	return result;
}