Commit graph

213 commits

Author SHA1 Message Date
Andreas Kling
3407ab0fd1 LibGfx: Make Font::width() return a float 2023-01-03 15:25:02 +01:00
Andreas Kling
bfa7381852 LibGfx: Make Font::pixel_size() return a float
Here's yet another place where we were chopping of decimals.
2023-01-03 15:25:02 +01:00
MacDue
ca01017f32 LibGfx: Add Painter::fill_pixels()
This function fills a region of pixels with the result of a callback
function. This is an alternative to a for loop that repeatedly calls
Painter::set_pixel(), which can get very expensive due to the clipping
checks set_pixel() does each call.
2022-12-25 15:35:31 +01:00
FrHun
df30440117 LibGfx: Add NearestFractional scaling type to painter
This is useful for cases where you want to avoid scaling artifacts.
2022-12-23 12:16:46 +00:00
MacDue
6c27f2c071 LibGfx: Don't blend opaque pixels 2022-12-20 11:03:18 +01:00
Sam Atkins
83f31cb4a7 LibGfx: Add int overloads for (AntiAliasing)Painter float methods
Without this change, the upcoming LibWeb pixel types will require a
silly doubled conversion in some places.

eg: `some_rect.to_type<int>().to_type<float>()`

With these overloads, we can get away with `some_rect.to_type<int>()`.
2022-12-08 12:46:03 +00:00
MacDue
27fae78335 Meta+Userland: Pass Gfx::IntSize by value
Just two ints like Gfx::IntPoint.
2022-12-07 11:48:27 +01:00
MacDue
e011eafd37 Meta+Userland: Pass Gfx::FloatPoint by value
Just a small 8-byte value like Gfx::IntPoint.
2022-12-07 11:48:27 +01:00
MacDue
7be0b27dd3 Meta+Userland: Pass Gfx::IntPoint by value
This is just two ints or 8 bytes or the size of the reference on
x86_64 or AArch64.
2022-12-07 11:48:27 +01:00
MacDue
bbc149ebb9 Meta+Userland: Pass Gfx::Color by value
Gfx::Color is always 4 bytes (it's just a wrapper over u32) it's less
work just to pass the color directly.

This also updates IPCCompiler to prevent from generating
Gfx::Color const &, which makes replacement easier.
2022-12-07 11:48:27 +01:00
Linus Groh
57dc179b1f Everywhere: Rename to_{string => deprecated_string}() where applicable
This will make it easier to support both string types at the same time
while we convert code, and tracking down remaining uses.

One big exception is Value::to_string() in LibJS, where the name is
dictated by the ToString AO.
2022-12-06 08:54:33 +01:00
Linus Groh
6e19ab2bbc AK+Everywhere: Rename String to DeprecatedString
We have a new, improved string type coming up in AK (OOM aware, no null
state), and while it's going to use UTF-8, the name UTF8String is a
mouthful - so let's free up the String name by renaming the existing
class.
Making the old one have an annoying name will hopefully also help with
quick adoption :^)
2022-12-06 08:54:33 +01:00
lanmonster
2b7aa4a971 LibGfx+LibGUI: Use constant for line spacing instead of magic number 2022-11-30 07:57:21 +01:00
MacDue
0e65de2e11 LibGfx: Don't write blended pixel if the alpha is zero 2022-11-29 11:08:50 +01:00
Lucas CHOLLET
4219d50a21 LibGfx: Use the Midpoint Ellipse Algorithm
It is only used to draw non-antialiased and non-filled ellipses.
2022-11-26 00:49:05 +01:00
jack gleeson
4bf587811f PixelPaint+LibGfx: Allow resizing images and layers
This PR adds resize ability to PixelPaint as per issue 11862.
The previous behaviour was to always rescale the canvas when
resizing an image. This adds a checkbox to toggle between
rescaling, and resizing which blits the existing canvas to
the top left of the new, resized canvas.

As part of this, a new ScalingMode is added to
LibGfx - None.
2022-11-02 10:59:18 +00:00
Nico Weber
2af028132a AK+Everywhere: Add AK_COMPILER_{GCC,CLANG} and use them most places
Doesn't use them in libc headers so that those don't have to pull in
AK/Platform.h.

AK_COMPILER_GCC is set _only_ for gcc, not for clang too. (__GNUC__ is
defined in clang builds as well.) Using AK_COMPILER_GCC simplifies
things some.

AK_COMPILER_CLANG isn't as much of a win, other than that it's
consistent with AK_COMPILER_GCC.
2022-10-04 23:35:07 +01:00
Andreas Kling
b52165c5d7 LibWeb+LibGfx: Move the blit image through 2D transfrom to Gfx::Painter
Even though this code is obnoxiously slow, it still belongs in LibGfx
and should not be hidden away in LibWeb's CanvasRenderingContext2D.
2022-09-24 13:00:53 +02:00
MacDue
60356c8dde LibGfx: Support getting a bitmap for a region of painter
This will be needed so we can apply filter effects to the backdrop
of an element in LibWeb.

This now also allows getting a crop of a bitmap in a different format
than the source bitmap. This is for if the painter's bitmap does not
have an alpha channel, but you want to ensure the cropped bitmap does.
2022-09-16 10:50:48 +01:00
Jelle Raaijmakers
bfb4e08612 LibGfx: Use memcpy instead of fast_u32_copy for blitting
In some artificial full screen blitting profiling, I've seen `memcpy`
take up about 4% fewer samples each time I measure. It seems like
`fast_u32_copy` is not as fast as it'd like to believe.
2022-09-14 17:17:19 +02:00
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 &nbsp;) as empty space
This fixes an issue where &nbsp; 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