Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright 2009 John-Mark Bell <jmb@netsurf-browser.org>
  3.  *
  4.  * This file is part of NetSurf, http://www.netsurf-browser.org/
  5.  *
  6.  * NetSurf is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; version 2 of the License.
  9.  *
  10.  * NetSurf is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17.  */
  18.  
  19. #include "css/css.h"
  20. #include "css/utils.h"
  21. #include "desktop/options.h"
  22. #include "render/font.h"
  23.  
  24. static plot_font_generic_family_t plot_font_generic_family(
  25.                 enum css_font_family_e css);
  26. static int plot_font_weight(enum css_font_weight_e css);
  27. static plot_font_flags_t plot_font_flags(enum css_font_style_e style,
  28.                 enum css_font_variant_e variant);
  29.  
  30. /**
  31.  * Populate a font style using data from a computed CSS style
  32.  *
  33.  * \param css     Computed style to consider
  34.  * \param fstyle  Font style to populate
  35.  */
  36. void font_plot_style_from_css(const css_computed_style *css,
  37.                 plot_font_style_t *fstyle)
  38. {
  39.         lwc_string **families;
  40.         css_fixed length = 0;
  41.         css_unit unit = CSS_UNIT_PX;
  42.         css_color col;
  43.  
  44.         fstyle->family = plot_font_generic_family(
  45.                         css_computed_font_family(css, &families));
  46.  
  47.         css_computed_font_size(css, &length, &unit);
  48.         fstyle->size = FIXTOINT(FMUL(nscss_len2pt(length, unit),
  49.                                       INTTOFIX(FONT_SIZE_SCALE)));
  50.  
  51.         /* Clamp font size to configured minimum */
  52.         if (fstyle->size < (nsoption_int(font_min_size) * FONT_SIZE_SCALE) / 10)
  53.                 fstyle->size = (nsoption_int(font_min_size) * FONT_SIZE_SCALE) / 10;
  54.  
  55.         fstyle->weight = plot_font_weight(css_computed_font_weight(css));
  56.         fstyle->flags = plot_font_flags(css_computed_font_style(css),
  57.                         css_computed_font_variant(css));
  58.  
  59.         css_computed_color(css, &col);
  60.         fstyle->foreground = nscss_color_to_ns(col);
  61.         fstyle->background = 0;
  62. }
  63.  
  64. /******************************************************************************
  65.  * Helper functions                                                           *
  66.  ******************************************************************************/
  67.  
  68. /**
  69.  * Map a generic CSS font family to a generic plot font family
  70.  *
  71.  * \param css  Generic CSS font family
  72.  * \return Plot font family
  73.  */
  74. plot_font_generic_family_t plot_font_generic_family(
  75.                 enum css_font_family_e css)
  76. {
  77.         plot_font_generic_family_t plot;
  78.  
  79.         switch (css) {
  80.         case CSS_FONT_FAMILY_SERIF:
  81.                 plot = PLOT_FONT_FAMILY_SERIF;
  82.                 break;
  83.         case CSS_FONT_FAMILY_MONOSPACE:
  84.                 plot = PLOT_FONT_FAMILY_MONOSPACE;
  85.                 break;
  86.         case CSS_FONT_FAMILY_CURSIVE:
  87.                 plot = PLOT_FONT_FAMILY_CURSIVE;
  88.                 break;
  89.         case CSS_FONT_FAMILY_FANTASY:
  90.                 plot = PLOT_FONT_FAMILY_FANTASY;
  91.                 break;
  92.         case CSS_FONT_FAMILY_SANS_SERIF:
  93.         default:
  94.                 plot = PLOT_FONT_FAMILY_SANS_SERIF;
  95.                 break;
  96.         }
  97.  
  98.         return plot;
  99. }
  100.  
  101. /**
  102.  * Map a CSS font weight to a plot weight value
  103.  *
  104.  * \param css  CSS font weight
  105.  * \return Plot weight
  106.  */
  107. int plot_font_weight(enum css_font_weight_e css)
  108. {
  109.         int weight;
  110.  
  111.         switch (css) {
  112.         case CSS_FONT_WEIGHT_100:
  113.                 weight = 100;
  114.                 break;
  115.         case CSS_FONT_WEIGHT_200:
  116.                 weight = 200;
  117.                 break;
  118.         case CSS_FONT_WEIGHT_300:
  119.                 weight = 300;
  120.                 break;
  121.         case CSS_FONT_WEIGHT_400:
  122.         case CSS_FONT_WEIGHT_NORMAL:
  123.         default:
  124.                 weight = 400;
  125.                 break;
  126.         case CSS_FONT_WEIGHT_500:
  127.                 weight = 500;
  128.                 break;
  129.         case CSS_FONT_WEIGHT_600:
  130.                 weight = 600;
  131.                 break;
  132.         case CSS_FONT_WEIGHT_700:
  133.         case CSS_FONT_WEIGHT_BOLD:
  134.                 weight = 700;
  135.                 break;
  136.         case CSS_FONT_WEIGHT_800:
  137.                 weight = 800;
  138.                 break;
  139.         case CSS_FONT_WEIGHT_900:
  140.                 weight = 900;
  141.                 break;
  142.         }
  143.  
  144.         return weight;
  145. }
  146.  
  147. /**
  148.  * Map a CSS font style and font variant to plot font flags
  149.  *
  150.  * \param style    CSS font style
  151.  * \param variant  CSS font variant
  152.  * \return Computed plot flags
  153.  */
  154. plot_font_flags_t plot_font_flags(enum css_font_style_e style,
  155.                 enum css_font_variant_e variant)
  156. {
  157.         plot_font_flags_t flags = FONTF_NONE;
  158.  
  159.         if (style == CSS_FONT_STYLE_ITALIC)
  160.                 flags |= FONTF_ITALIC;
  161.         else if (style == CSS_FONT_STYLE_OBLIQUE)
  162.                 flags |= FONTF_OBLIQUE;
  163.  
  164.         if (variant == CSS_FONT_VARIANT_SMALL_CAPS)
  165.                 flags |= FONTF_SMALLCAPS;
  166.  
  167.         return flags;
  168. }
  169.  
  170.