]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
lib/fonts: Implement glyph rotation
authorThomas Zimmermann <tzimmermann@suse.de>
Tue, 7 Apr 2026 09:23:16 +0000 (11:23 +0200)
committerHelge Deller <deller@gmx.de>
Tue, 7 Apr 2026 15:38:07 +0000 (17:38 +0200)
Move the glyph rotation helpers from fbcon to the font library. Wrap them
behind clean interfaces. Also clear the output memory to zero. Previously,
the implementation relied on the caller to do that.

Go through the fbcon code and callers of the glyph-rotation helpers. In
addition to the font rotation, there's also the cursor code, which uses
the rotation helpers.

The font-rotation relied on a single memset to zero for the whole font.
This is now multiple memsets on each glyph. This will be sorted out when
the font library also implements font rotation.

Building glyph rotation in the font library still depends on
CONFIG_FRAMEBUFFER_CONSOLE_ROTATION=y. If we get more users of the code,
we can still add a dedicated Kconfig symbol to the font library.

No changes have been made to the actual implementation of the rotate_*()
and pattern_*() functions. These will be refactored as separate changes.

v2:
- fix typos

Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de>
Signed-off-by: Helge Deller <deller@gmx.de>
drivers/video/fbdev/core/fbcon_ccw.c
drivers/video/fbdev/core/fbcon_cw.c
drivers/video/fbdev/core/fbcon_rotate.c
drivers/video/fbdev/core/fbcon_rotate.h
include/linux/font.h
lib/fonts/Makefile
lib/fonts/font_rotate.c [new file with mode: 0644]

index 96ef449ee6ac8b929f79ddc1312991736417d1ca..72453a2aaca87352f80c2c0e06047b960d551556 100644 (file)
@@ -12,6 +12,7 @@
 #include <linux/slab.h>
 #include <linux/string.h>
 #include <linux/fb.h>
+#include <linux/font.h>
 #include <linux/vt_kern.h>
 #include <linux/console.h>
 #include <asm/types.h>
@@ -344,8 +345,7 @@ static void ccw_cursor(struct vc_data *vc, struct fb_info *info, bool enable,
                size = cur_height * width;
                while (size--)
                        tmp[i++] = 0xff;
-               memset(mask, 0, w * vc->vc_font.width);
-               rotate_ccw(tmp, mask, vc->vc_font.width, vc->vc_font.height);
+               font_glyph_rotate_270(tmp, vc->vc_font.width, vc->vc_font.height, mask);
                kfree(tmp);
        }
 
index ea712654edae31952307e2efc442014c62a7122d..5690fc1d78547180a548f426c837cb616c930929 100644 (file)
@@ -12,6 +12,7 @@
 #include <linux/slab.h>
 #include <linux/string.h>
 #include <linux/fb.h>
+#include <linux/font.h>
 #include <linux/vt_kern.h>
 #include <linux/console.h>
 #include <asm/types.h>
@@ -327,8 +328,7 @@ static void cw_cursor(struct vc_data *vc, struct fb_info *info, bool enable,
                size = cur_height * width;
                while (size--)
                        tmp[i++] = 0xff;
-               memset(mask, 0, w * vc->vc_font.width);
-               rotate_cw(tmp, mask, vc->vc_font.width, vc->vc_font.height);
+               font_glyph_rotate_90(tmp, vc->vc_font.width, vc->vc_font.height, mask);
                kfree(tmp);
        }
 
index 18575c5182db33078131366c5ab6e557df9ca2ba..588dc9d6758ab4e22949ff6996767fa9d4d8cc08 100644 (file)
@@ -12,6 +12,7 @@
 #include <linux/slab.h>
 #include <linux/string.h>
 #include <linux/fb.h>
+#include <linux/font.h>
 #include <linux/vt_kern.h>
 #include <linux/console.h>
 #include <asm/types.h>
@@ -60,30 +61,25 @@ int fbcon_rotate_font(struct fb_info *info, struct vc_data *vc)
        }
 
        dst = par->fontbuffer;
-       memset(dst, 0, par->fd_size);
 
        switch (par->rotate) {
        case FB_ROTATE_UD:
                for (i = len; i--; ) {
-                       rotate_ud(src, dst, vc->vc_font.width,
-                                 vc->vc_font.height);
-
+                       font_glyph_rotate_180(src, vc->vc_font.width, vc->vc_font.height, dst);
                        src += s_cellsize;
                        dst += d_cellsize;
                }
                break;
        case FB_ROTATE_CW:
                for (i = len; i--; ) {
-                       rotate_cw(src, dst, vc->vc_font.width,
-                                 vc->vc_font.height);
+                       font_glyph_rotate_90(src, vc->vc_font.width, vc->vc_font.height, dst);
                        src += s_cellsize;
                        dst += d_cellsize;
                }
                break;
        case FB_ROTATE_CCW:
                for (i = len; i--; ) {
-                       rotate_ccw(src, dst, vc->vc_font.width,
-                                  vc->vc_font.height);
+                       font_glyph_rotate_270(src, vc->vc_font.width, vc->vc_font.height, dst);
                        src += s_cellsize;
                        dst += d_cellsize;
                }
index 8cb019e8a9c0f9317a3bfc856fcb7ea6441e5bad..725bcae2df619676d79a7ed02cb5f8f713c3dddb 100644 (file)
         (fb_scrollmode(s) == SCROLL_REDRAW || fb_scrollmode(s) == SCROLL_MOVE || !(i)->fix.xpanstep) ? \
         (i)->var.xres : (i)->var.xres_virtual; })
 
