C++位图中的ttc字体

Posted

技术标签:

【中文标题】C++位图中的ttc字体【英文标题】:ttc Font in Bitmap in C++ 【发布时间】:2020-05-06 13:37:50 【问题描述】:

有没有一种简单的方法可以将字体插入位图图像? 目前我使用以下类来编辑我的位图:

class BitmapImg
public:
 BitmapImg();
 ~BitmapImg();
 void setPixel(int x, int y, int redn, int greenn, int bluen);
 void getPixel(int x, int y, int& redn, int& greenn, int& bluen);
private:
 unsigned short int red[1080][1080]; //1080X1080 Pixels
 unsigned short int green[1080][1080];
 unsigned short int blue[1080][1080];
;

但现在我已经开始通过 xbm 文件将字母导入 XCode,然后使用各种循环来更改数组中的 RGB 值。但是这个解决方案非常复杂。

我也很难从图像中获取单个像素位。目前我使用这个循环来改变我在位图图像中的像素:

BitmapImg Picture;
// ctor makes is completely blue -> no problem with the whit color below
int counter = 0;
for (int y=0;y<=199;y++)

    for (int x = 0; x<=199 ;x++)
    
        for (int n = 0; n<16;n++)
        
            bool bit =(A_bits[counter]>>n) & 1U;
                       
            if(bit)
              Picture.setPixel(counter%200,counter%200,255,255,255);
            counter ++;
            std::cout << counter<< std::endl; //for debugging
        
    

xvm-File 的头文件:

 #define A_width 200
 #define A_height 200
 static unsigned short A_bits[] =  0x0000, 0x0000,.... 

xbm 文件描述了一个“A”,但我只从左上角对角线得到一个像素宽的线。

【问题讨论】:

【参考方案1】:

此外,我在取出单个位时遇到了问题。目前我使用这个循环来改变我在位图图像中的像素:

基本上,您在这里尝试做的是将像素从一张图像复制到另一张图像。 XBM 只是一种非常基本的单声道格式,因此只是将像素设置为所需颜色(前景)或保持原样(背景)的问题。

这几乎是你所拥有的。请注意,这会将其绘制在图像的左上角 (0,0),并假设目标图像足够大。您应该添加边界检查以安全地剪辑平局。

void draw_A(BitmapImg &picture, unsigned short r, unsigned short g, unsigned short b)

    for (int y = 0; y < A_height; ++y)
    
        for (int x = 0; x < A_width; ++x)
        
            unsigned bytes_per_row = (A_width + 7) / 8; // divide rounding up
            unsigned row_byte = x / 8; // 8 bits per byte
            unsigned row_bit = x % 8;
            unsigned char src_byte = A_bits[y * bytes_per_row + row_byte]; // row by row, left to right, top to bottom
            bool src_bit = ((src_byte >> row_bit) & 0x1) != 0; // least signifcant bit (1) is first
            if (src_bit)
            
                picture.setPixel(x, y, r, g, b);
            
        
    


unsigned short int red[1080][1080]; //1080X1080 Pixels
unsigned short int green[1080][1080];
unsigned short int blue[1080][1080];

请注意,这是一种非常不常见的存储图像数据的方式,通常每个像素都保存在一个数组中,而且 2D 数组不适用于动态调整大小(你不能这样做p = new unsigned short[width][height]; p[50][40] = 10;)。

例如 8bpp 24 位 RGB 可能存储为 unsigned char pixels[width * height * 3]; pixels[50 * 3 + 40 * width * 3 + 1] = 10; /*set green at (50,40)*/。在处理许多库和文件格式的图像数据时,您会看到这一点。尽管请注意某些图像格式尤其是从底部开始,而不是从顶部开始。

但现在我已经开始通过 xbm 文件将字母导入 XCode,然后使用各种循环来更改数组中的 RGB 值。但是这个解决方案非常复杂。

直接用图像做很多事情,但往往会变得相当复杂,尤其是一旦开始考虑透明度/混合、缩放、旋转等变换,以及要处理的所有不同图像格式(索引颜色、16、24、32 等) . 位整数像素、自下而上而不是自上而下的格式等)。

周围有许多图形库,包括硬件加速和软件、平台特定(例如所有 Windows GDI、DirectWrite、Direct2D、Direct3D 等)或可移植的。其中许多库将支持该样式的输入和输出图像,对特定像素使用不同的格式,并提供大量解决方案来根据需要将一种格式转换为另一种格式。

【讨论】:

以上是关于C++位图中的ttc字体的主要内容,如果未能解决你的问题,请参考以下文章

关于JDK/Java支持TTC字体的思路

使用 ttc 文件的 Css 字体

可以使用FontForge查看TTC字体文件中,包含哪些字体

PDFBox 2.0 和 TTC 字体

同样是字体文件,TTF和TTC有何差异

TTF OTF TTC 这三种格式的字体哪种好?跪求解释