Subversion Repositories Kolibri OS

Rev

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

  1. /*
  2.  * This file is part of LibCSS.
  3.  * Licensed under the MIT License,
  4.  *                http://www.opensource.org/licenses/mit-license.php
  5.  * Copyright 2009 John-Mark Bell <jmb@netsurf-browser.org>
  6.  */
  7.  
  8. #include <assert.h>
  9. #include <string.h>
  10.  
  11. #include "bytecode/bytecode.h"
  12. #include "bytecode/opcodes.h"
  13. #include "parse/properties/properties.h"
  14. #include "parse/properties/utils.h"
  15.  
  16. /**
  17.  * Determine if a given font-family ident is reserved
  18.  *
  19.  * \param c      Parsing context
  20.  * \param ident  IDENT to consider
  21.  * \return True if IDENT is reserved, false otherwise
  22.  */
  23. static bool font_family_reserved(css_language *c, const css_token *ident)
  24. {
  25.         bool match;
  26.  
  27.         return (lwc_string_caseless_isequal(
  28.                         ident->idata, c->strings[SERIF],
  29.                         &match) == lwc_error_ok && match) ||
  30.                 (lwc_string_caseless_isequal(
  31.                         ident->idata, c->strings[SANS_SERIF],
  32.                         &match) == lwc_error_ok && match) ||
  33.                 (lwc_string_caseless_isequal(
  34.                         ident->idata, c->strings[CURSIVE],
  35.                         &match) == lwc_error_ok && match) ||
  36.                 (lwc_string_caseless_isequal(
  37.                         ident->idata, c->strings[FANTASY],
  38.                         &match) == lwc_error_ok && match) ||
  39.                 (lwc_string_caseless_isequal(
  40.                         ident->idata, c->strings[MONOSPACE],
  41.                         &match) == lwc_error_ok && match);
  42. }
  43.  
  44. /**
  45.  * Convert a font-family token into a bytecode value
  46.  *
  47.  * \param c      Parsing context
  48.  * \param token  Token to consider
  49.  * \param first  Whether the token is the first
  50.  * \return Bytecode value
  51.  */
  52. static css_code_t font_family_value(css_language *c, const css_token *token, bool first)
  53. {
  54.         uint16_t value;
  55.         bool match;
  56.  
  57.         if (token->type == CSS_TOKEN_IDENT) {
  58.                 if ((lwc_string_caseless_isequal(
  59.                                 token->idata, c->strings[SERIF],
  60.                                 &match) == lwc_error_ok && match))
  61.                         value = FONT_FAMILY_SERIF;
  62.                 else if ((lwc_string_caseless_isequal(
  63.                                 token->idata, c->strings[SANS_SERIF],
  64.                                 &match) == lwc_error_ok && match))
  65.                         value = FONT_FAMILY_SANS_SERIF;
  66.                 else if ((lwc_string_caseless_isequal(
  67.                                 token->idata, c->strings[CURSIVE],
  68.                                 &match) == lwc_error_ok && match))
  69.                         value = FONT_FAMILY_CURSIVE;
  70.                 else if ((lwc_string_caseless_isequal(
  71.                                 token->idata, c->strings[FANTASY],
  72.                                 &match) == lwc_error_ok && match))
  73.                         value = FONT_FAMILY_FANTASY;
  74.                 else if ((lwc_string_caseless_isequal(
  75.                                 token->idata, c->strings[MONOSPACE],
  76.                                 &match) == lwc_error_ok && match))
  77.                         value = FONT_FAMILY_MONOSPACE;
  78.                 else
  79.                         value = FONT_FAMILY_IDENT_LIST;
  80.         } else {
  81.                 value = FONT_FAMILY_STRING;
  82.         }
  83.  
  84.         return first ? buildOPV(CSS_PROP_FONT_FAMILY, 0, value) : value;
  85. }
  86.  
  87. /**
  88.  * Parse font-family
  89.  *
  90.  * \param c       Parsing context
  91.  * \param vector  Vector of tokens to process
  92.  * \param ctx     Pointer to vector iteration context
  93.  * \param result  Pointer to location to receive resulting style
  94.  * \return CSS_OK on success,
  95.  *         CSS_NOMEM on memory exhaustion,
  96.  *         CSS_INVALID if the input is not valid
  97.  *
  98.  * Post condition: \a *ctx is updated with the next token to process
  99.  *                 If the input is invalid, then \a *ctx remains unchanged.
  100.  */
  101. css_error css__parse_font_family(css_language *c,
  102.                 const parserutils_vector *vector, int *ctx,
  103.                 css_style *result)
  104. {
  105.         int orig_ctx = *ctx;
  106.         css_error error;
  107.         const css_token *token;
  108.         bool match;
  109.  
  110.         /* [ IDENT+ | STRING ] [ ',' [ IDENT+ | STRING ] ]* | IDENT(inherit)
  111.          *
  112.          * In the case of IDENT+, any whitespace between tokens is collapsed to
  113.          * a single space
  114.          *
  115.          * \todo Mozilla makes the comma optional.
  116.          * Perhaps this is a quirk we should inherit?
  117.          */
  118.  
  119.         token = parserutils_vector_iterate(vector, ctx);
  120.         if (token == NULL || (token->type != CSS_TOKEN_IDENT &&
  121.                         token->type != CSS_TOKEN_STRING)) {
  122.                 *ctx = orig_ctx;
  123.                 return CSS_INVALID;
  124.         }
  125.  
  126.         if (token->type == CSS_TOKEN_IDENT &&
  127.                         (lwc_string_caseless_isequal(
  128.                         token->idata, c->strings[INHERIT],
  129.                         &match) == lwc_error_ok && match)) {
  130.                 error = css_stylesheet_style_inherit(result, CSS_PROP_FONT_FAMILY);
  131.         } else {
  132.                 *ctx = orig_ctx;
  133.  
  134.                 error = css__comma_list_to_style(c, vector, ctx,
  135.                                 font_family_reserved, font_family_value,
  136.                                 result);
  137.                 if (error != CSS_OK) {
  138.                         *ctx = orig_ctx;
  139.                         return error;
  140.                 }
  141.  
  142.                 error = css__stylesheet_style_append(result, FONT_FAMILY_END);
  143.         }
  144.  
  145.         if (error != CSS_OK) {
  146.                 *ctx = orig_ctx;
  147.                 return error;
  148.         }
  149.  
  150.         return CSS_OK;
  151. }
  152.