From patchwork Thu Oct 28 05:19:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heinrich Schuchardt X-Patchwork-Id: 1547328 X-Patchwork-Delegate: agust@denx.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=gmx.net header.i=@gmx.net header.a=rsa-sha256 header.s=badeba3b8450 header.b=h+9+nWjk; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4Hfv4b001Zz9sS8 for ; Thu, 28 Oct 2021 16:19:26 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 591CF82FE7; Thu, 28 Oct 2021 07:19:23 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=gmx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; secure) header.d=gmx.net header.i=@gmx.net header.b="h+9+nWjk"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 5592E8314A; Thu, 28 Oct 2021 07:19:21 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,FREEMAIL_FROM,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.2 Received: from mout.gmx.net (mout.gmx.net [212.227.17.21]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 1B94680F1A for ; Thu, 28 Oct 2021 07:19:17 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=xypron.glpk@gmx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1635398356; bh=fQvHnYPIY53VfBU+tjtO9G7FPJ0UWMZJMzddFBRLiGI=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date; b=h+9+nWjkCB3ZIkeDzb9Q0ymzOk/9MLal4/IZ1SpCfGu/LSj0IqBnHDeFVbKodLYXP CEFffXUfNPB4hW7bL2AmssL1Eo9aQ+VJR/em2f11At1sH/wszdB4b3OghzK8MUvf0l ArEFKMm64Q3C004zLUmhq46F+dVstMkR/kVrLf9Y= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from LT02.fritz.box ([88.152.144.157]) by mail.gmx.net (mrgmx104 [212.227.17.174]) with ESMTPSA (Nemesis) id 1MKsnF-1mNcef0GGj-00LEYb; Thu, 28 Oct 2021 07:19:16 +0200 From: Heinrich Schuchardt To: Anatolij Gustschin Cc: Simon Glass , u-boot@lists.denx.de, Heinrich Schuchardt Subject: [PATCH v2 1/1] video: fix positioning in TrueType console Date: Thu, 28 Oct 2021 07:19:07 +0200 Message-Id: <20211028051907.86539-1-xypron.glpk@gmx.de> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Provags-ID: V03:K1:kCkiHL+6X93W0F56aErb0K+4WVxtgPeta7AAhJiyeQTfmyp4196 GZ6Ij5Iohw8ThZDjI2VuAhRURt6SfQquKgDOudGqNG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nzYYzgNFglNHyvL+5fY42Mj/Qf+YmNJkxYRYaY/NVpwFfTSAmUbcl DFRww777lC0MGI5JtPUayIlGLMccHzxwT9+4ZJN9UOWiU4+RFRkxaw/0kVo1OP3jeSBlAEc86 bAkQ7UtiMhq+xzxwbQK5sLkJcarP9iy/LjnEk+X2lVHwLrh+uCMfhr/pCUatYWLxOfKnty2JD 5YuHvu2JJjHDtk= X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean With the patch accurate positioning is possible for mono-typed fonts: Fix the return value of console_truetype_putc_xy(). The current position is passed as parameter x. Some part of x represents a fractional pixel. The return value represents how much the character position must be advanced. This should only comprise the width of the current character and not the preexisting fractional pixel position. Characters are not square. As all characters of a mono-type font we can take the width of any character. 'W' as one of the widest ANSI characters provides also a good value for variable width fonts. The character width must be a float. Signed-off-by: Heinrich Schuchardt --- v2: Adjust hash values in tests --- drivers/video/console_truetype.c | 27 ++++++++++++++++++++------- include/video_console.h | 2 +- test/dm/video.c | 6 +++--- 3 files changed, 24 insertions(+), 11 deletions(-) -- 2.30.2 diff --git a/drivers/video/console_truetype.c b/drivers/video/console_truetype.c index d9ad52cce0..6fe238eab3 100644 --- a/drivers/video/console_truetype.c +++ b/drivers/video/console_truetype.c @@ -209,7 +209,7 @@ static int console_truetype_putc_xy(struct udevice *dev, uint x, uint y, int width_frac, linenum; struct pos_info *pos; u8 *bits, *data; - int advance; + int advance, kern_adv; void *start, *line, *sync_start, *sync_end; int row, ret; int bg_r, bg_g, bg_b; @@ -224,8 +224,11 @@ static int console_truetype_putc_xy(struct udevice *dev, uint x, uint y, * this character */ xpos = frac(VID_TO_PIXEL((double)x)); if (vc_priv->last_ch) { - xpos += priv->scale * stbtt_GetCodepointKernAdvance(font, - vc_priv->last_ch, ch); + kern_adv = stbtt_GetCodepointKernAdvance(font, vc_priv->last_ch, + ch); + xpos += priv->scale * kern_adv; + } else { + kern_adv = 0; } /* @@ -236,8 +239,8 @@ static int console_truetype_putc_xy(struct udevice *dev, uint x, uint y, */ x_shift = xpos - (double)tt_floor(xpos); xpos += advance * priv->scale; - width_frac = (int)VID_TO_POS(xpos); - if (x + width_frac >= vc_priv->xsize_frac) + width_frac = VID_TO_POS(priv->scale * (kern_adv + advance)); + if (x + (int)VID_TO_POS(xpos) >= vc_priv->xsize_frac) return -EAGAIN; /* Write the current cursor position into history */ @@ -585,20 +588,21 @@ static int console_truetype_probe(struct udevice *dev) struct udevice *vid_dev = dev->parent; struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev); stbtt_fontinfo *font = &priv->font; - int ascent; + int advance, ascent, lsb; debug("%s: start\n", __func__); + if (vid_priv->font_size) priv->font_size = vid_priv->font_size; else priv->font_size = CONFIG_CONSOLE_TRUETYPE_SIZE; + priv->font_data = console_truetype_find_font(); if (!priv->font_data) { debug("%s: Could not find any fonts\n", __func__); return -EBFONT; } - vc_priv->x_charsize = priv->font_size; vc_priv->y_charsize = priv->font_size; vc_priv->xstart_frac = VID_TO_POS(2); vc_priv->cols = vid_priv->xsize / priv->font_size; @@ -612,6 +616,15 @@ static int console_truetype_probe(struct udevice *dev) /* Pre-calculate some things we will need regularly */ priv->scale = stbtt_ScaleForPixelHeight(font, priv->font_size); + + /* Assuming that 'W' is the widest character */ + stbtt_GetCodepointHMetrics(font, 'W', &advance, &lsb); + advance += stbtt_GetCodepointKernAdvance(font, 'W', 'W'); + vc_priv->cols = + (int)VID_TO_POS(vid_priv->xsize - 2) / + (int)VID_TO_POS(advance * priv->scale); + vc_priv->x_charsize = advance * priv->scale; + stbtt_GetFontVMetrics(font, &ascent, 0, 0); priv->baseline = (int)(ascent * priv->scale); debug("%s: ready\n", __func__); diff --git a/include/video_console.h b/include/video_console.h index 06b798ef10..6ec9b4c5b0 100644 --- a/include/video_console.h +++ b/include/video_console.h @@ -68,7 +68,7 @@ struct vidconsole_priv { int ycur; int rows; int cols; - int x_charsize; + double x_charsize; int y_charsize; int tab_width_frac; int xsize_frac; diff --git a/test/dm/video.c b/test/dm/video.c index 1d29b2d61c..c0ad83521a 100644 --- a/test/dm/video.c +++ b/test/dm/video.c @@ -344,7 +344,7 @@ static int dm_test_video_truetype(struct unit_test_state *uts) ut_assertok(uclass_get_device(UCLASS_VIDEO, 0, &dev)); ut_assertok(uclass_get_device(UCLASS_VIDEO_CONSOLE, 0, &con)); vidconsole_put_string(con, test_string); - ut_asserteq(13001, compress_frame_buffer(uts, dev)); + ut_asserteq(12752, compress_frame_buffer(uts, dev)); return 0; } @@ -365,7 +365,7 @@ static int dm_test_video_truetype_scroll(struct unit_test_state *uts) ut_assertok(uclass_get_device(UCLASS_VIDEO, 0, &dev)); ut_assertok(uclass_get_device(UCLASS_VIDEO_CONSOLE, 0, &con)); vidconsole_put_string(con, test_string); - ut_asserteq(36952, compress_frame_buffer(uts, dev)); + ut_asserteq(36493, compress_frame_buffer(uts, dev)); return 0; } @@ -386,7 +386,7 @@ static int dm_test_video_truetype_bs(struct unit_test_state *uts) ut_assertok(uclass_get_device(UCLASS_VIDEO, 0, &dev)); ut_assertok(uclass_get_device(UCLASS_VIDEO_CONSOLE, 0, &con)); vidconsole_put_string(con, test_string); - ut_asserteq(30747, compress_frame_buffer(uts, dev)); + ut_asserteq(31117, compress_frame_buffer(uts, dev)); return 0; }