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 2011 Things Made Out Of Other Things Ltd.
  6.  * Written by James Montgomerie <jamie@th.ingsmadeoutofotherthin.gs>
  7.  */
  8.  
  9. #include <string.h>
  10.  
  11. #include "select/font_face.h"
  12.  
  13. static void font_faces_srcs_destroy(css_font_face *font_face)
  14. {
  15.         uint32_t i;
  16.         css_font_face_src *srcs = font_face->srcs;
  17.  
  18.         for (i = 0; i < font_face->n_srcs; ++i) {
  19.                 if (srcs[i].location != NULL) {
  20.                         lwc_string_unref(srcs[i].location);
  21.                 }
  22.         }
  23.        
  24.         font_face->alloc(srcs, 0, font_face->pw);
  25.         font_face->srcs = NULL;
  26. }
  27.  
  28. static const css_font_face default_font_face = {
  29.         NULL,
  30.         NULL,
  31.         0,
  32.         { (CSS_FONT_WEIGHT_NORMAL << 2) | CSS_FONT_STYLE_NORMAL },
  33.         NULL,
  34.         NULL
  35. };
  36.  
  37. /**
  38.  * Create a font-face
  39.  *
  40.  * \param alloc   Memory (de)allocation function
  41.  * \param pw      Pointer to client-specific data
  42.  * \param result  Pointer to location to receive result
  43.  * \return CSS_OK on success,
  44.  *         CSS_NOMEM on memory exhaustion,
  45.  *         CSS_BADPARM on bad parameters.
  46.  */
  47. css_error css__font_face_create(css_allocator_fn alloc, void *pw,
  48.                 css_font_face **result)
  49. {
  50.         css_font_face *f;
  51.        
  52.         if (alloc == NULL || result == NULL)
  53.                 return CSS_BADPARM;
  54.        
  55.         f = alloc(NULL, sizeof(css_font_face), pw);
  56.         if (f == NULL)
  57.                 return CSS_NOMEM;
  58.        
  59.         memcpy(f, &default_font_face, sizeof(css_font_face));
  60.        
  61.         f->alloc = alloc;
  62.         f->pw = pw;
  63.        
  64.         *result = f;
  65.        
  66.         return CSS_OK;
  67. }
  68.  
  69. /**
  70.  * Destroy a font-face
  71.  *
  72.  * \param font_face  Font-face to destroy
  73.  * \return CSS_OK on success, appropriate error otherwise
  74.  */
  75. css_error css__font_face_destroy(css_font_face *font_face)
  76. {      
  77.         if (font_face == NULL)
  78.                 return CSS_BADPARM;
  79.  
  80.         if (font_face->font_family != NULL)
  81.                 lwc_string_unref(font_face->font_family);
  82.        
  83.         if (font_face->srcs != NULL)
  84.                 font_faces_srcs_destroy(font_face);
  85.  
  86.         font_face->alloc(font_face, 0, font_face->pw);
  87.        
  88.         return CSS_OK;
  89. }
  90.  
  91.  
  92. /**
  93.  * Set a font-face's font-family name
  94.  *
  95.  * \param font_face    The font-face
  96.  * \param font_family  Font-family name
  97.  * \param result       Pointer to location to receive result
  98.  * \return CSS_OK on success,
  99.  *         CSS_BADPARM on bad parameters.
  100.  */
  101. css_error css__font_face_set_font_family(css_font_face *font_face,
  102.                 lwc_string *font_family)
  103. {
  104.         if (font_face == NULL || font_family == NULL)
  105.                 return CSS_BADPARM;
  106.        
  107.         if (font_face->font_family != NULL)
  108.                 lwc_string_unref(font_face->font_family);
  109.                
  110.         font_face->font_family = lwc_string_ref(font_family);
  111.  
  112.         return CSS_OK;
  113. }
  114.  
  115. /**
  116.  * Get a font-face's font-family name
  117.  *
  118.  * \param font_face  The font-face
  119.  * \param result     Pointer to location to receive result
  120.  * \return CSS_OK on success,
  121.  *         CSS_BADPARM on bad parameters.
  122.  */
  123. css_error css_font_face_get_font_family(const css_font_face *font_face,
  124.                 lwc_string **font_family)
  125. {
  126.         if (font_face == NULL || font_family == NULL)
  127.                 return CSS_BADPARM;
  128.        
  129.         *font_family = font_face->font_family;
  130.        
  131.         return CSS_OK;
  132. }
  133.  
  134. /**
  135.  * Get the style of font for a font-face.
  136.  *
  137.  * \param src  The font-face
  138.  * \return The style, as a css_font_style_e
  139.  */
  140. uint8_t css_font_face_font_style(const css_font_face *font_face)
  141. {
  142.         return font_face->bits[0] & 0x3;
  143. }
  144.  
  145. /**
  146.  * Get the weight of font for a font-face.
  147.  *
  148.  * \param src  The font-face
  149.  * \return The style, as a css_font_weight_e
  150.  */
  151. uint8_t css_font_face_font_weight(const css_font_face *font_face)
  152. {
  153.         return (font_face->bits[0] >> 2) & 0xf;
  154. }
  155.  
  156. /**
  157.  * Get the number of potential src locations for a font-face
  158.  *
  159.  * \param font_face  The font-face
  160.  * \param count      Pointer to location to receive result
  161.  * \return CSS_OK on success,
  162.  *         CSS_BADPARM on bad parameters.
  163.  */
  164. css_error css_font_face_count_srcs(const css_font_face *font_face,
  165.                 uint32_t *count)
  166. {
  167.         if (font_face == NULL || count == NULL)
  168.                 return CSS_BADPARM;
  169.  
  170.         *count = font_face->n_srcs;
  171.         return CSS_OK;
  172. }
  173.  
  174. /**
  175.  * Get a specific src location from a font-face
  176.  *
  177.  * \param font_face  The font-face
  178.  * \param index      The index for the wanted src.
  179.  * \param count      Pointer to location to receive result
  180.  * \return CSS_OK on success,
  181.  *         CSS_BADPARM on bad parameters.
  182.  */
  183. css_error css_font_face_get_src(const css_font_face *font_face,
  184.                 uint32_t index, const css_font_face_src **src)
  185. {
  186.         if (font_face == NULL || src == NULL || index >= font_face->n_srcs)
  187.                 return CSS_BADPARM;
  188.  
  189.         *src = &(font_face->srcs[index]);
  190.        
  191.         return CSS_OK;
  192. }
  193.  
  194. /**
  195.  * Get the location for a font-face src.
  196.  *
  197.  * \param font_face  The font-face
  198.  * \param count      Pointer to location to receive result
  199.  * \return CSS_OK on success,
  200.  *         CSS_BADPARM on bad parameters.
  201.  *
  202.  * \note  The type of location (local or URL) can be gathered from
  203.  *        css_font_face_src_location_type, and the format of font (if specified)
  204.  *        from css_font_face_src_format.
  205.  */
  206. css_error css_font_face_src_get_location(const css_font_face_src *src,
  207.                 lwc_string **location)
  208. {
  209.         if (src == NULL || location == NULL)
  210.                 return CSS_BADPARM;
  211.        
  212.         *location = src->location;
  213.  
  214.         return CSS_OK; 
  215. }
  216.  
  217. /**
  218.  * Get the location type for a font-face src.
  219.  *
  220.  * \param src  The font-face src
  221.  * \return The location type
  222.  */
  223. css_font_face_location_type css_font_face_src_location_type(
  224.                 const css_font_face_src *src)
  225. {
  226.         return src->bits[0] & 0x3;
  227. }
  228.  
  229. /**
  230.  * Get the format of font for a font-face src.
  231.  *
  232.  * \param src  The font-face src
  233.  * \return The format, if specified
  234.  */
  235. css_font_face_format css_font_face_src_format(const css_font_face_src *src)
  236. {
  237.         return (src->bits[0] >> 2) & 0x1f;
  238. }
  239.  
  240. /**
  241.  * Set a font-faces array of srcs.
  242.  *
  243.  * \param font_face  The font-face
  244.  * \param srcs       The array of css_font_face_srcs
  245.  * \param n_srcs     The count of css_font_face_srcs in the array
  246.  * \return The format, if specified
  247.  */
  248. css_error css__font_face_set_srcs(css_font_face *font_face,
  249.                 css_font_face_src *srcs, uint32_t n_srcs)
  250. {
  251.         if (font_face->srcs != NULL)
  252.                 font_faces_srcs_destroy(font_face);
  253.        
  254.         font_face->srcs = srcs;
  255.         font_face->n_srcs = n_srcs;
  256.        
  257.         return CSS_OK;
  258. }
  259.  
  260.  
  261.