Font library (Rasterworks)

Discussing libraries simplifying applications development
maxcodehack
Posts: 406
Joined: Mon Sep 07, 2020 7:09 pm
Has thanked: 1 time
Been thanked: 1 time

Re: Font library (Rasterworks)

Post by maxcodehack »

Допустим есть код

Code: Select all

	char* text = "HELLO!";
void *buf = malloc(768*256*3 * sizeof(char));
	*((int*)buf) = 768;
	*((int*)buf+1) = 256;
	memset((char*)buf+8, (char)-1, 768*256*3);
	int len = countUTF8Z(text, -1);
	drawText(buf, 0, 0, text, len, 0xFFFF0000, 0x08031428);
Он работает, но фон белый
И где тут заполняется буфер цветом?
Если он тут не заполняется, как его тогда вообще заполнить?
PS это Си
User avatar
dunkaist
Mentor
Posts: 742
Joined: Mon Oct 19, 2009 10:58 am
Has thanked: 9 times
Been thanked: 10 times

Re: Font library (Rasterworks)

Post by dunkaist »

maxcodehack wrote:И где тут заполняется буфер цветом?

Code: Select all

memset((char*)buf+8, (char)-1, 768*256*3);
BTW, this memset writes beyond the buffer. Check the length.
maxcodehack
Posts: 406
Joined: Mon Sep 07, 2020 7:09 pm
Has thanked: 1 time
Been thanked: 1 time

Re: Font library (Rasterworks)

Post by maxcodehack »

I need to write
memset((char*)buf+8, (char)-1, 768*256*3 - sizeof(char));
?
I get this code from C_Layer
Last edited by maxcodehack on Sun Sep 27, 2020 7:52 pm, edited 2 times in total.
User avatar
dunkaist
Mentor
Posts: 742
Joined: Mon Oct 19, 2009 10:58 am
Has thanked: 9 times
Been thanked: 10 times

Re: Font library (Rasterworks)

Post by dunkaist »

What is the address of the last byte you allocate via malloc?
What is the address of the last byte you write to via memset?
maxcodehack wrote:sizeof(char)
You can read about sizeof(char) in the C standard draft, 6.5.3.4 The sizeof operator.
maxcodehack
Posts: 406
Joined: Mon Sep 07, 2020 7:09 pm
Has thanked: 1 time
Been thanked: 1 time

Re: Font library (Rasterworks)

Post by maxcodehack »

I get this code from C_Layer
User avatar
dunkaist
Mentor
Posts: 742
Joined: Mon Oct 19, 2009 10:58 am
Has thanked: 9 times
Been thanked: 10 times

Re: Font library (Rasterworks)

Post by dunkaist »

  1. A pixel color is coded as one or more channels.
  2. Each color channel is coded separately according to its depth.
  3. The number of channels and their ordering and depth are collectively called pixel format.
  4. The most common pixel formats have 8-bit channels, e.g. Red, Green, Blue. Hence for such pixel formats one channel occupies one byte.
  5. There is no single standartized way to name pixel formats, e.g. RGB, RGB888, true color and 24bit may refer to the same format.
  6. A convenient way to write colors in text is hex, one of binary-to-text encodings.
  7. Description of drawText function (part of RasterWorks) mentions two pixel formats accepted: 24bpp and 32bpp (bpp -- bits per pixel).
  8. You define the format to use in the last argument of the function, it's 24bpp in your example.
  9. What KolibriOS expects to be 24bpp can be found in description of sf7, it's BBGGRRBBGGRRBBGGRR...
  10. I.e. a byte describing Blue component of the first pixel, a byte describing Green component of the first pixel, a byte describing Red component of the first pixel. Then data of the second and other pixels go.
  11. White color in 24bpp pixel format is Red=255, Green=255, Blue=255. Or, using shorter hex notation, 0xFFFFFF.
  12. (char)-1 in your example is a byte with value 0xFF in hex, read why.
  13. So, the background color in your example is white because memset writes many 0xFF's, which are interpreted as 0xFFFFFF triplets, which mean white pixels.
  14. Description of drawText function says that the first argument is a pointer to the structure of three fields: xSize, ySize, picture.
  15. To allocate enough memory for this structure, calculate its size, which is the sum of the sizes of its fields. It's not always the case, but this time you are lucky.
qullarwee
Posts: 25
Joined: Wed Apr 05, 2023 5:41 pm
Has thanked: 6 times
Been thanked: 8 times

Re: Rasterworks

Post by qullarwee »

Pathoswithin wrote: Mon Nov 16, 2015 1:34 pm Добавил выравнивание по центру или справа с учётом указаной координаты Х (или двух). Сделал автоматическое сужение строки, если она не вписывается в изображение или указаную область. Функция возвращает итоговые ширину символа и координату начала текста.
Или я чего-то не понимаю, или... Буфер заканчивается ровно перед скроллбаром, но RasterWorks все равно сужает символ, хотя он туда бы влез.
mar.png
mar.png (1.4 KiB) Viewed 251 times
Код если что такой:

Code: Select all

drawText(buf, pen->pos.x, pen->pos.y, text, len, pen->color | 0xFF000000, (render_style_mask(pen) << 48) | 0x030000 | (pen->font_size.x << 16) | pen->font_size.y);
Спасибо.
User avatar
Pathoswithin
Mentor/Kernel Developer
Posts: 1307
Joined: Thu Mar 26, 2015 5:16 pm
Been thanked: 2 times

Re: Font library (Rasterworks)

Post by Pathoswithin »

Так падажжи...
Что выдаёт render_style_mask?
И... << 48 это же сдвиг на 48? Там всего 32 бита.
qullarwee
Posts: 25
Joined: Wed Apr 05, 2023 5:41 pm
Has thanked: 6 times
Been thanked: 8 times

Re: Font library (Rasterworks)

Post by qullarwee »

Упс.. Должно быть так:

Code: Select all

drawText(buf, pen->pos.x, pen->pos.y, text, len, pen->color | 0xFF000000, (render_style_mask(pen) << 24) | 0x030000 | (pen->font_size.x << 8) | pen->font_size.y);
Но ошибка не пропала.

render_style_mask выдает маску стиля (bold, italic):

Code: Select all

return pen->style.bold | (pen->style.italic << 1);
User avatar
Pathoswithin
Mentor/Kernel Developer
Posts: 1307
Joined: Thu Mar 26, 2015 5:16 pm
Been thanked: 2 times

Re: Font library (Rasterworks)

Post by Pathoswithin »

Глянул код. Действительно, ширина строки рассчитывается на символ больше. А вот почему я так сделал уже не помню...
Может, из-за курсива.
qullarwee
Posts: 25
Joined: Wed Apr 05, 2023 5:41 pm
Has thanked: 6 times
Been thanked: 8 times

Re: Font library (Rasterworks)

Post by qullarwee »

Так и оставите, или поменяете потом как нибудь?
User avatar
Pathoswithin
Mentor/Kernel Developer
Posts: 1307
Joined: Thu Mar 26, 2015 5:16 pm
Been thanked: 2 times

Re: Font library (Rasterworks)

Post by Pathoswithin »

Да оставлю пожалуй, раз зачем-то сделал. Иначе как минимум курсив будет вылазить с другой стороны, а то и падать с ошибкой. Ничего не мешает выводить на экран не всю ширину буфера.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests