Commit graph

193 commits

Author SHA1 Message Date
Jelle Raaijmakers
15a9fca461 LibGfx: Correctly shift pixels during bilinear blend
Our bilinear scaling logic worked well for upscaling, but during
downscaling the bitmap was often shifted one pixel to the bottom right.

This is a common problem, described here in more detail:

  https://bartwronski.com/2021/02/15/bilinear-down-upsampling-pixel-grids-and-that-half-pixel-offset/

Fix it by calculating coordinate shift values that align the pixel's
boundaries between the source and target pixels before selecting the
source pixels to interpolate.
2022-09-06 18:00:50 +01:00
MacDue
eef148f7e7 LibGfx: Fix direction of vertical gradients
Previously the color blending and alpha blending were working from
opposite sides.
2022-07-17 20:11:38 +01:00
MacDue
03037cbe4e LibGfx: Support alpha blending on gradients 2022-07-17 20:11:38 +01:00
Andreas Kling
80ed7d220c LibGfx: Paint whitespace characters (including  ) as empty space
This fixes an issue where   would sometimes render as "A" in
web content.
2022-07-09 22:16:13 +02:00
Andreas Kling
d40167f7bb LibGfx: Always truncate glyph coordinates in Painter::draw_text_run()
This fixes an issue with glyphs jiggling 1px back and forth due to
inconsistent rounding.
2022-07-05 00:48:59 +02:00
FrHun
565f68f8bb LibGUI: Reimplement Painter::draw_triangle to be more symmetrical
This is a reimplementation of draw_triangle that manages without
floating point arithmetic.
It's most important property, compared to the previous implementation is
that rotating the same triangle 90 degrees won't drastically change the
appearance of that triangle. (it did before)
2022-06-20 12:40:51 +01:00
FrHun
b3d1fa4c54 LibGUI: Add drawing triangle from coordinate list 2022-06-20 12:40:51 +01:00
Karol Kosek
3d7838c5fb LibGfx: Implement SmoothPixels scaling mode
If you wanted to upscale an image, you had two options:
- use Nearest Neighbor: it's probably a good choice. The image stays
  sharp.. unless you aren't using integer scales.
- use Bilinear blending, but this on the other hand, doesn't handle
  upscaling well. It just blurs everything.

But what if we could take the best of both of them and make the image
sharp on integers and just blur it a little when needed?

Well, there's Smooth Pixels!

This mode is similar to the Bilinear Blend, with the main difference
is that the blend ratio is multiplied by the current scale, so the blur
on corners can be only 1px wide.

From my testing this mode doesn't handles downscaling as good as the
Bilinear blending though.
2022-06-16 14:26:55 +01:00
Karol Kosek
c409881b5f LibGfx: Pass scaling mode as an enum in do_draw_scaled_bitmap 2022-06-16 14:26:55 +01:00
MacDue
81a3ec0692 LibGfx: Add Painter::get_pixel()
This is required to do some painting tricks in LibWeb, this has to be
added to the painter so it can apply the paint translation first.
2022-06-16 10:28:07 +01:00
Hediadyoin1
527fed1391 LibGfx: Purge doubles from Painter and use more of AK::Math
The added precision of doubles is most likely not needed here and floats
are usually cheaper than doubles, so lets always stick to them.

This also simplifies some calls to sin+cos to AK:sincos and a call to
atan(1/x) to atan2(1,x) to avoid a division.
2022-06-14 23:00:52 +02:00
Karol Kosek
8a9211de4b LibGfx: Add the glyph spacing also to spaces in Painter::draw_text_run()
This caused the text selection not to match the selected glyph after
some words.
2022-05-09 20:16:59 +02:00
Hendiadyoin1
9aa4958234 LibGfx: Use round_to<int> in Painter::draw_text_line 2022-05-07 20:25:39 +02:00
Simon Danner
9ad9c72827 LibGfx: Draw complex emojis correctly
Previously draw_text_run only passed a single code point to
draw_glyph_or_emoji. This lead e.g. to broken unicode flag support.
Improve this by passing along the code_point iterator, so the emoji code
can detect the correct emojis and advance it as needed.
2022-04-11 21:09:52 +02:00
Simon Wanner
206d6ece55 LibGfx: Move other font-related files to LibGfx/Font/ 2022-04-09 23:48:18 +02:00
Jelle Raaijmakers
7334636933 LibGfx: Return kerning values as a float instead of int
This allows for a more precise rounding of glyph positions.
2022-04-01 12:58:03 +02:00
Jelle Raaijmakers
ee9a2e0715 LibGfx: Implement font kerning for Painter::draw_text_run 2022-04-01 12:58:03 +02:00
Andreas Kling
7850628ff1 LibGfx: Add Painter::draw_text_run(), a simplified text painting API
This API does:
    - Take a Utf8View
    - Take the starting point on the baseline as its input coordinate

