recovery 界面汉化过程详解
一. 主要是针对recovery汉化,主要汉化对象是界面显示为中文。
二. 基于中文的汉化,有两种方式,一种是基于GB2312的编码格式汉化,另外一种是基于unicode编码格式汉化。下面介绍unicode中文汉化。
三. 汉化主要需要修改四个文件,汉化步骤:
3.1 default_device.cpp修改,主界面汉化
增加中文的界面,但是由于是unicode编码,所以在source insight里面显示乱码:
这些看似乱码,我们可以这样获取,桌面新建txt文档,使用notepad++工具打开, 选择格式,以UTF-8 无BOM格式编码,里面写入需要显示的汉字,然后保存,然后把新建的txt使用source insight打开即可。
3.2 unicode的中文字库获取,网上可以下载, eg:fontcn30_18x48.h
3.3 针对unicode的解析源文件graphics_cn.c
/*
* Copyright (C) 2007 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 <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <linux/fb.h>
#include <linux/kd.h>
#include <time.h>
#include "fontcn30_18x48.h"
//#include "font_10x18.h"
#include "minui.h"
#include "graphics.h"
typedef struct {
GRSurface* texture;
unsigned offset[97];
void** fontdata;
unsigned count;
unsigned *unicodemap;
unsigned char *cwidth;
unsigned char *cheight;
unsigned ascent;
} GRFont;
static GRFont* gr_font = NULL;
static minui_backend* gr_backend = NULL;
static int overscan_percent = OVERSCAN_PERCENT;
static int overscan_offset_x = 0;
static int overscan_offset_y = 0;
static int gr_vt_fd = -1;
const unsigned cw_en = 10;
static unsigned char gr_current_r = 255;
static unsigned char gr_current_g = 255;
static unsigned char gr_current_b = 255;
static unsigned char gr_current_a = 255;
static GRSurface* gr_draw = NULL;
static bool outside(int x, int y)
{
return x < 0 || x >= gr_draw->width || y < 0 || y >= gr_draw->height;
}
int getUNICharID(unsigned short unicode, void* pFont)
{
int i;
GRFont *gfont = (GRFont*) pFont;
if (!gfont) gfont = gr_font;
for (i = 0; i < gfont->count; i++) {
if (unicode == gfont->unicodemap[i]) return i;
}
return -1;
}
int gr_measure(const char *s)
{
return 0;
}
void gr_font_size(int *x, int *y)
{
*x = gr_font->cwidth[100];
*y = gr_font->cheight[100];
}
static void text_blend(unsigned char* src_p, int src_row_bytes,
unsigned char* dst_p, int dst_row_bytes,
int width, int height)
{
int i, j;
for (j = 0; j < height; ++j) {
unsigned char* sx = src_p;
unsigned char* px = dst_p;
for (i = 0; i < width; ++i) {
unsigned char a = *sx++;
if (gr_current_a < 255) a = ((int)a * gr_current_a) / 255;
if (a == 255) {
*px++ = gr_current_r;
*px++ = gr_current_g;
*px++ = gr_current_b;
*px++ = 255;
} else if (a > 0) {
*px = (*px * (255-a) + gr_current_r * a) / 255;
++px;
*px = (*px * (255-a) + gr_current_g * a) / 255;
++px;
*px = (*px * (255-a) + gr_current_b * a) / 255;
++px;
*px++ = 255;
} else {
px += 3;
*px++ = 255;
}
}
src_p += src_row_bytes;
dst_p += dst_row_bytes;
}
}
void gr_text(int x, int y, const char *s, int bold)
{
GRFont *font = gr_font;
unsigned off, n , width, height;
unsigned off2;
unsigned off3;
wchar_t ch;
int id;
unsigned short unicode;
if (!font->texture) return;
if (gr_current_a == 0) return;
x += overscan_offset_x;
y += overscan_offset_y;
//printf("--------%X %X %X %X\n", s[0], s[1], s[2], s[3]);
while((off = *s++)) {
if (off < 0x80)
{
//ASCII 不处理
} else {
if ((off & 0xF0) == 0xE0)
{
off2 = *s++;
off3 = *s++;
unicode = (off & 0x1F) << 12;
unicode |= (off2 & 0x3F) << 6;
unicode |= (off3 & 0x3F);
id = getUNICharID(unicode, font);
//printf("%X %X %X %X %d\n", off, off2, off3, unicode, id);
if (id >= 0) {
width = font->cwidth[id];
height = font->cheight[id];
unsigned char* src_p = font->fontdata[id];
unsigned char* dst_p = gr_draw->data + y*gr_draw->row_bytes + x*gr_draw->pixel_bytes;
text_blend(src_p, font->texture->row_bytes,dst_p, gr_draw->row_bytes,width, height);
x += width;
} else {
x += width;
}
} else {
x += cw_en;
}
}
}
}
void gr_texticon(int x, int y, GRSurface* icon) {
if (icon == NULL) return;
if (icon->pixel_bytes != 1) {
printf("gr_texticon: source has wrong format\n");
return;
}
x += overscan_offset_x;
y += overscan_offset_y;
if (outside(x, y) || outside(x+icon->width-1, y+icon->height-1)) return;
unsigned char* src_p = icon->data;
unsigned char* dst_p = gr_draw->data + y*gr_draw->row_bytes + x*gr_draw->pixel_bytes;
text_blend(src_p, icon->row_bytes,
dst_p, gr_draw->row_bytes,
icon->width, icon->height);
}
void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
{
gr_current_r = r;
gr_current_g = g;
gr_current_b = b;
gr_current_a = a;
}
void gr_clear()
{
if (gr_current_r == gr_current_g &&
gr_current_r == gr_current_b) {
memset(gr_draw->data, gr_current_r, gr_draw->height * gr_draw->row_bytes);
} else {
int x, y;
unsigned char* px = gr_draw->data;
for (y = 0; y < gr_draw->height; ++y) {
for (x = 0; x < gr_draw->width; ++x) {
*px++ = gr_current_r;
*px++ = gr_current_g;
*px++ = gr_current_b;
px++;
}
px += gr_draw->row_bytes - (gr_draw->width * gr_draw->pixel_bytes);
}
}
}
void gr_fill(int x1, int y1, int x2, int y2)
{
x1 += overscan_offset_x;
y1 += overscan_offset_y;
x2 += overscan_offset_x;
y2 += overscan_offset_y;
if (outside(x1, y1) || outside(x2-1, y2-1)) return;
unsigned char* p = gr_draw->data + y1 * gr_draw->row_bytes + x1 * gr_draw->pixel_bytes;
if (gr_current_a == 255) {
int x, y;
for (y = y1; y < y2; ++y) {
unsigned char* px = p;
for (x = x1; x < x2; ++x) {
*px++ = gr_current_r;
*px++ = gr_current_g;
*px++ = gr_current_b;
*px++ = 255;
}
p += gr_draw->row_bytes;
}
} else if (gr_current_a > 0) {
int x, y;
for (y = y1; y < y2; ++y) {
unsigned char* px = p;
for (x = x1; x < x2; ++x) {
*px = (*px * (255-gr_current_a) + gr_current_r * gr_current_a) / 255;
++px;
*px = (*px * (255-gr_current_a) + gr_current_g * gr_current_a) / 255;
++px;
*px = (*px * (255-gr_current_a) + gr_current_b * gr_current_a) / 255;
++px;
*px++ = 255;
}
p += gr_draw->row_bytes;
}
}
}
void gr_blit(GRSurface* source, int sx, int sy, int w, int h, int dx, int dy) {
if (source == NULL) return;
if (gr_draw->pixel_bytes != source->pixel_bytes) {
printf("gr_blit: source has wrong format\n");
return;
}
dx += overscan_offset_x;
dy += overscan_offset_y;
if (outside(dx, dy) || outside(dx+w-1, dy+h-1)) return;
unsigned char* src_p = source->data + sy*source->row_bytes + sx*source->pixel_bytes;
unsigned char* dst_p = gr_draw->data + dy*gr_draw->row_bytes + dx*gr_draw->pixel_bytes;
int i;
int j;
for (i = 0; i < h; ++i) {
if (source->pixel_bytes == 4 && source->alpha ==1) {
unsigned char *p0 = src_p;
unsigned char *p1 = dst_p;
unsigned char alpha = 255;
for (j = 0; j < w; j++) {
alpha = *(p0+3);
*p1 = (*p1 * (255-alpha) + *p0++ * alpha) / 255;
++p1;
*p1 = (*p1 * (255-alpha) + *p0++ * alpha) / 255;
++p1;
*p1 = (*p1 * (255-alpha) + *p0++ * alpha) / 255;
++p1;
*p1++ = 255;
++p0;
}
} else {
memcpy(dst_p, src_p, w * source->pixel_bytes);
}
src_p += source->row_bytes;
dst_p += gr_draw->row_bytes;
}
}
unsigned int gr_get_width(GRSurface* surface) {
if (surface == NULL) {
return 0;
}
return surface->width;
}
unsigned int gr_get_height(GRSurface* surface) {
if (surface == NULL) {
return 0;
}
return surface->height;
}
static void gr_init_font(void)
{
unsigned i, n, d;
void** font_data;
int bmp, pos;
unsigned char *in, data;
unsigned char *width, *height;
gr_font = calloc(sizeof(*gr_font), 1);
font_data = (void**)malloc(font.count * sizeof(void*));
width = malloc(font.count);
height = malloc(font.count);
for(n = 0; n < font.count; n++) {
if (n<95) {
font_data[n] = malloc(font.ewidth*font.eheight);
memset(font_data[n], 0, font.ewidth*font.eheight);
width[n] = font.ewidth;
height[n] = font.eheight;
}
else {
font_data[n] = malloc(font.cwidth*font.cheight);
memset(font_data[n], 0, font.cwidth * font.cheight);
width[n] = font.cwidth;
height[n] = font.cheight;
}
}
d = 0;
in = font.rundata;
while((data = *in++)) {
n = data & 0x7f;
for(i = 0; i < n; i++, d++) {
if (d<95*font.ewidth*font.eheight) {
bmp = d/(font.ewidth*font.eheight);
pos = d%(font.ewidth*font.eheight);
}
else {
bmp = (d-95*font.ewidth*font.eheight)/(font.cwidth*font.cheight)+95;
pos = (d-95*font.ewidth*font.eheight)%(font.cwidth*font.cheight);
}
((unsigned char*)(font_data[bmp]))[pos] = (data & 0x80) ? 0xff : 0;
}
}
gr_font->texture = malloc(sizeof(GRSurface));
gr_font->texture->width = font.ewidth;
gr_font->texture->height = font.eheight;
gr_font->texture->row_bytes = font.cwidth;
gr_font->texture->pixel_bytes = 1;
gr_font->count = font.count;
gr_font->unicodemap = font.unicodemap;
gr_font->cwidth = width;
gr_font->cheight = height;
gr_font->fontdata = font_data;
gr_font->ascent = font.cheight;
}
#if 0
// Exercises many of the gr_*() functions; useful for testing.
static void gr_test() {
GRSurface** images;
int frames;
int result = res_create_multi_surface("icon_installing", &frames, &images);
if (result < 0) {
printf("create surface %d\n", result);
gr_exit();
return;
}
time_t start = time(NULL);
int x;
for (x = 0; x <= 1200; ++x) {
if (x < 400) {
gr_color(0, 0, 0, 255);
} else {
gr_color(0, (x-400)%128, 0, 255);
}
gr_clear();
gr_color(255, 0, 0, 255);
gr_surface frame = images[x%frames];
gr_blit(frame, 0, 0, frame->width, frame->height, x, 0);
gr_color(255, 0, 0, 128);
gr_fill(400, 150, 600, 350);
gr_color(255, 255, 255, 255);
gr_text(500, 225, "hello, world!", 0);
gr_color(255, 255, 0, 128);
gr_text(300+x, 275, "pack my box with five dozen liquor jugs", 1);
gr_color(0, 0, 255, 128);
gr_fill(gr_draw->width - 200 - x, 300, gr_draw->width - x, 500);
gr_draw = gr_backend->flip(gr_backend);
}
printf("getting end time\n");
time_t end = time(NULL);
printf("got end time\n");
printf("start %ld end %ld\n", (long)start, (long)end);
if (end > start) {
printf("%.2f fps\n", ((double)x) / (end-start));
}
}
#endif
void gr_flip() {
gr_draw = gr_backend->flip(gr_backend);
}
int gr_init(void)
{
gr_init_font();
gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC);
if (gr_vt_fd < 0) {
// This is non-fatal; post-Cupcake kernels don't have tty0.
perror("can't open /dev/tty0");
} else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) {
// However, if we do open tty0, we expect the ioctl to work.
perror("failed KDSETMODE to KD_GRAPHICS on tty0");
gr_exit();
return -1;
}
gr_backend = open_adf();
if (gr_backend) {
gr_draw = gr_backend->init(gr_backend);
if (!gr_draw) {
gr_backend->exit(gr_backend);
}
}
if (!gr_draw) {
gr_backend = open_fbdev();
gr_draw = gr_backend->init(gr_backend);
if (gr_draw == NULL) {
return -1;
}
}
overscan_offset_x = gr_draw->width * overscan_percent / 100;
overscan_offset_y = gr_draw->height * overscan_percent / 100;
gr_flip();
gr_flip();
return 0;
}
void gr_exit(void)
{
gr_backend->exit(gr_backend);
ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT);
close(gr_vt_fd);
gr_vt_fd = -1;
}
int gr_fb_width(void)
{
return gr_draw->width - 2*overscan_offset_x;
}
int gr_fb_height(void)
{
return gr_draw->height - 2*overscan_offset_y;
}
void gr_fb_blank(bool blank)
{
gr_backend->blank(gr_backend, blank);
}
3.4 minui/Android.mk中选择graphics_cn.c编译,使用宏控制:
ifeq ($(TARGET_RECOVERY_SUPPORT_CHINESE_FONT), true) LOCAL_SRC_FILES += graphics_cn.c else LOCAL_SRC_FILES += graphics.c endif
四. 至此汉化完成,我们定义TARGET_RECOVERY_SUPPORT_CHINESE_FONT即可,编译出来的界面即为中文界面,若遇到其他问题,相应的解决即可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!