-
-static inline int pattern_test_bit(u32 x, u32 y, u32 pitch, const char *pat)
-{
-       u32 tmp = (y * pitch) + x, index = tmp / 8,  bit = tmp % 8;
-
-       pat +=index;
-       return (*pat) & (0x80 >> bit);
-}
-
-static inline void pattern_set_bit(u32 x, u32 y, u32 pitch, char *pat)
-{
-       u32 tmp = (y * pitch) + x, index = tmp / 8, bit = tmp % 8;
-
-       pat += index;
-
-       (*pat) |= 0x80 >> bit;
-}
-
-static inline void rotate_ud(const char *in, char *out, u32 width, u32 height)
-{
-       int i, j;
-       int shift = (8 - (width % 8)) & 7;
-
-       width = (width + 7) & ~7;
-
-       for (i = 0; i < height; i++) {
-               for (j = 0; j < width - shift; j++) {
-                       if (pattern_test_bit(j, i, width, in))
-                               pattern_set_bit(width - (1 + j + shift),
-                                               height - (1 + i),
-                                               width, out);
-               }
-
-       }
-}
-
-static inline void rotate_cw(const char *in, char *out, u32 width, u32 height)
-{
-       int i, j, h = height, w = width;
-       int shift = (8 - (height % 8)) & 7;
-
-       width = (width + 7) & ~7;
-       height = (height + 7) & ~7;
-
-       for (i = 0; i < h; i++) {
-               for (j = 0; j < w; j++) {
-                       if (pattern_test_bit(j, i, width, in))
-                               pattern_set_bit(height - 1 - i - shift, j,
-                                               height, out);
-
-               }
-       }
-}
-
-static inline void rotate_ccw(const char *in, char *out, u32 width, u32 height)
-{
-       int i, j, h = height, w = width;
-       int shift = (8 - (width % 8)) & 7;
-
-       width = (width + 7) & ~7;
-       height = (height + 7) & ~7;
-
-       for (i = 0; i < h; i++) {
-               for (j = 0; j < w; j++) {
-                       if (pattern_test_bit(j, i, width, in))
-                               pattern_set_bit(i, width - 1 - j - shift,
-                                               height, out);
-               }
-       }
-}
-
 int fbcon_rotate_font(struct fb_info *info, struct vc_data *vc);
 
 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_ROTATION)
index 3bd49d914b22f0bc83cc5656a74a64ad56ae053a..0a240dd7042257ca970764713761b2d49fe28744 100644 (file)
@@ -104,6 +104,14 @@ unsigned int font_data_size(font_data_t *fd);
 bool font_data_is_equal(font_data_t *lhs, font_data_t *rhs);
 int font_data_export(font_data_t *fd, struct console_font *font, unsigned int vpitch);
 
+/* font_rotate.c */
+void font_glyph_rotate_90(const unsigned char *glyph, unsigned int width, unsigned int height,
+                         unsigned char *out);
+void font_glyph_rotate_180(const unsigned char *glyph, unsigned int width, unsigned int height,
+                          unsigned char *out);
+void font_glyph_rotate_270(const unsigned char *glyph, unsigned int width, unsigned int height,
+                          unsigned char *out);
+
 /*
  * Font description
  */
index b176af53d53eb1feb013da1beab0d05459d115c9..7202a70a56ef083f51166f5b2032b58421e3b0a9 100644 (file)
@@ -2,6 +2,7 @@
 # Font handling
 
 font-y := fonts.o
+font-$(CONFIG_FRAMEBUFFER_CONSOLE_ROTATION) += font_rotate.o
 
 # Built-in fonts; sorted by Family-Size in ascending order
 font-$(CONFIG_FONT_6x8)       += font_6x8.o
