123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683 |
- deferred class CD_TEXT
- -- A raster text using a font with styles. The position the text is drawn
- -- depends on the text alignment attribute.
- --
- -- The library has at least 4 standard typefaces: "System" (which depends on
- -- the driver and platform), "Courier" (mono spaced with serif), "Times"
- -- (proportional with serif) and "Helvetica" (proportional without serif). Each
- -- typeface can have some styles: Plain, Bold, Italic and a combination of Bold
- -- and Italic. As an alternative to the standard typefaces, you can use other
- -- typefaces or native driver typefaces with the function "set_native_font", but
- -- they may work in a reduced set of drivers.
- --
- -- You may retrieve the dimensions of the selected font with function
- -- "get_font_dim". Also you may retrieve the bounding box of a specific text
- -- before drawing by using the "get_text_size" and "get_text_box" functions.
- --
- -- The text is drawn using a reference point; you can change the alignment
- -- relative to this point using the "set_text_aligment" function.
- inherit
- CANVAS_DRAW
- feature {ANY}
- -- Operations
- draws_text (x, y: INTEGER; text: STRING)
- -- Draws a text in the position (x,y) according to the current font and
- -- text alignment. It expects an ANSI string. Can have line breaks.
- do
- int_canvas_text (cnvs, x, y, text.to_external)
- end
- draws_text_real (x, y: REAL_64; text: STRING)
- -- Like "draws_text" but with real coordinates.
- do
- int_canvas_c_double_text (cnvs, x, y, text.to_external)
- end
- wd_draws_text (x, y: REAL_64; text: STRING)
- -- Like "draws_text" but with World coordinates.
- do
- int_wd_canvas_text (cnvs, x, y, text.to_external)
- end
- -- Attributes
- set_font_typeface (typeface: STRING)
- -- The font type can be one of the standard type faces or other driver
- -- dependent type face. Since font face names are not a standard between
- -- drivers, a few names are specially handled to improve application
- -- portability. If you want to use names that work for all systems we
- -- recommend using: "Courier", "Times" and "Helvetica".
- -- Default: System.
- do
- int_canvas_font(cnvs, typeface.to_external, -1, 0)
- end
- get_font_typeface: STRING
- local
- t, s, z: POINTER
- str: STRING
- do
- int_canvas_get_font (cnvs, t, s, z)
- create str.from_external_copy(t)
- Result := str
- end
- set_font_styles (styles: INTEGER)
- -- Defines the style of the font. Can be a combination of:
- --
- -- 0 = Plain
- -- 1 = Bold
- -- 2 = Italic
- -- 4 = Underline
- -- 8 = Strikeout
- --
- -- Only the Windows and PDF drivers support underline and strikeout.
- -- For example to define Bold and Italic, use 1|2 (bitwise logical
- -- inclusive). Default: 0 (Plain).
- local
- p: POINTER
- do
- int_canvas_font(cnvs, p, styles, 0)
- end
- get_font_styles: INTEGER
- local
- p, si: POINTER
- s: INTEGER
- do
- int_canvas_get_font (cnvs, p, $s, si)
- Result := s
- end
- set_font_size (size: INTEGER)
- -- The size is provided in points (1/72 inch) or in pixels (using negative
- -- values).
- -- Default: 12.
- local
- p: POINTER
- do
- int_canvas_font(cnvs, p, -1, size)
- end
- get_font_size: INTEGER
- local
- p, st: POINTER
- s: INTEGER
- do
- int_canvas_get_font (cnvs, p, st, $s)
- Result := s
- end
- set_wd_font_size (size: REAL_64)
- -- Size is specified in millimeters, but is internally converted
- -- to points.
- local
- p: POINTER
- do
- int_wd_canvas_font(cnvs, p, -1, size)
- end
- get_wd_font_styles: REAL_64
- local
- p, st: POINTER
- s: REAL_64
- do
- int_wd_canvas_get_font (cnvs, p, st, $s)
- Result := s
- end
- set_native_font (format: STRING)
- local
- p: POINTER
- do
- p := int_canvas_native_font(cnvs, format.to_external)
- end
- get_previous_native_font: STRING
- -- Selects a font based on a string description. The description can
- -- depend on the driver and the platform, but a common definition is
- -- available for all drivers. It does not need to be stored by the
- -- application, as it is internally replicated by the library. The string
- -- is case sensitive.
- --
- -- The common format definition is similar to the the Pango library
- -- Font Description, used by GTK+2. It is defined as having 3 parts:
- -- <font family>, <font styles> <font size>. For ex: "Times, Bold 18",
- -- or "Arial,Helvetica, Italic Underline -24". The supported styles
- -- include: Bold, Italic, Underline and Strikeout. Underline,
- -- Strikeout, and negative pixel values are not supported by the
- -- standard Pango Font Description. The Pango format include many
- -- other definitions not supported by the CD format, they are just
- -- ignored.
- --
- -- The IUP "FONT" attribute internal formats are also accepted in all
- -- drivers and platforms.
- local
- p, s: POINTER
- str: STRING
- do
- s := int_canvas_native_font (cnvs, p)
- create str.from_external_copy(s)
- Result := str
- end
- get_current_native_font: STRING
- local
- p: POINTER
- v, str: STRING
- do
- v := "(char*)CD_QUERY"
- p := int_canvas_native_font (cnvs, v.to_external)
- create str.from_external_copy(p)
- Result := str
- end
- set_text_alignment (value: STRING)
- -- Defines the vertical and horizontal alignment of a text
- require
- is_valid_alignment(value)
- local
- v: INTEGER
- do
- v := int_canvas_text_alignment(cnvs, alignment_to_int(value))
- end
- get_text_alignment: STRING
- local
- i: INTEGER
- do
- i := int_canvas_text_alignment(cnvs, -1)
- Result := int_to_alignment(i)
- end
- set_text_orientation (angle: REAL_64)
- -- Defines the text orientation, which is an angle provided in degrees
- -- relative to the horizontal line according to which the text is
- -- drawn. Default: 0.
- local
- i: REAL_64
- do
- i := int_canvas_text_orientation(cnvs, angle)
- end
- get_text_orientation: REAL_64
- do
- Result := int_canvas_text_orientation(cnvs, -1)
- end
- -- Properties
- get_font_dim: TUPLE[INTEGER, INTEGER, INTEGER, INTEGER]
- -- Returns (in this order) the maximum width of a character, the
- -- line's height, the ascent and descent of the characters of the
- -- currently selected font. The line's height is the sum of the ascent
- -- and descent of a given additional space (if this is the case).
- -- All values are given in pixels and are positive.
- local
- max_width, height, ascent, descent: INTEGER
- do
- int_canvas_get_font_dim(cnvs, $max_width, $height, $ascent, $descent)
- Result := [max_width, height, ascent, descent]
- end
- get_wd_font_dim: TUPLE[REAL_64, REAL_64, REAL_64, REAL_64]
- -- Returns (in this order) the maximum width of a character, the
- -- line's height, the ascent and descent of the characters of the
- -- currently selected font. The line's height is the sum of the ascent
- -- and descent of a given additional space (if this is the case).
- -- All values are given in millimeters and are positive.
- local
- max_width, height, ascent, descent: REAL_64
- do
- int_wd_canvas_get_font_dim(cnvs, $max_width, $height, $ascent, $descent)
- Result := [max_width, height, ascent, descent]
- end
- get_text_size (text: STRING): TUPLE[INTEGER, INTEGER]
- -- Returns the text size independent from orientation.
- local
- w, h: INTEGER
- do
- int_canvas_get_text_size(cnvs, text.to_external, $w, $h)
- Result := [w, h]
- end
- get_wd_text_size (text: STRING): TUPLE[REAL_64, REAL_64]
- -- Returns the text size, in millimeters, independent from orientation.
- local
- w, h: REAL_64
- do
- int_wd_canvas_get_text_size(cnvs, text.to_external, $w, $h)
- Result := [w, h]
- end
- get_text_bounds (x, y: INTEGER; text: STRING): TUPLE[INTEGER, INTEGER, INTEGER, INTEGER, INTEGER, INTEGER, INTEGER, INTEGER]
- -- Returns the oriented bounding rectangle occupied by a text at a given
- -- position. The rectangle has the same dimentions returned by feature
- -- get_text_size. The rectangle corners are returned in counter-clock
- -- wise order starting with the bottom left corner, arranged
- -- (x0,y0,x1,y1,x2,y2,x3,y3).
- local
- points: NATIVE_ARRAY[INTEGER]
- do
- points := points.calloc(8)
- points.set_all_with(0, 7)
- int_canvas_get_text_bounds(cnvs, x, y, text.to_external, points.to_external)
- Result := [points.item(0), points.item(1),
- points.item(2), points.item(3),
- points.item(4), points.item(5),
- points.item(6), points.item(7)]
- end
- get_text_bounds_real (x, y: REAL_64; text: STRING): TUPLE[REAL_64, REAL_64, REAL_64, REAL_64, REAL_64, REAL_64, REAL_64, REAL_64]
- -- Like 'get_text_bounds' but with REAL_64 values.
- local
- points: NATIVE_ARRAY[REAL_64]
- do
- points := points.calloc(8)
- points.set_all_with(0, 7)
-
- int_canvas_c_double_get_text_bounds(cnvs, x, y, text.to_external, points.to_external)
- Result := [points.item(0), points.item(1),
- points.item(2), points.item(3),
- points.item(4), points.item(5),
- points.item(6), points.item(7)]
- end
- get_wd_text_bounds (x, y: REAL_64; text: STRING): TUPLE[REAL_64, REAL_64, REAL_64, REAL_64, REAL_64, REAL_64, REAL_64, REAL_64]
- -- Like 'get_text_bounds' but using World Coordinates.
- local
- p: POINTER
- points: NATIVE_ARRAY[REAL_64]
- do
- points := points.calloc(8)
- points.set_all_with(0, 7)
-
- int_wd_canvas_get_text_bounds(cnvs, x, y, text.to_external, points.to_external)
- Result := [points.item(0), points.item(1),
- points.item(2), points.item(3),
- points.item(4), points.item(5),
- points.item(6), points.item(7)]
- end
- get_text_box (x, y: INTEGER; text: STRING): TUPLE[INTEGER, INTEGER, INTEGER, INTEGER]
- -- Returns the horizontal bounding rectangle occupied by a text at a
- -- given position. If orientation is not 0 then its area is always larger
- -- than the area of the rectangle returned by 'get_text_bounds'.
- local
- xmin, xmax, ymin, ymax: INTEGER
- do
- int_canvas_get_text_box(cnvs, x, y, text.to_external, $xmin, $xmax, $ymin, $ymax)
- Result := [xmin, xmax, ymin, ymax]
- end
- get_text_box_real (x, y: REAL_64; text: STRING): TUPLE[REAL_64, REAL_64, REAL_64, REAL_64]
- -- Like 'get_text_box' but using REAL_64 values.
- local
- xmin, xmax, ymin, ymax: REAL_64
- do
- int_canvas_c_double_get_text_box(cnvs, x, y, text.to_external, $xmin, $xmax, $ymin, $ymax)
- Result := [xmin, xmax, ymin, ymax]
- end
- get_wd_text_box (x, y: REAL_64; text: STRING): TUPLE[REAL_64, REAL_64, REAL_64, REAL_64]
- -- Like 'get_text_box' but using World Coordinates.
- local
- xmin, xmax, ymin, ymax: REAL_64
- do
- int_wd_canvas_get_text_box(cnvs, x, y, text.to_external, $xmin, $xmax, $ymin, $ymax)
- Result := [xmin, xmax, ymin, ymax]
- end
-
- feature {}
- -- Convert
- styles_to_int (styles: ARRAY[STRING]): INTEGER
- local
- i, x: INTEGER
- style: STRING
- do
- i := 0
- from
- x := 0
- invariant
- valid_style: is_valid_style (styles.item(x))
- until
- x = styles.count
- loop
- style := styles.item(x)
-
- if style.is_equal("CD_PLAIN") then
- i := i | 0
- elseif style.is_equal("CD_BOLD") then
- i := i | 1
- elseif style.is_equal("CD_ITALIC") then
- i := i | 2
- elseif style.is_equal("CD_UNDERLINE") then
- i := i | 4
- elseif style.is_equal("CD_STRIKEOUT") then
- i := i | 8
- end
- x := x + 1
- end
- Result := i
- end
- alignment_to_int (value: STRING): INTEGER
- do
- if value.is_equal("CD_NORTH") then
- Result := 0
- elseif value.is_equal("CD_SOUTH") then
- Result := 1
- elseif value.is_equal("CD_EAST") then
- Result := 2
- elseif value.is_equal("CD_WEST") then
- Result := 3
- elseif value.is_equal("CD_NORTH_EAST") then
- Result := 4
- elseif value.is_equal("CD_NORTH_WEST") then
- Result := 5
- elseif value.is_equal("CD_SOUTH_EAST") then
- Result := 6
- elseif value.is_equal("CD_SOUTH_WEST") then
- Result := 7
- elseif value.is_equal("CD_CENTER") then
- Result := 8
- elseif value.is_equal("CD_BASE_LEFT") then
- Result := 9
- elseif value.is_equal("CD_BASE_CENTER") then
- Result := 10
- elseif value.is_equal("CD_BASE_RIGHT") then
- Result := 11
- end
- end
- int_to_alignment (value: INTEGER): STRING
- do
- if value.is_equal(0) then
- Result := "CD_NORTH"
- elseif value.is_equal(1) then
- Result := "CD_SOUTH"
- elseif value.is_equal(2) then
- Result := "CD_EAST"
- elseif value.is_equal(3) then
- Result := "CD_WEST"
- elseif value.is_equal(4) then
- Result := "CD_NORTH_EAST"
- elseif value.is_equal(5) then
- Result := "CD_NORTH_WEST"
- elseif value.is_equal(6) then
- Result := "CD_SOUTH_EAST"
- elseif value.is_equal(7) then
- Result := "CD_SOUTH_WEST"
- elseif value.is_equal(8) then
- Result := "CD_CENTER"
- elseif value.is_equal(9) then
- Result := "CD_BASE_LEFT"
- elseif value.is_equal(10) then
- Result := "CD_BASE_CENTER"
- elseif value.is_equal(11) then
- Result := "CD_BASE_RIGHT"
- end
- end
-
- -- Validations
-
- is_valid_style (style: STRING): BOOLEAN
- do
- if style.is_equal("CD_PLAIN") or
- style.is_equal("CD_BOLD") or
- style.is_equal("CD_ITALIC") or
- style.is_equal("CD_UNDERLINE") or
- style.is_equal("CD_STRIKEOUT") then
- Result := True
- else
- Result := False
- end
- end
- is_valid_alignment (value: STRING): BOOLEAN
- do
- if value.is_equal("CD_NORTH") or
- value.is_equal("CD_SOUTH") or
- value.is_equal("CD_EAST") or
- value.is_equal("CD_WEST") or
- value.is_equal("CD_NORTH_EAST") or
- value.is_equal("CD_NORTH_WEST") or
- value.is_equal("CD_SOUTH_EAST") or
- value.is_equal("CD_SOUTH_WEST") or
- value.is_equal("CD_CENTER") or
- value.is_equal("CD_BASE_LEFT") or
- value.is_equal("CD_BASE_CENTER") or
- value.is_equal("CD_BASE_RIGHT") then
- Result := True
- else
- Result := False
- end
- end
- -- Internals
- int_canvas_text (wgt: POINTER; x, y: INTEGER; text: POINTER)
- external "plug_in"
- alias "{
- location: "${sys}/plugins"
- module_name: "iup"
- feature_name: "cdCanvasText"
- }"
- end
- int_canvas_c_double_text (wgt: POINTER; x, y: REAL_64; text: POINTER)
- external "plug_in"
- alias "{
- location: "${sys}/plugins"
- module_name: "iup"
- feature_name: "cdfCanvasText"
- }"
- end
- int_wd_canvas_text (wgt: POINTER; x, y: REAL_64; text: POINTER)
- external "plug_in"
- alias "{
- location: "${sys}/plugins"
- module_name: "iup"
- feature_name: "wdCanvasText"
- }"
- end
- int_canvas_font (wgt, f: POINTER; tf, fs: INTEGER)
- external "plug_in"
- alias "{
- location: "${sys}/plugins"
- module_name: "iup"
- feature_name: "cdCanvasFont"
- }"
- end
- int_wd_canvas_font (wgt, f: POINTER; tf: INTEGER; fs: REAL_64)
- external "plug_in"
- alias "{
- location: "${sys}/plugins"
- module_name: "iup"
- feature_name: "wdCanvasFont"
- }"
- end
- int_canvas_get_font (wgt, f, tf, fs: POINTER)
- external "plug_in"
- alias "{
- location: "${sys}/plugins"
- module_name: "iup"
- feature_name: "cdCanvasGetFont"
- }"
- end
- int_wd_canvas_get_font (wgt, f, tf, fs: POINTER)
- external "plug_in"
- alias "{
- location: "${sys}/plugins"
- module_name: "iup"
- feature_name: "wdCanvasGetFont"
- }"
- end
- int_canvas_native_font (wgt, sf: POINTER): POINTER
- external "plug_in"
- alias "{
- location: "${sys}/plugins"
- module_name: "iup"
- feature_name: "cdCanvasNativeFont"
- }"
- end
- int_canvas_text_alignment (wgt: POINTER; algn: INTEGER): INTEGER
- external "plug_in"
- alias "{
- location: "${sys}/plugins"
- module_name: "iup"
- feature_name: "cdCanvasTextAlignment"
- }"
- end
- int_canvas_text_orientation (wgt: POINTER; ang: REAL_64): REAL_64
- external "plug_in"
- alias "{
- location: "${sys}/plugins"
- module_name: "iup"
- feature_name: "cdCanvasTextOrientation"
- }"
- end
- int_canvas_get_font_dim (wgt, mw, h, a, d: POINTER)
- external "plug_in"
- alias "{
- location: "${sys}/plugins"
- module_name: "iup"
- feature_name: "cdCanvasGetFontDim"
- }"
- end
- int_wd_canvas_get_font_dim (wgt, mw, h, a, d: POINTER)
- external "plug_in"
- alias "{
- location: "${sys}/plugins"
- module_name: "iup"
- feature_name: "wdCanvasGetFontDim"
- }"
- end
- int_canvas_get_text_size (wgt, text, w, h: POINTER)
- external "plug_in"
- alias "{
- location: "${sys}/plugins"
- module_name: "iup"
- feature_name: "cdCanvasGetTextSize"
- }"
- end
- int_wd_canvas_get_text_size (wgt, text, w, h: POINTER)
- external "plug_in"
- alias "{
- location: "${sys}/plugins"
- module_name: "iup"
- feature_name: "wdCanvasGetTextSize"
- }"
- end
- int_canvas_get_text_bounds (wgt: POINTER; x, y: INTEGER; text, r: POINTER)
- external "plug_in"
- alias "{
- location: "${sys}/plugins"
- module_name: "iup"
- feature_name: "cdCanvasGetTextBounds"
- }"
- end
- int_canvas_c_double_get_text_bounds (wgt: POINTER; x, y: REAL_64; text, r: POINTER)
- external "plug_in"
- alias "{
- location: "${sys}/plugins"
- module_name: "iup"
- feature_name: "cdfCanvasGetTextBounds"
- }"
- end
- int_wd_canvas_get_text_bounds (wgt: POINTER; x, y: REAL_64; text, r: POINTER)
- external "plug_in"
- alias "{
- location: "${sys}/plugins"
- module_name: "iup"
- feature_name: "wdCanvasGetTextBounds"
- }"
- end
- int_canvas_get_text_box (wgt: POINTER; x, y: INTEGER; text, xmin, xmax, ymin, ymax: POINTER)
- external "plug_in"
- alias "{
- location: "${sys}/plugins"
- module_name: "iup"
- feature_name: "cdCanvasGetTextBox"
- }"
- end
- int_canvas_c_double_get_text_box (wgt: POINTER; x, y: REAL_64; text, xmin, xmax, ymin, ymax: POINTER)
- external "plug_in"
- alias "{
- location: "${sys}/plugins"
- module_name: "iup"
- feature_name: "cdfCanvasGetTextBox"
- }"
- end
- int_wd_canvas_get_text_box (wgt: POINTER; x, y: REAL_64; text, xmin, xmax, ymin, ymax: POINTER)
- external "plug_in"
- alias "{
- location: "${sys}/plugins"
- module_name: "iup"
- feature_name: "wdCanvasGetTextBox"
- }"
- end
-
- end
- -- The MIT License (MIT)
- -- Copyright (c) 2017 by German A. Arias
- -- Permission is hereby granted, free of charge, to any person obtaining a copy
- -- of this software and associated documentation files (the "Software"), to deal
- -- in the Software without restriction, including without limitation the rights
- -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- -- copies of the Software, and to permit persons to whom the Software is
- -- furnished to do so, subject to the following conditions:
- --
- -- The above copyright notice and this permission notice shall be included in
- -- all copies or substantial portions of the Software.
- --
- -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- -- SOFTWARE.
|