JFLib

ui/imgview.rs#

Source: lib/ui/imgview.rs

imgview -- SVG pictures and the image viewer (ROADMAP §6)

  ui.svg(id, &doc, w, h) -> EV        an img_svg::Svg drawn sharp at its layout size: w / h in
       DIP (0 and 0: the intrinsic size, one 0: from the aspect ratio); currentColor is the
       text colour (icons follow the theme); clickable when id != 0; K_IMAGE record with
       the document's <title>.  Text in the SVG is set with the UI's fonts (SvgText).
  ui.image_view(id, &mut st, img, flags) -> EV    a zoomable, scrollable view of a bitmap
       (ViewImage::Bitmap(&Rc<Surface>, version)) or an SVG (ViewImage::Svg(&Rc<Svg>)),
       filling its parent unless next_size says otherwise.  EV_ACT when the zoom or the
       position changed (st.zoom, st.cx / cy, st.fit), EV_HOVER, EV_FOCUS, EV_DOUBLE.
       Mouse: the wheel zooms at the pointer (IV_WHEEL_SCROLL: the wheel scrolls, Ctrl +
       wheel zooms; Shift + wheel scrolls sideways), a left or middle drag pans, Ctrl + drag
       draws a rectangle to zoom into, a double click switches between 100 % (at the
       pointer) and fit.  Keys (focused): arrows pan an eighth of the view, PgUp / PgDn a
       page, Home / End to the top / bottom; + / - (with or without Ctrl) zoom in steps,
       Ctrl+0 fits, Ctrl+1 shows 100 %.  Scroll bars at the right and bottom edges when the
       image is larger than the view (IV_NO_BARS: none).
       Drawing: bitmaps through mip levels (2 x 2 box) and bilinear sampling below 400 %,
       nearest neighbour above (IV_SMOOTH: bilinear at every zoom), a pixel grid from 800 %
       (IV_NO_GRID: none); SVGs are drawn again at the zoom (only the visible part,
       currentColor black, IV_TEXT_COLOR: the text colour).  Transparent parts over a
       checkerboard (IV_NO_CHECKER: the view's background); the theme decides its colours
       (light, dark, high contrast: plain background).
       With a GPU the view's zoom and pan come from tiles while they run (canvas.rs, st.gpu: "image"),
       the CPU draws the picture a moment later.
  ImageViewState { zoom, cx, cy, fit, label }: zoom 1.0 = 100 % (a bitmap pixel on one
       screen pixel, an SVG at its intrinsic size in DIP); (cx, cy) the image point at the
       view's centre; fit: the zoom follows the view.  st.set_fit() / set_actual() /
       zoom_to(rect) / zoom_by(factor) / zoom_pct().  A new image (another Rc) fits.
  SvgText { text, fonts }: img_svg::TextHook on ui's text engine; SvgFonts picks the faces:
       families registered with ui.font_register, "monospace" = ui.mono_font_set's font,
       everything else the style's fonts (regular / semibold / bold; italic slanted).

Ids: id (the view: focus, K_IMAGE with st.label, the SVG's title or "Bild"; value = zoom %),
part(id, 1) / part(id, 2) the vertical / horizontal scroll bar.
/// image_view flags
IV_WHEEL_SCROLL: u32 = 1  // the wheel scrolls, Ctrl + wheel zooms
IV_NO_GRID: u32 = 2  // no pixel grid at high zoom
IV_SMOOTH: u32 = 4  // bilinear at every zoom (no visible pixels)
IV_NO_BARS: u32 = 8  // no scroll bars
IV_NO_CHECKER: u32 = 16  // transparency over the view background
IV_TEXT_COLOR: u32 = 32  // an SVG's currentColor is the text colour (else black)
  • struct SvgFonts the faces SvgText may use (taken from the Ui at frame time)

    • faces: Vec<(String, u32, bool, u32)> -- (family lower case, weight, italic, font id) of ui.font_register
    • mono: Option<u32> -- "monospace"
  • struct SvgText<'a> an img_svg::TextHook on a text engine

    • text: &'a mut Text
    • fonts: &'a SvgFonts

impl Ui

  • fn svg_fonts(&mut self) -> Rc<SvgFonts> the faces for SVG text (registered families, the monospace font); shared until the registered fonts change

  • fn svg(&mut self, id: u32, doc: &Rc<Svg>, w: i32, h: i32) -> u32 ui_svg(id, doc, w, h) -> EV: an SVG at w x h DIP (see the module header of imgview.rs)

  • enum ViewImage<'a> what ui.image_view shows

    • Bitmap(&'a Rc<Surface>, u32) -- a bitmap (FMT_PARGB, FMT_ARGB or FMT_XRGB) and its version (bump it when the pixels change)
    • Svg(&'a Rc<Svg>)
  • struct ImageViewState the app-owned state of an image view (see the module header of imgview.rs)

    • zoom: f64 -- 1.0 = 100 %
    • cx: f64 -- the image point (image px) at the centre of the view
    • cy: f64
    • fit: bool -- the zoom follows the view: the whole image, centred
    • label: String -- the accessible name (empty: the SVG's title, else "Bild")
    • gpu: CanvasGpu -- how the view's zoom and pan use the GPU (canvas.rs)

impl ImageViewState

  • fn zoom_pct(&self) -> i32 the zoom in percent (rounded)
  • fn set_fit(&mut self) fit mode: the whole image in the view
  • fn set_actual(&mut self) 100 %, the centre kept
  • fn zoom_by(&mut self, f: f64) multiply the zoom (about the view's centre)
  • fn zoom_to(&mut self, r: [f64; 4]) show the image rectangle x, y, w, h (image px) as large as the view allows

impl Ui

  • fn image_view(&mut self, id: u32, st: &mut ImageViewState, img: ViewImage, flags: u32) -> u32 ui_image_view(id, st, img, flags) -> EV: the image viewer (see the module header of imgview.rs)

Source: docs/API.md in the JFLib repository.