Changeset 2cc1ec0 in mainline for uspace/lib/draw/font/embedded.c


Ignore:
Timestamp:
2014-08-27T21:23:01Z (11 years ago)
Author:
Martin Sucha <sucha14@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
a9763c6
Parents:
613d644
Message:

Refactor drawing of fonts into multiple layers.

This will need further work to split glyph resolution
process to separate functions in order to support ligatures,
addition of kerning support, separate text layout functions,
etc.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/draw/font/embedded.c

    r613d644 r2cc1ec0  
    11/*
    22 * Copyright (c) 2012 Petr Koupy
     3 * Copyright (c) 2014 Martin Sucha
    34 * All rights reserved.
    45 *
     
    3435 */
    3536
    36 #include <assert.h>
    3737#include <sys/types.h>
    3838#include <malloc.h>
     39#include <errno.h>
    3940
    4041#include "../gfx/font-8x16.h"
    4142#include "embedded.h"
    4243#include "../drawctx.h"
     44#include "bitmap_backend.h"
    4345
    44 static void fde_init(char *path, uint16_t *glyph_count, void **data)
     46static int fde_resolve_glyph(void *unused, const wchar_t chr,
     47    glyph_id_t *glyph_id)
    4548{
    46         assert(glyph_count);
    47         assert(data);
    48 
    49         (*glyph_count) = FONT_GLYPHS;
    50         (*data) = NULL;
     49        bool found = false;
     50        uint16_t glyph = fb_font_glyph(chr, &found);
     51        if (!found)
     52                return ENOENT;
     53       
     54        *glyph_id = glyph;
     55        return EOK;
    5156}
    5257
    53 static uint16_t fde_resolve(const wchar_t chr, void *data)
     58static int fde_load_glyph_surface(void *unused, glyph_id_t glyph_id,
     59    surface_t **out_surface)
    5460{
    55         return fb_font_glyph(chr);
     61        surface_t *surface = surface_create(FONT_WIDTH, FONT_SCANLINES, NULL, 0);
     62        if (!surface)
     63                return ENOMEM;
     64       
     65        for (unsigned int y = 0; y < FONT_SCANLINES; ++y) {
     66                for (unsigned int x = 0; x < FONT_WIDTH; ++x) {
     67                        pixel_t p = (fb_font[glyph_id][y] & (1 << (7 - x))) ?
     68                            PIXEL(255, 0, 0, 0) : PIXEL(0, 0, 0, 0);
     69                        surface_put_pixel(surface, x, y, p);
     70                }
     71        }
     72       
     73        *out_surface = surface;
     74        return EOK;
    5675}
    5776
    58 static surface_t *fde_render(uint16_t glyph, uint16_t points)
     77static int fde_load_glyph_metrics(void *unused, glyph_id_t glyph_id,
     78    glyph_metrics_t *gm)
    5979{
    60         surface_t *template = surface_create(FONT_WIDTH, FONT_SCANLINES, NULL, 0);
    61         if (!template) {
    62                 return NULL;
    63         }
    64         for (unsigned int y = 0; y < FONT_SCANLINES; ++y) {
    65                 for (unsigned int x = 0; x < FONT_WIDTH; ++x) {
    66                         pixel_t p = (fb_font[glyph][y] & (1 << (7 - x))) ?
    67                             PIXEL(255, 0, 0, 0) : PIXEL(0, 0, 0, 0);
    68                         surface_put_pixel(template, x, y, p);
    69                 }
    70         }
    71 
    72         source_t source;
    73         source_init(&source);
    74         source_set_texture(&source, template, false);
    75 
    76         transform_t transform;
    77         transform_identity(&transform);
    78         if (points != FONT_SCANLINES) {
    79                 double ratio = ((double) points) / ((double) FONT_SCANLINES);
    80                 transform_scale(&transform, ratio, ratio);
    81                 source_set_transform(&source, transform);
    82         }
    83 
    84         double width = FONT_WIDTH;
    85         double height = FONT_SCANLINES;
    86         transform_apply_linear(&transform, &width, &height);
    87         surface_t *result =
    88             surface_create((sysarg_t) (width + 0.5), (sysarg_t) (height + 0.5), NULL, 0);
    89         if (!result) {
    90                 surface_destroy(template);
    91                 return NULL;
    92         }
    93 
    94         drawctx_t context;
    95         drawctx_init(&context, result);
    96         drawctx_set_source(&context, &source);
    97         drawctx_transfer(&context, 0, 0,
    98             (sysarg_t) (width + 0.5), (sysarg_t) (height + 0.5));
    99 
    100         surface_destroy(template);
    101 
    102         return result;
     80        /* This is simple monospaced font, so fill this data statically */
     81        gm->left_side_bearing = 0;
     82        gm->width = FONT_WIDTH;
     83        gm->right_side_bearing = 0;
     84        gm->ascender = FONT_ASCENDER;
     85        gm->height = FONT_SCANLINES;
     86       
     87        return EOK;
    10388}
    10489
     
    10893}
    10994
    110 font_decoder_t fd_embedded = {
    111         .init = fde_init,
    112         .resolve = fde_resolve,
    113         .render = fde_render,
     95bitmap_font_decoder_t fd_embedded = {
     96        .resolve_glyph = fde_resolve_glyph,
     97        .load_glyph_surface = fde_load_glyph_surface,
     98        .load_glyph_metrics = fde_load_glyph_metrics,
    11499        .release = fde_release
    115100};
    116101
     102font_metrics_t font_metrics = {
     103        .ascender = FONT_ASCENDER,
     104        .descender = (FONT_SCANLINES - FONT_ASCENDER),
     105        .leading = 0
     106};
     107
     108int embedded_font_create(font_t **font, uint16_t points)
     109{
     110        return bitmap_font_create(&fd_embedded, NULL, FONT_GLYPHS, font_metrics,
     111            points, font);
     112}
     113
    117114/** @}
    118115 */
Note: See TracChangeset for help on using the changeset viewer.