{-# LINE 2 "./Graphics/Rendering/Pango/Markup.chs" #-}
-- -*-haskell-*-
-- GIMP Toolkit (GTK) Markup
--
-- Author : Axel Simon
--
-- Created: 5 June 2001
--
-- Copyright (C) 1999-2005 Axel Simon
--
-- This library is free software; you can redistribute it and/or
-- modify it under the terms of the GNU Lesser General Public
-- License as published by the Free Software Foundation; either
-- version 2.1 of the License, or (at your option) any later version.
--
-- This library is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-- Lesser General Public License for more details.
--
-- |
-- Maintainer : gtk2hs-users@lists.sourceforge.net
-- Stability : provisional
-- Portability : portable (depends on GHC)
--
-- This module defines some helper functions for generating texts with
-- embedded attributes. Note that there is no need to use these functions.
-- In particular, if you set markup in labels that are subject to
-- internationalization, it can be of advantage to write out the markup
-- instead of using the functions in this module.
--
-- In order to display a string that may contain markup characters, use
-- 'Graphics.UI.Gtk.Pango.Layout.escapeMarkup'.
--
-- When you write markup directly, you can make use of the following
-- convenience tags:
--
-- [@b@] Bold
--
-- [@big@] Makes font relatively larger
--
-- [@i@] Italic
--
-- [@s@] Strikethrough
--
-- [@sub@] Subscript
--
-- [@sup@] Superscript
--
-- [@small@] Makes font relatively smaller
--
-- [@tt@] Monospace font
--
-- [@u@] Underline
--
module Graphics.Rendering.Pango.Markup (
  SpanAttribute(..),
  markSpan,
  parseMarkup
  ) where

import qualified Graphics.Rendering.Pango.Enums as Pango
import Graphics.Rendering.Pango.Attributes ( parseMarkup )

-- | These are all the attributes the 'markSpan' function can express.
--
data SpanAttribute
  -- | Choose a font by textual description.
  --
  -- * Takes a string to completely describe the font, example:
  -- @FontDescr@ \"Sans Italic 12\"
  = FontDescr String

  -- | Specify the family of font to use.
  --
  -- * Example: @FontFamily@ \"Sans\"
  | FontFamily String

  -- | Change the size of the current font.
  --
  -- * The constructor takes the size in points (pt) or a predefined
  -- sizes. Setting the absolute size 12.5pt can be achieved by passing
  -- 'FontSize' ('SizePoint' 12.5) to 'markSpan'. Next to predefined
  -- absolute sizes such as 'Pango.SizeSmall' the size can be changed by
  -- asking for the next larger or smaller front with
  -- 'Pango.SizeLarger' and 'Pango.SizeSmaller', respectively.
  | FontSize Pango.Size

  -- | Change the slant of the current font.
  --
  | FontStyle Pango.FontStyle

  -- | Change the thickness of the current font.
  --
  -- * The constructor takes one of the six predefined weights. Most likely to
  -- be supported: 'Pango.WeightBold'.
  --
  | FontWeight Pango.Weight

  -- | Choosing an alternative rendering for lower case letters.
  --
  -- * The argument 'Pango.VariantSmallCaps' will display lower case letters
  -- as smaller upper case letters, if this option is available.
  | FontVariant Pango.Variant

  -- | Choose a different width.
  --
  -- * Takes one of nine font widths, e.g. 'Pango.WidthExpanded'.
  --
  | FontStretch Pango.Stretch

  -- | Foreground color.
  --
  -- * This constructor and 'FontBackground' take both a description
  -- of the color to be used for rendering. The name is either a
  -- hex code of the form \"#RRGGBB\" or an X11 color name like
  -- \"dark olive green\".
  --
  | FontForeground String -- FIXME: should be ColorName from GDK or so

  -- | Background color.
  | FontBackground String

  -- | Specify underlining of text.
  --
  | FontUnderline Pango.Underline

  -- | Specify a vertical displacement.
  --
  -- * Takes the vertical displacement in em (the width of the \'m\' character
  -- in the current font).
  | FontRise Double

  -- | Give a hint about the language to be displayed.
  --
  -- * This hint might help the system rendering a particular piece of text
  -- with different fonts that are more suitable for the given language.
  --
  | FontLang Pango.Language


  -- | Gravity of text, use for ratation.
  | FontGravity Pango.PangoGravity

  -- | Intensity of gravity.
  | FontGravityHint Pango.PangoGravityHint


instance Show SpanAttribute where
  showsPrec :: Int -> SpanAttribute -> ShowS
showsPrec Int
_ (FontDescr String
str) = String -> ShowS
showString String
" font_desc="ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.String -> ShowS
forall a. Show a => a -> ShowS
shows String
str
  showsPrec Int
_ (FontFamily String
str) = String -> ShowS
showString String
" font_family="ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.String -> ShowS
forall a. Show a => a -> ShowS
shows String
str
  showsPrec Int
_ (FontSize Size
size) = String -> ShowS
showString String
" size="ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Size -> ShowS
forall a. Show a => a -> ShowS
shows Size
size
  showsPrec Int
_ (FontStyle FontStyle
style) = String -> ShowS
showString String
" style="ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.FontStyle -> ShowS
forall a. Show a => a -> ShowS
shows FontStyle
style
  showsPrec Int
_ (FontWeight Weight
w) = String -> ShowS
showString String
" weight="ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Weight -> ShowS
forall a. Show a => a -> ShowS
shows Weight
w
  showsPrec Int
_ (FontVariant Variant
v) = String -> ShowS
showString String
" variant="ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Variant -> ShowS
forall a. Show a => a -> ShowS
shows Variant
v
  showsPrec Int
_ (FontStretch Stretch
s) = String -> ShowS
showString String
" stretch="ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Stretch -> ShowS
forall a. Show a => a -> ShowS
shows Stretch
s
  showsPrec Int
_ (FontForeground String
c) = String -> ShowS
showString String
" foreground="ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.String -> ShowS
forall a. Show a => a -> ShowS
shows String
c
  showsPrec Int
_ (FontBackground String
c) = String -> ShowS
showString String
" background="ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.String -> ShowS
forall a. Show a => a -> ShowS
shows String
c
  showsPrec Int
_ (FontUnderline Underline
u) = String -> ShowS
showString String
" underline="ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Underline -> ShowS
forall a. Show a => a -> ShowS
shows Underline
u
  showsPrec Int
_ (FontRise Double
r) = String -> ShowS
showString String
" rise="ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.String -> ShowS
forall a. Show a => a -> ShowS
shows
                                   (Integer -> String
forall a. Show a => a -> String
show (Double -> Integer
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
round (Double
rDouble -> Double -> Double
forall a. Num a => a -> a -> a
*Double
10000)))
  showsPrec Int
_ (FontLang Language
l) = String -> ShowS
showString String
" lang="ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Language -> ShowS
forall a. Show a => a -> ShowS
shows Language
l

  showsPrec Int
_ (FontGravity PangoGravity
g) = String -> ShowS
showString String
" gravity="ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.PangoGravity -> ShowS
forall a. Show a => a -> ShowS
shows PangoGravity
g
  showsPrec Int
_ (FontGravityHint PangoGravityHint
h) = String -> ShowS
showString String
" gravity_hint"ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.PangoGravityHint -> ShowS
forall a. Show a => a -> ShowS
shows PangoGravityHint
h


-- | Create the most generic span attribute.
--
markSpan :: [SpanAttribute] -> String -> String
markSpan :: [SpanAttribute] -> ShowS
markSpan [SpanAttribute]
attrs String
text = String -> ShowS
showString String
"<span"ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
                      (ShowS -> ShowS -> ShowS) -> ShowS -> [ShowS] -> ShowS
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
(.) (Char -> ShowS
showChar Char
'>') ((SpanAttribute -> ShowS) -> [SpanAttribute] -> [ShowS]
forall a b. (a -> b) -> [a] -> [b]
map SpanAttribute -> ShowS
forall a. Show a => a -> ShowS
shows [SpanAttribute]
attrs)ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
                      String -> ShowS
showString String
textShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
                      String -> ShowS
showString String
"</span>" ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ String
""