2
-- GIMP Toolkit (GTK) - text layout functions: Font Descriptions
6
-- Created: 8 Feburary 2003
8
-- Version $Revision: 1.9 $ from $Date: 2005/10/17 23:06:11 $
10
-- Copyright (C) 1999-2005 Axel Simon
12
-- This library is free software; you can redistribute it and/or
13
-- modify it under the terms of the GNU Lesser General Public
14
-- License as published by the Free Software Foundation; either
15
-- version 2.1 of the License, or (at your option) any later version.
17
-- This library is distributed in the hope that it will be useful,
18
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
19
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
-- Lesser General Public License for more details.
25
-- Maintainer : gtk2hs-users@lists.sourceforge.net
26
-- Stability : provisional
27
-- Portability : portable (depends on GHC)
29
-- Functions to manage font descriptions.
31
-- * Font descriptions provide a way to query and state requirements on
32
-- fonts. This data structure has several fields describing different
33
-- characteristics of a font. Each of these fields can be set of left
36
module Graphics.UI.Gtk.Pango.Description (
40
fontDescriptionSetFamily,
41
fontDescriptionGetFamily,
42
fontDescriptionSetStyle,
43
fontDescriptionGetStyle,
44
fontDescriptionSetVariant,
45
fontDescriptionGetVariant,
46
fontDescriptionSetWeight,
47
fontDescriptionGetWeight,
48
fontDescriptionSetStretch,
49
fontDescriptionGetStretch,
50
fontDescriptionSetSize,
51
fontDescriptionGetSize,
53
fontDescriptionUnsetFields,
55
fontDescriptionBetterMatch,
56
fontDescriptionFromString,
57
fontDescriptionToString
62
import Data.Bits ((.&.))
64
import System.Glib.FFI
65
import System.Glib.Flags (Flags, fromFlags)
66
import System.Glib.UTFString
67
{#import Graphics.UI.Gtk.Types#}
68
import System.Glib.GObject (makeNewGObject)
69
{#import Graphics.UI.Gtk.Pango.Types#}
70
import Graphics.UI.Gtk.Pango.Enums
71
import Graphics.UI.Gtk.General.Enums
72
import Graphics.UI.Gtk.General.Structs (pangoScale)
74
{# context lib="pango" prefix="pango_font_description" #}
76
-- | Create a new font description.
78
-- * All field are unset.
80
fontDescriptionNew :: IO FontDescription
81
fontDescriptionNew = {#call unsafe new#} >>= makeNewFontDescription
83
-- | Make a deep copy of a font description.
85
fontDescriptionCopy :: FontDescription -> IO FontDescription
86
fontDescriptionCopy fd = {#call unsafe copy#} fd >>= makeNewFontDescription
88
-- | Set the font famliy.
90
-- * A font family is a name designating the design of the font (e.g. Sans
91
-- or Times) without the variant.
93
-- * In some contexts a comma separated list of font families can be used.
95
fontDescriptionSetFamily :: FontDescription -> String -> IO ()
96
fontDescriptionSetFamily fd family = withUTFString family $ \strPtr ->
97
{#call unsafe set_family_static#} fd strPtr
99
-- | Get the font family.
101
-- * 'Nothing' is returned if the font family is not set.
103
fontDescriptionGetFamily :: FontDescription -> IO (Maybe String)
104
fontDescriptionGetFamily fd = do
105
strPtr <- {#call unsafe get_family#} fd
106
if strPtr==nullPtr then return Nothing else
107
liftM Just $ peekUTFString strPtr
109
-- | Flags denoting which fields in a font description are set.
110
{#enum PangoFontMask as FontMask {underscoreToCase} deriving(Bounded) #}
112
instance Flags FontMask
114
-- | Set the style field.
116
-- * Most fonts will have either a 'StyleItalic' or 'StyleOblique'
119
fontDescriptionSetStyle :: FontDescription -> FontStyle -> IO ()
120
fontDescriptionSetStyle fd p =
121
{#call unsafe set_style#} fd (fromIntegral (fromEnum p))
123
-- | Get the style field.
124
fontDescriptionGetStyle :: FontDescription -> IO (Maybe FontStyle)
125
fontDescriptionGetStyle fd = do
126
fields <- {#call unsafe get_set_fields#} fd
127
if (fromEnum PangoFontMaskStyle) .&. (fromIntegral fields) /=0
128
then liftM (Just . toEnum . fromIntegral) $
129
{#call unsafe get_style#} fd
132
-- | Set the variant field.
134
fontDescriptionSetVariant :: FontDescription -> Variant -> IO ()
135
fontDescriptionSetVariant fd p =
136
{#call unsafe set_variant#} fd (fromIntegral (fromEnum p))
138
-- | Get the variant field.
139
fontDescriptionGetVariant :: FontDescription -> IO (Maybe Variant)
140
fontDescriptionGetVariant fd = do
141
fields <- {#call unsafe get_set_fields#} fd
142
if (fromEnum PangoFontMaskVariant) .&. (fromIntegral fields) /=0
143
then liftM (Just . toEnum . fromIntegral) $
144
{#call unsafe get_variant#} fd
147
-- | Set the weight field.
149
fontDescriptionSetWeight :: FontDescription -> Weight -> IO ()
150
fontDescriptionSetWeight fd p =
151
{#call unsafe set_weight#} fd (fromIntegral (fromEnum p))
153
-- | Get the weight field.
154
fontDescriptionGetWeight :: FontDescription -> IO (Maybe Weight)
155
fontDescriptionGetWeight fd = do
156
fields <- {#call unsafe get_set_fields#} fd
157
if (fromEnum PangoFontMaskWeight) .&. (fromIntegral fields) /=0
158
then liftM (Just . toEnum . fromIntegral) $
159
{#call unsafe get_weight#} fd
162
-- | Set the stretch field.
164
fontDescriptionSetStretch :: FontDescription -> Stretch -> IO ()
165
fontDescriptionSetStretch fd p =
166
{#call unsafe set_stretch#} fd (fromIntegral (fromEnum p))
168
-- | Get the stretch field.
169
fontDescriptionGetStretch :: FontDescription -> IO (Maybe Stretch)
170
fontDescriptionGetStretch fd = do
171
fields <- {#call unsafe get_set_fields#} fd
172
if (fromEnum PangoFontMaskStretch) .&. (fromIntegral fields) /=0
173
then liftM (Just . toEnum . fromIntegral) $
174
{#call unsafe get_stretch#} fd
177
-- | Set the size field.
179
-- * The given size is in points (pts). One point is 1\/72 inch.
181
fontDescriptionSetSize :: FontDescription -> PangoUnit -> IO ()
182
fontDescriptionSetSize fd p =
183
{#call unsafe set_size#} fd (puToInt p)
185
-- | Get the size field.
186
fontDescriptionGetSize :: FontDescription -> IO (Maybe PangoUnit)
187
fontDescriptionGetSize fd = do
188
fields <- {#call unsafe get_set_fields#} fd
189
if (fromEnum PangoFontMaskSize) .&. (fromIntegral fields) /=0
190
then liftM (\x -> Just (intToPu x)) $
191
{#call unsafe get_size#} fd
194
-- | Reset fields in a font description.
196
fontDescriptionUnsetFields :: FontDescription -> [FontMask] -> IO ()
197
fontDescriptionUnsetFields fd mask =
198
{#call unsafe unset_fields#} fd (fromIntegral (fromFlags mask))
200
-- | Merge two font descriptions.
202
-- * Copy fields from the second description to the first. If the boolean
203
-- parameter is set, existing fields in the first description will be
206
fontDescriptionMerge :: FontDescription -> FontDescription -> Bool -> IO ()
207
fontDescriptionMerge fd1 fd2 replace =
208
{#call unsafe merge#} fd1 fd2 (fromBool replace)
210
-- | Determine if two descriptions are simliar.
212
-- * Returns 'True' if the two descriptions only differ in weight or style.
214
fontDescriptionIsMatch :: FontDescription -> FontDescription -> Bool
215
fontDescriptionIsMatch fdA fdB = unsafePerformIO $ liftM toBool $
216
{#call unsafe better_match#} fdA (FontDescription nullForeignPtr) fdB
218
-- | Determine which of two descriptions matches a given description better.
220
-- * Returns @True@ if the last description is a better match to the first
221
-- arguement than the middle one.
223
-- * Approximate matching is done on weight and style. If the other
224
-- attributes do not match, the function returns @False@.
226
fontDescriptionBetterMatch :: FontDescription -> FontDescription ->
227
FontDescription -> Bool
228
fontDescriptionBetterMatch fd fdA fdB = unsafePerformIO $ liftM toBool $
229
{#call unsafe better_match#} fd fdA fdB
231
-- | Create a font description from a string.
233
-- * The given argument must have the form
234
-- @[FAMILY-LIST] [STYLE-OPTIONS] [SIZE]@ where @FAMILY_LIST@ is a comma
235
-- separated list of font families optionally terminated by a comma,
236
-- @STYLE_OPTIONS@ is a whitespace separated list of words where each
237
-- word describes one of style, variant, weight or stretch. @SIZE@ is
238
-- a decimal number giving the size of the font in points. If any of
239
-- these fields is absent, the resulting 'FontDescription' will have
240
-- the corresponing fields unset.
242
fontDescriptionFromString :: String -> IO FontDescription
243
fontDescriptionFromString descr = withUTFString descr $ \strPtr ->
244
{#call unsafe from_string#} strPtr >>= makeNewFontDescription
246
-- | Convert a font description to a string.
248
-- * Creates a string representation of a font description. See
249
-- 'fontDescriptionFromString' for the format of the string.
251
fontDescriptionToString :: FontDescription -> IO String
252
fontDescriptionToString fd = do
253
strPtr <- {#call unsafe to_string#} fd
254
str <- peekUTFString strPtr
255
{#call unsafe g_free#} (castPtr strPtr)