diff --git a/lib/fonts/font_rotate.c b/lib/fonts/font_rotate.c
new file mode 100644 (file)
index 0000000..d107a8d
--- /dev/null
@@ -0,0 +1,150 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Font rotation
+ *
+ *    Copyright (C) 2005 Antonino Daplas <adaplas @pol.net>
+ *
+ * This file is subject to the terms and conditions of the GNU General Public
+ * License.  See the file COPYING in the main directory of this archive for
+ * more details.
+ */
+
+#include <linux/export.h>
+#include <linux/math.h>
+#include <linux/string.h>
+
+#include "font.h"
+
+static inline int pattern_test_bit(u32 x, u32 y, u32 pitch, const char *pat)
+{
+       u32 tmp = (y * pitch) + x, index = tmp / 8,  bit = tmp % 8;
+
+       pat += index;
+       return (*pat) & (0x80 >> bit);
+}
+
+static inline void pattern_set_bit(u32 x, u32 y, u32 pitch, char *pat)
+{
+       u32 tmp = (y * pitch) + x, index = tmp / 8, bit = tmp % 8;
+
+       pat += index;
+
+       (*pat) |= 0x80 >> bit;
+}
+
+static inline void rotate_cw(const char *in, char *out, u32 width, u32 height)
+{
+       int i, j, h = height, w = width;
+       int shift = (8 - (height % 8)) & 7;
+
+       width = (width + 7) & ~7;
+       height = (height + 7) & ~7;
+
+       for (i = 0; i < h; i++) {
+               for (j = 0; j < w; j++) {
+                       if (pattern_test_bit(j, i, width, in))
+                               pattern_set_bit(height - 1 - i - shift, j,
+                                               height, out);
+               }
+       }
+}
+
+/**
+ * font_glyph_rotate_90 - Rotate a glyph pattern by 90° in clockwise direction
+ * @glyph: The glyph to rotate
+ * @width: The glyph width in bits per scanline
+ * @height: The number of scanlines in the glyph
+ * @out: The rotated glyph bitmap
+ *
+ * The parameters @width and @height refer to the input glyph given in @glyph.
+ * The caller has to provide the output buffer @out of sufficient size to hold
+ * the rotated glyph. Rotating by 90° flips the width and height for the output
+ * glyph. Depending on the glyph pitch, the size of the output glyph can be
+ * different than the size of the input. Callers have to take this into account
+ * when allocating the output memory.
+ */
+void font_glyph_rotate_90(const unsigned char *glyph, unsigned int width, unsigned int height,
+                         unsigned char *out)
+{
+       memset(out, 0, font_glyph_size(height, width)); /* flip width/height */
+
+       rotate_cw(glyph, out, width, height);
+}
+EXPORT_SYMBOL_GPL(font_glyph_rotate_90);
+
+static inline void rotate_ud(const char *in, char *out, u32 width, u32 height)
+{
+       int i, j;
+       int shift = (8 - (width % 8)) & 7;
+
+       width = (width + 7) & ~7;
+
+       for (i = 0; i < height; i++) {
+               for (j = 0; j < width - shift; j++) {
+                       if (pattern_test_bit(j, i, width, in))
+                               pattern_set_bit(width - (1 + j + shift),
+                                               height - (1 + i),
+                                               width, out);
+               }
+       }
+}
+
+/**
+ * font_glyph_rotate_180 - Rotate a glyph pattern by 180°
+ * @glyph: The glyph to rotate
+ * @width: The glyph width in bits per scanline
+ * @height: The number of scanlines in the glyph
+ * @out: The rotated glyph bitmap
+ *
+ * The parameters @width and @height refer to the input glyph given in @glyph.
+ * The caller has to provide the output buffer @out of sufficient size to hold
+ * the rotated glyph.
+ */
+void font_glyph_rotate_180(const unsigned char *glyph, unsigned int width, unsigned int height,
+                          unsigned char *out)
+{
+       memset(out, 0, font_glyph_size(width, height));
+
+       rotate_ud(glyph, out, width, height);
+}
+EXPORT_SYMBOL_GPL(font_glyph_rotate_180);
+
+static inline void rotate_ccw(const char *in, char *out, u32 width, u32 height)
+{
+       int i, j, h = height, w = width;
+       int shift = (8 - (width % 8)) & 7;
+
+       width = (width + 7) & ~7;
+       height = (height + 7) & ~7;
+
+       for (i = 0; i < h; i++) {
+               for (j = 0; j < w; j++) {
+                       if (pattern_test_bit(j, i, width, in))
+                               pattern_set_bit(i, width - 1 - j - shift,
+                                               height, out);
+               }
+       }
+}
+
+/**
+ * font_glyph_rotate_270 - Rotate a glyph pattern by 270° in clockwise direction
+ * @glyph: The glyph to rotate
+ * @width: The glyph width in bits per scanline
+ * @height: The number of scanlines in the glyph
+ * @out: The rotated glyph bitmap
+ *
+ * The parameters @width and @height refer to the input glyph given in @glyph.
+ * The caller has to provide the output buffer @out of sufficient size to hold
+ * the rotated glyph. Rotating by 270° flips the width and height for the output
+ * glyph. Depending on the glyph pitch, the size of the output glyph can be
+ * different than the size of the input. Callers have to take this into account
+ * when allocating the output memory.
+ */
+void font_glyph_rotate_270(const unsigned char *glyph, unsigned int width, unsigned int height,
+                          unsigned char *out)
+{
+       memset(out, 0, font_glyph_size(height, width)); /* flip width/height */
+
+       rotate_ccw(glyph, out, width, height);
+}
+EXPORT_SYMBOL_GPL(font_glyph_rotate_270);