Skip to content

Commit 004b1ad

Browse files
committed
Update the online book to Skia 138
1 parent d86b4fa commit 004b1ad

5 files changed

Lines changed: 55 additions & 62 deletions

File tree

book/animations.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,12 @@ class Browser:
250250
sdl2.SDL_WINDOWPOS_CENTERED,
251251
WIDTH, HEIGHT,
252252
sdl2.SDL_WINDOW_SHOWN | sdl2.SDL_WINDOW_OPENGL)
253+
254+
sdl2.SDL_GL_SetAttribute(sdl2.SDL_GL_CONTEXT_MAJOR_VERSION, 3)
255+
sdl2.SDL_GL_SetAttribute(sdl2.SDL_GL_CONTEXT_MINOR_VERSION, 2)
256+
sdl2.SDL_GL_SetAttribute(sdl2.SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG, True)
257+
sdl2.SDL_GL_SetAttribute(sdl2.SDL_GL_CONTEXT_PROFILE_MASK,
258+
sdl2.SDL_GL_CONTEXT_PROFILE_CORE)
253259
self.gl_context = sdl2.SDL_GL_CreateContext(
254260
self.sdl_window)
255261
print(("OpenGL initialized: vendor={}," + \

book/embeds.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -244,29 +244,32 @@ higher-quality "bilinear" or even "[Lanczos][lanczos]" algorithms.[^resizing]
244244
To give web page authors control over this performance bottleneck,
245245
there's an [`image-rendering`][image-rendering] CSS property that
246246
indicates which algorithm to use. Let's add that as an argument to
247-
`DrawImage`:
247+
`DrawImage`:[^skia-87]
248+
249+
[^skia-87]: The 1^st^ edition of *Web Browser Engineering* used an
250+
older API for image rendering quality, described in the
251+
[porting notes](https://browser.engineering/porting.html).
248252

249253
[image-rendering]: https://developer.mozilla.org/en-US/docs/Web/CSS/image-rendering
250254

251255
``` {.python}
252256
def parse_image_rendering(quality):
253257
if quality == "high-quality":
254-
return skia.FilterQuality.kHigh_FilterQuality
258+
return skia.SamplingOptions(skia.CubicResampler.Mitchell())
255259
elif quality == "crisp-edges":
256-
return skia.FilterQuality.kLow_FilterQuality
260+
return skia.SamplingOptions(
261+
skia.FilterMode.kNearest, skia.MipmapMode.kNone)
257262
else:
258-
return skia.FilterQuality.kMedium_FilterQuality
263+
return skia.SamplingOptions(
264+
skia.FilterMode.kLinear, skia.MipmapMode.kLinear)
259265
260266
class DrawImage(PaintCommand):
261267
def __init__(self, image, rect, quality):
262268
# ...
263269
self.quality = parse_image_rendering(quality)
264270
265271
def execute(self, canvas):
266-
paint = skia.Paint(
267-
FilterQuality=self.quality,
268-
)
269-
canvas.drawImageRect(self.image, self.rect, paint)
272+
canvas.drawImageRect(self.image, self.rect, self.quality)
270273
```
271274

272275
But to talk about where this argument comes from, or more generally to

book/porting.md

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,48 @@ title: Porting *WBE* to Recent Software Releases
33
...
44

55
The code in this book was developed for and tested on particular
6-
versions of the libraries it depends on, including Python 3.12, Skia
7-
87, Tk 8.6.14, DukPy 0.3.0, and PySDL2 0.9.15. This page documents
8-
code changes necessary to port the code of *Web Browser Engineering*
9-
to the most recent release of each library. It will be regularly
10-
updated as new versions are released and tested.
11-
12-
Porting to Skia 124
13-
===================
14-
15-
The text of this book uses Skia 87, and the associated skia-python
16-
87.6 release. Skia 124, and the associated skia-python 124b7 release,
17-
changes a couple of APIs used in this book. One major change is the
18-
removal of `FilterQuality`, used in [Chapter 15](embeds.md), and its
19-
replacement by `SamplingOptions`. This requires updating
20-
`parse_image_rendering` like so:
6+
library versions, including Python 3.14, Skia 138, Tk 8.6.14, DukPy
7+
0.3.0, and PySDL2 0.9.15. Earlier editions of the book, however,
8+
relied on earlier versions, and future editions might rely on later
9+
ones. This page documents code changes necessary to port the code of
10+
*Web Browser Engineering* to other relevant releases of each library.
11+
It will be regularly updated as new versions are released and tested.
12+
13+
Porting to Skia 87
14+
==================
15+
16+
The text of this online book uses Skia 138, but the printed 1^st^
17+
edition used the earlier version 87. This earlier version was missing
18+
the `SamplingOptions` API used in [Chapter 15](embeds.md) of this
19+
book. Skia 87 instead provides the older `FilterQuality` API.
20+
21+
Readers of the 1^st^ edition thus saw a different implementation of
22+
`parse_image_rendering`:
2123

2224
``` {.python}
2325
def parse_image_rendering(quality):
2426
if quality == "high-quality":
25-
return skia.SamplingOptions(skia.CubicResampler.Mitchell())
27+
return skia.FilterQuality.kHigh_FilterQuality
2628
elif quality == "crisp-edges":
27-
return skia.SamplingOptions(
28-
skia.FilterMode.kNearest, skia.MipmapMode.kNone)
29+
return skia.FilterQuality.kLow_FilterQuality
2930
else:
30-
return skia.SamplingOptions(
31-
skia.FilterMode.kLinear, skia.MipmapMode.kLinear)
31+
return skia.FilterQuality.kMedium_FilterQuality
3232
```
3333

3434
And changing the `execute` method of `DrawImage` like so:
3535

3636
``` {.python}
3737
class DrawImage:
3838
def execute(self, canvas):
39-
canvas.drawImageRect(self.image, self.rect, self.quality)
39+
paint = skia.Paint(
40+
FilterQuality=self.quality,
41+
)
42+
canvas.drawImageRect(self.image, self.rect, paint)
4043
```
4144

42-
The new API is somewhat cleaner and more customizable.
45+
The `SDL_GL_SetAttribute` method calls in the `Browser` constructor
46+
were also not necessary on this older version.
4347

44-
Finally, add these `SDL_GL_SetAttribute` calls to set GL version Skia
45-
expects:
46-
47-
``` {.python}
48-
class Browser:
49-
def __init__(self):
50-
self.sdl_window = sdl2.SDL_CreateWindow(b'Browser',
51-
sdl2.SDL_WINDOWPOS_CENTERED,
52-
sdl2.SDL_WINDOWPOS_CENTERED,
53-
WIDTH, HEIGHT,
54-
sdl2.SDL_WINDOW_SHOWN | sdl2.SDL_WINDOW_OPENGL)
55-
56-
sdl2.SDL_GL_SetAttribute(sdl2.SDL_GL_CONTEXT_MAJOR_VERSION, 3)
57-
sdl2.SDL_GL_SetAttribute(sdl2.SDL_GL_CONTEXT_MINOR_VERSION, 2)
58-
sdl2.SDL_GL_SetAttribute(sdl2.SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG, True)
59-
sdl2.SDL_GL_SetAttribute(sdl2.SDL_GL_CONTEXT_PROFILE_MASK,
60-
sdl2.SDL_GL_CONTEXT_PROFILE_CORE)
61-
```
62-
48+
We recommend new readers use a recent Skia version, as described in
49+
the main text, but the old code is documented here to preserve the
50+
code of the 1^st^ edition.

book/visual-effects.md

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ Tkinter uses, dates from the early 1990s, before high-performance
3737
graphics cards and GPUs became widespread.
3838

3939
::: {.installation}
40-
Start by installing [Skia][skia-python] and [SDL][sdl-python]:
40+
Start by installing [Skia][skia-python] and [SDL][sdl-python]:[^book-old]
4141

4242
``` {.sh}
43-
python3 -m pip install 'skia-python==87.*' pysdl2 pysdl2-dll
43+
python3 -m pip install skia-python pysdl2 pysdl2-dll
4444
```
4545

4646
As elsewhere in this book, you may need to install the `pip` package
@@ -50,18 +50,14 @@ Also, you may not be able to install `pysdl2-dll`; if so, you'll need
5050
to find SDL in your system package manager instead. Consult the
5151
[`skia-python`][skia-python] and [`pysdl2`][sdl-python] web pages for
5252
more details.
53-
54-
Note that I'm explicitly installing Skia version 87. Skia makes
55-
regular releases that change APIs or break compatibility; version 87
56-
is fairly old and should work reliably on most systems. In your own
57-
projects, or before filing bug reports in Skia, please do use more
58-
recent Skia releases. It's also possible that future Python version no
59-
longer support Skia 87; our
60-
[porting notes](https://browser.engineering/porting.html) explain how
61-
to use recent Skia releases for the code in this book.
62-
6353
:::
6454

55+
[^book-old]: The 1^st^ edition of *Web Browser Engineering* used an
56+
older Skia version, and if you're reading this website together
57+
with a printed 1^st^ edition, make sure to check the
58+
[porting notes](https://browser.engineering/porting.html)
59+
to understand the differences between the two.
60+
6561
[skia-python]: https://kyamagu.github.io/skia-python/
6662
[sdl-python]: https://pypi.org/project/PySDL2/
6763

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
dukpy==0.3.0
33

44
# Skia is used in Chapters 11+ for advanced graphics
5-
skia-python==87
5+
skia-python==138
66
pybind11==2.10.4
77

88
# SDL2 is used in Chapters 11+ for a Skia-compatible GUI

0 commit comments

Comments
 (0)