| 416 | } |
| 417 | |
| 418 | Data Device::getTextureDataForText(std::string_view text, |
| 419 | const FontDefinition& textDefinition, |
| 420 | TextAlign align, |
| 421 | int& width, |
| 422 | int& height, |
| 423 | bool& hasPremultipliedAlpha) |
| 424 | { |
| 425 | Data ret; |
| 426 | do |
| 427 | { |
| 428 | BitmapDC& dc = sharedBitmapDC(); |
| 429 | |
| 430 | if (!dc.setFont(textDefinition._fontName.c_str(), (int)textDefinition._fontSize, false)) |
| 431 | { |
| 432 | AXLOGW("Can't found font({}), use system default", textDefinition._fontName); |
| 433 | } |
| 434 | |
| 435 | // draw text |
| 436 | // does changing to SIZE here affects the font size by rounding from float? |
| 437 | SIZE size = {(LONG)textDefinition._dimensions.width, (LONG)textDefinition._dimensions.height}; |
| 438 | AX_BREAK_IF(!dc.drawText(text, size, align, textDefinition._fontName.c_str(), (int)textDefinition._fontSize, |
| 439 | textDefinition._enableWrap, textDefinition._overflow)); |
| 440 | |
| 441 | int dataLen = size.cx * size.cy * 4; |
| 442 | unsigned char* dataBuf = (unsigned char*)malloc(sizeof(unsigned char) * dataLen); |
| 443 | AX_BREAK_IF(!dataBuf); |
| 444 | |
| 445 | struct |
| 446 | { |
| 447 | BITMAPINFOHEADER bmiHeader; |
| 448 | int mask[4]; |
| 449 | } bi = {0}; |
| 450 | bi.bmiHeader.biSize = sizeof(bi.bmiHeader); |
| 451 | AX_BREAK_IF(!GetDIBits(dc.getDC(), dc.getBitmap(), 0, 0, nullptr, (LPBITMAPINFO)&bi, DIB_RGB_COLORS)); |
| 452 | |
| 453 | width = static_cast<int>(size.cx); |
| 454 | height = static_cast<int>(size.cy); |
| 455 | |
| 456 | // copy pixel data |
| 457 | bi.bmiHeader.biHeight = (bi.bmiHeader.biHeight > 0) ? -bi.bmiHeader.biHeight : bi.bmiHeader.biHeight; |
| 458 | GetDIBits(dc.getDC(), dc.getBitmap(), 0, height, dataBuf, (LPBITMAPINFO)&bi, DIB_RGB_COLORS); |
| 459 | |
| 460 | COLORREF textColor = (textDefinition._fontFillColor.b << 16 | textDefinition._fontFillColor.g << 8 | |
| 461 | textDefinition._fontFillColor.r) & |
| 462 | 0x00ffffff; |
| 463 | float alpha = textDefinition._fontAlpha / 255.0f; |
| 464 | COLORREF* pPixel = nullptr; |
| 465 | for (int y = 0; y < height; ++y) |
| 466 | { |
| 467 | pPixel = (COLORREF*)dataBuf + y * width; |
| 468 | for (int x = 0; x < width; ++x) |
| 469 | { |
| 470 | COLORREF& clr = *pPixel; |
| 471 | clr = ((BYTE)(GetRValue(clr) * alpha) << 24) | textColor; |
| 472 | ++pPixel; |
| 473 | } |
| 474 | } |
| 475 | |