This API does not:
    - Align the text
    - Wrap the text
    - Elide too-long text into "..."
2022-03-30 00:57:15 +02:00
Andreas Kling
5c5e4b5ae5 Revert "LibGfx: Remove bogus baseline adjustment in Painter's draw_text_line()"
This reverts commit 2b2915656d.

While this adjustment is bogus, it is currently responsible for putting
CenterLeft aligned scalable text in the right position.

This is going to take a bunch of work to get right.
2022-03-27 03:01:09 +02:00
Andreas Kling
2b2915656d LibGfx: Remove bogus baseline adjustment in Painter's draw_text_line()
This didn't achieve anything other than a slight vertical misalignment
for scalable fonts.
2022-03-27 01:14:56 +01:00
Andreas Kling
65629d26fe LibGfx: Use Font::pixel_size() instead of glyph_height() when painting
This gives us correct height metrics for both bitmap and scalable fonts.
2022-03-27 01:14:56 +01:00
MacDue
3c0e17f29f LibGfx: Support scaling in AntiAliasingPainter::draw_circle()
Previously the painter would crash if scaling was enabled.
2022-03-26 18:24:11 +00:00
Jelle Raaijmakers
b17fb76ace LibGfx: Implement TTF kerning tables
If a TTF font contains kern tables, we now read through all of them and
apply any kerning values to character rendering.
2022-03-24 16:53:21 +01:00
Jelle Raaijmakers
bec474d99b LibGfx: Calculate source rect once in do_draw_scaled_bitmap 2022-03-23 11:53:34 +01:00
Jelle Raaijmakers
f790a69431 LibGfx: Draw last row and column of scaled bitmaps in Painter
There was an off-by-one bug in `Painter::do_draw_scaled_bitmap` where
the last column and row of the source bitmap would be skipped. This was
especially visible in PixelPaint when zooming in and out on smaller
images.

Instead of the top/left of the pixel, we now use the bottom/right side
of the pixel as a threshold to stop drawing.
2022-03-23 11:53:34 +01:00
Simon Wanner
4f348f1733 LibGfx: Clip away coordinates outside the source bitmap
This prevents accessing the source pixels out of bounds.
2022-03-22 02:06:21 +01:00
Simon Wanner
917d06fa30 LibGfx: Clamp coordinates for bilinear blending correctly
Clamp these coordinates based on the src_rect's top/right/bottom/left
instead of assuming src_rect is positioned at 0,0
2022-03-21 01:48:21 +01:00
Andreas Kling
d09e8978c2 LibGfx: Be more aggressive when splitting bezier curves
Significantly reduce the tolerance when splitting bezier curves. This
gives a smoother final appearance at the cost of drawing more lines.
2022-03-19 22:04:43 +01:00
MacDue
51e54ab1ba LibGfx: AntiAliasingPainter::draw_circle/fill_rect_with_rounded_corners
Follows the efficient algorithm from this paper:
https://cs.uwaterloo.ca/research/tr/1984/CS-84-38.pdf

Can be extended ellipses in future.
2022-03-18 11:31:33 +01:00
Lenny Maiorani
9c56a83b76 Libraries: Use default constructors/destructors in LibGfx
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#cother-other-default-operation-rules

"The compiler is more likely to get the default semantics right and
you cannot implement these functions better than the compiler."
2022-03-17 17:23:49 +00:00
Joe Petrus
2632f6ae65 LibGfx: Abort draw_circle_arc_intersecting with 0 radius
In testing a particular website (https://www.icpms.com), WebContent
was crashing with infinite recursion in draw_circle_arc_intersecting.
Presumably, radius must be > 0 to paint something, so this trivial
patch simply returns if radius <= 0. The website in question no longer
crashes WebContent.
2022-03-11 10:54:06 +01:00
Vrins
3b22fd9a9f LibGfx: Add support for TextAlignment::TopCenter / BottomCenter
Now supports TextAlignment::TopCenter and TextAlignment::BottomCenter
for the Painter::draw_text.

Also patched this in Spreadsheet/CellTypeDialog.cpp
2022-03-08 22:09:52 +01:00
Andreas Kling
9fa78b1a05 LibGfx: Don't mix up red/blue channels when blitting RGBA8888 bitmap
This makes CanvasRenderingContext2D.putImageData() actually paint the
right colors into the canvas.
2022-03-06 00:15:12 +01:00
Andreas Kling
a6a8ba80fc LibGfx: Rename Color::from_rgba() => Color::from_argb()
This matches the rename of RGBA32 to ARGB32. It also makes more sense
when you see it used with 32-bit hexadecimal literals:

Before:
    Color::from_rgba(0xaarrggbb)

After:
    Color::from_argb(0xaarrggbb)
2022-03-04 23:40:21 +01:00
Andreas Kling
5ace66a903 LibGfx: Rename RGBA32 => ARGB32
The ARGB32 typedef is used for 32-bit #AARRGGBB quadruplets. As such,
the name RGBA32 was misleading, so let's call it ARGB32 instead.

Since endianness is a thing, let's not encode any assumptions about byte
order in the name of this type. ARGB32 is basically a "machine word"
of color.
2022-03-04 23:40:21 +01:00
Linus Groh
c5c026890e LibGfx: Add basic variation selector and regional indicator support :^)
I'm sure there's a spec for this somewhere, but this is a first working
approximation.

Closes #3315.
2022-02-23 21:53:30 +00:00
Linus Groh
cab032f1ee LibGfx+LibGUI: Support multi code point emojis in text painting :^)
This necessitates switching from passing a single code point to the
callback to passing a non-const Utf8CodePointIterator instead.

Note that the text selection mechanisms in LibGUI and LibWeb don't
handle this properly yet; they still assume that each code point
renders as one glyph. Similarly, width calculations for text widths
don't either, so a single such an emoji will require space for more
than one glyph.

It also doesn't work in LibVT's TerminalWidget, where each code point
is handled and rendered separately, so LibGfx never gets a chance to
check if subsequent code points could result in a combined emoji.
2022-02-23 21:53:30 +00:00
Steven Schmoll
d345a3689f LibGfx: Support color blending in Painter::draw_bitmap
This method is commonly used by bitmap text rendering. Adding support
for color blending enables support in the browser for text opacity using
their color property.
2022-02-14 12:49:19 +01:00
Tobias Christiansen
0277118cb4 LibGfx: Add Painter::draw_triangle_wave()
This patch adds support for drawing triangular waves.
For now those can only be horizontal, but as they are intended for
underlining text, it's an okay way to handle this.
2022-01-23 15:48:27 +03:30
Maciej
8542d1da5a LibGfx: Always scale emojis to fit font height 2022-01-22 00:26:23 +00:00
Filiph Sandström
c4b78bee45 LibGfx: Remove VERIFY in draw_rect_with_thickness
draw_line with thickness already supports scaling so that verify isn't
needed anymore. This fixes a scaling chrash introduced in 8a1d77f.
2022-01-01 20:30:09 +00:00
Hendiadyoin1
9e7c16d0a4 LibGfx: Load default font lazily
This is required when trying to use a Painter from lagom, due to
/res/font not being present
2021-11-29 23:17:05 +03:30
Andreas Kling
8b1108e485 Everywhere: Pass AK::StringView by value 2021-11-11 01:27:46 +01:00
Gal Horowitz
aa180c821d LibGFX: Draw the ends of lines with non-standard thickness
Lines are drawn using squares the size of the thickness, so if the
length of the line was not a multiple of the thickness, the end of the
line was not drawn correctly.
2021-10-02 20:54:47 +02:00
Sam Atkins
1c807410cd LibGfx: Add optional bilinear filtering to draw_scaled_bitmap()
The algorithm is quite simple: You grab a 2x2 area of pixels around the
point you want from the source bitmap, and then linearly interpolate
between them based on how far they are from that point.

This works well when scaling up images, and moderately well when scaling
down - small details may get skipped over. The way GPUs solve this is
with mipmaps, which is not something I want to get into right now. (And
increases the memory usage per bitmap by 50%.)

I have not focused on performance, but this does reuse much of the
existing fixed-point calculation, and uses constexpr so that the
performance for nearest-neighbor should be the same as it was
previously.
2021-09-20 22:18:20 +02:00
thankyouverycool
ae59d8a728 LibGfx: Paint replacement character, 0xFFFD, for unknown glyphs
Note: most systems now use a font's .notdef character for unknown
glyphs (commonly the tofu box) and reserve 0xFFFD for encoding
errors. Until Serenity supports tofu, 0xFFFD is a preferable, if
deprecated, alternative to '?' to reduce ambiguity.
2021-09-19 00:58:59 +02:00
Ali Mohammad Pur
433725fef2 LibGfx: Implement cubic bezier curves by splitting them to subcurves
This makes them significantly more nicer-looking, and fixes a FIXME :^)
2021-09-18 02:12:38 +04:30
Ali Mohammad Pur
02d949cfb6 LibGfx: Switch Painter.{h,cpp} to use east-const 2021-09-18 02:12:38 +04:30
Ali Mohammad Pur
e2cd558101 LibGfx: Start a very basic anti-aliased Painter implementation
This can currently draw AA lines (and by proxy, AA paths), and passes
all its output through a 2D affine transform to an underlying
Gfx::Painter.
2021-09-18 02:12:38 +04:30
Andreas Kling
32b9d80ee5 LibGfx: Make Painter::fill_path() take Path by const reference
Taking a mutable reference here made the API look very strange.
2021-09-17 13:41:03 +02:00